本帖最后由 盛夏之蝉 于 2015-4-29 17:42 编辑
—————————————————————————————————————————————————————————————
core_cm3.h
—————————————————————————————————————————————————————————————
core_cm3.h文件是和内核紧密相关的功能模块类型封装,有NVIC,SYSTICK,SCB,ITM等,这些模块的内容的具体介绍是在Cortex-M3 权威指南(建议看英文的,我最开始看的是宋岩翻译 的中文版,但是有很多错误和模糊的地方,最后还是看的英文的)里.和我们应用紧密相关的是NVIC,SYSTICK,这篇主要讲解NVIC,SYSTICK。
首先 NVIC:
typedef struct
{
__IO uint32_t ISER[8]; /*!< Offset: 0x000 中断使能寄存器 ,写1置位,写0无效*/
uint32_t RESERVED0[24];
__IO uint32_t ICER[8]; /*!< Offset: 0x080 中断 清除使能寄存器,写1清0,写0无效 */
uint32_t RSERVED1[24];
__IO uint32_t ISPR[8]; /*!< Offset: 0x100 中断悬起寄存器 */
uint32_t RESERVED2[24];
__IO uint32_t ICPR[8]; /*!< Offset: 0x180 中断解悬寄存器 */
uint32_t RESERVED3[24];
__IO uint32_t IABR[8]; /*!< Offset: 0x200 中断活动状态寄存器,只读类型,与中断嵌套相关 */
uint32_t RESERVED4[56];
__IO uint8_t IP[240]; /*!< Offset: 0x300 中断优先级寄存器*/
uint32_t RESERVED5[644];
__O uint32_t STIR; /*!< Offset: 0xE00 软件触发中断寄存器 */
} NVIC_Type
与NVIC相关的库函数有 12个:
1)、static __INLINE void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) //设置优先级组
{
uint32_t reg_value;
uint32_t PriorityGroupTmp = (PriorityGroup & 0x07); /* 优先级组只有3bit位,故可以用的也就0到7这8个级别*/
reg_value = SCB->AIRCR; /* 先读出AIRCR的内容*/
reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); /*先清0*/
reg_value = (reg_value | (0x5FA AIRCR & SCB_AIRCR_PRIGROUP_Msk) >> SCB_AIRCR_PRIGROUP_Pos);
}
3)、static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn) //设置外部中断使能 ,代码简练,优美
{
NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 ICER[((uint32_t)(IRQn) >> 5)] = (1 ISPR[(uint32_t)(IRQn) >> 5] & (1 ISPR[((uint32_t)(IRQn) >> 5)] = (1 ICPR[((uint32_t)(IRQn) >> 5)] = (1 IABR[(uint32_t)(IRQn) >> 5] & (1 SHP[((uint32_t)(IRQn) & 0xF)-4 = ((priority > (8 - __NVIC_PRIO_BITS))); }
else {//中断
return((uint32_t)(NVIC->IP[(uint32_t)(IRQn)] >> (8 - __NVIC_PRIO_BITS))); }
}
11)、static __INLINE uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority) //根据指定的优先级组,对给定的抢占优先级PreemptPriority,子优先级SubPriority 进行编码,组合成一个优先级数字 ,代码很有趣
{
uint32_t PriorityGroupTmp = (PriorityGroup & 0x07); /*优先级组只有低3位可用*/
uint32_t PreemptPriorityBits;
uint32_t SubPriorityBits;
PreemptPriorityBits = ((7 - PriorityGroupTmp) > __NVIC_PRIO_BITS) ? __NVIC_PRIO_BITS : 7 - PriorityGroupTmp;
SubPriorityBits = ((PriorityGroupTmp + __NVIC_PRIO_BITS) < 7) ? 0 : PriorityGroupTmp - 7 + __NVIC_PRIO_BITS;
return (
((PreemptPriority & ((1 SubPriorityBits) & ((1 LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /*减到0,所以需要减1 */
NVIC_SetPriority (SysTick_IRQn, (1CTRL = SysTick_CTRL_CLKSOURCE_Msk | //使用FCLOk作为systick的时钟源,,默认是72M
SysTick_CTRL_TICKINT_Msk | //到0产生异常
SysTick_CTRL_ENABLE_Msk; /*使能*/
return (0);
}
————————————————————————————————————————————————————————
这些天把st公司给的库源码研究完了,做了相应的笔记,只是有惰性没有写成贴子,以后努力把所有的文件都写成帖子。