serial.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * WARNING: be careful changing this code, it is very timing dependent
  3. */
  4. #include "quantum.h"
  5. #include "serial.h"
  6. #include "wait.h"
  7. #include <hal.h>
  8. // TODO: resolve/remove build warnings
  9. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) && defined(PROTOCOL_CHIBIOS) && defined(WS2812_DRIVER_BITBANG)
  10. # warning "RGBLED_SPLIT not supported with bitbang WS2812 driver"
  11. #endif
  12. // default wait implementation cannot be called within interrupt
  13. // this method seems to be more accurate than GPT timers
  14. #if PORT_SUPPORTS_RT == FALSE
  15. # error "chSysPolledDelayX method not supported on this platform"
  16. #else
  17. # undef wait_us
  18. # define wait_us(x) chSysPolledDelayX(US2RTC(CPU_CLOCK, x))
  19. #endif
  20. #ifndef SELECT_SOFT_SERIAL_SPEED
  21. # define SELECT_SOFT_SERIAL_SPEED 1
  22. // TODO: correct speeds...
  23. // 0: about 189kbps (Experimental only)
  24. // 1: about 137kbps (default)
  25. // 2: about 75kbps
  26. // 3: about 39kbps
  27. // 4: about 26kbps
  28. // 5: about 20kbps
  29. #endif
  30. // Serial pulse period in microseconds. At the moment, going lower than 12 causes communication failure
  31. #if SELECT_SOFT_SERIAL_SPEED == 0
  32. # define SERIAL_DELAY 12
  33. #elif SELECT_SOFT_SERIAL_SPEED == 1
  34. # define SERIAL_DELAY 16
  35. #elif SELECT_SOFT_SERIAL_SPEED == 2
  36. # define SERIAL_DELAY 24
  37. #elif SELECT_SOFT_SERIAL_SPEED == 3
  38. # define SERIAL_DELAY 32
  39. #elif SELECT_SOFT_SERIAL_SPEED == 4
  40. # define SERIAL_DELAY 48
  41. #elif SELECT_SOFT_SERIAL_SPEED == 5
  42. # define SERIAL_DELAY 64
  43. #else
  44. # error invalid SELECT_SOFT_SERIAL_SPEED value
  45. #endif
  46. inline static void serial_delay(void) { wait_us(SERIAL_DELAY); }
  47. inline static void serial_delay_half(void) { wait_us(SERIAL_DELAY / 2); }
  48. inline static void serial_delay_blip(void) { wait_us(1); }
  49. inline static void serial_output(void) { setPinOutput(SOFT_SERIAL_PIN); }
  50. inline static void serial_input(void) { setPinInputHigh(SOFT_SERIAL_PIN); }
  51. inline static bool serial_read_pin(void) { return !!readPin(SOFT_SERIAL_PIN); }
  52. inline static void serial_low(void) { writePinLow(SOFT_SERIAL_PIN); }
  53. inline static void serial_high(void) { writePinHigh(SOFT_SERIAL_PIN); }
  54. void interrupt_handler(void *arg);
  55. // Use thread + palWaitLineTimeout instead of palSetLineCallback
  56. // - Methods like setPinOutput and palEnableLineEvent/palDisableLineEvent
  57. // cause the interrupt to lock up, which would limit to only receiving data...
  58. static THD_WORKING_AREA(waThread1, 128);
  59. static THD_FUNCTION(Thread1, arg) {
  60. (void)arg;
  61. chRegSetThreadName("blinker");
  62. while (true) {
  63. palWaitLineTimeout(SOFT_SERIAL_PIN, TIME_INFINITE);
  64. interrupt_handler(NULL);
  65. }
  66. }
  67. void soft_serial_initiator_init(void) {
  68. serial_output();
  69. serial_high();
  70. }
  71. void soft_serial_target_init(void) {
  72. serial_input();
  73. palEnablePadEvent(PAL_PORT(SOFT_SERIAL_PIN), PAL_PAD(SOFT_SERIAL_PIN), PAL_EVENT_MODE_FALLING_EDGE);
  74. chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL);
  75. }
  76. // Used by the master to synchronize timing with the slave.
  77. static void __attribute__((noinline)) sync_recv(void) {
  78. serial_input();
  79. // This shouldn't hang if the slave disconnects because the
  80. // serial line will float to high if the slave does disconnect.
  81. while (!serial_read_pin()) {
  82. }
  83. serial_delay();
  84. }
  85. // Used by the slave to send a synchronization signal to the master.
  86. static void __attribute__((noinline)) sync_send(void) {
  87. serial_output();
  88. serial_low();
  89. serial_delay();
  90. serial_high();
  91. }
  92. // Reads a byte from the serial line
  93. static uint8_t __attribute__((noinline)) serial_read_byte(void) {
  94. uint8_t byte = 0;
  95. serial_input();
  96. for (uint8_t i = 0; i < 8; ++i) {
  97. byte = (byte << 1) | serial_read_pin();
  98. serial_delay();
  99. }
  100. return byte;
  101. }
  102. // Sends a byte with MSB ordering
  103. static void __attribute__((noinline)) serial_write_byte(uint8_t data) {
  104. uint8_t b = 8;
  105. serial_output();
  106. while (b--) {
  107. if (data & (1 << b)) {
  108. serial_high();
  109. } else {
  110. serial_low();
  111. }
  112. serial_delay();
  113. }
  114. }
  115. // interrupt handle to be used by the slave device
  116. void interrupt_handler(void *arg) {
  117. chSysLockFromISR();
  118. sync_send();
  119. // read mid pulses
  120. serial_delay_blip();
  121. uint8_t checksum_computed = 0;
  122. int sstd_index = 0;
  123. sstd_index = serial_read_byte();
  124. sync_send();
  125. split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
  126. for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
  127. split_trans_initiator2target_buffer(trans)[i] = serial_read_byte();
  128. sync_send();
  129. checksum_computed += split_trans_initiator2target_buffer(trans)[i];
  130. }
  131. checksum_computed ^= 7;
  132. uint8_t checksum_received = serial_read_byte();
  133. sync_send();
  134. // wait for the sync to finish sending
  135. serial_delay();
  136. // Allow any slave processing to occur
  137. if (trans->slave_callback) {
  138. trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans));
  139. }
  140. uint8_t checksum = 0;
  141. for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
  142. serial_write_byte(split_trans_target2initiator_buffer(trans)[i]);
  143. sync_send();
  144. serial_delay_half();
  145. checksum += split_trans_target2initiator_buffer(trans)[i];
  146. }
  147. serial_write_byte(checksum ^ 7);
  148. sync_send();
  149. // wait for the sync to finish sending
  150. serial_delay();
  151. // end transaction
  152. serial_input();
  153. // TODO: remove extra delay between transactions
  154. serial_delay();
  155. chSysUnlockFromISR();
  156. }
  157. /////////
  158. // start transaction by initiator
  159. //
  160. // bool soft_serial_transaction(int sstd_index)
  161. //
  162. // this code is very time dependent, so we need to disable interrupts
  163. bool soft_serial_transaction(int sstd_index) {
  164. if (sstd_index > NUM_TOTAL_TRANSACTIONS) return false;
  165. split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
  166. // TODO: remove extra delay between transactions
  167. serial_delay();
  168. // this code is very time dependent, so we need to disable interrupts
  169. chSysLock();
  170. // signal to the slave that we want to start a transaction
  171. serial_output();
  172. serial_low();
  173. serial_delay_blip();
  174. // wait for the slaves response
  175. serial_input();
  176. serial_high();
  177. serial_delay();
  178. // check if the slave is present
  179. if (serial_read_pin()) {
  180. // slave failed to pull the line low, assume not present
  181. dprintf("serial::NO_RESPONSE\n");
  182. chSysUnlock();
  183. return false;
  184. }
  185. // if the slave is present syncronize with it
  186. uint8_t checksum = 0;
  187. // send data to the slave
  188. serial_write_byte(sstd_index); // first chunk is transaction id
  189. sync_recv();
  190. for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
  191. serial_write_byte(split_trans_initiator2target_buffer(trans)[i]);
  192. sync_recv();
  193. checksum += split_trans_initiator2target_buffer(trans)[i];
  194. }
  195. serial_write_byte(checksum ^ 7);
  196. sync_recv();
  197. serial_delay();
  198. serial_delay(); // read mid pulses
  199. // receive data from the slave
  200. uint8_t checksum_computed = 0;
  201. for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
  202. split_trans_target2initiator_buffer(trans)[i] = serial_read_byte();
  203. sync_recv();
  204. checksum_computed += split_trans_target2initiator_buffer(trans)[i];
  205. }
  206. checksum_computed ^= 7;
  207. uint8_t checksum_received = serial_read_byte();
  208. sync_recv();
  209. serial_delay();
  210. if ((checksum_computed) != (checksum_received)) {
  211. dprintf("serial::FAIL[%u,%u,%u]\n", checksum_computed, checksum_received, sstd_index);
  212. serial_output();
  213. serial_high();
  214. chSysUnlock();
  215. return false;
  216. }
  217. // always, release the line when not in use
  218. serial_high();
  219. serial_output();
  220. chSysUnlock();
  221. return true;
  222. }