serial.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(STM32_SYSCLK, 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. static SSTD_t *Transaction_table = NULL;
  68. static uint8_t Transaction_table_size = 0;
  69. void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size) {
  70. Transaction_table = sstd_table;
  71. Transaction_table_size = (uint8_t)sstd_table_size;
  72. serial_output();
  73. serial_high();
  74. }
  75. void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size) {
  76. Transaction_table = sstd_table;
  77. Transaction_table_size = (uint8_t)sstd_table_size;
  78. serial_input();
  79. palEnablePadEvent(PAL_PORT(SOFT_SERIAL_PIN), PAL_PAD(SOFT_SERIAL_PIN), PAL_EVENT_MODE_FALLING_EDGE);
  80. chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL);
  81. }
  82. // Used by the master to synchronize timing with the slave.
  83. static void __attribute__((noinline)) sync_recv(void) {
  84. serial_input();
  85. // This shouldn't hang if the slave disconnects because the
  86. // serial line will float to high if the slave does disconnect.
  87. while (!serial_read_pin()) {
  88. }
  89. serial_delay();
  90. }
  91. // Used by the slave to send a synchronization signal to the master.
  92. static void __attribute__((noinline)) sync_send(void) {
  93. serial_output();
  94. serial_low();
  95. serial_delay();
  96. serial_high();
  97. }
  98. // Reads a byte from the serial line
  99. static uint8_t __attribute__((noinline)) serial_read_byte(void) {
  100. uint8_t byte = 0;
  101. serial_input();
  102. for (uint8_t i = 0; i < 8; ++i) {
  103. byte = (byte << 1) | serial_read_pin();
  104. serial_delay();
  105. }
  106. return byte;
  107. }
  108. // Sends a byte with MSB ordering
  109. static void __attribute__((noinline)) serial_write_byte(uint8_t data) {
  110. uint8_t b = 8;
  111. serial_output();
  112. while (b--) {
  113. if (data & (1 << b)) {
  114. serial_high();
  115. } else {
  116. serial_low();
  117. }
  118. serial_delay();
  119. }
  120. }
  121. // interrupt handle to be used by the slave device
  122. void interrupt_handler(void *arg) {
  123. chSysLockFromISR();
  124. sync_send();
  125. // read mid pulses
  126. serial_delay_blip();
  127. uint8_t checksum_computed = 0;
  128. int sstd_index = 0;
  129. #ifdef SERIAL_USE_MULTI_TRANSACTION
  130. sstd_index = serial_read_byte();
  131. sync_send();
  132. #endif
  133. SSTD_t *trans = &Transaction_table[sstd_index];
  134. for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
  135. trans->initiator2target_buffer[i] = serial_read_byte();
  136. sync_send();
  137. checksum_computed += trans->initiator2target_buffer[i];
  138. }
  139. checksum_computed ^= 7;
  140. uint8_t checksum_received = serial_read_byte();
  141. sync_send();
  142. // wait for the sync to finish sending
  143. serial_delay();
  144. uint8_t checksum = 0;
  145. for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
  146. serial_write_byte(trans->target2initiator_buffer[i]);
  147. sync_send();
  148. serial_delay_half();
  149. checksum += trans->target2initiator_buffer[i];
  150. }
  151. serial_write_byte(checksum ^ 7);
  152. sync_send();
  153. // wait for the sync to finish sending
  154. serial_delay();
  155. *trans->status = (checksum_computed == checksum_received) ? TRANSACTION_ACCEPTED : TRANSACTION_DATA_ERROR;
  156. // end transaction
  157. serial_input();
  158. // TODO: remove extra delay between transactions
  159. serial_delay();
  160. chSysUnlockFromISR();
  161. }
  162. /////////
  163. // start transaction by initiator
  164. //
  165. // int soft_serial_transaction(int sstd_index)
  166. //
  167. // Returns:
  168. // TRANSACTION_END
  169. // TRANSACTION_NO_RESPONSE
  170. // TRANSACTION_DATA_ERROR
  171. // this code is very time dependent, so we need to disable interrupts
  172. #ifndef SERIAL_USE_MULTI_TRANSACTION
  173. int soft_serial_transaction(void) {
  174. int sstd_index = 0;
  175. #else
  176. int soft_serial_transaction(int sstd_index) {
  177. #endif
  178. if (sstd_index > Transaction_table_size) return TRANSACTION_TYPE_ERROR;
  179. SSTD_t *trans = &Transaction_table[sstd_index];
  180. // TODO: remove extra delay between transactions
  181. serial_delay();
  182. // this code is very time dependent, so we need to disable interrupts
  183. chSysLock();
  184. // signal to the slave that we want to start a transaction
  185. serial_output();
  186. serial_low();
  187. serial_delay_blip();
  188. // wait for the slaves response
  189. serial_input();
  190. serial_high();
  191. serial_delay();
  192. // check if the slave is present
  193. if (serial_read_pin()) {
  194. // slave failed to pull the line low, assume not present
  195. dprintf("serial::NO_RESPONSE\n");
  196. chSysUnlock();
  197. return TRANSACTION_NO_RESPONSE;
  198. }
  199. // if the slave is present syncronize with it
  200. uint8_t checksum = 0;
  201. // send data to the slave
  202. #ifdef SERIAL_USE_MULTI_TRANSACTION
  203. serial_write_byte(sstd_index); // first chunk is transaction id
  204. sync_recv();
  205. #endif
  206. for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
  207. serial_write_byte(trans->initiator2target_buffer[i]);
  208. sync_recv();
  209. checksum += trans->initiator2target_buffer[i];
  210. }
  211. serial_write_byte(checksum ^ 7);
  212. sync_recv();
  213. serial_delay();
  214. serial_delay(); // read mid pulses
  215. // receive data from the slave
  216. uint8_t checksum_computed = 0;
  217. for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
  218. trans->target2initiator_buffer[i] = serial_read_byte();
  219. sync_recv();
  220. checksum_computed += trans->target2initiator_buffer[i];
  221. }
  222. checksum_computed ^= 7;
  223. uint8_t checksum_received = serial_read_byte();
  224. sync_recv();
  225. serial_delay();
  226. if ((checksum_computed) != (checksum_received)) {
  227. dprintf("serial::FAIL[%u,%u,%u]\n", checksum_computed, checksum_received, sstd_index);
  228. serial_output();
  229. serial_high();
  230. chSysUnlock();
  231. return TRANSACTION_DATA_ERROR;
  232. }
  233. // always, release the line when not in use
  234. serial_high();
  235. serial_output();
  236. chSysUnlock();
  237. return TRANSACTION_END;
  238. }