2 * WARNING: be careful changing this code, it is very timing dependent
11 #define F_CPU 16000000
15 #include <avr/interrupt.h>
16 #include <util/delay.h>
21 #ifdef SOFT_SERIAL_PIN
23 #ifdef __AVR_ATmega32U4__
24 // if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial.
26 #if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1
27 #error Using ATmega32U4 I2C, so can not use PD0, PD1
31 #if SOFT_SERIAL_PIN >= D0 && SOFT_SERIAL_PIN <= D3
32 #define SERIAL_PIN_DDR DDRD
33 #define SERIAL_PIN_PORT PORTD
34 #define SERIAL_PIN_INPUT PIND
35 #if SOFT_SERIAL_PIN == D0
36 #define SERIAL_PIN_MASK _BV(PD0)
37 #define EIMSK_BIT _BV(INT0)
38 #define EICRx_BIT (~(_BV(ISC00) | _BV(ISC01)))
39 #define SERIAL_PIN_INTERRUPT INT0_vect
40 #elif SOFT_SERIAL_PIN == D1
41 #define SERIAL_PIN_MASK _BV(PD1)
42 #define EIMSK_BIT _BV(INT1)
43 #define EICRx_BIT (~(_BV(ISC10) | _BV(ISC11)))
44 #define SERIAL_PIN_INTERRUPT INT1_vect
45 #elif SOFT_SERIAL_PIN == D2
46 #define SERIAL_PIN_MASK _BV(PD2)
47 #define EIMSK_BIT _BV(INT2)
48 #define EICRx_BIT (~(_BV(ISC20) | _BV(ISC21)))
49 #define SERIAL_PIN_INTERRUPT INT2_vect
50 #elif SOFT_SERIAL_PIN == D3
51 #define SERIAL_PIN_MASK _BV(PD3)
52 #define EIMSK_BIT _BV(INT3)
53 #define EICRx_BIT (~(_BV(ISC30) | _BV(ISC31)))
54 #define SERIAL_PIN_INTERRUPT INT3_vect
56 #elif SOFT_SERIAL_PIN == E6
57 #define SERIAL_PIN_DDR DDRE
58 #define SERIAL_PIN_PORT PORTE
59 #define SERIAL_PIN_INPUT PINE
60 #define SERIAL_PIN_MASK _BV(PE6)
61 #define EIMSK_BIT _BV(INT6)
62 #define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61)))
63 #define SERIAL_PIN_INTERRUPT INT6_vect
65 #error invalid SOFT_SERIAL_PIN value
69 #error serial.c now support ATmega32U4 only
72 //////////////// for backward compatibility ////////////////////////////////
73 #ifndef SERIAL_USE_MULTI_TRANSACTION
74 /* --- USE Simple API (OLD API, compatible with let's split serial.c) */
75 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
76 uint8_t volatile serial_slave_buffer
[SERIAL_SLAVE_BUFFER_LENGTH
] = {0};
78 #if SERIAL_MASTER_BUFFER_LENGTH > 0
79 uint8_t volatile serial_master_buffer
[SERIAL_MASTER_BUFFER_LENGTH
] = {0};
81 uint8_t volatile status0
= 0;
83 SSTD_t transactions
[] = {
84 { (uint8_t *)&status0
,
85 #if SERIAL_MASTER_BUFFER_LENGTH > 0
86 sizeof(serial_master_buffer
), (uint8_t *)serial_master_buffer
,
90 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
91 sizeof(serial_slave_buffer
), (uint8_t *)serial_slave_buffer
98 void serial_master_init(void)
99 { soft_serial_initiator_init(transactions
, TID_LIMIT(transactions
)); }
101 void serial_slave_init(void)
102 { soft_serial_target_init(transactions
, TID_LIMIT(transactions
)); }
105 // 1 => slave did not respond
106 // 2 => checksum error
107 int serial_update_buffers()
110 result
= soft_serial_transaction();
114 #endif // end of Simple API (OLD API, compatible with let's split serial.c)
115 ////////////////////////////////////////////////////////////////////////////
117 #define ALWAYS_INLINE __attribute__((always_inline))
118 #define NO_INLINE __attribute__((noinline))
119 #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
123 #define EVEN_PARITY 0
124 #define PARITY EVEN_PARITY
127 // custom setup in config.h
128 // #define TID_SEND_ADJUST 2
129 // #define SERIAL_DELAY 6 // micro sec
130 // #define READ_WRITE_START_ADJUST 30 // cycles
131 // #define READ_WRITE_WIDTH_ADJUST 8 // cycles
133 // ============ Standard setups ============
135 #ifndef SELECT_SOFT_SERIAL_SPEED
136 #define SELECT_SOFT_SERIAL_SPEED 1
138 // 1: about 137kbps (default)
146 #define TID_SEND_ADJUST 14
148 #define TID_SEND_ADJUST 2
151 #if SELECT_SOFT_SERIAL_SPEED == 0
153 #define SERIAL_DELAY 4 // micro sec
155 #define READ_WRITE_START_ADJUST 33 // cycles
156 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
158 #define READ_WRITE_START_ADJUST 34 // cycles
159 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
161 #elif SELECT_SOFT_SERIAL_SPEED == 1
163 #define SERIAL_DELAY 6 // micro sec
165 #define READ_WRITE_START_ADJUST 30 // cycles
166 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
168 #define READ_WRITE_START_ADJUST 33 // cycles
169 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
171 #elif SELECT_SOFT_SERIAL_SPEED == 2
173 #define SERIAL_DELAY 12 // micro sec
174 #define READ_WRITE_START_ADJUST 30 // cycles
176 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
178 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
180 #elif SELECT_SOFT_SERIAL_SPEED == 3
182 #define SERIAL_DELAY 24 // micro sec
183 #define READ_WRITE_START_ADJUST 30 // cycles
185 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
187 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
189 #elif SELECT_SOFT_SERIAL_SPEED == 4
191 #define SERIAL_DELAY 36 // micro sec
192 #define READ_WRITE_START_ADJUST 30 // cycles
194 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
196 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
198 #elif SELECT_SOFT_SERIAL_SPEED == 5
200 #define SERIAL_DELAY 48 // micro sec
201 #define READ_WRITE_START_ADJUST 30 // cycles
203 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
205 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
208 #error invalid SELECT_SOFT_SERIAL_SPEED value
209 #endif /* SELECT_SOFT_SERIAL_SPEED */
210 #endif /* SERIAL_DELAY */
212 #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
213 #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
215 #define SLAVE_INT_WIDTH_US 1
216 #ifndef SERIAL_USE_MULTI_TRANSACTION
217 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
219 #define SLAVE_INT_ACK_WIDTH_UNIT 2
220 #define SLAVE_INT_ACK_WIDTH 4
223 static SSTD_t
*Transaction_table
= NULL
;
224 static uint8_t Transaction_table_size
= 0;
226 inline static void serial_delay(void) ALWAYS_INLINE
;
228 void serial_delay(void) {
229 _delay_us(SERIAL_DELAY
);
232 inline static void serial_delay_half1(void) ALWAYS_INLINE
;
234 void serial_delay_half1(void) {
235 _delay_us(SERIAL_DELAY_HALF1
);
238 inline static void serial_delay_half2(void) ALWAYS_INLINE
;
240 void serial_delay_half2(void) {
241 _delay_us(SERIAL_DELAY_HALF2
);
244 inline static void serial_output(void) ALWAYS_INLINE
;
246 void serial_output(void) {
247 SERIAL_PIN_DDR
|= SERIAL_PIN_MASK
;
250 // make the serial pin an input with pull-up resistor
251 inline static void serial_input_with_pullup(void) ALWAYS_INLINE
;
253 void serial_input_with_pullup(void) {
254 SERIAL_PIN_DDR
&= ~SERIAL_PIN_MASK
;
255 SERIAL_PIN_PORT
|= SERIAL_PIN_MASK
;
258 inline static uint8_t serial_read_pin(void) ALWAYS_INLINE
;
260 uint8_t serial_read_pin(void) {
261 return !!(SERIAL_PIN_INPUT
& SERIAL_PIN_MASK
);
264 inline static void serial_low(void) ALWAYS_INLINE
;
266 void serial_low(void) {
267 SERIAL_PIN_PORT
&= ~SERIAL_PIN_MASK
;
270 inline static void serial_high(void) ALWAYS_INLINE
;
272 void serial_high(void) {
273 SERIAL_PIN_PORT
|= SERIAL_PIN_MASK
;
276 void soft_serial_initiator_init(SSTD_t
*sstd_table
, int sstd_table_size
)
278 Transaction_table
= sstd_table
;
279 Transaction_table_size
= (uint8_t)sstd_table_size
;
284 void soft_serial_target_init(SSTD_t
*sstd_table
, int sstd_table_size
)
286 Transaction_table
= sstd_table
;
287 Transaction_table_size
= (uint8_t)sstd_table_size
;
288 serial_input_with_pullup();
290 // Enable INT0-INT3,INT6
292 #if SERIAL_PIN_MASK == _BV(PE6)
293 // Trigger on falling edge of INT6
296 // Trigger on falling edge of INT0-INT3
301 // Used by the sender to synchronize timing with the reciver.
302 static void sync_recv(void) NO_INLINE
;
304 void sync_recv(void) {
305 for (uint8_t i
= 0; i
< SERIAL_DELAY
*5 && serial_read_pin(); i
++ ) {
307 // This shouldn't hang if the target disconnects because the
308 // serial line will float to high if the target does disconnect.
309 while (!serial_read_pin());
312 // Used by the reciver to send a synchronization signal to the sender.
313 static void sync_send(void) NO_INLINE
;
315 void sync_send(void) {
321 // Reads a byte from the serial line
322 static uint8_t serial_read_chunk(uint8_t *pterrcount
, uint8_t bit
) NO_INLINE
;
323 static uint8_t serial_read_chunk(uint8_t *pterrcount
, uint8_t bit
) {
324 uint8_t byte
, i
, p
, pb
;
326 _delay_sub_us(READ_WRITE_START_ADJUST
);
327 for( i
= 0, byte
= 0, p
= PARITY
; i
< bit
; i
++ ) {
328 serial_delay_half1(); // read the middle of pulses
329 if( serial_read_pin() ) {
330 byte
= (byte
<< 1) | 1; p
^= 1;
332 byte
= (byte
<< 1) | 0; p
^= 0;
334 _delay_sub_us(READ_WRITE_WIDTH_ADJUST
);
335 serial_delay_half2();
337 /* recive parity bit */
338 serial_delay_half1(); // read the middle of pulses
339 pb
= serial_read_pin();
340 _delay_sub_us(READ_WRITE_WIDTH_ADJUST
);
341 serial_delay_half2();
343 *pterrcount
+= (p
!= pb
)? 1 : 0;
348 // Sends a byte with MSB ordering
349 void serial_write_chunk(uint8_t data
, uint8_t bit
) NO_INLINE
;
350 void serial_write_chunk(uint8_t data
, uint8_t bit
) {
352 for( p
= PARITY
, b
= 1<<(bit
-1); b
; b
>>= 1) {
354 serial_high(); p
^= 1;
356 serial_low(); p
^= 0;
360 /* send parity bit */
361 if(p
& 1) { serial_high(); }
362 else { serial_low(); }
365 serial_low(); // sync_send() / senc_recv() need raise edge
368 static void serial_send_packet(uint8_t *buffer
, uint8_t size
) NO_INLINE
;
370 void serial_send_packet(uint8_t *buffer
, uint8_t size
) {
371 for (uint8_t i
= 0; i
< size
; ++i
) {
375 serial_write_chunk(data
,8);
379 static uint8_t serial_recive_packet(uint8_t *buffer
, uint8_t size
) NO_INLINE
;
381 uint8_t serial_recive_packet(uint8_t *buffer
, uint8_t size
) {
383 for (uint8_t i
= 0; i
< size
; ++i
) {
386 data
= serial_read_chunk(&pecount
, 8);
393 void change_sender2reciver(void) {
395 serial_delay_half1(); //1
397 serial_input_with_pullup(); //2
398 serial_delay_half1(); //3
402 void change_reciver2sender(void) {
407 serial_delay_half1(); //4
410 static inline uint8_t nibble_bits_count(uint8_t bits
)
412 bits
= (bits
& 0x5) + (bits
>> 1 & 0x5);
413 bits
= (bits
& 0x3) + (bits
>> 2 & 0x3);
417 // interrupt handle to be used by the target device
418 ISR(SERIAL_PIN_INTERRUPT
) {
420 #ifndef SERIAL_USE_MULTI_TRANSACTION
423 SSTD_t
*trans
= Transaction_table
;
425 // recive transaction table index
429 bits
= serial_read_chunk(&pecount
,7);
431 bits
= (bits
&7) != nibble_bits_count(tid
);
432 if( bits
|| pecount
> 0 || tid
> Transaction_table_size
) {
435 serial_delay_half1();
437 serial_high(); // response step1 low->high
439 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT
*SLAVE_INT_ACK_WIDTH
);
440 SSTD_t
*trans
= &Transaction_table
[tid
];
441 serial_low(); // response step2 ack high->low
445 if( trans
->target2initiator_buffer_size
> 0 )
446 serial_send_packet((uint8_t *)trans
->target2initiator_buffer
,
447 trans
->target2initiator_buffer_size
);
448 // target switch to input
449 change_sender2reciver();
451 // target recive phase
452 if( trans
->initiator2target_buffer_size
> 0 ) {
453 if (serial_recive_packet((uint8_t *)trans
->initiator2target_buffer
,
454 trans
->initiator2target_buffer_size
) ) {
455 *trans
->status
= TRANSACTION_ACCEPTED
;
457 *trans
->status
= TRANSACTION_DATA_ERROR
;
460 *trans
->status
= TRANSACTION_ACCEPTED
;
463 sync_recv(); //weit initiator output to high
467 // start transaction by initiator
469 // int soft_serial_transaction(int sstd_index)
473 // TRANSACTION_NO_RESPONSE
474 // TRANSACTION_DATA_ERROR
475 // this code is very time dependent, so we need to disable interrupts
476 #ifndef SERIAL_USE_MULTI_TRANSACTION
477 int soft_serial_transaction(void) {
478 SSTD_t
*trans
= Transaction_table
;
480 int soft_serial_transaction(int sstd_index
) {
481 if( sstd_index
> Transaction_table_size
)
482 return TRANSACTION_TYPE_ERROR
;
483 SSTD_t
*trans
= &Transaction_table
[sstd_index
];
487 // signal to the target that we want to start a transaction
490 _delay_us(SLAVE_INT_WIDTH_US
);
492 #ifndef SERIAL_USE_MULTI_TRANSACTION
493 // wait for the target response
494 serial_input_with_pullup();
495 _delay_us(SLAVE_INT_RESPONSE_TIME
);
497 // check if the target is present
498 if (serial_read_pin()) {
499 // target failed to pull the line low, assume not present
502 *trans
->status
= TRANSACTION_NO_RESPONSE
;
504 return TRANSACTION_NO_RESPONSE
;
508 // send transaction table index
509 int tid
= (sstd_index
<<3) | (7 & nibble_bits_count(sstd_index
));
511 _delay_sub_us(TID_SEND_ADJUST
);
512 serial_write_chunk(tid
, 7);
513 serial_delay_half1();
515 // wait for the target response (step1 low->high)
516 serial_input_with_pullup();
517 while( !serial_read_pin() ) {
521 // check if the target is present (step2 high->low)
522 for( int i
= 0; serial_read_pin(); i
++ ) {
523 if (i
> SLAVE_INT_ACK_WIDTH
+ 1) {
524 // slave failed to pull the line low, assume not present
527 *trans
->status
= TRANSACTION_NO_RESPONSE
;
529 return TRANSACTION_NO_RESPONSE
;
531 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT
);
535 // initiator recive phase
536 // if the target is present syncronize with it
537 if( trans
->target2initiator_buffer_size
> 0 ) {
538 if (!serial_recive_packet((uint8_t *)trans
->target2initiator_buffer
,
539 trans
->target2initiator_buffer_size
) ) {
542 *trans
->status
= TRANSACTION_DATA_ERROR
;
544 return TRANSACTION_DATA_ERROR
;
548 // initiator switch to output
549 change_reciver2sender();
551 // initiator send phase
552 if( trans
->initiator2target_buffer_size
> 0 ) {
553 serial_send_packet((uint8_t *)trans
->initiator2target_buffer
,
554 trans
->initiator2target_buffer_size
);
557 // always, release the line when not in use
560 *trans
->status
= TRANSACTION_END
;
562 return TRANSACTION_END
;
565 #ifdef SERIAL_USE_MULTI_TRANSACTION
566 int soft_serial_get_and_clean_status(int sstd_index
) {
567 SSTD_t
*trans
= &Transaction_table
[sstd_index
];
569 int retval
= *trans
->status
;
578 // Helix serial.c history
579 // 2018-1-29 fork from let's split and add PD2, modify sync_recv() (#2308, bceffdefc)
580 // 2018-6-28 bug fix master to slave comm and speed up (#3255, 1038bbef4)
581 // (adjusted with avr-gcc 4.9.2)
582 // 2018-7-13 remove USE_SERIAL_PD2 macro (#3374, f30d6dd78)
583 // (adjusted with avr-gcc 4.9.2)
584 // 2018-8-11 add support multi-type transaction (#3608, feb5e4aae)
585 // (adjusted with avr-gcc 4.9.2)
586 // 2018-10-21 fix serial and RGB animation conflict (#4191, 4665e4fff)
587 // (adjusted with avr-gcc 7.3.0)
588 // 2018-10-28 re-adjust compiler depend value of delay (#4269, 8517f8a66)
589 // (adjusted with avr-gcc 5.4.0, 7.3.0)