main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * CNC-remote-control
  3. * Button processor
  4. *
  5. * Copyright (C) 2009-2016 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "util.h"
  17. #include "spi_interface.h"
  18. #include <avr/io.h>
  19. #include <avr/interrupt.h>
  20. #include <avr/wdt.h>
  21. #include <stdint.h>
  22. typedef uint16_t jiffies_t;
  23. #define BUTTON_DEBOUNCE msec2jiffies(40)
  24. #define ENC_DEBOUNCE usec2jiffies(3500)
  25. /* Hardware state of a button */
  26. struct button_hwstate {
  27. bool state; /* 1 = pressed, 0 = released */
  28. bool synchronized; /* Is synchronized with software state? */
  29. jiffies_t sync_deadline; /* Deadline for sync */
  30. };
  31. /* Hardware state of a torque encoder */
  32. struct encoder_hwstate {
  33. uint8_t gray; /* The graycode state */
  34. uint8_t prev_gray;
  35. bool synchronized; /* Is synchronized with software state? */
  36. jiffies_t sync_deadline; /* Deadline for sync */
  37. };
  38. /* Software state of a torque encoder */
  39. struct encoder_swstate {
  40. int8_t state;
  41. };
  42. static struct button_hwstate hwstates[14];
  43. static uint16_t swstates;
  44. static struct encoder_hwstate enc_hwstates[1];
  45. static struct encoder_swstate enc_swstates[1];
  46. /* Convert 2bit graycode to binary */
  47. static inline uint8_t gray2bin_2bit(uint8_t graycode)
  48. {
  49. if (graycode & 2)
  50. graycode ^= 1;
  51. return graycode;
  52. }
  53. static void jiffies_init(void)
  54. {
  55. #define JPS 31250 /* jiffies per second */
  56. /* Initialize the timer to 8M/256=31250 */
  57. TCNT1 = 0;
  58. OCR1A = 0;
  59. TIMSK = 0;
  60. TCCR1A = 0;
  61. TCCR1B = (0 << CS10) | (0 << CS11) | (1 << CS12);
  62. }
  63. #define msec2jiffies(ms) ((jiffies_t)((uint32_t)(ms) * JPS / (uint32_t)1000))
  64. #define usec2jiffies(us) ((jiffies_t)((uint32_t)(us) * JPS / (uint32_t)1000000))
  65. #define time_after(a, b) ((int16_t)(b) - (int16_t)(a) < 0)
  66. #define time_before(a, b) time_after(b, a)
  67. static inline jiffies_t jiffies_get(void)
  68. {
  69. return TCNT1;
  70. }
  71. static inline void do_button_read(struct button_hwstate *hw,
  72. bool state,
  73. jiffies_t timestamp)
  74. {
  75. if (state != hw->state) {
  76. hw->state = state;
  77. hw->synchronized = 0;
  78. hw->sync_deadline = timestamp + BUTTON_DEBOUNCE;
  79. }
  80. }
  81. static inline void do_encoder_read(struct encoder_hwstate *hw,
  82. bool a, bool b,
  83. jiffies_t timestamp)
  84. {
  85. uint8_t gray;
  86. gray = (uint8_t)((uint8_t)a | ((uint8_t)b << 1u));
  87. if (gray != hw->gray) {
  88. hw->gray = gray;
  89. hw->synchronized = 0;
  90. hw->sync_deadline = timestamp + ENC_DEBOUNCE;
  91. }
  92. }
  93. /* Read the hardware states of the buttons */
  94. static void buttons_read(void)
  95. {
  96. uint8_t b, c, d;
  97. jiffies_t now;
  98. b = PINB;
  99. c = PINC;
  100. d = PIND;
  101. now = jiffies_get();
  102. /* Interpret the buttons */
  103. do_button_read(&hwstates[0], !(b & (1 << 0)), now);
  104. do_button_read(&hwstates[1], !(b & (1 << 1)), now);
  105. do_button_read(&hwstates[2], !(c & (1 << 0)), now);
  106. do_button_read(&hwstates[3], !(c & (1 << 1)), now);
  107. do_button_read(&hwstates[4], !(c & (1 << 2)), now);
  108. do_button_read(&hwstates[5], !(c & (1 << 3)), now);
  109. do_button_read(&hwstates[6], !(c & (1 << 4)), now);
  110. do_button_read(&hwstates[7], !(c & (1 << 5)), now);
  111. do_button_read(&hwstates[8], !(d & (1 << 0)), now);
  112. do_button_read(&hwstates[9], !(d & (1 << 1)), now);
  113. do_button_read(&hwstates[10], !(d & (1 << 2)), now);
  114. do_button_read(&hwstates[11], !(d & (1 << 3)), now);
  115. do_button_read(&hwstates[12], !(d & (1 << 4)), now);
  116. do_button_read(&hwstates[13], !(d & (1 << 5)), now);
  117. BUILD_BUG_ON(ARRAY_SIZE(hwstates) != 14);
  118. BUILD_BUG_ON(ARRAY_SIZE(hwstates) > sizeof(swstates) * 8);
  119. /* Interpret the torque encoders */
  120. do_encoder_read(&enc_hwstates[0], !(d & (1 << 6)), !(d & (1 << 7)), now);
  121. BUILD_BUG_ON(ARRAY_SIZE(enc_hwstates) != 1);
  122. BUILD_BUG_ON(ARRAY_SIZE(enc_hwstates) != ARRAY_SIZE(enc_swstates));
  123. }
  124. static void buttons_init(void)
  125. {
  126. uint8_t i;
  127. /* Configure inputs and pullups */
  128. DDRB = (uint8_t)(DDRB & ~0x03u);
  129. PORTB = (uint8_t)(PORTB | 0x03u);
  130. DDRC = (uint8_t)(DDRC & ~0x3Fu);
  131. PORTC = (uint8_t)(PORTC | 0x3Fu);
  132. DDRD = (uint8_t)(DDRD & ~0xFFu);
  133. PORTD = (uint8_t)(PORTD | 0xFFu);
  134. buttons_read();
  135. for (i = 0; i < ARRAY_SIZE(enc_hwstates); i++)
  136. enc_hwstates[i].prev_gray = enc_hwstates[i].gray;
  137. }
  138. static void trigger_trans_interrupt(void)
  139. {
  140. SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT &
  141. ~(1u << SPI_SLAVE_TRANSIRQ_BIT));
  142. nop();
  143. nop();
  144. SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT |
  145. (1u << SPI_SLAVE_TRANSIRQ_BIT));
  146. }
  147. static inline uint8_t do_sync_button(struct button_hwstate *hw,
  148. uint8_t swstate_bit,
  149. jiffies_t now)
  150. {
  151. bool state;
  152. if (!hw->synchronized) {
  153. if (time_after(now, hw->sync_deadline)) {
  154. state = hw->state;
  155. irq_disable();
  156. if (state)
  157. swstates |= (1u << swstate_bit);
  158. else
  159. swstates &= ~(1u << swstate_bit);
  160. irq_enable();
  161. hw->synchronized = 1;
  162. return 1;
  163. }
  164. }
  165. return 0;
  166. }
  167. static inline uint8_t do_sync_encoder(struct encoder_hwstate *hw,
  168. struct encoder_swstate *sw,
  169. jiffies_t now)
  170. {
  171. uint8_t cur, prev;
  172. if (!hw->synchronized) {
  173. if (time_after(now, hw->sync_deadline)) {
  174. cur = gray2bin_2bit(hw->gray);
  175. prev = gray2bin_2bit(hw->prev_gray);
  176. hw->prev_gray = hw->gray;
  177. hw->synchronized = 1;
  178. if (cur == ((prev + 1) & 3)) {
  179. irq_disable();
  180. sw->state--;
  181. irq_enable();
  182. return 1;
  183. }
  184. if (cur == ((prev - 1) & 3)) {
  185. irq_disable();
  186. sw->state++;
  187. irq_enable();
  188. return 1;
  189. }
  190. }
  191. }
  192. return 0;
  193. }
  194. /* Synchronize the software state of the buttons */
  195. static void buttons_synchronize(void)
  196. {
  197. uint8_t i, one_state_changed = 0;
  198. jiffies_t now;
  199. now = jiffies_get();
  200. /* Sync buttons */
  201. for (i = 0; i < ARRAY_SIZE(hwstates); i++) {
  202. one_state_changed |= do_sync_button(&hwstates[i], i,
  203. now);
  204. }
  205. /* Sync encoders */
  206. for (i = 0; i < ARRAY_SIZE(enc_hwstates); i++) {
  207. one_state_changed |= do_sync_encoder(&enc_hwstates[i],
  208. &enc_swstates[i],
  209. now);
  210. }
  211. if (one_state_changed)
  212. trigger_trans_interrupt();
  213. }
  214. static noreturn void enter_bootloader(void)
  215. {
  216. irq_disable();
  217. wdt_reset();
  218. /* Jump to bootloader code */
  219. __asm__ __volatile__(
  220. "ijmp\n"
  221. : /* None */
  222. : [_Z] "z" (BOOT_OFFSET / 2)
  223. );
  224. unreachable();
  225. }
  226. ISR(SPI_STC_vect)
  227. {
  228. uint8_t data;
  229. static uint8_t checksum;
  230. static bool enterboot_first_stage_done;
  231. data = SPDR;
  232. switch (data) {
  233. case SPI_CONTROL_ENTERBOOT:
  234. data = SPI_RESULT_OK;
  235. checksum = 0;
  236. enterboot_first_stage_done = 1;
  237. goto out;
  238. case SPI_CONTROL_ENTERBOOT2:
  239. if (enterboot_first_stage_done)
  240. enter_bootloader();
  241. data = SPI_RESULT_FAIL;
  242. checksum = 0;
  243. goto out;
  244. default:
  245. enterboot_first_stage_done = 0;
  246. }
  247. switch (data) {
  248. case SPI_CONTROL_GETLOW:
  249. data = swstates & 0xFF;
  250. checksum ^= data;
  251. break;
  252. case SPI_CONTROL_GETHIGH:
  253. data = (uint8_t)((swstates >> 8) & 0xFFu);
  254. checksum ^= data;
  255. break;
  256. case SPI_CONTROL_GETENC:
  257. data = (uint8_t)(enc_swstates[0].state);
  258. enc_swstates[0].state = 0;
  259. checksum ^= data;
  260. break;
  261. case SPI_CONTROL_GETSUM:
  262. data = checksum ^ 0xFF;
  263. checksum = 0;
  264. break;
  265. case SPI_CONTROL_TESTAPP:
  266. data = SPI_RESULT_OK;
  267. checksum = 0;
  268. break;
  269. case SPI_CONTROL_ENTERAPP:
  270. case SPI_CONTROL_NOP:
  271. default:
  272. data = 0;
  273. checksum = 0;
  274. }
  275. out:
  276. SPDR = data;
  277. }
  278. static void spi_init(void)
  279. {
  280. /* SPI slave mode 0 with IRQ enabled. */
  281. DDRB = (uint8_t)(DDRB | (1u << 4/*MISO*/));
  282. DDRB = (uint8_t)(DDRB & ~((1u << 5/*SCK*/) | (1u << 3/*MOSI*/) |
  283. (1u << 2/*SS*/)));
  284. SPI_SLAVE_TRANSIRQ_PORT = (uint8_t)(SPI_SLAVE_TRANSIRQ_PORT |
  285. (1u << SPI_SLAVE_TRANSIRQ_BIT));
  286. SPI_SLAVE_TRANSIRQ_DDR = (uint8_t)(SPI_SLAVE_TRANSIRQ_DDR |
  287. (1u << SPI_SLAVE_TRANSIRQ_BIT));
  288. SPCR = (1u << SPE) | (1u << SPIE) | (0u << CPOL) | (0u << CPHA);
  289. (void)SPSR; /* clear state */
  290. (void)SPDR; /* clear state */
  291. }
  292. int main(void) _mainfunc;
  293. int main(void)
  294. {
  295. irq_disable();
  296. wdt_enable(WDTO_500MS);
  297. jiffies_init();
  298. buttons_init();
  299. spi_init();
  300. irq_enable();
  301. while (1) {
  302. buttons_read();
  303. buttons_synchronize();
  304. wdt_reset();
  305. }
  306. }