logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

eeprom_legacy_emulated_flash.c (22371B)


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