Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

297 lines
9.1KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. // To configure the EEPROM size, edit E2END in avr/eeprom.h.
  31. //
  32. // Generally you should avoid editing this code, unless you really
  33. // know what you're doing.
  34. #include "imxrt.h"
  35. #include <avr/eeprom.h>
  36. #include <string.h>
  37. #include "debug/printf.h"
  38. #define FLASH_BASEADDR 0x601F0000
  39. #define FLASH_SECTORS 15
  40. #if E2END > (255*FLASH_SECTORS-1)
  41. #error "E2END is set larger than the maximum possible EEPROM size"
  42. #endif
  43. static void flash_write(void *addr, const void *data, uint32_t len);
  44. static void flash_erase_sector(void *addr);
  45. static uint8_t initialized=0;
  46. static uint16_t sector_index[FLASH_SECTORS];
  47. void eeprom_initialize(void)
  48. {
  49. uint32_t sector;
  50. //printf("eeprom init\n");
  51. for (sector=0; sector < FLASH_SECTORS; sector++) {
  52. const uint16_t *p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  53. const uint16_t *end = (uint16_t *)(FLASH_BASEADDR + (sector + 1) * 4096);
  54. uint16_t index = 0;
  55. do {
  56. if (*p++ == 0xFFFF) break;
  57. index++;
  58. } while (p < end);
  59. sector_index[sector] = index;
  60. }
  61. initialized = 1;
  62. }
  63. uint8_t eeprom_read_byte(const uint8_t *addr_ptr)
  64. {
  65. uint32_t addr = (uint32_t)addr_ptr;
  66. uint32_t sector, offset;
  67. const uint16_t *p, *end;
  68. uint8_t data=0xFF;
  69. if (addr > E2END) return 0xFF;
  70. if (!initialized) eeprom_initialize();
  71. sector = (addr >> 2) % FLASH_SECTORS;
  72. offset = (addr & 3) | (((addr >> 2) / FLASH_SECTORS) << 2);
  73. //printf("ee_rd, addr=%u, sector=%u, offset=%u, len=%u\n",
  74. //addr, sector, offset, sector_index[sector]);
  75. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  76. end = p + sector_index[sector];
  77. while (p < end) {
  78. uint32_t val = *p++;
  79. if ((val & 255) == offset) data = val >> 8;
  80. }
  81. return data;
  82. }
  83. void eeprom_write_byte(uint8_t *addr_ptr, uint8_t data)
  84. {
  85. uint32_t addr = (uint32_t)addr_ptr;
  86. uint32_t sector, offset, index, i;
  87. uint16_t *p, *end;
  88. uint8_t olddata=0xFF;
  89. uint8_t buf[256];
  90. if (addr > E2END) return;
  91. if (!initialized) eeprom_initialize();
  92. sector = (addr >> 2) % FLASH_SECTORS;
  93. offset = (addr & 3) | (((addr >> 2) / FLASH_SECTORS) << 2);
  94. //printf("ee_wr, addr=%u, sector=%u, offset=%u, len=%u\n",
  95. //addr, sector, offset, sector_index[sector]);
  96. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  97. end = p + sector_index[sector];
  98. while (p < end) {
  99. uint16_t val = *p++;
  100. if ((val & 255) == offset) olddata = val >> 8;
  101. }
  102. if (data == olddata) return;
  103. if (sector_index[sector] < 2048) {
  104. //printf("ee_wr, writing\n");
  105. uint16_t newdata = offset | (data << 8);
  106. flash_write(end, &newdata, 2);
  107. sector_index[sector] = sector_index[sector] + 1;
  108. } else {
  109. //printf("ee_wr, erase then write\n");
  110. memset(buf, 0xFF, sizeof(buf));
  111. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  112. end = p + 2048;
  113. while (p < end) {
  114. uint16_t val = *p++;
  115. buf[val & 255] = val >> 8;
  116. }
  117. buf[offset] = data;
  118. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  119. flash_erase_sector(p);
  120. index = 0;
  121. for (i=0; i < 256; i++) {
  122. if (buf[i] != 0xFF) {
  123. // TODO: combining these to larger write
  124. // would (probably) be more efficient
  125. uint16_t newval = i | (buf[i] << 8);
  126. flash_write(p + index, &newval, 2);
  127. index = index + 1;
  128. }
  129. }
  130. sector_index[sector] = index;
  131. }
  132. }
  133. uint16_t eeprom_read_word(const uint16_t *addr)
  134. {
  135. const uint8_t *p = (const uint8_t *)addr;
  136. return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8);
  137. }
  138. uint32_t eeprom_read_dword(const uint32_t *addr)
  139. {
  140. const uint8_t *p = (const uint8_t *)addr;
  141. return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8)
  142. | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24);
  143. }
  144. void eeprom_read_block(void *buf, const void *addr, uint32_t len)
  145. {
  146. const uint8_t *p = (const uint8_t *)addr;
  147. uint8_t *dest = (uint8_t *)buf;
  148. while (len--) {
  149. *dest++ = eeprom_read_byte(p++);
  150. }
  151. }
  152. int eeprom_is_ready(void)
  153. {
  154. return 1;
  155. }
  156. void eeprom_write_word(uint16_t *addr, uint16_t value)
  157. {
  158. uint8_t *p = (uint8_t *)addr;
  159. eeprom_write_byte(p++, value);
  160. eeprom_write_byte(p, value >> 8);
  161. }
  162. void eeprom_write_dword(uint32_t *addr, uint32_t value)
  163. {
  164. uint8_t *p = (uint8_t *)addr;
  165. eeprom_write_byte(p++, value);
  166. eeprom_write_byte(p++, value >> 8);
  167. eeprom_write_byte(p++, value >> 16);
  168. eeprom_write_byte(p, value >> 24);
  169. }
  170. void eeprom_write_block(const void *buf, void *addr, uint32_t len)
  171. {
  172. uint8_t *p = (uint8_t *)addr;
  173. const uint8_t *src = (const uint8_t *)buf;
  174. while (len--) {
  175. eeprom_write_byte(p++, *src++);
  176. }
  177. }
  178. #define LUT0(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)))
  179. #define LUT1(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)) << 16)
  180. #define CMD_SDR FLEXSPI_LUT_OPCODE_CMD_SDR
  181. #define ADDR_SDR FLEXSPI_LUT_OPCODE_RADDR_SDR
  182. #define READ_SDR FLEXSPI_LUT_OPCODE_READ_SDR
  183. #define WRITE_SDR FLEXSPI_LUT_OPCODE_WRITE_SDR
  184. #define PINS1 FLEXSPI_LUT_NUM_PADS_1
  185. #define PINS4 FLEXSPI_LUT_NUM_PADS_4
  186. static void flash_wait()
  187. {
  188. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x05) | LUT1(READ_SDR, PINS1, 1); // 05 = read status
  189. FLEXSPI_LUT61 = 0;
  190. uint8_t status;
  191. do {
  192. FLEXSPI_IPRXFCR = FLEXSPI_IPRXFCR_CLRIPRXF; // clear rx fifo
  193. FLEXSPI_IPCR0 = 0;
  194. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15) | FLEXSPI_IPCR1_IDATSZ(1);
  195. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  196. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) {
  197. asm("nop");
  198. }
  199. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  200. status = *(uint8_t *)&FLEXSPI_RFDR0;
  201. } while (status & 1);
  202. FLEXSPI_MCR0 |= FLEXSPI_MCR0_SWRESET; // purge stale data from FlexSPI's AHB FIFO
  203. while (FLEXSPI_MCR0 & FLEXSPI_MCR0_SWRESET) ; // wait
  204. __enable_irq();
  205. }
  206. // write bytes into flash memory (which is already erased to 0xFF)
  207. static void flash_write(void *addr, const void *data, uint32_t len)
  208. {
  209. __disable_irq();
  210. FLEXSPI_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  211. FLEXSPI_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  212. FLEXSPI_IPCR0 = 0;
  213. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x06); // 06 = write enable
  214. FLEXSPI_LUT61 = 0;
  215. FLEXSPI_LUT62 = 0;
  216. FLEXSPI_LUT63 = 0;
  217. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  218. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  219. arm_dcache_delete(addr, len); // purge old data from ARM's cache
  220. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  221. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  222. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x32) | LUT1(ADDR_SDR, PINS1, 24); // 32 = quad write
  223. FLEXSPI_LUT61 = LUT0(WRITE_SDR, PINS4, 1);
  224. FLEXSPI_IPTXFCR = FLEXSPI_IPTXFCR_CLRIPTXF; // clear tx fifo
  225. FLEXSPI_IPCR0 = (uint32_t)addr & 0x001FFFFF;
  226. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15) | FLEXSPI_IPCR1_IDATSZ(len);
  227. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  228. const uint8_t *src = (const uint8_t *)data;
  229. uint32_t n;
  230. while (!((n = FLEXSPI_INTR) & FLEXSPI_INTR_IPCMDDONE)) {
  231. if (n & FLEXSPI_INTR_IPTXWE) {
  232. uint32_t wrlen = len;
  233. if (wrlen > 8) wrlen = 8;
  234. if (wrlen > 0) {
  235. memcpy((void *)&FLEXSPI_TFDR0, src, wrlen);
  236. src += wrlen;
  237. len -= wrlen;
  238. }
  239. FLEXSPI_INTR = FLEXSPI_INTR_IPTXWE;
  240. }
  241. }
  242. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE | FLEXSPI_INTR_IPTXWE;
  243. flash_wait();
  244. }
  245. // erase a 4K sector
  246. static void flash_erase_sector(void *addr)
  247. {
  248. __disable_irq();
  249. FLEXSPI_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  250. FLEXSPI_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  251. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x06); // 06 = write enable
  252. FLEXSPI_LUT61 = 0;
  253. FLEXSPI_LUT62 = 0;
  254. FLEXSPI_LUT63 = 0;
  255. FLEXSPI_IPCR0 = 0;
  256. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  257. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  258. arm_dcache_delete((void *)((uint32_t)addr & 0xFFFFF000), 4096); // purge data from cache
  259. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  260. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  261. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x20) | LUT1(ADDR_SDR, PINS1, 24); // 20 = sector erase
  262. FLEXSPI_IPCR0 = (uint32_t)addr & 0x001FF000;
  263. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  264. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  265. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  266. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  267. flash_wait();
  268. }