| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #if 0
- #include "bsp_V8M_flash.h"
- #include "stm32f4xx.h"
- #define IAP_FLAG_ADDR ((uint32_t)0x0800C000)
- #define IPA_FLAG_NEED_UPDATE ((uint16_t)0xABCD)
- // FLASH扇区的基地址
- #define ADDR_FLASH_SECTOR_0 \
- ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbyte */
- #define ADDR_FLASH_SECTOR_1 \
- ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbyte */
- #define ADDR_FLASH_SECTOR_2 \
- ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbyte */
- #define ADDR_FLASH_SECTOR_3 \
- ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbyte */
- #define ADDR_FLASH_SECTOR_4 \
- ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbyte */
- #define ADDR_FLASH_SECTOR_5 \
- ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbyte */
- #define ADDR_FLASH_SECTOR_6 \
- ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbyte */
- #define ADDR_FLASH_SECTOR_7 \
- ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbyte */
- #define ADDR_FLASH_SECTOR_8 \
- ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbyte */
- #define ADDR_FLASH_SECTOR_9 \
- ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbyte */
- #define ADDR_FLASH_SECTOR_10 \
- ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbyte */
- #define ADDR_FLASH_SECTOR_11 \
- ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbyte */
- // FLASH的结束地址
- #define USER_FLASH_END_ADDRESS 0x080FFFFF
- //用户用来存储APP的FLASH大小
- #define USER_FLASH_SIZE (USER_FLASH_END_ADDRESS - APPLICATION_ADDRESS + 1)
- //用户APP起始地址
- #define APPLICATION_ADDRESS (uint32_t)0x08010000
- /**
- * @brief 根据 mcu flash 地址计算 所在扇区
- */
- static uint32_t GetSector(uint32_t Address)
- {
- uint32_t sector = 0;
- if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
- {
- sector = FLASH_Sector_0;
- }
- else if ((Address < ADDR_FLASH_SECTOR_2) &&
- (Address >= ADDR_FLASH_SECTOR_1))
- {
- sector = FLASH_Sector_1;
- }
- else if ((Address < ADDR_FLASH_SECTOR_3) &&
- (Address >= ADDR_FLASH_SECTOR_2))
- {
- sector = FLASH_Sector_2;
- }
- else if ((Address < ADDR_FLASH_SECTOR_4) &&
- (Address >= ADDR_FLASH_SECTOR_3))
- {
- sector = FLASH_Sector_3;
- }
- else if ((Address < ADDR_FLASH_SECTOR_5) &&
- (Address >= ADDR_FLASH_SECTOR_4))
- {
- sector = FLASH_Sector_4;
- }
- else if ((Address < ADDR_FLASH_SECTOR_6) &&
- (Address >= ADDR_FLASH_SECTOR_5))
- {
- sector = FLASH_Sector_5;
- }
- else if ((Address < ADDR_FLASH_SECTOR_7) &&
- (Address >= ADDR_FLASH_SECTOR_6))
- {
- sector = FLASH_Sector_6;
- }
- else if ((Address < ADDR_FLASH_SECTOR_8) &&
- (Address >= ADDR_FLASH_SECTOR_7))
- {
- sector = FLASH_Sector_7;
- }
- else if ((Address < ADDR_FLASH_SECTOR_9) &&
- (Address >= ADDR_FLASH_SECTOR_8))
- {
- sector = FLASH_Sector_8;
- }
- else if ((Address < ADDR_FLASH_SECTOR_10) &&
- (Address >= ADDR_FLASH_SECTOR_9))
- {
- sector = FLASH_Sector_9;
- }
- else if ((Address < ADDR_FLASH_SECTOR_11) &&
- (Address >= ADDR_FLASH_SECTOR_10))
- {
- sector = FLASH_Sector_10;
- }
- else if ((Address < USER_FLASH_END_ADDRESS) &&
- (Address >= ADDR_FLASH_SECTOR_11))
- {
- sector = FLASH_Sector_11;
- }
- return sector;
- }
- /**
- * @brief mcu flash 初始化,写入操作前调用,会解锁 mcu flash, 清除标志位
- */
- void V8M_McuFlashInit(void)
- {
- //解锁
- FLASH_Unlock();
- //清除flash待处理标志位
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
- FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
- }
- /**
- * @brief 从 mcu flash 读取 N 个字节
- */
- int V8M_McuFlashReadNbytes(uint32_t ReadAddress, uint8_t *ReadBuf,
- int32_t ReadNum)
- {
- int DataNum = 0;
- while (DataNum < ReadNum)
- {
- *(ReadBuf + DataNum) = *(__IO uint8_t *)ReadAddress++;
- DataNum++;
- }
- return DataNum;
- }
- /**
- * @brief 往 mcu flash 指定地址写入 2 字节类型数据
- */
- void V8M_McuFlashWriteHalfWord(uint32_t FlashAddress, uint16_t data)
- {
- while (FLASH_EraseSector(GetSector(FlashAddress), VoltageRange_3) !=
- FLASH_COMPLETE)
- ;
- FLASH_ProgramHalfWord(FlashAddress, data);
- }
- /**
- * @brief mcu flash 擦除页
- */
- void McuFlahErasePage() {}
- /**
- * @brief mcu flash 上锁,写入结束后执行
- */
- void V8M_McuFlashLock(void) { FLASH_Lock(); }
- /**
- * @brief 清除升级标志
- */
- void V8M_clear_iap_flag(void)
- {
- __disable_irq();
- V8M_McuFlashInit();
- V8M_McuFlashWriteHalfWord(IAP_FLAG_ADDR, (uint16_t)0xFFFF);
- V8M_McuFlashLock();
- __enable_irq();
- }
- /**
- * @brief 记录升级标志
- */
- void V8M_record_iap_flag(void)
- {
- __disable_irq();
- V8M_McuFlashInit();
- V8M_McuFlashWriteHalfWord(IAP_FLAG_ADDR, IPA_FLAG_NEED_UPDATE);
- V8M_McuFlashLock();
- __enable_irq();
- }
- /**
- * @brief 检查升级标志位
- * @note 主控的升级标志位位于 mcu 板载存储中,地址 0x800C000, 需要升级标志为
- * 0xABCD
- */
- void V8M_check_iap_flag(void)
- {
- uint16_t iapFlag;
- V8M_McuFlashReadNbytes(IAP_FLAG_ADDR, (uint8_t *)&iapFlag, sizeof(iapFlag));
- /* 如果升级标志位写的是需要升级,则写为不需要升级 */
- if (iapFlag == IPA_FLAG_NEED_UPDATE)
- {
- V8M_clear_iap_flag();
- }
- }
- #endif
|