2 * WARNING: be careful changing this code, it is very timing dependent
10 #include <avr/interrupt.h>
11 #include <util/delay.h>
17 #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
19 // Serial pulse period in microseconds.
20 #define SELECT_SERIAL_SPEED 1
21 #if SELECT_SERIAL_SPEED == 0
23 #define SERIAL_DELAY 4 // micro sec
24 #define READ_WRITE_START_ADJUST 30 // cycles
25 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
26 #elif SELECT_SERIAL_SPEED == 1
28 #define SERIAL_DELAY 6 // micro sec
29 #define READ_WRITE_START_ADJUST 23 // cycles
30 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
31 #elif SELECT_SERIAL_SPEED == 2
33 #define SERIAL_DELAY 12 // micro sec
34 #define READ_WRITE_START_ADJUST 25 // cycles
35 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
36 #elif SELECT_SERIAL_SPEED == 3
38 #define SERIAL_DELAY 24 // micro sec
39 #define READ_WRITE_START_ADJUST 25 // cycles
40 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
41 #elif SELECT_SERIAL_SPEED == 4
43 #define SERIAL_DELAY 50 // micro sec
44 #define READ_WRITE_START_ADJUST 25 // cycles
45 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
47 #error Illegal Serial Speed
51 #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
52 #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
54 #define SLAVE_INT_WIDTH 1
55 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
57 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
58 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
60 #define SLAVE_DATA_CORRUPT (1<<0)
61 volatile uint8_t status = 0;
64 void serial_delay(void) {
65 _delay_us(SERIAL_DELAY);
69 void serial_delay_half1(void) {
70 _delay_us(SERIAL_DELAY_HALF1);
74 void serial_delay_half2(void) {
75 _delay_us(SERIAL_DELAY_HALF2);
79 void serial_output(void) {
80 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
83 // make the serial pin an input with pull-up resistor
85 void serial_input_with_pullup(void) {
86 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
87 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
91 uint8_t serial_read_pin(void) {
92 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
96 void serial_low(void) {
97 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
101 void serial_high(void) {
102 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
105 void serial_master_init(void) {
110 void serial_slave_init(void) {
111 serial_input_with_pullup();
116 // Trigger on falling edge of INT3
117 EICRA &= ~(_BV(ISC30) | _BV(ISC31));
121 // Used by the sender to synchronize timing with the reciver.
123 void sync_recv(void) {
124 for (int i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
126 // This shouldn't hang if the slave disconnects because the
127 // serial line will float to high if the slave does disconnect.
128 while (!serial_read_pin());
131 // Used by the reciver to send a synchronization signal to the sender.
133 void sync_send(void) {
139 // Reads a byte from the serial line
141 uint8_t serial_read_byte(void) {
143 _delay_sub_us(READ_WRITE_START_ADJUST);
144 for ( uint8_t i = 0; i < 8; ++i) {
145 serial_delay_half1(); // read the middle of pulses
146 byte = (byte << 1) | serial_read_pin();
147 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
148 serial_delay_half2();
153 // Sends a byte with MSB ordering
155 void serial_write_byte(uint8_t data) {
166 serial_low(); // sync_send() / senc_recv() need raise edge
169 // interrupt handle to be used by the slave device
170 ISR(SERIAL_PIN_INTERRUPT) {
174 uint8_t checksum = 0;
175 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
177 serial_write_byte(serial_slave_buffer[i]);
178 checksum += serial_slave_buffer[i];
181 serial_write_byte(checksum);
183 // slave switch to input
185 serial_delay_half1(); //1
187 serial_input_with_pullup(); //2
188 serial_delay_half1(); //3
190 // slave recive phase
191 uint8_t checksum_computed = 0;
192 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
194 serial_master_buffer[i] = serial_read_byte();
195 checksum_computed += serial_master_buffer[i];
198 uint8_t checksum_received = serial_read_byte();
200 if ( checksum_computed != checksum_received ) {
201 status |= SLAVE_DATA_CORRUPT;
203 status &= ~SLAVE_DATA_CORRUPT;
206 sync_recv(); //weit master output to high
210 bool serial_slave_DATA_CORRUPT(void) {
211 return status & SLAVE_DATA_CORRUPT;
214 // Copies the serial_slave_buffer to the master and sends the
215 // serial_master_buffer to the slave.
219 // 1 => slave did not respond
220 // 2 => checksum error
221 int serial_update_buffers(void) {
222 // this code is very time dependent, so we need to disable interrupts
225 // signal to the slave that we want to start a transaction
228 _delay_us(SLAVE_INT_WIDTH);
230 // wait for the slaves response
231 serial_input_with_pullup();
232 _delay_us(SLAVE_INT_RESPONSE_TIME);
234 // check if the slave is present
235 if (serial_read_pin()) {
236 // slave failed to pull the line low, assume not present
243 // master recive phase
244 // if the slave is present syncronize with it
246 uint8_t checksum_computed = 0;
247 // receive data from the slave
248 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
250 serial_slave_buffer[i] = serial_read_byte();
251 checksum_computed += serial_slave_buffer[i];
254 uint8_t checksum_received = serial_read_byte();
256 if (checksum_computed != checksum_received) {
263 // master switch to output
267 serial_output(); // 3
268 serial_delay_half1(); //4
271 uint8_t checksum = 0;
273 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
275 serial_write_byte(serial_master_buffer[i]);
276 checksum += serial_master_buffer[i];
279 serial_write_byte(checksum);
281 // always, release the line when not in use