選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

eeprom.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. static void flash_write(void *addr, const void *data, uint32_t len);
  54. static void flash_erase_sector(void *addr);
  55. static uint8_t initialized=0;
  56. static uint16_t sector_index[FLASH_SECTORS];
  57. void eeprom_initialize(void)
  58. {
  59. uint32_t sector;
  60. //printf("eeprom init\n");
  61. for (sector=0; sector < FLASH_SECTORS; sector++) {
  62. const uint16_t *p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  63. const uint16_t *end = (uint16_t *)(FLASH_BASEADDR + (sector + 1) * 4096);
  64. uint16_t index = 0;
  65. do {
  66. if (*p++ == 0xFFFF) break;
  67. index++;
  68. } while (p < end);
  69. sector_index[sector] = index;
  70. }
  71. initialized = 1;
  72. }
  73. uint8_t eeprom_read_byte(const uint8_t *addr_ptr)
  74. {
  75. uint32_t addr = (uint32_t)addr_ptr;
  76. uint32_t sector, offset;
  77. const uint16_t *p, *end;
  78. uint8_t data=0xFF;
  79. if (addr > E2END) return 0xFF;
  80. if (!initialized) eeprom_initialize();
  81. sector = (addr >> 2) % FLASH_SECTORS;
  82. offset = (addr & 3) | (((addr >> 2) / FLASH_SECTORS) << 2);
  83. //printf("ee_rd, addr=%u, sector=%u, offset=%u, len=%u\n",
  84. //addr, sector, offset, sector_index[sector]);
  85. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  86. end = p + sector_index[sector];
  87. while (p < end) {
  88. uint32_t val = *p++;
  89. if ((val & 255) == offset) data = val >> 8;
  90. }
  91. return data;
  92. }
  93. void eeprom_write_byte(uint8_t *addr_ptr, uint8_t data)
  94. {
  95. uint32_t addr = (uint32_t)addr_ptr;
  96. uint32_t sector, offset, index, i;
  97. uint16_t *p, *end;
  98. uint8_t olddata=0xFF;
  99. uint8_t buf[256];
  100. if (addr > E2END) return;
  101. if (!initialized) eeprom_initialize();
  102. sector = (addr >> 2) % FLASH_SECTORS;
  103. offset = (addr & 3) | (((addr >> 2) / FLASH_SECTORS) << 2);
  104. //printf("ee_wr, addr=%u, sector=%u, offset=%u, len=%u\n",
  105. //addr, sector, offset, sector_index[sector]);
  106. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  107. end = p + sector_index[sector];
  108. while (p < end) {
  109. uint16_t val = *p++;
  110. if ((val & 255) == offset) olddata = val >> 8;
  111. }
  112. if (data == olddata) return;
  113. if (sector_index[sector] < 2048) {
  114. //printf("ee_wr, writing\n");
  115. uint16_t newdata = offset | (data << 8);
  116. flash_write(end, &newdata, 2);
  117. sector_index[sector] = sector_index[sector] + 1;
  118. } else {
  119. //printf("ee_wr, erase then write\n");
  120. memset(buf, 0xFF, sizeof(buf));
  121. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  122. end = p + 2048;
  123. while (p < end) {
  124. uint16_t val = *p++;
  125. buf[val & 255] = val >> 8;
  126. }
  127. buf[offset] = data;
  128. p = (uint16_t *)(FLASH_BASEADDR + sector * 4096);
  129. flash_erase_sector(p);
  130. index = 0;
  131. for (i=0; i < 256; i++) {
  132. if (buf[i] != 0xFF) {
  133. // TODO: combining these to larger write
  134. // would (probably) be more efficient
  135. uint16_t newval = i | (buf[i] << 8);
  136. flash_write(p + index, &newval, 2);
  137. index = index + 1;
  138. }
  139. }
  140. sector_index[sector] = index;
  141. }
  142. }
  143. uint16_t eeprom_read_word(const uint16_t *addr)
  144. {
  145. const uint8_t *p = (const uint8_t *)addr;
  146. return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8);
  147. }
  148. uint32_t eeprom_read_dword(const uint32_t *addr)
  149. {
  150. const uint8_t *p = (const uint8_t *)addr;
  151. return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8)
  152. | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24);
  153. }
  154. void eeprom_read_block(void *buf, const void *addr, uint32_t len)
  155. {
  156. const uint8_t *p = (const uint8_t *)addr;
  157. uint8_t *dest = (uint8_t *)buf;
  158. while (len--) {
  159. *dest++ = eeprom_read_byte(p++);
  160. }
  161. }
  162. int eeprom_is_ready(void)
  163. {
  164. return 1;
  165. }
  166. void eeprom_write_word(uint16_t *addr, uint16_t value)
  167. {
  168. uint8_t *p = (uint8_t *)addr;
  169. eeprom_write_byte(p++, value);
  170. eeprom_write_byte(p, value >> 8);
  171. }
  172. void eeprom_write_dword(uint32_t *addr, uint32_t value)
  173. {
  174. uint8_t *p = (uint8_t *)addr;
  175. eeprom_write_byte(p++, value);
  176. eeprom_write_byte(p++, value >> 8);
  177. eeprom_write_byte(p++, value >> 16);
  178. eeprom_write_byte(p, value >> 24);
  179. }
  180. void eeprom_write_block(const void *buf, void *addr, uint32_t len)
  181. {
  182. uint8_t *p = (uint8_t *)addr;
  183. const uint8_t *src = (const uint8_t *)buf;
  184. while (len--) {
  185. eeprom_write_byte(p++, *src++);
  186. }
  187. }
  188. #define LUT0(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)))
  189. #define LUT1(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)) << 16)
  190. #define CMD_SDR FLEXSPI_LUT_OPCODE_CMD_SDR
  191. #define ADDR_SDR FLEXSPI_LUT_OPCODE_RADDR_SDR
  192. #define READ_SDR FLEXSPI_LUT_OPCODE_READ_SDR
  193. #define WRITE_SDR FLEXSPI_LUT_OPCODE_WRITE_SDR
  194. #define PINS1 FLEXSPI_LUT_NUM_PADS_1
  195. #define PINS4 FLEXSPI_LUT_NUM_PADS_4
  196. static void flash_wait()
  197. {
  198. FLEXSPI_LUT60 = LUT0(CMD_SDR, PINS1, 0x05) | LUT1(READ_SDR, PINS1, 1); // 05 = read status
  199. FLEXSPI_LUT61 = 0;
  200. uint8_t status;
  201. do {
  202. FLEXSPI_IPRXFCR = FLEXSPI_IPRXFCR_CLRIPRXF; // clear rx fifo
  203. FLEXSPI_IPCR0 = 0;
  204. FLEXSPI_IPCR1 = FLEXSPI_IPCR1_ISEQID(15) | FLEXSPI_IPCR1_IDATSZ(1);
  205. FLEXSPI_IPCMD = FLEXSPI_IPCMD_TRG;
  206. while (!(FLEXSPI_INTR & FLEXSPI_INTR_IPCMDDONE)) {
  207. asm("nop");
  208. }
  209. FLEXSPI_INTR = FLEXSPI_INTR_IPCMDDONE;
  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. static void 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 & 0x007FFFFF;
  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. static void 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 & 0x007FF000;
  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. }