diff options
Diffstat (limited to 'keyboards/keebio/bfo9000/serial.c')
-rw-r--r-- | keyboards/keebio/bfo9000/serial.c | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/keyboards/keebio/bfo9000/serial.c b/keyboards/keebio/bfo9000/serial.c new file mode 100644 index 000000000..fea57b651 --- /dev/null +++ b/keyboards/keebio/bfo9000/serial.c | |||
@@ -0,0 +1,230 @@ | |||
1 | /* | ||
2 | * WARNING: be careful changing this code, it is very timing dependent | ||
3 | */ | ||
4 | |||
5 | #ifndef F_CPU | ||
6 | #define F_CPU 16000000 | ||
7 | #endif | ||
8 | |||
9 | #include <avr/io.h> | ||
10 | #include <avr/interrupt.h> | ||
11 | #include <util/delay.h> | ||
12 | #include <stdbool.h> | ||
13 | #include "serial.h" | ||
14 | |||
15 | #ifndef USE_I2C | ||
16 | |||
17 | // Serial pulse period in microseconds. Its probably a bad idea to lower this | ||
18 | // value. | ||
19 | #define SERIAL_DELAY 24 | ||
20 | |||
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}; | ||
23 | |||
24 | #define ROW_MASK (((matrix_row_t)0-1)>>(8*sizeof(matrix_row_t)-MATRIX_COLS)) | ||
25 | |||
26 | #define SLAVE_DATA_CORRUPT (1<<0) | ||
27 | volatile uint8_t status = 0; | ||
28 | |||
29 | inline static | ||
30 | void serial_delay(void) { | ||
31 | _delay_us(SERIAL_DELAY); | ||
32 | } | ||
33 | |||
34 | inline static | ||
35 | void serial_output(void) { | ||
36 | SERIAL_PIN_DDR |= SERIAL_PIN_MASK; | ||
37 | } | ||
38 | |||
39 | // make the serial pin an input with pull-up resistor | ||
40 | inline static | ||
41 | void serial_input(void) { | ||
42 | SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; | ||
43 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | ||
44 | } | ||
45 | |||
46 | inline static | ||
47 | matrix_row_t serial_read_pin(void) { | ||
48 | return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); | ||
49 | } | ||
50 | |||
51 | inline static | ||
52 | void serial_low(void) { | ||
53 | SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; | ||
54 | } | ||
55 | |||
56 | inline static | ||
57 | void serial_high(void) { | ||
58 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | ||
59 | } | ||
60 | |||
61 | void serial_master_init(void) { | ||
62 | serial_output(); | ||
63 | serial_high(); | ||
64 | } | ||
65 | |||
66 | void serial_slave_init(void) { | ||
67 | serial_input(); | ||
68 | |||
69 | // Enable INT0 | ||
70 | EIMSK |= _BV(INT0); | ||
71 | // Trigger on falling edge of INT0 | ||
72 | EICRA &= ~(_BV(ISC00) | _BV(ISC01)); | ||
73 | } | ||
74 | |||
75 | // Used by the master to synchronize timing with the slave. | ||
76 | static | ||
77 | void sync_recv(void) { | ||
78 | serial_input(); | ||
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()); | ||
82 | serial_delay(); | ||
83 | } | ||
84 | |||
85 | // Used by the slave to send a synchronization signal to the master. | ||
86 | static | ||
87 | void sync_send(void) { | ||
88 | serial_output(); | ||
89 | |||
90 | serial_low(); | ||
91 | serial_delay(); | ||
92 | |||
93 | serial_high(); | ||
94 | } | ||
95 | |||
96 | // Reads a byte from the serial line | ||
97 | static | ||
98 | matrix_row_t serial_read_byte(void) { | ||
99 | matrix_row_t byte = 0; | ||
100 | serial_input(); | ||
101 | for ( uint8_t i = 0; i < MATRIX_COLS; ++i) { | ||
102 | byte = (byte << 1) | serial_read_pin(); | ||
103 | serial_delay(); | ||
104 | _delay_us(1); | ||
105 | } | ||
106 | |||
107 | return byte; | ||
108 | } | ||
109 | |||
110 | // Sends a byte with MSB ordering | ||
111 | static | ||
112 | void serial_write_byte(matrix_row_t data) { | ||
113 | matrix_row_t b = MATRIX_COLS; | ||
114 | serial_output(); | ||
115 | while( b-- ) { | ||
116 | if(data & (1UL << b)) { | ||
117 | serial_high(); | ||
118 | } else { | ||
119 | serial_low(); | ||
120 | } | ||
121 | serial_delay(); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | // interrupt handle to be used by the slave device | ||
126 | ISR(SERIAL_PIN_INTERRUPT) { | ||
127 | sync_send(); | ||
128 | |||
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]); | ||
132 | sync_send(); | ||
133 | checksum += ROW_MASK & serial_slave_buffer[i]; | ||
134 | } | ||
135 | serial_write_byte(checksum); | ||
136 | sync_send(); | ||
137 | |||
138 | // wait for the sync to finish sending | ||
139 | serial_delay(); | ||
140 | |||
141 | // read the middle of pulses | ||
142 | _delay_us(SERIAL_DELAY/2); | ||
143 | |||
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(); | ||
147 | sync_send(); | ||
148 | checksum_computed += ROW_MASK & serial_master_buffer[i]; | ||
149 | } | ||
150 | matrix_row_t checksum_received = serial_read_byte(); | ||
151 | sync_send(); | ||
152 | |||
153 | serial_input(); // end transaction | ||
154 | |||
155 | if ( checksum_computed != checksum_received ) { | ||
156 | status |= SLAVE_DATA_CORRUPT; | ||
157 | } else { | ||
158 | status &= ~SLAVE_DATA_CORRUPT; | ||
159 | } | ||
160 | } | ||
161 | |||
162 | inline | ||
163 | bool serial_slave_DATA_CORRUPT(void) { | ||
164 | return status & SLAVE_DATA_CORRUPT; | ||
165 | } | ||
166 | |||
167 | // Copies the serial_slave_buffer to the master and sends the | ||
168 | // serial_master_buffer to the slave. | ||
169 | // | ||
170 | // Returns: | ||
171 | // 0 => no error | ||
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 | ||
175 | cli(); | ||
176 | |||
177 | // signal to the slave that we want to start a transaction | ||
178 | serial_output(); | ||
179 | serial_low(); | ||
180 | _delay_us(1); | ||
181 | |||
182 | // wait for the slaves response | ||
183 | serial_input(); | ||
184 | serial_high(); | ||
185 | _delay_us(SERIAL_DELAY); | ||
186 | |||
187 | // check if the slave is present | ||
188 | if (serial_read_pin()) { | ||
189 | // slave failed to pull the line low, assume not present | ||
190 | sei(); | ||
191 | return 1; | ||
192 | } | ||
193 | |||
194 | // if the slave is present syncronize with it | ||
195 | sync_recv(); | ||
196 | |||
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(); | ||
201 | sync_recv(); | ||
202 | checksum_computed += ROW_MASK & serial_slave_buffer[i]; | ||
203 | } | ||
204 | matrix_row_t checksum_received = serial_read_byte(); | ||
205 | sync_recv(); | ||
206 | |||
207 | if (checksum_computed != checksum_received) { | ||
208 | sei(); | ||
209 | return 1; | ||
210 | } | ||
211 | |||
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]); | ||
216 | sync_recv(); | ||
217 | checksum += ROW_MASK & serial_master_buffer[i]; | ||
218 | } | ||
219 | serial_write_byte(checksum); | ||
220 | sync_recv(); | ||
221 | |||
222 | // always, release the line when not in use | ||
223 | serial_output(); | ||
224 | serial_high(); | ||
225 | |||
226 | sei(); | ||
227 | return 0; | ||
228 | } | ||
229 | |||
230 | #endif | ||