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.

221 lines
6.5 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. /*
  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 "debug.h"
  30. #include "timer.h"
  31. #include "spi_master.h"
  32. #include "eeprom.h"
  33. #include "eeprom_spi.h"
  34. #define CMD_WREN 6
  35. #define CMD_WRDI 4
  36. #define CMD_RDSR 5
  37. #define CMD_WRSR 1
  38. #define CMD_READ 3
  39. #define CMD_WRITE 2
  40. #define SR_WIP 0x01
  41. // #define DEBUG_EEPROM_OUTPUT
  42. #ifndef EXTERNAL_EEPROM_SPI_TIMEOUT
  43. # define EXTERNAL_EEPROM_SPI_TIMEOUT 100
  44. #endif
  45. static bool spi_eeprom_start(void) {
  46. return spi_start(EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN, EXTERNAL_EEPROM_SPI_LSBFIRST, EXTERNAL_EEPROM_SPI_MODE, EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR);
  47. }
  48. static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
  49. uint32_t deadline = timer_read32() + timeout;
  50. spi_status_t response;
  51. do {
  52. spi_write(CMD_RDSR);
  53. response = spi_read();
  54. if (timer_read32() >= deadline) {
  55. return SPI_STATUS_TIMEOUT;
  56. }
  57. } while (response & SR_WIP);
  58. return SPI_STATUS_SUCCESS;
  59. }
  60. static void spi_eeprom_transmit_address(uintptr_t addr) {
  61. uint8_t buffer[EXTERNAL_EEPROM_ADDRESS_SIZE];
  62. for (int i = 0; i < EXTERNAL_EEPROM_ADDRESS_SIZE; ++i) {
  63. buffer[EXTERNAL_EEPROM_ADDRESS_SIZE - 1 - i] = addr & 0xFF;
  64. addr >>= 8;
  65. }
  66. spi_transmit(buffer, EXTERNAL_EEPROM_ADDRESS_SIZE);
  67. }
  68. //----------------------------------------------------------------------------------------------------------------------
  69. void eeprom_driver_init(void) {
  70. spi_init();
  71. }
  72. void eeprom_driver_erase(void) {
  73. #if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT)
  74. uint32_t start = timer_read32();
  75. #endif
  76. uint8_t buf[EXTERNAL_EEPROM_PAGE_SIZE];
  77. memset(buf, 0x00, EXTERNAL_EEPROM_PAGE_SIZE);
  78. for (uint32_t addr = 0; addr < EXTERNAL_EEPROM_BYTE_COUNT; addr += EXTERNAL_EEPROM_PAGE_SIZE) {
  79. eeprom_write_block(buf, (void *)(uintptr_t)addr, EXTERNAL_EEPROM_PAGE_SIZE);
  80. }
  81. #if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT)
  82. dprintf("EEPROM erase took %ldms to complete\n", ((long)(timer_read32() - start)));
  83. #endif
  84. }
  85. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  86. //-------------------------------------------------
  87. // Wait for the write-in-progress bit to be cleared
  88. bool res = spi_eeprom_start();
  89. if (!res) {
  90. dprint("failed to start SPI for WIP check\n");
  91. memset(buf, 0, len);
  92. return;
  93. }
  94. spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
  95. spi_stop();
  96. if (response == SPI_STATUS_TIMEOUT) {
  97. dprint("SPI timeout for WIP check\n");
  98. memset(buf, 0, len);
  99. return;
  100. }
  101. //-------------------------------------------------
  102. // Perform read
  103. res = spi_eeprom_start();
  104. if (!res) {
  105. dprint("failed to start SPI for read\n");
  106. memset(buf, 0, len);
  107. return;
  108. }
  109. spi_write(CMD_READ);
  110. spi_eeprom_transmit_address((uintptr_t)addr);
  111. spi_receive(buf, len);
  112. #if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT)
  113. dprintf("[EEPROM R] 0x%08lX: ", ((uint32_t)(uintptr_t)addr));
  114. for (size_t i = 0; i < len; ++i) {
  115. dprintf(" %02X", (int)(((uint8_t *)buf)[i]));
  116. }
  117. dprintf("\n");
  118. #endif // DEBUG_EEPROM_OUTPUT
  119. spi_stop();
  120. }
  121. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  122. bool res;
  123. uint8_t * read_buf = (uint8_t *)buf;
  124. uintptr_t target_addr = (uintptr_t)addr;
  125. while (len > 0) {
  126. uintptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE;
  127. int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset;
  128. if (write_length > len) {
  129. write_length = len;
  130. }
  131. //-------------------------------------------------
  132. // Wait for the write-in-progress bit to be cleared
  133. res = spi_eeprom_start();
  134. if (!res) {
  135. dprint("failed to start SPI for WIP check\n");
  136. return;
  137. }
  138. spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
  139. spi_stop();
  140. if (response == SPI_STATUS_TIMEOUT) {
  141. dprint("SPI timeout for WIP check\n");
  142. return;
  143. }
  144. //-------------------------------------------------
  145. // Enable writes
  146. res = spi_eeprom_start();
  147. if (!res) {
  148. dprint("failed to start SPI for write-enable\n");
  149. return;
  150. }
  151. spi_write(CMD_WREN);
  152. spi_stop();
  153. //-------------------------------------------------
  154. // Perform the write
  155. res = spi_eeprom_start();
  156. if (!res) {
  157. dprint("failed to start SPI for write\n");
  158. return;
  159. }
  160. #if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT)
  161. dprintf("[EEPROM W] 0x%08lX: ", ((uint32_t)(uintptr_t)target_addr));
  162. for (size_t i = 0; i < write_length; i++) {
  163. dprintf(" %02X", (int)(uint8_t)(read_buf[i]));
  164. }
  165. dprintf("\n");
  166. #endif // DEBUG_EEPROM_OUTPUT
  167. spi_write(CMD_WRITE);
  168. spi_eeprom_transmit_address(target_addr);
  169. spi_transmit(read_buf, write_length);
  170. spi_stop();
  171. read_buf += write_length;
  172. target_addr += write_length;
  173. len -= write_length;
  174. }
  175. //-------------------------------------------------
  176. // Disable writes
  177. res = spi_eeprom_start();
  178. if (!res) {
  179. dprint("failed to start SPI for write-disable\n");
  180. return;
  181. }
  182. spi_write(CMD_WRDI);
  183. spi_stop();
  184. }