]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/main_arm_atsam.c
Massdrop keyboard updates for SEND_STRING, syscalls, stdio, debug prints, Auto Shift...
[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 void main_subtasks(void);
35 uint8_t keyboard_leds(void);
36 void send_keyboard(report_keyboard_t *report);
37 void send_mouse(report_mouse_t *report);
38 void send_system(uint16_t data);
39 void send_consumer(uint16_t data);
40
41 host_driver_t arm_atsam_driver = {
42     keyboard_leds,
43     send_keyboard,
44     send_mouse,
45     send_system,
46     send_consumer
47 };
48
49 uint8_t led_states;
50
51 uint8_t keyboard_leds(void)
52 {
53 #ifdef NKRO_ENABLE
54     if (keymap_config.nkro)
55         return udi_hid_nkro_report_set;
56     else
57 #endif //NKRO_ENABLE
58         return udi_hid_kbd_report_set;
59 }
60
61 void send_keyboard(report_keyboard_t *report)
62 {
63     uint32_t irqflags;
64
65 #ifdef NKRO_ENABLE
66     if (!keymap_config.nkro)
67     {
68 #endif //NKRO_ENABLE
69         while (udi_hid_kbd_b_report_trans_ongoing) { main_subtasks(); } //Run other tasks while waiting for USB to be free
70
71         irqflags = __get_PRIMASK();
72         __disable_irq();
73         __DMB();
74
75         memcpy(udi_hid_kbd_report, report->raw, UDI_HID_KBD_REPORT_SIZE);
76         udi_hid_kbd_b_report_valid = 1;
77         udi_hid_kbd_send_report();
78
79         __DMB();
80         __set_PRIMASK(irqflags);
81 #ifdef NKRO_ENABLE
82     }
83     else
84     {
85         while (udi_hid_nkro_b_report_trans_ongoing) { main_subtasks(); } //Run other tasks while waiting for USB to be free
86
87         irqflags = __get_PRIMASK();
88         __disable_irq();
89         __DMB();
90
91         memcpy(udi_hid_nkro_report, report->raw, UDI_HID_NKRO_REPORT_SIZE);
92         udi_hid_nkro_b_report_valid = 1;
93         udi_hid_nkro_send_report();
94
95         __DMB();
96         __set_PRIMASK(irqflags);
97     }
98 #endif //NKRO_ENABLE
99 }
100
101 void send_mouse(report_mouse_t *report)
102 {
103 #ifdef MOUSEKEY_ENABLE
104     uint32_t irqflags;
105
106     irqflags = __get_PRIMASK();
107     __disable_irq();
108     __DMB();
109
110     memcpy(udi_hid_mou_report, report, UDI_HID_MOU_REPORT_SIZE);
111     udi_hid_mou_b_report_valid = 1;
112     udi_hid_mou_send_report();
113
114     __DMB();
115     __set_PRIMASK(irqflags);
116 #endif //MOUSEKEY_ENABLE
117 }
118
119 void send_system(uint16_t data)
120 {
121 #ifdef EXTRAKEY_ENABLE
122     uint32_t irqflags;
123
124     irqflags = __get_PRIMASK();
125     __disable_irq();
126     __DMB();
127
128     udi_hid_exk_report.desc.report_id = REPORT_ID_SYSTEM;
129     if (data != 0) data = data - SYSTEM_POWER_DOWN + 1;
130     udi_hid_exk_report.desc.report_data = data;
131     udi_hid_exk_b_report_valid = 1;
132     udi_hid_exk_send_report();
133
134     __DMB();
135     __set_PRIMASK(irqflags);
136 #endif //EXTRAKEY_ENABLE
137 }
138
139 void send_consumer(uint16_t data)
140 {
141 #ifdef EXTRAKEY_ENABLE
142     uint32_t irqflags;
143
144     irqflags = __get_PRIMASK();
145     __disable_irq();
146     __DMB();
147
148     udi_hid_exk_report.desc.report_id = REPORT_ID_CONSUMER;
149     udi_hid_exk_report.desc.report_data = data;
150     udi_hid_exk_b_report_valid = 1;
151     udi_hid_exk_send_report();
152
153     __DMB();
154     __set_PRIMASK(irqflags);
155 #endif //EXTRAKEY_ENABLE
156 }
157
158 uint8_t g_drvid;
159
160 void main_subtask_usb_state(void)
161 {
162     if (usb_state == USB_STATE_POWERDOWN)
163     {
164         uint32_t timer_led = timer_read32();
165
166         led_on;
167         if (led_enabled)
168         {
169             for (g_drvid = 0; g_drvid < ISSI3733_DRIVER_COUNT; g_drvid++)
170             {
171                 I2C3733_Control_Set(0);
172             }
173         }
174         while (usb_state == USB_STATE_POWERDOWN)
175         {
176             if (timer_read32() - timer_led > 1000) led_off; //Good to indicate went to sleep, but only for a second
177         }
178         if (led_enabled)
179         {
180             for (g_drvid = 0; g_drvid < ISSI3733_DRIVER_COUNT; g_drvid++)
181             {
182                 I2C3733_Control_Set(1);
183             }
184         }
185         led_off;
186     }
187 }
188
189 void main_subtask_led(void)
190 {
191     led_matrix_task();
192 }
193
194 void main_subtask_power_check(void)
195 {
196     static uint64_t next_5v_checkup = 0;
197
198     if (CLK_get_ms() > next_5v_checkup)
199     {
200         next_5v_checkup = CLK_get_ms() + 5;
201
202         v_5v = adc_get(ADC_5V);
203         v_5v_avg = 0.9 * v_5v_avg + 0.1 * v_5v;
204
205         gcr_compute();
206     }
207 }
208
209 void main_subtask_usb_extra_device(void)
210 {
211     static uint64_t next_usb_checkup = 0;
212
213     if (CLK_get_ms() > next_usb_checkup)
214     {
215         next_usb_checkup = CLK_get_ms() + 10;
216
217         USB_HandleExtraDevice();
218     }
219 }
220
221 void main_subtasks(void)
222 {
223     main_subtask_usb_state();
224     main_subtask_led();
225     main_subtask_power_check();
226     main_subtask_usb_extra_device();
227 }
228
229 int main(void)
230 {
231     led_ena;
232     m15_ena;
233
234     debug_code_init();
235
236     CLK_init();
237
238     ADC0_init();
239
240     SPI_Init();
241
242     i2c1_init();
243
244     matrix_init();
245
246     USB2422_init();
247
248     DBGC(DC_MAIN_UDC_START_BEGIN);
249     udc_start();
250     DBGC(DC_MAIN_UDC_START_COMPLETE);
251
252     DBGC(DC_MAIN_CDC_INIT_BEGIN);
253     CDC_init();
254     DBGC(DC_MAIN_CDC_INIT_COMPLETE);
255
256     while (USB2422_Port_Detect_Init() == 0) {}
257
258     led_off;
259     m15_off;
260
261     led_matrix_init();
262
263     while (I2C3733_Init_Control() != 1) {}
264     while (I2C3733_Init_Drivers() != 1) {}
265
266     I2C_DMAC_LED_Init();
267
268     i2c_led_q_init();
269
270     for (g_drvid = 0; g_drvid < ISSI3733_DRIVER_COUNT; g_drvid++)
271         I2C_LED_Q_ONOFF(g_drvid); //Queue data
272
273     keyboard_setup();
274
275     keyboard_init();
276
277     host_set_driver(&arm_atsam_driver);
278
279 #ifdef VIRTSER_ENABLE
280     uint64_t next_print = 0;
281 #endif //VIRTSER_ENABLE
282
283     v_5v_avg = adc_get(ADC_5V);
284
285     debug_code_disable();
286
287     while (1)
288     {
289         keyboard_task();
290
291         main_subtasks(); //Note these tasks will also be run while waiting for USB keyboard polling intervals
292
293 #ifdef VIRTSER_ENABLE
294         if (CLK_get_ms() > next_print)
295         {
296             next_print = CLK_get_ms() + 250;
297             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);
298         }
299 #endif //VIRTSER_ENABLE
300     }
301
302     return 1;
303 }
304