]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/vusb/main.c
Remove MCU dependent code from common/keyboard.c
[tmk_firmware.git] / 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     CLKPR = 0x80, CLKPR = 0;
52 #ifndef PS2_USE_USART
53     uart_init(UART_BAUD_RATE);
54 #endif
55
56     keyboard_init();
57     host_set_driver(vusb_driver());
58
59     debug("initForUsbConnectivity()\n");
60     initForUsbConnectivity();
61
62     debug("main loop\n");
63     while (1) {
64 #if USB_COUNT_SOF
65         if (usbSofCount != 0) {
66             suspended = false;
67             usbSofCount = 0;
68             last_timer = timer_read();
69         } else {
70             // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
71             if (timer_elapsed(last_timer) > 5) {
72                 suspended = true;
73 /*
74                 uart_putchar('S');
75                 _delay_ms(1);
76                 cli();
77                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
78                 sleep_enable();
79                 sleep_bod_disable();
80                 sei();
81                 sleep_cpu();
82                 sleep_disable();
83                 _delay_ms(10);
84                 uart_putchar('W');
85 */
86             }
87         }
88 #endif
89         if (!suspended) {
90             usbPoll();
91
92             // TODO: configuration process is incosistent. it sometime fails.
93             // To prevent failing to configure NOT scan keyboard during configuration
94             if (usbConfiguration && usbInterruptIsReady()) {
95                 keyboard_task();
96             }
97             vusb_transfer_keyboard();
98         }
99     }
100 }