diff options
Diffstat (limited to 'drivers/eeprom')
| -rw-r--r-- | drivers/eeprom/eeprom_i2c.c | 23 | ||||
| -rw-r--r-- | drivers/eeprom/eeprom_stm32_L0_L1.c | 96 | ||||
| -rw-r--r-- | drivers/eeprom/eeprom_stm32_L0_L1.h | 33 |
3 files changed, 22 insertions, 130 deletions
diff --git a/drivers/eeprom/eeprom_i2c.c b/drivers/eeprom/eeprom_i2c.c index 4210f06f9..8e80ff544 100644 --- a/drivers/eeprom/eeprom_i2c.c +++ b/drivers/eeprom/eeprom_i2c.c | |||
| @@ -16,6 +16,9 @@ | |||
| 16 | 16 | ||
| 17 | #include <stdint.h> | 17 | #include <stdint.h> |
| 18 | #include <string.h> | 18 | #include <string.h> |
| 19 | #if defined(EXTERNAL_EEPROM_WP_PIN) | ||
| 20 | # include "gpio.h" | ||
| 21 | #endif | ||
| 19 | 22 | ||
| 20 | /* | 23 | /* |
| 21 | Note that the implementations of eeprom_XXXX_YYYY on AVR are normally | 24 | Note that the implementations of eeprom_XXXX_YYYY on AVR are normally |
| @@ -50,7 +53,14 @@ static inline void fill_target_address(uint8_t *buffer, const void *addr) { | |||
| 50 | } | 53 | } |
| 51 | } | 54 | } |
| 52 | 55 | ||
| 53 | void eeprom_driver_init(void) { i2c_init(); } | 56 | void eeprom_driver_init(void) { |
| 57 | i2c_init(); | ||
| 58 | #if defined(EXTERNAL_EEPROM_WP_PIN) | ||
| 59 | /* We are setting the WP pin to high in a way that requires at least two bit-flips to change back to 0 */ | ||
| 60 | writePin(EXTERNAL_EEPROM_WP_PIN, 1); | ||
| 61 | setPinInputHigh(EXTERNAL_EEPROM_WP_PIN); | ||
| 62 | #endif | ||
| 63 | } | ||
| 54 | 64 | ||
| 55 | void eeprom_driver_erase(void) { | 65 | void eeprom_driver_erase(void) { |
| 56 | #if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) | 66 | #if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) |
| @@ -89,6 +99,11 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) { | |||
| 89 | uint8_t * read_buf = (uint8_t *)buf; | 99 | uint8_t * read_buf = (uint8_t *)buf; |
| 90 | uintptr_t target_addr = (uintptr_t)addr; | 100 | uintptr_t target_addr = (uintptr_t)addr; |
| 91 | 101 | ||
| 102 | #if defined(EXTERNAL_EEPROM_WP_PIN) | ||
| 103 | setPinOutput(EXTERNAL_EEPROM_WP_PIN); | ||
| 104 | writePin(EXTERNAL_EEPROM_WP_PIN, 0); | ||
| 105 | #endif | ||
| 106 | |||
| 92 | while (len > 0) { | 107 | while (len > 0) { |
| 93 | uintptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE; | 108 | uintptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE; |
| 94 | int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset; | 109 | int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset; |
| @@ -116,4 +131,10 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) { | |||
| 116 | target_addr += write_length; | 131 | target_addr += write_length; |
| 117 | len -= write_length; | 132 | len -= write_length; |
| 118 | } | 133 | } |
| 134 | |||
| 135 | #if defined(EXTERNAL_EEPROM_WP_PIN) | ||
| 136 | /* We are setting the WP pin to high in a way that requires at least two bit-flips to change back to 0 */ | ||
| 137 | writePin(EXTERNAL_EEPROM_WP_PIN, 1); | ||
| 138 | setPinInputHigh(EXTERNAL_EEPROM_WP_PIN); | ||
| 139 | #endif | ||
| 119 | } | 140 | } |
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 | } | ||
diff --git a/drivers/eeprom/eeprom_stm32_L0_L1.h b/drivers/eeprom/eeprom_stm32_L0_L1.h deleted file mode 100644 index a35defca8..000000000 --- a/drivers/eeprom/eeprom_stm32_L0_L1.h +++ /dev/null | |||
| @@ -1,33 +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 | #pragma once | ||
| 18 | |||
| 19 | /* | ||
| 20 | The size used by the STM32 L0/L1 EEPROM driver. | ||
| 21 | */ | ||
| 22 | #ifndef STM32_ONBOARD_EEPROM_SIZE | ||
| 23 | # ifdef VIA_ENABLE | ||
| 24 | # define STM32_ONBOARD_EEPROM_SIZE 1024 | ||
| 25 | # else | ||
| 26 | # include "eeconfig.h" | ||
| 27 | # define STM32_ONBOARD_EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO and EEPROM page sizing | ||
| 28 | # endif | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #if STM32_ONBOARD_EEPROM_SIZE > 128 | ||
| 32 | # pragma message("Please note: resetting EEPROM using an STM32L0/L1 device takes up to 1 second for every 1kB of internal EEPROM used.") | ||
| 33 | #endif | ||
