eeprom_spi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*
  19. Note that the implementations of eeprom_XXXX_YYYY on AVR are normally
  20. provided by avr-libc. The same functions are reimplemented below and are
  21. rerouted to the external SPI equivalent.
  22. Seemingly, as this is compiled from within QMK, the object file generated
  23. during the build overrides the avr-libc implementation during the linking
  24. stage.
  25. On other platforms such as ARM, there are no provided implementations, so
  26. there is nothing to override during linkage.
  27. */
  28. #include "wait.h"
  29. #include "spi_master.h"
  30. #include "eeprom.h"
  31. #include "eeprom_spi.h"
  32. #define CMD_WREN 6
  33. #define CMD_WRDI 4
  34. #define CMD_RDSR 5
  35. #define CMD_WRSR 1
  36. #define CMD_READ 3
  37. #define CMD_WRITE 2
  38. #define SR_WIP 0x01
  39. // #define DEBUG_EEPROM_OUTPUT
  40. #ifndef EXTERNAL_EEPROM_SPI_TIMEOUT
  41. # define EXTERNAL_EEPROM_SPI_TIMEOUT 100
  42. #endif
  43. #ifdef CONSOLE_ENABLE
  44. # include "print.h"
  45. #endif // CONSOLE_ENABLE
  46. static void init_spi_if_required(void) {
  47. static int done = 0;
  48. if (!done) {
  49. spi_init();
  50. done = 1;
  51. }
  52. }
  53. static bool spi_eeprom_start(void) { return spi_start(EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN, EXTERNAL_EEPROM_SPI_LSBFIRST, EXTERNAL_EEPROM_SPI_MODE, EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR); }
  54. static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
  55. uint32_t deadline = timer_read32() + timeout;
  56. spi_status_t response;
  57. do {
  58. spi_write(CMD_RDSR);
  59. response = spi_read();
  60. if (timer_read32() >= deadline) {
  61. return SPI_STATUS_TIMEOUT;
  62. }
  63. } while (response & SR_WIP);
  64. return SPI_STATUS_SUCCESS;
  65. }
  66. static void spi_eeprom_transmit_address(uintptr_t addr) {
  67. uint8_t buffer[EXTERNAL_EEPROM_ADDRESS_SIZE];
  68. for (int i = 0; i < EXTERNAL_EEPROM_ADDRESS_SIZE; ++i) {
  69. buffer[EXTERNAL_EEPROM_ADDRESS_SIZE - 1 - i] = addr & 0xFF;
  70. addr >>= 8;
  71. }
  72. spi_transmit(buffer, EXTERNAL_EEPROM_ADDRESS_SIZE);
  73. }
  74. //----------------------------------------------------------------------------------------------------------------------
  75. void eeprom_driver_init(void) {}
  76. void eeprom_driver_erase(void) {
  77. #ifdef CONSOLE_ENABLE
  78. uint32_t start = timer_read32();
  79. #endif
  80. uint8_t buf[EXTERNAL_EEPROM_PAGE_SIZE];
  81. memset(buf, 0x00, EXTERNAL_EEPROM_PAGE_SIZE);
  82. for (uint32_t addr = 0; addr < EXTERNAL_EEPROM_BYTE_COUNT; addr += EXTERNAL_EEPROM_PAGE_SIZE) {
  83. eeprom_write_block(buf, (void *)(uintptr_t)addr, EXTERNAL_EEPROM_PAGE_SIZE);
  84. }
  85. #ifdef CONSOLE_ENABLE
  86. dprintf("EEPROM erase took %ldms to complete\n", ((long)(timer_read32() - start)));
  87. #endif
  88. }
  89. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  90. init_spi_if_required();
  91. //-------------------------------------------------
  92. // Wait for the write-in-progress bit to be cleared
  93. bool res = spi_eeprom_start();
  94. if (!res) {
  95. dprint("failed to start SPI for WIP check\n");
  96. memset(buf, 0, len);
  97. return;
  98. }
  99. spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
  100. spi_stop();
  101. if (response == SPI_STATUS_TIMEOUT) {
  102. dprint("SPI timeout for WIP check\n");
  103. memset(buf, 0, len);
  104. return;
  105. }
  106. //-------------------------------------------------
  107. // Perform read
  108. res = spi_eeprom_start();
  109. if (!res) {
  110. dprint("failed to start SPI for read\n");
  111. memset(buf, 0, len);
  112. return;
  113. }
  114. spi_write(CMD_READ);
  115. spi_eeprom_transmit_address((uintptr_t)addr);
  116. spi_receive(buf, len);
  117. #ifdef DEBUG_EEPROM_OUTPUT
  118. dprintf("[EEPROM R] 0x%08lX: ", ((uint32_t)(uintptr_t)addr));
  119. for (size_t i = 0; i < len; ++i) {
  120. dprintf(" %02X", (int)(((uint8_t *)buf)[i]));
  121. }
  122. dprintf("\n");
  123. #endif // DEBUG_EEPROM_OUTPUT
  124. spi_stop();
  125. }
  126. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  127. init_spi_if_required();
  128. bool res;
  129. uint8_t * read_buf = (uint8_t *)buf;
  130. uintptr_t target_addr = (uintptr_t)addr;
  131. while (len > 0) {
  132. uintptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE;
  133. int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset;
  134. if (write_length > len) {
  135. write_length = len;
  136. }
  137. //-------------------------------------------------
  138. // Wait for the write-in-progress bit to be cleared
  139. res = spi_eeprom_start();
  140. if (!res) {
  141. dprint("failed to start SPI for WIP check\n");
  142. return;
  143. }
  144. spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
  145. spi_stop();
  146. if (response == SPI_STATUS_TIMEOUT) {
  147. dprint("SPI timeout for WIP check\n");
  148. return;
  149. }
  150. //-------------------------------------------------
  151. // Enable writes
  152. res = spi_eeprom_start();
  153. if (!res) {
  154. dprint("failed to start SPI for write-enable\n");
  155. return;
  156. }
  157. spi_write(CMD_WREN);
  158. spi_stop();
  159. //-------------------------------------------------
  160. // Perform the write
  161. res = spi_eeprom_start();
  162. if (!res) {
  163. dprint("failed to start SPI for write\n");
  164. return;
  165. }
  166. #ifdef DEBUG_EEPROM_OUTPUT
  167. dprintf("[EEPROM W] 0x%08lX: ", ((uint32_t)(uintptr_t)target_addr));
  168. for (size_t i = 0; i < write_length; i++) {
  169. dprintf(" %02X", (int)(uint8_t)(read_buf[i]));
  170. }
  171. dprintf("\n");
  172. #endif // DEBUG_EEPROM_OUTPUT
  173. spi_write(CMD_WRITE);
  174. spi_eeprom_transmit_address(target_addr);
  175. spi_transmit(read_buf, write_length);
  176. spi_stop();
  177. read_buf += write_length;
  178. target_addr += write_length;
  179. len -= write_length;
  180. }
  181. //-------------------------------------------------
  182. // Disable writes
  183. res = spi_eeprom_start();
  184. if (!res) {
  185. dprint("failed to start SPI for write-disable\n");
  186. return;
  187. }
  188. spi_write(CMD_WRDI);
  189. spi_stop();
  190. }