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)
139 #define TID_SEND_ADJUST 2
141 #if SELECT_SOFT_SERIAL_SPEED == 0
143 #define SERIAL_DELAY 4 // micro sec
144 #define READ_WRITE_START_ADJUST 33 // cycles
145 #define READ_WRITE_WIDTH_ADJUST 6 // cycles
146 #elif SELECT_SOFT_SERIAL_SPEED == 1
148 #define SERIAL_DELAY 6 // micro sec
149 #define READ_WRITE_START_ADJUST 30 // cycles
150 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
151 #elif SELECT_SOFT_SERIAL_SPEED == 2
153 #define SERIAL_DELAY 12 // micro sec
154 #define READ_WRITE_START_ADJUST 30 // cycles
155 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
156 #elif SELECT_SOFT_SERIAL_SPEED == 3
158 #define SERIAL_DELAY 24 // micro sec
159 #define READ_WRITE_START_ADJUST 30 // cycles
160 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
161 #elif SELECT_SOFT_SERIAL_SPEED == 4
163 #define SERIAL_DELAY 36 // micro sec
164 #define READ_WRITE_START_ADJUST 30 // cycles
165 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
166 #elif SELECT_SOFT_SERIAL_SPEED == 5
168 #define SERIAL_DELAY 48 // micro sec
169 #define READ_WRITE_START_ADJUST 30 // cycles
170 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
172 #error invalid SELECT_SOFT_SERIAL_SPEED value
173 #endif /* SELECT_SOFT_SERIAL_SPEED */
174 #endif /* SERIAL_DELAY */
176 #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
177 #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
179 #define SLAVE_INT_WIDTH_US 1
180 #ifndef SERIAL_USE_MULTI_TRANSACTION
181 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
183 #define SLAVE_INT_ACK_WIDTH_UNIT 2
184 #define SLAVE_INT_ACK_WIDTH 4
187 static SSTD_t *Transaction_table = NULL;
188 static uint8_t Transaction_table_size = 0;
191 void serial_delay(void) {
192 _delay_us(SERIAL_DELAY);
196 void serial_delay_half1(void) {
197 _delay_us(SERIAL_DELAY_HALF1);
201 void serial_delay_half2(void) {
202 _delay_us(SERIAL_DELAY_HALF2);
205 inline static void serial_output(void) ALWAYS_INLINE;
207 void serial_output(void) {
208 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
211 // make the serial pin an input with pull-up resistor
212 inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
214 void serial_input_with_pullup(void) {
215 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
216 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
220 uint8_t serial_read_pin(void) {
221 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
224 inline static void serial_low(void) ALWAYS_INLINE;
226 void serial_low(void) {
227 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
230 inline static void serial_high(void) ALWAYS_INLINE;
232 void serial_high(void) {
233 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
236 void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size)
238 Transaction_table = sstd_table;
239 Transaction_table_size = (uint8_t)sstd_table_size;
244 void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size)
246 Transaction_table = sstd_table;
247 Transaction_table_size = (uint8_t)sstd_table_size;
248 serial_input_with_pullup();
250 // Enable INT0-INT3,INT6
252 #if SERIAL_PIN_MASK == _BV(PE6)
253 // Trigger on falling edge of INT6
256 // Trigger on falling edge of INT0-INT3
261 // Used by the sender to synchronize timing with the reciver.
262 static void sync_recv(void) NO_INLINE;
264 void sync_recv(void) {
265 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
267 // This shouldn't hang if the target disconnects because the
268 // serial line will float to high if the target does disconnect.
269 while (!serial_read_pin());
272 // Used by the reciver to send a synchronization signal to the sender.
273 static void sync_send(void)NO_INLINE;
275 void sync_send(void) {
281 // Reads a byte from the serial line
282 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
283 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
284 uint8_t byte, i, p, pb;
286 _delay_sub_us(READ_WRITE_START_ADJUST);
287 for( i = 0, byte = 0, p = PARITY; i < bit; i++ ) {
288 serial_delay_half1(); // read the middle of pulses
289 if( serial_read_pin() ) {
290 byte = (byte << 1) | 1; p ^= 1;
292 byte = (byte << 1) | 0; p ^= 0;
294 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
295 serial_delay_half2();
297 /* recive parity bit */
298 serial_delay_half1(); // read the middle of pulses
299 pb = serial_read_pin();
300 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
301 serial_delay_half2();
303 *pterrcount += (p != pb)? 1 : 0;
308 // Sends a byte with MSB ordering
309 void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
310 void serial_write_chunk(uint8_t data, uint8_t bit) {
312 for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) {
314 serial_high(); p ^= 1;
316 serial_low(); p ^= 0;
320 /* send parity bit */
321 if(p & 1) { serial_high(); }
322 else { serial_low(); }
325 serial_low(); // sync_send() / senc_recv() need raise edge
328 static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
330 void serial_send_packet(uint8_t *buffer, uint8_t size) {
331 for (uint8_t i = 0; i < size; ++i) {
335 serial_write_chunk(data,8);
339 static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
341 uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
343 for (uint8_t i = 0; i < size; ++i) {
346 data = serial_read_chunk(&pecount, 8);
353 void change_sender2reciver(void) {
355 serial_delay_half1(); //1
357 serial_input_with_pullup(); //2
358 serial_delay_half1(); //3
362 void change_reciver2sender(void) {
367 serial_delay_half1(); //4
370 static inline uint8_t nibble_bits_count(uint8_t bits)
372 bits = (bits & 0x5) + (bits >> 1 & 0x5);
373 bits = (bits & 0x3) + (bits >> 2 & 0x3);
377 // interrupt handle to be used by the target device
378 ISR(SERIAL_PIN_INTERRUPT) {
380 #ifndef SERIAL_USE_MULTI_TRANSACTION
383 SSTD_t *trans = Transaction_table;
385 // recive transaction table index
389 bits = serial_read_chunk(&pecount,7);
391 bits = (bits&7) != nibble_bits_count(tid);
392 if( bits || pecount> 0 || tid > Transaction_table_size ) {
395 serial_delay_half1();
397 serial_high(); // response step1 low->high
399 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
400 SSTD_t *trans = &Transaction_table[tid];
401 serial_low(); // response step2 ack high->low
405 if( trans->target2initiator_buffer_size > 0 )
406 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
407 trans->target2initiator_buffer_size);
408 // target switch to input
409 change_sender2reciver();
411 // target recive phase
412 if( trans->initiator2target_buffer_size > 0 ) {
413 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
414 trans->initiator2target_buffer_size) ) {
415 *trans->status = TRANSACTION_ACCEPTED;
417 *trans->status = TRANSACTION_DATA_ERROR;
420 *trans->status = TRANSACTION_ACCEPTED;
423 sync_recv(); //weit initiator output to high
427 // start transaction by initiator
429 // int soft_serial_transaction(int sstd_index)
433 // TRANSACTION_NO_RESPONSE
434 // TRANSACTION_DATA_ERROR
435 // this code is very time dependent, so we need to disable interrupts
436 #ifndef SERIAL_USE_MULTI_TRANSACTION
437 int soft_serial_transaction(void) {
438 SSTD_t *trans = Transaction_table;
440 int soft_serial_transaction(int sstd_index) {
441 if( sstd_index > Transaction_table_size )
442 return TRANSACTION_TYPE_ERROR;
443 SSTD_t *trans = &Transaction_table[sstd_index];
447 // signal to the target that we want to start a transaction
450 _delay_us(SLAVE_INT_WIDTH_US);
452 #ifndef SERIAL_USE_MULTI_TRANSACTION
453 // wait for the target response
454 serial_input_with_pullup();
455 _delay_us(SLAVE_INT_RESPONSE_TIME);
457 // check if the target is present
458 if (serial_read_pin()) {
459 // target failed to pull the line low, assume not present
462 *trans->status = TRANSACTION_NO_RESPONSE;
464 return TRANSACTION_NO_RESPONSE;
468 // send transaction table index
469 int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index));
471 _delay_sub_us(TID_SEND_ADJUST);
472 serial_write_chunk(tid, 7);
473 serial_delay_half1();
475 // wait for the target response (step1 low->high)
476 serial_input_with_pullup();
477 while( !serial_read_pin() ) {
481 // check if the target is present (step2 high->low)
482 for( int i = 0; serial_read_pin(); i++ ) {
483 if (i > SLAVE_INT_ACK_WIDTH + 1) {
484 // slave failed to pull the line low, assume not present
487 *trans->status = TRANSACTION_NO_RESPONSE;
489 return TRANSACTION_NO_RESPONSE;
491 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
495 // initiator recive phase
496 // if the target is present syncronize with it
497 if( trans->target2initiator_buffer_size > 0 ) {
498 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
499 trans->target2initiator_buffer_size) ) {
502 *trans->status = TRANSACTION_DATA_ERROR;
504 return TRANSACTION_DATA_ERROR;
508 // initiator switch to output
509 change_reciver2sender();
511 // initiator send phase
512 if( trans->initiator2target_buffer_size > 0 ) {
513 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
514 trans->initiator2target_buffer_size);
517 // always, release the line when not in use
520 *trans->status = TRANSACTION_END;
522 return TRANSACTION_END;
525 #ifdef SERIAL_USE_MULTI_TRANSACTION
526 int soft_serial_get_and_clean_status(int sstd_index) {
527 SSTD_t *trans = &Transaction_table[sstd_index];
529 int retval = *trans->status;
538 // Helix serial.c history
539 // 2018-1-29 fork from let's split (#2308)
540 // 2018-6-28 bug fix master to slave comm (#3255)
541 // 2018-8-11 improvements (#3608)
542 // 2018-10-21 fix serial and RGB animation conflict (#4191)