You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.0 KiB

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