2010년 9월 20일 월요일

internal flash write (stm32f103)

#define FLASH_PAGE_SIZE ((uint16_t)0x400)
//#define BANK1_WRITE_START_ADDR ((uint32_t)0x08008000)
//#define BANK1_WRITE_END_ADDR ((uint32_t)0x0800C000)

// Middle density
// Page 127 (0x0801FC00 ~ 0x0801FFFF : 1kbyte)
#define PAGE_START_ADDR ((uint32_t)0x0801FC00)
#define PAGE_END_ADDR ((uint32_t)0x08020000)

#if 0
// High density
// Page 127 (0x0803F800 ~ 0x0802FFFF : 2kbyte)
#define PAGE_START_ADDR ((uint32_t)0x0803F800)
#define PAGE_END_ADDR ((uint32_t)0x08030000)
#endif

void save_env(void)
{
   uint32_t Address = 0x00;
   uint32_t Data = 0x3210ABCD;
   volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;

   /* Porgram FLASH Bank1 ********************************************************/
   /* Unlock the Flash Bank1 Program Erase controller */
   FLASH_UnlockBank1();

   /* Clear All pending flags */
   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

   /* Erase the FLASH pages */
   FLASHStatus = FLASH_ErasePage(PAGE_START_ADDR);
   if (FLASHStatus != FLASH_COMPLETE) return;

   /* Program Flash Bank1 */
   Address = PAGE_START_ADDR;
   while ((Address < PAGE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE)) {
      FLASHStatus = FLASH_ProgramWord(Address, Data);
      Address = Address + 4;
   }

   FLASH_LockBank1();
}

댓글 없음:

댓글 쓰기