serial.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  114. # define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
  115. # define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  116. # define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  117. # define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
  118. # define ALWAYS_INLINE __attribute__((always_inline))
  119. # define NO_INLINE __attribute__((noinline))
  120. # define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
  121. // parity check
  122. # define ODD_PARITY 1
  123. # define EVEN_PARITY 0
  124. # define PARITY EVEN_PARITY
  125. # ifdef SERIAL_DELAY
  126. // custom setup in config.h
  127. // #define TID_SEND_ADJUST 2
  128. // #define SERIAL_DELAY 6 // micro sec
  129. // #define READ_WRITE_START_ADJUST 30 // cycles
  130. // #define READ_WRITE_WIDTH_ADJUST 8 // cycles
  131. # else
  132. // ============ Standard setups ============
  133. # ifndef SELECT_SOFT_SERIAL_SPEED
  134. # define SELECT_SOFT_SERIAL_SPEED 1
  135. // 0: about 189kbps (Experimental only)
  136. // 1: about 137kbps (default)
  137. // 2: about 75kbps
  138. // 3: about 39kbps
  139. // 4: about 26kbps
  140. // 5: about 20kbps
  141. # endif
  142. # if __GNUC__ < 6
  143. # define TID_SEND_ADJUST 14
  144. # else
  145. # define TID_SEND_ADJUST 2
  146. # endif
  147. # if SELECT_SOFT_SERIAL_SPEED == 0
  148. // Very High speed
  149. # define SERIAL_DELAY 4 // micro sec
  150. # if __GNUC__ < 6
  151. # define READ_WRITE_START_ADJUST 33 // cycles
  152. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  153. # else
  154. # define READ_WRITE_START_ADJUST 34 // cycles
  155. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  156. # endif
  157. # elif SELECT_SOFT_SERIAL_SPEED == 1
  158. // High speed
  159. # define SERIAL_DELAY 6 // micro sec
  160. # if __GNUC__ < 6
  161. # define READ_WRITE_START_ADJUST 30 // cycles
  162. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  163. # else
  164. # define READ_WRITE_START_ADJUST 33 // cycles
  165. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  166. # endif
  167. # elif SELECT_SOFT_SERIAL_SPEED == 2
  168. // Middle speed
  169. # define SERIAL_DELAY 12 // micro sec
  170. # define READ_WRITE_START_ADJUST 30 // cycles
  171. # if __GNUC__ < 6
  172. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  173. # else
  174. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  175. # endif
  176. # elif SELECT_SOFT_SERIAL_SPEED == 3
  177. // Low speed
  178. # define SERIAL_DELAY 24 // micro sec
  179. # define READ_WRITE_START_ADJUST 30 // cycles
  180. # if __GNUC__ < 6
  181. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  182. # else
  183. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  184. # endif
  185. # elif SELECT_SOFT_SERIAL_SPEED == 4
  186. // Very Low speed
  187. # define SERIAL_DELAY 36 // micro sec
  188. # define READ_WRITE_START_ADJUST 30 // cycles
  189. # if __GNUC__ < 6
  190. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  191. # else
  192. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  193. # endif
  194. # elif SELECT_SOFT_SERIAL_SPEED == 5
  195. // Ultra Low speed
  196. # define SERIAL_DELAY 48 // micro sec
  197. # define READ_WRITE_START_ADJUST 30 // cycles
  198. # if __GNUC__ < 6
  199. # define READ_WRITE_WIDTH_ADJUST 3 // cycles
  200. # else
  201. # define READ_WRITE_WIDTH_ADJUST 7 // cycles
  202. # endif
  203. # else
  204. # error invalid SELECT_SOFT_SERIAL_SPEED value
  205. # endif /* SELECT_SOFT_SERIAL_SPEED */
  206. # endif /* SERIAL_DELAY */
  207. # define SERIAL_DELAY_HALF1 (SERIAL_DELAY / 2)
  208. # define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY / 2)
  209. # define SLAVE_INT_WIDTH_US 1
  210. # ifndef SERIAL_USE_MULTI_TRANSACTION
  211. # define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
  212. # else
  213. # define SLAVE_INT_ACK_WIDTH_UNIT 2
  214. # define SLAVE_INT_ACK_WIDTH 4
  215. # endif
  216. static SSTD_t *Transaction_table = NULL;
  217. static uint8_t Transaction_table_size = 0;
  218. inline static void serial_delay(void) ALWAYS_INLINE;
  219. inline static void serial_delay(void) { _delay_us(SERIAL_DELAY); }
  220. inline static void serial_delay_half1(void) ALWAYS_INLINE;
  221. inline static void serial_delay_half1(void) { _delay_us(SERIAL_DELAY_HALF1); }
  222. inline static void serial_delay_half2(void) ALWAYS_INLINE;
  223. inline static void serial_delay_half2(void) { _delay_us(SERIAL_DELAY_HALF2); }
  224. inline static void serial_output(void) ALWAYS_INLINE;
  225. inline static void serial_output(void) { setPinOutput(SOFT_SERIAL_PIN); }
  226. // make the serial pin an input with pull-up resistor
  227. inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
  228. inline static void serial_input_with_pullup(void) { setPinInputHigh(SOFT_SERIAL_PIN); }
  229. inline static uint8_t serial_read_pin(void) ALWAYS_INLINE;
  230. inline static uint8_t serial_read_pin(void) { return !!readPin(SOFT_SERIAL_PIN); }
  231. inline static void serial_low(void) ALWAYS_INLINE;
  232. inline static void serial_low(void) { writePinLow(SOFT_SERIAL_PIN); }
  233. inline static void serial_high(void) ALWAYS_INLINE;
  234. inline static void serial_high(void) { writePinHigh(SOFT_SERIAL_PIN); }
  235. void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size) {
  236. Transaction_table = sstd_table;
  237. Transaction_table_size = (uint8_t)sstd_table_size;
  238. serial_output();
  239. serial_high();
  240. }
  241. void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size) {
  242. Transaction_table = sstd_table;
  243. Transaction_table_size = (uint8_t)sstd_table_size;
  244. serial_input_with_pullup();
  245. // Enable INT0-INT7
  246. EIMSK |= EIMSK_BIT;
  247. EICRx &= EICRx_BIT;
  248. }
  249. // Used by the sender to synchronize timing with the reciver.
  250. static void sync_recv(void) NO_INLINE;
  251. static void sync_recv(void) {
  252. for (uint8_t i = 0; i < SERIAL_DELAY * 5 && serial_read_pin(); i++) {
  253. }
  254. // This shouldn't hang if the target disconnects because the
  255. // serial line will float to high if the target does disconnect.
  256. while (!serial_read_pin())
  257. ;
  258. }
  259. // Used by the reciver to send a synchronization signal to the sender.
  260. static void sync_send(void) NO_INLINE;
  261. static void sync_send(void) {
  262. serial_low();
  263. serial_delay();
  264. serial_high();
  265. }
  266. // Reads a byte from the serial line
  267. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
  268. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
  269. uint8_t byte, i, p, pb;
  270. _delay_sub_us(READ_WRITE_START_ADJUST);
  271. for (i = 0, byte = 0, p = PARITY; i < bit; i++) {
  272. serial_delay_half1(); // read the middle of pulses
  273. if (serial_read_pin()) {
  274. byte = (byte << 1) | 1;
  275. p ^= 1;
  276. } else {
  277. byte = (byte << 1) | 0;
  278. p ^= 0;
  279. }
  280. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  281. serial_delay_half2();
  282. }
  283. /* recive parity bit */
  284. serial_delay_half1(); // read the middle of pulses
  285. pb = serial_read_pin();
  286. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  287. serial_delay_half2();
  288. *pterrcount += (p != pb) ? 1 : 0;
  289. return byte;
  290. }
  291. // Sends a byte with MSB ordering
  292. void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
  293. void serial_write_chunk(uint8_t data, uint8_t bit) {
  294. uint8_t b, p;
  295. for (p = PARITY, b = 1 << (bit - 1); b; b >>= 1) {
  296. if (data & b) {
  297. serial_high();
  298. p ^= 1;
  299. } else {
  300. serial_low();
  301. p ^= 0;
  302. }
  303. serial_delay();
  304. }
  305. /* send parity bit */
  306. if (p & 1) {
  307. serial_high();
  308. } else {
  309. serial_low();
  310. }
  311. serial_delay();
  312. serial_low(); // sync_send() / senc_recv() need raise edge
  313. }
  314. static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  315. static void serial_send_packet(uint8_t *buffer, uint8_t size) {
  316. for (uint8_t i = 0; i < size; ++i) {
  317. uint8_t data;
  318. data = buffer[i];
  319. sync_send();
  320. serial_write_chunk(data, 8);
  321. }
  322. }
  323. static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  324. static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
  325. uint8_t pecount = 0;
  326. for (uint8_t i = 0; i < size; ++i) {
  327. uint8_t data;
  328. sync_recv();
  329. data = serial_read_chunk(&pecount, 8);
  330. buffer[i] = data;
  331. }
  332. return pecount == 0;
  333. }
  334. inline static void change_sender2reciver(void) {
  335. sync_send(); // 0
  336. serial_delay_half1(); // 1
  337. serial_low(); // 2
  338. serial_input_with_pullup(); // 2
  339. serial_delay_half1(); // 3
  340. }
  341. inline static void change_reciver2sender(void) {
  342. sync_recv(); // 0
  343. serial_delay(); // 1
  344. serial_low(); // 3
  345. serial_output(); // 3
  346. serial_delay_half1(); // 4
  347. }
  348. static inline uint8_t nibble_bits_count(uint8_t bits) {
  349. bits = (bits & 0x5) + (bits >> 1 & 0x5);
  350. bits = (bits & 0x3) + (bits >> 2 & 0x3);
  351. return bits;
  352. }
  353. // interrupt handle to be used by the target device
  354. ISR(SERIAL_PIN_INTERRUPT) {
  355. # ifndef SERIAL_USE_MULTI_TRANSACTION
  356. serial_low();
  357. serial_output();
  358. SSTD_t *trans = Transaction_table;
  359. # else
  360. // recive transaction table index
  361. uint8_t tid, bits;
  362. uint8_t pecount = 0;
  363. sync_recv();
  364. bits = serial_read_chunk(&pecount, 7);
  365. tid = bits >> 3;
  366. bits = (bits & 7) != nibble_bits_count(tid);
  367. if (bits || pecount > 0 || tid > Transaction_table_size) {
  368. return;
  369. }
  370. serial_delay_half1();
  371. serial_high(); // response step1 low->high
  372. serial_output();
  373. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT * SLAVE_INT_ACK_WIDTH);
  374. SSTD_t *trans = &Transaction_table[tid];
  375. serial_low(); // response step2 ack high->low
  376. # endif
  377. // target send phase
  378. if (trans->target2initiator_buffer_size > 0) serial_send_packet((uint8_t *)trans->target2initiator_buffer, trans->target2initiator_buffer_size);
  379. // target switch to input
  380. change_sender2reciver();
  381. // target recive phase
  382. if (trans->initiator2target_buffer_size > 0) {
  383. if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer, trans->initiator2target_buffer_size)) {
  384. *trans->status = TRANSACTION_ACCEPTED;
  385. } else {
  386. *trans->status = TRANSACTION_DATA_ERROR;
  387. }
  388. } else {
  389. *trans->status = TRANSACTION_ACCEPTED;
  390. }
  391. sync_recv(); // weit initiator output to high
  392. }
  393. /////////
  394. // start transaction by initiator
  395. //
  396. // int soft_serial_transaction(int sstd_index)
  397. //
  398. // Returns:
  399. // TRANSACTION_END
  400. // TRANSACTION_NO_RESPONSE
  401. // TRANSACTION_DATA_ERROR
  402. // this code is very time dependent, so we need to disable interrupts
  403. # ifndef SERIAL_USE_MULTI_TRANSACTION
  404. int soft_serial_transaction(void) {
  405. SSTD_t *trans = Transaction_table;
  406. # else
  407. int soft_serial_transaction(int sstd_index) {
  408. if (sstd_index > Transaction_table_size) return TRANSACTION_TYPE_ERROR;
  409. SSTD_t *trans = &Transaction_table[sstd_index];
  410. # endif
  411. cli();
  412. // signal to the target that we want to start a transaction
  413. serial_output();
  414. serial_low();
  415. _delay_us(SLAVE_INT_WIDTH_US);
  416. # ifndef SERIAL_USE_MULTI_TRANSACTION
  417. // wait for the target response
  418. serial_input_with_pullup();
  419. _delay_us(SLAVE_INT_RESPONSE_TIME);
  420. // check if the target is present
  421. if (serial_read_pin()) {
  422. // target failed to pull the line low, assume not present
  423. serial_output();
  424. serial_high();
  425. *trans->status = TRANSACTION_NO_RESPONSE;
  426. sei();
  427. return TRANSACTION_NO_RESPONSE;
  428. }
  429. # else
  430. // send transaction table index
  431. int tid = (sstd_index << 3) | (7 & nibble_bits_count(sstd_index));
  432. sync_send();
  433. _delay_sub_us(TID_SEND_ADJUST);
  434. serial_write_chunk(tid, 7);
  435. serial_delay_half1();
  436. // wait for the target response (step1 low->high)
  437. serial_input_with_pullup();
  438. while (!serial_read_pin()) {
  439. _delay_sub_us(2);
  440. }
  441. // check if the target is present (step2 high->low)
  442. for (int i = 0; serial_read_pin(); i++) {
  443. if (i > SLAVE_INT_ACK_WIDTH + 1) {
  444. // slave failed to pull the line low, assume not present
  445. serial_output();
  446. serial_high();
  447. *trans->status = TRANSACTION_NO_RESPONSE;
  448. sei();
  449. return TRANSACTION_NO_RESPONSE;
  450. }
  451. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
  452. }
  453. # endif
  454. // initiator recive phase
  455. // if the target is present syncronize with it
  456. if (trans->target2initiator_buffer_size > 0) {
  457. if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer, trans->target2initiator_buffer_size)) {
  458. serial_output();
  459. serial_high();
  460. *trans->status = TRANSACTION_DATA_ERROR;
  461. sei();
  462. return TRANSACTION_DATA_ERROR;
  463. }
  464. }
  465. // initiator switch to output
  466. change_reciver2sender();
  467. // initiator send phase
  468. if (trans->initiator2target_buffer_size > 0) {
  469. serial_send_packet((uint8_t *)trans->initiator2target_buffer, trans->initiator2target_buffer_size);
  470. }
  471. // always, release the line when not in use
  472. sync_send();
  473. *trans->status = TRANSACTION_END;
  474. sei();
  475. return TRANSACTION_END;
  476. }
  477. # ifdef SERIAL_USE_MULTI_TRANSACTION
  478. int soft_serial_get_and_clean_status(int sstd_index) {
  479. SSTD_t *trans = &Transaction_table[sstd_index];
  480. cli();
  481. int retval = *trans->status;
  482. *trans->status = 0;
  483. ;
  484. sei();
  485. return retval;
  486. }
  487. # endif
  488. #endif
  489. // Helix serial.c history
  490. // 2018-1-29 fork from let's split and add PD2, modify sync_recv() (#2308, bceffdefc)
  491. // 2018-6-28 bug fix master to slave comm and speed up (#3255, 1038bbef4)
  492. // (adjusted with avr-gcc 4.9.2)
  493. // 2018-7-13 remove USE_SERIAL_PD2 macro (#3374, f30d6dd78)
  494. // (adjusted with avr-gcc 4.9.2)
  495. // 2018-8-11 add support multi-type transaction (#3608, feb5e4aae)
  496. // (adjusted with avr-gcc 4.9.2)
  497. // 2018-10-21 fix serial and RGB animation conflict (#4191, 4665e4fff)
  498. // (adjusted with avr-gcc 7.3.0)
  499. // 2018-10-28 re-adjust compiler depend value of delay (#4269, 8517f8a66)
  500. // (adjusted with avr-gcc 5.4.0, 7.3.0)
  501. // 2018-12-17 copy to TOP/quantum/split_common/ and remove backward compatibility code (#4669)