]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/main_arm_atsam.c
Bringing Massdrop keyboard hardware configuration to keyboard level (#4593)
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / main_arm_atsam.c
1 /*
2 Copyright 2018 Massdrop Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "samd51j18a.h"
19 #include "tmk_core/common/keyboard.h"
20
21 #include "report.h"
22 #include "host.h"
23 #include "host_driver.h"
24 #include "keycode_config.h"
25 #include <string.h>
26 #include "quantum.h"
27
28 //From protocol directory
29 #include "arm_atsam_protocol.h"
30
31 //From keyboard's directory
32 #include "config_led.h"
33
34 uint8_t g_usb_state = USB_FSMSTATUS_FSMSTATE_OFF_Val;   //Saved USB state from hardware value to detect changes
35
36 void main_subtasks(void);
37 uint8_t keyboard_leds(void);
38 void send_keyboard(report_keyboard_t *report);
39 void send_mouse(report_mouse_t *report);
40 void send_system(uint16_t data);
41 void send_consumer(uint16_t data);
42
43 host_driver_t arm_atsam_driver = {
44     keyboard_leds,
45     send_keyboard,
46     send_mouse,
47     send_system,
48     send_consumer
49 };
50
51 uint8_t led_states;
52
53 uint8_t keyboard_leds(void)
54 {
55 #ifdef NKRO_ENABLE
56     if (keymap_config.nkro)
57         return udi_hid_nkro_report_set;
58     else
59 #endif //NKRO_ENABLE
60         return udi_hid_kbd_report_set;
61 }
62
63 void send_keyboard(report_keyboard_t *report)
64 {
65     uint32_t irqflags;
66
67 #ifdef NKRO_ENABLE
68     if (!keymap_config.nkro)
69     {
70 #endif //NKRO_ENABLE
71         while (udi_hid_kbd_b_report_trans_ongoing) { main_subtasks(); } //Run other tasks while waiting for USB to be free
72
73         irqflags = __get_PRIMASK();
74         __disable_irq();
75         __DMB();
76
77         memcpy(udi_hid_kbd_report, report->raw, UDI_HID_KBD_REPORT_SIZE);
78         udi_hid_kbd_b_report_valid = 1;
79         udi_hid_kbd_send_report();
80
81         __DMB();
82         __set_PRIMASK(irqflags);
83 #ifdef NKRO_ENABLE
84     }
85     else
86     {
87         while (udi_hid_nkro_b_report_trans_ongoing) { main_subtasks(); } //Run other tasks while waiting for USB to be free
88
89         irqflags = __get_PRIMASK();
90         __disable_irq();
91         __DMB();
92
93         memcpy(udi_hid_nkro_report, report->raw, UDI_HID_NKRO_REPORT_SIZE);
94         udi_hid_nkro_b_report_valid = 1;
95         udi_hid_nkro_send_report();
96
97         __DMB();
98         __set_PRIMASK(irqflags);
99     }
100 #endif //NKRO_ENABLE
101 }
102
103 void send_mouse(report_mouse_t *report)
104 {
105 #ifdef MOUSEKEY_ENABLE
106     uint32_t irqflags;
107
108     irqflags = __get_PRIMASK();
109     __disable_irq();
110     __DMB();
111
112     memcpy(udi_hid_mou_report, report, UDI_HID_MOU_REPORT_SIZE);
113     udi_hid_mou_b_report_valid = 1;
114     udi_hid_mou_send_report();
115
116     __DMB();
117     __set_PRIMASK(irqflags);
118 #endif //MOUSEKEY_ENABLE
119 }
120
121 void send_system(uint16_t data)
122 {
123 #ifdef EXTRAKEY_ENABLE
124     uint32_t irqflags;
125
126     irqflags = __get_PRIMASK();
127     __disable_irq();
128     __DMB();
129
130     udi_hid_exk_report.desc.report_id = REPORT_ID_SYSTEM;
131     if (data != 0) data = data - SYSTEM_POWER_DOWN + 1;
132     udi_hid_exk_report.desc.report_data = data;
133     udi_hid_exk_b_report_valid = 1;
134     udi_hid_exk_send_report();
135
136     __DMB();
137     __set_PRIMASK(irqflags);
138 #endif //EXTRAKEY_ENABLE
139 }
140
141 void send_consumer(uint16_t data)
142 {
143 #ifdef EXTRAKEY_ENABLE
144     uint32_t irqflags;
145
146     irqflags = __get_PRIMASK();
147     __disable_irq();
148     __DMB();
149
150     udi_hid_exk_report.desc.report_id = REPORT_ID_CONSUMER;
151     udi_hid_exk_report.desc.report_data = data;
152     udi_hid_exk_b_report_valid = 1;
153     udi_hid_exk_send_report();
154
155     __DMB();
156     __set_PRIMASK(irqflags);
157 #endif //EXTRAKEY_ENABLE
158 }
159
160 void main_subtask_usb_state(void)
161 {
162     static uint32_t fsmstate_on_delay = 0;                          //Delay timer to be sure USB is actually operating before bringing up hardware
163     uint8_t fsmstate_now = USB->DEVICE.FSMSTATUS.reg;               //Current state from hardware register
164
165     if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val)         //If USB SUSPENDED
166     {
167         fsmstate_on_delay = 0;                                      //Clear ON delay timer
168
169         if (g_usb_state != USB_FSMSTATUS_FSMSTATE_SUSPEND_Val)      //If previously not SUSPENDED
170         {
171             suspend_power_down();                                   //Run suspend routine
172             g_usb_state = fsmstate_now;                             //Save current USB state
173         }
174     }
175     else if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_SLEEP_Val)      //Else if USB SLEEPING
176     {
177         fsmstate_on_delay = 0;                                      //Clear ON delay timer
178
179         if (g_usb_state != USB_FSMSTATUS_FSMSTATE_SLEEP_Val)        //If previously not SLEEPING
180         {
181             suspend_power_down();                                   //Run suspend routine
182             g_usb_state = fsmstate_now;                             //Save current USB state
183         }
184     }
185     else if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_ON_Val)         //Else if USB ON
186     {
187         if (g_usb_state != USB_FSMSTATUS_FSMSTATE_ON_Val)           //If previously not ON
188         {
189             if (fsmstate_on_delay == 0)                             //If ON delay timer is cleared
190             {
191                 fsmstate_on_delay = CLK_get_ms() + 250;             //Set ON delay timer
192             }
193             else if (CLK_get_ms() > fsmstate_on_delay)              //Else if ON delay timer is active and timed out
194             {
195                 suspend_wakeup_init();                              //Run wakeup routine
196                 g_usb_state = fsmstate_now;                         //Save current USB state
197             }
198         }
199     }
200     else                                                            //Else if USB is in a state not being tracked
201     {
202         fsmstate_on_delay = 0;                                      //Clear ON delay timer
203     }
204 }
205
206 void main_subtask_led(void)
207 {
208     if (g_usb_state != USB_FSMSTATUS_FSMSTATE_ON_Val) return; //Only run LED tasks if USB is operating
209
210     led_matrix_task();
211 }
212
213 void main_subtask_power_check(void)
214 {
215     static uint64_t next_5v_checkup = 0;
216
217     if (CLK_get_ms() > next_5v_checkup)
218     {
219         next_5v_checkup = CLK_get_ms() + 5;
220
221         v_5v = adc_get(ADC_5V);
222         v_5v_avg = 0.9 * v_5v_avg + 0.1 * v_5v;
223
224         gcr_compute();
225     }
226 }
227
228 void main_subtask_usb_extra_device(void)
229 {
230     static uint64_t next_usb_checkup = 0;
231
232     if (CLK_get_ms() > next_usb_checkup)
233     {
234         next_usb_checkup = CLK_get_ms() + 10;
235
236         USB_HandleExtraDevice();
237     }
238 }
239
240 void main_subtasks(void)
241 {
242     main_subtask_usb_state();
243     main_subtask_led();
244     main_subtask_power_check();
245     main_subtask_usb_extra_device();
246 }
247
248 int main(void)
249 {
250     DBG_LED_ENA;
251     DBG_1_ENA;
252     DBG_1_OFF;
253     DBG_2_ENA;
254     DBG_2_OFF;
255     DBG_3_ENA;
256     DBG_3_OFF;
257
258     debug_code_init();
259
260     CLK_init();
261
262     ADC0_init();
263
264     SR_EXP_Init();
265
266     i2c1_init();
267
268     matrix_init();
269
270     USB2422_init();
271
272     DBGC(DC_MAIN_UDC_START_BEGIN);
273     udc_start();
274     DBGC(DC_MAIN_UDC_START_COMPLETE);
275
276     DBGC(DC_MAIN_CDC_INIT_BEGIN);
277     CDC_init();
278     DBGC(DC_MAIN_CDC_INIT_COMPLETE);
279
280     while (USB2422_Port_Detect_Init() == 0) {}
281
282     DBG_LED_OFF;
283
284     led_matrix_init();
285
286     while (I2C3733_Init_Control() != 1) {}
287     while (I2C3733_Init_Drivers() != 1) {}
288
289     I2C_DMAC_LED_Init();
290
291     i2c_led_q_init();
292
293     for (uint8_t drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++)
294         I2C_LED_Q_ONOFF(drvid); //Queue data
295
296     keyboard_setup();
297
298     keyboard_init();
299
300     host_set_driver(&arm_atsam_driver);
301
302 #ifdef CONSOLE_ENABLE
303     uint64_t next_print = 0;
304 #endif //CONSOLE_ENABLE
305
306     v_5v_avg = adc_get(ADC_5V);
307
308     debug_code_disable();
309
310     while (1)
311     {
312         main_subtasks(); //Note these tasks will also be run while waiting for USB keyboard polling intervals
313
314         if (g_usb_state == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val || g_usb_state == USB_FSMSTATUS_FSMSTATE_SLEEP_Val)
315         {
316             if (suspend_wakeup_condition())
317             {
318                 udc_remotewakeup(); //Send remote wakeup signal
319                 wait_ms(50);
320             }
321
322             continue;
323         }
324
325         keyboard_task();
326
327 #ifdef CONSOLE_ENABLE
328         if (CLK_get_ms() > next_print)
329         {
330             next_print = CLK_get_ms() + 250;
331             //Add any debug information here that you want to see very often
332             //dprintf("5v=%u 5vu=%u dlow=%u dhi=%u gca=%u gcd=%u\r\n", v_5v, v_5v_avg, v_5v_avg - V5_LOW, v_5v_avg - V5_HIGH, gcr_actual, gcr_desired);
333         }
334 #endif //CONSOLE_ENABLE
335     }
336
337
338     return 1;
339 }
340