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);
33 void serial_output(void) {
34 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
37 // make the serial pin an input with pull-up resistor
39 void serial_input(void) {
40 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
41 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
45 uint8_t serial_read_pin(void) {
46 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
50 void serial_low(void) {
51 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
55 void serial_high(void) {
56 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
59 void serial_master_init(void) {
64 void serial_slave_init(void) {
69 // Trigger on falling edge of INT0
70 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
73 // Used by the master to synchronize timing with the slave.
75 void sync_recv(void) {
77 // This shouldn't hang if the slave disconnects because the
78 // serial line will float to high if the slave does disconnect.
79 while (!serial_read_pin());
83 // Used by the slave to send a synchronization signal to the master.
85 void sync_send(void) {
94 // Reads a byte from the serial line
96 uint8_t serial_read_byte(void) {
99 for ( uint8_t i = 0; i < 8; ++i) {
100 byte = (byte << 1) | serial_read_pin();
108 // Sends a byte with MSB ordering
110 void serial_write_byte(uint8_t data) {
114 if(data & (1 << b)) {
123 // interrupt handle to be used by the slave device
124 ISR(SERIAL_PIN_INTERRUPT) {
127 uint8_t checksum = 0;
128 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
129 serial_write_byte(serial_slave_buffer[i]);
131 checksum += serial_slave_buffer[i];
133 serial_write_byte(checksum);
136 // wait for the sync to finish sending
139 // read the middle of pulses
140 _delay_us(SERIAL_DELAY/2);
142 uint8_t checksum_computed = 0;
143 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
144 serial_master_buffer[i] = serial_read_byte();
146 checksum_computed += serial_master_buffer[i];
148 uint8_t checksum_received = serial_read_byte();
151 serial_input(); // end transaction
153 if ( checksum_computed != checksum_received ) {
154 status |= SLAVE_DATA_CORRUPT;
156 status &= ~SLAVE_DATA_CORRUPT;
161 bool serial_slave_DATA_CORRUPT(void) {
162 return status & SLAVE_DATA_CORRUPT;
165 // Copies the serial_slave_buffer to the master and sends the
166 // serial_master_buffer to the slave.
170 // 1 => slave did not respond
171 int serial_update_buffers(void) {
172 // this code is very time dependent, so we need to disable interrupts
175 // signal to the slave that we want to start a transaction
180 // wait for the slaves response
183 _delay_us(SERIAL_DELAY);
185 // check if the slave is present
186 if (serial_read_pin()) {
187 // slave failed to pull the line low, assume not present
192 // if the slave is present syncronize with it
195 uint8_t checksum_computed = 0;
196 // receive data from the slave
197 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
198 serial_slave_buffer[i] = serial_read_byte();
200 checksum_computed += serial_slave_buffer[i];
202 uint8_t checksum_received = serial_read_byte();
205 if (checksum_computed != checksum_received) {
210 uint8_t checksum = 0;
211 // send data to the slave
212 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
213 serial_write_byte(serial_master_buffer[i]);
215 checksum += serial_master_buffer[i];
217 serial_write_byte(checksum);
220 // always, release the line when not in use