2 * WARNING: be careful changing this code, it is very timing dependent
10 #include <avr/interrupt.h>
11 #include <util/delay.h>
15 //#include <pro_micro.h>
17 #ifdef SOFT_SERIAL_PIN
19 #ifdef __AVR_ATmega32U4__
20 // if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial.
22 #if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1
23 #error Using ATmega32U4 I2C, so can not use PD0, PD1
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
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
61 #error invalid SOFT_SERIAL_PIN value
65 #error serial.c now support ATmega32U4 only
68 #ifndef SERIAL_USE_MULTI_TRANSACTION
69 /* --- USE Simple API (OLD API, compatible with let's split serial.c) */
70 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
71 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
73 #if SERIAL_MASTER_BUFFER_LENGTH > 0
74 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
76 uint8_t volatile status0 = 0;
78 SSTD_t transactions[] = {
79 { (uint8_t *)&status0,
80 #if SERIAL_MASTER_BUFFER_LENGTH > 0
81 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
85 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
86 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
93 void serial_master_init(void)
94 { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
96 void serial_slave_init(void)
97 { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
100 // 1 => slave did not respond
101 // 2 => checksum error
102 int serial_update_buffers()
105 result = soft_serial_transaction();
109 #endif // Simple API (OLD API, compatible with let's split serial.c)
111 #define ALWAYS_INLINE __attribute__((always_inline))
112 #define NO_INLINE __attribute__((noinline))
113 #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
117 #define EVEN_PARITY 0
118 #define PARITY EVEN_PARITY
121 // custom setup in config.h
122 // #define TID_SEND_ADJUST 2
123 // #define SERIAL_DELAY 6 // micro sec
124 // #define READ_WRITE_START_ADJUST 30 // cycles
125 // #define READ_WRITE_WIDTH_ADJUST 8 // cycles
127 // ============ Standard setups ============
129 #ifndef SELECT_SOFT_SERIAL_SPEED
130 #define SELECT_SOFT_SERIAL_SPEED 1
132 // 1: about 137kbps (default)
140 #define TID_SEND_ADJUST 14
142 #define TID_SEND_ADJUST 2
145 #if SELECT_SOFT_SERIAL_SPEED == 0
147 #define SERIAL_DELAY 4 // micro sec
149 #define READ_WRITE_START_ADJUST 33 // cycles
150 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
152 #define READ_WRITE_START_ADJUST 34 // cycles
153 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
155 #elif SELECT_SOFT_SERIAL_SPEED == 1
157 #define SERIAL_DELAY 6 // micro sec
159 #define READ_WRITE_START_ADJUST 30 // cycles
160 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
162 #define READ_WRITE_START_ADJUST 33 // cycles
163 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
165 #elif SELECT_SOFT_SERIAL_SPEED == 2
167 #define SERIAL_DELAY 12 // micro sec
168 #define READ_WRITE_START_ADJUST 30 // cycles
170 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
172 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
174 #elif SELECT_SOFT_SERIAL_SPEED == 3
176 #define SERIAL_DELAY 24 // micro sec
177 #define READ_WRITE_START_ADJUST 30 // cycles
179 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
181 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
183 #elif SELECT_SOFT_SERIAL_SPEED == 4
185 #define SERIAL_DELAY 36 // micro sec
186 #define READ_WRITE_START_ADJUST 30 // cycles
188 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
190 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
192 #elif SELECT_SOFT_SERIAL_SPEED == 5
194 #define SERIAL_DELAY 48 // micro sec
195 #define READ_WRITE_START_ADJUST 30 // cycles
197 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
199 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
202 #error invalid SELECT_SOFT_SERIAL_SPEED value
203 #endif /* SELECT_SOFT_SERIAL_SPEED */
204 #endif /* SERIAL_DELAY */
206 #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
207 #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
209 #define SLAVE_INT_WIDTH_US 1
210 #ifndef SERIAL_USE_MULTI_TRANSACTION
211 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
213 #define SLAVE_INT_ACK_WIDTH_UNIT 2
214 #define SLAVE_INT_ACK_WIDTH 4
217 static SSTD_t *Transaction_table = NULL;
218 static uint8_t Transaction_table_size = 0;
220 inline static void serial_delay(void) ALWAYS_INLINE;
222 void serial_delay(void) {
223 _delay_us(SERIAL_DELAY);
226 inline static void serial_delay_half1(void) ALWAYS_INLINE;
228 void serial_delay_half1(void) {
229 _delay_us(SERIAL_DELAY_HALF1);
232 inline static void serial_delay_half2(void) ALWAYS_INLINE;
234 void serial_delay_half2(void) {
235 _delay_us(SERIAL_DELAY_HALF2);
238 inline static void serial_output(void) ALWAYS_INLINE;
240 void serial_output(void) {
241 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
244 // make the serial pin an input with pull-up resistor
245 inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
247 void serial_input_with_pullup(void) {
248 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
249 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
252 inline static uint8_t serial_read_pin(void) ALWAYS_INLINE;
254 uint8_t serial_read_pin(void) {
255 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
258 inline static void serial_low(void) ALWAYS_INLINE;
260 void serial_low(void) {
261 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
264 inline static void serial_high(void) ALWAYS_INLINE;
266 void serial_high(void) {
267 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
270 void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size)
272 Transaction_table = sstd_table;
273 Transaction_table_size = (uint8_t)sstd_table_size;
278 void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size)
280 Transaction_table = sstd_table;
281 Transaction_table_size = (uint8_t)sstd_table_size;
282 serial_input_with_pullup();
284 // Enable INT0-INT3,INT6
286 #if SERIAL_PIN_MASK == _BV(PE6)
287 // Trigger on falling edge of INT6
290 // Trigger on falling edge of INT0-INT3
295 // Used by the sender to synchronize timing with the reciver.
296 static void sync_recv(void) NO_INLINE;
298 void sync_recv(void) {
299 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
301 // This shouldn't hang if the target disconnects because the
302 // serial line will float to high if the target does disconnect.
303 while (!serial_read_pin());
306 // Used by the reciver to send a synchronization signal to the sender.
307 static void sync_send(void) NO_INLINE;
309 void sync_send(void) {
315 // Reads a byte from the serial line
316 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
317 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
318 uint8_t byte, i, p, pb;
320 _delay_sub_us(READ_WRITE_START_ADJUST);
321 for( i = 0, byte = 0, p = PARITY; i < bit; i++ ) {
322 serial_delay_half1(); // read the middle of pulses
323 if( serial_read_pin() ) {
324 byte = (byte << 1) | 1; p ^= 1;
326 byte = (byte << 1) | 0; p ^= 0;
328 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
329 serial_delay_half2();
331 /* recive parity bit */
332 serial_delay_half1(); // read the middle of pulses
333 pb = serial_read_pin();
334 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
335 serial_delay_half2();
337 *pterrcount += (p != pb)? 1 : 0;
342 // Sends a byte with MSB ordering
343 void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
344 void serial_write_chunk(uint8_t data, uint8_t bit) {
346 for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) {
348 serial_high(); p ^= 1;
350 serial_low(); p ^= 0;
354 /* send parity bit */
355 if(p & 1) { serial_high(); }
356 else { serial_low(); }
359 serial_low(); // sync_send() / senc_recv() need raise edge
362 static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
364 void serial_send_packet(uint8_t *buffer, uint8_t size) {
365 for (uint8_t i = 0; i < size; ++i) {
369 serial_write_chunk(data,8);
373 static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
375 uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
377 for (uint8_t i = 0; i < size; ++i) {
380 data = serial_read_chunk(&pecount, 8);
387 void change_sender2reciver(void) {
389 serial_delay_half1(); //1
391 serial_input_with_pullup(); //2
392 serial_delay_half1(); //3
396 void change_reciver2sender(void) {
401 serial_delay_half1(); //4
404 static inline uint8_t nibble_bits_count(uint8_t bits)
406 bits = (bits & 0x5) + (bits >> 1 & 0x5);
407 bits = (bits & 0x3) + (bits >> 2 & 0x3);
411 // interrupt handle to be used by the target device
412 ISR(SERIAL_PIN_INTERRUPT) {
414 #ifndef SERIAL_USE_MULTI_TRANSACTION
417 SSTD_t *trans = Transaction_table;
419 // recive transaction table index
423 bits = serial_read_chunk(&pecount,7);
425 bits = (bits&7) != nibble_bits_count(tid);
426 if( bits || pecount> 0 || tid > Transaction_table_size ) {
429 serial_delay_half1();
431 serial_high(); // response step1 low->high
433 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
434 SSTD_t *trans = &Transaction_table[tid];
435 serial_low(); // response step2 ack high->low
439 if( trans->target2initiator_buffer_size > 0 )
440 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
441 trans->target2initiator_buffer_size);
442 // target switch to input
443 change_sender2reciver();
445 // target recive phase
446 if( trans->initiator2target_buffer_size > 0 ) {
447 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
448 trans->initiator2target_buffer_size) ) {
449 *trans->status = TRANSACTION_ACCEPTED;
451 *trans->status = TRANSACTION_DATA_ERROR;
454 *trans->status = TRANSACTION_ACCEPTED;
457 sync_recv(); //weit initiator output to high
461 // start transaction by initiator
463 // int soft_serial_transaction(int sstd_index)
467 // TRANSACTION_NO_RESPONSE
468 // TRANSACTION_DATA_ERROR
469 // this code is very time dependent, so we need to disable interrupts
470 #ifndef SERIAL_USE_MULTI_TRANSACTION
471 int soft_serial_transaction(void) {
472 SSTD_t *trans = Transaction_table;
474 int soft_serial_transaction(int sstd_index) {
475 if( sstd_index > Transaction_table_size )
476 return TRANSACTION_TYPE_ERROR;
477 SSTD_t *trans = &Transaction_table[sstd_index];
481 // signal to the target that we want to start a transaction
484 _delay_us(SLAVE_INT_WIDTH_US);
486 #ifndef SERIAL_USE_MULTI_TRANSACTION
487 // wait for the target response
488 serial_input_with_pullup();
489 _delay_us(SLAVE_INT_RESPONSE_TIME);
491 // check if the target is present
492 if (serial_read_pin()) {
493 // target failed to pull the line low, assume not present
496 *trans->status = TRANSACTION_NO_RESPONSE;
498 return TRANSACTION_NO_RESPONSE;
502 // send transaction table index
503 int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index));
505 _delay_sub_us(TID_SEND_ADJUST);
506 serial_write_chunk(tid, 7);
507 serial_delay_half1();
509 // wait for the target response (step1 low->high)
510 serial_input_with_pullup();
511 while( !serial_read_pin() ) {
515 // check if the target is present (step2 high->low)
516 for( int i = 0; serial_read_pin(); i++ ) {
517 if (i > SLAVE_INT_ACK_WIDTH + 1) {
518 // slave failed to pull the line low, assume not present
521 *trans->status = TRANSACTION_NO_RESPONSE;
523 return TRANSACTION_NO_RESPONSE;
525 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
529 // initiator recive phase
530 // if the target is present syncronize with it
531 if( trans->target2initiator_buffer_size > 0 ) {
532 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
533 trans->target2initiator_buffer_size) ) {
536 *trans->status = TRANSACTION_DATA_ERROR;
538 return TRANSACTION_DATA_ERROR;
542 // initiator switch to output
543 change_reciver2sender();
545 // initiator send phase
546 if( trans->initiator2target_buffer_size > 0 ) {
547 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
548 trans->initiator2target_buffer_size);
551 // always, release the line when not in use
554 *trans->status = TRANSACTION_END;
556 return TRANSACTION_END;
559 #ifdef SERIAL_USE_MULTI_TRANSACTION
560 int soft_serial_get_and_clean_status(int sstd_index) {
561 SSTD_t *trans = &Transaction_table[sstd_index];
563 int retval = *trans->status;
572 // Helix serial.c history
573 // 2018-1-29 fork from let's split (#2308)
574 // 2018-6-28 bug fix master to slave comm (#3255)
575 // 2018-8-11 improvements (#3608)
576 // 2018-10-21 fix serial and RGB animation conflict (#4191)