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.

235 lines
4.9 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. #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. #ifndef USE_SERIAL_PD2
  53. // Enable INT0
  54. EIMSK |= _BV(INT0);
  55. // Trigger on falling edge of INT0
  56. EICRA &= ~(_BV(ISC00) | _BV(ISC01));
  57. #else
  58. // Enable INT2
  59. EIMSK |= _BV(INT2);
  60. // Trigger on falling edge of INT2
  61. EICRA &= ~(_BV(ISC20) | _BV(ISC21));
  62. #endif
  63. }
  64. // Used by the master to synchronize timing with the slave.
  65. static
  66. void sync_recv(void) {
  67. serial_input();
  68. // This shouldn't hang if the slave disconnects because the
  69. // serial line will float to high if the slave does disconnect.
  70. while (!serial_read_pin());
  71. serial_delay();
  72. }
  73. // Used by the slave to send a synchronization signal to the master.
  74. static
  75. void sync_send(void) {
  76. serial_output();
  77. serial_low();
  78. serial_delay();
  79. serial_high();
  80. }
  81. // Reads a byte from the serial line
  82. static
  83. uint8_t serial_read_byte(void) {
  84. uint8_t byte = 0;
  85. serial_input();
  86. for ( uint8_t i = 0; i < 8; ++i) {
  87. byte = (byte << 1) | serial_read_pin();
  88. serial_delay();
  89. _delay_us(1);
  90. }
  91. return byte;
  92. }
  93. // Sends a byte with MSB ordering
  94. static
  95. void serial_write_byte(uint8_t data) {
  96. uint8_t b = 8;
  97. serial_output();
  98. while( b-- ) {
  99. if(data & (1 << b)) {
  100. serial_high();
  101. } else {
  102. serial_low();
  103. }
  104. serial_delay();
  105. }
  106. }
  107. // interrupt handle to be used by the slave device
  108. ISR(SERIAL_PIN_INTERRUPT) {
  109. sync_send();
  110. uint8_t checksum = 0;
  111. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  112. serial_write_byte(serial_slave_buffer[i]);
  113. sync_send();
  114. checksum += serial_slave_buffer[i];
  115. }
  116. serial_write_byte(checksum);
  117. sync_send();
  118. // wait for the sync to finish sending
  119. serial_delay();
  120. // read the middle of pulses
  121. _delay_us(SERIAL_DELAY/2);
  122. uint8_t checksum_computed = 0;
  123. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  124. serial_master_buffer[i] = serial_read_byte();
  125. sync_send();
  126. checksum_computed += serial_master_buffer[i];
  127. }
  128. uint8_t checksum_received = serial_read_byte();
  129. sync_send();
  130. serial_input(); // end transaction
  131. if ( checksum_computed != checksum_received ) {
  132. status |= SLAVE_DATA_CORRUPT;
  133. } else {
  134. status &= ~SLAVE_DATA_CORRUPT;
  135. }
  136. }
  137. inline
  138. bool serial_slave_DATA_CORRUPT(void) {
  139. return status & SLAVE_DATA_CORRUPT;
  140. }
  141. // Copies the serial_slave_buffer to the master and sends the
  142. // serial_master_buffer to the slave.
  143. //
  144. // Returns:
  145. // 0 => no error
  146. // 1 => slave did not respond
  147. int serial_update_buffers(void) {
  148. // this code is very time dependent, so we need to disable interrupts
  149. cli();
  150. // signal to the slave that we want to start a transaction
  151. serial_output();
  152. serial_low();
  153. _delay_us(1);
  154. // wait for the slaves response
  155. serial_input();
  156. serial_high();
  157. _delay_us(SERIAL_DELAY);
  158. // check if the slave is present
  159. if (serial_read_pin()) {
  160. // slave failed to pull the line low, assume not present
  161. sei();
  162. return 1;
  163. }
  164. // if the slave is present syncronize with it
  165. sync_recv();
  166. uint8_t checksum_computed = 0;
  167. // receive data from the slave
  168. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  169. serial_slave_buffer[i] = serial_read_byte();
  170. sync_recv();
  171. checksum_computed += serial_slave_buffer[i];
  172. }
  173. uint8_t checksum_received = serial_read_byte();
  174. sync_recv();
  175. if (checksum_computed != checksum_received) {
  176. sei();
  177. return 2;
  178. }
  179. uint8_t checksum = 0;
  180. // send data to the slave
  181. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  182. serial_write_byte(serial_master_buffer[i]);
  183. sync_recv();
  184. checksum += serial_master_buffer[i];
  185. }
  186. serial_write_byte(checksum);
  187. sync_recv();
  188. // always, release the line when not in use
  189. serial_output();
  190. serial_high();
  191. sei();
  192. return 0;
  193. }
  194. #endif