diff options
| author | hathach <[email protected]> | 2019-03-27 08:52:25 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-03-27 08:52:25 -0700 |
| commit | 86c24b310509a368cc6a9dfbec43ce531e71d598 (patch) | |
| tree | 81fec0b3724083a3515ef09e0b7d601ce01ec621 /src/device | |
| parent | 547cc045fa1d8862e7e00c1d7b4a0026531d0098 (diff) | |
| parent | 38f7d281d44c69593f1de3885a292ee7c5c39355 (diff) | |
Merge pull request #49 from hathach/develop
clean up porting API
Diffstat (limited to 'src/device')
| -rw-r--r-- | src/device/dcd.h | 35 | ||||
| -rw-r--r-- | src/device/usbd.c | 50 | ||||
| -rw-r--r-- | src/device/usbd.h | 5 | ||||
| -rw-r--r-- | src/device/usbd_control.c | 14 | ||||
| -rw-r--r-- | src/device/usbd_pvt.h | 5 |
5 files changed, 64 insertions, 45 deletions
diff --git a/src/device/dcd.h b/src/device/dcd.h index 95a187298..186c4af0c 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -94,24 +94,6 @@ void dcd_set_address(uint8_t rhport, uint8_t dev_addr); // Receive Set Config request
void dcd_set_config (uint8_t rhport, uint8_t config_num);
-// Get current frame number
-uint32_t dcd_get_frame_number(uint8_t rhport);
-
-/*------------------------------------------------------------------*/
-/* Event Function
- * Called by DCD to notify USBD
- *------------------------------------------------------------------*/
-void dcd_event_handler(dcd_event_t const * event, bool in_isr);
-
-// helper to send bus signal event
-void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr);
-
-// helper to send setup received
-void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr);
-
-// helper to send transfer complete event
-void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
-
/*------------------------------------------------------------------*/
/* Endpoint API
* - open : Configure endpoint's registers
@@ -120,7 +102,6 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ * - busy : Check if endpoint transferring is complete (TODO remove)
* - stall : stall endpoint
* - clear_stall : clear stall
- * - stalled : check if stalled ( TODO remove )
*------------------------------------------------------------------*/
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
@@ -128,7 +109,21 @@ bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr); void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
-bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr);
+
+/*------------------------------------------------------------------*/
+/* Event Function
+ * Called by DCD to notify USBD
+ *------------------------------------------------------------------*/
+void dcd_event_handler(dcd_event_t const * event, bool in_isr);
+
+// helper to send bus signal event
+void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr);
+
+// helper to send setup received
+void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr);
+
+// helper to send transfer complete event
+void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
#ifdef __cplusplus
}
diff --git a/src/device/usbd.c b/src/device/usbd.c index 877837fae..604f35bc5 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -28,8 +28,6 @@ #if TUSB_OPT_DEVICE_ENABLED
-#define _TINY_USB_SOURCE_FILE_
-
#include "tusb.h"
#include "usbd.h"
#include "device/usbd_pvt.h"
@@ -44,9 +42,11 @@ typedef struct {
uint8_t config_num;
- uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
- uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid )
+ uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
+ uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid )
+ uint8_t ep_busy_mask[2]; // bit mask for busy endpoint
+ uint8_t ep_stall_mask[2]; // bit mask for stalled endpoint
}usbd_device_t;
static usbd_device_t _usbd_dev = { 0 };
@@ -102,7 +102,6 @@ static usbd_class_driver_t const usbd_class_drivers[] = },
#endif
-
#if CFG_TUD_HID
{
.class_code = TUSB_CLASS_HID,
@@ -241,10 +240,12 @@ void tud_task (void) switch ( event.event_id )
{
case DCD_EVENT_SETUP_RECEIVED:
- // Process control request, if failed control endpoint is stalled
+ // Process control request
if ( !process_control_request(event.rhport, &event.setup_received) )
{
- usbd_control_stall(event.rhport);
+ // Failed -> stall both control endpoint IN and OUT
+ dcd_edpt_stall(event.rhport, 0);
+ dcd_edpt_stall(event.rhport, 0 | TUSB_DIR_IN_MASK);
}
break;
@@ -379,7 +380,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const {
case TUSB_REQ_GET_STATUS:
{
- uint16_t status = dcd_edpt_stalled(rhport, tu_u16_low(p_request->wIndex)) ? 0x0001 : 0x0000;
+ uint16_t status = usbd_edpt_stalled(rhport, tu_u16_low(p_request->wIndex)) ? 0x0001 : 0x0000;
usbd_control_xfer(rhport, p_request, &status, 2);
}
break;
@@ -392,7 +393,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const case TUSB_REQ_SET_FEATURE:
// only endpoint feature is halted/stalled
- dcd_edpt_stall(rhport, tu_u16_low(p_request->wIndex));
+ usbd_edpt_stall(rhport, tu_u16_low(p_request->wIndex));
usbd_control_status(rhport, p_request);
break;
@@ -650,4 +651,35 @@ void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr) dcd_event_handler(&event, in_isr);
}
+//--------------------------------------------------------------------+
+// USBD Endpoint API
+//--------------------------------------------------------------------+
+void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
+{
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ dcd_edpt_stall(rhport, ep_addr);
+ _usbd_dev.ep_stall_mask[dir] = tu_bit_set(_usbd_dev.ep_stall_mask[dir], epnum);
+}
+
+void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
+{
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ dcd_edpt_clear_stall(rhport, ep_addr);
+ _usbd_dev.ep_stall_mask[dir] = tu_bit_clear(_usbd_dev.ep_stall_mask[dir], epnum);
+}
+
+bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr)
+{
+ (void) rhport;
+
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ return tu_bit_test(_usbd_dev.ep_stall_mask[dir], epnum);
+}
+
#endif
diff --git a/src/device/usbd.h b/src/device/usbd.h index 11ef63887..614b4f311 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -38,7 +38,6 @@ // INCLUDE
//--------------------------------------------------------------------+
#include <common/tusb_common.h>
-#include "osal/osal.h"
#include "device/dcd.h"
//--------------------------------------------------------------------+
@@ -75,10 +74,10 @@ void tud_task (void); // APPLICATION CALLBACK (WEAK is optional)
//--------------------------------------------------------------------+
-/** Callback invoked when device is mounted (configured) */
+// Callback invoked when device is mounted (configured)
ATTR_WEAK void tud_mount_cb(void);
-/** Callback invoked when device is unmounted (bus reset/unplugged) */
+// Callback invoked when device is unmounted (bus reset/unplugged)
ATTR_WEAK void tud_umount_cb(void);
//void tud_device_suspended_cb(void);
diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index 094d2fb44..24234f4d2 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -28,8 +28,6 @@ #if TUSB_OPT_DEVICE_ENABLED
-#define _TINY_USB_SOURCE_FILE_
-
#include "tusb.h"
#include "device/usbd_pvt.h"
@@ -60,13 +58,6 @@ void usbd_control_reset (uint8_t rhport) tu_varclr(&_control_state);
}
-void usbd_control_stall(uint8_t rhport)
-{
- // when stalling control endpoint both IN and OUt will be stalled
- dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
- dcd_edpt_stall(rhport, EDPT_CTRL_IN);
-}
-
bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request)
{
// status direction is reversed to one in the setup packet
@@ -147,8 +138,9 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result TU_ASSERT( usbd_control_status(rhport, &_control_state.request) );
}else
{
- // stall due to callback
- usbd_control_stall(rhport);
+ // Stall both IN and OUT control endpoint
+ dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
+ dcd_edpt_stall(rhport, EDPT_CTRL_IN);
}
}
else
diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index d2562d298..dab67581f 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -49,8 +49,9 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v // Send STATUS (zero length) packet bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request); -// Stall control endpoint (both IN and OUT) until new setup packet arrived -void usbd_control_stall(uint8_t rhport); +void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr); +void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr); +bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr); /*------------------------------------------------------------------*/ /* Helper |
