eeprom_flash
Dependents: STM32F103C8T6_WIFI_Heating_system
Fork of eeprom_flash by
eeprom_flash.cpp@4:404dd375323c, 2018-09-18 (annotated)
- Committer:
- andrewklmn
- Date:
- Tue Sep 18 20:47:20 2018 +0000
- Revision:
- 4:404dd375323c
- Parent:
- 3:35953bc8607a
change page address to 63k
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 | |
andrewklmn | 3:35953bc8607a | 4 | void _FLASH_PageErase(uint32_t PageAddress) |
andrewklmn | 3:35953bc8607a | 5 | { |
andrewklmn | 3:35953bc8607a | 6 | /* Proceed to erase the page */ |
andrewklmn | 3:35953bc8607a | 7 | SET_BIT(FLASH->CR, FLASH_CR_PER); |
andrewklmn | 3:35953bc8607a | 8 | while (FLASH->SR & FLASH_SR_BSY); |
andrewklmn | 3:35953bc8607a | 9 | WRITE_REG(FLASH->AR, PageAddress); |
andrewklmn | 3:35953bc8607a | 10 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
andrewklmn | 3:35953bc8607a | 11 | while (FLASH->SR & FLASH_SR_BSY); |
andrewklmn | 3:35953bc8607a | 12 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); |
andrewklmn | 3:35953bc8607a | 13 | } |
andrewklmn | 2:a37f58f78b2b | 14 | |
olympux | 0:ea2bec485611 | 15 | /* |
olympux | 0:ea2bec485611 | 16 | * Must call this first to enable writing |
olympux | 0:ea2bec485611 | 17 | */ |
olympux | 0:ea2bec485611 | 18 | void enableEEPROMWriting() { |
andrewklmn | 3:35953bc8607a | 19 | HAL_FLASH_Unlock(); |
andrewklmn | 3:35953bc8607a | 20 | _FLASH_PageErase(EEPROM_START_ADDRESS); |
olympux | 0:ea2bec485611 | 21 | } |
olympux | 0:ea2bec485611 | 22 | |
olympux | 0:ea2bec485611 | 23 | void disableEEPROMWriting() { |
olympux | 0:ea2bec485611 | 24 | HAL_FLASH_Lock(); |
olympux | 0:ea2bec485611 | 25 | } |
olympux | 0:ea2bec485611 | 26 | |
olympux | 0:ea2bec485611 | 27 | /* |
olympux | 0:ea2bec485611 | 28 | * Writing functions |
olympux | 0:ea2bec485611 | 29 | * Must call enableEEPROMWriting() first |
olympux | 0:ea2bec485611 | 30 | */ |
olympux | 0:ea2bec485611 | 31 | HAL_StatusTypeDef writeEEPROMHalfWord(uint32_t address, uint16_t data) { |
olympux | 0:ea2bec485611 | 32 | HAL_StatusTypeDef status; |
olympux | 0:ea2bec485611 | 33 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 34 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, data); |
olympux | 0:ea2bec485611 | 35 | return status; |
olympux | 0:ea2bec485611 | 36 | } |
olympux | 0:ea2bec485611 | 37 | |
olympux | 0:ea2bec485611 | 38 | HAL_StatusTypeDef writeEEPROMWord(uint32_t address, uint32_t data) { |
olympux | 0:ea2bec485611 | 39 | HAL_StatusTypeDef status; |
olympux | 0:ea2bec485611 | 40 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 41 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data); |
olympux | 0:ea2bec485611 | 42 | return status; |
olympux | 0:ea2bec485611 | 43 | } |
olympux | 0:ea2bec485611 | 44 | |
olympux | 0:ea2bec485611 | 45 | /* |
olympux | 0:ea2bec485611 | 46 | * Reading functions |
olympux | 0:ea2bec485611 | 47 | */ |
olympux | 0:ea2bec485611 | 48 | uint16_t readEEPROMHalfWord(uint32_t address) { |
olympux | 0:ea2bec485611 | 49 | uint16_t val = 0; |
olympux | 0:ea2bec485611 | 50 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 51 | val = *(__IO uint16_t*)address; |
olympux | 0:ea2bec485611 | 52 | return val; |
olympux | 0:ea2bec485611 | 53 | } |
olympux | 0:ea2bec485611 | 54 | |
olympux | 0:ea2bec485611 | 55 | uint32_t readEEPROMWord(uint32_t address) { |
olympux | 0:ea2bec485611 | 56 | uint32_t val = 0; |
olympux | 0:ea2bec485611 | 57 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 58 | val = *(__IO uint32_t*)address; |
olympux | 0:ea2bec485611 | 59 | return val; |
olympux | 0:ea2bec485611 | 60 | } |