2
0

serial.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #ifdef USE_SERIAL
  12. #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
  13. // Serial pulse period in microseconds.
  14. #define SELECT_SERIAL_SPEED 1
  15. #if SELECT_SERIAL_SPEED == 0
  16. // Very High speed
  17. #define SERIAL_DELAY 4 // micro sec
  18. #define READ_WRITE_START_ADJUST 30 // cycles
  19. #define READ_WRITE_WIDTH_ADJUST 10 // cycles
  20. #elif SELECT_SERIAL_SPEED == 1
  21. // High speed
  22. #define SERIAL_DELAY 6 // micro sec
  23. #define READ_WRITE_START_ADJUST 23 // cycles
  24. #define READ_WRITE_WIDTH_ADJUST 10 // cycles
  25. #elif SELECT_SERIAL_SPEED == 2
  26. // Middle speed
  27. #define SERIAL_DELAY 12 // micro sec
  28. #define READ_WRITE_START_ADJUST 25 // cycles
  29. #define READ_WRITE_WIDTH_ADJUST 10 // cycles
  30. #elif SELECT_SERIAL_SPEED == 3
  31. // Low speed
  32. #define SERIAL_DELAY 24 // micro sec
  33. #define READ_WRITE_START_ADJUST 25 // cycles
  34. #define READ_WRITE_WIDTH_ADJUST 10 // cycles
  35. #elif SELECT_SERIAL_SPEED == 4
  36. // Very Low speed
  37. #define SERIAL_DELAY 50 // micro sec
  38. #define READ_WRITE_START_ADJUST 25 // cycles
  39. #define READ_WRITE_WIDTH_ADJUST 10 // cycles
  40. #else
  41. #error Illegal Serial Speed
  42. #endif
  43. #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
  44. #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
  45. #define SLAVE_INT_WIDTH 1
  46. #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
  47. uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
  48. uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
  49. #define SLAVE_DATA_CORRUPT (1<<0)
  50. volatile uint8_t status = 0;
  51. inline static
  52. void serial_delay(void) {
  53. _delay_us(SERIAL_DELAY);
  54. }
  55. inline static
  56. void serial_delay_half1(void) {
  57. _delay_us(SERIAL_DELAY_HALF1);
  58. }
  59. inline static
  60. void serial_delay_half2(void) {
  61. _delay_us(SERIAL_DELAY_HALF2);
  62. }
  63. inline static
  64. void serial_output(void) {
  65. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  66. }
  67. // make the serial pin an input with pull-up resistor
  68. inline static
  69. void serial_input_with_pullup(void) {
  70. SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
  71. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  72. }
  73. inline static
  74. uint8_t serial_read_pin(void) {
  75. return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
  76. }
  77. inline static
  78. void serial_low(void) {
  79. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  80. }
  81. inline static
  82. void serial_high(void) {
  83. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  84. }
  85. void serial_master_init(void) {
  86. serial_output();
  87. serial_high();
  88. }
  89. void serial_slave_init(void) {
  90. serial_input_with_pullup();
  91. #if SERIAL_PIN_MASK == _BV(PD0)
  92. // Enable INT0
  93. EIMSK |= _BV(INT0);
  94. // Trigger on falling edge of INT0
  95. EICRA &= ~(_BV(ISC00) | _BV(ISC01));
  96. #elif SERIAL_PIN_MASK == _BV(PD2)
  97. // Enable INT2
  98. EIMSK |= _BV(INT2);
  99. // Trigger on falling edge of INT2
  100. EICRA &= ~(_BV(ISC20) | _BV(ISC21));
  101. #else
  102. #error unknown SERIAL_PIN_MASK value
  103. #endif
  104. }
  105. // Used by the sender to synchronize timing with the reciver.
  106. static
  107. void sync_recv(void) {
  108. for (int i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
  109. }
  110. // This shouldn't hang if the slave disconnects because the
  111. // serial line will float to high if the slave does disconnect.
  112. while (!serial_read_pin());
  113. }
  114. // Used by the reciver to send a synchronization signal to the sender.
  115. static
  116. void sync_send(void) {
  117. serial_low();
  118. serial_delay();
  119. serial_high();
  120. }
  121. // Reads a byte from the serial line
  122. static
  123. uint8_t serial_read_byte(void) {
  124. uint8_t byte = 0;
  125. _delay_sub_us(READ_WRITE_START_ADJUST);
  126. for ( uint8_t i = 0; i < 8; ++i) {
  127. serial_delay_half1(); // read the middle of pulses
  128. byte = (byte << 1) | serial_read_pin();
  129. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  130. serial_delay_half2();
  131. }
  132. return byte;
  133. }
  134. // Sends a byte with MSB ordering
  135. static
  136. void serial_write_byte(uint8_t data) {
  137. uint8_t b = 1<<7;
  138. while( b ) {
  139. if(data & b) {
  140. serial_high();
  141. } else {
  142. serial_low();
  143. }
  144. b >>= 1;
  145. serial_delay();
  146. }
  147. serial_low(); // sync_send() / senc_recv() need raise edge
  148. }
  149. // interrupt handle to be used by the slave device
  150. ISR(SERIAL_PIN_INTERRUPT) {
  151. serial_output();
  152. // slave send phase
  153. uint8_t checksum = 0;
  154. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  155. sync_send();
  156. serial_write_byte(serial_slave_buffer[i]);
  157. checksum += serial_slave_buffer[i];
  158. }
  159. sync_send();
  160. serial_write_byte(checksum);
  161. // slave switch to input
  162. sync_send(); //0
  163. serial_delay_half1(); //1
  164. serial_low(); //2
  165. serial_input_with_pullup(); //2
  166. serial_delay_half1(); //3
  167. // slave recive phase
  168. uint8_t checksum_computed = 0;
  169. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  170. sync_recv();
  171. serial_master_buffer[i] = serial_read_byte();
  172. checksum_computed += serial_master_buffer[i];
  173. }
  174. sync_recv();
  175. uint8_t checksum_received = serial_read_byte();
  176. if ( checksum_computed != checksum_received ) {
  177. status |= SLAVE_DATA_CORRUPT;
  178. } else {
  179. status &= ~SLAVE_DATA_CORRUPT;
  180. }
  181. sync_recv(); //weit master output to high
  182. }
  183. inline
  184. bool serial_slave_DATA_CORRUPT(void) {
  185. return status & SLAVE_DATA_CORRUPT;
  186. }
  187. // Copies the serial_slave_buffer to the master and sends the
  188. // serial_master_buffer to the slave.
  189. //
  190. // Returns:
  191. // 0 => no error
  192. // 1 => slave did not respond
  193. // 2 => checksum error
  194. int serial_update_buffers(void) {
  195. // this code is very time dependent, so we need to disable interrupts
  196. cli();
  197. // signal to the slave that we want to start a transaction
  198. serial_output();
  199. serial_low();
  200. _delay_us(SLAVE_INT_WIDTH);
  201. // wait for the slaves response
  202. serial_input_with_pullup();
  203. _delay_us(SLAVE_INT_RESPONSE_TIME);
  204. // check if the slave is present
  205. if (serial_read_pin()) {
  206. // slave failed to pull the line low, assume not present
  207. serial_output();
  208. serial_high();
  209. sei();
  210. return 1;
  211. }
  212. // master recive phase
  213. // if the slave is present syncronize with it
  214. uint8_t checksum_computed = 0;
  215. // receive data from the slave
  216. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  217. sync_recv();
  218. serial_slave_buffer[i] = serial_read_byte();
  219. checksum_computed += serial_slave_buffer[i];
  220. }
  221. sync_recv();
  222. uint8_t checksum_received = serial_read_byte();
  223. if (checksum_computed != checksum_received) {
  224. serial_output();
  225. serial_high();
  226. sei();
  227. return 2;
  228. }
  229. // master switch to output
  230. sync_recv(); //0
  231. serial_delay(); //1
  232. serial_low(); //3
  233. serial_output(); // 3
  234. serial_delay_half1(); //4
  235. // master send phase
  236. uint8_t checksum = 0;
  237. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  238. sync_send();
  239. serial_write_byte(serial_master_buffer[i]);
  240. checksum += serial_master_buffer[i];
  241. }
  242. sync_send();
  243. serial_write_byte(checksum);
  244. // always, release the line when not in use
  245. sync_send();
  246. sei();
  247. return 0;
  248. }