请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:MSPM0G3507工具/软件:
您好、
我使用 MSPM0G3507 Launchpad 进行开发。我遇到了与闪存相关的示例代码。
我实现了闪存写入的基本驱动程序:
1.阅读整个行业。
2.修改字节
3.擦除扇区
4.更新行业
这是我使用的顺序。 我将共享代码片段。 它也在工作。
但我认为没有优化。 是否只需修改一个字节的任何其他方法?
#define FLASH_WORD_SIZE 8.
#define FLASH_SECTOR_SIZE 1024
/******************************************************************** */
int8_t Flash_Update 扇区 (uint32_t sectorAddr、uint8_t *newData)
{
DL_FLASHCTL_COMMAND_STATUS gCmdStatus;
//擦除扇区
DL_FlashCTL_executeClearStatus (FLASHCTL);
DL_FlashCTL_unprotectSector (FLASHCTL、sectorAddr、DL_FLASHCTL_REGION_SELECT_MAIN);
gCmdStatus = DL_FlashCTL_eraseMemoryFromRAM (FLASHCTL、sectorAddr、DL_FLASHCTL_COMMAND_SIZE_SECTOR);
If (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED)
返回 BOARD_FLASH_ERROR;
//编程后扇区(逐字编程)
对于 (uint32_t offset = 0;偏移< FLASH_SECTOR_SIZE;偏移+= FLASH_WORD_SIZE)
{
DL_FlashCTL_executeClearStatus (FLASHCTL);
DL_FlashCTL_unprotectSector (FLASHCTL、sectorAddr + OFFSET、DL_FLASHCTL_REGION_SELECT_MAIN);
gCmdStatus = DL_FlashCTL_programMemoryFromRAM64WithECCGenerated (FLASHCTL、sectorAddr + offset、
(uint32_t *)&newData[offset]);
If (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED)
返回 BOARD_FLASH_ERROR;
}
Return Board_FLASH_OK;
}
int8_t Flash_Write_Bytes(uint32_t 地址、uint8_t *pData、uint32_t 长度)
{
uint32_t sectorAddr = address 和~(FLASH_SECTOR_SIZE - 1);//扇区基址
uint8_t buffer[FLASH_SECTOR_size];
//将整个扇区复制到 RAM 缓冲区
memcpy (buffer、(void *) sectorAddr、FLASH_SECTOR_SIZE);
//修改所需的字节
对于 (uint32_t i = 0;i < length;i++)
{
Buffer[(address - sectorAddr)+ i]= pData[i];
}
//重新编程整个扇区
返回 Flash_Update 扇区 (sectorAddr、buffer);
}