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 matrix_row_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
22 matrix_row_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
24 #define ROW_MASK (((matrix_row_t)0-1)>>(8*sizeof(matrix_row_t)-MATRIX_COLS))
26 #define SLAVE_DATA_CORRUPT (1<<0)
27 volatile uint8_t status = 0;
30 void serial_delay(void) {
31 _delay_us(SERIAL_DELAY);
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 matrix_row_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) {
71 // Trigger on falling edge of INT0
72 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
75 // Used by the master to synchronize timing with the slave.
77 void sync_recv(void) {
79 // This shouldn't hang if the slave disconnects because the
80 // serial line will float to high if the slave does disconnect.
81 while (!serial_read_pin());
85 // Used by the slave to send a synchronization signal to the master.
87 void sync_send(void) {
96 // Reads a byte from the serial line
98 matrix_row_t serial_read_byte(void) {
99 matrix_row_t byte = 0;
101 for ( uint8_t i = 0; i < MATRIX_COLS; ++i) {
102 byte = (byte << 1) | serial_read_pin();
110 // Sends a byte with MSB ordering
112 void serial_write_byte(matrix_row_t data) {
113 matrix_row_t b = MATRIX_COLS;
116 if(data & (1UL << b)) {
125 // interrupt handle to be used by the slave device
126 ISR(SERIAL_PIN_INTERRUPT) {
129 matrix_row_t checksum = 0;
130 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
131 serial_write_byte(serial_slave_buffer[i]);
133 checksum += ROW_MASK & serial_slave_buffer[i];
135 serial_write_byte(checksum);
138 // wait for the sync to finish sending
141 // read the middle of pulses
142 _delay_us(SERIAL_DELAY/2);
144 matrix_row_t checksum_computed = 0;
145 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
146 serial_master_buffer[i] = serial_read_byte();
148 checksum_computed += ROW_MASK & serial_master_buffer[i];
150 matrix_row_t checksum_received = serial_read_byte();
153 serial_input(); // end transaction
155 if ( checksum_computed != checksum_received ) {
156 status |= SLAVE_DATA_CORRUPT;
158 status &= ~SLAVE_DATA_CORRUPT;
163 bool serial_slave_DATA_CORRUPT(void) {
164 return status & SLAVE_DATA_CORRUPT;
167 // Copies the serial_slave_buffer to the master and sends the
168 // serial_master_buffer to the slave.
172 // 1 => slave did not respond
173 int serial_update_buffers(void) {
174 // this code is very time dependent, so we need to disable interrupts
177 // signal to the slave that we want to start a transaction
182 // wait for the slaves response
185 _delay_us(SERIAL_DELAY);
187 // check if the slave is present
188 if (serial_read_pin()) {
189 // slave failed to pull the line low, assume not present
194 // if the slave is present syncronize with it
197 matrix_row_t checksum_computed = 0;
198 // receive data from the slave
199 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
200 serial_slave_buffer[i] = serial_read_byte();
202 checksum_computed += ROW_MASK & serial_slave_buffer[i];
204 matrix_row_t checksum_received = serial_read_byte();
207 if (checksum_computed != checksum_received) {
212 matrix_row_t checksum = 0;
213 // send data to the slave
214 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
215 serial_write_byte(serial_master_buffer[i]);
217 checksum += ROW_MASK & serial_master_buffer[i];
219 serial_write_byte(checksum);
222 // always, release the line when not in use