serial.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * WARNING: be careful changing this code, it is very timing dependent
  3. *
  4. * 2018-10-28 checked
  5. * avr-gcc 4.9.2
  6. * avr-gcc 5.4.0
  7. * avr-gcc 7.3.0
  8. */
  9. #ifndef F_CPU
  10. # define F_CPU 16000000
  11. #endif
  12. #include <avr/io.h>
  13. #include <avr/interrupt.h>
  14. #include <util/delay.h>
  15. #include <stddef.h>
  16. #include <stdbool.h>
  17. #include "gpio.h"
  18. #include "serial.h"
  19. #ifdef SOFT_SERIAL_PIN
  20. # if !(defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  21. # error serial.c is not supported for the currently selected MCU
  22. # endif
  23. // if using ATmega32U4/2, AT90USBxxx I2C, can not use PD0 and PD1 in soft serial.
  24. # if defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
  25. # if defined(USE_AVR_I2C) && (SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1)
  26. # error Using I2C, so can not use PD0, PD1
  27. # endif
  28. # endif
  29. // PD0..PD3, common config
  30. # if SOFT_SERIAL_PIN == D0
  31. # define EIMSK_BIT _BV(INT0)
  32. # define EICRx_BIT (~(_BV(ISC00) | _BV(ISC01)))
  33. # define SERIAL_PIN_INTERRUPT INT0_vect
  34. # define EICRx EICRA
  35. # elif SOFT_SERIAL_PIN == D1
  36. # define EIMSK_BIT _BV(INT1)
  37. # define EICRx_BIT (~(_BV(ISC10) | _BV(ISC11)))
  38. # define SERIAL_PIN_INTERRUPT INT1_vect
  39. # define EICRx EICRA
  40. # elif SOFT_SERIAL_PIN == D2
  41. # define EIMSK_BIT _BV(INT2)
  42. # define EICRx_BIT (~(_BV(ISC20) | _BV(ISC21)))
  43. # define SERIAL_PIN_INTERRUPT INT2_vect
  44. # define EICRx EICRA
  45. # elif SOFT_SERIAL_PIN == D3
  46. # define EIMSK_BIT _BV(INT3)
  47. # define EICRx_BIT (~(_BV(ISC30) | _BV(ISC31)))
  48. # define SERIAL_PIN_INTERRUPT INT3_vect
  49. # define EICRx EICRA
  50. # endif
  51. // ATmegaxxU2/AT90USB162 specific config
  52. # if defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_AT90USB162__)
  53. // PD4(INT5), PD6(INT6), PD7(INT7), PC7(INT4)
  54. # if SOFT_SERIAL_PIN == D4
  55. # define EIMSK_BIT _BV(INT5)
  56. # define EICRx_BIT (~(_BV(ISC50) | _BV(ISC51)))
  57. # define SERIAL_PIN_INTERRUPT INT5_vect
  58. # define EICRx EICRB
  59. # elif SOFT_SERIAL_PIN == D6
  60. # define EIMSK_BIT _BV(INT6)
  61. # define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61)))
  62. # define SERIAL_PIN_INTERRUPT INT6_vect
  63. # define EICRx EICRB
  64. # elif SOFT_SERIAL_PIN == D7
  65. # define EIMSK_BIT _BV(INT7)
  66. # define EICRx_BIT (~(_BV(ISC70) | _BV(ISC71)))
  67. # define SERIAL_PIN_INTERRUPT INT7_vect
  68. # define EICRx EICRB
  69. # elif SOFT_SERIAL_PIN == C7
  70. # define EIMSK_BIT _BV(INT4)
  71. # define EICRx_BIT (~(_BV(ISC40) | _BV(ISC41)))
  72. # define SERIAL_PIN_INTERRUPT INT4_vect
  73. # define EICRx EICRB
  74. # endif
  75. # endif
  76. // ATmegaxxU4 specific config
  77. # if defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)
  78. // PE6(INT6)
  79. # if SOFT_SERIAL_PIN == E6
  80. # define EIMSK_BIT _BV(INT6)
  81. # define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61)))
  82. # define SERIAL_PIN_INTERRUPT INT6_vect
  83. # define EICRx EICRB
  84. # endif
  85. # endif
  86. // AT90USBxxx specific config
  87. # if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
  88. // PE4..PE7(INT4..INT7)
  89. # if SOFT_SERIAL_PIN == E4
  90. # define EIMSK_BIT _BV(INT4)
  91. # define EICRx_BIT (~(_BV(ISC40) | _BV(ISC41)))
  92. # define SERIAL_PIN_INTERRUPT INT4_vect
  93. # define EICRx EICRB
  94. # elif SOFT_SERIAL_PIN == E5
  95. # define EIMSK_BIT _BV(INT5)
  96. # define EICRx_BIT (~(_BV(ISC50) | _BV(ISC51)))
  97. # define SERIAL_PIN_INTERRUPT INT5_vect
  98. # define EICRx EICRB
  99. # elif SOFT_SERIAL_PIN == E6
  100. # define EIMSK_BIT _BV(INT6)
  101. # define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61)))
  102. # define SERIAL_PIN_INTERRUPT INT6_vect
  103. # define EICRx EICRB
  104. # elif SOFT_SERIAL_PIN == E7
  105. # define EIMSK_BIT _BV(INT7)
  106. # define EICRx_BIT (~(_BV(ISC70) | _BV(ISC71)))
  107. # define SERIAL_PIN_INTERRUPT INT7_vect
  108. # define EICRx EICRB
  109. # endif
  110. # endif
  111. # ifndef SERIAL_PIN_INTERRUPT
  112. # error invalid SOFT_SERIAL_PIN value
  113. # endif
  114. # define ALWAYS_INLINE __attribute__((always_inline))
  115. # define NO_INLINE __attribute__((noinline))
  116. # define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
  117. // parity check
  118. # define ODD_PARITY 1
  119. # define EVEN_PARITY 0
  120. # define PARITY EVEN_PARITY
  121. # ifdef SERIAL_DELAY
  122. // custom setup in config.h
  123. // #define TID_SEND_ADJUST 2
  124. // #define SERIAL_DELAY 6 // micro sec
  125. // #define READ_WRITE_START_ADJUST 30 // cycles
  126. // #define READ_WRITE_WIDTH_ADJUST 8 // cycles
  127. # else
  128. // ============ Standard setups ============
  129. # ifndef SELECT_SOFT_SERIAL_SPEED
  130. # define SELECT_SOFT_SERIAL_SPEED 1
  131. // 0: about 189kbps (Experimental only)
  132. // 1: about 137kbps (default)
  133. // 2: about 75kbps
  134. // 3: about 39kbps
  135. // 4: about 26kbps
  136. // 5: about 20kbps
  137. # endif
  138. # if __GNUC__ < 6
  139. # define TID_SEND_ADJUST 14
  140. # else
  141. # define TID_SEND_ADJUST 2
  142. # endif
  143. # if SELECT_SOFT_SERIAL_SPEED == 0
  144. // Very High speed
  145. # define SERIAL_DELAY 4 // micro sec
  146. # if __GNUC__ < 6
  147. # define READ_WRITE_START_ADJUST 33 // cycles
  148. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  149. # else
  150. # define READ_WRITE_START_ADJUST 34 // cycles
  151. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  152. # endif
  153. # elif SELECT_SOFT_SERIAL_SPEED == 1
  154. // High speed
  155. # define SERIAL_DELAY 6 // micro sec
  156. # if __GNUC__ < 6
  157. # define READ_WRITE_START_ADJUST 30 // cycles
  158. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  159. # else
  160. # define READ_WRITE_START_ADJUST 33 // cycles
  161. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  162. # endif
  163. # elif SELECT_SOFT_SERIAL_SPEED == 2
  164. // Middle speed
  165. # define SERIAL_DELAY 12 // micro sec
  166. # define READ_WRITE_START_ADJUST 30 // cycles
  167. # if __GNUC__ < 6
  168. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  169. # else
  170. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  171. # endif
  172. # elif SELECT_SOFT_SERIAL_SPEED == 3
  173. // Low speed
  174. # define SERIAL_DELAY 24 // micro sec
  175. # define READ_WRITE_START_ADJUST 30 // cycles
  176. # if __GNUC__ < 6
  177. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  178. # else
  179. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  180. # endif
  181. # elif SELECT_SOFT_SERIAL_SPEED == 4
  182. // Very Low speed
  183. # define SERIAL_DELAY 36 // micro sec
  184. # define READ_WRITE_START_ADJUST 30 // cycles
  185. # if __GNUC__ < 6
  186. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  187. # else
  188. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  189. # endif
  190. # elif SELECT_SOFT_SERIAL_SPEED == 5
  191. // Ultra Low speed
  192. # define SERIAL_DELAY 48 // micro sec
  193. # define READ_WRITE_START_ADJUST 30 // cycles
  194. # if __GNUC__ < 6
  195. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  196. # else
  197. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  198. # endif
  199. # else
  200. # error invalid SELECT_SOFT_SERIAL_SPEED value
  201. # endif /* SELECT_SOFT_SERIAL_SPEED */
  202. # endif /* SERIAL_DELAY */
  203. # define SERIAL_DELAY_HALF1 (SERIAL_DELAY / 2)
  204. # define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY / 2)
  205. # define SLAVE_INT_WIDTH_US 1
  206. # define SLAVE_INT_ACK_WIDTH_UNIT 2
  207. # define SLAVE_INT_ACK_WIDTH 4
  208. inline static void serial_delay(void) ALWAYS_INLINE;
  209. inline static void serial_delay(void) {
  210. _delay_us(SERIAL_DELAY);
  211. }
  212. inline static void serial_delay_half1(void) ALWAYS_INLINE;
  213. inline static void serial_delay_half1(void) {
  214. _delay_us(SERIAL_DELAY_HALF1);
  215. }
  216. inline static void serial_delay_half2(void) ALWAYS_INLINE;
  217. inline static void serial_delay_half2(void) {
  218. _delay_us(SERIAL_DELAY_HALF2);
  219. }
  220. inline static void serial_output(void) ALWAYS_INLINE;
  221. inline static void serial_output(void) {
  222. setPinOutput(SOFT_SERIAL_PIN);
  223. }
  224. // make the serial pin an input with pull-up resistor
  225. inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
  226. inline static void serial_input_with_pullup(void) {
  227. setPinInputHigh(SOFT_SERIAL_PIN);
  228. }
  229. inline static uint8_t serial_read_pin(void) ALWAYS_INLINE;
  230. inline static uint8_t serial_read_pin(void) {
  231. return !!readPin(SOFT_SERIAL_PIN);
  232. }
  233. inline static void serial_low(void) ALWAYS_INLINE;
  234. inline static void serial_low(void) {
  235. writePinLow(SOFT_SERIAL_PIN);
  236. }
  237. inline static void serial_high(void) ALWAYS_INLINE;
  238. inline static void serial_high(void) {
  239. writePinHigh(SOFT_SERIAL_PIN);
  240. }
  241. void soft_serial_initiator_init(void) {
  242. serial_output();
  243. serial_high();
  244. }
  245. void soft_serial_target_init(void) {
  246. serial_input_with_pullup();
  247. // Enable INT0-INT7
  248. EIMSK |= EIMSK_BIT;
  249. EICRx &= EICRx_BIT;
  250. }
  251. // Used by the sender to synchronize timing with the reciver.
  252. static void sync_recv(void) NO_INLINE;
  253. static void sync_recv(void) {
  254. for (uint8_t i = 0; i < SERIAL_DELAY * 5 && serial_read_pin(); i++) {
  255. }
  256. // This shouldn't hang if the target disconnects because the
  257. // serial line will float to high if the target does disconnect.
  258. while (!serial_read_pin())
  259. ;
  260. }
  261. // Used by the reciver to send a synchronization signal to the sender.
  262. static void sync_send(void) NO_INLINE;
  263. static void sync_send(void) {
  264. serial_low();
  265. serial_delay();
  266. serial_high();
  267. }
  268. // Reads a byte from the serial line
  269. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
  270. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
  271. uint8_t byte, i, p, pb;
  272. _delay_sub_us(READ_WRITE_START_ADJUST);
  273. for (i = 0, byte = 0, p = PARITY; i < bit; i++) {
  274. serial_delay_half1(); // read the middle of pulses
  275. if (serial_read_pin()) {
  276. byte = (byte << 1) | 1;
  277. p ^= 1;
  278. } else {
  279. byte = (byte << 1) | 0;
  280. p ^= 0;
  281. }
  282. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  283. serial_delay_half2();
  284. }
  285. /* recive parity bit */
  286. serial_delay_half1(); // read the middle of pulses
  287. pb = serial_read_pin();
  288. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  289. serial_delay_half2();
  290. *pterrcount += (p != pb) ? 1 : 0;
  291. return byte;
  292. }
  293. // Sends a byte with MSB ordering
  294. void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
  295. void serial_write_chunk(uint8_t data, uint8_t bit) {
  296. uint8_t b, p;
  297. for (p = PARITY, b = 1 << (bit - 1); b; b >>= 1) {
  298. if (data & b) {
  299. serial_high();
  300. p ^= 1;
  301. } else {
  302. serial_low();
  303. p ^= 0;
  304. }
  305. serial_delay();
  306. }
  307. /* send parity bit */
  308. if (p & 1) {
  309. serial_high();
  310. } else {
  311. serial_low();
  312. }
  313. serial_delay();
  314. serial_low(); // sync_send() / senc_recv() need raise edge
  315. }
  316. static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  317. static void serial_send_packet(uint8_t *buffer, uint8_t size) {
  318. for (uint8_t i = 0; i < size; ++i) {
  319. uint8_t data;
  320. data = buffer[i];
  321. sync_send();
  322. serial_write_chunk(data, 8);
  323. }
  324. }
  325. static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  326. static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
  327. uint8_t pecount = 0;
  328. for (uint8_t i = 0; i < size; ++i) {
  329. uint8_t data;
  330. sync_recv();
  331. data = serial_read_chunk(&pecount, 8);
  332. buffer[i] = data;
  333. }
  334. return pecount == 0;
  335. }
  336. inline static void change_sender2reciver(void) {
  337. sync_send(); // 0
  338. serial_delay_half1(); // 1
  339. serial_low(); // 2
  340. serial_input_with_pullup(); // 2
  341. serial_delay_half1(); // 3
  342. }
  343. inline static void change_reciver2sender(void) {
  344. sync_recv(); // 0
  345. serial_delay(); // 1
  346. serial_low(); // 3
  347. serial_output(); // 3
  348. serial_delay_half1(); // 4
  349. }
  350. static inline uint8_t nibble_bits_count(uint8_t bits) {
  351. bits = (bits & 0x5) + (bits >> 1 & 0x5);
  352. bits = (bits & 0x3) + (bits >> 2 & 0x3);
  353. return bits;
  354. }
  355. // interrupt handle to be used by the target device
  356. ISR(SERIAL_PIN_INTERRUPT) {
  357. // recive transaction table index
  358. uint8_t tid, bits;
  359. uint8_t pecount = 0;
  360. sync_recv();
  361. bits = serial_read_chunk(&pecount, 8);
  362. tid = bits >> 3;
  363. bits = (bits & 7) != (nibble_bits_count(tid) & 7);
  364. if (bits || pecount > 0 || tid > NUM_TOTAL_TRANSACTIONS) {
  365. return;
  366. }
  367. serial_delay_half1();
  368. serial_high(); // response step1 low->high
  369. serial_output();
  370. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT * SLAVE_INT_ACK_WIDTH);
  371. split_transaction_desc_t *trans = &split_transaction_table[tid];
  372. serial_low(); // response step2 ack high->low
  373. // If the transaction has a callback, we can execute it now
  374. if (trans->slave_callback) {
  375. trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans));
  376. }
  377. // target send phase
  378. if (trans->target2initiator_buffer_size > 0) serial_send_packet((uint8_t *)split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size);
  379. // target switch to input
  380. change_sender2reciver();
  381. // target recive phase
  382. if (trans->initiator2target_buffer_size > 0) {
  383. serial_recive_packet((uint8_t *)split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size);
  384. }
  385. sync_recv(); // weit initiator output to high
  386. }
  387. /////////
  388. // start transaction by initiator
  389. //
  390. // bool soft_serial_transaction(int sstd_index)
  391. //
  392. // this code is very time dependent, so we need to disable interrupts
  393. bool soft_serial_transaction(int sstd_index) {
  394. if (sstd_index > NUM_TOTAL_TRANSACTIONS) return false;
  395. split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
  396. cli();
  397. // signal to the target that we want to start a transaction
  398. serial_output();
  399. serial_low();
  400. _delay_us(SLAVE_INT_WIDTH_US);
  401. // send transaction table index
  402. int tid = (sstd_index << 3) | (7 & nibble_bits_count(sstd_index));
  403. sync_send();
  404. _delay_sub_us(TID_SEND_ADJUST);
  405. serial_write_chunk(tid, 8);
  406. serial_delay_half1();
  407. // wait for the target response (step1 low->high)
  408. serial_input_with_pullup();
  409. while (!serial_read_pin()) {
  410. _delay_sub_us(2);
  411. }
  412. // check if the target is present (step2 high->low)
  413. for (int i = 0; serial_read_pin(); i++) {
  414. if (i > SLAVE_INT_ACK_WIDTH + 1) {
  415. // slave failed to pull the line low, assume not present
  416. serial_output();
  417. serial_high();
  418. sei();
  419. return false;
  420. }
  421. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
  422. }
  423. // initiator recive phase
  424. // if the target is present syncronize with it
  425. if (trans->target2initiator_buffer_size > 0) {
  426. if (!serial_recive_packet((uint8_t *)split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) {
  427. serial_output();
  428. serial_high();
  429. sei();
  430. return false;
  431. }
  432. }
  433. // initiator switch to output
  434. change_reciver2sender();
  435. // initiator send phase
  436. if (trans->initiator2target_buffer_size > 0) {
  437. serial_send_packet((uint8_t *)split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size);
  438. }
  439. // always, release the line when not in use
  440. sync_send();
  441. sei();
  442. return true;
  443. }
  444. #else
  445. # ifndef USE_I2C
  446. # error SOFT_SERIAL_PIN or USE_I2C is required but has not been defined.
  447. # endif
  448. #endif
  449. // Helix serial.c history
  450. // 2018-1-29 fork from let's split and add PD2, modify sync_recv() (#2308, bceffdefc)
  451. // 2018-6-28 bug fix master to slave comm and speed up (#3255, 1038bbef4)
  452. // (adjusted with avr-gcc 4.9.2)
  453. // 2018-7-13 remove USE_SERIAL_PD2 macro (#3374, f30d6dd78)
  454. // (adjusted with avr-gcc 4.9.2)
  455. // 2018-8-11 add support multi-type transaction (#3608, feb5e4aae)
  456. // (adjusted with avr-gcc 4.9.2)
  457. // 2018-10-21 fix serial and RGB animation conflict (#4191, 4665e4fff)
  458. // (adjusted with avr-gcc 7.3.0)
  459. // 2018-10-28 re-adjust compiler depend value of delay (#4269, 8517f8a66)
  460. // (adjusted with avr-gcc 5.4.0, 7.3.0)
  461. // 2018-12-17 copy to TOP/quantum/split_common/ and remove backward compatibility code (#4669)