Explorar o código

fix bug with osal_queue for freeRTOS
implement process mouser report to clarify mouse app task

hathach %!s(int64=13) %!d(string=hai) anos
pai
achega
38172d3d3c
Modificáronse 2 ficheiros con 40 adicións e 21 borrados
  1. 35 18
      demos/host/src/mouse_app.c
  2. 5 3
      tinyusb/osal/osal_freeRTOS.h

+ 35 - 18
demos/host/src/mouse_app.c

@@ -45,18 +45,19 @@
 //--------------------------------------------------------------------+
 // MACRO CONSTANT TYPEDEF
 //--------------------------------------------------------------------+
-#define QUEUE_MOUSE_REPORT_DEPTH   5
+#define QUEUE_MOUSE_REPORT_DEPTH   4
 
 //--------------------------------------------------------------------+
 // INTERNAL OBJECT & FUNCTION DECLARATION
 //--------------------------------------------------------------------+
 OSAL_TASK_DEF(mouse_task_def, "mouse app", mouse_app_task, 128, MOUSE_APP_TASK_PRIO);
+OSAL_QUEUE_DEF(queue_mouse_def, QUEUE_MOUSE_REPORT_DEPTH, tusb_mouse_report_t);
 
-OSAL_QUEUE_DEF(queue_mouse_report, QUEUE_MOUSE_REPORT_DEPTH, tusb_mouse_report_t);
-static osal_queue_handle_t q_mouse_report_hdl;
-
+static osal_queue_handle_t queue_mouse_hdl;
 static tusb_mouse_report_t usb_mouse_report TUSB_CFG_ATTR_USBRAM;
 
+static inline void process_mouse_report(tusb_mouse_report_t const * report);
+
 //--------------------------------------------------------------------+
 // tinyusb callback (ISR context)
 //--------------------------------------------------------------------+
@@ -65,7 +66,7 @@ void tusbh_hid_mouse_isr(uint8_t dev_addr, uint8_t instance_num, tusb_event_t ev
   switch(event)
   {
     case TUSB_EVENT_INTERFACE_OPEN: // application set-up
-      osal_queue_flush(q_mouse_report_hdl);
+      osal_queue_flush(queue_mouse_hdl);
       tusbh_hid_mouse_get_report(dev_addr, instance_num, (uint8_t*) &usb_mouse_report); // first report
     break;
 
@@ -74,7 +75,7 @@ void tusbh_hid_mouse_isr(uint8_t dev_addr, uint8_t instance_num, tusb_event_t ev
     break;
 
     case TUSB_EVENT_XFER_COMPLETE:
-      osal_queue_send(q_mouse_report_hdl, &usb_mouse_report);
+      osal_queue_send(queue_mouse_hdl, &usb_mouse_report);
       tusbh_hid_mouse_get_report(dev_addr, instance_num, (uint8_t*) &usb_mouse_report);
     break;
 
@@ -98,9 +99,8 @@ void mouse_app_init(void)
 
   ASSERT( TUSB_ERROR_NONE == osal_task_create(&mouse_task_def), (void) 0 );
 
-  q_mouse_report_hdl = osal_queue_create(&queue_mouse_report);
-  ASSERT_PTR( q_mouse_report_hdl, (void) 0 );
-
+  queue_mouse_hdl = osal_queue_create(&queue_mouse_def);
+  ASSERT_PTR( queue_mouse_hdl, (void) 0 );
 }
 
 //------------- main task -------------//
@@ -111,20 +111,37 @@ OSAL_TASK_FUNCTION( mouse_app_task ) (void* p_task_para)
 
   OSAL_TASK_LOOP_BEGIN
 
-  osal_queue_receive(q_mouse_report_hdl, &mouse_report, OSAL_TIMEOUT_WAIT_FOREVER, &error);
+  osal_queue_receive(queue_mouse_hdl, &mouse_report, OSAL_TIMEOUT_WAIT_FOREVER, &error);
+  process_mouse_report(&mouse_report);
+
+  OSAL_TASK_LOOP_END
+}
+
+//--------------------------------------------------------------------+
+// HELPER
+//--------------------------------------------------------------------+
+static inline void process_mouse_report(tusb_mouse_report_t const * report)
+{
+  static tusb_mouse_report_t prev_report = { 0 };
 
-  if ( mouse_report.buttons || mouse_report.x || mouse_report.y)
+  //------------- button state  -------------//
+  uint8_t button_changed_mask = report->buttons ^ prev_report.buttons;
+  if ( button_changed_mask & report->buttons)
   {
-      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);
+     // example only display button pressed, ignore hold & dragging etc
+    printf(" %c%c%c ",
+       report->buttons & HID_MOUSEBUTTON_LEFT   ? 'L' : '-',
+       report->buttons & HID_MOUSEBUTTON_MIDDLE ? 'M' : '-',
+       report->buttons & HID_MOUSEBUTTON_RIGHT  ? 'R' : '-');
   }
 
-  memclr_(&mouse_report, sizeof(tusb_mouse_report_t) );
+  //------------- coordinator -------------//
+  if ( report->x != 0 || report->y != 0 )
+  {
+    printf(" (%d, %d) ", report->x, report->y);
+  }
 
-  OSAL_TASK_LOOP_END
 }
 
+
 #endif

+ 5 - 3
tinyusb/osal/osal_freeRTOS.h

@@ -167,18 +167,20 @@ static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl)
 // QUEUE API
 //--------------------------------------------------------------------+
 typedef struct{
-  uint8_t const depth        ; ///< buffer size
+  uint8_t const depth;     ///< buffer size
+  uint8_t const item_size; ///< size of each item
 } osal_queue_t;
 
 typedef xQueueHandle osal_queue_handle_t;
 
 #define OSAL_QUEUE_DEF(name, queue_depth, type)\
   osal_queue_t name = {\
-      .depth   = queue_depth\
+      .depth     = queue_depth,\
+      .item_size = sizeof(type)\
   }
 
 #define osal_queue_create(p_queue) \
-  xQueueCreate((p_queue)->depth, sizeof(uint32_t))
+  xQueueCreate((p_queue)->depth, (p_queue)->item_size)
 
 static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE;
 static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error)