serial.c 16 KB

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