serial.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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
  168. void serial_delay(void) {
  169. _delay_us(SERIAL_DELAY);
  170. }
  171. inline static void serial_delay_half1(void) ALWAYS_INLINE;
  172. inline static
  173. void serial_delay_half1(void) {
  174. _delay_us(SERIAL_DELAY_HALF1);
  175. }
  176. inline static void serial_delay_half2(void) ALWAYS_INLINE;
  177. inline static
  178. void serial_delay_half2(void) {
  179. _delay_us(SERIAL_DELAY_HALF2);
  180. }
  181. inline static void serial_output(void) ALWAYS_INLINE;
  182. inline static
  183. void serial_output(void) {
  184. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  185. }
  186. // make the serial pin an input with pull-up resistor
  187. inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
  188. inline static
  189. void serial_input_with_pullup(void) {
  190. SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
  191. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  192. }
  193. inline static uint8_t serial_read_pin(void) ALWAYS_INLINE;
  194. inline static
  195. uint8_t serial_read_pin(void) {
  196. return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
  197. }
  198. inline static void serial_low(void) ALWAYS_INLINE;
  199. inline static
  200. void serial_low(void) {
  201. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  202. }
  203. inline static void serial_high(void) ALWAYS_INLINE;
  204. inline static
  205. void serial_high(void) {
  206. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  207. }
  208. void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size)
  209. {
  210. Transaction_table = sstd_table;
  211. Transaction_table_size = (uint8_t)sstd_table_size;
  212. serial_output();
  213. serial_high();
  214. }
  215. void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size)
  216. {
  217. Transaction_table = sstd_table;
  218. Transaction_table_size = (uint8_t)sstd_table_size;
  219. serial_input_with_pullup();
  220. // Enable INT0-INT3,INT6
  221. EIMSK |= EIMSK_BIT;
  222. #if SERIAL_PIN_MASK == _BV(PE6)
  223. // Trigger on falling edge of INT6
  224. EICRB &= EICRx_BIT;
  225. #else
  226. // Trigger on falling edge of INT0-INT3
  227. EICRA &= EICRx_BIT;
  228. #endif
  229. }
  230. // Used by the sender to synchronize timing with the reciver.
  231. static void sync_recv(void) NO_INLINE;
  232. static
  233. void sync_recv(void) {
  234. for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
  235. }
  236. // This shouldn't hang if the target disconnects because the
  237. // serial line will float to high if the target does disconnect.
  238. while (!serial_read_pin());
  239. }
  240. // Used by the reciver to send a synchronization signal to the sender.
  241. static void sync_send(void) NO_INLINE;
  242. static
  243. void sync_send(void) {
  244. serial_low();
  245. serial_delay();
  246. serial_high();
  247. }
  248. // Reads a byte from the serial line
  249. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
  250. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
  251. uint8_t byte, i, p, pb;
  252. _delay_sub_us(READ_WRITE_START_ADJUST);
  253. for( i = 0, byte = 0, p = PARITY; i < bit; i++ ) {
  254. serial_delay_half1(); // read the middle of pulses
  255. if( serial_read_pin() ) {
  256. byte = (byte << 1) | 1; p ^= 1;
  257. } else {
  258. byte = (byte << 1) | 0; p ^= 0;
  259. }
  260. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  261. serial_delay_half2();
  262. }
  263. /* recive parity bit */
  264. serial_delay_half1(); // read the middle of pulses
  265. pb = serial_read_pin();
  266. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  267. serial_delay_half2();
  268. *pterrcount += (p != pb)? 1 : 0;
  269. return byte;
  270. }
  271. // Sends a byte with MSB ordering
  272. void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
  273. void serial_write_chunk(uint8_t data, uint8_t bit) {
  274. uint8_t b, p;
  275. for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) {
  276. if(data & b) {
  277. serial_high(); p ^= 1;
  278. } else {
  279. serial_low(); p ^= 0;
  280. }
  281. serial_delay();
  282. }
  283. /* send parity bit */
  284. if(p & 1) { serial_high(); }
  285. else { serial_low(); }
  286. serial_delay();
  287. serial_low(); // sync_send() / senc_recv() need raise edge
  288. }
  289. static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  290. static
  291. void serial_send_packet(uint8_t *buffer, uint8_t size) {
  292. for (uint8_t i = 0; i < size; ++i) {
  293. uint8_t data;
  294. data = buffer[i];
  295. sync_send();
  296. serial_write_chunk(data,8);
  297. }
  298. }
  299. static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  300. static
  301. uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
  302. uint8_t pecount = 0;
  303. for (uint8_t i = 0; i < size; ++i) {
  304. uint8_t data;
  305. sync_recv();
  306. data = serial_read_chunk(&pecount, 8);
  307. buffer[i] = data;
  308. }
  309. return pecount == 0;
  310. }
  311. inline static
  312. void change_sender2reciver(void) {
  313. sync_send(); //0
  314. serial_delay_half1(); //1
  315. serial_low(); //2
  316. serial_input_with_pullup(); //2
  317. serial_delay_half1(); //3
  318. }
  319. inline static
  320. void change_reciver2sender(void) {
  321. sync_recv(); //0
  322. serial_delay(); //1
  323. serial_low(); //3
  324. serial_output(); //3
  325. serial_delay_half1(); //4
  326. }
  327. static inline uint8_t nibble_bits_count(uint8_t bits)
  328. {
  329. bits = (bits & 0x5) + (bits >> 1 & 0x5);
  330. bits = (bits & 0x3) + (bits >> 2 & 0x3);
  331. return bits;
  332. }
  333. // interrupt handle to be used by the target device
  334. ISR(SERIAL_PIN_INTERRUPT) {
  335. #ifndef SERIAL_USE_MULTI_TRANSACTION
  336. serial_low();
  337. serial_output();
  338. SSTD_t *trans = Transaction_table;
  339. #else
  340. // recive transaction table index
  341. uint8_t tid, bits;
  342. uint8_t pecount = 0;
  343. sync_recv();
  344. bits = serial_read_chunk(&pecount,7);
  345. tid = bits>>3;
  346. bits = (bits&7) != nibble_bits_count(tid);
  347. if( bits || pecount> 0 || tid > Transaction_table_size ) {
  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. SSTD_t *trans = &Transaction_table[tid];
  355. serial_low(); // response step2 ack high->low
  356. #endif
  357. // target send phase
  358. if( trans->target2initiator_buffer_size > 0 )
  359. serial_send_packet((uint8_t *)trans->target2initiator_buffer,
  360. trans->target2initiator_buffer_size);
  361. // target switch to input
  362. change_sender2reciver();
  363. // target recive phase
  364. if( trans->initiator2target_buffer_size > 0 ) {
  365. if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
  366. 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. #ifndef SERIAL_USE_MULTI_TRANSACTION
  387. int soft_serial_transaction(void) {
  388. SSTD_t *trans = Transaction_table;
  389. #else
  390. int soft_serial_transaction(int sstd_index) {
  391. if( sstd_index > Transaction_table_size )
  392. return TRANSACTION_TYPE_ERROR;
  393. SSTD_t *trans = &Transaction_table[sstd_index];
  394. #endif
  395. cli();
  396. // signal to the target that we want to start a transaction
  397. serial_output();
  398. serial_low();
  399. _delay_us(SLAVE_INT_WIDTH_US);
  400. #ifndef SERIAL_USE_MULTI_TRANSACTION
  401. // wait for the target response
  402. serial_input_with_pullup();
  403. _delay_us(SLAVE_INT_RESPONSE_TIME);
  404. // check if the target is present
  405. if (serial_read_pin()) {
  406. // target failed to pull the line low, assume not present
  407. serial_output();
  408. serial_high();
  409. *trans->status = TRANSACTION_NO_RESPONSE;
  410. sei();
  411. return TRANSACTION_NO_RESPONSE;
  412. }
  413. #else
  414. // send transaction table index
  415. int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index));
  416. sync_send();
  417. _delay_sub_us(TID_SEND_ADJUST);
  418. serial_write_chunk(tid, 7);
  419. serial_delay_half1();
  420. // wait for the target response (step1 low->high)
  421. serial_input_with_pullup();
  422. while( !serial_read_pin() ) {
  423. _delay_sub_us(2);
  424. }
  425. // check if the target is present (step2 high->low)
  426. for( int i = 0; serial_read_pin(); i++ ) {
  427. if (i > SLAVE_INT_ACK_WIDTH + 1) {
  428. // slave failed to pull the line low, assume not present
  429. serial_output();
  430. serial_high();
  431. *trans->status = TRANSACTION_NO_RESPONSE;
  432. sei();
  433. return TRANSACTION_NO_RESPONSE;
  434. }
  435. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
  436. }
  437. #endif
  438. // initiator recive phase
  439. // if the target is present syncronize with it
  440. if( trans->target2initiator_buffer_size > 0 ) {
  441. if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
  442. trans->target2initiator_buffer_size) ) {
  443. serial_output();
  444. serial_high();
  445. *trans->status = TRANSACTION_DATA_ERROR;
  446. sei();
  447. return TRANSACTION_DATA_ERROR;
  448. }
  449. }
  450. // initiator switch to output
  451. change_reciver2sender();
  452. // initiator send phase
  453. if( trans->initiator2target_buffer_size > 0 ) {
  454. serial_send_packet((uint8_t *)trans->initiator2target_buffer,
  455. trans->initiator2target_buffer_size);
  456. }
  457. // always, release the line when not in use
  458. sync_send();
  459. *trans->status = TRANSACTION_END;
  460. sei();
  461. return TRANSACTION_END;
  462. }
  463. #ifdef SERIAL_USE_MULTI_TRANSACTION
  464. int soft_serial_get_and_clean_status(int sstd_index) {
  465. SSTD_t *trans = &Transaction_table[sstd_index];
  466. cli();
  467. int retval = *trans->status;
  468. *trans->status = 0;;
  469. sei();
  470. return retval;
  471. }
  472. #endif
  473. #endif
  474. // Helix serial.c history
  475. // 2018-1-29 fork from let's split and add PD2, modify sync_recv() (#2308, bceffdefc)
  476. // 2018-6-28 bug fix master to slave comm and speed up (#3255, 1038bbef4)
  477. // (adjusted with avr-gcc 4.9.2)
  478. // 2018-7-13 remove USE_SERIAL_PD2 macro (#3374, f30d6dd78)
  479. // (adjusted with avr-gcc 4.9.2)
  480. // 2018-8-11 add support multi-type transaction (#3608, feb5e4aae)
  481. // (adjusted with avr-gcc 4.9.2)
  482. // 2018-10-21 fix serial and RGB animation conflict (#4191, 4665e4fff)
  483. // (adjusted with avr-gcc 7.3.0)
  484. // 2018-10-28 re-adjust compiler depend value of delay (#4269, 8517f8a66)
  485. // (adjusted with avr-gcc 5.4.0, 7.3.0)
  486. // 2018-12-17 copy to TOP/quantum/split_common/ and remove backward compatibility code (#4669)