本帖最后由 风萧寒 于 2016-11-13 14:58 编辑
在 STM8L151G6U6中可支持字节擦写和块擦写,块擦写可一次擦写很所字节。字节擦写比较简单,需要注意的是块擦写,在块擦写中,需要配置一个地方。
对于IAR来说,
Uncomment the line “#define RAM_EXECUTION (1)” in the stm8l15x.h file to enable the FLASH functions execution from RAM through the specific __ramfunc keyword.
之后可在程序中调用Flash_BlockProgram();来进行块擦写。
在main函数前要做以下声明: #ifdef _RAISONANCE_/* needed by memcpy for raisonance */#include <string.h>extern int __address__FLASH_EraseBlock;extern int __size__FLASH_EraseBlock;extern int __address__FLASH_ProgramBlock;extern int __size__FLASH_ProgramBlock;#endif /*_RAISONANCE_*/ /* Private function prototypes -----------------------------------------------*/ /* Declare _fctcpy function prototype as it is packaged by default in the Cosmicmachine library */#ifdef _COSMIC_int _fctcpy(char name);#endif /*_COSMIC_*/之后可使用下面函数来进行擦写到epprom中, void Flash_WriteDataBlock(uint8_t block_count, uint8_t *Buffer){ FLASH_Unlock(FLASH_MemType_Data);//可以擦写EEPROM或Flash:FLASH_Unlock(FLASH_MemType_Program); while (FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET) {} FLASH_ProgramBlock(block_count, FLASH_MemType_Data, FLASH_ProgramMode_Standard, Buffer); while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET) {} //FLASH_WaitForLastOperation(); FLASH_Lock(FLASH_MemType_Data);} 之后编译即可,不知道是否要将工程设置为支持C标准库,可能要,测试成功的工程是做了这个设置的,大家也可以自己做一下测试 |