Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

310 lines
9.6KB

  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. #if defined(ARDUINO_TEENSY40)
  39. #define FLASH_BASEADDR 0x601F0000
  40. #define FLASH_SECTORS 15
  41. #elif defined(ARDUINO_TEENSY41)
  42. #define FLASH_BASEADDR 0x607C0000
  43. #define FLASH_SECTORS 63
  44. #elif defined(ARDUINO_TEENSY_MICROMOD)
  45. #define FLASH_BASEADDR 0x60FC0000
  46. #define FLASH_SECTORS 63
  47. #endif
  48. #if E2END > (255*FLASH_SECTORS-1)
  49. #error "E2END is set larger than the maximum possible EEPROM size"
  50. #endif
  51. // Conversation about how this code works & what the upper limits are
  52. // https://forum.pjrc.com/threads/57377?p=214566&viewfull=1#post214566
  53. // To be called from LittleFS_Program, any other use at your own risk!
  54. void eepromemu_flash_write(void *addr, const void *data, uint32_t len);
  55. void eepromemu_flash_erase_sector(void *addr);
  56. static uint8_t initialized=0;
  57. static uint16_t sector_index[FLASH_SECTORS];
  58. void eeprom_initialize(void)
  59. {
  60. uint32_t sector;
  61. //printf("eeprom init\n");
  62. for (sector=0; sector < FLASH_SECTORS; sector++) {
  63. const uint16_t *p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  64. const uint16_t *end = (uint16_t *)(FLASH_BASEADDR + (sector + 1) * 4096);
  65. uint16_t index = 0;
  66. do {
  67. if (*p++ == 0xFFFF) break;
  68. index++;
  69. } while (p < end);
  70. sector_index[sector] = index;
  71. }
  72. initialized = 1;
  73. }
  74. uint8_t eeprom_read_byte(const uint8_t *addr_ptr)
  75. {
  76. uint32_t addr = (uint32_t)addr_ptr;
  77. uint32_t sector, offset;
  78. const uint16_t *p, *end;
  79. uint8_t data=0xFF;
  80. if (addr > E2END) return 0xFF;
  81. if (!initialized) eeprom_initialize();
  82. sector = (addr >> 2) % FLASH_SECTORS;
  83. offset = (addr & 3) | (((addr >> 2) / FLASH_SECTORS) << 2);
  84. //printf("ee_rd, addr=%u, sector=%u, offset=%u, len=%u\n",
  85. //addr, sector, offset, sector_index[sector]);
  86. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  87. end = p + sector_index[sector];
  88. while (p < end) {
  89. uint32_t val = *p++;
  90. if ((val & 255) == offset) data = val >> 8;
  91. }
  92. return data;
  93. }
  94. void eeprom_write_byte(uint8_t *addr_ptr, uint8_t data)
  95. {
  96. uint32_t addr = (uint32_t)addr_ptr;
  97. uint32_t sector, offset, index, i;
  98. uint16_t *p, *end;
  99. uint8_t olddata=0xFF;
  100. uint8_t buf[256];
  101. if (addr > E2END) return;
  102. if (!initialized) eeprom_initialize();
  103. sector = (addr >> 2) % FLASH_SECTORS;
  104. offset = (addr & 3) | (((addr >> 2) / FLASH_SECTORS) << 2);
  105. //printf("ee_wr, addr=%u, sector=%u, offset=%u, len=%u\n",
  106. //addr, sector, offset, sector_index[sector]);
  107. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  108. end = p + sector_index[sector];
  109. while (p < end) {
  110. uint16_t val = *p++;
  111. if ((val & 255) == offset) olddata = val >> 8;
  112. }
  113. if (data == olddata) return;
  114. if (sector_index[sector] < 2048) {
  115. //printf("ee_wr, writing\n");
  116. uint16_t newdata = offset | (data << 8);
  117. eepromemu_flash_write(end, &newdata, 2);
  118. sector_index[sector] = sector_index[sector] + 1;
  119. } else {
  120. //printf("ee_wr, erase then write\n");
  121. memset(buf, 0xFF, sizeof(buf));
  122. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  123. end = p + 2048;
  124. while (p < end) {
  125. uint16_t val = *p++;
  126. buf[val & 255] = val >> 8;
  127. }
  128. buf[offset] = data;
  129. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  130. eepromemu_flash_erase_sector(p);
  131. index = 0;
  132. for (i=0; i < 256; i++) {
  133. if (buf[i] != 0xFF) {
  134. // TODO: combining these to larger write
  135. // would (probably) be more efficient
  136. uint16_t newval = i | (buf[i] << 8);
  137. eepromemu_flash_write(p + index, &newval, 2);
  138. index = index + 1;
  139. }
  140. }
  141. sector_index[sector] = index;
  142. }
  143. }
  144. uint16_t eeprom_read_word(const uint16_t *addr)
  145. {
  146. const uint8_t *p = (const uint8_t *)addr;
  147. return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8);
  148. }
  149. uint32_t eeprom_read_dword(const uint32_t *addr)
  150. {
  151. const uint8_t *p = (const uint8_t *)addr;
  152. return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8)
  153. | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24);
  154. }
  155. void eeprom_read_block(void *buf, const void *addr, uint32_t len)
  156. {
  157. const uint8_t *p = (const uint8_t *)addr;
  158. uint8_t *dest = (uint8_t *)buf;
  159. while (len--) {
  160. *dest++ = eeprom_read_byte(p++);
  161. }
  162. }
  163. int eeprom_is_ready(void)
  164. {
  165. return 1;
  166. }
  167. void eeprom_write_word(uint16_t *addr, uint16_t value)
  168. {
  169. uint8_t *p = (uint8_t *)addr;
  170. eeprom_write_byte(p++, value);
  171. eeprom_write_byte(p, value >> 8);
  172. }
  173. void eeprom_write_dword(uint32_t *addr, uint32_t value)
  174. {
  175. uint8_t *p = (uint8_t *)addr;
  176. eeprom_write_byte(p++, value);
  177. eeprom_write_byte(p++, value >> 8);
  178. eeprom_write_byte(p++, value >> 16);
  179. eeprom_write_byte(p, value >> 24);
  180. }
  181. void eeprom_write_block(const void *buf, void *addr, uint32_t len)
  182. {
  183. uint8_t *p = (uint8_t *)addr;
  184. const uint8_t *src = (const uint8_t *)buf;
  185. while (len--) {
  186. eeprom_write_byte(p++, *src++);
  187. }
  188. }
  189. #define LUT0(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)))
  190. #define LUT1(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)) << 16)
  191. #define CMD_SDR FLEXSPI_LUT_OPCODE_CMD_SDR
  192. #define ADDR_SDR FLEXSPI_LUT_OPCODE_RADDR_SDR
  193. #define READ_SDR FLEXSPI_LUT_OPCODE_READ_SDR
  194. #define WRITE_SDR FLEXSPI_LUT_OPCODE_WRITE_SDR
  195. #define PINS1 FLEXSPI_LUT_NUM_PADS_1
  196. #define PINS4 FLEXSPI_LUT_NUM_PADS_4
  197. static void flash_wait()
  198. {
  199. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x05) | LUT1(READ_SDR, PINS1, 1); // 05 = read status
  200. FLEXSPI_LUT61 = 0;
  201. uint8_t status;
  202. do {
  203. FLEXSPI_IPRXFCR = FLEXSPI_IPRXFCR_CLRIPRXF; // clear rx fifo
  204. FLEXSPI_IPCR0 = 0;
  205. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15) | FLEXSPI_IPCR1_IDATSZ(1);
  206. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  207. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) {
  208. asm("nop");
  209. }
  210. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  211. status = *(uint8_t *)&FLEXSPI_RFDR0;
  212. } while (status & 1);
  213. FLEXSPI_MCR0 |= FLEXSPI_MCR0_SWRESET; // purge stale data from FlexSPI's AHB FIFO
  214. while (FLEXSPI_MCR0 & FLEXSPI_MCR0_SWRESET) ; // wait
  215. __enable_irq();
  216. }
  217. // write bytes into flash memory (which is already erased to 0xFF)
  218. void eepromemu_flash_write(void *addr, const void *data, uint32_t len)
  219. {
  220. __disable_irq();
  221. FLEXSPI_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  222. FLEXSPI_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  223. FLEXSPI_IPCR0 = 0;
  224. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x06); // 06 = write enable
  225. FLEXSPI_LUT61 = 0;
  226. FLEXSPI_LUT62 = 0;
  227. FLEXSPI_LUT63 = 0;
  228. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  229. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  230. arm_dcache_delete(addr, len); // purge old data from ARM's cache
  231. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  232. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  233. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x32) | LUT1(ADDR_SDR, PINS1, 24); // 32 = quad write
  234. FLEXSPI_LUT61 = LUT0(WRITE_SDR, PINS4, 1);
  235. FLEXSPI_IPTXFCR = FLEXSPI_IPTXFCR_CLRIPTXF; // clear tx fifo
  236. FLEXSPI_IPCR0 = (uint32_t)addr & 0x00FFFFFF;
  237. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15) | FLEXSPI_IPCR1_IDATSZ(len);
  238. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  239. const uint8_t *src = (const uint8_t *)data;
  240. uint32_t n;
  241. while (!((n = FLEXSPI_INTR) & FLEXSPI_INTR_IPCMDDONE)) {
  242. if (n & FLEXSPI_INTR_IPTXWE) {
  243. uint32_t wrlen = len;
  244. if (wrlen > 8) wrlen = 8;
  245. if (wrlen > 0) {
  246. memcpy((void *)&FLEXSPI_TFDR0, src, wrlen);
  247. src += wrlen;
  248. len -= wrlen;
  249. }
  250. FLEXSPI_INTR = FLEXSPI_INTR_IPTXWE;
  251. }
  252. }
  253. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE | FLEXSPI_INTR_IPTXWE;
  254. flash_wait();
  255. }
  256. // erase a 4K sector
  257. void eepromemu_flash_erase_sector(void *addr)
  258. {
  259. __disable_irq();
  260. FLEXSPI_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  261. FLEXSPI_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  262. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x06); // 06 = write enable
  263. FLEXSPI_LUT61 = 0;
  264. FLEXSPI_LUT62 = 0;
  265. FLEXSPI_LUT63 = 0;
  266. FLEXSPI_IPCR0 = 0;
  267. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  268. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  269. arm_dcache_delete((void *)((uint32_t)addr & 0xFFFFF000), 4096); // purge data from cache
  270. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  271. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  272. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x20) | LUT1(ADDR_SDR, PINS1, 24); // 20 = sector erase
  273. FLEXSPI_IPCR0 = (uint32_t)addr & 0x00FFF000;
  274. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  275. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  276. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  277. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  278. flash_wait();
  279. }