logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

eeprom_stm32_L0_L1.c (4139B)


  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 EEPROM_WORD(location) (*(__IO uint32_t *)EEPROM_PTR(location))
  26. #define BUFFER_BYTE(buffer, offset) (*(((uint8_t *)buffer) + offset))
  27. #define FLASH_PEKEY1 0x89ABCDEF
  28. #define FLASH_PEKEY2 0x02030405
  29. static inline void STM32_L0_L1_EEPROM_WaitNotBusy(void) {
  30. while (FLASH->SR & FLASH_SR_BSY) {
  31. __WFI();
  32. }
  33. }
  34. static inline void STM32_L0_L1_EEPROM_Unlock(void) {
  35. STM32_L0_L1_EEPROM_WaitNotBusy();
  36. if (FLASH->PECR & FLASH_PECR_PELOCK) {
  37. FLASH->PEKEYR = FLASH_PEKEY1;
  38. FLASH->PEKEYR = FLASH_PEKEY2;
  39. }
  40. }
  41. static inline void STM32_L0_L1_EEPROM_Lock(void) {
  42. STM32_L0_L1_EEPROM_WaitNotBusy();
  43. FLASH->PECR |= FLASH_PECR_PELOCK;
  44. }
  45. void eeprom_driver_init(void) {}
  46. void eeprom_driver_format(bool erase) {
  47. if (erase) {
  48. eeprom_driver_erase();
  49. }
  50. }
  51. void eeprom_driver_erase(void) {
  52. STM32_L0_L1_EEPROM_Unlock();
  53. for (size_t offset = 0; offset < STM32_ONBOARD_EEPROM_SIZE; offset += sizeof(uint32_t)) {
  54. #ifdef QMK_MCU_SERIES_STM32L0XX
  55. FLASH->PECR |= FLASH_PECR_ERASE | FLASH_PECR_DATA;
  56. #endif
  57. EEPROM_WORD(offset) = (uint32_t)0;
  58. STM32_L0_L1_EEPROM_WaitNotBusy();
  59. #ifdef QMK_MCU_SERIES_STM32L0XX
  60. FLASH->PECR &= ~(FLASH_PECR_ERASE | FLASH_PECR_DATA);
  61. #endif
  62. }
  63. STM32_L0_L1_EEPROM_Lock();
  64. }
  65. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  66. for (size_t offset = 0; offset < len; ++offset) {
  67. // Drop out if we've hit the limit of the EEPROM
  68. if ((((uint32_t)addr) + offset) >= STM32_ONBOARD_EEPROM_SIZE) {
  69. break;
  70. }
  71. STM32_L0_L1_EEPROM_WaitNotBusy();
  72. BUFFER_BYTE(buf, offset) = EEPROM_BYTE(addr, offset);
  73. }
  74. }
  75. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  76. // use word-aligned write to overcome issues with writing null bytes
  77. uint32_t start_addr = (uint32_t)addr;
  78. if (start_addr >= (STM32_ONBOARD_EEPROM_SIZE)) {
  79. return;
  80. }
  81. uint32_t max_len = (STM32_ONBOARD_EEPROM_SIZE)-start_addr;
  82. if (len > max_len) {
  83. len = max_len;
  84. }
  85. uint32_t end_addr = start_addr + len;
  86. uint32_t aligned_start = start_addr & ~0x3;
  87. uint32_t aligned_end = (end_addr + 3) & ~0x3;
  88. STM32_L0_L1_EEPROM_Unlock();
  89. for (uint32_t word_addr = aligned_start; word_addr < aligned_end; word_addr += 4) {
  90. uint32_t existing_word = EEPROM_WORD(word_addr);
  91. uint32_t new_word = existing_word;
  92. // Update the relevant bytes in the word
  93. for (int i = 0; i < 4; i++) {
  94. uint32_t byte_addr = word_addr + i;
  95. if (byte_addr >= start_addr && byte_addr < end_addr) {
  96. uint8_t new_byte = BUFFER_BYTE(buf, byte_addr - start_addr);
  97. new_word = (new_word & ~(0xFFU << (i * 8))) | ((uint32_t)new_byte << (i * 8));
  98. }
  99. }
  100. // Only write if the word has changed
  101. if (new_word != existing_word) {
  102. STM32_L0_L1_EEPROM_WaitNotBusy();
  103. EEPROM_WORD(word_addr) = new_word;
  104. }
  105. }
  106. STM32_L0_L1_EEPROM_Lock();
  107. }