您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

309 行
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. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  209. asm("":::"memory");
  210. status = *(uint8_t *)&FLEXSPI_RFDR0;
  211. } while (status & 1);
  212. FLEXSPI_MCR0 |= FLEXSPI_MCR0_SWRESET; // purge stale data from FlexSPI's AHB FIFO
  213. while (FLEXSPI_MCR0 & FLEXSPI_MCR0_SWRESET) ; // wait
  214. __enable_irq();
  215. }
  216. // write bytes into flash memory (which is already erased to 0xFF)
  217. void eepromemu_flash_write(void *addr, const void *data, uint32_t len)
  218. {
  219. __disable_irq();
  220. FLEXSPI_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  221. FLEXSPI_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  222. FLEXSPI_IPCR0 = 0;
  223. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x06); // 06 = write enable
  224. FLEXSPI_LUT61 = 0;
  225. FLEXSPI_LUT62 = 0;
  226. FLEXSPI_LUT63 = 0;
  227. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  228. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  229. arm_dcache_delete(addr, len); // purge old data from ARM's cache
  230. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  231. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  232. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x32) | LUT1(ADDR_SDR, PINS1, 24); // 32 = quad write
  233. FLEXSPI_LUT61 = LUT0(WRITE_SDR, PINS4, 1);
  234. FLEXSPI_IPTXFCR = FLEXSPI_IPTXFCR_CLRIPTXF; // clear tx fifo
  235. FLEXSPI_IPCR0 = (uint32_t)addr & 0x00FFFFFF;
  236. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15) | FLEXSPI_IPCR1_IDATSZ(len);
  237. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  238. const uint8_t *src = (const uint8_t *)data;
  239. uint32_t n;
  240. while (!((n = FLEXSPI_INTR) & FLEXSPI_INTR_IPCMDDONE)) {
  241. if (n & FLEXSPI_INTR_IPTXWE) {
  242. uint32_t wrlen = len;
  243. if (wrlen > 8) wrlen = 8;
  244. if (wrlen > 0) {
  245. memcpy((void *)&FLEXSPI_TFDR0, src, wrlen);
  246. src += wrlen;
  247. len -= wrlen;
  248. }
  249. FLEXSPI_INTR = FLEXSPI_INTR_IPTXWE;
  250. }
  251. }
  252. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE | FLEXSPI_INTR_IPTXWE;
  253. flash_wait();
  254. }
  255. // erase a 4K sector
  256. void eepromemu_flash_erase_sector(void *addr)
  257. {
  258. __disable_irq();
  259. FLEXSPI_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  260. FLEXSPI_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  261. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x06); // 06 = write enable
  262. FLEXSPI_LUT61 = 0;
  263. FLEXSPI_LUT62 = 0;
  264. FLEXSPI_LUT63 = 0;
  265. FLEXSPI_IPCR0 = 0;
  266. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  267. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  268. arm_dcache_delete((void *)((uint32_t)addr & 0xFFFFF000), 4096); // purge data from cache
  269. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  270. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  271. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x20) | LUT1(ADDR_SDR, PINS1, 24); // 20 = sector erase
  272. FLEXSPI_IPCR0 = (uint32_t)addr & 0x00FFF000;
  273. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15);
  274. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  275. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) ; // wait
  276. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  277. flash_wait();
  278. }