settings.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Xytronic LF-1600
  3. * Settings handling
  4. *
  5. * Copyright (c) 2015-2017 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 "settings.h"
  22. #include "util.h"
  23. #include "timer.h"
  24. #include "controller_temp.h"
  25. #include "presets.h"
  26. #include "ring.h"
  27. #include <string.h>
  28. #include <avr/eeprom.h>
  29. #ifndef ee_addr_t
  30. typedef uint16_t ee_addr_t;
  31. #define ee_addr_t ee_addr_t
  32. #endif
  33. struct settings_context {
  34. struct timer store_timer;
  35. bool store_request;
  36. struct settings cache;
  37. #if CONF_EERING
  38. uint8_t ee_index;
  39. #endif
  40. uint8_t ee_write_offset;
  41. };
  42. static struct settings_context settings;
  43. #if CONF_EERING
  44. # define NR_EE_SETTINGS ((E2END + 1) / sizeof(struct settings))
  45. #else
  46. # define NR_EE_SETTINGS 1
  47. #endif
  48. /* The permanent EEPROM storage. */
  49. static struct settings EEMEM ee_settings[NR_EE_SETTINGS] = {
  50. {
  51. .temp_k = {
  52. /* TEMPBOOST_NORMAL */
  53. {
  54. .kp = FLOAT_TO_FIXPT(CONTRTEMP_PID_KP_NORMAL),
  55. .ki = FLOAT_TO_FIXPT(CONTRTEMP_PID_KI_NORMAL),
  56. .kd = FLOAT_TO_FIXPT(CONTRTEMP_PID_KD_NORMAL),
  57. .d_decay_div = FLOAT_TO_FIXPT(CONTRTEMP_PID_D_DECAY_NORMAL),
  58. },
  59. #if CONF_BOOST
  60. /* TEMPBOOST_BOOST1 */
  61. {
  62. .kp = FLOAT_TO_FIXPT(CONTRTEMP_PID_KP_BOOST1),
  63. .ki = FLOAT_TO_FIXPT(CONTRTEMP_PID_KI_BOOST1),
  64. .kd = FLOAT_TO_FIXPT(CONTRTEMP_PID_KD_BOOST1),
  65. .d_decay_div = FLOAT_TO_FIXPT(CONTRTEMP_PID_D_DECAY_BOOST1),
  66. },
  67. /* TEMPBOOST_BOOST2 */
  68. {
  69. .kp = FLOAT_TO_FIXPT(CONTRTEMP_PID_KP_BOOST2),
  70. .ki = FLOAT_TO_FIXPT(CONTRTEMP_PID_KI_BOOST2),
  71. .kd = FLOAT_TO_FIXPT(CONTRTEMP_PID_KD_BOOST2),
  72. .d_decay_div = FLOAT_TO_FIXPT(CONTRTEMP_PID_D_DECAY_BOOST2),
  73. },
  74. #endif
  75. },
  76. #if CONF_IDLE
  77. .temp_idle_setpoint = FLOAT_TO_FIXPT(CONTRTEMP_DEF_IDLE_SETPOINT),
  78. #endif
  79. .temp_setpoint = {
  80. FLOAT_TO_FIXPT(PRESET_DEFAULT0),
  81. #if CONF_PRESETS
  82. FLOAT_TO_FIXPT(PRESET_DEFAULT1),
  83. FLOAT_TO_FIXPT(PRESET_DEFAULT2),
  84. FLOAT_TO_FIXPT(PRESET_DEFAULT3),
  85. FLOAT_TO_FIXPT(PRESET_DEFAULT4),
  86. FLOAT_TO_FIXPT(PRESET_DEFAULT5),
  87. #endif
  88. },
  89. .temp_setpoint_active = PRESET_DEFAULT_INDEX,
  90. .temp_adj = INT_TO_FIXPT(0),
  91. .reserved = { },
  92. .serial = 0,
  93. },
  94. };
  95. static uint8_t ee_read_byte(ee_addr_t addr)
  96. {
  97. uint8_t data;
  98. eeprom_busy_wait();
  99. EEAR = addr;
  100. EECR |= (1 << EERE);
  101. data = EEDR;
  102. return data;
  103. }
  104. static void ee_read_block(void *dest, ee_addr_t addr, uint8_t count)
  105. {
  106. uint8_t *d = (uint8_t *)dest;
  107. for ( ; count; count--, d++, addr++)
  108. *d = ee_read_byte(addr);
  109. }
  110. ISR(EE_READY_vect)
  111. {
  112. ee_addr_t address;
  113. uint8_t data;
  114. uint8_t offset;
  115. uint8_t index = 0u;
  116. #if CONF_EERING
  117. index = settings.ee_index;
  118. #endif
  119. offset = settings.ee_write_offset;
  120. address = (ee_addr_t)((uint8_t *)&ee_settings[index] + offset);
  121. data = *((uint8_t *)&settings.cache + offset);
  122. EEAR = address;
  123. /* Read the byte. */
  124. EECR |= (1 << EERE);
  125. if (EEDR == data) {
  126. /* The data in EEPROM matches the data to be written.
  127. * No write is needed.
  128. * This interrupt will trigger again immediately.
  129. */
  130. } else {
  131. /* Start programming of the byte.
  132. * This interrupt will trigger again when programming finished.
  133. */
  134. EEDR = data;
  135. EECR |= (1 << EEMPE);
  136. EECR |= (1 << EEPE);
  137. }
  138. settings.ee_write_offset = ++offset;
  139. if (offset >= sizeof(struct settings)) {
  140. /* Done writing. Disable the interrupt. */
  141. EECR &= (uint8_t)~(1 << EERIE);
  142. }
  143. }
  144. struct settings * get_settings(void)
  145. {
  146. return &settings.cache;
  147. }
  148. void store_settings(void)
  149. {
  150. settings.store_request = true;
  151. timer_arm(&settings.store_timer, 1000);
  152. }
  153. void settings_work(void)
  154. {
  155. if (!settings.store_request)
  156. return;
  157. if (!timer_expired(&settings.store_timer))
  158. return;
  159. irq_disable();
  160. settings.store_request = false;
  161. #if CONF_EERING
  162. /* Increment the serial number. This might wrap. */
  163. settings.cache.serial++;
  164. /* Increment the store index. */
  165. settings.ee_index = ring_next(settings.ee_index,
  166. ARRAY_SIZE(ee_settings) - 1u);
  167. #endif
  168. /* Reset the store byte offset. */
  169. settings.ee_write_offset = 0;
  170. /* Enable the eeprom-ready interrupt.
  171. * It will fire, if the EEPROM is ready.
  172. */
  173. EECR |= (1 << EERIE);
  174. irq_enable();
  175. }
  176. void settings_init(void)
  177. {
  178. #if CONF_EERING
  179. uint8_t next_index, serial, next_serial;
  180. #endif
  181. uint8_t found_index = 0u;
  182. #if !defined(__CHECKER__) && !defined(SIMULATOR)
  183. build_assert(sizeof(struct settings) == 64);
  184. #endif
  185. build_assert(SEA_SIZE(struct settings, temp_k) >= NR_BOOST_MODES);
  186. build_assert(SEA_SIZE(struct settings, temp_setpoint) >= NR_PRESETS);
  187. #if CONF_EERING
  188. /* Find the latest settings in the eeprom.
  189. * The latest setting is the one with the largest
  190. * index. However, wrap around must be considered.
  191. */
  192. serial = ee_read_byte((ee_addr_t)&ee_settings[0].serial);
  193. next_index = 0;
  194. do {
  195. found_index = next_index;
  196. next_index = ring_next(next_index, ARRAY_SIZE(ee_settings) + 1u);
  197. next_serial = ee_read_byte((ee_addr_t)&ee_settings[next_index].serial);
  198. if (next_serial != ((serial + 1u) & 0xFFu))
  199. break;
  200. serial = next_serial;
  201. } while (next_index != 0);
  202. settings.ee_index = found_index;
  203. #endif /* CONF_EERING */
  204. /* Read settings from EEPROM. */
  205. ee_read_block(&settings.cache,
  206. (ee_addr_t)&ee_settings[found_index],
  207. sizeof(settings.cache));
  208. }