]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/main_arm_atsam.c
RGB Matrix support for Massdrop CTRL/ALT (#5328)
[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 uint64_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 = timer_read64() + 250;             //Set ON delay timer
192             }
193             else if (timer_read64() > 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_power_check(void)
207 {
208     static uint64_t next_5v_checkup = 0;
209
210     if (timer_read64() > next_5v_checkup)
211     {
212         next_5v_checkup = timer_read64() + 5;
213
214         v_5v = adc_get(ADC_5V);
215         v_5v_avg = 0.9 * v_5v_avg + 0.1 * v_5v;
216
217 #ifdef RGB_MATRIX_ENABLE
218         gcr_compute();
219 #endif
220     }
221 }
222
223 void main_subtask_usb_extra_device(void)
224 {
225     static uint64_t next_usb_checkup = 0;
226
227     if (timer_read64() > next_usb_checkup)
228     {
229         next_usb_checkup = timer_read64() + 10;
230
231         USB_HandleExtraDevice();
232     }
233 }
234
235 void main_subtasks(void)
236 {
237     main_subtask_usb_state();
238     main_subtask_power_check();
239     main_subtask_usb_extra_device();
240 }
241
242 int main(void)
243 {
244     DBG_LED_ENA;
245     DBG_1_ENA;
246     DBG_1_OFF;
247     DBG_2_ENA;
248     DBG_2_OFF;
249     DBG_3_ENA;
250     DBG_3_OFF;
251
252     debug_code_init();
253
254     CLK_init();
255
256     ADC0_init();
257
258     SR_EXP_Init();
259
260 #ifdef RGB_MATRIX_ENABLE
261     i2c1_init();
262 #endif // RGB_MATRIX_ENABLE
263
264     matrix_init();
265
266     USB2422_init();
267
268     DBGC(DC_MAIN_UDC_START_BEGIN);
269     udc_start();
270     DBGC(DC_MAIN_UDC_START_COMPLETE);
271
272     DBGC(DC_MAIN_CDC_INIT_BEGIN);
273     CDC_init();
274     DBGC(DC_MAIN_CDC_INIT_COMPLETE);
275
276     while (USB2422_Port_Detect_Init() == 0) {}
277
278     DBG_LED_OFF;
279
280 #ifdef RGB_MATRIX_ENABLE
281     while (I2C3733_Init_Control() != 1) {}
282     while (I2C3733_Init_Drivers() != 1) {}
283
284     I2C_DMAC_LED_Init();
285
286     i2c_led_q_init();
287
288     for (uint8_t drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++)
289         I2C_LED_Q_ONOFF(drvid); //Queue data
290 #endif // RGB_MATRIX_ENABLE
291
292     keyboard_setup();
293
294     keyboard_init();
295
296     host_set_driver(&arm_atsam_driver);
297
298 #ifdef CONSOLE_ENABLE
299     uint64_t next_print = 0;
300 #endif //CONSOLE_ENABLE
301
302     v_5v_avg = adc_get(ADC_5V);
303
304     debug_code_disable();
305
306     while (1)
307     {
308         main_subtasks(); //Note these tasks will also be run while waiting for USB keyboard polling intervals
309
310         if (g_usb_state == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val || g_usb_state == USB_FSMSTATUS_FSMSTATE_SLEEP_Val)
311         {
312             if (suspend_wakeup_condition())
313             {
314                 udc_remotewakeup(); //Send remote wakeup signal
315                 wait_ms(50);
316             }
317
318             continue;
319         }
320
321         keyboard_task();
322
323 #ifdef CONSOLE_ENABLE
324         if (timer_read64() > next_print)
325         {
326             next_print = timer_read64() + 250;
327             //Add any debug information here that you want to see very often
328             //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);
329         }
330 #endif //CONSOLE_ENABLE
331     }
332
333
334     return 1;
335 }
336