aboutsummaryrefslogtreecommitdiff
path: root/keyboards/bioi/main.c
diff options
context:
space:
mode:
authorscottywei <scot.wei@gmail.com>2021-01-11 10:57:58 +0800
committerGitHub <noreply@github.com>2021-01-10 18:57:58 -0800
commit6edbd845ebda0b67e9d56a21977381949476930a (patch)
tree421096928a897032dbfbce426ef6fec16c225e34 /keyboards/bioi/main.c
parent46b3245d6628a78eefab1b09209021d9b02bcdb9 (diff)
downloadqmk_firmware-6edbd845ebda0b67e9d56a21977381949476930a.tar.gz
qmk_firmware-6edbd845ebda0b67e9d56a21977381949476930a.zip
[Keyboard] Add BIOI Keyboards (#9602)
* Add BIOI Keyboards Add keyboards from BIOI, including dual-mode G60, Morgan65, and S65 * Update keyboards/bioi/g60/config.h Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/bioi/g60/config.h Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/bioi/g60/g60.c Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/bioi/g60/config.h Co-authored-by: Ryan <fauxpark@gmail.com> * Apply suggestions from code review Co-authored-by: Ryan <fauxpark@gmail.com> * Rename rule.mk to rules.mk * Rename rule.mk to rules.mk * Rename rule.mk to rules.mk * Update readme.md * Apply suggestions from code review Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update ble.h * Update config.h Change VenderID to 8101 * Update config.h * Update config.h * Update ble.c * Update ble.h * Add license headers * Apply suggestions from code review Co-authored-by: Drashna Jaelre <drashna@live.com> * Add license header in keymap files * Fix year in license header * Update keyboards/bioi/s65/readme.md Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Ryan <fauxpark@gmail.com> Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'keyboards/bioi/main.c')
-rw-r--r--keyboards/bioi/main.c389
1 files changed, 389 insertions, 0 deletions
diff --git a/keyboards/bioi/main.c b/keyboards/bioi/main.c
new file mode 100644
index 000000000..14f0f8de7
--- /dev/null
+++ b/keyboards/bioi/main.c
@@ -0,0 +1,389 @@
1/*
2Copyright 2019 Basic I/O Instruments(Scott Wei) <scot.wei@gmail.com>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7This program is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU General Public License for more details.
11You should have received a copy of the GNU General Public License
12along with this program. If not, see <http://www.gnu.org/licenses/>.
13*/
14
15#include <avr/pgmspace.h>
16#include <util/delay.h>
17
18#include "report.h"
19#include "host.h"
20#include "host_driver.h"
21#include "keyboard.h"
22#include "action.h"
23#include "led.h"
24#include "sendchar.h"
25#include "debug.h"
26#include "print.h"
27#ifdef SLEEP_LED_ENABLE
28#include "sleep_led.h"
29#endif
30#include "suspend.h"
31
32#include "usb_descriptor.h"
33#include "lufa.h"
34#include "quantum.h"
35#include <util/atomic.h>
36#include "outputselect.h"
37
38#ifdef NKRO_ENABLE
39#include "keycode_config.h"
40
41extern keymap_config_t keymap_config;
42#endif
43
44#ifdef AUDIO_ENABLE
45#include <audio.h>
46#endif
47
48#ifdef BLUETOOTH_ENABLE
49#ifdef MODULE_ADAFRUIT_BLE
50#include "adafruit_ble.h"
51#else
52#include "bluetooth.h"
53#endif
54#endif
55
56#ifdef VIRTSER_ENABLE
57#include "virtser.h"
58#endif
59
60#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
61#include "rgblight.h"
62#endif
63
64#ifdef MIDI_ENABLE
65#include "qmk_midi.h"
66#endif
67
68#ifdef RAW_ENABLE
69#include "raw_hid.h"
70#endif
71
72#include "ble.h"
73#include "usart.h"
74
75#include <avr/power.h>
76#include <avr/sleep.h>
77
78bool force_usb = false; //Reserved for FORCE USB Mode function.
79bool force_ble = false; //Reserved for FORCE USB Mode function.
80
81bool usb_connected = false;
82bool ble_enabled = false;
83
84uint32_t kb_idle_timer = 0;
85
86bool usb_state_sent = false;
87
88uint8_t USB_DeviceLastState = 0;
89
90#ifdef RAW_ENABLE
91/** \brief Raw HID Task
92 *
93 * FIXME: Needs doc
94 */
95static void raw_hid_task(void)
96{
97 // Create a temporary buffer to hold the read in data from the host
98 uint8_t data[RAW_EPSIZE];
99 bool data_read = false;
100
101 // Device must be connected and configured for the task to run
102 if (USB_DeviceState != DEVICE_STATE_Configured)
103 return;
104
105 Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
106
107 // Check to see if a packet has been sent from the host
108 if (Endpoint_IsOUTReceived())
109 {
110 // Check to see if the packet contains data
111 if (Endpoint_IsReadWriteAllowed())
112 {
113 /* Read data */
114 Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
115 data_read = true;
116 }
117
118 // Finalize the stream transfer to receive the last packet
119 Endpoint_ClearOUT();
120
121 if (data_read)
122 {
123 raw_hid_receive(data, sizeof(data));
124 }
125 }
126}
127#endif
128
129static void setup_mcu(void)
130{
131 /* Disable watchdog if enabled by bootloader/fuses */
132 MCUSR &= ~(1 << WDRF);
133 wdt_disable();
134
135 CLKPR = (1 << CLKPCE);
136 CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
137}
138
139static void setup_usb(void)
140{
141 // Leonardo needs. Without this USB device is not recognized.
142 USB_Disable();
143
144 USB_Init();
145
146 // for Console_Task
147 USB_Device_EnableSOFEvents();
148 print_set_sendchar(sendchar);
149}
150
151void power_saving(void)
152{
153 power_adc_disable();
154 power_usart0_disable();
155 power_spi_disable();
156 power_twi_disable();
157
158 USBCON |= (1 << FRZCLK); // Freeze the USB Clock
159 PLLCSR &= ~(1 << PLLE); // Disable the USB Clock (PPL)
160 USBCON &= ~(1 << USBE);
161}
162
163void power_recover(void)
164{
165
166 USBCON |= (1 << USBE);
167 PLLCSR |= (1 << PLLE); // Resume the USB Clock (PPL)
168 USBCON &= ~(1 << FRZCLK); // Resume the USB Clock
169
170 power_adc_enable();
171 power_usart0_enable();
172 power_spi_enable();
173 power_twi_enable();
174}
175
176void ble_task_init(void)
177{
178 kb_idle_timer = timer_read32(); //Mark current time, reserved for further usage;
179}
180
181void ble_task(void)
182{
183
184 if (USB_DeviceLastState != USB_DeviceState)
185 {
186 usb_state_sent = false;
187#ifdef BLE_DEBUG
188 send_str(PSTR("USB State Changed\r\n"));
189 if (USB_DeviceState == DEVICE_STATE_Unattached)
190 {
191 send_str(PSTR("USB State Unattached\r\n"));
192 }
193#endif
194 if (USB_DeviceState == DEVICE_STATE_Powered)
195 {
196#ifdef BLE_DEBUG
197 send_str(PSTR("USB State Powered\r\n"));
198#endif
199 power_recover();
200 host_set_driver(&null_driver);
201 }
202#ifdef BLE_DEBUG
203 if ((USB_DeviceState == DEVICE_STATE_Default))
204 {
205 send_str(PSTR("USB State Default\r\n"));
206 }
207 if ((USB_DeviceState == DEVICE_STATE_Addressed))
208 {
209 send_str(PSTR("USB State Addressed\r\n"));
210 }
211 if (USB_DeviceState == DEVICE_STATE_Configured)
212 {
213 send_str(PSTR("USB State Configured\r\n"));
214 }
215 if (USB_DeviceState > DEVICE_STATE_Unattached)
216 {
217 }
218 else
219 {
220 //
221 }
222#endif
223 }
224 else
225 {
226#ifdef BLE_DEBUG
227 if (!usb_state_sent)
228 {
229 if (USB_DeviceState == DEVICE_STATE_Unattached)
230 {
231 send_str(PSTR("USB State Stopped at Unattached\r\n"));
232 }
233 if (USB_DeviceState == DEVICE_STATE_Powered)
234 {
235 send_str(PSTR("USB State Stopped at Powered\r\n"));
236 }
237 if ((USB_DeviceState == DEVICE_STATE_Default))
238 {
239 send_str(PSTR("USB State Stopped at Default\r\n"));
240 }
241 if ((USB_DeviceState == DEVICE_STATE_Addressed))
242 {
243 send_str(PSTR("USB State Stopped at Addressed\r\n"));
244 }
245 if (USB_DeviceState == DEVICE_STATE_Configured)
246 {
247 send_str(PSTR("USB State Stopped at Configured\r\n"));
248 }
249 }
250#endif
251 if (USB_DeviceState == DEVICE_STATE_Unattached)
252 {
253 if (host_get_driver() && host_get_driver() != &bluefruit_driver)
254 {
255#ifdef BLE_DEBUG
256 send_str(PSTR("USB State stopped at Unattached\r\n"));
257#endif
258 ble_task_init();
259
260 force_usb = 0;
261 usb_connected = 0;
262
263 //Reinit USB to prepare for next connection.
264 USB_Init();
265 USB_Detach();
266 USB_Attach();
267
268#ifdef BLE_DEBUG
269 send_str(PSTR("Loading &bluefruit_driver\r\n"));
270#endif
271 host_set_driver(&bluefruit_driver);
272 clear_keyboard();
273 power_saving();
274 }
275 else
276 {
277 //Do nothing if USB is unattached and the driver is &bluefruit_driver
278 }
279 }
280 if (USB_DeviceState == DEVICE_STATE_Configured)
281 {
282 if (host_get_driver() && host_get_driver() != &lufa_driver)
283 {
284#ifdef BLE_DEBUG
285 send_str(PSTR("USB State stopped at Configured\r\n"));
286#endif
287 power_recover();
288
289 usb_connected = 1;
290 ble_enabled = 0;
291#ifdef BLE_DEBUG
292 send_str(PSTR("Loading &lufa_driver\r\n"));
293#endif
294 host_set_driver(&lufa_driver);
295 clear_keyboard();
296 }
297 else
298 {
299 //Do nothing if the driver is &lufa_driver
300 }
301 }
302
303 usb_state_sent = true;
304 }
305
306 USB_DeviceLastState = USB_DeviceState;
307}
308
309// Use a custom main() function because the task logic is different from the common one.
310int main(void)
311{
312#ifdef MIDI_ENABLE
313 setup_midi();
314#endif
315
316 setup_mcu();
317
318 keyboard_setup();
319
320 setup_usb();
321 sei();
322
323#if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
324 serial_init();
325#endif
326
327 /* wait for USB startup to get ready for debug output */
328 uint8_t timeout = 255; // timeout when USB is not available(Bluetooth)
329 while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured)
330 {
331 wait_ms(4);
332#if defined(INTERRUPT_CONTROL_ENDPOINT)
333 ;
334#else
335 USB_USBTask();
336#endif
337 }
338
339 print("\nUSB init\n");
340
341 keyboard_init();
342 host_set_driver(&lufa_driver);
343
344 backlight_disable();
345 //host_set_driver(&lufa_driver);
346 print("Keyboard initialized.\n");
347
348 //Init Hardware UART
349 usart_init();
350
351#ifdef BLE_DEBUG
352 send_str(PSTR("Keyboard has been setup up\r\n"));
353
354 if (usb_connected)
355 {
356 send_str(PSTR("usb_connected=1\r\n"));
357 }
358 else
359 {
360 send_str(PSTR("usb_connected=0\r\n"));
361 }
362#endif
363
364#ifdef SLEEP_LED_ENABLE
365 sleep_led_init();
366#endif
367
368#ifdef VIRTSER_ENABLE
369 virtser_init();
370#endif
371
372 while (1)
373 {
374 ble_task();
375 keyboard_task();
376
377#ifdef RAW_ENABLE
378 raw_hid_task();
379#endif
380
381#if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE)
382 rgblight_task();
383#endif
384
385#if !defined(INTERRUPT_CONTROL_ENDPOINT)
386 USB_USBTask();
387#endif
388 }
389}