eeprom.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Simple PWM controller
  3. * EEPROM
  4. *
  5. * Copyright (c) 2020 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "compat.h"
  22. #include "eeprom.h"
  23. #include "arithmetic.h"
  24. #include "debug.h"
  25. #include "ring.h"
  26. #include "standby.h"
  27. #include "util.h"
  28. #include "watchdog.h"
  29. #define EEPROM_STORE_DELAY_MS 1500u
  30. #ifndef ee_addr_t
  31. # ifdef EEARH
  32. typedef uint16_t ee_addr_t;
  33. # else
  34. typedef uint8_t ee_addr_t;
  35. # endif
  36. # define ee_addr_t ee_addr_t
  37. #endif
  38. #define EE_RING_SIZE ((E2END + 1u) / sizeof(struct eeprom_data))
  39. #define EE_RING_MAX_INDEX (EE_RING_SIZE - 1u)
  40. static struct eeprom_data EEMEM eep_addrspace[EE_RING_SIZE] = {
  41. {
  42. .flags = 0u,
  43. .setpoints = { },
  44. .pwmcorr_mul = { },
  45. .pwmcorr_div = { },
  46. .serial = 0u,
  47. },
  48. };
  49. static struct {
  50. struct eeprom_data shadow_copy; /* Copy of EEPROM. */
  51. struct eeprom_data work_copy; /* Copy that can be modified. */
  52. uint8_t ee_index; /* Ring index. */
  53. uint8_t ee_write_offset; /* Byte offset of current write. */
  54. uint16_t store_timer_ms; /* Store delay timer. */
  55. bool store_request; /* EEPROM write has been requested. */
  56. bool store_running; /* EEPROM write is currently running. */
  57. } eep;
  58. static void eeprom_update_standby_suppress(void)
  59. {
  60. if (USE_EEPROM && USE_DEEP_SLEEP) {
  61. set_standby_suppress(STANDBY_SRC_EEPROM,
  62. eep.store_running || eep.store_request);
  63. }
  64. }
  65. /* Convert a pointer to the EEPROM address space into an integer. */
  66. static ee_addr_t ptr_to_eeaddr(const void *eemem_ptr)
  67. {
  68. return (ee_addr_t)(uintptr_t)eemem_ptr;
  69. }
  70. /* Read one byte from EEPROM. */
  71. static uint8_t ee_read_byte(ee_addr_t addr)
  72. {
  73. uint8_t data = 0u;
  74. if (USE_EEPROM) {
  75. eeprom_busy_wait();
  76. EEAR = addr;
  77. EECR |= (1u << EERE);
  78. data = EEDR;
  79. }
  80. return data;
  81. }
  82. /* Read a block of bytes from EEPROM. */
  83. static void ee_read_block(void *dest, ee_addr_t addr, uint8_t count)
  84. {
  85. uint8_t *d = (uint8_t *)dest;
  86. if (USE_EEPROM) {
  87. for ( ; count; count--, d++, addr++)
  88. *d = ee_read_byte(addr);
  89. }
  90. }
  91. /* EEPROM interrupt service routine. */
  92. #if USE_EEPROM
  93. ISR(EE_READY_vect)
  94. {
  95. ee_addr_t address;
  96. uint8_t data;
  97. uint8_t offset;
  98. uint8_t index = 0u;
  99. index = eep.ee_index;
  100. offset = eep.ee_write_offset;
  101. address = ptr_to_eeaddr(&eep_addrspace[index]) + offset;
  102. data = *((uint8_t *)&eep.work_copy + offset);
  103. EEAR = address;
  104. /* Read the byte. */
  105. EECR |= (1u << EERE);
  106. if (EEDR == data) {
  107. /* The data in EEPROM matches the data to be written.
  108. * No write is needed.
  109. * This interrupt will trigger again immediately. */
  110. } else {
  111. /* Start programming of the byte.
  112. * This interrupt will trigger again when programming finished. */
  113. EEDR = data;
  114. EECR |= (1u << EEMPE);
  115. EECR |= (1u << EEPE);
  116. }
  117. eep.ee_write_offset = ++offset;
  118. if (offset >= sizeof(struct eeprom_data)) {
  119. /* Done writing. Disable the interrupt. */
  120. EECR &= (uint8_t)~(1u << EERIE);
  121. /* Finalize write. Update shadow copy. */
  122. eep.shadow_copy = eep.work_copy;
  123. eep.store_running = false;
  124. eeprom_update_standby_suppress();
  125. dprintf("EEPROM write done.\r\n");
  126. }
  127. }
  128. #endif /* USE_EEPROM */
  129. /* Start storing data to EEPROM.
  130. * Interrupts shall be disabled before calling this function. */
  131. static void eeprom_trigger_store(void)
  132. {
  133. if (USE_EEPROM) {
  134. if (memcmp(&eep.work_copy,
  135. &eep.shadow_copy,
  136. sizeof(eep.work_copy)) == 0) {
  137. /* The data did not change. */
  138. dprintf("EEPROM write not necessary.\r\n");
  139. } else {
  140. dprintf("EEPROM write start.\r\n");
  141. eep.store_running = true;
  142. /* Avoid standby during eeprom write. */
  143. eeprom_update_standby_suppress();
  144. /* Increment the serial number. This might wrap. */
  145. eep.work_copy.serial++;
  146. /* Increment the store index. */
  147. eep.ee_index = ring_next(eep.ee_index, EE_RING_MAX_INDEX);
  148. /* Reset the store byte offset. */
  149. eep.ee_write_offset = 0u;
  150. /* Enable the eeprom-ready interrupt.
  151. * It will fire, if the EEPROM is ready. */
  152. EECR |= (1u << EERIE);
  153. }
  154. eep.store_request = false;
  155. }
  156. }
  157. /* Get the active dataset. */
  158. struct eeprom_data * eeprom_get_data(void)
  159. {
  160. if (USE_EEPROM)
  161. return &eep.work_copy;
  162. return NULL;
  163. }
  164. /* Schedule storing data to EEPROM. */
  165. void eeprom_store_data(void)
  166. {
  167. uint8_t irq_state;
  168. if (USE_EEPROM) {
  169. irq_state = irq_disable_save();
  170. eep.store_request = true;
  171. eep.store_timer_ms = EEPROM_STORE_DELAY_MS;
  172. irq_restore(irq_state);
  173. }
  174. }
  175. /* Handle wake up from deep sleep.
  176. * Called with interrupts disabled. */
  177. void eeprom_handle_deep_sleep_wakeup(void)
  178. {
  179. if (USE_EEPROM && USE_DEEP_SLEEP)
  180. eeprom_update_standby_suppress();
  181. }
  182. /* Watchdog timer interrupt service routine
  183. * for EEPROM handling.
  184. * Called with interrupts disabled. */
  185. void eeprom_handle_watchdog_interrupt(void)
  186. {
  187. if (USE_EEPROM) {
  188. if (eep.store_request && !eep.store_running) {
  189. eep.store_timer_ms = sub_sat_u16(eep.store_timer_ms,
  190. watchdog_interval_ms());
  191. if (eep.store_timer_ms == 0u)
  192. eeprom_trigger_store();
  193. }
  194. }
  195. }
  196. /* Initialize EEPROM. */
  197. void eeprom_init(void)
  198. {
  199. uint8_t next_index, serial, next_serial;
  200. uint8_t found_index = 0u;
  201. if (!USE_EEPROM)
  202. return;
  203. build_assert(EE_RING_MAX_INDEX <= 0xFFu - 1u);
  204. /* Find the latest settings in the eeprom.
  205. * The latest setting is the one with the largest
  206. * index. However, wrap around must be considered. */
  207. serial = ee_read_byte(ptr_to_eeaddr(&eep_addrspace[0].serial));
  208. next_index = 0u;
  209. do {
  210. found_index = next_index;
  211. next_index = ring_next(next_index, EE_RING_MAX_INDEX);
  212. next_serial = ee_read_byte(ptr_to_eeaddr(&eep_addrspace[next_index].serial));
  213. if (next_serial != ((serial + 1u) & 0xFFu))
  214. break;
  215. serial = next_serial;
  216. } while (next_index != 0u);
  217. eep.ee_index = found_index;
  218. /* Read settings from EEPROM. */
  219. ee_read_block(&eep.work_copy,
  220. ptr_to_eeaddr(&eep_addrspace[found_index]),
  221. sizeof(eep.work_copy));
  222. eep.shadow_copy = eep.work_copy;
  223. eeprom_update_standby_suppress();
  224. }