diff options
author | That-Canadian <Poole.Chris.11@gmail.com> | 2018-07-16 19:25:02 -0700 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2018-07-16 22:25:02 -0400 |
commit | 0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c (patch) | |
tree | 15a411d5ed6ad203982337448cfde11ed26ce7b7 /quantum/split_common/serial.c | |
parent | b2877470ced1deb9651ecb39f6a82f5ef380b399 (diff) | |
download | qmk_firmware-0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c.tar.gz qmk_firmware-0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c.zip |
Lets split eh (#3120)
* Line ending stuff again
* Added Let's Split Eh? Files and updated #USE_IC2 checks to also include th EH revision (can only be used in I2C)
* Added personal keymap, updated some of the EH files
* Created new keyboard file for testing "lets_split_eh" will merge into lets_split once fully functional
* Added split code from lets_split, removed pro micro imports and LED code
THIS IS WORKING CODE, WITHOUT RGB AND BACKLIGHT
* Took back original Lets Slit files for the lets_split keyboard, working in the lets_split_eh folder for now
* Updated eh.c
* More rework of the I2C code, added global flags for split boards.
* Introduced RGB over I2C, having weird edge case issues at the moment though
* Fixed weird I2C edgecase with RGB, although still would like to track down route cause..
* Changed RGB keycodes (static ones) to activate on key-up instead of key-down to elimate weird ghosting issue over I2C
* Lots of changes, mainly externalized the Split keyboard code and added logic for only including when needed.
- Added makefile option "SPLIT_KEYBOARD" that when = yes will include the split keyboard files and custom matrix
- Split keyboard files placed into quantum/split_common/
- Added define option for config files "SPLIT_HAND_PIN" FOr using high/low pin to determine handedness, low = right hand, high = left hand
- Cleaned up split logic for RGB and Backlight so it is only exectuted / included when needed
* Updated documentation for the new makefile options and #defines specific to split keyboards
* Added a bit more info to docs, so people aren't confused
* Modifed Let's Split to use externalized code, also added left and right hand eeprom files to the split_common folder
* Removed some debugging from eh.c
* Small changes to keyboard configs. Also added a default keymap (just a copy of my that_canadian keymap).
* Added a README file to the Let's Split Eh?
* Changed it so RGB static updates are done on key-up ONLY for split boards rather than all boards. Also fixed leftover un-used variable in rgblight.c
* Updated default keymap and my keymap for Let's Split Eh? Updated the comments so it reflects RGB control, and removed audio functions.
* Fixed lets_split_eh not having a default version
* Removed "eh" references from lets_split folder for now
* Took lets_split folder from master to fix travis build errors, weird my local was overriding.
* Changed LAYOUT_ortho_4x12_kc -> LAYOUT_kc_ortho_4x12 to match bakingpy and others
* Removed rules.mk from my lets_split keymap, not needed
* Updated the config_options doc to better explain the usage of "#define SPLIT_HAND_PIN"
Diffstat (limited to 'quantum/split_common/serial.c')
-rw-r--r-- | quantum/split_common/serial.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/quantum/split_common/serial.c b/quantum/split_common/serial.c new file mode 100644 index 000000000..74bcbb6bf --- /dev/null +++ b/quantum/split_common/serial.c | |||
@@ -0,0 +1,228 @@ | |||
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 | uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; | ||
22 | uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; | ||
23 | |||
24 | #define SLAVE_DATA_CORRUPT (1<<0) | ||
25 | volatile uint8_t status = 0; | ||
26 | |||
27 | inline static | ||
28 | void serial_delay(void) { | ||
29 | _delay_us(SERIAL_DELAY); | ||
30 | } | ||
31 | |||
32 | inline static | ||
33 | void serial_output(void) { | ||
34 | SERIAL_PIN_DDR |= SERIAL_PIN_MASK; | ||
35 | } | ||
36 | |||
37 | // make the serial pin an input with pull-up resistor | ||
38 | inline static | ||
39 | void serial_input(void) { | ||
40 | SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; | ||
41 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | ||
42 | } | ||
43 | |||
44 | inline static | ||
45 | uint8_t serial_read_pin(void) { | ||
46 | return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); | ||
47 | } | ||
48 | |||
49 | inline static | ||
50 | void serial_low(void) { | ||
51 | SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; | ||
52 | } | ||
53 | |||
54 | inline static | ||
55 | void serial_high(void) { | ||
56 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | ||
57 | } | ||
58 | |||
59 | void serial_master_init(void) { | ||
60 | serial_output(); | ||
61 | serial_high(); | ||
62 | } | ||
63 | |||
64 | void serial_slave_init(void) { | ||
65 | serial_input(); | ||
66 | |||
67 | // Enable INT0 | ||
68 | EIMSK |= _BV(INT0); | ||
69 | // Trigger on falling edge of INT0 | ||
70 | EICRA &= ~(_BV(ISC00) | _BV(ISC01)); | ||
71 | } | ||
72 | |||
73 | // Used by the master to synchronize timing with the slave. | ||
74 | static | ||
75 | void sync_recv(void) { | ||
76 | serial_input(); | ||
77 | // This shouldn't hang if the slave disconnects because the | ||
78 | // serial line will float to high if the slave does disconnect. | ||
79 | while (!serial_read_pin()); | ||
80 | serial_delay(); | ||
81 | } | ||
82 | |||
83 | // Used by the slave to send a synchronization signal to the master. | ||
84 | static | ||
85 | void sync_send(void) { | ||
86 | serial_output(); | ||
87 | |||
88 | serial_low(); | ||
89 | serial_delay(); | ||
90 | |||
91 | serial_high(); | ||
92 | } | ||
93 | |||
94 | // Reads a byte from the serial line | ||
95 | static | ||
96 | uint8_t serial_read_byte(void) { | ||
97 | uint8_t byte = 0; | ||
98 | serial_input(); | ||
99 | for ( uint8_t i = 0; i < 8; ++i) { | ||
100 | byte = (byte << 1) | serial_read_pin(); | ||
101 | serial_delay(); | ||
102 | _delay_us(1); | ||
103 | } | ||
104 | |||
105 | return byte; | ||
106 | } | ||
107 | |||
108 | // Sends a byte with MSB ordering | ||
109 | static | ||
110 | void serial_write_byte(uint8_t data) { | ||
111 | uint8_t b = 8; | ||
112 | serial_output(); | ||
113 | while( b-- ) { | ||
114 | if(data & (1 << b)) { | ||
115 | serial_high(); | ||
116 | } else { | ||
117 | serial_low(); | ||
118 | } | ||
119 | serial_delay(); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | // interrupt handle to be used by the slave device | ||
124 | ISR(SERIAL_PIN_INTERRUPT) { | ||
125 | sync_send(); | ||
126 | |||
127 | uint8_t checksum = 0; | ||
128 | for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | ||
129 | serial_write_byte(serial_slave_buffer[i]); | ||
130 | sync_send(); | ||
131 | checksum += serial_slave_buffer[i]; | ||
132 | } | ||
133 | serial_write_byte(checksum); | ||
134 | sync_send(); | ||
135 | |||
136 | // wait for the sync to finish sending | ||
137 | serial_delay(); | ||
138 | |||
139 | // read the middle of pulses | ||
140 | _delay_us(SERIAL_DELAY/2); | ||
141 | |||
142 | uint8_t checksum_computed = 0; | ||
143 | for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | ||
144 | serial_master_buffer[i] = serial_read_byte(); | ||
145 | sync_send(); | ||
146 | checksum_computed += serial_master_buffer[i]; | ||
147 | } | ||
148 | uint8_t checksum_received = serial_read_byte(); | ||
149 | sync_send(); | ||
150 | |||
151 | serial_input(); // end transaction | ||
152 | |||
153 | if ( checksum_computed != checksum_received ) { | ||
154 | status |= SLAVE_DATA_CORRUPT; | ||
155 | } else { | ||
156 | status &= ~SLAVE_DATA_CORRUPT; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | inline | ||
161 | bool serial_slave_DATA_CORRUPT(void) { | ||
162 | return status & SLAVE_DATA_CORRUPT; | ||
163 | } | ||
164 | |||
165 | // Copies the serial_slave_buffer to the master and sends the | ||
166 | // serial_master_buffer to the slave. | ||
167 | // | ||
168 | // Returns: | ||
169 | // 0 => no error | ||
170 | // 1 => slave did not respond | ||
171 | int serial_update_buffers(void) { | ||
172 | // this code is very time dependent, so we need to disable interrupts | ||
173 | cli(); | ||
174 | |||
175 | // signal to the slave that we want to start a transaction | ||
176 | serial_output(); | ||
177 | serial_low(); | ||
178 | _delay_us(1); | ||
179 | |||
180 | // wait for the slaves response | ||
181 | serial_input(); | ||
182 | serial_high(); | ||
183 | _delay_us(SERIAL_DELAY); | ||
184 | |||
185 | // check if the slave is present | ||
186 | if (serial_read_pin()) { | ||
187 | // slave failed to pull the line low, assume not present | ||
188 | sei(); | ||
189 | return 1; | ||
190 | } | ||
191 | |||
192 | // if the slave is present syncronize with it | ||
193 | sync_recv(); | ||
194 | |||
195 | uint8_t checksum_computed = 0; | ||
196 | // receive data from the slave | ||
197 | for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | ||
198 | serial_slave_buffer[i] = serial_read_byte(); | ||
199 | sync_recv(); | ||
200 | checksum_computed += serial_slave_buffer[i]; | ||
201 | } | ||
202 | uint8_t checksum_received = serial_read_byte(); | ||
203 | sync_recv(); | ||
204 | |||
205 | if (checksum_computed != checksum_received) { | ||
206 | sei(); | ||
207 | return 1; | ||
208 | } | ||
209 | |||
210 | uint8_t checksum = 0; | ||
211 | // send data to the slave | ||
212 | for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | ||
213 | serial_write_byte(serial_master_buffer[i]); | ||
214 | sync_recv(); | ||
215 | checksum += serial_master_buffer[i]; | ||
216 | } | ||
217 | serial_write_byte(checksum); | ||
218 | sync_recv(); | ||
219 | |||
220 | // always, release the line when not in use | ||
221 | serial_output(); | ||
222 | serial_high(); | ||
223 | |||
224 | sei(); | ||
225 | return 0; | ||
226 | } | ||
227 | |||
228 | #endif | ||