Andrew Klimenyuk / eeprom_flash

Dependents:   STM32F103C8T6_WIFI_Heating_system

Fork of eeprom_flash by Chau Vo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eeprom_flash.cpp Source File

eeprom_flash.cpp

00001 #include "mbed.h"
00002 #include "eeprom_flash.h"
00003 
00004 void _FLASH_PageErase(uint32_t PageAddress)
00005 {
00006     /* Proceed to erase the page */
00007     SET_BIT(FLASH->CR, FLASH_CR_PER);
00008     while (FLASH->SR & FLASH_SR_BSY);
00009     WRITE_REG(FLASH->AR, PageAddress);
00010     SET_BIT(FLASH->CR, FLASH_CR_STRT);
00011     while (FLASH->SR & FLASH_SR_BSY);
00012     CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
00013 }
00014 
00015 /*
00016  * Must call this first to enable writing
00017  */
00018 void enableEEPROMWriting() {
00019     HAL_FLASH_Unlock();
00020     _FLASH_PageErase(EEPROM_START_ADDRESS);
00021 }
00022 
00023 void disableEEPROMWriting() {
00024     HAL_FLASH_Lock();
00025 }
00026 
00027 /*
00028  * Writing functions
00029  * Must call enableEEPROMWriting() first
00030  */
00031 HAL_StatusTypeDef writeEEPROMHalfWord(uint32_t address, uint16_t data) {
00032     HAL_StatusTypeDef status;
00033     address = address + EEPROM_START_ADDRESS;
00034     status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, data);
00035     return status;
00036 }
00037 
00038 HAL_StatusTypeDef writeEEPROMWord(uint32_t address, uint32_t data) {
00039     HAL_StatusTypeDef status;
00040     address = address + EEPROM_START_ADDRESS;
00041     status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data);
00042     return status;
00043 }
00044 
00045 /*
00046  * Reading functions
00047  */
00048 uint16_t readEEPROMHalfWord(uint32_t address) {
00049     uint16_t val = 0;
00050     address = address + EEPROM_START_ADDRESS;
00051     val = *(__IO uint16_t*)address;
00052     return val;
00053 }
00054 
00055 uint32_t readEEPROMWord(uint32_t address) {
00056     uint32_t val = 0;
00057     address = address + EEPROM_START_ADDRESS;
00058     val = *(__IO uint32_t*)address;
00059     return val;
00060 }