diff options
Diffstat (limited to 'keyboard/lufa/lufa.c')
| -rw-r--r-- | keyboard/lufa/lufa.c | 129 |
1 files changed, 118 insertions, 11 deletions
diff --git a/keyboard/lufa/lufa.c b/keyboard/lufa/lufa.c index d7e7c1654..d841f45c6 100644 --- a/keyboard/lufa/lufa.c +++ b/keyboard/lufa/lufa.c | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright 2012 Jun Wako <wakojun@gmail.com> | 2 | * Copyright 2012 Jun Wako <wakojun@gmail.com> |
| 3 | * This file is based on LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse. | 3 | * This file is based on: |
| 4 | * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse | ||
| 5 | * LUFA-120219/Demos/Device/Lowlevel/GenericHID | ||
| 4 | */ | 6 | */ |
| 5 | 7 | ||
| 6 | /* | 8 | /* |
| @@ -38,6 +40,8 @@ | |||
| 38 | #include "host.h" | 40 | #include "host.h" |
| 39 | #include "host_driver.h" | 41 | #include "host_driver.h" |
| 40 | #include "keyboard.h" | 42 | #include "keyboard.h" |
| 43 | #include "sendchar.h" | ||
| 44 | #include "debug.h" | ||
| 41 | #include "lufa.h" | 45 | #include "lufa.h" |
| 42 | 46 | ||
| 43 | static uint8_t keyboard_led_stats = 0; | 47 | static uint8_t keyboard_led_stats = 0; |
| @@ -64,11 +68,21 @@ int main(void) | |||
| 64 | SetupHardware(); | 68 | SetupHardware(); |
| 65 | sei(); | 69 | sei(); |
| 66 | 70 | ||
| 71 | print_enable = true; | ||
| 72 | debug_enable = true; | ||
| 73 | debug_matrix = true; | ||
| 74 | debug_keyboard = true; | ||
| 75 | debug_mouse = true; | ||
| 76 | |||
| 77 | _delay_ms(3000); | ||
| 78 | print("abcdefg\n"); | ||
| 79 | |||
| 67 | keyboard_init(); | 80 | keyboard_init(); |
| 68 | host_set_driver(&lufa_driver); | 81 | host_set_driver(&lufa_driver); |
| 69 | while (1) { | 82 | while (1) { |
| 70 | keyboard_proc(); | 83 | keyboard_proc(); |
| 71 | Keyboard_HID_Task(); | 84 | Keyboard_HID_Task(); |
| 85 | Generic_HID_Task(); | ||
| 72 | USB_USBTask(); | 86 | USB_USBTask(); |
| 73 | } | 87 | } |
| 74 | } | 88 | } |
| @@ -112,6 +126,12 @@ void EVENT_USB_Device_ConfigurationChanged(void) | |||
| 112 | /* Setup Mouse HID Report Endpoint */ | 126 | /* Setup Mouse HID Report Endpoint */ |
| 113 | ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | 127 | ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, |
| 114 | HID_EPSIZE, ENDPOINT_BANK_SINGLE); | 128 | HID_EPSIZE, ENDPOINT_BANK_SINGLE); |
| 129 | |||
| 130 | /* Setup Generic HID Report Endpoints */ | ||
| 131 | ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | ||
| 132 | GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE); | ||
| 133 | ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, | ||
| 134 | GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE); | ||
| 115 | } | 135 | } |
| 116 | 136 | ||
| 117 | /** Event handler for the USB_ControlRequest event. | 137 | /** Event handler for the USB_ControlRequest event. |
| @@ -119,8 +139,8 @@ void EVENT_USB_Device_ConfigurationChanged(void) | |||
| 119 | */ | 139 | */ |
| 120 | void EVENT_USB_Device_ControlRequest(void) | 140 | void EVENT_USB_Device_ControlRequest(void) |
| 121 | { | 141 | { |
| 122 | uint8_t* ReportData; | 142 | uint8_t* ReportData = NULL; |
| 123 | uint8_t ReportSize; | 143 | uint8_t ReportSize = 0; |
| 124 | 144 | ||
| 125 | /* Handle HID Class specific requests */ | 145 | /* Handle HID Class specific requests */ |
| 126 | switch (USB_ControlRequest.bRequest) | 146 | switch (USB_ControlRequest.bRequest) |
| @@ -130,16 +150,18 @@ void EVENT_USB_Device_ControlRequest(void) | |||
| 130 | { | 150 | { |
| 131 | Endpoint_ClearSETUP(); | 151 | Endpoint_ClearSETUP(); |
| 132 | 152 | ||
| 133 | /* Determine if it is the mouse or the keyboard data that is being requested */ | 153 | // Interface |
| 134 | if (!(USB_ControlRequest.wIndex)) | 154 | switch (USB_ControlRequest.wIndex) { |
| 135 | { | 155 | case 1: // Keyboard |
| 136 | ReportData = (uint8_t*)&keyboard_report_sent; | 156 | ReportData = (uint8_t*)&keyboard_report_sent; |
| 137 | ReportSize = sizeof(keyboard_report_sent); | 157 | ReportSize = sizeof(keyboard_report_sent); |
| 138 | } | 158 | break; |
| 139 | else | 159 | case 2: // Mouse |
| 140 | { | ||
| 141 | ReportData = (uint8_t*)&mouse_report_sent; | 160 | ReportData = (uint8_t*)&mouse_report_sent; |
| 142 | ReportSize = sizeof(mouse_report_sent); | 161 | ReportSize = sizeof(mouse_report_sent); |
| 162 | break; | ||
| 163 | case 3: // Generic | ||
| 164 | break; | ||
| 143 | } | 165 | } |
| 144 | 166 | ||
| 145 | /* Write the report data to the control endpoint */ | 167 | /* Write the report data to the control endpoint */ |
| @@ -160,8 +182,17 @@ void EVENT_USB_Device_ControlRequest(void) | |||
| 160 | return; | 182 | return; |
| 161 | } | 183 | } |
| 162 | 184 | ||
| 163 | /* Read in the LED report from the host */ | 185 | // Interface |
| 164 | keyboard_led_stats = Endpoint_Read_8(); | 186 | switch (USB_ControlRequest.wIndex) { |
| 187 | case 1: // Keyboard | ||
| 188 | /* Read in the LED report from the host */ | ||
| 189 | keyboard_led_stats = Endpoint_Read_8(); | ||
| 190 | break; | ||
| 191 | case 2: // Mouse | ||
| 192 | break; | ||
| 193 | case 3: // Generic | ||
| 194 | break; | ||
| 195 | } | ||
| 165 | 196 | ||
| 166 | Endpoint_ClearOUT(); | 197 | Endpoint_ClearOUT(); |
| 167 | Endpoint_ClearStatusStage(); | 198 | Endpoint_ClearStatusStage(); |
| @@ -176,6 +207,10 @@ void EVENT_USB_Device_ControlRequest(void) | |||
| 176 | */ | 207 | */ |
| 177 | void Keyboard_HID_Task(void) | 208 | void Keyboard_HID_Task(void) |
| 178 | { | 209 | { |
| 210 | /* Device must be connected and configured for the task to run */ | ||
| 211 | if (USB_DeviceState != DEVICE_STATE_Configured) | ||
| 212 | return; | ||
| 213 | |||
| 179 | /* Select the Keyboard LED Report Endpoint */ | 214 | /* Select the Keyboard LED Report Endpoint */ |
| 180 | Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); | 215 | Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); |
| 181 | 216 | ||
| @@ -190,6 +225,40 @@ void Keyboard_HID_Task(void) | |||
| 190 | } | 225 | } |
| 191 | } | 226 | } |
| 192 | 227 | ||
| 228 | void 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 | } | ||
| 193 | 262 | ||
| 194 | /******************************************************************************* | 263 | /******************************************************************************* |
| 195 | * Host driver | 264 | * Host driver |
| @@ -241,3 +310,41 @@ static void send_system(uint16_t data) | |||
| 241 | static void send_consumer(uint16_t data) | 310 | static void send_consumer(uint16_t data) |
| 242 | { | 311 | { |
| 243 | } | 312 | } |
| 313 | |||
| 314 | |||
| 315 | /******************************************************************************* | ||
| 316 | * sendchar | ||
| 317 | ******************************************************************************/ | ||
| 318 | int8_t sendchar(uint8_t c) | ||
| 319 | { | ||
| 320 | if (USB_DeviceState != DEVICE_STATE_Configured) | ||
| 321 | return -1; | ||
| 322 | |||
| 323 | Endpoint_SelectEndpoint(GENERIC_IN_EPNUM); | ||
| 324 | |||
| 325 | uint8_t timeout = 10; | ||
| 326 | uint16_t prevFN = USB_Device_GetFrameNumber(); | ||
| 327 | while (!Endpoint_IsINReady()) { | ||
| 328 | switch (USB_DeviceState) { | ||
| 329 | case DEVICE_STATE_Unattached: | ||
| 330 | case DEVICE_STATE_Suspended: | ||
| 331 | return -1; | ||
| 332 | } | ||
| 333 | if (Endpoint_IsStalled()) | ||
| 334 | return -1; | ||
| 335 | uint16_t currFN = USB_Device_GetFrameNumber(); | ||
| 336 | if (prevFN != USB_Device_GetFrameNumber()) { | ||
| 337 | if (!(timeout--)) | ||
| 338 | return -1; | ||
| 339 | prevFN = USB_Device_GetFrameNumber(); | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | Endpoint_Write_8(c); | ||
| 344 | |||
| 345 | // send when packet is full | ||
| 346 | if (!Endpoint_IsReadWriteAllowed()) | ||
| 347 | Endpoint_ClearIN(); | ||
| 348 | |||
| 349 | return 0; | ||
| 350 | } | ||
