serial.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * WARNING: be careful changing this code, it is very timing dependent
  3. */
  4. #pragma once
  5. #endif
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include <util/delay.h>
  9. #include <stdbool.h>
  10. #include "serial.h"
  11. #ifndef USE_I2C
  12. // Serial pulse period in microseconds. Its probably a bad idea to lower this
  13. // value.
  14. #define SERIAL_DELAY 24
  15. uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
  16. uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
  17. #define SLAVE_DATA_CORRUPT (1<<0)
  18. volatile uint8_t status = 0;
  19. inline static
  20. void serial_delay(void) {
  21. _delay_us(SERIAL_DELAY);
  22. }
  23. inline static
  24. void serial_output(void) {
  25. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  26. }
  27. // make the serial pin an input with pull-up resistor
  28. inline static
  29. void serial_input(void) {
  30. SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
  31. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  32. }
  33. inline static
  34. uint8_t serial_read_pin(void) {
  35. return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
  36. }
  37. inline static
  38. void serial_low(void) {
  39. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  40. }
  41. inline static
  42. void serial_high(void) {
  43. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  44. }
  45. void serial_master_init(void) {
  46. serial_output();
  47. serial_high();
  48. }
  49. void serial_slave_init(void) {
  50. serial_input();
  51. // Enable INT0
  52. EIMSK |= _BV(INT0);
  53. // Trigger on falling edge of INT0
  54. EICRA &= ~(_BV(ISC00) | _BV(ISC01));
  55. }
  56. // Used by the master to synchronize timing with the slave.
  57. static
  58. void sync_recv(void) {
  59. serial_input();
  60. // This shouldn't hang if the slave disconnects because the
  61. // serial line will float to high if the slave does disconnect.
  62. while (!serial_read_pin());
  63. serial_delay();
  64. }
  65. // Used by the slave to send a synchronization signal to the master.
  66. static
  67. void sync_send(void) {
  68. serial_output();
  69. serial_low();
  70. serial_delay();
  71. serial_high();
  72. }
  73. // Reads a byte from the serial line
  74. static
  75. uint8_t serial_read_byte(void) {
  76. uint8_t byte = 0;
  77. serial_input();
  78. for ( uint8_t i = 0; i < 8; ++i) {
  79. byte = (byte << 1) | serial_read_pin();
  80. serial_delay();
  81. _delay_us(1);
  82. }
  83. return byte;
  84. }
  85. // Sends a byte with MSB ordering
  86. static
  87. void serial_write_byte(uint8_t data) {
  88. uint8_t b = 8;
  89. serial_output();
  90. while( b-- ) {
  91. if(data & (1 << b)) {
  92. serial_high();
  93. } else {
  94. serial_low();
  95. }
  96. serial_delay();
  97. }
  98. }
  99. // interrupt handle to be used by the slave device
  100. ISR(SERIAL_PIN_INTERRUPT) {
  101. sync_send();
  102. uint8_t checksum = 0;
  103. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  104. serial_write_byte(serial_slave_buffer[i]);
  105. sync_send();
  106. checksum += serial_slave_buffer[i];
  107. }
  108. serial_write_byte(checksum);
  109. sync_send();
  110. // wait for the sync to finish sending
  111. serial_delay();
  112. // read the middle of pulses
  113. _delay_us(SERIAL_DELAY/2);
  114. uint8_t checksum_computed = 0;
  115. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  116. serial_master_buffer[i] = serial_read_byte();
  117. sync_send();
  118. checksum_computed += serial_master_buffer[i];
  119. }
  120. uint8_t checksum_received = serial_read_byte();
  121. sync_send();
  122. serial_input(); // end transaction
  123. if ( checksum_computed != checksum_received ) {
  124. status |= SLAVE_DATA_CORRUPT;
  125. } else {
  126. status &= ~SLAVE_DATA_CORRUPT;
  127. }
  128. contacted_by_master = true;
  129. }
  130. inline
  131. bool serial_slave_DATA_CORRUPT(void) {
  132. return status & SLAVE_DATA_CORRUPT;
  133. }
  134. // Copies the serial_slave_buffer to the master and sends the
  135. // serial_master_buffer to the slave.
  136. //
  137. // Returns:
  138. // 0 => no error
  139. // 1 => slave did not respond
  140. int serial_update_buffers(void) {
  141. // this code is very time dependent, so we need to disable interrupts
  142. cli();
  143. // signal to the slave that we want to start a transaction
  144. serial_output();
  145. serial_low();
  146. _delay_us(1);
  147. // wait for the slaves response
  148. serial_input();
  149. serial_high();
  150. _delay_us(SERIAL_DELAY);
  151. // check if the slave is present
  152. if (serial_read_pin()) {
  153. // slave failed to pull the line low, assume not present
  154. sei();
  155. return 1;
  156. }
  157. // if the slave is present syncronize with it
  158. sync_recv();
  159. uint8_t checksum_computed = 0;
  160. // receive data from the slave
  161. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  162. serial_slave_buffer[i] = serial_read_byte();
  163. sync_recv();
  164. checksum_computed += serial_slave_buffer[i];
  165. }
  166. uint8_t checksum_received = serial_read_byte();
  167. sync_recv();
  168. if (checksum_computed != checksum_received) {
  169. sei();
  170. return 1;
  171. }
  172. uint8_t checksum = 0;
  173. // send data to the slave
  174. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  175. serial_write_byte(serial_master_buffer[i]);
  176. sync_recv();
  177. checksum += serial_master_buffer[i];
  178. }
  179. serial_write_byte(checksum);
  180. sync_recv();
  181. // always, release the line when not in use
  182. serial_output();
  183. serial_high();
  184. sei();
  185. return 0;
  186. }