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