diff options
Diffstat (limited to 'drivers/eeprom/eeprom_stm32_L0_L1.c')
| -rw-r--r-- | drivers/eeprom/eeprom_stm32_L0_L1.c | 96 |
1 files changed, 0 insertions, 96 deletions
diff --git a/drivers/eeprom/eeprom_stm32_L0_L1.c b/drivers/eeprom/eeprom_stm32_L0_L1.c deleted file mode 100644 index ed26cc714..000000000 --- a/drivers/eeprom/eeprom_stm32_L0_L1.c +++ /dev/null | |||
| @@ -1,96 +0,0 @@ | |||
| 1 | /* Copyright 2020 Nick Brassel (tzarc) | ||
| 2 | * | ||
| 3 | * This program is free software: you can redistribute it and/or modify | ||
| 4 | * it under the terms of the GNU General Public License as published by | ||
| 5 | * the Free Software Foundation, either version 2 of the License, or | ||
| 6 | * (at your option) any later version. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope that it will be useful, | ||
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | * GNU General Public License for more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <stdint.h> | ||
| 18 | #include <string.h> | ||
| 19 | |||
| 20 | #include <hal.h> | ||
| 21 | #include "eeprom_driver.h" | ||
| 22 | #include "eeprom_stm32_L0_L1.h" | ||
| 23 | |||
| 24 | #define EEPROM_BASE_ADDR 0x08080000 | ||
| 25 | #define EEPROM_ADDR(offset) (EEPROM_BASE_ADDR + (offset)) | ||
| 26 | #define EEPROM_PTR(offset) ((__IO uint8_t *)EEPROM_ADDR(offset)) | ||
| 27 | #define EEPROM_BYTE(location, offset) (*(EEPROM_PTR(((uint32_t)location) + ((uint32_t)offset)))) | ||
| 28 | |||
| 29 | #define BUFFER_BYTE(buffer, offset) (*(((uint8_t *)buffer) + offset)) | ||
| 30 | |||
| 31 | #define FLASH_PEKEY1 0x89ABCDEF | ||
| 32 | #define FLASH_PEKEY2 0x02030405 | ||
| 33 | |||
| 34 | static inline void STM32_L0_L1_EEPROM_WaitNotBusy(void) { | ||
| 35 | while (FLASH->SR & FLASH_SR_BSY) { | ||
| 36 | __WFI(); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | static inline void STM32_L0_L1_EEPROM_Unlock(void) { | ||
| 41 | STM32_L0_L1_EEPROM_WaitNotBusy(); | ||
| 42 | if (FLASH->PECR & FLASH_PECR_PELOCK) { | ||
| 43 | FLASH->PEKEYR = FLASH_PEKEY1; | ||
| 44 | FLASH->PEKEYR = FLASH_PEKEY2; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | static inline void STM32_L0_L1_EEPROM_Lock(void) { | ||
| 49 | STM32_L0_L1_EEPROM_WaitNotBusy(); | ||
| 50 | FLASH->PECR |= FLASH_PECR_PELOCK; | ||
| 51 | } | ||
| 52 | |||
| 53 | void eeprom_driver_init(void) {} | ||
| 54 | |||
| 55 | void eeprom_driver_erase(void) { | ||
| 56 | STM32_L0_L1_EEPROM_Unlock(); | ||
| 57 | |||
| 58 | for (size_t offset = 0; offset < STM32_ONBOARD_EEPROM_SIZE; offset += sizeof(uint32_t)) { | ||
| 59 | FLASH->PECR |= FLASH_PECR_ERASE | FLASH_PECR_DATA; | ||
| 60 | |||
| 61 | *(__IO uint32_t *)EEPROM_ADDR(offset) = (uint32_t)0; | ||
| 62 | |||
| 63 | STM32_L0_L1_EEPROM_WaitNotBusy(); | ||
| 64 | FLASH->PECR &= ~(FLASH_PECR_ERASE | FLASH_PECR_DATA); | ||
| 65 | } | ||
| 66 | |||
| 67 | STM32_L0_L1_EEPROM_Lock(); | ||
| 68 | } | ||
| 69 | |||
| 70 | void eeprom_read_block(void *buf, const void *addr, size_t len) { | ||
| 71 | for (size_t offset = 0; offset < len; ++offset) { | ||
| 72 | // Drop out if we've hit the limit of the EEPROM | ||
| 73 | if ((((uint32_t)addr) + offset) >= STM32_ONBOARD_EEPROM_SIZE) { | ||
| 74 | break; | ||
| 75 | } | ||
| 76 | |||
| 77 | STM32_L0_L1_EEPROM_WaitNotBusy(); | ||
| 78 | BUFFER_BYTE(buf, offset) = EEPROM_BYTE(addr, offset); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | void eeprom_write_block(const void *buf, void *addr, size_t len) { | ||
| 83 | STM32_L0_L1_EEPROM_Unlock(); | ||
| 84 | |||
| 85 | for (size_t offset = 0; offset < len; ++offset) { | ||
| 86 | // Drop out if we've hit the limit of the EEPROM | ||
| 87 | if ((((uint32_t)addr) + offset) >= STM32_ONBOARD_EEPROM_SIZE) { | ||
| 88 | break; | ||
| 89 | } | ||
| 90 | |||
| 91 | STM32_L0_L1_EEPROM_WaitNotBusy(); | ||
| 92 | EEPROM_BYTE(addr, offset) = BUFFER_BYTE(buf, offset); | ||
| 93 | } | ||
| 94 | |||
| 95 | STM32_L0_L1_EEPROM_Lock(); | ||
| 96 | } | ||
