You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.9 KiB

  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. #include <stdint.h>
  17. #include <string.h>
  18. #include <hal.h>
  19. #include "eeprom_driver.h"
  20. #include "eeprom_stm32_L0_L1.h"
  21. #define EEPROM_BASE_ADDR 0x08080000
  22. #define EEPROM_ADDR(offset) (EEPROM_BASE_ADDR + (offset))
  23. #define EEPROM_PTR(offset) ((__IO uint8_t *)EEPROM_ADDR(offset))
  24. #define EEPROM_BYTE(location, offset) (*(EEPROM_PTR(((uint32_t)location) + ((uint32_t)offset))))
  25. #define BUFFER_BYTE(buffer, offset) (*(((uint8_t *)buffer) + offset))
  26. #define FLASH_PEKEY1 0x89ABCDEF
  27. #define FLASH_PEKEY2 0x02030405
  28. static inline void STM32_L0_L1_EEPROM_WaitNotBusy(void) {
  29. while (FLASH->SR & FLASH_SR_BSY) {
  30. __WFI();
  31. }
  32. }
  33. static inline void STM32_L0_L1_EEPROM_Unlock(void) {
  34. STM32_L0_L1_EEPROM_WaitNotBusy();
  35. if (FLASH->PECR & FLASH_PECR_PELOCK) {
  36. FLASH->PEKEYR = FLASH_PEKEY1;
  37. FLASH->PEKEYR = FLASH_PEKEY2;
  38. }
  39. }
  40. static inline void STM32_L0_L1_EEPROM_Lock(void) {
  41. STM32_L0_L1_EEPROM_WaitNotBusy();
  42. FLASH->PECR |= FLASH_PECR_PELOCK;
  43. }
  44. void eeprom_driver_init(void) {}
  45. void eeprom_driver_erase(void) {
  46. STM32_L0_L1_EEPROM_Unlock();
  47. for (size_t offset = 0; offset < STM32_ONBOARD_EEPROM_SIZE; offset += sizeof(uint32_t)) {
  48. FLASH->PECR |= FLASH_PECR_ERASE | FLASH_PECR_DATA;
  49. *(__IO uint32_t *)EEPROM_ADDR(offset) = (uint32_t)0;
  50. STM32_L0_L1_EEPROM_WaitNotBusy();
  51. FLASH->PECR &= ~(FLASH_PECR_ERASE | FLASH_PECR_DATA);
  52. }
  53. STM32_L0_L1_EEPROM_Lock();
  54. }
  55. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  56. for (size_t offset = 0; offset < len; ++offset) {
  57. // Drop out if we've hit the limit of the EEPROM
  58. if ((((uint32_t)addr) + offset) >= STM32_ONBOARD_EEPROM_SIZE) {
  59. break;
  60. }
  61. STM32_L0_L1_EEPROM_WaitNotBusy();
  62. BUFFER_BYTE(buf, offset) = EEPROM_BYTE(addr, offset);
  63. }
  64. }
  65. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  66. STM32_L0_L1_EEPROM_Unlock();
  67. for (size_t offset = 0; offset < len; ++offset) {
  68. // Drop out if we've hit the limit of the EEPROM
  69. if ((((uint32_t)addr) + offset) >= STM32_ONBOARD_EEPROM_SIZE) {
  70. break;
  71. }
  72. STM32_L0_L1_EEPROM_WaitNotBusy();
  73. EEPROM_BYTE(addr, offset) = BUFFER_BYTE(buf, offset);
  74. }
  75. STM32_L0_L1_EEPROM_Lock();
  76. }