STM32中断向量表偏移地址配置方法

1-------------------------------------------------------------------------

from:   https://blog.csdn.net/gengyiping18/article/details/50735358

ST公司重定位向量表的库函数:

void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset){ 

  assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));

  assert_param(IS_NVIC_OFFSET(Offset));     

  SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);

}

其中NVIC_VectTab要么是FLASH要么是RAM的起始位置,Offset: Vector Table base offset field. This value must be a multiple of 0x200,这里先是IS_NVIC_OFFSET(OFFSET)  ((OFFSET) < 0x000FFFFF)断言机制,ST公司技术支持给我的回信是这么说的“The max flash size is 1MB, that is 0x100000, so the vector table must be placed within this address range, so ((OFFSET) < 0x000FFFFF) is checked.”f10x 内置flash最大也就512K,SRAM内置是64k,并没有看到官方人员说的1MB,我想这些断言机制恐怕也是为了给很多芯片共同使用而写的,也就是说实际还是要自己小心着用啊~

然后(Offset & (uint32_t)0x1FFFFF80)事实上就是取了Offset的[28:7]位。但是你还是需要人为让其为0x200的倍数,至于为什么,在ARM官方给出的Cortex-m3 technial reference manul中是这么说的:

The Vector Table Offset Register positions the vector table in CODE or SRAM space. 

The default, on reset, is 0 (CODE space). When setting a position, the offset must be 

aligned based on the number of exceptions in the table. This means that the minimal 

alignment is 32 words that you can use for up to 16 interrupts. For more interrupts, you 

must adjust the alignment by rounding up to the next power of two. For example, if you 

require 21 interrupts, the alignment must be on a 64-word boundary because table size 

is 37 words, next power of two is 64.

所以由于人家规定要对齐向量表,由于stm32的中断向量一共有68+16=84个,应该把这个数增加到下一个2的整数倍即128,然后换算成地址范围128*4=512,就得到了0x200

2---------------------------------------------------------------------------------------------------------

from:https://blog.csdn.net/Tommy666666/article/details/80256174

以将中断向量表偏移地址改到0x8005000为例

第一种方式, 寄存器法:

设置system_stm32f1xx.c文件  #defineVECT_TAB_OFFSET  0x5000 

在void SystemInit (void)函数中通过“SCB->VTOR =FLASH_BASE | VECT_TAB_OFFSET”命令实现地址偏移

第二种方式 库函数法:

运用库函数时,可以在main()函数开头,加上

  NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x5000);__ASM("CPSIE  I"); 命令实现地址偏移

NVIC_VectTab_FLASH 和 FLASH_BASE 都为 0x08000000

三  相对应的链接配置设置

要实现这个功能,还需要在MDK的option->target 配置IROM1为0x8005000,如图

猜你喜欢

转载自blog.csdn.net/twd_1991/article/details/82707596