eeprom_stm32.c 28 KB

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