eeprom_teensy.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #include <ch.h>
  2. #include <hal.h>
  3. #include "eeconfig.h"
  4. /*************************************/
  5. /* Hardware backend */
  6. /* */
  7. /* Code from PJRC/Teensyduino */
  8. /*************************************/
  9. /* Teensyduino Core Library
  10. * http://www.pjrc.com/teensy/
  11. * Copyright (c) 2013 PJRC.COM, LLC.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this software and associated documentation files (the
  15. * "Software"), to deal in the Software without restriction, including
  16. * without limitation the rights to use, copy, modify, merge, publish,
  17. * distribute, sublicense, and/or sell copies of the Software, and to
  18. * permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * 1. The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * 2. If the Software is incorporated into a build system that allows
  25. * selection among a list of target devices, then similar target
  26. * devices manufactured by PJRC.COM must be included in the list of
  27. * target devices and selectable in the same manner.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  33. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  35. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  36. * SOFTWARE.
  37. */
  38. #define SMC_PMSTAT_RUN ((uint8_t)0x01)
  39. #define SMC_PMSTAT_HSRUN ((uint8_t)0x80)
  40. #define F_CPU KINETIS_SYSCLK_FREQUENCY
  41. static inline int kinetis_hsrun_disable(void) {
  42. #if defined(MK66F18)
  43. if (SMC->PMSTAT == SMC_PMSTAT_HSRUN) {
  44. // First, reduce the CPU clock speed, but do not change
  45. // the peripheral speed (F_BUS). Serial1 & Serial2 baud
  46. // rates will be impacted, but most other peripherals
  47. // will continue functioning at the same speed.
  48. # if F_CPU == 256000000 && F_BUS == 64000000
  49. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // TODO: TEST
  50. # elif F_CPU == 256000000 && F_BUS == 128000000
  51. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // TODO: TEST
  52. # elif F_CPU == 240000000 && F_BUS == 60000000
  53. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
  54. # elif F_CPU == 240000000 && F_BUS == 80000000
  55. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  56. # elif F_CPU == 240000000 && F_BUS == 120000000
  57. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  58. # elif F_CPU == 216000000 && F_BUS == 54000000
  59. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
  60. # elif F_CPU == 216000000 && F_BUS == 72000000
  61. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  62. # elif F_CPU == 216000000 && F_BUS == 108000000
  63. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  64. # elif F_CPU == 192000000 && F_BUS == 48000000
  65. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
  66. # elif F_CPU == 192000000 && F_BUS == 64000000
  67. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  68. # elif F_CPU == 192000000 && F_BUS == 96000000
  69. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  70. # elif F_CPU == 180000000 && F_BUS == 60000000
  71. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  72. # elif F_CPU == 180000000 && F_BUS == 90000000
  73. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  74. # elif F_CPU == 168000000 && F_BUS == 56000000
  75. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5); // ok
  76. # elif F_CPU == 144000000 && F_BUS == 48000000
  77. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5); // ok
  78. # elif F_CPU == 144000000 && F_BUS == 72000000
  79. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 5); // ok
  80. # elif F_CPU == 120000000 && F_BUS == 60000000
  81. SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(KINETIS_CLKDIV1_OUTDIV1 - 1) | SIM_CLKDIV1_OUTDIV2(KINETIS_CLKDIV1_OUTDIV2 - 1) |
  82. # if defined(MK66F18)
  83. SIM_CLKDIV1_OUTDIV3(KINETIS_CLKDIV1_OUTDIV3 - 1) |
  84. # endif
  85. SIM_CLKDIV1_OUTDIV4(KINETIS_CLKDIV1_OUTDIV4 - 1);
  86. # else
  87. return 0;
  88. # endif
  89. // Then turn off HSRUN mode
  90. SMC->PMCTRL = SMC_PMCTRL_RUNM_SET(0);
  91. while (SMC->PMSTAT == SMC_PMSTAT_HSRUN)
  92. ; // wait
  93. return 1;
  94. }
  95. #endif
  96. return 0;
  97. }
  98. static inline int kinetis_hsrun_enable(void) {
  99. #if defined(MK66F18)
  100. if (SMC->PMSTAT == SMC_PMSTAT_RUN) {
  101. // Turn HSRUN mode on
  102. SMC->PMCTRL = SMC_PMCTRL_RUNM_SET(3);
  103. while (SMC->PMSTAT != SMC_PMSTAT_HSRUN) {
  104. ;
  105. } // wait
  106. // Then configure clock for full speed
  107. # if F_CPU == 256000000 && F_BUS == 64000000
  108. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
  109. # elif F_CPU == 256000000 && F_BUS == 128000000
  110. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
  111. # elif F_CPU == 240000000 && F_BUS == 60000000
  112. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
  113. # elif F_CPU == 240000000 && F_BUS == 80000000
  114. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
  115. # elif F_CPU == 240000000 && F_BUS == 120000000
  116. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
  117. # elif F_CPU == 216000000 && F_BUS == 54000000
  118. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
  119. # elif F_CPU == 216000000 && F_BUS == 72000000
  120. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
  121. # elif F_CPU == 216000000 && F_BUS == 108000000
  122. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
  123. # elif F_CPU == 192000000 && F_BUS == 48000000
  124. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 6);
  125. # elif F_CPU == 192000000 && F_BUS == 64000000
  126. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
  127. # elif F_CPU == 192000000 && F_BUS == 96000000
  128. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
  129. # elif F_CPU == 180000000 && F_BUS == 60000000
  130. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
  131. # elif F_CPU == 180000000 && F_BUS == 90000000
  132. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
  133. # elif F_CPU == 168000000 && F_BUS == 56000000
  134. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 5);
  135. # elif F_CPU == 144000000 && F_BUS == 48000000
  136. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 4);
  137. # elif F_CPU == 144000000 && F_BUS == 72000000
  138. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 4);
  139. # elif F_CPU == 120000000 && F_BUS == 60000000
  140. SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(KINETIS_CLKDIV1_OUTDIV1 - 1) | SIM_CLKDIV1_OUTDIV2(KINETIS_CLKDIV1_OUTDIV2 - 1) |
  141. # if defined(MK66F18)
  142. SIM_CLKDIV1_OUTDIV3(KINETIS_CLKDIV1_OUTDIV3 - 1) |
  143. # endif
  144. SIM_CLKDIV1_OUTDIV4(KINETIS_CLKDIV1_OUTDIV4 - 1);
  145. # else
  146. return 0;
  147. # endif
  148. return 1;
  149. }
  150. #endif
  151. return 0;
  152. }
  153. #if defined(K20x) || defined(MK66F18) /* chip selection */
  154. /* Teensy 3.0, 3.1, 3.2; mchck; infinity keyboard */
  155. // The EEPROM is really RAM with a hardware-based backup system to
  156. // flash memory. Selecting a smaller size EEPROM allows more wear
  157. // leveling, for higher write endurance. If you edit this file,
  158. // set this to the smallest size your application can use. Also,
  159. // due to Freescale's implementation, writing 16 or 32 bit words
  160. // (aligned to 2 or 4 byte boundaries) has twice the endurance
  161. // compared to writing 8 bit bytes.
  162. //
  163. # ifndef EEPROM_SIZE
  164. # define EEPROM_SIZE 32
  165. # endif
  166. /*
  167. ^^^ Here be dragons:
  168. NXP AppNote AN4282 section 3.1 states that partitioning must only be done once.
  169. Once EEPROM partitioning is done, the size is locked to this initial configuration.
  170. Attempts to modify the EEPROM_SIZE setting may brick your board.
  171. */
  172. // Writing unaligned 16 or 32 bit data is handled automatically when
  173. // this is defined, but at a cost of extra code size. Without this,
  174. // any unaligned write will cause a hard fault exception! If you're
  175. // absolutely sure all 16 and 32 bit writes will be aligned, you can
  176. // remove the extra unnecessary code.
  177. //
  178. # define HANDLE_UNALIGNED_WRITES
  179. # if defined(K20x)
  180. # define EEPROM_MAX 2048
  181. # define EEPARTITION 0x03 // all 32K dataflash for EEPROM, none for Data
  182. # define EEESPLIT 0x30 // must be 0x30 on these chips
  183. # elif defined(MK66F18)
  184. # define EEPROM_MAX 4096
  185. # define EEPARTITION 0x05 // 128K dataflash for EEPROM, 128K for Data
  186. # define EEESPLIT 0x10 // best endurance: 0x00 = first 12%, 0x10 = first 25%, 0x30 = all equal
  187. # endif
  188. // Minimum EEPROM Endurance
  189. // ------------------------
  190. # if (EEPROM_SIZE == 4096)
  191. # define EEESIZE 0x02
  192. # elif (EEPROM_SIZE == 2048) // 35000 writes/byte or 70000 writes/word
  193. # define EEESIZE 0x03
  194. # elif (EEPROM_SIZE == 1024) // 75000 writes/byte or 150000 writes/word
  195. # define EEESIZE 0x04
  196. # elif (EEPROM_SIZE == 512) // 155000 writes/byte or 310000 writes/word
  197. # define EEESIZE 0x05
  198. # elif (EEPROM_SIZE == 256) // 315000 writes/byte or 630000 writes/word
  199. # define EEESIZE 0x06
  200. # elif (EEPROM_SIZE == 128) // 635000 writes/byte or 1270000 writes/word
  201. # define EEESIZE 0x07
  202. # elif (EEPROM_SIZE == 64) // 1275000 writes/byte or 2550000 writes/word
  203. # define EEESIZE 0x08
  204. # elif (EEPROM_SIZE == 32) // 2555000 writes/byte or 5110000 writes/word
  205. # define EEESIZE 0x09
  206. # endif
  207. /** \brief eeprom initialization
  208. *
  209. * FIXME: needs doc
  210. */
  211. void eeprom_initialize(void) {
  212. uint32_t count = 0;
  213. uint16_t do_flash_cmd[] = {0xf06f, 0x037f, 0x7003, 0x7803, 0xf013, 0x0f80, 0xd0fb, 0x4770};
  214. uint8_t status;
  215. if (FTFL->FCNFG & FTFL_FCNFG_RAMRDY) {
  216. uint8_t stat = FTFL->FSTAT & 0x70;
  217. if (stat) FTFL->FSTAT = stat;
  218. // FlexRAM is configured as traditional RAM
  219. // We need to reconfigure for EEPROM usage
  220. kinetis_hsrun_disable();
  221. FTFL->FCCOB0 = 0x80; // PGMPART = Program Partition Command
  222. FTFL->FCCOB3 = 0;
  223. FTFL->FCCOB4 = EEESPLIT | EEESIZE;
  224. FTFL->FCCOB5 = EEPARTITION;
  225. __disable_irq();
  226. // do_flash_cmd() must execute from RAM. Luckily the C syntax is simple...
  227. (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFL->FSTAT));
  228. __enable_irq();
  229. kinetis_hsrun_enable();
  230. status = FTFL->FSTAT;
  231. if (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL)) {
  232. FTFL->FSTAT = (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL));
  233. return; // error
  234. }
  235. }
  236. // wait for eeprom to become ready (is this really necessary?)
  237. while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
  238. if (++count > 200000) break;
  239. }
  240. }
  241. # define FlexRAM ((volatile uint8_t *)0x14000000)
  242. /** \brief eeprom read byte
  243. *
  244. * FIXME: needs doc
  245. */
  246. uint8_t eeprom_read_byte(const uint8_t *addr) {
  247. uint32_t offset = (uint32_t)addr;
  248. if (offset >= EEPROM_SIZE) return 0;
  249. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  250. return FlexRAM[offset];
  251. }
  252. /** \brief eeprom read word
  253. *
  254. * FIXME: needs doc
  255. */
  256. uint16_t eeprom_read_word(const uint16_t *addr) {
  257. uint32_t offset = (uint32_t)addr;
  258. if (offset >= EEPROM_SIZE - 1) return 0;
  259. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  260. return *(uint16_t *)(&FlexRAM[offset]);
  261. }
  262. /** \brief eeprom read dword
  263. *
  264. * FIXME: needs doc
  265. */
  266. uint32_t eeprom_read_dword(const uint32_t *addr) {
  267. uint32_t offset = (uint32_t)addr;
  268. if (offset >= EEPROM_SIZE - 3) return 0;
  269. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  270. return *(uint32_t *)(&FlexRAM[offset]);
  271. }
  272. /** \brief eeprom read block
  273. *
  274. * FIXME: needs doc
  275. */
  276. void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
  277. uint32_t offset = (uint32_t)addr;
  278. uint8_t *dest = (uint8_t *)buf;
  279. uint32_t end = offset + len;
  280. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  281. if (end > EEPROM_SIZE) end = EEPROM_SIZE;
  282. while (offset < end) {
  283. *dest++ = FlexRAM[offset++];
  284. }
  285. }
  286. /** \brief eeprom is ready
  287. *
  288. * FIXME: needs doc
  289. */
  290. int eeprom_is_ready(void) { return (FTFL->FCNFG & FTFL_FCNFG_EEERDY) ? 1 : 0; }
  291. /** \brief flexram wait
  292. *
  293. * FIXME: needs doc
  294. */
  295. static void flexram_wait(void) {
  296. while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
  297. // TODO: timeout
  298. }
  299. }
  300. /** \brief eeprom_write_byte
  301. *
  302. * FIXME: needs doc
  303. */
  304. void eeprom_write_byte(uint8_t *addr, uint8_t value) {
  305. uint32_t offset = (uint32_t)addr;
  306. if (offset >= EEPROM_SIZE) return;
  307. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  308. if (FlexRAM[offset] != value) {
  309. kinetis_hsrun_disable();
  310. uint8_t stat = FTFL->FSTAT & 0x70;
  311. if (stat) FTFL->FSTAT = stat;
  312. FlexRAM[offset] = value;
  313. flexram_wait();
  314. kinetis_hsrun_enable();
  315. }
  316. }
  317. /** \brief eeprom write word
  318. *
  319. * FIXME: needs doc
  320. */
  321. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  322. uint32_t offset = (uint32_t)addr;
  323. if (offset >= EEPROM_SIZE - 1) return;
  324. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  325. # ifdef HANDLE_UNALIGNED_WRITES
  326. if ((offset & 1) == 0) {
  327. # endif
  328. if (*(uint16_t *)(&FlexRAM[offset]) != value) {
  329. kinetis_hsrun_disable();
  330. uint8_t stat = FTFL->FSTAT & 0x70;
  331. if (stat) FTFL->FSTAT = stat;
  332. *(uint16_t *)(&FlexRAM[offset]) = value;
  333. flexram_wait();
  334. kinetis_hsrun_enable();
  335. }
  336. # ifdef HANDLE_UNALIGNED_WRITES
  337. } else {
  338. if (FlexRAM[offset] != value) {
  339. kinetis_hsrun_disable();
  340. uint8_t stat = FTFL->FSTAT & 0x70;
  341. if (stat) FTFL->FSTAT = stat;
  342. FlexRAM[offset] = value;
  343. flexram_wait();
  344. kinetis_hsrun_enable();
  345. }
  346. if (FlexRAM[offset + 1] != (value >> 8)) {
  347. kinetis_hsrun_disable();
  348. uint8_t stat = FTFL->FSTAT & 0x70;
  349. if (stat) FTFL->FSTAT = stat;
  350. FlexRAM[offset + 1] = value >> 8;
  351. flexram_wait();
  352. kinetis_hsrun_enable();
  353. }
  354. }
  355. # endif
  356. }
  357. /** \brief eeprom write dword
  358. *
  359. * FIXME: needs doc
  360. */
  361. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  362. uint32_t offset = (uint32_t)addr;
  363. if (offset >= EEPROM_SIZE - 3) return;
  364. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  365. # ifdef HANDLE_UNALIGNED_WRITES
  366. switch (offset & 3) {
  367. case 0:
  368. # endif
  369. if (*(uint32_t *)(&FlexRAM[offset]) != value) {
  370. kinetis_hsrun_disable();
  371. uint8_t stat = FTFL->FSTAT & 0x70;
  372. if (stat) FTFL->FSTAT = stat;
  373. *(uint32_t *)(&FlexRAM[offset]) = value;
  374. flexram_wait();
  375. kinetis_hsrun_enable();
  376. }
  377. return;
  378. # ifdef HANDLE_UNALIGNED_WRITES
  379. case 2:
  380. if (*(uint16_t *)(&FlexRAM[offset]) != value) {
  381. kinetis_hsrun_disable();
  382. uint8_t stat = FTFL->FSTAT & 0x70;
  383. if (stat) FTFL->FSTAT = stat;
  384. *(uint16_t *)(&FlexRAM[offset]) = value;
  385. flexram_wait();
  386. kinetis_hsrun_enable();
  387. }
  388. if (*(uint16_t *)(&FlexRAM[offset + 2]) != (value >> 16)) {
  389. kinetis_hsrun_disable();
  390. uint8_t stat = FTFL->FSTAT & 0x70;
  391. if (stat) FTFL->FSTAT = stat;
  392. *(uint16_t *)(&FlexRAM[offset + 2]) = value >> 16;
  393. flexram_wait();
  394. kinetis_hsrun_enable();
  395. }
  396. return;
  397. default:
  398. if (FlexRAM[offset] != value) {
  399. kinetis_hsrun_disable();
  400. uint8_t stat = FTFL->FSTAT & 0x70;
  401. if (stat) FTFL->FSTAT = stat;
  402. FlexRAM[offset] = value;
  403. flexram_wait();
  404. kinetis_hsrun_enable();
  405. }
  406. if (*(uint16_t *)(&FlexRAM[offset + 1]) != (value >> 8)) {
  407. kinetis_hsrun_disable();
  408. uint8_t stat = FTFL->FSTAT & 0x70;
  409. if (stat) FTFL->FSTAT = stat;
  410. *(uint16_t *)(&FlexRAM[offset + 1]) = value >> 8;
  411. flexram_wait();
  412. kinetis_hsrun_enable();
  413. }
  414. if (FlexRAM[offset + 3] != (value >> 24)) {
  415. kinetis_hsrun_disable();
  416. uint8_t stat = FTFL->FSTAT & 0x70;
  417. if (stat) FTFL->FSTAT = stat;
  418. FlexRAM[offset + 3] = value >> 24;
  419. flexram_wait();
  420. kinetis_hsrun_enable();
  421. }
  422. }
  423. # endif
  424. }
  425. /** \brief eeprom write block
  426. *
  427. * FIXME: needs doc
  428. */
  429. void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
  430. uint32_t offset = (uint32_t)addr;
  431. const uint8_t *src = (const uint8_t *)buf;
  432. if (offset >= EEPROM_SIZE) return;
  433. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  434. if (len >= EEPROM_SIZE) len = EEPROM_SIZE;
  435. if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset;
  436. kinetis_hsrun_disable();
  437. while (len > 0) {
  438. uint32_t lsb = offset & 3;
  439. if (lsb == 0 && len >= 4) {
  440. // write aligned 32 bits
  441. uint32_t val32;
  442. val32 = *src++;
  443. val32 |= (*src++ << 8);
  444. val32 |= (*src++ << 16);
  445. val32 |= (*src++ << 24);
  446. if (*(uint32_t *)(&FlexRAM[offset]) != val32) {
  447. uint8_t stat = FTFL->FSTAT & 0x70;
  448. if (stat) FTFL->FSTAT = stat;
  449. *(uint32_t *)(&FlexRAM[offset]) = val32;
  450. flexram_wait();
  451. }
  452. offset += 4;
  453. len -= 4;
  454. } else if ((lsb == 0 || lsb == 2) && len >= 2) {
  455. // write aligned 16 bits
  456. uint16_t val16;
  457. val16 = *src++;
  458. val16 |= (*src++ << 8);
  459. if (*(uint16_t *)(&FlexRAM[offset]) != val16) {
  460. uint8_t stat = FTFL->FSTAT & 0x70;
  461. if (stat) FTFL->FSTAT = stat;
  462. *(uint16_t *)(&FlexRAM[offset]) = val16;
  463. flexram_wait();
  464. }
  465. offset += 2;
  466. len -= 2;
  467. } else {
  468. // write 8 bits
  469. uint8_t val8 = *src++;
  470. if (FlexRAM[offset] != val8) {
  471. uint8_t stat = FTFL->FSTAT & 0x70;
  472. if (stat) FTFL->FSTAT = stat;
  473. FlexRAM[offset] = val8;
  474. flexram_wait();
  475. }
  476. offset++;
  477. len--;
  478. }
  479. }
  480. kinetis_hsrun_enable();
  481. }
  482. /*
  483. void do_flash_cmd(volatile uint8_t *fstat)
  484. {
  485. *fstat = 0x80;
  486. while ((*fstat & 0x80) == 0) ; // wait
  487. }
  488. 00000000 <do_flash_cmd>:
  489. 0: f06f 037f mvn.w r3, #127 ; 0x7f
  490. 4: 7003 strb r3, [r0, #0]
  491. 6: 7803 ldrb r3, [r0, #0]
  492. 8: f013 0f80 tst.w r3, #128 ; 0x80
  493. c: d0fb beq.n 6 <do_flash_cmd+0x6>
  494. e: 4770 bx lr
  495. */
  496. #elif defined(KL2x) /* chip selection */
  497. /* Teensy LC (emulated) */
  498. # define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  499. extern uint32_t __eeprom_workarea_start__;
  500. extern uint32_t __eeprom_workarea_end__;
  501. # define EEPROM_SIZE 128
  502. static uint32_t flashend = 0;
  503. void eeprom_initialize(void) {
  504. const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
  505. do {
  506. if (*p++ == 0xFFFF) {
  507. flashend = (uint32_t)(p - 2);
  508. return;
  509. }
  510. } while (p < (uint16_t *)SYMVAL(__eeprom_workarea_end__));
  511. flashend = (uint32_t)(p - 1);
  512. }
  513. uint8_t eeprom_read_byte(const uint8_t *addr) {
  514. uint32_t offset = (uint32_t)addr;
  515. const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
  516. const uint16_t *end = (const uint16_t *)((uint32_t)flashend);
  517. uint16_t val;
  518. uint8_t data = 0xFF;
  519. if (!end) {
  520. eeprom_initialize();
  521. end = (const uint16_t *)((uint32_t)flashend);
  522. }
  523. if (offset < EEPROM_SIZE) {
  524. while (p <= end) {
  525. val = *p++;
  526. if ((val & 255) == offset) data = val >> 8;
  527. }
  528. }
  529. return data;
  530. }
  531. static void flash_write(const uint16_t *code, uint32_t addr, uint32_t data) {
  532. // with great power comes great responsibility....
  533. uint32_t stat;
  534. *(uint32_t *)&(FTFA->FCCOB3) = 0x06000000 | (addr & 0x00FFFFFC);
  535. *(uint32_t *)&(FTFA->FCCOB7) = data;
  536. __disable_irq();
  537. (*((void (*)(volatile uint8_t *))((uint32_t)code | 1)))(&(FTFA->FSTAT));
  538. __enable_irq();
  539. stat = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
  540. if (stat) {
  541. FTFA->FSTAT = stat;
  542. }
  543. MCM->PLACR |= MCM_PLACR_CFCC;
  544. }
  545. void eeprom_write_byte(uint8_t *addr, uint8_t data) {
  546. uint32_t offset = (uint32_t)addr;
  547. const uint16_t *p, *end = (const uint16_t *)((uint32_t)flashend);
  548. uint32_t i, val, flashaddr;
  549. uint16_t do_flash_cmd[] = {0x2380, 0x7003, 0x7803, 0xb25b, 0x2b00, 0xdafb, 0x4770};
  550. uint8_t buf[EEPROM_SIZE];
  551. if (offset >= EEPROM_SIZE) return;
  552. if (!end) {
  553. eeprom_initialize();
  554. end = (const uint16_t *)((uint32_t)flashend);
  555. }
  556. if (++end < (uint16_t *)SYMVAL(__eeprom_workarea_end__)) {
  557. val = (data << 8) | offset;
  558. flashaddr = (uint32_t)end;
  559. flashend = flashaddr;
  560. if ((flashaddr & 2) == 0) {
  561. val |= 0xFFFF0000;
  562. } else {
  563. val <<= 16;
  564. val |= 0x0000FFFF;
  565. }
  566. flash_write(do_flash_cmd, flashaddr, val);
  567. } else {
  568. for (i = 0; i < EEPROM_SIZE; i++) {
  569. buf[i] = 0xFF;
  570. }
  571. val = 0;
  572. for (p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); p < (uint16_t *)SYMVAL(__eeprom_workarea_end__); p++) {
  573. val = *p;
  574. if ((val & 255) < EEPROM_SIZE) {
  575. buf[val & 255] = val >> 8;
  576. }
  577. }
  578. buf[offset] = data;
  579. for (flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); flashaddr < (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_end__); flashaddr += 1024) {
  580. *(uint32_t *)&(FTFA->FCCOB3) = 0x09000000 | flashaddr;
  581. __disable_irq();
  582. (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFA->FSTAT));
  583. __enable_irq();
  584. val = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
  585. ;
  586. if (val) FTFA->FSTAT = val;
  587. MCM->PLACR |= MCM_PLACR_CFCC;
  588. }
  589. flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__);
  590. for (i = 0; i < EEPROM_SIZE; i++) {
  591. if (buf[i] == 0xFF) continue;
  592. if ((flashaddr & 2) == 0) {
  593. val = (buf[i] << 8) | i;
  594. } else {
  595. val = val | (buf[i] << 24) | (i << 16);
  596. flash_write(do_flash_cmd, flashaddr, val);
  597. }
  598. flashaddr += 2;
  599. }
  600. flashend = flashaddr;
  601. if ((flashaddr & 2)) {
  602. val |= 0xFFFF0000;
  603. flash_write(do_flash_cmd, flashaddr, val);
  604. }
  605. }
  606. }
  607. /*
  608. void do_flash_cmd(volatile uint8_t *fstat)
  609. {
  610. *fstat = 0x80;
  611. while ((*fstat & 0x80) == 0) ; // wait
  612. }
  613. 00000000 <do_flash_cmd>:
  614. 0: 2380 movs r3, #128 ; 0x80
  615. 2: 7003 strb r3, [r0, #0]
  616. 4: 7803 ldrb r3, [r0, #0]
  617. 6: b25b sxtb r3, r3
  618. 8: 2b00 cmp r3, #0
  619. a: dafb bge.n 4 <do_flash_cmd+0x4>
  620. c: 4770 bx lr
  621. */
  622. uint16_t eeprom_read_word(const uint16_t *addr) {
  623. const uint8_t *p = (const uint8_t *)addr;
  624. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
  625. }
  626. uint32_t eeprom_read_dword(const uint32_t *addr) {
  627. const uint8_t *p = (const uint8_t *)addr;
  628. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
  629. }
  630. void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
  631. const uint8_t *p = (const uint8_t *)addr;
  632. uint8_t * dest = (uint8_t *)buf;
  633. while (len--) {
  634. *dest++ = eeprom_read_byte(p++);
  635. }
  636. }
  637. int eeprom_is_ready(void) { return 1; }
  638. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  639. uint8_t *p = (uint8_t *)addr;
  640. eeprom_write_byte(p++, value);
  641. eeprom_write_byte(p, value >> 8);
  642. }
  643. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  644. uint8_t *p = (uint8_t *)addr;
  645. eeprom_write_byte(p++, value);
  646. eeprom_write_byte(p++, value >> 8);
  647. eeprom_write_byte(p++, value >> 16);
  648. eeprom_write_byte(p, value >> 24);
  649. }
  650. void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
  651. uint8_t * p = (uint8_t *)addr;
  652. const uint8_t *src = (const uint8_t *)buf;
  653. while (len--) {
  654. eeprom_write_byte(p++, *src++);
  655. }
  656. }
  657. #else
  658. // No EEPROM supported, so emulate it
  659. # ifndef EEPROM_SIZE
  660. # include "eeconfig.h"
  661. # define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
  662. # endif
  663. __attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE];
  664. uint8_t eeprom_read_byte(const uint8_t *addr) {
  665. uint32_t offset = (uint32_t)addr;
  666. return buffer[offset];
  667. }
  668. void eeprom_write_byte(uint8_t *addr, uint8_t value) {
  669. uint32_t offset = (uint32_t)addr;
  670. buffer[offset] = value;
  671. }
  672. uint16_t eeprom_read_word(const uint16_t *addr) {
  673. const uint8_t *p = (const uint8_t *)addr;
  674. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
  675. }
  676. uint32_t eeprom_read_dword(const uint32_t *addr) {
  677. const uint8_t *p = (const uint8_t *)addr;
  678. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
  679. }
  680. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  681. const uint8_t *p = (const uint8_t *)addr;
  682. uint8_t * dest = (uint8_t *)buf;
  683. while (len--) {
  684. *dest++ = eeprom_read_byte(p++);
  685. }
  686. }
  687. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  688. uint8_t *p = (uint8_t *)addr;
  689. eeprom_write_byte(p++, value);
  690. eeprom_write_byte(p, value >> 8);
  691. }
  692. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  693. uint8_t *p = (uint8_t *)addr;
  694. eeprom_write_byte(p++, value);
  695. eeprom_write_byte(p++, value >> 8);
  696. eeprom_write_byte(p++, value >> 16);
  697. eeprom_write_byte(p, value >> 24);
  698. }
  699. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  700. uint8_t * p = (uint8_t *)addr;
  701. const uint8_t *src = (const uint8_t *)buf;
  702. while (len--) {
  703. eeprom_write_byte(p++, *src++);
  704. }
  705. }
  706. #endif /* chip selection */
  707. // The update functions just calls write for now, but could probably be optimized
  708. void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); }
  709. void eeprom_update_word(uint16_t *addr, uint16_t value) {
  710. uint8_t *p = (uint8_t *)addr;
  711. eeprom_write_byte(p++, value);
  712. eeprom_write_byte(p, value >> 8);
  713. }
  714. void eeprom_update_dword(uint32_t *addr, uint32_t value) {
  715. uint8_t *p = (uint8_t *)addr;
  716. eeprom_write_byte(p++, value);
  717. eeprom_write_byte(p++, value >> 8);
  718. eeprom_write_byte(p++, value >> 16);
  719. eeprom_write_byte(p, value >> 24);
  720. }
  721. void eeprom_update_block(const void *buf, void *addr, size_t len) {
  722. uint8_t * p = (uint8_t *)addr;
  723. const uint8_t *src = (const uint8_t *)buf;
  724. while (len--) {
  725. eeprom_write_byte(p++, *src++);
  726. }
  727. }