conf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * PiLC HAT firmware
  3. * I2C configuration interface
  4. *
  5. * Copyright (c) 2016 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 "conf.h"
  22. #include "i2c_slave.h"
  23. #include "util.h"
  24. #include "pb_txen.h"
  25. #include "eepemu_24cxx.h"
  26. #include <string.h>
  27. #include <avr/io.h>
  28. enum conf_items {
  29. CONF_NONE,
  30. CONF_XTALCAL, /* Crystal calibration */
  31. CONF_EEMUWE, /* EEPROM emulation write enable */
  32. CONF_PBTXENDBG, /* TX-en debug mode */
  33. CONF_PBTXENTO, /* TX-en timeout */
  34. };
  35. struct conf_context {
  36. enum conf_items item;
  37. uint8_t count;
  38. uint8_t buf[4];
  39. };
  40. static struct conf_context conf;
  41. static void set_osccal(uint8_t osccal)
  42. {
  43. OSCCAL = osccal;
  44. }
  45. static uint8_t get_osccal(void)
  46. {
  47. return OSCCAL;
  48. }
  49. static uint8_t handle_bool_read(bool (*getter)(void))
  50. {
  51. struct conf_context *pc = &conf;
  52. uint8_t ret;
  53. ret = (uint8_t)getter();
  54. pc->item = CONF_NONE;
  55. return ret;
  56. }
  57. static uint8_t handle_u8_read(uint8_t (*getter)(void))
  58. {
  59. struct conf_context *pc = &conf;
  60. uint8_t ret;
  61. ret = getter();
  62. pc->item = CONF_NONE;
  63. return ret;
  64. }
  65. static uint8_t handle_u16_read(uint16_t (*getter)(void))
  66. {
  67. struct conf_context *pc = &conf;
  68. uint16_t value;
  69. uint8_t ret;
  70. build_assert(ARRAY_SIZE(pc->buf) >= 2);
  71. if (pc->count == 0) {
  72. value = getter();
  73. pc->buf[0] = (uint8_t)value;
  74. pc->buf[1] = (uint8_t)(value >> 8);
  75. }
  76. ret = pc->buf[pc->count++];
  77. if (pc->count >= ARRAY_SIZE(pc->buf))
  78. pc->item = CONF_NONE;
  79. return ret;
  80. }
  81. static uint8_t conf_transmit(bool start)
  82. {
  83. struct conf_context *pc = &conf;
  84. uint8_t ret = 0;
  85. if (start)
  86. pc->count = 0;
  87. switch (pc->item) {
  88. case CONF_NONE:
  89. /* error */
  90. break;
  91. case CONF_XTALCAL:
  92. ret = handle_u8_read(get_osccal);
  93. break;
  94. case CONF_EEMUWE:
  95. ret = handle_bool_read(ee24cxx_get_we);
  96. break;
  97. case CONF_PBTXENDBG:
  98. ret = handle_bool_read(pb_txen_get_debug);
  99. break;
  100. case CONF_PBTXENTO:
  101. ret = handle_u16_read(pb_txen_get_timeout);
  102. break;
  103. }
  104. return ret;
  105. }
  106. static void handle_safe_bool_write(uint8_t data, void (*handler)(bool value))
  107. {
  108. struct conf_context *pc = &conf;
  109. uint8_t a, b;
  110. build_assert(ARRAY_SIZE(pc->buf) >= 2);
  111. pc->buf[pc->count++] = data;
  112. if (pc->count >= 2) {
  113. a = pc->buf[0];
  114. b = (uint8_t)~(pc->buf[1]);
  115. if (a == b && (a == 0 || a == 1))
  116. handler(!!a);
  117. pc->item = CONF_NONE;
  118. }
  119. }
  120. static void handle_safe_u8_write(uint8_t data, void (*handler)(uint8_t value))
  121. {
  122. struct conf_context *pc = &conf;
  123. uint8_t a, b;
  124. build_assert(ARRAY_SIZE(pc->buf) >= 2);
  125. pc->buf[pc->count++] = data;
  126. if (pc->count >= 2) {
  127. a = pc->buf[0];
  128. b = (uint8_t)~(pc->buf[1]);
  129. if (a == b)
  130. handler(a);
  131. pc->item = CONF_NONE;
  132. }
  133. }
  134. static void handle_safe_u16_write(uint8_t data, void (*handler)(uint16_t value))
  135. {
  136. struct conf_context *pc = &conf;
  137. uint16_t a, b;
  138. build_assert(ARRAY_SIZE(pc->buf) >= 4);
  139. pc->buf[pc->count++] = data;
  140. if (pc->count >= 4) {
  141. a = (uint16_t)pc->buf[0];
  142. a |= (uint16_t)pc->buf[1] << 8;
  143. b = (uint16_t)pc->buf[2];
  144. b |= (uint16_t)pc->buf[3] << 8;
  145. b = ~b;
  146. if (a == b)
  147. handler(a);
  148. pc->item = CONF_NONE;
  149. }
  150. }
  151. static void conf_receive(bool start, uint8_t data)
  152. {
  153. struct conf_context *pc = &conf;
  154. if (start) {
  155. pc->item = CONF_NONE;
  156. pc->count = 0;
  157. }
  158. switch (pc->item) {
  159. case CONF_NONE:
  160. pc->item = (enum conf_items)data;
  161. pc->count = 0;
  162. break;
  163. case CONF_XTALCAL:
  164. handle_safe_u8_write(data, set_osccal);
  165. break;
  166. case CONF_EEMUWE:
  167. handle_safe_bool_write(data, ee24cxx_set_we);
  168. break;
  169. case CONF_PBTXENDBG:
  170. handle_safe_bool_write(data, pb_txen_set_debug);
  171. break;
  172. case CONF_PBTXENTO:
  173. handle_safe_u16_write(data, pb_txen_set_timeout);
  174. break;
  175. }
  176. }
  177. static const struct i2c_slave_ops __flash conf_i2c_slave_ops = {
  178. .transmit = conf_transmit,
  179. .receive = conf_receive,
  180. };
  181. void conf_init(void)
  182. {
  183. memset(&conf, 0, sizeof(conf));
  184. conf.item = CONF_NONE;
  185. i2cs_add_slave(CONF_ADDR, &conf_i2c_slave_ops);
  186. }