STM8L151 在IAR中實現Flash/EEPROM的擦寫
阿新 • • 發佈:2019-01-10
在 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 Cosmic
machine 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標準庫,可能要,測試成功的工程是做了這個設定的,大家也可以自己做一下測試