serial.c 8.1 KB

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