2 Copyright 2018 Massdrop Inc.
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.
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.
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/>.
18 #include "samd51j18a.h"
19 #include "tmk_core/common/keyboard.h"
23 #include "host_driver.h"
24 #include "keycode_config.h"
28 //From protocol directory
29 #include "arm_atsam_protocol.h"
31 //From keyboard's directory
32 #include "config_led.h"
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);
40 host_driver_t arm_atsam_driver = {
50 uint8_t keyboard_leds(void)
53 if (keymap_config.nkro)
54 return udi_hid_nkro_report_set;
57 return udi_hid_kbd_report_set;
60 void send_keyboard(report_keyboard_t *report)
65 if (!keymap_config.nkro)
70 irqflags = __get_PRIMASK();
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();
79 __set_PRIMASK(irqflags);
86 irqflags = __get_PRIMASK();
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();
95 __set_PRIMASK(irqflags);
100 void send_mouse(report_mouse_t *report)
102 #ifdef MOUSEKEY_ENABLE
107 irqflags = __get_PRIMASK();
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();
116 __set_PRIMASK(irqflags);
117 #endif //MOUSEKEY_ENABLE
120 void send_system(uint16_t data)
122 #ifdef EXTRAKEY_ENABLE
123 dprintf("s-exks %i\r\n", data);
127 irqflags = __get_PRIMASK();
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();
138 __set_PRIMASK(irqflags);
139 #endif //EXTRAKEY_ENABLE
142 void send_consumer(uint16_t data)
144 #ifdef EXTRAKEY_ENABLE
145 dprintf("s-exkc %i\r\n",data);
149 irqflags = __get_PRIMASK();
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();
159 __set_PRIMASK(irqflags);
160 #endif //EXTRAKEY_ENABLE
182 DBGC(DC_MAIN_UDC_START_BEGIN);
184 DBGC(DC_MAIN_UDC_START_COMPLETE);
186 DBGC(DC_MAIN_CDC_INIT_BEGIN);
188 DBGC(DC_MAIN_CDC_INIT_COMPLETE);
190 while (USB2422_Port_Detect_Init() == 0) {}
197 while (I2C3733_Init_Control() != 1) {}
198 while (I2C3733_Init_Drivers() != 1) {}
205 for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
206 I2C_LED_Q_ONOFF(drvid); //Queue data
212 host_set_driver(&arm_atsam_driver);
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;
220 v_5v_avg = adc_get(ADC_5V);
222 debug_code_disable();
226 if (usb_state == USB_STATE_POWERDOWN)
231 for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
233 I2C3733_Control_Set(0);
236 while (usb_state == USB_STATE_POWERDOWN) {}
239 for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++)
241 I2C3733_Control_Set(1);
251 if (CLK_get_ms() > next_5v_checkup)
253 next_5v_checkup = CLK_get_ms() + 5;
255 v_5v = adc_get(ADC_5V);
256 v_5v_avg = 0.9 * v_5v_avg + 0.1 * v_5v;
261 if (CLK_get_ms() > next_usb_checkup)
263 next_usb_checkup = CLK_get_ms() + 10;
265 USB_HandleExtraDevice();
268 #ifdef VIRTSER_ENABLE
269 if (CLK_get_ms() > next_print)
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);
274 #endif //VIRTSER_ENABLE