serial.c 15 KB

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