EEPROM emulation using Flash for STM32F103 with fix error on "FLASH_PageErase"
Fork of eeprom_flash by
eeprom_flash.cpp@2:16f5373f8b31, 2018-05-05 (annotated)
- Committer:
- Leech
- Date:
- Sat May 05 19:06:35 2018 +0000
- Revision:
- 2:16f5373f8b31
- Parent:
- 0:ea2bec485611
Fix error on FLASH_PageErase
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
olympux | 0:ea2bec485611 | 1 | #include "mbed.h" |
olympux | 0:ea2bec485611 | 2 | #include "eeprom_flash.h" |
olympux | 0:ea2bec485611 | 3 | |
olympux | 0:ea2bec485611 | 4 | /* |
olympux | 0:ea2bec485611 | 5 | * Debug option |
olympux | 0:ea2bec485611 | 6 | */ |
olympux | 0:ea2bec485611 | 7 | #if 0 |
olympux | 0:ea2bec485611 | 8 | //Enable debug |
olympux | 0:ea2bec485611 | 9 | #include <cstdio> |
olympux | 0:ea2bec485611 | 10 | #define DBG(x, ...) std::printf("[eeprom: DBG]"x"\r\n", ##__VA_ARGS__); |
olympux | 0:ea2bec485611 | 11 | #define WARN(x, ...) std::printf("[eeprom: WARN]"x"\r\n", ##__VA_ARGS__); |
olympux | 0:ea2bec485611 | 12 | #define ERR(x, ...) std::printf("[eeprom: ERR]"x"\r\n", ##__VA_ARGS__); |
olympux | 0:ea2bec485611 | 13 | #else |
olympux | 0:ea2bec485611 | 14 | //Disable debug |
olympux | 0:ea2bec485611 | 15 | #define DBG(x, ...) |
olympux | 0:ea2bec485611 | 16 | #define WARN(x, ...) |
olympux | 0:ea2bec485611 | 17 | #define ERR(x, ...) |
olympux | 0:ea2bec485611 | 18 | #endif |
olympux | 0:ea2bec485611 | 19 | |
olympux | 0:ea2bec485611 | 20 | /* |
olympux | 0:ea2bec485611 | 21 | * Must call this first to enable writing |
olympux | 0:ea2bec485611 | 22 | */ |
olympux | 0:ea2bec485611 | 23 | void enableEEPROMWriting() { |
olympux | 0:ea2bec485611 | 24 | HAL_StatusTypeDef status = HAL_FLASH_Unlock(); |
olympux | 0:ea2bec485611 | 25 | FLASH_PageErase(EEPROM_START_ADDRESS); // required to re-write |
olympux | 0:ea2bec485611 | 26 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); // Bug fix: bit PER has been set in Flash_PageErase(), must clear it here |
olympux | 0:ea2bec485611 | 27 | } |
olympux | 0:ea2bec485611 | 28 | |
Leech | 2:16f5373f8b31 | 29 | void FLASH_PageErase(uint32_t PageAddress) |
Leech | 2:16f5373f8b31 | 30 | { |
Leech | 2:16f5373f8b31 | 31 | /* Proceed to erase the page */ |
Leech | 2:16f5373f8b31 | 32 | SET_BIT(FLASH->CR, FLASH_CR_PER); |
Leech | 2:16f5373f8b31 | 33 | while (FLASH->SR & FLASH_SR_BSY); |
Leech | 2:16f5373f8b31 | 34 | WRITE_REG(FLASH->AR, PageAddress); |
Leech | 2:16f5373f8b31 | 35 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
Leech | 2:16f5373f8b31 | 36 | while (FLASH->SR & FLASH_SR_BSY); |
Leech | 2:16f5373f8b31 | 37 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); |
Leech | 2:16f5373f8b31 | 38 | } |
Leech | 2:16f5373f8b31 | 39 | |
olympux | 0:ea2bec485611 | 40 | void disableEEPROMWriting() { |
olympux | 0:ea2bec485611 | 41 | HAL_FLASH_Lock(); |
olympux | 0:ea2bec485611 | 42 | } |
olympux | 0:ea2bec485611 | 43 | |
olympux | 0:ea2bec485611 | 44 | /* |
olympux | 0:ea2bec485611 | 45 | * Writing functions |
olympux | 0:ea2bec485611 | 46 | * Must call enableEEPROMWriting() first |
olympux | 0:ea2bec485611 | 47 | */ |
olympux | 0:ea2bec485611 | 48 | HAL_StatusTypeDef writeEEPROMHalfWord(uint32_t address, uint16_t data) { |
olympux | 0:ea2bec485611 | 49 | HAL_StatusTypeDef status; |
olympux | 0:ea2bec485611 | 50 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 51 | |
olympux | 0:ea2bec485611 | 52 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, data); |
olympux | 0:ea2bec485611 | 53 | |
olympux | 0:ea2bec485611 | 54 | return status; |
olympux | 0:ea2bec485611 | 55 | } |
olympux | 0:ea2bec485611 | 56 | |
olympux | 0:ea2bec485611 | 57 | HAL_StatusTypeDef writeEEPROMWord(uint32_t address, uint32_t data) { |
olympux | 0:ea2bec485611 | 58 | HAL_StatusTypeDef status; |
olympux | 0:ea2bec485611 | 59 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 60 | |
olympux | 0:ea2bec485611 | 61 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data); |
olympux | 0:ea2bec485611 | 62 | |
olympux | 0:ea2bec485611 | 63 | return status; |
olympux | 0:ea2bec485611 | 64 | } |
olympux | 0:ea2bec485611 | 65 | |
olympux | 0:ea2bec485611 | 66 | /* |
olympux | 0:ea2bec485611 | 67 | * Reading functions |
olympux | 0:ea2bec485611 | 68 | */ |
olympux | 0:ea2bec485611 | 69 | uint16_t readEEPROMHalfWord(uint32_t address) { |
olympux | 0:ea2bec485611 | 70 | uint16_t val = 0; |
olympux | 0:ea2bec485611 | 71 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 72 | val = *(__IO uint16_t*)address; |
olympux | 0:ea2bec485611 | 73 | |
olympux | 0:ea2bec485611 | 74 | return val; |
olympux | 0:ea2bec485611 | 75 | } |
olympux | 0:ea2bec485611 | 76 | |
olympux | 0:ea2bec485611 | 77 | uint32_t readEEPROMWord(uint32_t address) { |
olympux | 0:ea2bec485611 | 78 | uint32_t val = 0; |
olympux | 0:ea2bec485611 | 79 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 80 | val = *(__IO uint32_t*)address; |
olympux | 0:ea2bec485611 | 81 | |
olympux | 0:ea2bec485611 | 82 | return val; |
olympux | 0:ea2bec485611 | 83 | } |