Move rgblight and backlight task to common location (#7733)
[jackhill/qmk/firmware.git] / tmk_core / protocol / vusb / main.c
1 /* Name: main.c
2 * Project: hid-mouse, a very simple HID example
3 * Author: Christian Starkjohann
4 * Creation Date: 2008-04-07
5 * Tabsize: 4
6 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $
9 */
10 #include <stdint.h>
11 #include <avr/interrupt.h>
12 #include <avr/wdt.h>
13 #include <avr/sleep.h>
14 #include <util/delay.h>
15 #include "usbdrv.h"
16 #include "oddebug.h"
17 #include "vusb.h"
18 #include "keyboard.h"
19 #include "host.h"
20 #include "timer.h"
21 #include "uart.h"
22 #include "debug.h"
23 #include "rgblight_reconfig.h"
24
25 #if (defined(RGB_MIDI) || defined(RGBLIGHT_ANIMATIONS)) && defined(RGBLIGHT_ENABLE)
26 # include "rgblight.h"
27 #endif
28
29 #define UART_BAUD_RATE 115200
30
31 /* This is from main.c of USBaspLoader */
32 static void initForUsbConnectivity(void) {
33 uint8_t i = 0;
34
35 usbInit();
36 /* enforce USB re-enumerate: */
37 usbDeviceDisconnect(); /* do this while interrupts are disabled */
38 while (--i) { /* fake USB disconnect for > 250 ms */
39 wdt_reset();
40 _delay_ms(1);
41 }
42 usbDeviceConnect();
43 sei();
44 }
45
46 int main(void) {
47 bool suspended = false;
48 #if USB_COUNT_SOF
49 uint16_t last_timer = timer_read();
50 #endif
51
52 #ifdef CLKPR
53 // avoid unintentional changes of clock frequency in devices that have a
54 // clock prescaler
55 CLKPR = 0x80, CLKPR = 0;
56 #endif
57 #ifndef NO_UART
58 uart_init(UART_BAUD_RATE);
59 #endif
60 keyboard_setup();
61
62 keyboard_init();
63 host_set_driver(vusb_driver());
64
65 debug("initForUsbConnectivity()\n");
66 initForUsbConnectivity();
67
68 debug("main loop\n");
69 while (1) {
70 #if USB_COUNT_SOF
71 if (usbSofCount != 0) {
72 suspended = false;
73 usbSofCount = 0;
74 last_timer = timer_read();
75 } else {
76 // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
77 if (timer_elapsed(last_timer) > 5) {
78 suspended = true;
79 /*
80 uart_putchar('S');
81 _delay_ms(1);
82 cli();
83 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
84 sleep_enable();
85 sleep_bod_disable();
86 sei();
87 sleep_cpu();
88 sleep_disable();
89 _delay_ms(10);
90 uart_putchar('W');
91 */
92 }
93 }
94 #endif
95 if (!suspended) {
96 usbPoll();
97
98 // TODO: configuration process is incosistent. it sometime fails.
99 // To prevent failing to configure NOT scan keyboard during configuration
100 if (usbConfiguration && usbInterruptIsReady()) {
101 keyboard_task();
102 }
103 vusb_transfer_keyboard();
104 }
105 }
106 }