你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32文档中关于NVIC寄存器说明的位置

[复制链接]
STMCU小助手 发布时间:2022-1-17 22:09
下面,我们看看关于NVIC寄存器的描述,都在哪些手册里面有提到
在使用STM32F10x standard peripheral library写有关于stm32中断的程序的时候,需要开启某个特定中断的控制位,除了对应外设的寄存器之外,还需要设置NVIC的相关寄存器的对应位。例如:
  1. /******  STM32 specific Interrupt Numbers *********************************************************/
    ) m5 x% i) R% G4 E: B
  2.   WWDG_IRQn                   = 0,      /*!< Window WatchDog Interrupt                            */) U+ m& U: D; w. Y( `) L
  3.   PVD_IRQn                    = 1,      /*!< PVD through EXTI Line detection Interrupt            */9 O; D  n9 P0 C2 Z8 Y
  4.   TAMPER_IRQn                 = 2,      /*!< Tamper Interrupt                                     */
    / `3 o7 p. R$ c& q- D! ^
  5.   RTC_IRQn                    = 3,      /*!< RTC global Interrupt                                 */, a- Q% a1 f) m0 W
  6.   FLASH_IRQn                  = 4,      /*!< FLASH global Interrupt                               */
    8 f% B6 A4 [) A
  7.   RCC_IRQn                    = 5,      /*!< RCC global Interrupt                                 */3 H0 H: I, c# r* q
  8.   EXTI0_IRQn                  = 6,      /*!< EXTI Line0 Interrupt                                 */7 J7 w& H9 F- O" @: Q7 }8 `
  9.   EXTI1_IRQn                  = 7,      /*!< EXTI Line1 Interrupt                                 */# n5 @. {& J9 n2 o
  10.   EXTI2_IRQn                  = 8,      /*!< EXTI Line2 Interrupt                                 */+ g1 Z% P5 Q# U6 c, y. ?
  11.   EXTI3_IRQn                  = 9,      /*!< EXTI Line3 Interrupt                                 */3 N3 A7 |9 }! C
  12.   EXTI4_IRQn                  = 10,     /*!< EXTI Line4 Interrupt                                 */
    / l' b3 M2 C; s  H* r
  13.   DMA1_Channel1_IRQn          = 11,     /*!< DMA1 Channel 1 global Interrupt                      */
    " n: u+ U2 ]+ z: U
  14.   DMA1_Channel2_IRQn          = 12,     /*!< DMA1 Channel 2 global Interrupt                      */
    + i: |+ X) J0 I: H
  15.   DMA1_Channel3_IRQn          = 13,     /*!< DMA1 Channel 3 global Interrupt                      */
    ; L( u* f/ K- K" f8 B0 P: B, X" i
  16.   DMA1_Channel4_IRQn          = 14,     /*!< DMA1 Channel 4 global Interrupt                      */. ~' a9 e6 L: Y7 O
  17.   DMA1_Channel5_IRQn          = 15,     /*!< DMA1 Channel 5 global Interrupt                      */
    4 H/ H) X: t3 E" W; V8 k9 F
  18.   DMA1_Channel6_IRQn          = 16,     /*!< DMA1 Channel 6 global Interrupt                      */
    . Z& i9 P* T2 K4 o
  19.   DMA1_Channel7_IRQn          = 17,     /*!< DMA1 Channel 7 global Interrupt                      */
    # B# S5 i# i6 k4 M
  20. & r% Q. V$ r2 D+ B) D
  21.     NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;0 ~6 H, m9 C+ I) Q5 A- y  |: s
  22.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    * [7 c# O4 @4 t) Q: a
  23.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    3 Q: }3 ?  Z  W1 K& c
  24.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    . P+ q# A' {/ e7 @- H  r! U9 n* @) a. a
  25.     NVIC_Init(&NVIC_InitStructure);
复制代码

