serial.c 16 KB

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