2 * WARNING: be careful changing this code, it is very timing dependent
10 #include <avr/interrupt.h>
11 #include <util/delay.h>
17 // Serial pulse period in microseconds. Its probably a bad idea to lower this
19 #define SERIAL_DELAY 24
21 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
22 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
24 #define SLAVE_DATA_CORRUPT (1<<0)
25 volatile uint8_t status = 0;
28 void serial_delay(void) {
29 _delay_us(SERIAL_DELAY);
31 void serial_delay_short(void) {
32 _delay_us(SERIAL_DELAY-1);
35 void serial_output(void) {
36 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
39 // make the serial pin an input with pull-up resistor
41 void serial_input(void) {
42 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
43 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
47 uint8_t serial_read_pin(void) {
48 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
52 void serial_low(void) {
53 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
57 void serial_high(void) {
58 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
61 void serial_master_init(void) {
66 void serial_slave_init(void) {
69 #ifndef USE_SERIAL_PD2
72 // Trigger on falling edge of INT0
73 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
77 // Trigger on falling edge of INT2
78 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
82 // Used by the master to synchronize timing with the slave.
84 void sync_recv(void) {
86 // This shouldn't hang if the slave disconnects because the
87 // serial line will float to high if the slave does disconnect.
88 while (!serial_read_pin());
90 _delay_us(SERIAL_DELAY-5);
93 // Used by the slave to send a synchronization signal to the master.
95 void sync_send(void) {
104 // Reads a byte from the serial line
106 uint8_t serial_read_byte(void) {
109 for ( uint8_t i = 0; i < 8; ++i) {
110 byte = (byte << 1) | serial_read_pin();
118 // Sends a byte with MSB ordering
120 void serial_write_byte(uint8_t data) {
124 if(data & (1 << b)) {
133 // interrupt handle to be used by the slave device
134 ISR(SERIAL_PIN_INTERRUPT) {
137 uint8_t checksum = 0;
138 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
139 serial_write_byte(serial_slave_buffer[i]);
141 checksum += serial_slave_buffer[i];
143 serial_write_byte(checksum);
146 // wait for the sync to finish sending
149 // read the middle of pulses
150 _delay_us(SERIAL_DELAY/2);
152 uint8_t checksum_computed = 0;
153 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
154 serial_master_buffer[i] = serial_read_byte();
156 checksum_computed += serial_master_buffer[i];
158 uint8_t checksum_received = serial_read_byte();
161 serial_input(); // end transaction
163 if ( checksum_computed != checksum_received ) {
164 status |= SLAVE_DATA_CORRUPT;
166 status &= ~SLAVE_DATA_CORRUPT;
171 bool serial_slave_DATA_CORRUPT(void) {
172 return status & SLAVE_DATA_CORRUPT;
175 // Copies the serial_slave_buffer to the master and sends the
176 // serial_master_buffer to the slave.
180 // 1 => slave did not respond
181 int serial_update_buffers(void) {
182 // this code is very time dependent, so we need to disable interrupts
185 // signal to the slave that we want to start a transaction
190 // wait for the slaves response
193 _delay_us(SERIAL_DELAY);
195 // check if the slave is present
196 if (serial_read_pin()) {
197 // slave failed to pull the line low, assume not present
202 // if the slave is present syncronize with it
205 uint8_t checksum_computed = 0;
206 // receive data from the slave
207 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
208 serial_slave_buffer[i] = serial_read_byte();
210 checksum_computed += serial_slave_buffer[i];
212 uint8_t checksum_received = serial_read_byte();
215 if (checksum_computed != checksum_received) {
220 uint8_t checksum = 0;
221 // send data to the slave
222 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
223 serial_write_byte(serial_master_buffer[i]);
225 checksum += serial_master_buffer[i];
227 serial_write_byte(checksum);
230 // always, release the line when not in use