aboutsummaryrefslogtreecommitdiff
path: root/keyboards/crkbd/serial.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/crkbd/serial.c')
-rw-r--r--keyboards/crkbd/serial.c452
1 files changed, 329 insertions, 123 deletions
diff --git a/keyboards/crkbd/serial.c b/keyboards/crkbd/serial.c
index e918ab6ee..11ceff0b3 100644
--- a/keyboards/crkbd/serial.c
+++ b/keyboards/crkbd/serial.c
@@ -9,36 +9,128 @@
9#include <avr/io.h> 9#include <avr/io.h>
10#include <avr/interrupt.h> 10#include <avr/interrupt.h>
11#include <util/delay.h> 11#include <util/delay.h>
12#include <stddef.h>
12#include <stdbool.h> 13#include <stdbool.h>
13#include "serial.h" 14#include "serial.h"
15//#include <pro_micro.h>
14 16
15#ifdef USE_SERIAL 17#ifdef USE_SERIAL
16 18
17// Serial pulse period in microseconds. Its probably a bad idea to lower this 19#ifndef SERIAL_USE_MULTI_TRANSACTION
18// value. 20/* --- USE Simple API (OLD API, compatible with let's split serial.c) */
19#define SERIAL_DELAY 24 21 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
22 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
23 #endif
24 #if SERIAL_MASTER_BUFFER_LENGTH > 0
25 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
26 #endif
27 uint8_t volatile status0 = 0;
28
29SSTD_t transactions[] = {
30 { (uint8_t *)&status0,
31 #if SERIAL_MASTER_BUFFER_LENGTH > 0
32 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
33 #else
34 0, (uint8_t *)NULL,
35 #endif
36 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
37 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
38 #else
39 0, (uint8_t *)NULL,
40 #endif
41 }
42};
43
44void serial_master_init(void)
45{ soft_serial_initiator_init(transactions); }
20 46
21uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; 47void serial_slave_init(void)
22uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; 48{ soft_serial_target_init(transactions); }
23 49
24#define SLAVE_DATA_CORRUPT (1<<0) 50// 0 => no error
25volatile uint8_t status = 0; 51// 1 => slave did not respond
52// 2 => checksum error
53int serial_update_buffers()
54{ return soft_serial_transaction(); }
55
56#endif // Simple API (OLD API, compatible with let's split serial.c)
57
58#define ALWAYS_INLINE __attribute__((always_inline))
59#define NO_INLINE __attribute__((noinline))
60#define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
61
62// Serial pulse period in microseconds.
63#define TID_SEND_ADJUST 14
64
65#define SELECT_SERIAL_SPEED 1
66#if SELECT_SERIAL_SPEED == 0
67 // Very High speed
68 #define SERIAL_DELAY 4 // micro sec
69 #define READ_WRITE_START_ADJUST 33 // cycles
70 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
71#elif SELECT_SERIAL_SPEED == 1
72 // High speed
73 #define SERIAL_DELAY 6 // micro sec
74 #define READ_WRITE_START_ADJUST 30 // cycles
75 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
76#elif SELECT_SERIAL_SPEED == 2
77 // Middle speed
78 #define SERIAL_DELAY 12 // micro sec
79 #define READ_WRITE_START_ADJUST 30 // cycles
80 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
81#elif SELECT_SERIAL_SPEED == 3
82 // Low speed
83 #define SERIAL_DELAY 24 // micro sec
84 #define READ_WRITE_START_ADJUST 30 // cycles
85 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
86#elif SELECT_SERIAL_SPEED == 4
87 // Very Low speed
88 #define SERIAL_DELAY 50 // micro sec
89 #define READ_WRITE_START_ADJUST 30 // cycles
90 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
91#else
92#error Illegal Serial Speed
93#endif
94
95
96#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
97#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
98
99#define SLAVE_INT_WIDTH_US 1
100#ifndef SERIAL_USE_MULTI_TRANSACTION
101 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
102#else
103 #define SLAVE_INT_ACK_WIDTH_UNIT 2
104 #define SLAVE_INT_ACK_WIDTH 4
105#endif
106
107static SSTD_t *Transaction_table = NULL;
26 108
27inline static 109inline static
28void serial_delay(void) { 110void serial_delay(void) {
29 _delay_us(SERIAL_DELAY); 111 _delay_us(SERIAL_DELAY);
30} 112}
31void serial_delay_short(void) { 113
32 _delay_us(SERIAL_DELAY-1); 114inline static
115void serial_delay_half1(void) {
116 _delay_us(SERIAL_DELAY_HALF1);
117}
118
119inline static
120void serial_delay_half2(void) {
121 _delay_us(SERIAL_DELAY_HALF2);
33} 122}
123
124inline static void serial_output(void) ALWAYS_INLINE;
34inline static 125inline static
35void serial_output(void) { 126void serial_output(void) {
36 SERIAL_PIN_DDR |= SERIAL_PIN_MASK; 127 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
37} 128}
38 129
39// make the serial pin an input with pull-up resistor 130// make the serial pin an input with pull-up resistor
131inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
40inline static 132inline static
41void serial_input(void) { 133void serial_input_with_pullup(void) {
42 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; 134 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
43 SERIAL_PIN_PORT |= SERIAL_PIN_MASK; 135 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
44} 136}
@@ -48,191 +140,305 @@ uint8_t serial_read_pin(void) {
48 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); 140 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
49} 141}
50 142
143inline static void serial_low(void) ALWAYS_INLINE;
51inline static 144inline static
52void serial_low(void) { 145void serial_low(void) {
53 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; 146 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
54} 147}
55 148
149inline static void serial_high(void) ALWAYS_INLINE;
56inline static 150inline static
57void serial_high(void) { 151void serial_high(void) {
58 SERIAL_PIN_PORT |= SERIAL_PIN_MASK; 152 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
59} 153}
60 154
61void serial_master_init(void) { 155void soft_serial_initiator_init(SSTD_t *sstd_table)
62 serial_output(); 156{
63 serial_high(); 157 Transaction_table = sstd_table;
158 serial_output();
159 serial_high();
64} 160}
65 161
66void serial_slave_init(void) { 162void soft_serial_target_init(SSTD_t *sstd_table)
67 serial_input(); 163{
68 164 Transaction_table = sstd_table;
69#ifndef USE_SERIAL_PD2 165 serial_input_with_pullup();
70 // Enable INT0 166
71 EIMSK |= _BV(INT0); 167#if SERIAL_PIN_MASK == _BV(PD0)
72 // Trigger on falling edge of INT0 168 // Enable INT0
73 EICRA &= ~(_BV(ISC00) | _BV(ISC01)); 169 EIMSK |= _BV(INT0);
170 // Trigger on falling edge of INT0
171 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
172#elif SERIAL_PIN_MASK == _BV(PD2)
173 // Enable INT2
174 EIMSK |= _BV(INT2);
175 // Trigger on falling edge of INT2
176 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
74#else 177#else
75 // Enable INT2 178 #error unknown SERIAL_PIN_MASK value
76 EIMSK |= _BV(INT2);
77 // Trigger on falling edge of INT2
78 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
79#endif 179#endif
80} 180}
81 181
82// Used by the master to synchronize timing with the slave. 182// Used by the sender to synchronize timing with the reciver.
183static void sync_recv(void) NO_INLINE;
83static 184static
84void sync_recv(void) { 185void sync_recv(void) {
85 serial_input(); 186 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
86 // This shouldn't hang if the slave disconnects because the 187 }
87 // serial line will float to high if the slave does disconnect. 188 // This shouldn't hang if the target disconnects because the
189 // serial line will float to high if the target does disconnect.
88 while (!serial_read_pin()); 190 while (!serial_read_pin());
89 //serial_delay();
90 _delay_us(SERIAL_DELAY-5);
91} 191}
92 192
93// Used by the slave to send a synchronization signal to the master. 193// Used by the reciver to send a synchronization signal to the sender.
194static void sync_send(void)NO_INLINE;
94static 195static
95void sync_send(void) { 196void sync_send(void) {
96 serial_output();
97
98 serial_low(); 197 serial_low();
99 serial_delay(); 198 serial_delay();
100
101 serial_high(); 199 serial_high();
102} 200}
103 201
104// Reads a byte from the serial line 202// Reads a byte from the serial line
105static 203static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
106uint8_t serial_read_byte(void) { 204static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
107 uint8_t byte = 0; 205 uint8_t byte, i, p, pb;
108 serial_input(); 206
109 for ( uint8_t i = 0; i < 8; ++i) { 207 _delay_sub_us(READ_WRITE_START_ADJUST);
110 byte = (byte << 1) | serial_read_pin(); 208 for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
111 serial_delay(); 209 serial_delay_half1(); // read the middle of pulses
112 _delay_us(1); 210 if( serial_read_pin() ) {
211 byte = (byte << 1) | 1; p ^= 1;
212 } else {
213 byte = (byte << 1) | 0; p ^= 0;
214 }
215 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
216 serial_delay_half2();
113 } 217 }
218 /* recive parity bit */
219 serial_delay_half1(); // read the middle of pulses
220 pb = serial_read_pin();
221 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
222 serial_delay_half2();
223
224 *pterrcount += (p != pb)? 1 : 0;
114 225
115 return byte; 226 return byte;
116} 227}
117 228
118// Sends a byte with MSB ordering 229// Sends a byte with MSB ordering
119static 230void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
120void serial_write_byte(uint8_t data) { 231void serial_write_chunk(uint8_t data, uint8_t bit) {
121 uint8_t b = 8; 232 uint8_t b, p;
122 serial_output(); 233 for( p = 0, b = 1<<(bit-1); b ; b >>= 1) {
123 while( b-- ) { 234 if(data & b) {
124 if(data & (1 << b)) { 235 serial_high(); p ^= 1;
125 serial_high(); 236 } else {
126 } else { 237 serial_low(); p ^= 0;
127 serial_low(); 238 }
239 serial_delay();
128 } 240 }
241 /* send parity bit */
242 if(p & 1) { serial_high(); }
243 else { serial_low(); }
129 serial_delay(); 244 serial_delay();
130 }
131}
132 245
133// interrupt handle to be used by the slave device 246 serial_low(); // sync_send() / senc_recv() need raise edge
134ISR(SERIAL_PIN_INTERRUPT) { 247}
135 sync_send();
136 248
137 uint8_t checksum = 0; 249static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
138 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { 250static
139 serial_write_byte(serial_slave_buffer[i]); 251void serial_send_packet(uint8_t *buffer, uint8_t size) {
252 for (uint8_t i = 0; i < size; ++i) {
253 uint8_t data;
254 data = buffer[i];
140 sync_send(); 255 sync_send();
141 checksum += serial_slave_buffer[i]; 256 serial_write_chunk(data,8);
142 } 257 }
143 serial_write_byte(checksum); 258}
144 sync_send();
145 259
146 // wait for the sync to finish sending 260static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
147 serial_delay(); 261static
262uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
263 uint8_t pecount = 0;
264 for (uint8_t i = 0; i < size; ++i) {
265 uint8_t data;
266 sync_recv();
267 data = serial_read_chunk(&pecount, 8);
268 buffer[i] = data;
269 }
270 return pecount == 0;
271}
148 272
149 // read the middle of pulses 273inline static
150 _delay_us(SERIAL_DELAY/2); 274void change_sender2reciver(void) {
275 sync_send(); //0
276 serial_delay_half1(); //1
277 serial_low(); //2
278 serial_input_with_pullup(); //2
279 serial_delay_half1(); //3
280}
151 281
152 uint8_t checksum_computed = 0; 282inline static
153 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { 283void change_reciver2sender(void) {
154 serial_master_buffer[i] = serial_read_byte(); 284 sync_recv(); //0
155 sync_send(); 285 serial_delay(); //1
156 checksum_computed += serial_master_buffer[i]; 286 serial_low(); //3
157 } 287 serial_output(); //3
158 uint8_t checksum_received = serial_read_byte(); 288 serial_delay_half1(); //4
159 sync_send(); 289}
160 290
161 serial_input(); // end transaction 291// interrupt handle to be used by the target device
292ISR(SERIAL_PIN_INTERRUPT) {
162 293
163 if ( checksum_computed != checksum_received ) { 294#ifndef SERIAL_USE_MULTI_TRANSACTION
164 status |= SLAVE_DATA_CORRUPT; 295 serial_low();
296 serial_output();
297 SSTD_t *trans = Transaction_table;
298#else
299 // recive transaction table index
300 uint8_t tid;
301 uint8_t pecount = 0;
302 sync_recv();
303 tid = serial_read_chunk(&pecount,4);
304 if(pecount> 0)
305 return;
306 serial_delay_half1();
307
308 serial_high(); // response step1 low->high
309 serial_output();
310 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
311 SSTD_t *trans = &Transaction_table[tid];
312 serial_low(); // response step2 ack high->low
313#endif
314
315 // target send phase
316 if( trans->target2initiator_buffer_size > 0 )
317 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
318 trans->target2initiator_buffer_size);
319 // target switch to input
320 change_sender2reciver();
321
322 // target recive phase
323 if( trans->initiator2target_buffer_size > 0 ) {
324 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
325 trans->initiator2target_buffer_size) ) {
326 *trans->status = TRANSACTION_ACCEPTED;
327 } else {
328 *trans->status = TRANSACTION_DATA_ERROR;
329 }
165 } else { 330 } else {
166 status &= ~SLAVE_DATA_CORRUPT; 331 *trans->status = TRANSACTION_ACCEPTED;
167 } 332 }
168}
169 333
170inline 334 sync_recv(); //weit initiator output to high
171bool serial_slave_DATA_CORRUPT(void) {
172 return status & SLAVE_DATA_CORRUPT;
173} 335}
174 336
175// Copies the serial_slave_buffer to the master and sends the 337/////////
176// serial_master_buffer to the slave. 338// start transaction by initiator
339//
340// int soft_serial_transaction(int sstd_index)
177// 341//
178// Returns: 342// Returns:
179// 0 => no error 343// TRANSACTION_END
180// 1 => slave did not respond 344// TRANSACTION_NO_RESPONSE
181int serial_update_buffers(void) { 345// TRANSACTION_DATA_ERROR
182 // this code is very time dependent, so we need to disable interrupts 346// this code is very time dependent, so we need to disable interrupts
347#ifndef SERIAL_USE_MULTI_TRANSACTION
348int soft_serial_transaction(void) {
349 SSTD_t *trans = Transaction_table;
350#else
351int soft_serial_transaction(int sstd_index) {
352 SSTD_t *trans = &Transaction_table[sstd_index];
353#endif
183 cli(); 354 cli();
184 355
185 // signal to the slave that we want to start a transaction 356 // signal to the target that we want to start a transaction
186 serial_output(); 357 serial_output();
187 serial_low(); 358 serial_low();
188 _delay_us(1); 359 _delay_us(SLAVE_INT_WIDTH_US);
189 360
190 // wait for the slaves response 361#ifndef SERIAL_USE_MULTI_TRANSACTION
191 serial_input(); 362 // wait for the target response
192 serial_high(); 363 serial_input_with_pullup();
193 _delay_us(SERIAL_DELAY); 364 _delay_us(SLAVE_INT_RESPONSE_TIME);
194 365
195 // check if the slave is present 366 // check if the target is present
196 if (serial_read_pin()) { 367 if (serial_read_pin()) {
197 // slave failed to pull the line low, assume not present 368 // target failed to pull the line low, assume not present
369 serial_output();
370 serial_high();
371 *trans->status = TRANSACTION_NO_RESPONSE;
198 sei(); 372 sei();
199 return 1; 373 return TRANSACTION_NO_RESPONSE;
200 } 374 }
201 375
202 // if the slave is present syncronize with it 376#else
203 sync_recv(); 377 // send transaction table index
204 378 sync_send();
205 uint8_t checksum_computed = 0; 379 _delay_sub_us(TID_SEND_ADJUST);
206 // receive data from the slave 380 serial_write_chunk(sstd_index, 4);
207 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { 381 serial_delay_half1();
208 serial_slave_buffer[i] = serial_read_byte(); 382
209 sync_recv(); 383 // wait for the target response (step1 low->high)
210 checksum_computed += serial_slave_buffer[i]; 384 serial_input_with_pullup();
385 while( !serial_read_pin() ) {
386 _delay_sub_us(2);
211 } 387 }
212 uint8_t checksum_received = serial_read_byte();
213 sync_recv();
214 388
215 if (checksum_computed != checksum_received) { 389 // check if the target is present (step2 high->low)
216 sei(); 390 for( int i = 0; serial_read_pin(); i++ ) {
217 return 2; 391 if (i > SLAVE_INT_ACK_WIDTH + 1) {
392 // slave failed to pull the line low, assume not present
393 serial_output();
394 serial_high();
395 *trans->status = TRANSACTION_NO_RESPONSE;
396 sei();
397 return TRANSACTION_NO_RESPONSE;
398 }
399 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
218 } 400 }
401#endif
219 402
220 uint8_t checksum = 0; 403 // initiator recive phase
221 // send data to the slave 404 // if the target is present syncronize with it
222 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { 405 if( trans->target2initiator_buffer_size > 0 ) {
223 serial_write_byte(serial_master_buffer[i]); 406 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
224 sync_recv(); 407 trans->target2initiator_buffer_size) ) {
225 checksum += serial_master_buffer[i]; 408 serial_output();
409 serial_high();
410 *trans->status = TRANSACTION_DATA_ERROR;
411 sei();
412 return TRANSACTION_DATA_ERROR;
413 }
414 }
415
416 // initiator switch to output
417 change_reciver2sender();
418
419 // initiator send phase
420 if( trans->initiator2target_buffer_size > 0 ) {
421 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
422 trans->initiator2target_buffer_size);
226 } 423 }
227 serial_write_byte(checksum);
228 sync_recv();
229 424
230 // always, release the line when not in use 425 // always, release the line when not in use
231 serial_output(); 426 sync_send();
232 serial_high();
233 427
428 *trans->status = TRANSACTION_END;
234 sei(); 429 sei();
235 return 0; 430 return TRANSACTION_END;
236} 431}
237 432
433#ifdef SERIAL_USE_MULTI_TRANSACTION
434int soft_serial_get_and_clean_status(int sstd_index) {
435 SSTD_t *trans = &Transaction_table[sstd_index];
436 cli();
437 int retval = *trans->status;
438 *trans->status = 0;;
439 sei();
440 return retval;
441}
442#endif
443
238#endif 444#endif