aboutsummaryrefslogtreecommitdiff
path: root/keyboard/lufa/lufa.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/lufa/lufa.c')
-rw-r--r--keyboard/lufa/lufa.c146
1 files changed, 69 insertions, 77 deletions
diff --git a/keyboard/lufa/lufa.c b/keyboard/lufa/lufa.c
index d841f45c6..569960e2a 100644
--- a/keyboard/lufa/lufa.c
+++ b/keyboard/lufa/lufa.c
@@ -42,11 +42,15 @@
42#include "keyboard.h" 42#include "keyboard.h"
43#include "sendchar.h" 43#include "sendchar.h"
44#include "debug.h" 44#include "debug.h"
45
46#include "descriptor.h"
45#include "lufa.h" 47#include "lufa.h"
46 48
47static uint8_t keyboard_led_stats = 0; 49static uint8_t keyboard_led_stats = 0;
48report_keyboard_t keyboard_report_sent; 50
49report_mouse_t mouse_report_sent; 51// TODO: impl Control Request GET_REPORT
52static report_keyboard_t keyboard_report_sent;
53static report_mouse_t mouse_report_sent;
50 54
51/* Host driver */ 55/* Host driver */
52static uint8_t keyboard_leds(void); 56static uint8_t keyboard_leds(void);
@@ -63,6 +67,9 @@ static host_driver_t lufa_driver = {
63}; 67};
64 68
65 69
70static void SetupHardware(void);
71static void Generic_HID_Task(void);
72
66int main(void) 73int main(void)
67{ 74{
68 SetupHardware(); 75 SetupHardware();
@@ -74,20 +81,23 @@ int main(void)
74 debug_keyboard = true; 81 debug_keyboard = true;
75 debug_mouse = true; 82 debug_mouse = true;
76 83
77 _delay_ms(3000); 84/* TODO: can't print here
85 _delay_ms(5000);
86 USB_USBTask();
78 print("abcdefg\n"); 87 print("abcdefg\n");
88 USB_USBTask();
89*/
79 90
80 keyboard_init(); 91 keyboard_init();
81 host_set_driver(&lufa_driver); 92 host_set_driver(&lufa_driver);
82 while (1) { 93 while (1) {
83 keyboard_proc(); 94 keyboard_proc();
84 Keyboard_HID_Task(); 95
85 Generic_HID_Task(); 96 Generic_HID_Task();
86 USB_USBTask(); 97 USB_USBTask();
87 } 98 }
88} 99}
89 100
90/** Configures the board hardware and chip peripherals for the demo's functionality. */
91void SetupHardware(void) 101void SetupHardware(void)
92{ 102{
93 /* Disable watchdog if enabled by bootloader/fuses */ 103 /* Disable watchdog if enabled by bootloader/fuses */
@@ -100,6 +110,46 @@ void SetupHardware(void)
100 USB_Init(); 110 USB_Init();
101} 111}
102 112
113static void Generic_HID_Task(void)
114{
115 /* Device must be connected and configured for the task to run */
116 if (USB_DeviceState != DEVICE_STATE_Configured)
117 return;
118
119 // TODO: impl receivechar()/recvchar()
120 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
121
122 /* Check to see if a packet has been sent from the host */
123 if (Endpoint_IsOUTReceived())
124 {
125 /* Check to see if the packet contains data */
126 if (Endpoint_IsReadWriteAllowed())
127 {
128 /* Create a temporary buffer to hold the read in report from the host */
129 uint8_t GenericData[GENERIC_REPORT_SIZE];
130
131 /* Read Generic Report Data */
132 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL);
133
134 /* Process Generic Report Data */
135 //ProcessGenericHIDReport(GenericData);
136 }
137
138 /* Finalize the stream transfer to send the last packet */
139 Endpoint_ClearOUT();
140 }
141
142 /* IN packet */
143 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
144 // send IN packet
145 if (Endpoint_IsINReady())
146 Endpoint_ClearIN();
147}
148
149
150/*******************************************************************************
151 * USB Events
152 ******************************************************************************/
103/** Event handler for the USB_Connect event. */ 153/** Event handler for the USB_Connect event. */
104void EVENT_USB_Device_Connect(void) 154void EVENT_USB_Device_Connect(void)
105{ 155{
@@ -120,18 +170,16 @@ void EVENT_USB_Device_ConfigurationChanged(void)
120 /* Setup Keyboard HID Report Endpoints */ 170 /* Setup Keyboard HID Report Endpoints */
121 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 171 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
122 HID_EPSIZE, ENDPOINT_BANK_SINGLE); 172 HID_EPSIZE, ENDPOINT_BANK_SINGLE);
123 ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
124 HID_EPSIZE, ENDPOINT_BANK_SINGLE);
125 173
126 /* Setup Mouse HID Report Endpoint */ 174 /* Setup Mouse HID Report Endpoint */
127 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 175 ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
128 HID_EPSIZE, ENDPOINT_BANK_SINGLE); 176 HID_EPSIZE, ENDPOINT_BANK_SINGLE);
129 177
130 /* Setup Generic HID Report Endpoints */ 178 /* Setup Generic HID Report Endpoints */
131 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 179 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
132 GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE); 180 GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE);
133 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, 181 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
134 GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE); 182 GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE);
135} 183}
136 184
137/** Event handler for the USB_ControlRequest event. 185/** Event handler for the USB_ControlRequest event.
@@ -152,15 +200,17 @@ void EVENT_USB_Device_ControlRequest(void)
152 200
153 // Interface 201 // Interface
154 switch (USB_ControlRequest.wIndex) { 202 switch (USB_ControlRequest.wIndex) {
155 case 1: // Keyboard 203 case KEYBOARD_INTERFACE:
204 // TODO: test/check
156 ReportData = (uint8_t*)&keyboard_report_sent; 205 ReportData = (uint8_t*)&keyboard_report_sent;
157 ReportSize = sizeof(keyboard_report_sent); 206 ReportSize = sizeof(keyboard_report_sent);
158 break; 207 break;
159 case 2: // Mouse 208 case MOUSE_INTERFACE:
209 // TODO: test/check
160 ReportData = (uint8_t*)&mouse_report_sent; 210 ReportData = (uint8_t*)&mouse_report_sent;
161 ReportSize = sizeof(mouse_report_sent); 211 ReportSize = sizeof(mouse_report_sent);
162 break; 212 break;
163 case 3: // Generic 213 case GENERIC_INTERFACE:
164 break; 214 break;
165 } 215 }
166 216
@@ -184,13 +234,14 @@ void EVENT_USB_Device_ControlRequest(void)
184 234
185 // Interface 235 // Interface
186 switch (USB_ControlRequest.wIndex) { 236 switch (USB_ControlRequest.wIndex) {
187 case 1: // Keyboard 237 case KEYBOARD_INTERFACE:
238 // TODO: test/check
188 /* Read in the LED report from the host */ 239 /* Read in the LED report from the host */
189 keyboard_led_stats = Endpoint_Read_8(); 240 keyboard_led_stats = Endpoint_Read_8();
190 break; 241 break;
191 case 2: // Mouse 242 case MOUSE_INTERFACE:
192 break; 243 break;
193 case 3: // Generic 244 case GENERIC_INTERFACE:
194 break; 245 break;
195 } 246 }
196 247
@@ -202,64 +253,6 @@ void EVENT_USB_Device_ControlRequest(void)
202 } 253 }
203} 254}
204 255
205/** Keyboard task.
206 * This processes host LED status reports sent to the device via the keyboard OUT reporting endpoint.
207 */
208void Keyboard_HID_Task(void)
209{
210 /* Device must be connected and configured for the task to run */
211 if (USB_DeviceState != DEVICE_STATE_Configured)
212 return;
213
214 /* Select the Keyboard LED Report Endpoint */
215 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);
216
217 /* Check if Keyboard LED Endpoint Ready for Read/Write */
218 if (Endpoint_IsReadWriteAllowed())
219 {
220 /* Read in the LED report from the host */
221 keyboard_led_stats = Endpoint_Read_8();
222
223 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
224 Endpoint_ClearOUT();
225 }
226}
227
228void Generic_HID_Task(void)
229{
230 /* Device must be connected and configured for the task to run */
231 if (USB_DeviceState != DEVICE_STATE_Configured)
232 return;
233
234 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
235
236 /* Check to see if a packet has been sent from the host */
237 if (Endpoint_IsOUTReceived())
238 {
239 /* Check to see if the packet contains data */
240 if (Endpoint_IsReadWriteAllowed())
241 {
242 /* Create a temporary buffer to hold the read in report from the host */
243 uint8_t GenericData[GENERIC_REPORT_SIZE];
244
245 /* Read Generic Report Data */
246 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL);
247
248 /* Process Generic Report Data */
249 //TODO: ProcessGenericHIDReport(GenericData);
250 }
251
252 /* Finalize the stream transfer to send the last packet */
253 Endpoint_ClearOUT();
254 }
255
256 /* IN packet */
257 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
258 // send IN packet
259 if (Endpoint_IsINReady())
260 Endpoint_ClearIN();
261}
262
263/******************************************************************************* 256/*******************************************************************************
264 * Host driver 257 * Host driver
265 ******************************************************************************/ 258 ******************************************************************************/
@@ -332,7 +325,6 @@ int8_t sendchar(uint8_t c)
332 } 325 }
333 if (Endpoint_IsStalled()) 326 if (Endpoint_IsStalled())
334 return -1; 327 return -1;
335 uint16_t currFN = USB_Device_GetFrameNumber();
336 if (prevFN != USB_Device_GetFrameNumber()) { 328 if (prevFN != USB_Device_GetFrameNumber()) {
337 if (!(timeout--)) 329 if (!(timeout--))
338 return -1; 330 return -1;