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>
18 //#ifndef USE_SERIAL_PD2
20 #ifndef SERIAL_USE_MULTI_TRANSACTION
21 /* --- USE Simple API (OLD API, compatible with let's split serial.c) */
22 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
23 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
25 #if SERIAL_MASTER_BUFFER_LENGTH > 0
26 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
28 uint8_t volatile status0 = 0;
30 SSTD_t transactions[] = {
31 { (uint8_t *)&status0,
32 #if SERIAL_MASTER_BUFFER_LENGTH > 0
33 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
37 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
38 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
45 void serial_master_init(void)
46 { soft_serial_initiator_init(transactions); }
48 void serial_slave_init(void)
49 { soft_serial_target_init(transactions); }
52 // 1 => slave did not respond
53 // 2 => checksum error
54 int serial_update_buffers()
55 { return soft_serial_transaction(); }
57 #endif // Simple API (OLD API, compatible with let's split serial.c)
59 #define ALWAYS_INLINE __attribute__((always_inline))
60 #define NO_INLINE __attribute__((noinline))
61 #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
63 // Serial pulse period in microseconds.
64 #define TID_SEND_ADJUST 14
66 #define SELECT_SERIAL_SPEED 1
67 #if SELECT_SERIAL_SPEED == 0
69 #define SERIAL_DELAY 4 // micro sec
70 #define READ_WRITE_START_ADJUST 33 // cycles
71 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
72 #elif SELECT_SERIAL_SPEED == 1
74 #define SERIAL_DELAY 6 // micro sec
75 #define READ_WRITE_START_ADJUST 30 // cycles
76 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
77 #elif SELECT_SERIAL_SPEED == 2
79 #define SERIAL_DELAY 12 // micro sec
80 #define READ_WRITE_START_ADJUST 30 // cycles
81 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
82 #elif SELECT_SERIAL_SPEED == 3
84 #define SERIAL_DELAY 24 // micro sec
85 #define READ_WRITE_START_ADJUST 30 // cycles
86 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
87 #elif SELECT_SERIAL_SPEED == 4
89 #define SERIAL_DELAY 50 // micro sec
90 #define READ_WRITE_START_ADJUST 30 // cycles
91 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
93 #error Illegal Serial Speed
97 #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
98 #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
100 #define SLAVE_INT_WIDTH_US 1
101 #ifndef SERIAL_USE_MULTI_TRANSACTION
102 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
104 #define SLAVE_INT_ACK_WIDTH_UNIT 2
105 #define SLAVE_INT_ACK_WIDTH 4
108 static SSTD_t *Transaction_table = NULL;
111 void serial_delay(void) {
112 _delay_us(SERIAL_DELAY);
116 void serial_delay_half1(void) {
117 _delay_us(SERIAL_DELAY_HALF1);
121 void serial_delay_half2(void) {
122 _delay_us(SERIAL_DELAY_HALF2);
125 inline static void serial_output(void) ALWAYS_INLINE;
127 void serial_output(void) {
128 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
131 // make the serial pin an input with pull-up resistor
132 inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
134 void serial_input_with_pullup(void) {
135 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
136 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
140 uint8_t serial_read_pin(void) {
141 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
144 inline static void serial_low(void) ALWAYS_INLINE;
146 void serial_low(void) {
147 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
150 inline static void serial_high(void) ALWAYS_INLINE;
152 void serial_high(void) {
153 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
156 void soft_serial_initiator_init(SSTD_t *sstd_table)
158 Transaction_table = sstd_table;
163 void soft_serial_target_init(SSTD_t *sstd_table)
165 Transaction_table = sstd_table;
166 serial_input_with_pullup();
168 #if SERIAL_PIN_MASK == _BV(PD0)
171 // Trigger on falling edge of INT0
172 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
173 #elif SERIAL_PIN_MASK == _BV(PD2)
176 // Trigger on falling edge of INT2
177 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
179 #error unknown SERIAL_PIN_MASK value
183 // Used by the sender to synchronize timing with the reciver.
184 static void sync_recv(void) NO_INLINE;
186 void sync_recv(void) {
187 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
189 // This shouldn't hang if the target disconnects because the
190 // serial line will float to high if the target does disconnect.
191 while (!serial_read_pin());
194 // Used by the reciver to send a synchronization signal to the sender.
195 static void sync_send(void)NO_INLINE;
197 void sync_send(void) {
203 // Reads a byte from the serial line
204 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
205 static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
206 uint8_t byte, i, p, pb;
208 _delay_sub_us(READ_WRITE_START_ADJUST);
209 for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
210 serial_delay_half1(); // read the middle of pulses
211 if( serial_read_pin() ) {
212 byte = (byte << 1) | 1; p ^= 1;
214 byte = (byte << 1) | 0; p ^= 0;
216 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
217 serial_delay_half2();
219 /* recive parity bit */
220 serial_delay_half1(); // read the middle of pulses
221 pb = serial_read_pin();
222 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
223 serial_delay_half2();
225 *pterrcount += (p != pb)? 1 : 0;
230 // Sends a byte with MSB ordering
231 void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
232 void serial_write_chunk(uint8_t data, uint8_t bit) {
234 for( p = 0, b = 1<<(bit-1); b ; b >>= 1) {
236 serial_high(); p ^= 1;
238 serial_low(); p ^= 0;
242 /* send parity bit */
243 if(p & 1) { serial_high(); }
244 else { serial_low(); }
247 serial_low(); // sync_send() / senc_recv() need raise edge
250 static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
252 void serial_send_packet(uint8_t *buffer, uint8_t size) {
253 for (uint8_t i = 0; i < size; ++i) {
257 serial_write_chunk(data,8);
261 static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
263 uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
265 for (uint8_t i = 0; i < size; ++i) {
268 data = serial_read_chunk(&pecount, 8);
275 void change_sender2reciver(void) {
277 serial_delay_half1(); //1
279 serial_input_with_pullup(); //2
280 serial_delay_half1(); //3
284 void change_reciver2sender(void) {
289 serial_delay_half1(); //4
292 // interrupt handle to be used by the target device
293 ISR(SERIAL_PIN_INTERRUPT) {
295 #ifndef SERIAL_USE_MULTI_TRANSACTION
298 SSTD_t *trans = Transaction_table;
300 // recive transaction table index
304 tid = serial_read_chunk(&pecount,4);
307 serial_delay_half1();
309 serial_high(); // response step1 low->high
311 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
312 SSTD_t *trans = &Transaction_table[tid];
313 serial_low(); // response step2 ack high->low
317 if( trans->target2initiator_buffer_size > 0 )
318 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
319 trans->target2initiator_buffer_size);
320 // target switch to input
321 change_sender2reciver();
323 // target recive phase
324 if( trans->initiator2target_buffer_size > 0 ) {
325 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
326 trans->initiator2target_buffer_size) ) {
327 *trans->status = TRANSACTION_ACCEPTED;
329 *trans->status = TRANSACTION_DATA_ERROR;
332 *trans->status = TRANSACTION_ACCEPTED;
335 sync_recv(); //weit initiator output to high
339 // start transaction by initiator
341 // int soft_serial_transaction(int sstd_index)
345 // TRANSACTION_NO_RESPONSE
346 // TRANSACTION_DATA_ERROR
347 // this code is very time dependent, so we need to disable interrupts
348 #ifndef SERIAL_USE_MULTI_TRANSACTION
349 int soft_serial_transaction(void) {
350 SSTD_t *trans = Transaction_table;
352 int soft_serial_transaction(int sstd_index) {
353 SSTD_t *trans = &Transaction_table[sstd_index];
357 // signal to the target that we want to start a transaction
360 _delay_us(SLAVE_INT_WIDTH_US);
362 #ifndef SERIAL_USE_MULTI_TRANSACTION
363 // wait for the target response
364 serial_input_with_pullup();
365 _delay_us(SLAVE_INT_RESPONSE_TIME);
367 // check if the target is present
368 if (serial_read_pin()) {
369 // target failed to pull the line low, assume not present
372 *trans->status = TRANSACTION_NO_RESPONSE;
374 return TRANSACTION_NO_RESPONSE;
378 // send transaction table index
380 _delay_sub_us(TID_SEND_ADJUST);
381 serial_write_chunk(sstd_index, 4);
382 serial_delay_half1();
384 // wait for the target response (step1 low->high)
385 serial_input_with_pullup();
386 while( !serial_read_pin() ) {
390 // check if the target is present (step2 high->low)
391 for( int i = 0; serial_read_pin(); i++ ) {
392 if (i > SLAVE_INT_ACK_WIDTH + 1) {
393 // slave failed to pull the line low, assume not present
396 *trans->status = TRANSACTION_NO_RESPONSE;
398 return TRANSACTION_NO_RESPONSE;
400 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
404 // initiator recive phase
405 // if the target is present syncronize with it
406 if( trans->target2initiator_buffer_size > 0 ) {
407 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
408 trans->target2initiator_buffer_size) ) {
411 *trans->status = TRANSACTION_DATA_ERROR;
413 return TRANSACTION_DATA_ERROR;
417 // initiator switch to output
418 change_reciver2sender();
420 // initiator send phase
421 if( trans->initiator2target_buffer_size > 0 ) {
422 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
423 trans->initiator2target_buffer_size);
426 // always, release the line when not in use
429 *trans->status = TRANSACTION_END;
431 return TRANSACTION_END;
434 #ifdef SERIAL_USE_MULTI_TRANSACTION
435 int soft_serial_get_and_clean_status(int sstd_index) {
436 SSTD_t *trans = &Transaction_table[sstd_index];
438 int retval = *trans->status;