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>
19 #ifndef SERIAL_USE_MULTI_TRANSACTION
20 /* --- USE Simple API (OLD API, compatible with let's split serial.c) */
21 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
22 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
24 #if SERIAL_MASTER_BUFFER_LENGTH > 0
25 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
27 uint8_t volatile status0 = 0;
29 SSTD_t transactions[] = {
30 { (uint8_t *)&status0,
31 #if SERIAL_MASTER_BUFFER_LENGTH > 0
32 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
36 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
37 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
44 void serial_master_init(void)
45 { soft_serial_initiator_init(transactions); }
47 void serial_slave_init(void)
48 { soft_serial_target_init(transactions); }
51 // 1 => slave did not respond
52 // 2 => checksum error
53 int serial_update_buffers()
54 { return soft_serial_transaction(); }
56 #endif // Simple API (OLD API, compatible with let's split serial.c)
58 #define ALWAYS_INLINE __attribute__((always_inline))
59 #define NO_INLINE __attribute__((noinline))
60 #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
62 // Serial pulse period in microseconds.
63 #define TID_SEND_ADJUST 14
65 #define SELECT_SERIAL_SPEED 1
66 #if SELECT_SERIAL_SPEED == 0
68 #define SERIAL_DELAY 4 // micro sec
69 #define READ_WRITE_START_ADJUST 33 // cycles
70 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
71 #elif SELECT_SERIAL_SPEED == 1
73 #define SERIAL_DELAY 6 // micro sec
74 #define READ_WRITE_START_ADJUST 30 // cycles
75 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
76 #elif SELECT_SERIAL_SPEED == 2
78 #define SERIAL_DELAY 12 // micro sec
79 #define READ_WRITE_START_ADJUST 30 // cycles
80 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
81 #elif SELECT_SERIAL_SPEED == 3
83 #define SERIAL_DELAY 24 // micro sec
84 #define READ_WRITE_START_ADJUST 30 // cycles
85 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
86 #elif SELECT_SERIAL_SPEED == 4
88 #define SERIAL_DELAY 50 // micro sec
89 #define READ_WRITE_START_ADJUST 30 // cycles
90 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
92 #error Illegal Serial Speed
96 #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
97 #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
99 #define SLAVE_INT_WIDTH_US 1
100 #ifndef SERIAL_USE_MULTI_TRANSACTION
101 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
103 #define SLAVE_INT_ACK_WIDTH_UNIT 2
104 #define SLAVE_INT_ACK_WIDTH 4
107 static SSTD_t *Transaction_table = NULL;
110 void serial_delay(void) {
111 _delay_us(SERIAL_DELAY);
115 void serial_delay_half1(void) {
116 _delay_us(SERIAL_DELAY_HALF1);
120 void serial_delay_half2(void) {
121 _delay_us(SERIAL_DELAY_HALF2);
124 inline static void serial_output(void) ALWAYS_INLINE;
126 void serial_output(void) {
127 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
130 // make the serial pin an input with pull-up resistor
131 inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
133 void serial_input_with_pullup(void) {
134 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
135 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
139 uint8_t serial_read_pin(void) {
140 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
143 inline static void serial_low(void) ALWAYS_INLINE;
145 void serial_low(void) {
146 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
149 inline static void serial_high(void) ALWAYS_INLINE;
151 void serial_high(void) {
152 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
155 void soft_serial_initiator_init(SSTD_t *sstd_table)
157 Transaction_table = sstd_table;
162 void soft_serial_target_init(SSTD_t *sstd_table)
164 Transaction_table = sstd_table;
165 serial_input_with_pullup();
167 #if SERIAL_PIN_MASK == _BV(PD0)
170 // Trigger on falling edge of INT0
171 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
172 #elif SERIAL_PIN_MASK == _BV(PD2)
175 // Trigger on falling edge of INT2
176 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
178 #error unknown SERIAL_PIN_MASK value
182 // Used by the sender to synchronize timing with the reciver.
183 static void sync_recv(void) NO_INLINE;
185 void sync_recv(void) {
186 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
188 // This shouldn't hang if the target disconnects because the
189 // serial line will float to high if the target does disconnect.
190 while (!serial_read_pin());
193 // Used by the reciver to send a synchronization signal to the sender.
194 static void sync_send(void)NO_INLINE;
196 void sync_send(void) {
202 // Reads a byte from the serial line
203 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
204 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
205 uint8_t byte, i, p, pb;
207 _delay_sub_us(READ_WRITE_START_ADJUST);
208 for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
209 serial_delay_half1(); // read the middle of pulses
210 if( serial_read_pin() ) {
211 byte = (byte << 1) | 1; p ^= 1;
213 byte = (byte << 1) | 0; p ^= 0;
215 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
216 serial_delay_half2();
218 /* recive parity bit */
219 serial_delay_half1(); // read the middle of pulses
220 pb = serial_read_pin();
221 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
222 serial_delay_half2();
224 *pterrcount += (p != pb)? 1 : 0;
229 // Sends a byte with MSB ordering
230 void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
231 void serial_write_chunk(uint8_t data, uint8_t bit) {
233 for( p = 0, b = 1<<(bit-1); b ; b >>= 1) {
235 serial_high(); p ^= 1;
237 serial_low(); p ^= 0;
241 /* send parity bit */
242 if(p & 1) { serial_high(); }
243 else { serial_low(); }
246 serial_low(); // sync_send() / senc_recv() need raise edge
249 static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
251 void serial_send_packet(uint8_t *buffer, uint8_t size) {
252 for (uint8_t i = 0; i < size; ++i) {
256 serial_write_chunk(data,8);
260 static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
262 uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
264 for (uint8_t i = 0; i < size; ++i) {
267 data = serial_read_chunk(&pecount, 8);
274 void change_sender2reciver(void) {
276 serial_delay_half1(); //1
278 serial_input_with_pullup(); //2
279 serial_delay_half1(); //3
283 void change_reciver2sender(void) {
288 serial_delay_half1(); //4
291 // interrupt handle to be used by the target device
292 ISR(SERIAL_PIN_INTERRUPT) {
294 #ifndef SERIAL_USE_MULTI_TRANSACTION
297 SSTD_t *trans = Transaction_table;
299 // recive transaction table index
303 tid = serial_read_chunk(&pecount,4);
306 serial_delay_half1();
308 serial_high(); // response step1 low->high
310 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
311 SSTD_t *trans = &Transaction_table[tid];
312 serial_low(); // response step2 ack high->low
316 if( trans->target2initiator_buffer_size > 0 )
317 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
318 trans->target2initiator_buffer_size);
319 // target switch to input
320 change_sender2reciver();
322 // target recive phase
323 if( trans->initiator2target_buffer_size > 0 ) {
324 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
325 trans->initiator2target_buffer_size) ) {
326 *trans->status = TRANSACTION_ACCEPTED;
328 *trans->status = TRANSACTION_DATA_ERROR;
331 *trans->status = TRANSACTION_ACCEPTED;
334 sync_recv(); //weit initiator output to high
338 // start transaction by initiator
340 // int soft_serial_transaction(int sstd_index)
344 // TRANSACTION_NO_RESPONSE
345 // TRANSACTION_DATA_ERROR
346 // this code is very time dependent, so we need to disable interrupts
347 #ifndef SERIAL_USE_MULTI_TRANSACTION
348 int soft_serial_transaction(void) {
349 SSTD_t *trans = Transaction_table;
351 int soft_serial_transaction(int sstd_index) {
352 SSTD_t *trans = &Transaction_table[sstd_index];
356 // signal to the target that we want to start a transaction
359 _delay_us(SLAVE_INT_WIDTH_US);
361 #ifndef SERIAL_USE_MULTI_TRANSACTION
362 // wait for the target response
363 serial_input_with_pullup();
364 _delay_us(SLAVE_INT_RESPONSE_TIME);
366 // check if the target is present
367 if (serial_read_pin()) {
368 // target failed to pull the line low, assume not present
371 *trans->status = TRANSACTION_NO_RESPONSE;
373 return TRANSACTION_NO_RESPONSE;
377 // send transaction table index
379 _delay_sub_us(TID_SEND_ADJUST);
380 serial_write_chunk(sstd_index, 4);
381 serial_delay_half1();
383 // wait for the target response (step1 low->high)
384 serial_input_with_pullup();
385 while( !serial_read_pin() ) {
389 // check if the target is present (step2 high->low)
390 for( int i = 0; serial_read_pin(); i++ ) {
391 if (i > SLAVE_INT_ACK_WIDTH + 1) {
392 // slave failed to pull the line low, assume not present
395 *trans->status = TRANSACTION_NO_RESPONSE;
397 return TRANSACTION_NO_RESPONSE;
399 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
403 // initiator recive phase
404 // if the target is present syncronize with it
405 if( trans->target2initiator_buffer_size > 0 ) {
406 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
407 trans->target2initiator_buffer_size) ) {
410 *trans->status = TRANSACTION_DATA_ERROR;
412 return TRANSACTION_DATA_ERROR;
416 // initiator switch to output
417 change_reciver2sender();
419 // initiator send phase
420 if( trans->initiator2target_buffer_size > 0 ) {
421 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
422 trans->initiator2target_buffer_size);
425 // always, release the line when not in use
428 *trans->status = TRANSACTION_END;
430 return TRANSACTION_END;
433 #ifdef SERIAL_USE_MULTI_TRANSACTION
434 int soft_serial_get_and_clean_status(int sstd_index) {
435 SSTD_t *trans = &Transaction_table[sstd_index];
437 int retval = *trans->status;