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.

287 lines
7.8 KiB

  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) {
  47. wait_us(SERIAL_DELAY);
  48. }
  49. inline static void serial_delay_half(void) {
  50. wait_us(SERIAL_DELAY / 2);
  51. }
  52. inline static void serial_delay_blip(void) {
  53. wait_us(1);
  54. }
  55. inline static void serial_output(void) {
  56. setPinOutput(SOFT_SERIAL_PIN);
  57. }
  58. inline static void serial_input(void) {
  59. setPinInputHigh(SOFT_SERIAL_PIN);
  60. }
  61. inline static bool serial_read_pin(void) {
  62. return !!readPin(SOFT_SERIAL_PIN);
  63. }
  64. inline static void serial_low(void) {
  65. writePinLow(SOFT_SERIAL_PIN);
  66. }
  67. inline static void serial_high(void) {
  68. writePinHigh(SOFT_SERIAL_PIN);
  69. }
  70. void interrupt_handler(void *arg);
  71. // Use thread + palWaitLineTimeout instead of palSetLineCallback
  72. // - Methods like setPinOutput and palEnableLineEvent/palDisableLineEvent
  73. // cause the interrupt to lock up, which would limit to only receiving data...
  74. static THD_WORKING_AREA(waThread1, 128);
  75. static THD_FUNCTION(Thread1, arg) {
  76. (void)arg;
  77. chRegSetThreadName("blinker");
  78. while (true) {
  79. palWaitLineTimeout(SOFT_SERIAL_PIN, TIME_INFINITE);
  80. interrupt_handler(NULL);
  81. }
  82. }
  83. void soft_serial_initiator_init(void) {
  84. serial_output();
  85. serial_high();
  86. }
  87. void soft_serial_target_init(void) {
  88. serial_input();
  89. palEnablePadEvent(PAL_PORT(SOFT_SERIAL_PIN), PAL_PAD(SOFT_SERIAL_PIN), PAL_EVENT_MODE_FALLING_EDGE);
  90. chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL);
  91. }
  92. // Used by the master to synchronize timing with the slave.
  93. static void __attribute__((noinline)) sync_recv(void) {
  94. serial_input();
  95. // This shouldn't hang if the slave disconnects because the
  96. // serial line will float to high if the slave does disconnect.
  97. while (!serial_read_pin()) {
  98. }
  99. serial_delay();
  100. }
  101. // Used by the slave to send a synchronization signal to the master.
  102. static void __attribute__((noinline)) sync_send(void) {
  103. serial_output();
  104. serial_low();
  105. serial_delay();
  106. serial_high();
  107. }
  108. // Reads a byte from the serial line
  109. static uint8_t __attribute__((noinline)) serial_read_byte(void) {
  110. uint8_t byte = 0;
  111. serial_input();
  112. for (uint8_t i = 0; i < 8; ++i) {
  113. byte = (byte << 1) | serial_read_pin();
  114. serial_delay();
  115. }
  116. return byte;
  117. }
  118. // Sends a byte with MSB ordering
  119. static void __attribute__((noinline)) serial_write_byte(uint8_t data) {
  120. uint8_t b = 8;
  121. serial_output();
  122. while (b--) {
  123. if (data & (1 << b)) {
  124. serial_high();
  125. } else {
  126. serial_low();
  127. }
  128. serial_delay();
  129. }
  130. }
  131. // interrupt handle to be used by the slave device
  132. void interrupt_handler(void *arg) {
  133. chSysLockFromISR();
  134. sync_send();
  135. // read mid pulses
  136. serial_delay_blip();
  137. uint8_t checksum_computed = 0;
  138. int sstd_index = 0;
  139. sstd_index = serial_read_byte();
  140. sync_send();
  141. split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
  142. for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
  143. split_trans_initiator2target_buffer(trans)[i] = serial_read_byte();
  144. sync_send();
  145. checksum_computed += split_trans_initiator2target_buffer(trans)[i];
  146. }
  147. checksum_computed ^= 7;
  148. uint8_t checksum_received = serial_read_byte();
  149. sync_send();
  150. // wait for the sync to finish sending
  151. serial_delay();
  152. // Allow any slave processing to occur
  153. if (trans->slave_callback) {
  154. trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans));
  155. }
  156. uint8_t checksum = 0;
  157. for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
  158. serial_write_byte(split_trans_target2initiator_buffer(trans)[i]);
  159. sync_send();
  160. serial_delay_half();
  161. checksum += split_trans_target2initiator_buffer(trans)[i];
  162. }
  163. serial_write_byte(checksum ^ 7);
  164. sync_send();
  165. // wait for the sync to finish sending
  166. serial_delay();
  167. // end transaction
  168. serial_input();
  169. // TODO: remove extra delay between transactions
  170. serial_delay();
  171. chSysUnlockFromISR();
  172. }
  173. /////////
  174. // start transaction by initiator
  175. //
  176. // bool soft_serial_transaction(int sstd_index)
  177. //
  178. // this code is very time dependent, so we need to disable interrupts
  179. bool soft_serial_transaction(int sstd_index) {
  180. if (sstd_index > NUM_TOTAL_TRANSACTIONS) return false;
  181. split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
  182. // TODO: remove extra delay between transactions
  183. serial_delay();
  184. // this code is very time dependent, so we need to disable interrupts
  185. chSysLock();
  186. // signal to the slave that we want to start a transaction
  187. serial_output();
  188. serial_low();
  189. serial_delay_blip();
  190. // wait for the slaves response
  191. serial_input();
  192. serial_high();
  193. serial_delay();
  194. // check if the slave is present
  195. if (serial_read_pin()) {
  196. // slave failed to pull the line low, assume not present
  197. dprintf("serial::NO_RESPONSE\n");
  198. chSysUnlock();
  199. return false;
  200. }
  201. // if the slave is present syncronize with it
  202. uint8_t checksum = 0;
  203. // send data to the slave
  204. serial_write_byte(sstd_index); // first chunk is transaction id
  205. sync_recv();
  206. for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
  207. serial_write_byte(split_trans_initiator2target_buffer(trans)[i]);
  208. sync_recv();
  209. checksum += split_trans_initiator2target_buffer(trans)[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. split_trans_target2initiator_buffer(trans)[i] = serial_read_byte();
  219. sync_recv();
  220. checksum_computed += split_trans_target2initiator_buffer(trans)[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 false;
  232. }
  233. // always, release the line when not in use
  234. serial_high();
  235. serial_output();
  236. chSysUnlock();
  237. return true;
  238. }