2 * WARNING: be careful changing this code, it is very timing dependent
10 #include <avr/interrupt.h>
11 #include <util/delay.h>
16 // Serial pulse period in microseconds. Its probably a bad idea to lower this
18 #define SERIAL_DELAY 24
20 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
21 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
23 #define SLAVE_DATA_CORRUPT (1<<0)
24 volatile uint8_t status = 0;
27 void serial_delay(void) {
28 _delay_us(SERIAL_DELAY);
32 void serial_output(void) {
33 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
36 // make the serial pin an input with pull-up resistor
38 void serial_input(void) {
39 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
40 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
44 uint8_t serial_read_pin(void) {
45 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
49 void serial_low(void) {
50 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
54 void serial_high(void) {
55 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
58 void serial_master_init(void) {
63 void serial_slave_init(void) {
68 // Trigger on falling edge of INT0
69 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
72 // Used by the master to synchronize timing with the slave.
74 void sync_recv(void) {
76 // This shouldn't hang if the slave disconnects because the
77 // serial line will float to high if the slave does disconnect.
78 while (!serial_read_pin());
82 // Used by the slave to send a synchronization signal to the master.
84 void sync_send(void) {
93 // Reads a byte from the serial line
95 uint8_t serial_read_byte(void) {
98 for ( uint8_t i = 0; i < 8; ++i) {
99 byte = (byte << 1) | serial_read_pin();
107 // Sends a byte with MSB ordering
109 void serial_write_byte(uint8_t data) {
113 if(data & (1 << b)) {
122 // interrupt handle to be used by the slave device
123 ISR(SERIAL_PIN_INTERRUPT) {
126 uint8_t checksum = 0;
127 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
128 serial_write_byte(serial_slave_buffer[i]);
130 checksum += serial_slave_buffer[i];
132 serial_write_byte(checksum);
135 // wait for the sync to finish sending
138 // read the middle of pulses
139 _delay_us(SERIAL_DELAY/2);
141 uint8_t checksum_computed = 0;
142 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
143 serial_master_buffer[i] = serial_read_byte();
145 checksum_computed += serial_master_buffer[i];
147 uint8_t checksum_received = serial_read_byte();
150 serial_input(); // end transaction
152 if ( checksum_computed != checksum_received ) {
153 status |= SLAVE_DATA_CORRUPT;
155 status &= ~SLAVE_DATA_CORRUPT;
160 bool serial_slave_DATA_CORRUPT(void) {
161 return status & SLAVE_DATA_CORRUPT;
164 // Copies the serial_slave_buffer to the master and sends the
165 // serial_master_buffer to the slave.
169 // 1 => slave did not respond
170 int serial_update_buffers(void) {
171 // this code is very time dependent, so we need to disable interrupts
174 // signal to the slave that we want to start a transaction
179 // wait for the slaves response
182 _delay_us(SERIAL_DELAY);
184 // check if the slave is present
185 if (serial_read_pin()) {
186 // slave failed to pull the line low, assume not present
191 // if the slave is present syncronize with it
194 uint8_t checksum_computed = 0;
195 // receive data from the slave
196 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
197 serial_slave_buffer[i] = serial_read_byte();
199 checksum_computed += serial_slave_buffer[i];
201 uint8_t checksum_received = serial_read_byte();
204 if (checksum_computed != checksum_received) {
209 uint8_t checksum = 0;
210 // send data to the slave
211 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
212 serial_write_byte(serial_master_buffer[i]);
214 checksum += serial_master_buffer[i];
216 serial_write_byte(checksum);
219 // always, release the line when not in use