eeprom_teensy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. #if defined(K20x) /* chip selection */
  39. /* Teensy 3.0, 3.1, 3.2; mchck; infinity keyboard */
  40. // The EEPROM is really RAM with a hardware-based backup system to
  41. // flash memory. Selecting a smaller size EEPROM allows more wear
  42. // leveling, for higher write endurance. If you edit this file,
  43. // set this to the smallest size your application can use. Also,
  44. // due to Freescale's implementation, writing 16 or 32 bit words
  45. // (aligned to 2 or 4 byte boundaries) has twice the endurance
  46. // compared to writing 8 bit bytes.
  47. //
  48. # ifndef EEPROM_SIZE
  49. # define EEPROM_SIZE 32
  50. # endif
  51. /*
  52. ^^^ Here be dragons:
  53. NXP AppNote AN4282 section 3.1 states that partitioning must only be done once.
  54. Once EEPROM partitioning is done, the size is locked to this initial configuration.
  55. Attempts to modify the EEPROM_SIZE setting may brick your board.
  56. */
  57. // Writing unaligned 16 or 32 bit data is handled automatically when
  58. // this is defined, but at a cost of extra code size. Without this,
  59. // any unaligned write will cause a hard fault exception! If you're
  60. // absolutely sure all 16 and 32 bit writes will be aligned, you can
  61. // remove the extra unnecessary code.
  62. //
  63. # define HANDLE_UNALIGNED_WRITES
  64. // Minimum EEPROM Endurance
  65. // ------------------------
  66. # if (EEPROM_SIZE == 2048) // 35000 writes/byte or 70000 writes/word
  67. # define EEESIZE 0x33
  68. # elif (EEPROM_SIZE == 1024) // 75000 writes/byte or 150000 writes/word
  69. # define EEESIZE 0x34
  70. # elif (EEPROM_SIZE == 512) // 155000 writes/byte or 310000 writes/word
  71. # define EEESIZE 0x35
  72. # elif (EEPROM_SIZE == 256) // 315000 writes/byte or 630000 writes/word
  73. # define EEESIZE 0x36
  74. # elif (EEPROM_SIZE == 128) // 635000 writes/byte or 1270000 writes/word
  75. # define EEESIZE 0x37
  76. # elif (EEPROM_SIZE == 64) // 1275000 writes/byte or 2550000 writes/word
  77. # define EEESIZE 0x38
  78. # elif (EEPROM_SIZE == 32) // 2555000 writes/byte or 5110000 writes/word
  79. # define EEESIZE 0x39
  80. # endif
  81. /** \brief eeprom initialization
  82. *
  83. * FIXME: needs doc
  84. */
  85. void eeprom_initialize(void) {
  86. uint32_t count = 0;
  87. uint16_t do_flash_cmd[] = {0xf06f, 0x037f, 0x7003, 0x7803, 0xf013, 0x0f80, 0xd0fb, 0x4770};
  88. uint8_t status;
  89. if (FTFL->FCNFG & FTFL_FCNFG_RAMRDY) {
  90. // FlexRAM is configured as traditional RAM
  91. // We need to reconfigure for EEPROM usage
  92. FTFL->FCCOB0 = 0x80; // PGMPART = Program Partition Command
  93. FTFL->FCCOB4 = EEESIZE; // EEPROM Size
  94. FTFL->FCCOB5 = 0x03; // 0K for Dataflash, 32K for EEPROM backup
  95. __disable_irq();
  96. // do_flash_cmd() must execute from RAM. Luckily the C syntax is simple...
  97. (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFL->FSTAT));
  98. __enable_irq();
  99. status = FTFL->FSTAT;
  100. if (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL)) {
  101. FTFL->FSTAT = (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL));
  102. return; // error
  103. }
  104. }
  105. // wait for eeprom to become ready (is this really necessary?)
  106. while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
  107. if (++count > 20000) break;
  108. }
  109. }
  110. # define FlexRAM ((uint8_t *)0x14000000)
  111. /** \brief eeprom read byte
  112. *
  113. * FIXME: needs doc
  114. */
  115. uint8_t eeprom_read_byte(const uint8_t *addr) {
  116. uint32_t offset = (uint32_t)addr;
  117. if (offset >= EEPROM_SIZE) return 0;
  118. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  119. return FlexRAM[offset];
  120. }
  121. /** \brief eeprom read word
  122. *
  123. * FIXME: needs doc
  124. */
  125. uint16_t eeprom_read_word(const uint16_t *addr) {
  126. uint32_t offset = (uint32_t)addr;
  127. if (offset >= EEPROM_SIZE - 1) return 0;
  128. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  129. return *(uint16_t *)(&FlexRAM[offset]);
  130. }
  131. /** \brief eeprom read dword
  132. *
  133. * FIXME: needs doc
  134. */
  135. uint32_t eeprom_read_dword(const uint32_t *addr) {
  136. uint32_t offset = (uint32_t)addr;
  137. if (offset >= EEPROM_SIZE - 3) return 0;
  138. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  139. return *(uint32_t *)(&FlexRAM[offset]);
  140. }
  141. /** \brief eeprom read block
  142. *
  143. * FIXME: needs doc
  144. */
  145. void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
  146. uint32_t offset = (uint32_t)addr;
  147. uint8_t *dest = (uint8_t *)buf;
  148. uint32_t end = offset + len;
  149. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  150. if (end > EEPROM_SIZE) end = EEPROM_SIZE;
  151. while (offset < end) {
  152. *dest++ = FlexRAM[offset++];
  153. }
  154. }
  155. /** \brief eeprom is ready
  156. *
  157. * FIXME: needs doc
  158. */
  159. int eeprom_is_ready(void) { return (FTFL->FCNFG & FTFL_FCNFG_EEERDY) ? 1 : 0; }
  160. /** \brief flexram wait
  161. *
  162. * FIXME: needs doc
  163. */
  164. static void flexram_wait(void) {
  165. while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
  166. // TODO: timeout
  167. }
  168. }
  169. /** \brief eeprom_write_byte
  170. *
  171. * FIXME: needs doc
  172. */
  173. void eeprom_write_byte(uint8_t *addr, uint8_t value) {
  174. uint32_t offset = (uint32_t)addr;
  175. if (offset >= EEPROM_SIZE) return;
  176. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  177. if (FlexRAM[offset] != value) {
  178. FlexRAM[offset] = value;
  179. flexram_wait();
  180. }
  181. }
  182. /** \brief eeprom write word
  183. *
  184. * FIXME: needs doc
  185. */
  186. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  187. uint32_t offset = (uint32_t)addr;
  188. if (offset >= EEPROM_SIZE - 1) return;
  189. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  190. # ifdef HANDLE_UNALIGNED_WRITES
  191. if ((offset & 1) == 0) {
  192. # endif
  193. if (*(uint16_t *)(&FlexRAM[offset]) != value) {
  194. *(uint16_t *)(&FlexRAM[offset]) = value;
  195. flexram_wait();
  196. }
  197. # ifdef HANDLE_UNALIGNED_WRITES
  198. } else {
  199. if (FlexRAM[offset] != value) {
  200. FlexRAM[offset] = value;
  201. flexram_wait();
  202. }
  203. if (FlexRAM[offset + 1] != (value >> 8)) {
  204. FlexRAM[offset + 1] = value >> 8;
  205. flexram_wait();
  206. }
  207. }
  208. # endif
  209. }
  210. /** \brief eeprom write dword
  211. *
  212. * FIXME: needs doc
  213. */
  214. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  215. uint32_t offset = (uint32_t)addr;
  216. if (offset >= EEPROM_SIZE - 3) return;
  217. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  218. # ifdef HANDLE_UNALIGNED_WRITES
  219. switch (offset & 3) {
  220. case 0:
  221. # endif
  222. if (*(uint32_t *)(&FlexRAM[offset]) != value) {
  223. *(uint32_t *)(&FlexRAM[offset]) = value;
  224. flexram_wait();
  225. }
  226. return;
  227. # ifdef HANDLE_UNALIGNED_WRITES
  228. case 2:
  229. if (*(uint16_t *)(&FlexRAM[offset]) != value) {
  230. *(uint16_t *)(&FlexRAM[offset]) = value;
  231. flexram_wait();
  232. }
  233. if (*(uint16_t *)(&FlexRAM[offset + 2]) != (value >> 16)) {
  234. *(uint16_t *)(&FlexRAM[offset + 2]) = value >> 16;
  235. flexram_wait();
  236. }
  237. return;
  238. default:
  239. if (FlexRAM[offset] != value) {
  240. FlexRAM[offset] = value;
  241. flexram_wait();
  242. }
  243. if (*(uint16_t *)(&FlexRAM[offset + 1]) != (value >> 8)) {
  244. *(uint16_t *)(&FlexRAM[offset + 1]) = value >> 8;
  245. flexram_wait();
  246. }
  247. if (FlexRAM[offset + 3] != (value >> 24)) {
  248. FlexRAM[offset + 3] = value >> 24;
  249. flexram_wait();
  250. }
  251. }
  252. # endif
  253. }
  254. /** \brief eeprom write block
  255. *
  256. * FIXME: needs doc
  257. */
  258. void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
  259. uint32_t offset = (uint32_t)addr;
  260. const uint8_t *src = (const uint8_t *)buf;
  261. if (offset >= EEPROM_SIZE) return;
  262. if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
  263. if (len >= EEPROM_SIZE) len = EEPROM_SIZE;
  264. if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset;
  265. while (len > 0) {
  266. uint32_t lsb = offset & 3;
  267. if (lsb == 0 && len >= 4) {
  268. // write aligned 32 bits
  269. uint32_t val32;
  270. val32 = *src++;
  271. val32 |= (*src++ << 8);
  272. val32 |= (*src++ << 16);
  273. val32 |= (*src++ << 24);
  274. if (*(uint32_t *)(&FlexRAM[offset]) != val32) {
  275. *(uint32_t *)(&FlexRAM[offset]) = val32;
  276. flexram_wait();
  277. }
  278. offset += 4;
  279. len -= 4;
  280. } else if ((lsb == 0 || lsb == 2) && len >= 2) {
  281. // write aligned 16 bits
  282. uint16_t val16;
  283. val16 = *src++;
  284. val16 |= (*src++ << 8);
  285. if (*(uint16_t *)(&FlexRAM[offset]) != val16) {
  286. *(uint16_t *)(&FlexRAM[offset]) = val16;
  287. flexram_wait();
  288. }
  289. offset += 2;
  290. len -= 2;
  291. } else {
  292. // write 8 bits
  293. uint8_t val8 = *src++;
  294. if (FlexRAM[offset] != val8) {
  295. FlexRAM[offset] = val8;
  296. flexram_wait();
  297. }
  298. offset++;
  299. len--;
  300. }
  301. }
  302. }
  303. /*
  304. void do_flash_cmd(volatile uint8_t *fstat)
  305. {
  306. *fstat = 0x80;
  307. while ((*fstat & 0x80) == 0) ; // wait
  308. }
  309. 00000000 <do_flash_cmd>:
  310. 0: f06f 037f mvn.w r3, #127 ; 0x7f
  311. 4: 7003 strb r3, [r0, #0]
  312. 6: 7803 ldrb r3, [r0, #0]
  313. 8: f013 0f80 tst.w r3, #128 ; 0x80
  314. c: d0fb beq.n 6 <do_flash_cmd+0x6>
  315. e: 4770 bx lr
  316. */
  317. #elif defined(KL2x) /* chip selection */
  318. /* Teensy LC (emulated) */
  319. # define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  320. extern uint32_t __eeprom_workarea_start__;
  321. extern uint32_t __eeprom_workarea_end__;
  322. # define EEPROM_SIZE 128
  323. static uint32_t flashend = 0;
  324. void eeprom_initialize(void) {
  325. const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
  326. do {
  327. if (*p++ == 0xFFFF) {
  328. flashend = (uint32_t)(p - 2);
  329. return;
  330. }
  331. } while (p < (uint16_t *)SYMVAL(__eeprom_workarea_end__));
  332. flashend = (uint32_t)((uint16_t *)SYMVAL(__eeprom_workarea_end__) - 1);
  333. }
  334. uint8_t eeprom_read_byte(const uint8_t *addr) {
  335. uint32_t offset = (uint32_t)addr;
  336. const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
  337. const uint16_t *end = (const uint16_t *)((uint32_t)flashend);
  338. uint16_t val;
  339. uint8_t data = 0xFF;
  340. if (!end) {
  341. eeprom_initialize();
  342. end = (const uint16_t *)((uint32_t)flashend);
  343. }
  344. if (offset < EEPROM_SIZE) {
  345. while (p <= end) {
  346. val = *p++;
  347. if ((val & 255) == offset) data = val >> 8;
  348. }
  349. }
  350. return data;
  351. }
  352. static void flash_write(const uint16_t *code, uint32_t addr, uint32_t data) {
  353. // with great power comes great responsibility....
  354. uint32_t stat;
  355. *(uint32_t *)&(FTFA->FCCOB3) = 0x06000000 | (addr & 0x00FFFFFC);
  356. *(uint32_t *)&(FTFA->FCCOB7) = data;
  357. __disable_irq();
  358. (*((void (*)(volatile uint8_t *))((uint32_t)code | 1)))(&(FTFA->FSTAT));
  359. __enable_irq();
  360. stat = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
  361. if (stat) {
  362. FTFA->FSTAT = stat;
  363. }
  364. MCM->PLACR |= MCM_PLACR_CFCC;
  365. }
  366. void eeprom_write_byte(uint8_t *addr, uint8_t data) {
  367. uint32_t offset = (uint32_t)addr;
  368. const uint16_t *p, *end = (const uint16_t *)((uint32_t)flashend);
  369. uint32_t i, val, flashaddr;
  370. uint16_t do_flash_cmd[] = {0x2380, 0x7003, 0x7803, 0xb25b, 0x2b00, 0xdafb, 0x4770};
  371. uint8_t buf[EEPROM_SIZE];
  372. if (offset >= EEPROM_SIZE) return;
  373. if (!end) {
  374. eeprom_initialize();
  375. end = (const uint16_t *)((uint32_t)flashend);
  376. }
  377. if (++end < (uint16_t *)SYMVAL(__eeprom_workarea_end__)) {
  378. val = (data << 8) | offset;
  379. flashaddr = (uint32_t)end;
  380. flashend = flashaddr;
  381. if ((flashaddr & 2) == 0) {
  382. val |= 0xFFFF0000;
  383. } else {
  384. val <<= 16;
  385. val |= 0x0000FFFF;
  386. }
  387. flash_write(do_flash_cmd, flashaddr, val);
  388. } else {
  389. for (i = 0; i < EEPROM_SIZE; i++) {
  390. buf[i] = 0xFF;
  391. }
  392. val = 0;
  393. for (p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); p < (uint16_t *)SYMVAL(__eeprom_workarea_end__); p++) {
  394. val = *p;
  395. if ((val & 255) < EEPROM_SIZE) {
  396. buf[val & 255] = val >> 8;
  397. }
  398. }
  399. buf[offset] = data;
  400. for (flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); flashaddr < (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_end__); flashaddr += 1024) {
  401. *(uint32_t *)&(FTFA->FCCOB3) = 0x09000000 | flashaddr;
  402. __disable_irq();
  403. (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFA->FSTAT));
  404. __enable_irq();
  405. val = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
  406. ;
  407. if (val) FTFA->FSTAT = val;
  408. MCM->PLACR |= MCM_PLACR_CFCC;
  409. }
  410. flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__);
  411. for (i = 0; i < EEPROM_SIZE; i++) {
  412. if (buf[i] == 0xFF) continue;
  413. if ((flashaddr & 2) == 0) {
  414. val = (buf[i] << 8) | i;
  415. } else {
  416. val = val | (buf[i] << 24) | (i << 16);
  417. flash_write(do_flash_cmd, flashaddr, val);
  418. }
  419. flashaddr += 2;
  420. }
  421. flashend = flashaddr;
  422. if ((flashaddr & 2)) {
  423. val |= 0xFFFF0000;
  424. flash_write(do_flash_cmd, flashaddr, val);
  425. }
  426. }
  427. }
  428. /*
  429. void do_flash_cmd(volatile uint8_t *fstat)
  430. {
  431. *fstat = 0x80;
  432. while ((*fstat & 0x80) == 0) ; // wait
  433. }
  434. 00000000 <do_flash_cmd>:
  435. 0: 2380 movs r3, #128 ; 0x80
  436. 2: 7003 strb r3, [r0, #0]
  437. 4: 7803 ldrb r3, [r0, #0]
  438. 6: b25b sxtb r3, r3
  439. 8: 2b00 cmp r3, #0
  440. a: dafb bge.n 4 <do_flash_cmd+0x4>
  441. c: 4770 bx lr
  442. */
  443. uint16_t eeprom_read_word(const uint16_t *addr) {
  444. const uint8_t *p = (const uint8_t *)addr;
  445. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
  446. }
  447. uint32_t eeprom_read_dword(const uint32_t *addr) {
  448. const uint8_t *p = (const uint8_t *)addr;
  449. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
  450. }
  451. void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
  452. const uint8_t *p = (const uint8_t *)addr;
  453. uint8_t * dest = (uint8_t *)buf;
  454. while (len--) {
  455. *dest++ = eeprom_read_byte(p++);
  456. }
  457. }
  458. int eeprom_is_ready(void) { return 1; }
  459. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  460. uint8_t *p = (uint8_t *)addr;
  461. eeprom_write_byte(p++, value);
  462. eeprom_write_byte(p, value >> 8);
  463. }
  464. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  465. uint8_t *p = (uint8_t *)addr;
  466. eeprom_write_byte(p++, value);
  467. eeprom_write_byte(p++, value >> 8);
  468. eeprom_write_byte(p++, value >> 16);
  469. eeprom_write_byte(p, value >> 24);
  470. }
  471. void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
  472. uint8_t * p = (uint8_t *)addr;
  473. const uint8_t *src = (const uint8_t *)buf;
  474. while (len--) {
  475. eeprom_write_byte(p++, *src++);
  476. }
  477. }
  478. #else
  479. // No EEPROM supported, so emulate it
  480. # ifndef EEPROM_SIZE
  481. # include "eeconfig.h"
  482. # define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
  483. # endif
  484. __attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE];
  485. uint8_t eeprom_read_byte(const uint8_t *addr) {
  486. uint32_t offset = (uint32_t)addr;
  487. return buffer[offset];
  488. }
  489. void eeprom_write_byte(uint8_t *addr, uint8_t value) {
  490. uint32_t offset = (uint32_t)addr;
  491. buffer[offset] = value;
  492. }
  493. uint16_t eeprom_read_word(const uint16_t *addr) {
  494. const uint8_t *p = (const uint8_t *)addr;
  495. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
  496. }
  497. uint32_t eeprom_read_dword(const uint32_t *addr) {
  498. const uint8_t *p = (const uint8_t *)addr;
  499. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
  500. }
  501. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  502. const uint8_t *p = (const uint8_t *)addr;
  503. uint8_t * dest = (uint8_t *)buf;
  504. while (len--) {
  505. *dest++ = eeprom_read_byte(p++);
  506. }
  507. }
  508. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  509. uint8_t *p = (uint8_t *)addr;
  510. eeprom_write_byte(p++, value);
  511. eeprom_write_byte(p, value >> 8);
  512. }
  513. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  514. uint8_t *p = (uint8_t *)addr;
  515. eeprom_write_byte(p++, value);
  516. eeprom_write_byte(p++, value >> 8);
  517. eeprom_write_byte(p++, value >> 16);
  518. eeprom_write_byte(p, value >> 24);
  519. }
  520. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  521. uint8_t * p = (uint8_t *)addr;
  522. const uint8_t *src = (const uint8_t *)buf;
  523. while (len--) {
  524. eeprom_write_byte(p++, *src++);
  525. }
  526. }
  527. #endif /* chip selection */
  528. // The update functions just calls write for now, but could probably be optimized
  529. void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); }
  530. void eeprom_update_word(uint16_t *addr, uint16_t value) {
  531. uint8_t *p = (uint8_t *)addr;
  532. eeprom_write_byte(p++, value);
  533. eeprom_write_byte(p, value >> 8);
  534. }
  535. void eeprom_update_dword(uint32_t *addr, uint32_t value) {
  536. uint8_t *p = (uint8_t *)addr;
  537. eeprom_write_byte(p++, value);
  538. eeprom_write_byte(p++, value >> 8);
  539. eeprom_write_byte(p++, value >> 16);
  540. eeprom_write_byte(p, value >> 24);
  541. }
  542. void eeprom_update_block(const void *buf, void *addr, size_t len) {
  543. uint8_t * p = (uint8_t *)addr;
  544. const uint8_t *src = (const uint8_t *)buf;
  545. while (len--) {
  546. eeprom_write_byte(p++, *src++);
  547. }
  548. }