]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/vusb/main.c
Add kb and user level keyboard initialization functions (#3113)
[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
24
25 #define UART_BAUD_RATE 115200
26
27
28 /* This is from main.c of USBaspLoader */
29 static void initForUsbConnectivity(void)
30 {
31     uint8_t i = 0;
32
33     usbInit();
34     /* enforce USB re-enumerate: */
35     usbDeviceDisconnect();  /* do this while interrupts are disabled */
36     while(--i){         /* fake USB disconnect for > 250 ms */
37         wdt_reset();
38         _delay_ms(1);
39     }
40     usbDeviceConnect();
41     sei();
42 }
43
44 int main(void)
45 {
46     bool suspended = false;
47 #if USB_COUNT_SOF
48     uint16_t last_timer = timer_read();
49 #endif
50
51 #ifdef CLKPR
52     // avoid unintentional changes of clock frequency in devices that have a
53     // clock prescaler
54     CLKPR = 0x80, CLKPR = 0;
55 #endif
56 #ifndef NO_UART
57     uart_init(UART_BAUD_RATE);
58 #endif
59     keyboard_setup();
60
61     keyboard_init();
62     host_set_driver(vusb_driver());
63
64     debug("initForUsbConnectivity()\n");
65     initForUsbConnectivity();
66
67     debug("main loop\n");
68     while (1) {
69 #if USB_COUNT_SOF
70         if (usbSofCount != 0) {
71             suspended = false;
72             usbSofCount = 0;
73             last_timer = timer_read();
74         } else {
75             // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
76             if (timer_elapsed(last_timer) > 5) {
77                 suspended = true;
78 /*
79                 uart_putchar('S');
80                 _delay_ms(1);
81                 cli();
82                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
83                 sleep_enable();
84                 sleep_bod_disable();
85                 sei();
86                 sleep_cpu();
87                 sleep_disable();
88                 _delay_ms(10);
89                 uart_putchar('W');
90 */
91             }
92         }
93 #endif
94         if (!suspended) {
95             usbPoll();
96
97             // TODO: configuration process is incosistent. it sometime fails.
98             // To prevent failing to configure NOT scan keyboard during configuration
99             if (usbConfiguration && usbInterruptIsReady()) {
100                 keyboard_task();
101             }
102             vusb_transfer_keyboard();
103         }
104     }
105 }