eeprom.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. .serial = 0u,
  45. },
  46. };
  47. static struct {
  48. struct eeprom_data shadow_copy; /* Copy of EEPROM. */
  49. struct eeprom_data work_copy; /* Copy that can be modified. */
  50. uint8_t ee_index; /* Ring index. */
  51. uint8_t ee_write_offset; /* Byte offset of current write. */
  52. uint16_t store_timer_ms; /* Store delay timer. */
  53. bool store_request; /* EEPROM write has been requested. */
  54. bool store_running; /* EEPROM write is currently running. */
  55. } eep;
  56. static void eeprom_update_standby_suppress(void)
  57. {
  58. if (USE_EEPROM) {
  59. set_standby_suppress(STANDBY_SRC_EEPROM,
  60. eep.store_running || eep.store_request);
  61. }
  62. }
  63. /* Convert a pointer to the EEPROM address space into an integer. */
  64. static ee_addr_t ptr_to_eeaddr(const void *eemem_ptr)
  65. {
  66. return (ee_addr_t)(uintptr_t)eemem_ptr;
  67. }
  68. /* Read one byte from EEPROM. */
  69. static uint8_t ee_read_byte(ee_addr_t addr)
  70. {
  71. uint8_t data = 0u;
  72. if (USE_EEPROM) {
  73. eeprom_busy_wait();
  74. EEAR = addr;
  75. EECR |= (1u << EERE);
  76. data = EEDR;
  77. }
  78. return data;
  79. }
  80. /* Read a block of bytes from EEPROM. */
  81. static void ee_read_block(void *dest, ee_addr_t addr, uint8_t count)
  82. {
  83. uint8_t *d = (uint8_t *)dest;
  84. if (USE_EEPROM) {
  85. for ( ; count; count--, d++, addr++)
  86. *d = ee_read_byte(addr);
  87. }
  88. }
  89. /* EEPROM interrupt service routine. */
  90. #if USE_EEPROM
  91. ISR(EE_READY_vect)
  92. {
  93. ee_addr_t address;
  94. uint8_t data;
  95. uint8_t offset;
  96. uint8_t index = 0u;
  97. index = eep.ee_index;
  98. offset = eep.ee_write_offset;
  99. address = ptr_to_eeaddr(&eep_addrspace[index]) + offset;
  100. data = *((uint8_t *)&eep.work_copy + offset);
  101. EEAR = address;
  102. /* Read the byte. */
  103. EECR |= (1u << EERE);
  104. if (EEDR == data) {
  105. /* The data in EEPROM matches the data to be written.
  106. * No write is needed.
  107. * This interrupt will trigger again immediately. */
  108. } else {
  109. /* Start programming of the byte.
  110. * This interrupt will trigger again when programming finished. */
  111. EEDR = data;
  112. EECR |= (1u << EEMPE);
  113. EECR |= (1u << EEPE);
  114. }
  115. eep.ee_write_offset = ++offset;
  116. if (offset >= sizeof(struct eeprom_data)) {
  117. /* Done writing. Disable the interrupt. */
  118. EECR &= (uint8_t)~(1u << EERIE);
  119. /* Finalize write. Update shadow copy. */
  120. eep.shadow_copy = eep.work_copy;
  121. eep.store_running = false;
  122. eeprom_update_standby_suppress();
  123. dprintf("EEPROM write done.\r\n");
  124. }
  125. }
  126. #endif /* USE_EEPROM */
  127. /* Start storing data to EEPROM.
  128. * Interrupts shall be disabled before calling this function. */
  129. static void eeprom_trigger_store(void)
  130. {
  131. if (USE_EEPROM) {
  132. if (memcmp(&eep.work_copy,
  133. &eep.shadow_copy,
  134. sizeof(eep.work_copy)) == 0) {
  135. /* The data did not change. */
  136. dprintf("EEPROM write not necessary.\r\n");
  137. } else {
  138. dprintf("EEPROM write start.\r\n");
  139. eep.store_running = true;
  140. /* Avoid standby during eeprom write. */
  141. eeprom_update_standby_suppress();
  142. /* Increment the serial number. This might wrap. */
  143. eep.work_copy.serial++;
  144. /* Increment the store index. */
  145. eep.ee_index = ring_next(eep.ee_index, EE_RING_MAX_INDEX);
  146. /* Reset the store byte offset. */
  147. eep.ee_write_offset = 0u;
  148. /* Enable the eeprom-ready interrupt.
  149. * It will fire, if the EEPROM is ready. */
  150. EECR |= (1u << EERIE);
  151. }
  152. eep.store_request = false;
  153. }
  154. }
  155. /* Get the active dataset. */
  156. struct eeprom_data * eeprom_get_data(void)
  157. {
  158. if (USE_EEPROM)
  159. return &eep.work_copy;
  160. return NULL;
  161. }
  162. /* Schedule storing data to EEPROM. */
  163. void eeprom_store_data(void)
  164. {
  165. uint8_t irq_state;
  166. if (USE_EEPROM) {
  167. irq_state = irq_disable_save();
  168. eep.store_request = true;
  169. eep.store_timer_ms = EEPROM_STORE_DELAY_MS;
  170. irq_restore(irq_state);
  171. }
  172. }
  173. /* Handle wake up from deep sleep.
  174. * Called with interrupts disabled. */
  175. void eeprom_handle_deep_sleep_wakeup(void)
  176. {
  177. eeprom_update_standby_suppress();
  178. }
  179. /* Watchdog timer interrupt service routine
  180. * for EEPROM handling.
  181. * Called with interrupts disabled. */
  182. void eeprom_handle_watchdog_interrupt(void)
  183. {
  184. if (USE_EEPROM) {
  185. if (eep.store_request && !eep.store_running) {
  186. eep.store_timer_ms = sub_sat_u16(eep.store_timer_ms,
  187. watchdog_interval_ms());
  188. if (eep.store_timer_ms == 0u)
  189. eeprom_trigger_store();
  190. }
  191. }
  192. }
  193. /* Initialize EEPROM. */
  194. void eeprom_init(void)
  195. {
  196. uint8_t next_index, serial, next_serial;
  197. uint8_t found_index = 0u;
  198. if (!USE_EEPROM)
  199. return;
  200. build_assert(EE_RING_MAX_INDEX <= 0xFFu - 1u);
  201. /* Find the latest settings in the eeprom.
  202. * The latest setting is the one with the largest
  203. * index. However, wrap around must be considered. */
  204. serial = ee_read_byte(ptr_to_eeaddr(&eep_addrspace[0].serial));
  205. next_index = 0u;
  206. do {
  207. found_index = next_index;
  208. next_index = ring_next(next_index, EE_RING_MAX_INDEX);
  209. next_serial = ee_read_byte(ptr_to_eeaddr(&eep_addrspace[next_index].serial));
  210. if (next_serial != ((serial + 1u) & 0xFFu))
  211. break;
  212. serial = next_serial;
  213. } while (next_index != 0u);
  214. eep.ee_index = found_index;
  215. /* Read settings from EEPROM. */
  216. ee_read_block(&eep.work_copy,
  217. ptr_to_eeaddr(&eep_addrspace[found_index]),
  218. sizeof(eep.work_copy));
  219. eep.shadow_copy = eep.work_copy;
  220. eeprom_update_standby_suppress();
  221. }