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

306 行
9.4KB

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