Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: STM32F103C8T6_WIFI_Heating_system
Fork of eeprom_flash by
eeprom_flash.cpp@2:a37f58f78b2b, 2018-09-17 (annotated)
- Committer:
- andrewklmn
- Date:
- Mon Sep 17 20:15:21 2018 +0000
- Revision:
- 2:a37f58f78b2b
- Parent:
- 0:ea2bec485611
- Child:
- 3:35953bc8607a
epprom_flash updated
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 | |
| andrewklmn | 2:a37f58f78b2b | 20 | FLASH_EraseInitTypeDef eraseInit = { |
| andrewklmn | 2:a37f58f78b2b | 21 | FLASH_TYPEERASE_PAGES, /*!< Pages erase only (Mass erase is disabled)*/ |
| andrewklmn | 2:a37f58f78b2b | 22 | 0, /*!< Select banks to erase when Mass erase is enabled.*/ |
| andrewklmn | 2:a37f58f78b2b | 23 | EEPROM_START_ADDRESS, /*!< Initial FLASH page address to erase when mass erase is disabled |
| andrewklmn | 2:a37f58f78b2b | 24 | This parameter must be a number between Min_Data = 0x08000000 and Max_Data = FLASH_BANKx_END |
| andrewklmn | 2:a37f58f78b2b | 25 | (x = 1 or 2 depending on devices)*/ |
| andrewklmn | 2:a37f58f78b2b | 26 | 1 /*!< Number of pagess to be erased. |
| andrewklmn | 2:a37f58f78b2b | 27 | This parameter must be a value between Min_Data = 1 and Max_Data = (max number of pages - value of initial page)*/ |
| andrewklmn | 2:a37f58f78b2b | 28 | }; |
| andrewklmn | 2:a37f58f78b2b | 29 | |
| andrewklmn | 2:a37f58f78b2b | 30 | uint32_t pageError; |
| andrewklmn | 2:a37f58f78b2b | 31 | |
| andrewklmn | 2:a37f58f78b2b | 32 | |
| olympux | 0:ea2bec485611 | 33 | /* |
| olympux | 0:ea2bec485611 | 34 | * Must call this first to enable writing |
| olympux | 0:ea2bec485611 | 35 | */ |
| olympux | 0:ea2bec485611 | 36 | void enableEEPROMWriting() { |
| olympux | 0:ea2bec485611 | 37 | HAL_StatusTypeDef status = HAL_FLASH_Unlock(); |
| andrewklmn | 2:a37f58f78b2b | 38 | //FLASH_PageErase(EEPROM_START_ADDRESS); // required to re-write |
| andrewklmn | 2:a37f58f78b2b | 39 | //CLEAR_BIT(FLASH->CR, FLASH_CR_PER); // Bug fix: bit PER has been set in Flash_PageErase(), must clear it here |
| andrewklmn | 2:a37f58f78b2b | 40 | HAL_FLASHEx_Erase(&eraseInit, &pageError); |
| olympux | 0:ea2bec485611 | 41 | } |
| olympux | 0:ea2bec485611 | 42 | |
| olympux | 0:ea2bec485611 | 43 | void disableEEPROMWriting() { |
| olympux | 0:ea2bec485611 | 44 | HAL_FLASH_Lock(); |
| olympux | 0:ea2bec485611 | 45 | } |
| olympux | 0:ea2bec485611 | 46 | |
| olympux | 0:ea2bec485611 | 47 | /* |
| olympux | 0:ea2bec485611 | 48 | * Writing functions |
| olympux | 0:ea2bec485611 | 49 | * Must call enableEEPROMWriting() first |
| olympux | 0:ea2bec485611 | 50 | */ |
| olympux | 0:ea2bec485611 | 51 | HAL_StatusTypeDef writeEEPROMHalfWord(uint32_t address, uint16_t data) { |
| olympux | 0:ea2bec485611 | 52 | HAL_StatusTypeDef status; |
| olympux | 0:ea2bec485611 | 53 | address = address + EEPROM_START_ADDRESS; |
| olympux | 0:ea2bec485611 | 54 | |
| olympux | 0:ea2bec485611 | 55 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, data); |
| olympux | 0:ea2bec485611 | 56 | |
| olympux | 0:ea2bec485611 | 57 | return status; |
| olympux | 0:ea2bec485611 | 58 | } |
| olympux | 0:ea2bec485611 | 59 | |
| olympux | 0:ea2bec485611 | 60 | HAL_StatusTypeDef writeEEPROMWord(uint32_t address, uint32_t data) { |
| olympux | 0:ea2bec485611 | 61 | HAL_StatusTypeDef status; |
| olympux | 0:ea2bec485611 | 62 | address = address + EEPROM_START_ADDRESS; |
| olympux | 0:ea2bec485611 | 63 | |
| olympux | 0:ea2bec485611 | 64 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data); |
| olympux | 0:ea2bec485611 | 65 | |
| olympux | 0:ea2bec485611 | 66 | return status; |
| olympux | 0:ea2bec485611 | 67 | } |
| olympux | 0:ea2bec485611 | 68 | |
| olympux | 0:ea2bec485611 | 69 | /* |
| olympux | 0:ea2bec485611 | 70 | * Reading functions |
| olympux | 0:ea2bec485611 | 71 | */ |
| olympux | 0:ea2bec485611 | 72 | uint16_t readEEPROMHalfWord(uint32_t address) { |
| olympux | 0:ea2bec485611 | 73 | uint16_t val = 0; |
| olympux | 0:ea2bec485611 | 74 | address = address + EEPROM_START_ADDRESS; |
| olympux | 0:ea2bec485611 | 75 | val = *(__IO uint16_t*)address; |
| olympux | 0:ea2bec485611 | 76 | |
| olympux | 0:ea2bec485611 | 77 | return val; |
| olympux | 0:ea2bec485611 | 78 | } |
| olympux | 0:ea2bec485611 | 79 | |
| olympux | 0:ea2bec485611 | 80 | uint32_t readEEPROMWord(uint32_t address) { |
| olympux | 0:ea2bec485611 | 81 | uint32_t val = 0; |
| olympux | 0:ea2bec485611 | 82 | address = address + EEPROM_START_ADDRESS; |
| olympux | 0:ea2bec485611 | 83 | val = *(__IO uint32_t*)address; |
| olympux | 0:ea2bec485611 | 84 | |
| olympux | 0:ea2bec485611 | 85 | return val; |
| olympux | 0:ea2bec485611 | 86 | } |
