gateway_mcu/CH32V303-FreeRTOS/App/hardwareDriver/Src/HD_Flash.c

61 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Flash.c
*
* Created on: 2024年5月16日
* Author: 34509
*/
#include "HD_Flash.h"
#include "pDebug.h"
uint8_t readFlash(uint32_t* pBuffer,uint32_t ReadAddr,uint16_t dataLen)
{
for (int i = 0; i < dataLen; i++) {
*(pBuffer + i) = *(uint32_t*)(ReadAddr + 4 * i);
}
return 1;
}
/*
* @brief 将数据写入flash的某个闪存页WriteAddr必须为某个页的开头
* @param pBuffer 要写入的数据
WriteAddr 要写入的地址
NumByteToWrite 要写入的长度
* @retval
*
*/
uint8_t writeFlash(uint32_t* pBuffer,uint32_t WriteAddr, uint16_t NumByteToWrite)
{
/* 擦除某个闪存页 */
int ret = FLASH_ROM_ERASE(WriteAddr, 256);
if (ret != FLASH_COMPLETE) {
return 0;
}
uint32_t buf[64] = {0};
memcpy(buf, pBuffer, (NumByteToWrite / 4));
ret = FLASH_ROM_WRITE(WriteAddr, buf, 256);
if (ret != FLASH_COMPLETE) {
return 0;
}
// FLASH_Unlock();
// for (int i = 0; i < NumByteToWrite; i++) {
// ret = FLASH_ProgramOptionByteData((uint32_t)((uint8_t *)WriteAddr + 1), *(pBuffer + i));
// if (ret != FLASH_COMPLETE) {
// FLASH_Lock();
// return 0;
// }
// }
// FLASH_Lock();
return 1;
}