You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

629 lines
22 KiB

  1. /*
  2. * This software is experimental and a work in progress.
  3. * Under no circumstances should these files be used in relation to any critical system(s).
  4. * Use of these files is at your own risk.
  5. *
  6. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  7. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  8. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  9. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  10. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  11. * DEALINGS IN THE SOFTWARE.
  12. *
  13. * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by
  14. * Artur F.
  15. *
  16. * Modifications for QMK and STM32F303 by Yiancar
  17. * Modifications to add flash wear leveling by Ilya Zhuravlev
  18. * Modifications to increase flash density by Don Kjer
  19. */
  20. #include <stdio.h>
  21. #include <stdbool.h>
  22. #include "util.h"
  23. #include "debug.h"
  24. #include "eeprom_stm32.h"
  25. #include "flash_stm32.h"
  26. /*
  27. * We emulate eeprom by writing a snapshot compacted view of eeprom contents,
  28. * followed by a write log of any change since that snapshot:
  29. *
  30. * === SIMULATED EEPROM CONTENTS ===
  31. *
  32. * Compacted Write Log
  33. * ............[BYTE][BYTE]
  34. * FFFF....FFFF[WRD0][WRD1]
  35. * FFFFFFFFFFFF[WORD][NEXT]
  36. * ....FFFFFFFF[BYTE][WRD0]
  37. *
  38. * PAGE_BASE
  39. * PAGE_LASTWRITE_BASE
  40. * WRITE_LAST
  41. *
  42. * Compacted contents are the 1's complement of the actual EEPROM contents.
  43. * e.g. An 'FFFF' represents a '0000' value.
  44. *
  45. * The size of the 'compacted' area is equal to the size of the 'emulated' eeprom.
  46. * The size of the compacted-area and write log are configurable, and the combined
  47. * size of Compacted + WriteLog is a multiple FEE_PAGE_SIZE, which is MCU dependent.
  48. * Simulated Eeprom contents are located at the end of available flash space.
  49. *
  50. * The following configuration defines can be set:
  51. *
  52. * FEE_PAGE_COUNT # Total number of pages to use for eeprom simulation (Compact + Write log)
  53. * FEE_DENSITY_BYTES # Size of simulated eeprom. (Defaults to half the space allocated by FEE_PAGE_COUNT)
  54. * NOTE: The current implementation does not include page swapping,
  55. * and FEE_DENSITY_BYTES will consume that amount of RAM as a cached view of actual EEPROM contents.
  56. *
  57. * The maximum size of FEE_DENSITY_BYTES is currently 16384. The write log size equals
  58. * FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES.
  59. * The larger the write log, the less frequently the compacted area needs to be rewritten.
  60. *
  61. *
  62. * *** General Algorithm ***
  63. *
  64. * During initialization:
  65. * The contents of the Compacted-flash area are loaded and the 1's complement value
  66. * is cached into memory (e.g. 0xFFFF in Flash represents 0x0000 in cache).
  67. * Write log entries are processed until a 0xFFFF is reached.
  68. * Each log entry updates a byte or word in the cache.
  69. *
  70. * During reads:
  71. * EEPROM contents are given back directly from the cache in memory.
  72. *
  73. * During writes:
  74. * The contents of the cache is updated first.
  75. * If the Compacted-flash area corresponding to the write address is unprogrammed, the 1's complement of the value is written directly into Compacted-flash
  76. * Otherwise:
  77. * If the write log is full, erase both the Compacted-flash area and the Write log, then write cached contents to the Compacted-flash area.
  78. * Otherwise a Write log entry is constructed and appended to the next free position in the Write log.
  79. *
  80. *
  81. * *** Write Log Structure ***
  82. *
  83. * Write log entries allow for optimized byte writes to addresses below 128. Writing 0 or 1 words are also optimized when word-aligned.
  84. *
  85. * === WRITE LOG ENTRY FORMATS ===
  86. *
  87. * Byte-Entry
  88. * 0XXXXXXXYYYYYYYY
  89. *
  90. * Address Value
  91. *
  92. * 0 <= Address < 0x80 (128)
  93. *
  94. * Word-Encoded 0
  95. * 100XXXXXXXXXXXXX
  96. *
  97. * Address >> 1
  98. * Value: 0
  99. *
  100. * 0 <= Address <= 0x3FFE (16382)
  101. *
  102. * Word-Encoded 1
  103. * 101XXXXXXXXXXXXX
  104. *
  105. * Address >> 1
  106. * Value: 1
  107. *
  108. * 0 <= Address <= 0x3FFE (16382)
  109. *
  110. * Reserved
  111. * 110XXXXXXXXXXXXX
  112. *
  113. *
  114. * Word-Next
  115. * 111XXXXXXXXXXXXXYYYYYYYYYYYYYYYY
  116. *
  117. * (Address-128)>>1 ~Value
  118. *
  119. * ( 0 <= Address < 0x0080 (128): Reserved)
  120. * 0x80 <= Address <= 0x3FFE (16382)
  121. *
  122. * Write Log entry ranges:
  123. * 0x0000 ... 0x7FFF - Byte-Entry; address is (Entry & 0x7F00) >> 4; value is (Entry & 0xFF)
  124. * 0x8000 ... 0x9FFF - Word-Encoded 0; address is (Entry & 0x1FFF) << 1; value is 0
  125. * 0xA000 ... 0xBFFF - Word-Encoded 1; address is (Entry & 0x1FFF) << 1; value is 1
  126. * 0xC000 ... 0xDFFF - Reserved
  127. * 0xE000 ... 0xFFBF - Word-Next; address is (Entry & 0x1FFF) << 1 + 0x80; value is ~(Next_Entry)
  128. * 0xFFC0 ... 0xFFFE - Reserved
  129. * 0xFFFF - Unprogrammed
  130. *
  131. */
  132. #include "eeprom_stm32_defs.h"
  133. /* These bits are used for optimizing encoding of bytes, 0 and 1 */
  134. #define FEE_WORD_ENCODING 0x8000
  135. #define FEE_VALUE_NEXT 0x6000
  136. #define FEE_VALUE_RESERVED 0x4000
  137. #define FEE_VALUE_ENCODED 0x2000
  138. #define FEE_BYTE_RANGE 0x80
  139. /* Flash word value after erase */
  140. #define FEE_EMPTY_WORD ((uint16_t)0xFFFF)
  141. #if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT) || !defined(FEE_MCU_FLASH_SIZE) || !defined(FEE_PAGE_BASE_ADDRESS)
  142. # error "not implemented."
  143. #endif
  144. /* In-memory contents of emulated eeprom for faster access */
  145. /* *TODO: Implement page swapping */
  146. static uint16_t WordBuf[FEE_DENSITY_BYTES / 2];
  147. static uint8_t *DataBuf = (uint8_t *)WordBuf;
  148. /* Pointer to the first available slot within the write log */
  149. static uint16_t *empty_slot;
  150. // #define DEBUG_EEPROM_OUTPUT
  151. /*
  152. * Debug print utils
  153. */
  154. #if defined(DEBUG_EEPROM_OUTPUT)
  155. # define debug_eeprom debug_enable
  156. # define eeprom_println(s) println(s)
  157. # define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__);
  158. #else /* NO_DEBUG */
  159. # define debug_eeprom false
  160. # define eeprom_println(s)
  161. # define eeprom_printf(fmt, ...)
  162. #endif /* NO_DEBUG */
  163. void print_eeprom(void) {
  164. #ifndef NO_DEBUG
  165. int empty_rows = 0;
  166. for (uint16_t i = 0; i < FEE_DENSITY_BYTES; i++) {
  167. if (i % 16 == 0) {
  168. if (i >= FEE_DENSITY_BYTES - 16) {
  169. /* Make sure we display the last row */
  170. empty_rows = 0;
  171. }
  172. /* Check if this row is uninitialized */
  173. ++empty_rows;
  174. for (uint16_t j = 0; j < 16; j++) {
  175. if (DataBuf[i + j]) {
  176. empty_rows = 0;
  177. break;
  178. }
  179. }
  180. if (empty_rows > 1) {
  181. /* Repeat empty row */
  182. if (empty_rows == 2) {
  183. /* Only display the first repeat empty row */
  184. println("*");
  185. }
  186. i += 15;
  187. continue;
  188. }
  189. xprintf("%04x", i);
  190. }
  191. if (i % 8 == 0) print(" ");
  192. xprintf(" %02x", DataBuf[i]);
  193. if ((i + 1) % 16 == 0) {
  194. println("");
  195. }
  196. }
  197. #endif
  198. }
  199. uint16_t EEPROM_Init(void) {
  200. /* Load emulated eeprom contents from compacted flash into memory */
  201. uint16_t *src = (uint16_t *)FEE_COMPACTED_BASE_ADDRESS;
  202. uint16_t *dest = (uint16_t *)DataBuf;
  203. for (; src < (uint16_t *)FEE_COMPACTED_LAST_ADDRESS; ++src, ++dest) {
  204. *dest = ~*src;
  205. }
  206. if (debug_eeprom) {
  207. println("EEPROM_Init Compacted Pages:");
  208. print_eeprom();
  209. println("EEPROM_Init Write Log:");
  210. }
  211. /* Replay write log */
  212. uint16_t *log_addr;
  213. for (log_addr = (uint16_t *)FEE_WRITE_LOG_BASE_ADDRESS; log_addr < (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS; ++log_addr) {
  214. uint16_t address = *log_addr;
  215. if (address == FEE_EMPTY_WORD) {
  216. break;
  217. }
  218. /* Check for lowest 128-bytes optimization */
  219. if (!(address & FEE_WORD_ENCODING)) {
  220. uint8_t bvalue = (uint8_t)address;
  221. address >>= 8;
  222. DataBuf[address] = bvalue;
  223. eeprom_printf("DataBuf[0x%02x] = 0x%02x;\n", address, bvalue);
  224. } else {
  225. uint16_t wvalue;
  226. /* Check if value is in next word */
  227. if ((address & FEE_VALUE_NEXT) == FEE_VALUE_NEXT) {
  228. /* Read value from next word */
  229. if (++log_addr >= (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS) {
  230. break;
  231. }
  232. wvalue = ~*log_addr;
  233. if (!wvalue) {
  234. eeprom_printf("Incomplete write at log_addr: 0x%04x;\n", (uint32_t)log_addr);
  235. /* Possibly incomplete write. Ignore and continue */
  236. continue;
  237. }
  238. address &= 0x1FFF;
  239. address <<= 1;
  240. /* Writes to addresses less than 128 are byte log entries */
  241. address += FEE_BYTE_RANGE;
  242. } else {
  243. /* Reserved for future use */
  244. if (address & FEE_VALUE_RESERVED) {
  245. eeprom_printf("Reserved encoded value at log_addr: 0x%04x;\n", (uint32_t)log_addr);
  246. continue;
  247. }
  248. /* Optimization for 0 or 1 values. */
  249. wvalue = (address & FEE_VALUE_ENCODED) >> 13;
  250. address &= 0x1FFF;
  251. address <<= 1;
  252. }
  253. if (address < FEE_DENSITY_BYTES) {
  254. eeprom_printf("DataBuf[0x%04x] = 0x%04x;\n", address, wvalue);
  255. *(uint16_t *)(&DataBuf[address]) = wvalue;
  256. } else {
  257. eeprom_printf("DataBuf[0x%04x] cannot be set to 0x%04x [BAD ADDRESS]\n", address, wvalue);
  258. }
  259. }
  260. }
  261. empty_slot = log_addr;
  262. if (debug_eeprom) {
  263. println("EEPROM_Init Final DataBuf:");
  264. print_eeprom();
  265. }
  266. return FEE_DENSITY_BYTES;
  267. }
  268. /* Clear flash contents (doesn't touch in-memory DataBuf) */
  269. static void eeprom_clear(void) {
  270. FLASH_Unlock();
  271. for (uint16_t page_num = 0; page_num < FEE_PAGE_COUNT; ++page_num) {
  272. eeprom_printf("FLASH_ErasePage(0x%04x)\n", (uint32_t)(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)));
  273. FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE));
  274. }
  275. FLASH_Lock();
  276. empty_slot = (uint16_t *)FEE_WRITE_LOG_BASE_ADDRESS;
  277. eeprom_printf("eeprom_clear empty_slot: 0x%08x\n", (uint32_t)empty_slot);
  278. }
  279. /* Erase emulated eeprom */
  280. void EEPROM_Erase(void) {
  281. eeprom_println("EEPROM_Erase");
  282. /* Erase compacted pages and write log */
  283. eeprom_clear();
  284. /* re-initialize to reset DataBuf */
  285. EEPROM_Init();
  286. }
  287. /* Compact write log */
  288. static uint8_t eeprom_compact(void) {
  289. /* Erase compacted pages and write log */
  290. eeprom_clear();
  291. FLASH_Unlock();
  292. FLASH_Status final_status = FLASH_COMPLETE;
  293. /* Write emulated eeprom contents from memory to compacted flash */
  294. uint16_t *src = (uint16_t *)DataBuf;
  295. uintptr_t dest = FEE_COMPACTED_BASE_ADDRESS;
  296. uint16_t value;
  297. for (; dest < FEE_COMPACTED_LAST_ADDRESS; ++src, dest += 2) {
  298. value = *src;
  299. if (value) {
  300. eeprom_printf("FLASH_ProgramHalfWord(0x%04x, 0x%04x)\n", (uint32_t)dest, ~value);
  301. FLASH_Status status = FLASH_ProgramHalfWord(dest, ~value);
  302. if (status != FLASH_COMPLETE) final_status = status;
  303. }
  304. }
  305. FLASH_Lock();
  306. if (debug_eeprom) {
  307. println("eeprom_compacted:");
  308. print_eeprom();
  309. }
  310. return final_status;
  311. }
  312. static uint8_t eeprom_write_direct_entry(uint16_t Address) {
  313. /* Check if we can just write this directly to the compacted flash area */
  314. uintptr_t directAddress = FEE_COMPACTED_BASE_ADDRESS + (Address & 0xFFFE);
  315. if (*(uint16_t *)directAddress == FEE_EMPTY_WORD) {
  316. /* Write the value directly to the compacted area without a log entry */
  317. uint16_t value = ~*(uint16_t *)(&DataBuf[Address & 0xFFFE]);
  318. /* Early exit if a write isn't needed */
  319. if (value == FEE_EMPTY_WORD) return FLASH_COMPLETE;
  320. FLASH_Unlock();
  321. eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x) [DIRECT]\n", (uint32_t)directAddress, value);
  322. FLASH_Status status = FLASH_ProgramHalfWord(directAddress, value);
  323. FLASH_Lock();
  324. return status;
  325. }
  326. return 0;
  327. }
  328. static uint8_t eeprom_write_log_word_entry(uint16_t Address) {
  329. FLASH_Status final_status = FLASH_COMPLETE;
  330. uint16_t value = *(uint16_t *)(&DataBuf[Address]);
  331. eeprom_printf("eeprom_write_log_word_entry(0x%04x): 0x%04x\n", Address, value);
  332. /* MSB signifies the lowest 128-byte optimization is not in effect */
  333. uint16_t encoding = FEE_WORD_ENCODING;
  334. uint8_t entry_size;
  335. if (value <= 1) {
  336. encoding |= value << 13;
  337. entry_size = 2;
  338. } else {
  339. encoding |= FEE_VALUE_NEXT;
  340. entry_size = 4;
  341. /* Writes to addresses less than 128 are byte log entries */
  342. Address -= FEE_BYTE_RANGE;
  343. }
  344. /* if we can't find an empty spot, we must compact emulated eeprom */
  345. if (empty_slot > (uint16_t *)(FEE_WRITE_LOG_LAST_ADDRESS - entry_size)) {
  346. /* compact the write log into the compacted flash area */
  347. return eeprom_compact();
  348. }
  349. /* Word log writes should be word-aligned. Take back a bit */
  350. Address >>= 1;
  351. Address |= encoding;
  352. /* ok we found a place let's write our data */
  353. FLASH_Unlock();
  354. /* address */
  355. eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, Address);
  356. final_status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, Address);
  357. /* value */
  358. if (encoding == (FEE_WORD_ENCODING | FEE_VALUE_NEXT)) {
  359. eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, ~value);
  360. FLASH_Status status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, ~value);
  361. if (status != FLASH_COMPLETE) final_status = status;
  362. }
  363. FLASH_Lock();
  364. return final_status;
  365. }
  366. static uint8_t eeprom_write_log_byte_entry(uint16_t Address) {
  367. eeprom_printf("eeprom_write_log_byte_entry(0x%04x): 0x%02x\n", Address, DataBuf[Address]);
  368. /* if couldn't find an empty spot, we must compact emulated eeprom */
  369. if (empty_slot >= (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS) {
  370. /* compact the write log into the compacted flash area */
  371. return eeprom_compact();
  372. }
  373. /* ok we found a place let's write our data */
  374. FLASH_Unlock();
  375. /* Pack address and value into the same word */
  376. uint16_t value = (Address << 8) | DataBuf[Address];
  377. /* write to flash */
  378. eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, value);
  379. FLASH_Status status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, value);
  380. FLASH_Lock();
  381. return status;
  382. }
  383. uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) {
  384. /* if the address is out-of-bounds, do nothing */
  385. if (Address >= FEE_DENSITY_BYTES) {
  386. eeprom_printf("EEPROM_WriteDataByte(0x%04x, 0x%02x) [BAD ADDRESS]\n", Address, DataByte);
  387. return FLASH_BAD_ADDRESS;
  388. }
  389. /* if the value is the same, don't bother writing it */
  390. if (DataBuf[Address] == DataByte) {
  391. eeprom_printf("EEPROM_WriteDataByte(0x%04x, 0x%02x) [SKIP SAME]\n", Address, DataByte);
  392. return 0;
  393. }
  394. /* keep DataBuf cache in sync */
  395. DataBuf[Address] = DataByte;
  396. eeprom_printf("EEPROM_WriteDataByte DataBuf[0x%04x] = 0x%02x\n", Address, DataBuf[Address]);
  397. /* perform the write into flash memory */
  398. /* First, attempt to write directly into the compacted flash area */
  399. FLASH_Status status = eeprom_write_direct_entry(Address);
  400. if (!status) {
  401. /* Otherwise append to the write log */
  402. if (Address < FEE_BYTE_RANGE) {
  403. status = eeprom_write_log_byte_entry(Address);
  404. } else {
  405. status = eeprom_write_log_word_entry(Address & 0xFFFE);
  406. }
  407. }
  408. if (status != 0 && status != FLASH_COMPLETE) {
  409. eeprom_printf("EEPROM_WriteDataByte [STATUS == %d]\n", status);
  410. }
  411. return status;
  412. }
  413. uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord) {
  414. /* if the address is out-of-bounds, do nothing */
  415. if (Address >= FEE_DENSITY_BYTES) {
  416. eeprom_printf("EEPROM_WriteDataWord(0x%04x, 0x%04x) [BAD ADDRESS]\n", Address, DataWord);
  417. return FLASH_BAD_ADDRESS;
  418. }
  419. /* Check for word alignment */
  420. FLASH_Status final_status = FLASH_COMPLETE;
  421. if (Address % 2) {
  422. final_status = EEPROM_WriteDataByte(Address, DataWord);
  423. FLASH_Status status = EEPROM_WriteDataByte(Address + 1, DataWord >> 8);
  424. if (status != FLASH_COMPLETE) final_status = status;
  425. if (final_status != 0 && final_status != FLASH_COMPLETE) {
  426. eeprom_printf("EEPROM_WriteDataWord [STATUS == %d]\n", final_status);
  427. }
  428. return final_status;
  429. }
  430. /* if the value is the same, don't bother writing it */
  431. uint16_t oldValue = *(uint16_t *)(&DataBuf[Address]);
  432. if (oldValue == DataWord) {
  433. eeprom_printf("EEPROM_WriteDataWord(0x%04x, 0x%04x) [SKIP SAME]\n", Address, DataWord);
  434. return 0;
  435. }
  436. /* keep DataBuf cache in sync */
  437. *(uint16_t *)(&DataBuf[Address]) = DataWord;
  438. eeprom_printf("EEPROM_WriteDataWord DataBuf[0x%04x] = 0x%04x\n", Address, *(uint16_t *)(&DataBuf[Address]));
  439. /* perform the write into flash memory */
  440. /* First, attempt to write directly into the compacted flash area */
  441. final_status = eeprom_write_direct_entry(Address);
  442. if (!final_status) {
  443. /* Otherwise append to the write log */
  444. /* Check if we need to fall back to byte write */
  445. if (Address < FEE_BYTE_RANGE) {
  446. final_status = FLASH_COMPLETE;
  447. /* Only write a byte if it has changed */
  448. if ((uint8_t)oldValue != (uint8_t)DataWord) {
  449. final_status = eeprom_write_log_byte_entry(Address);
  450. }
  451. FLASH_Status status = FLASH_COMPLETE;
  452. /* Only write a byte if it has changed */
  453. if ((oldValue >> 8) != (DataWord >> 8)) {
  454. status = eeprom_write_log_byte_entry(Address + 1);
  455. }
  456. if (status != FLASH_COMPLETE) final_status = status;
  457. } else {
  458. final_status = eeprom_write_log_word_entry(Address);
  459. }
  460. }
  461. if (final_status != 0 && final_status != FLASH_COMPLETE) {
  462. eeprom_printf("EEPROM_WriteDataWord [STATUS == %d]\n", final_status);
  463. }
  464. return final_status;
  465. }
  466. uint8_t EEPROM_ReadDataByte(uint16_t Address) {
  467. uint8_t DataByte = 0xFF;
  468. if (Address < FEE_DENSITY_BYTES) {
  469. DataByte = DataBuf[Address];
  470. }
  471. eeprom_printf("EEPROM_ReadDataByte(0x%04x): 0x%02x\n", Address, DataByte);
  472. return DataByte;
  473. }
  474. uint16_t EEPROM_ReadDataWord(uint16_t Address) {
  475. uint16_t DataWord = 0xFFFF;
  476. if (Address < FEE_DENSITY_BYTES - 1) {
  477. /* Check word alignment */
  478. if (Address % 2) {
  479. DataWord = DataBuf[Address] | (DataBuf[Address + 1] << 8);
  480. } else {
  481. DataWord = *(uint16_t *)(&DataBuf[Address]);
  482. }
  483. }
  484. eeprom_printf("EEPROM_ReadDataWord(0x%04x): 0x%04x\n", Address, DataWord);
  485. return DataWord;
  486. }
  487. /*****************************************************************************
  488. * Bind to eeprom_driver.c
  489. *******************************************************************************/
  490. void eeprom_driver_init(void) {
  491. EEPROM_Init();
  492. }
  493. void eeprom_driver_erase(void) {
  494. EEPROM_Erase();
  495. }
  496. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  497. const uint8_t *src = (const uint8_t *)addr;
  498. uint8_t * dest = (uint8_t *)buf;
  499. /* Check word alignment */
  500. if (len && (uintptr_t)src % 2) {
  501. /* Read the unaligned first byte */
  502. *dest++ = EEPROM_ReadDataByte((const uintptr_t)src++);
  503. --len;
  504. }
  505. uint16_t value;
  506. bool aligned = ((uintptr_t)dest % 2 == 0);
  507. while (len > 1) {
  508. value = EEPROM_ReadDataWord((const uintptr_t)((uint16_t *)src));
  509. if (aligned) {
  510. *(uint16_t *)dest = value;
  511. dest += 2;
  512. } else {
  513. *dest++ = value;
  514. *dest++ = value >> 8;
  515. }
  516. src += 2;
  517. len -= 2;
  518. }
  519. if (len) {
  520. *dest = EEPROM_ReadDataByte((const uintptr_t)src);
  521. }
  522. }
  523. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  524. uint8_t * dest = (uint8_t *)addr;
  525. const uint8_t *src = (const uint8_t *)buf;
  526. /* Check word alignment */
  527. if (len && (uintptr_t)dest % 2) {
  528. /* Write the unaligned first byte */
  529. EEPROM_WriteDataByte((uintptr_t)dest++, *src++);
  530. --len;
  531. }
  532. uint16_t value;
  533. bool aligned = ((uintptr_t)src % 2 == 0);
  534. while (len > 1) {
  535. if (aligned) {
  536. value = *(uint16_t *)src;
  537. } else {
  538. value = *(uint8_t *)src | (*(uint8_t *)(src + 1) << 8);
  539. }
  540. EEPROM_WriteDataWord((uintptr_t)((uint16_t *)dest), value);
  541. dest += 2;
  542. src += 2;
  543. len -= 2;
  544. }
  545. if (len) {
  546. EEPROM_WriteDataByte((uintptr_t)dest, *src);
  547. }
  548. }