comm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Host communication
  3. *
  4. * Copyright (C) 2013 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  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. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "comm.h"
  20. #include <string.h>
  21. #include <util/crc16.h>
  22. #include <avr/io.h>
  23. #include <avr/cpufunc.h>
  24. struct rx_context {
  25. struct comm_message queue[COMM_RX_QUEUE_SIZE];
  26. uint8_t in_ptr;
  27. uint8_t out_ptr;
  28. uint8_t count;
  29. uint8_t byte_ptr;
  30. uint16_t timeout;
  31. };
  32. struct tx_context {
  33. struct comm_message queue[COMM_TX_QUEUE_SIZE];
  34. uint8_t in_ptr;
  35. uint8_t out_ptr;
  36. uint8_t count;
  37. uint8_t byte_ptr;
  38. uint8_t seq_count;
  39. };
  40. static struct rx_context rx;
  41. static struct tx_context tx;
  42. static void comm_reset(void)
  43. {
  44. memset(&rx, 0, sizeof(rx));
  45. memset(&tx, 0, sizeof(tx));
  46. }
  47. static inline uint16_t to_little_endian_16(uint16_t v)
  48. {
  49. union {
  50. uint16_t le;
  51. uint8_t b[2];
  52. } u = {
  53. .b = { v & 0xFF, v >> 8, },
  54. };
  55. return u.le;
  56. }
  57. static comm_crc_t message_calc_crc(const struct comm_message *msg)
  58. {
  59. const uint8_t *data = (const uint8_t *)msg;
  60. uint16_t crc = 0xFFFF, len;
  61. comm_crc_t ret;
  62. len = sizeof(*msg) - COMM_FCS_LEN;
  63. do {
  64. crc = _crc16_update(crc, *data++);
  65. } while (--len);
  66. crc ^= 0xFFFF;
  67. ret = to_little_endian_16(crc);
  68. return ret;
  69. }
  70. static void tx_try_put_next_byte(void)
  71. {
  72. const struct comm_message *msg;
  73. const uint8_t *buf;
  74. uint8_t data;
  75. if (tx.count == 0)
  76. return;
  77. if (!(UCSRA & (1 << UDRE)))
  78. return;
  79. msg = &tx.queue[tx.out_ptr];
  80. buf = (const uint8_t *)msg;
  81. data = buf[tx.byte_ptr];
  82. tx.byte_ptr++;
  83. if (tx.byte_ptr >= sizeof(struct comm_message)) {
  84. tx.byte_ptr = 0;
  85. tx.out_ptr = (tx.out_ptr + 1) & COMM_TX_QUEUE_MASK;
  86. tx.count--;
  87. if (tx.count == 0)
  88. UCSRB &= ~(1 << UDRIE);
  89. }
  90. UDR = data;
  91. }
  92. ISR(USART_UDRE_vect)
  93. {
  94. tx_try_put_next_byte();
  95. }
  96. void comm_drain_tx_queue(void)
  97. {
  98. uint8_t sreg;
  99. sreg = irq_disable_save();
  100. while (tx.count)
  101. tx_try_put_next_byte();
  102. irq_restore(sreg);
  103. }
  104. /* Called with IRQs disabled. */
  105. static void handle_tx_queue_overflow(struct comm_message *msg,
  106. bool may_enable_irqs)
  107. {
  108. /* TX queue is full. Notify the overflow condition
  109. * to the serial control, once we get the message out. */
  110. comm_msg_set_err(msg, COMM_ERR_Q);
  111. msg->fcs = message_calc_crc(msg);
  112. /* Manually push TX to get things going. */
  113. do {
  114. tx_try_put_next_byte();
  115. if (may_enable_irqs) {
  116. /* IRQs were enabled before we were called.
  117. * Be nice to other interrupts. */
  118. irq_enable();
  119. _NOP();
  120. irq_disable();
  121. }
  122. } while (tx.count >= COMM_TX_QUEUE_SIZE);
  123. }
  124. static uint8_t uart_rx(uint8_t *data_buf)
  125. {
  126. uint8_t status, data;
  127. status = UCSRA;
  128. if (!(status & (1 << RXC)))
  129. return 0;
  130. data = UDR;
  131. if (data_buf)
  132. *data_buf = data;
  133. if (status & ((1 << FE) | (1 << PE) | (1 << DOR)))
  134. return 2;
  135. return 1;
  136. }
  137. void comm_message_send(struct comm_message *msg, uint8_t dest_addr)
  138. {
  139. uint8_t sreg;
  140. comm_msg_set_da(msg, dest_addr);
  141. sreg = irq_disable_save();
  142. msg->seq = tx.seq_count++;
  143. msg->fcs = message_calc_crc(msg);
  144. if (tx.count >= COMM_TX_QUEUE_SIZE)
  145. handle_tx_queue_overflow(msg, __irqs_enabled(sreg));
  146. memcpy(&tx.queue[tx.in_ptr], msg, sizeof(*msg));
  147. tx.in_ptr = (tx.in_ptr + 1) & COMM_TX_QUEUE_MASK;
  148. tx.count++;
  149. UCSRB |= (1 << UDRIE);
  150. tx_try_put_next_byte();
  151. irq_restore(sreg);
  152. }
  153. static void handle_rx(const struct comm_message *msg)
  154. {
  155. COMM_MSG(reply);
  156. comm_crc_t crc;
  157. bool ok;
  158. if (comm_msg_da(msg) != COMM_LOCAL_ADDRESS) {
  159. /* The message was not for us. */
  160. return;
  161. }
  162. crc = message_calc_crc(msg);
  163. if (crc != msg->fcs) {
  164. /* CRC mismatch. */
  165. comm_msg_set_err(&reply, COMM_ERR_FCS);
  166. goto ack;
  167. }
  168. if (msg->fc & COMM_FC_RESET) {
  169. comm_reset();
  170. comm_msg_set_err(&reply, COMM_ERR_OK);
  171. goto ack;
  172. }
  173. ok = comm_handle_rx_message(msg, comm_payload(void *, &reply));
  174. if (!ok) {
  175. comm_msg_set_err(&reply, COMM_ERR_FAIL);
  176. goto ack;
  177. }
  178. ack:
  179. if (msg->fc & COMM_FC_REQ_ACK) {
  180. reply.fc |= COMM_FC_ACK;
  181. comm_message_send(&reply, comm_msg_sa(msg));
  182. }
  183. }
  184. /* RX interrupt */
  185. ISR(USART_RXC_vect)
  186. {
  187. uint8_t res, data, *rxbuf;
  188. while (1) {
  189. res = uart_rx(&data);
  190. if (!res)
  191. return;
  192. if (rx.count >= COMM_RX_QUEUE_SIZE) {
  193. /* Queue overflow. */
  194. continue;//TODO
  195. }
  196. rxbuf = (uint8_t *)&rx.queue[rx.in_ptr];
  197. rxbuf[rx.byte_ptr] = data;
  198. rx.byte_ptr++;
  199. if (rx.byte_ptr >= sizeof(struct comm_message)) {
  200. rx.byte_ptr = 0;
  201. rx.in_ptr = (rx.in_ptr + 1) & COMM_RX_QUEUE_MASK;
  202. rx.timeout = 0;
  203. mb();
  204. rx.count++;
  205. }
  206. }
  207. }
  208. void comm_centisecond_tick(void)
  209. {
  210. uint8_t sreg;
  211. sreg = irq_disable_save();
  212. if (rx.byte_ptr > 0)
  213. rx.timeout++;
  214. if (rx.timeout > 50 /* 0.5 seconds */) {
  215. /* Timeout! Reset the RX buffer. */
  216. rx.byte_ptr = 0;
  217. rx.timeout = 0;
  218. }
  219. irq_restore(sreg);
  220. }
  221. void comm_work(void)
  222. {
  223. uint8_t sreg;
  224. mb();
  225. if (rx.count) {
  226. handle_rx(&rx.queue[rx.out_ptr]);
  227. rx.out_ptr = (rx.out_ptr + 1) & COMM_RX_QUEUE_MASK;
  228. sreg = irq_disable_save();
  229. rx.count--;
  230. irq_restore(sreg);
  231. }
  232. }
  233. #define USE_2X (((uint64_t)F_CPU % (8ull * COMM_BAUDRATE)) < \
  234. ((uint64_t)F_CPU % (16ull * COMM_BAUDRATE)))
  235. #define UBRRVAL ((uint64_t)F_CPU / ((USE_2X ? 8ull : 16ull) * COMM_BAUDRATE))
  236. static void uart_init(void)
  237. {
  238. /* Set baud rate */
  239. UBRRL = UBRRVAL & 0xFF;
  240. UBRRH = (UBRRVAL >> 8) & 0xFF & ~(1 << URSEL);
  241. UCSRA = (!!(USE_2X) << U2X);
  242. /* 8 data bits, 1 stop bit, No parity */
  243. UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
  244. /* Enable transceiver and RX IRQs */
  245. UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
  246. /* Drain the RX buffer */
  247. while (uart_rx(NULL))
  248. mb();
  249. }
  250. void comm_init(void)
  251. {
  252. comm_reset();
  253. uart_init();
  254. }