EEPROM emulation using Flash for STM32F103

Dependents:   F103-Web-Server

Committer:
olympux
Date:
Mon Aug 29 21:29:20 2016 +0000
Revision:
1:e593ae8ff81d
Parent:
0:ea2bec485611
Update EEPROM page address

Who changed what in which revision?

UserRevisionLine numberNew 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
olympux 0:ea2bec485611 29 void disableEEPROMWriting() {
olympux 0:ea2bec485611 30 HAL_FLASH_Lock();
olympux 0:ea2bec485611 31 }
olympux 0:ea2bec485611 32
olympux 0:ea2bec485611 33 /*
olympux 0:ea2bec485611 34 * Writing functions
olympux 0:ea2bec485611 35 * Must call enableEEPROMWriting() first
olympux 0:ea2bec485611 36 */
olympux 0:ea2bec485611 37 HAL_StatusTypeDef writeEEPROMHalfWord(uint32_t address, uint16_t data) {
olympux 0:ea2bec485611 38 HAL_StatusTypeDef status;
olympux 0:ea2bec485611 39 address = address + EEPROM_START_ADDRESS;
olympux 0:ea2bec485611 40
olympux 0:ea2bec485611 41 status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, data);
olympux 0:ea2bec485611 42
olympux 0:ea2bec485611 43 return status;
olympux 0:ea2bec485611 44 }
olympux 0:ea2bec485611 45
olympux 0:ea2bec485611 46 HAL_StatusTypeDef writeEEPROMWord(uint32_t address, uint32_t data) {
olympux 0:ea2bec485611 47 HAL_StatusTypeDef status;
olympux 0:ea2bec485611 48 address = address + EEPROM_START_ADDRESS;
olympux 0:ea2bec485611 49
olympux 0:ea2bec485611 50 status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data);
olympux 0:ea2bec485611 51
olympux 0:ea2bec485611 52 return status;
olympux 0:ea2bec485611 53 }
olympux 0:ea2bec485611 54
olympux 0:ea2bec485611 55 /*
olympux 0:ea2bec485611 56 * Reading functions
olympux 0:ea2bec485611 57 */
olympux 0:ea2bec485611 58 uint16_t readEEPROMHalfWord(uint32_t address) {
olympux 0:ea2bec485611 59 uint16_t val = 0;
olympux 0:ea2bec485611 60 address = address + EEPROM_START_ADDRESS;
olympux 0:ea2bec485611 61 val = *(__IO uint16_t*)address;
olympux 0:ea2bec485611 62
olympux 0:ea2bec485611 63 return val;
olympux 0:ea2bec485611 64 }
olympux 0:ea2bec485611 65
olympux 0:ea2bec485611 66 uint32_t readEEPROMWord(uint32_t address) {
olympux 0:ea2bec485611 67 uint32_t val = 0;
olympux 0:ea2bec485611 68 address = address + EEPROM_START_ADDRESS;
olympux 0:ea2bec485611 69 val = *(__IO uint32_t*)address;
olympux 0:ea2bec485611 70
olympux 0:ea2bec485611 71 return val;
olympux 0:ea2bec485611 72 }