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