serial.c 16 KB

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