diff --git a/demos/host/keyboard_app.c b/demos/host/keyboard_app.c index cb3f569ff..3b4e0beb4 100644 --- a/demos/host/keyboard_app.c +++ b/demos/host/keyboard_app.c @@ -39,21 +39,85 @@ // INCLUDE //--------------------------------------------------------------------+ #include "keyboard_app.h" + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ +#define QUEUE_KEYBOARD_REPORT_DEPTH 5 //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -tusb_keyboard_report_t keyboard_report TUSB_CFG_ATTR_USBRAM; +static tusb_keyboard_report_t usb_keyboard_report TUSB_CFG_ATTR_USBRAM; -//--------------------------------------------------------------------+ -// IMPLEMENTATION -//--------------------------------------------------------------------+ +OSAL_QUEUE_DEF(queue_kbd_report, QUEUE_KEYBOARD_REPORT_DEPTH, tusb_keyboard_report_t); +static osal_queue_handle_t q_kbd_report_hdl; // only convert a-z (case insensitive) + 0-9 static inline uint8_t keycode_to_ascii(uint8_t keycode) ATTR_CONST ATTR_ALWAYS_INLINE; + +//--------------------------------------------------------------------+ +// tinyusb callback (ISR context) +//--------------------------------------------------------------------+ +void tusbh_hid_keyboard_isr(uint8_t dev_addr, uint8_t instance_num, tusb_event_t event) +{ + switch(event) + { + case TUSB_EVENT_INTERFACE_OPEN: // application set-up + osal_queue_flush(q_kbd_report_hdl); + tusbh_hid_keyboard_get_report(dev_addr, instance_num, (uint8_t*) &usb_keyboard_report); // first report + break; + + case TUSB_EVENT_INTERFACE_CLOSE: // application tear-down + + break; + + case TUSB_EVENT_XFER_COMPLETE: + osal_queue_send(q_kbd_report_hdl, &usb_keyboard_report); + tusbh_hid_keyboard_get_report(dev_addr, instance_num, (uint8_t*) &usb_keyboard_report); + break; + + case TUSB_EVENT_XFER_ERROR: + tusbh_hid_keyboard_get_report(dev_addr, instance_num, (uint8_t*) &usb_keyboard_report); // ignore & continue + break; + + default : + break; + } +} + +//--------------------------------------------------------------------+ +// APPLICATION +//--------------------------------------------------------------------+ +void keyboard_app_init(void) +{ + q_kbd_report_hdl = osal_queue_create(&queue_kbd_report); + + // TODO keyboard_app_task create +} + +//------------- main task -------------// +OSAL_TASK_DECLARE( keyboard_app_task ) +{ + tusb_error_t error; + tusb_keyboard_report_t kbd_report; + + OSAL_TASK_LOOP_BEGIN + + osal_queue_receive(q_kbd_report_hdl, &kbd_report, OSAL_TIMEOUT_WAIT_FOREVER, &error); + + for(uint8_t i=0; i<6; i++) + { + if ( kbd_report.keycode[i] != 0 ) + printf("%c", keycode_to_ascii(kbd_report.keycode[i])); + } + + OSAL_TASK_LOOP_END +} + +//--------------------------------------------------------------------+ +// HELPER +//--------------------------------------------------------------------+ static inline uint8_t keycode_to_ascii(uint8_t keycode) { return @@ -61,62 +125,3 @@ static inline uint8_t keycode_to_ascii(uint8_t keycode) ( KEYBOARD_KEYCODE_1 <= keycode && keycode < KEYBOARD_KEYCODE_0) ? ( (keycode - KEYBOARD_KEYCODE_1) + '1' ) : ( KEYBOARD_KEYCODE_0 == keycode) ? '0' : 'x'; } - - -//--------------------------------------------------------------------+ -// tinyusb callback (ISR context) -//--------------------------------------------------------------------+ -//void tusbh_hid_keyboard_isr(uint8_t dev_addr, uint8_t instance_num, tusb_event_t event) -//{ -// switch(event) -// { -// case TUSB_EVENT_INTERFACE_OPEN: -// tusbh_hid_keyboard_get_report(dev_addr, 0, (uint8_t*) &keyboard_report); // first report -// break; -// -// case TUSB_EVENT_INTERFACE_CLOSE: -// -// break; -// -// case TUSB_EVENT_XFER_COMPLETE: -// tusbh_hid_keyboard_get_report(dev_addr, 0, (uint8_t*) &keyboard_report); -// break; -// -// case TUSB_EVENT_XFER_ERROR: -// break; -// -// default: -// } -//} - -void keyboard_app_task(void) -{ - for (uint8_t dev_addr = 1; dev_addr <= TUSB_CFG_HOST_DEVICE_MAX; dev_addr++) - { - if ( tusbh_hid_keyboard_is_supported(dev_addr) ) - { - switch (tusbh_hid_keyboard_status(dev_addr,0)) - { - case TUSB_INTERFACE_STATUS_READY: - case TUSB_INTERFACE_STATUS_ERROR: // skip error, get next key - tusbh_hid_keyboard_get_report(dev_addr, 0, (uint8_t*) &keyboard_report); - break; - - case TUSB_INTERFACE_STATUS_COMPLETE: - // TODO buffer in queue - for(uint8_t i=0; i<6; i++) - { - if ( keyboard_report.keycode[i] != 0 ) - printf("%c", keycode_to_ascii(keyboard_report.keycode[i])); - } - memclr_(&keyboard_report, sizeof(tusb_keyboard_report_t)); // TODO use callback to synchronize - tusbh_hid_keyboard_get_report(dev_addr, 0, (uint8_t*) &keyboard_report); - break; - - case TUSB_INTERFACE_STATUS_BUSY: - break; - - } - } - } -} diff --git a/demos/host/keyboard_app.h b/demos/host/keyboard_app.h index f7a257292..23109ca51 100644 --- a/demos/host/keyboard_app.h +++ b/demos/host/keyboard_app.h @@ -51,12 +51,16 @@ #ifndef _TUSB_KEYBOARD_APP_H_ #define _TUSB_KEYBOARD_APP_H_ +#include +#include + +#include "boards/board.h" +#include "tusb.h" + #ifdef __cplusplus extern "C" { #endif -#include "boards/board.h" -#include "tusb.h" #ifdef __cplusplus } diff --git a/demos/host/main.c b/demos/host/main.c index bb2084a59..7b935670c 100644 --- a/demos/host/main.c +++ b/demos/host/main.c @@ -21,6 +21,10 @@ int main(void) board_init(); tusb_init(); + //------------- application task init -------------// + keyboard_app_init(); + mouse_app_init(); + printf("reset\n"); while (1) { diff --git a/demos/host/mouse_app.c b/demos/host/mouse_app.c index b3df965bc..d1c37e245 100644 --- a/demos/host/mouse_app.c +++ b/demos/host/mouse_app.c @@ -39,50 +39,80 @@ // INCLUDE //--------------------------------------------------------------------+ #include "mouse_app.h" + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ +#define QUEUE_MOUSE_REPORT_DEPTH 5 //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -tusb_mouse_report_t mouse_report TUSB_CFG_ATTR_USBRAM; +static tusb_mouse_report_t usb_mouse_report TUSB_CFG_ATTR_USBRAM; + +OSAL_QUEUE_DEF(queue_mouse_report, QUEUE_MOUSE_REPORT_DEPTH, tusb_mouse_report_t); +static osal_queue_handle_t q_mouse_report_hdl; //--------------------------------------------------------------------+ -// IMPLEMENTATION +// tinyusb callback (ISR context) //--------------------------------------------------------------------+ -void mouse_app_task(void) +void tusbh_hid_mouse_isr(uint8_t dev_addr, uint8_t instance_num, tusb_event_t event) { - for (uint8_t dev_addr = 1; dev_addr <= TUSB_CFG_HOST_DEVICE_MAX; dev_addr++) + switch(event) { - if ( tusbh_hid_mouse_is_supported(dev_addr) ) - { - switch (tusbh_hid_mouse_status(dev_addr,0)) - { - case TUSB_INTERFACE_STATUS_READY: - case TUSB_INTERFACE_STATUS_ERROR: // skip error, get next key - tusbh_hid_mouse_get_report(dev_addr, 0, (uint8_t*) &mouse_report); - break; + case TUSB_EVENT_INTERFACE_OPEN: // application set-up + osal_queue_flush(q_mouse_report_hdl); + tusbh_hid_mouse_get_report(dev_addr, instance_num, (uint8_t*) &usb_mouse_report); // first report + break; - case TUSB_INTERFACE_STATUS_COMPLETE: - // TODO buffer in queue - if ( mouse_report.buttons || mouse_report.x || mouse_report.y) - { - printf("buttons: %c%c%c (x, y) = (%d, %d)\n", - mouse_report.buttons & HID_MOUSEBUTTON_LEFT ? 'L' : '-', - mouse_report.buttons & HID_MOUSEBUTTON_MIDDLE ? 'M' : '-', - mouse_report.buttons & HID_MOUSEBUTTON_RIGHT ? 'R' : '-', - mouse_report.x, mouse_report.y); - } + case TUSB_EVENT_INTERFACE_CLOSE: // application tear-down - memclr_(&mouse_report, sizeof(tusb_mouse_report_t)); // TODO use callback to synchronize - tusbh_hid_mouse_get_report(dev_addr, 0, (uint8_t*) &mouse_report); - break; + break; - case TUSB_INTERFACE_STATUS_BUSY: - break; + case TUSB_EVENT_XFER_COMPLETE: + osal_queue_send(q_mouse_report_hdl, &usb_mouse_report); + tusbh_hid_mouse_get_report(dev_addr, instance_num, (uint8_t*) &usb_mouse_report); + break; - } - } + case TUSB_EVENT_XFER_ERROR: + tusbh_hid_mouse_get_report(dev_addr, instance_num, (uint8_t*) &usb_mouse_report); // ignore & continue + break; + + default : + break; } } + +//--------------------------------------------------------------------+ +// APPLICATION +// NOTICE: MOUSE REPORT IS NOT CORRECT UNTIL A DECENT HID PARSER IS +// IMPLEMENTED, MEANWHILE IT CAN MISS DISPLAY BUTTONS OR X,Y etc +//--------------------------------------------------------------------+ +void mouse_app_init(void) +{ + q_mouse_report_hdl = osal_queue_create(&queue_mouse_report); + + // TODO mouse_app_task create +} + +//------------- main task -------------// +OSAL_TASK_DECLARE( mouse_app_task ) +{ + tusb_error_t error; + tusb_mouse_report_t mouse_report; + + OSAL_TASK_LOOP_BEGIN + + osal_queue_receive(q_mouse_report_hdl, &mouse_report, OSAL_TIMEOUT_WAIT_FOREVER, &error); + + if ( mouse_report.buttons || mouse_report.x || mouse_report.y) + { + printf("buttons: %c%c%c (x, y) = (%d, %d)\n", + mouse_report.buttons & HID_MOUSEBUTTON_LEFT ? 'L' : '-', + mouse_report.buttons & HID_MOUSEBUTTON_MIDDLE ? 'M' : '-', + mouse_report.buttons & HID_MOUSEBUTTON_RIGHT ? 'R' : '-', + mouse_report.x, mouse_report.y); + } + + OSAL_TASK_LOOP_END +} diff --git a/demos/host/mouse_app.h b/demos/host/mouse_app.h index 373aa1f07..6d494c006 100644 --- a/demos/host/mouse_app.h +++ b/demos/host/mouse_app.h @@ -51,13 +51,16 @@ #ifndef _TUSB_MOUSE_APP_H_ #define _TUSB_MOUSE_APP_H_ -#ifdef __cplusplus - extern "C" { -#endif +#include +#include #include "boards/board.h" #include "tusb.h" +#ifdef __cplusplus + extern "C" { +#endif + #ifdef __cplusplus } #endif diff --git a/tinyusb/host/ehci/ehci.c b/tinyusb/host/ehci/ehci.c index cc2c5738e..df3faebcb 100644 --- a/tinyusb/host/ehci/ehci.c +++ b/tinyusb/host/ehci/ehci.c @@ -471,8 +471,13 @@ void async_list_process_isr(ehci_qhd_t * const async_head) // free all TDs from the head td to the first active TD while(p_qhd->p_qtd_list_head != NULL && !p_qhd->p_qtd_list_head->active) { - // TODO check halted TD - if (p_qhd->p_qtd_list_head->int_on_complete) // end of request + // TD need to be freed and removed from qhd, before invoking callback + bool is_ioc = (p_qhd->p_qtd_list_head->int_on_complete != 0); + + p_qhd->p_qtd_list_head->used = 0; // free QTD + qtd_remove_1st_from_qhd(p_qhd); + + if (is_ioc) // end of request { pipe_handle_t pipe_hdl = { .dev_addr = p_qhd->device_address }; if (p_qhd->endpoint_number) // if not Control, can only be Bulk @@ -483,8 +488,6 @@ void async_list_process_isr(ehci_qhd_t * const async_head) usbh_isr( pipe_hdl, p_qhd->class_code, TUSB_EVENT_XFER_COMPLETE); // call USBH callback } - p_qhd->p_qtd_list_head->used = 0; // free QTD - qtd_remove_1st_from_qhd(p_qhd); } } p_qhd = (ehci_qhd_t*) align32(p_qhd->next.address); @@ -511,20 +514,24 @@ void period_list_process_isr(ehci_qhd_t const * const period_head) // free all TDs from the head td to the first active TD while(p_qhd_int->p_qtd_list_head != NULL && !p_qhd_int->p_qtd_list_head->active) { - // TODO check halted TD - if (p_qhd_int->p_qtd_list_head->int_on_complete) // end of request - { - pipe_handle_t pipe_hdl = { .dev_addr = p_qhd_int->device_address }; - if (p_qhd_int->endpoint_number) // if not Control, can only be Bulk - { - pipe_hdl.xfer_type = TUSB_XFER_INTERRUPT; - pipe_hdl.index = qhd_get_index(p_qhd_int); - } - usbh_isr( pipe_hdl, p_qhd_int->class_code, TUSB_EVENT_XFER_COMPLETE); // call USBH callback - } + // TD need to be freed and removed from qhd, before invoking callback + bool is_ioc = (p_qhd_int->p_qtd_list_head->int_on_complete != 0); p_qhd_int->p_qtd_list_head->used = 0; // free QTD qtd_remove_1st_from_qhd(p_qhd_int); + + if (is_ioc) // end of request + { + usbh_isr( (pipe_handle_t) + { + .dev_addr = p_qhd_int->device_address, + .xfer_type = TUSB_XFER_INTERRUPT, + .index = qhd_get_index(p_qhd_int) + }, + p_qhd_int->class_code, + TUSB_EVENT_XFER_COMPLETE); // call USBH callback + } + } } next_item = p_qhd_int->next; diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index ccf1af249..e26ec5b46 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -248,7 +248,6 @@ void usbh_device_unplugged_isr(uint8_t hostid) //--------------------------------------------------------------------+ // ENUMERATION TASK //--------------------------------------------------------------------+ -//TODO reduce Cyclomatic Complexity OSAL_TASK_DECLARE(usbh_enumeration_task) { tusb_error_t error; @@ -398,6 +397,8 @@ OSAL_TASK_DECLARE(usbh_enumeration_task) ) ); + usbh_devices[new_addr].state = TUSB_DEVICE_STATE_CONFIGURED; + //------------- parse configuration & install drivers -------------// p_desc = enum_data_buffer + sizeof(tusb_descriptor_configuration_t); @@ -440,7 +441,6 @@ OSAL_TASK_DECLARE(usbh_enumeration_task) } } - usbh_devices[new_addr].state = TUSB_DEVICE_STATE_CONFIGURED; tusbh_device_mount_succeed_cb(new_addr); OSAL_TASK_LOOP_END