关于这个函数虽然调用了很多子函数,但是貌似都是一些简单的操作,下面查看FLASH初始化
void HalFlashInit(void)
{
// Load the code to run from RAM into its reserved area of RAM once at startup.
HalFlashRead(PAGE_OF_RAM_CODE, OSET_OF_RAM_CODE, ramCode, SIZE_OF_RAM_CODE);
}
看注释是下载代码到RAM中
#define OSET_OF_RAM_CODE 0x5DD
#define PAGE_OF_RAM_CODE 51
#define SIZE_OF_RAM_CODE 0x23
static __no_init uint8 ramCode[SIZE_OF_RAM_CODE];
注释上说Z_Stack用的地址是0x9DDD-->pg 51, offset 0x5DD,重点还是HalFlashRead()函数
void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt)
{
// Calculate the offset into the containing flash bank as it gets mapped into XDATA.
uint8 *ptr = (uint8 *)(offset + HAL_FLASH_PAGE_MAP) +
((pg % HAL_FLASH_PAGE_PER_BANK) * HAL_FLASH_PAGE_SIZE);
uint8 memctr = MEMCTR; // Save to restore.
#if !defined HAL_OAD_BOOT_CODE
halIntState_t is;
#endif
pg /= HAL_FLASH_PAGE_PER_BANK; // Calculate the flash bank from the flash page.
#if !defined HAL_OAD_BOOT_CODE
HAL_ENTER_CRITICAL_SECTION(is);
#endif
// Calculate and map the containing flash bank into XDATA.
MEMCTR = (MEMCTR & 0xF8) | pg;
while (cnt--)
{
*buf++ = *ptr++;
}
MEMCTR = memctr;
#if !defined HAL_OAD_BOOT_CODE
HAL_EXIT_CRITICAL_SECTION(is);
#endif
}
拷贝cnt个字节数据到buf中,这里也涉及到存储器知识,以及存储器映射机制,
// CODE banks get mapped into the XDATA range 8000-FFFF.
#define HAL_FLASH_PAGE_MAP 0x8000
// Flash is partitioned into 8 banks of 32 KB or 16 pages.
#define HAL_FLASH_PAGE_PER_BANK 16
// Flash is constructed of 128 pages of 2 KB.
#define HAL_FLASH_PAGE_SIZE 2048
首先计算地址偏移量,分为8个banks,每一个bank占用32KB,又分为16个pages,一个page占用空间就是2KB了,保存MEMCTR的值,MEMCTR是存储器仲裁控制器
利用pg得到bank的个数,然后