serial.c 18 KB

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