]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/fortitude60/serial.c
Merge branch 'master' of github.com:qmk/qmk_firmware into hf/shinydox
[qmk_firmware.git] / keyboards / fortitude60 / serial.c
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 #ifndef USE_SERIAL_PD2
68   // Enable INT0
69   EIMSK |= _BV(INT0);
70   // Trigger on falling edge of INT0
71   EICRA &= ~(_BV(ISC00) | _BV(ISC01));
72 #else
73   // Enable INT2
74   EIMSK |= _BV(INT2);
75   // Trigger on falling edge of INT2
76   EICRA &= ~(_BV(ISC20) | _BV(ISC21));
77 #endif
78 }
79
80 // Used by the master to synchronize timing with the slave.
81 static
82 void sync_recv(void) {
83   serial_input();
84   // This shouldn't hang if the slave disconnects because the
85   // serial line will float to high if the slave does disconnect.
86   while (!serial_read_pin());
87   serial_delay();
88 }
89
90 // Used by the slave to send a synchronization signal to the master.
91 static
92 void sync_send(void) {
93   serial_output();
94
95   serial_low();
96   serial_delay();
97
98   serial_high();
99 }
100
101 // Reads a byte from the serial line
102 static
103 uint8_t serial_read_byte(void) {
104   uint8_t byte = 0;
105   serial_input();
106   for ( uint8_t i = 0; i < 8; ++i) {
107     byte = (byte << 1) | serial_read_pin();
108     serial_delay();
109     _delay_us(1);
110   }
111
112   return byte;
113 }
114
115 // Sends a byte with MSB ordering
116 static
117 void serial_write_byte(uint8_t data) {
118   uint8_t b = 8;
119   serial_output();
120   while( b-- ) {
121     if(data & (1 << b)) {
122       serial_high();
123     } else {
124       serial_low();
125     }
126     serial_delay();
127   }
128 }
129
130 // interrupt handle to be used by the slave device
131 ISR(SERIAL_PIN_INTERRUPT) {
132   sync_send();
133
134   uint8_t checksum = 0;
135   for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
136     serial_write_byte(serial_slave_buffer[i]);
137     sync_send();
138     checksum += serial_slave_buffer[i];
139   }
140   serial_write_byte(checksum);
141   sync_send();
142
143   // wait for the sync to finish sending
144   serial_delay();
145
146   // read the middle of pulses
147   _delay_us(SERIAL_DELAY/2);
148
149   uint8_t checksum_computed = 0;
150   for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
151     serial_master_buffer[i] = serial_read_byte();
152     sync_send();
153     checksum_computed += serial_master_buffer[i];
154   }
155   uint8_t checksum_received = serial_read_byte();
156   sync_send();
157
158   serial_input(); // end transaction
159
160   if ( checksum_computed != checksum_received ) {
161     status |= SLAVE_DATA_CORRUPT;
162   } else {
163     status &= ~SLAVE_DATA_CORRUPT;
164   }
165 }
166
167 inline
168 bool serial_slave_DATA_CORRUPT(void) {
169   return status & SLAVE_DATA_CORRUPT;
170 }
171
172 // Copies the serial_slave_buffer to the master and sends the
173 // serial_master_buffer to the slave.
174 //
175 // Returns:
176 // 0 => no error
177 // 1 => slave did not respond
178 int serial_update_buffers(void) {
179   // this code is very time dependent, so we need to disable interrupts
180   cli();
181
182   // signal to the slave that we want to start a transaction
183   serial_output();
184   serial_low();
185   _delay_us(1);
186
187   // wait for the slaves response
188   serial_input();
189   serial_high();
190   _delay_us(SERIAL_DELAY);
191
192   // check if the slave is present
193   if (serial_read_pin()) {
194     // slave failed to pull the line low, assume not present
195     sei();
196     return 1;
197   }
198
199   // if the slave is present syncronize with it
200   sync_recv();
201
202   uint8_t checksum_computed = 0;
203   // receive data from the slave
204   for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
205     serial_slave_buffer[i] = serial_read_byte();
206     sync_recv();
207     checksum_computed += serial_slave_buffer[i];
208   }
209   uint8_t checksum_received = serial_read_byte();
210   sync_recv();
211
212   if (checksum_computed != checksum_received) {
213     sei();
214     return 2;
215   }
216
217   uint8_t checksum = 0;
218   // send data to the slave
219   for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
220     serial_write_byte(serial_master_buffer[i]);
221     sync_recv();
222     checksum += serial_master_buffer[i];
223   }
224   serial_write_byte(checksum);
225   sync_recv();
226
227   // always, release the line when not in use
228   serial_output();
229   serial_high();
230
231   sei();
232   return 0;
233 }
234
235 #endif