7 `7 ?' X% Y4 M7 A; j/ r# P
而NVIC_Init()这个函数:
  1. /**
    & s# l! s+ k1 s6 u0 x& ~8 A
  2.   * @brief  Initializes the NVIC peripheral according to the specified" n* Y7 ?! H3 I8 Z4 K
  3.   *         parameters in the NVIC_InitStruct.) m& k4 @% M" E& K8 S' l
  4.   * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains* O* P& _+ v/ p( U& @/ B
  5.   *         the configuration information for the specified NVIC peripheral.
    4 L1 b; B4 f) f. s
  6.   * @retval None% Y+ E8 C$ x6 E- e3 I
  7.   */4 q( y# Z- L. }! H0 U/ ?7 g
  8. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)9 [5 ?$ I$ I+ P# A" L  O& F
  9. {
    ; [/ s' F2 F" p
  10.   uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;' m: U, S5 @. i( Z! m. w
  11.   
    0 E. L1 Y9 c, A, k( O2 F1 x0 y2 Z
  12.   /* Check the parameters */
    8 Y0 n' W! j  t8 ^) p3 I0 |
  13.   assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
    0 w/ o4 q, Q' h7 `. ]* {( j
  14.   assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));  * ?+ Z4 H# t, l4 p
  15.   assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));! t4 B& Q( i# M& d) m
  16.     7 b" Q& W- i. x
  17.   if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)3 u( I2 \4 c$ U( E
  18.   {
    9 R$ ]* e+ Y7 l- x$ ?1 e
  19.     /* Compute the Corresponding IRQ Priority --------------------------------*/   
    # `* m, f3 E* ]: g  Q' m
  20.     tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
    / e1 v3 X' K9 v: P/ W. H! s
  21.     tmppre = (0x4 - tmppriority);# V: x. \$ Z$ s1 o
  22.     tmpsub = tmpsub >> tmppriority;
    : f3 v- q, |3 S% \+ h" m

  23. ( u5 f, Q6 G5 E" V0 ~
  24.     tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;. [, l; R1 _! W' H
  25.     tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
    " G, O. `  _& _" D
  26.     tmppriority = tmppriority << 0x04;
    1 j& X. e& b$ n
  27.         + ^! \% ?, V: b; P* [
  28.     NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
    : S1 r" q3 r5 k$ _2 z4 O- x
  29.     ; r) u! g. J3 J8 t: z
  30.     /* Enable the Selected IRQ Channels --------------------------------------*/5 X2 J% j4 C+ p& ]. V/ u
  31.     NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =2 G9 _# }8 Y3 L; T4 r
  32.       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);: Q6 j' Q% f5 y" A8 _0 P% T
  33.   }
    , E+ f1 A: ~, Q, X! z
  34.   else
    : E& i1 z3 d" F# l9 T1 e& J8 J
  35.   {8 m  K  R( Q2 G( I0 V. q  C
  36.     /* Disable the Selected IRQ Channels -------------------------------------*/- v7 w+ w3 _: ^7 k1 O
  37.     NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
    * m7 U) N( p6 q7 x0 c* g
  38.       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
    % Q; C7 a/ A6 B/ S
  39.   }
    ) F! a$ V- I9 {  G% h$ q& P
  40. }
复制代码
  E: R3 g' x' K; q! K) Y6 @- I
可以看到,它会去设置几个寄存器:NVIC的IP,ISER,ICER等等,NVIC是基址,为:
  1. #define SCS_BASE            (0xE000E000)                              /*!< System Control Space Base Address */# V8 M2 J# i1 G
  2. #define NVIC_BASE           (SCS_BASE +  0x0100)                      /*!< NVIC Base Address                 */# T3 n( u7 {! O
  3. #define SCB_BASE            (SCS_BASE +  0x0D00)                      /*!< System Control Block Base Address */" v% X; b& F8 k+ r) I/ I
  4. # n5 g% P3 e; z) [
  5. % K6 C  u% y  w; X
  6. #define NVIC                ((NVIC_Type *)          NVIC_BASE)        /*!< NVIC configuration struct         */
复制代码

& D1 \2 u5 J3 m3 w
NVIC_Type为:
  1. /** @addtogroup CMSIS_CM3_NVIC CMSIS CM3 NVIC$ f4 g+ s  M, i8 X: \! b3 G
  2.   memory mapped structure for Nested Vectored Interrupt Controller (NVIC)
    3 p) E+ y2 z( P7 o
  3.   @{1 J9 U$ ?# Q3 H. b0 a# v( m
  4. */
    5 `" y- ]5 I! \6 D6 q# n
  5. typedef struct
    2 G  L% V0 k& p6 A! Q' ]
  6. {
    ) ]# o4 v% g6 h0 O4 h
  7.   __IO uint32_t ISER[8];                      /*!< Offset: 0x000  Interrupt Set Enable Register           */
    ( h' C% l" f) y. N% e% C  G* T, @
  8.        uint32_t RESERVED0[24];                                   
    1 `4 k! l7 ]; q% E
  9.   __IO uint32_t ICER[8];                      /*!< Offset: 0x080  Interrupt Clear Enable Register         */
    8 j& p6 s6 u& {2 K5 [2 l9 e' B5 w0 Y
  10.        uint32_t RSERVED1[24];                                    % ~2 B! ?6 ^, h8 |( N
  11.   __IO uint32_t ISPR[8];                      /*!< Offset: 0x100  Interrupt Set Pending Register          */
    + I  V6 H, H8 d# n* t, Y
  12.        uint32_t RESERVED2[24];                                   
    . X# m  }0 A5 I% d
  13.   __IO uint32_t ICPR[8];                      /*!< Offset: 0x180  Interrupt Clear Pending Register        */
    " `; s# p5 L! y- l: \( [
  14.        uint32_t RESERVED3[24];                                   : x+ e$ B( D$ m2 q/ M' ^( z& h) R% O, w! k
  15.   __IO uint32_t IABR[8];                      /*!< Offset: 0x200  Interrupt Active bit Register           */
    1 X3 A- W! M, W3 k3 f5 C
  16.        uint32_t RESERVED4[56];                                   
    2 b+ L* X: L# V& w! y; H, ]
  17.   __IO uint8_t  IP[240];                      /*!< Offset: 0x300  Interrupt Priority Register (8Bit wide) */  \* y2 s' W" k% [! a! Q* Y3 s* X
  18.        uint32_t RESERVED5[644];                                 
    & ?; J' ~7 z+ m) M
  19.   __O  uint32_t STIR;                         /*!< Offset: 0xE00  Software Trigger Interrupt Register     */
    / K  N! r/ F- v5 k" P
  20. }  NVIC_Type;                                               1 v5 S/ C7 p" X$ A0 q% d
  21. /*@}*/ /* end of group CMSIS_CM3_NVIC */
复制代码

: {- W+ O% ]1 c
从上面可以了解到的是,NVIC的寄存器的基址是没有错的,《The Definitive Guide to the ARM Cortex-M3》 85页有个比较详细的内存图;或者查看《Cortex-M3 Technical Reference Manual Revision r1p1》3.4 System address map,6.3 NVIC programmers model:

  V6 ~/ [( f! q
2012091220202734.png

1 T7 C: A' E3 s0 C# e
以及:
) c, Z7 A8 \& Y* W: T, C4 ?
2012091220245925.png

: M) y. l3 ?6 g8 t4 y  U
B3.4 Nested Vectored Interrupt Controller (NVIC)的相关说明,例如:
: J* M; x, e3 Q! `; C# j, ~0 ?! F
2012091220290853.png

/ G1 X; d0 E0 i: u2 j( {7 j3 M
当然,你看权威指南也可以的,在其APPENDIX D:NVIC Registers Quick Reference里面也有所描述。
但还有一个问题,我怎么知道这个Interrupt Set-Enable Registers寄存器的哪个位对应哪个中断呢?例如NVIC_ISER0的第0位对应哪个呢,其实,上面的表B3-31的说明已经说了,每一位对应一个中断号,所以我们还得看看stm32F103对应的中断号,可以查阅《RM0008:Reference manual》的第10章,Interrupts and events中的中断向量表,找到对应MD容量的那个表Table 63. Vector table for other STM32F10xxx devices,前面16个M3内核的中断,不可以从这个寄存器里控制,所以不管,我们从WWDG开始,可以看到这个表中的第一列即对应于寄存器中的某位(超过31的自己换算一下即可):

1 b& e5 R1 T1 S, a2 i! A6 p
2012091220434360.png
! Z  }6 s" Q3 L2 ^$ N4 A( N
可以看到,STM32F10x standard peripheral library文件stm32f10x.h中的中断号的定义是能够与这里对应上的。
另外,stm32F103RB的Datasheet(CD00161566)里面的2.3.5 Nested vectored interrupt controller (NVIC)提到F103仅有43个可屏蔽中断,所以只需用到NVIC_ISER0和NVIC_ISER1来设置启用外设中断。
0 L* V# c! _) O4 z' g' q6 d
收藏 评论0 发布时间:2022-1-17 22:09

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版