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.

228 lines
4.7 KiB

6 years ago
  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. #ifndef USE_I2C
  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. inline static
  25. void serial_output(void) {
  26. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  27. }
  28. // make the serial pin an input with pull-up resistor
  29. inline static
  30. void serial_input(void) {
  31. SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
  32. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  33. }
  34. inline static
  35. uint8_t serial_read_pin(void) {
  36. return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
  37. }
  38. inline static
  39. void serial_low(void) {
  40. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  41. }
  42. inline static
  43. void serial_high(void) {
  44. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  45. }
  46. void serial_master_init(void) {
  47. serial_output();
  48. serial_high();
  49. }
  50. void serial_slave_init(void) {
  51. serial_input();
  52. // Enable INT0
  53. EIMSK |= _BV(INT0);
  54. // Trigger on falling edge of INT0
  55. EICRA &= ~(_BV(ISC00) | _BV(ISC01));
  56. }
  57. // Used by the master to synchronize timing with the slave.
  58. static
  59. void sync_recv(void) {
  60. serial_input();
  61. // This shouldn't hang if the slave disconnects because the
  62. // serial line will float to high if the slave does disconnect.
  63. while (!serial_read_pin());
  64. serial_delay();
  65. }
  66. // Used by the slave to send a synchronization signal to the master.
  67. static
  68. void sync_send(void) {
  69. serial_output();
  70. serial_low();
  71. serial_delay();
  72. serial_high();
  73. }
  74. // Reads a byte from the serial line
  75. static
  76. uint8_t serial_read_byte(void) {
  77. uint8_t byte = 0;
  78. serial_input();
  79. for ( uint8_t i = 0; i < 8; ++i) {
  80. byte = (byte << 1) | serial_read_pin();
  81. serial_delay();
  82. _delay_us(1);
  83. }
  84. return byte;
  85. }
  86. // Sends a byte with MSB ordering
  87. static
  88. void serial_write_byte(uint8_t data) {
  89. uint8_t b = 8;
  90. serial_output();
  91. while( b-- ) {
  92. if(data & (1 << b)) {
  93. serial_high();
  94. } else {
  95. serial_low();
  96. }
  97. serial_delay();
  98. }
  99. }
  100. // interrupt handle to be used by the slave device
  101. ISR(SERIAL_PIN_INTERRUPT) {
  102. sync_send();
  103. uint8_t checksum = 0;
  104. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  105. serial_write_byte(serial_slave_buffer[i]);
  106. sync_send();
  107. checksum += serial_slave_buffer[i];
  108. }
  109. serial_write_byte(checksum);
  110. sync_send();
  111. // wait for the sync to finish sending
  112. serial_delay();
  113. // read the middle of pulses
  114. _delay_us(SERIAL_DELAY/2);
  115. uint8_t checksum_computed = 0;
  116. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  117. serial_master_buffer[i] = serial_read_byte();
  118. sync_send();
  119. checksum_computed += serial_master_buffer[i];
  120. }
  121. uint8_t checksum_received = serial_read_byte();
  122. sync_send();
  123. serial_input(); // end transaction
  124. if ( checksum_computed != checksum_received ) {
  125. status |= SLAVE_DATA_CORRUPT;
  126. } else {
  127. status &= ~SLAVE_DATA_CORRUPT;
  128. }
  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. }
  187. #endif