aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/vusb/protocol.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/protocol/vusb/protocol.c')
-rw-r--r--tmk_core/protocol/vusb/protocol.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/tmk_core/protocol/vusb/protocol.c b/tmk_core/protocol/vusb/protocol.c
new file mode 100644
index 000000000..89dc795b2
--- /dev/null
+++ b/tmk_core/protocol/vusb/protocol.c
@@ -0,0 +1,178 @@
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
11#include <stdint.h>
12
13#include <avr/interrupt.h>
14#include <avr/power.h>
15#include <avr/wdt.h>
16#include <avr/sleep.h>
17
18#include <usbdrv/usbdrv.h>
19
20#include "vusb.h"
21
22#include "keyboard.h"
23#include "host.h"
24#include "timer.h"
25#include "print.h"
26#include "suspend.h"
27#include "wait.h"
28#include "sendchar.h"
29
30#ifdef SLEEP_LED_ENABLE
31# include "sleep_led.h"
32#endif
33
34#ifdef CONSOLE_ENABLE
35void console_task(void);
36#endif
37
38#ifdef RAW_ENABLE
39void raw_hid_task(void);
40#endif
41
42/* This is from main.c of USBaspLoader */
43static void initForUsbConnectivity(void) {
44 uint8_t i = 0;
45
46 usbInit();
47 /* enforce USB re-enumerate: */
48 usbDeviceDisconnect(); /* do this while interrupts are disabled */
49 while (--i) { /* fake USB disconnect for > 250 ms */
50 wdt_reset();
51 wait_ms(1);
52 }
53 usbDeviceConnect();
54}
55
56static void vusb_send_remote_wakeup(void) {
57 cli();
58
59 uint8_t ddr_orig = USBDDR;
60 USBOUT |= (1 << USBMINUS);
61 USBDDR = ddr_orig | USBMASK;
62 USBOUT ^= USBMASK;
63
64 wait_ms(25);
65
66 USBOUT ^= USBMASK;
67 USBDDR = ddr_orig;
68 USBOUT &= ~(1 << USBMINUS);
69
70 sei();
71}
72
73bool vusb_suspended = false;
74
75static void vusb_suspend(void) {
76 vusb_suspended = true;
77
78#ifdef SLEEP_LED_ENABLE
79 sleep_led_enable();
80#endif
81
82 suspend_power_down();
83}
84
85#if USB_COUNT_SOF
86static void vusb_wakeup(void) {
87 vusb_suspended = false;
88 suspend_wakeup_init();
89
90# ifdef SLEEP_LED_ENABLE
91 sleep_led_disable();
92# endif
93}
94#endif
95
96/** \brief Setup USB
97 *
98 * FIXME: Needs doc
99 */
100static void setup_usb(void) { initForUsbConnectivity(); }
101
102uint16_t sof_timer = 0;
103
104void protocol_setup(void) {
105#if USB_COUNT_SOF
106 sof_timer = timer_read();
107#endif
108
109#ifdef CLKPR
110 // avoid unintentional changes of clock frequency in devices that have a
111 // clock prescaler
112 clock_prescale_set(clock_div_1);
113#endif
114 keyboard_setup();
115}
116
117void protocol_init(void) {
118 setup_usb();
119 sei();
120
121 keyboard_init();
122
123 host_set_driver(vusb_driver());
124
125 wait_ms(50);
126
127#ifdef SLEEP_LED_ENABLE
128 sleep_led_init();
129#endif
130}
131
132void protocol_task(void) {
133#if USB_COUNT_SOF
134 if (usbSofCount != 0) {
135 usbSofCount = 0;
136 sof_timer = timer_read();
137 if (vusb_suspended) {
138 vusb_wakeup();
139 }
140 } else {
141 // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
142 if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
143 vusb_suspend();
144 }
145 }
146#endif
147 if (vusb_suspended) {
148 vusb_suspend();
149 if (suspend_wakeup_condition()) {
150 vusb_send_remote_wakeup();
151 }
152 } else {
153 usbPoll();
154
155 // TODO: configuration process is inconsistent. it sometime fails.
156 // To prevent failing to configure NOT scan keyboard during configuration
157 if (usbConfiguration && usbInterruptIsReady()) {
158 keyboard_task();
159 }
160 vusb_transfer_keyboard();
161
162#ifdef RAW_ENABLE
163 usbPoll();
164
165 if (usbConfiguration && usbInterruptIsReady3()) {
166 raw_hid_task();
167 }
168#endif
169
170#ifdef CONSOLE_ENABLE
171 usbPoll();
172
173 if (usbConfiguration && usbInterruptIsReady3()) {
174 console_task();
175 }
176#endif
177 }
178}