diff options
| author | hathach <[email protected]> | 2019-06-10 18:50:59 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-06-10 18:50:59 +0700 |
| commit | 393492823ce037a2e46d367d61fad1235859af2e (patch) | |
| tree | 7bc829e94b0a542e6990566e71ec293a600d3553 /src/device | |
| parent | 69e98215ea94bf5d6a8adc042732fe2a67df9764 (diff) | |
| parent | 7156bfb54d5e9e13c8574b2384a47bd5554798a9 (diff) | |
Merge pull request #80 from hathach/develop
added usbd_edpt_xfer/usbd_edpt_busy to replace dcd_edpt_transfer/dcd_edpt_busy()
Diffstat (limited to 'src/device')
| -rw-r--r-- | src/device/dcd.h | 25 | ||||
| -rw-r--r-- | src/device/usbd.c | 46 | ||||
| -rw-r--r-- | src/device/usbd.h | 10 | ||||
| -rw-r--r-- | src/device/usbd_control.c | 1 | ||||
| -rw-r--r-- | src/device/usbd_pvt.h | 11 |
5 files changed, 67 insertions, 26 deletions
diff --git a/src/device/dcd.h b/src/device/dcd.h index 35164ac62..0ed4a77d9 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -52,7 +52,7 @@ typedef enum USBD_EVENT_FUNC_CALL
} dcd_eventid_t;
-typedef struct ATTR_ALIGNED(4)
+typedef struct TU_ATTR_ALIGNED(4)
{
uint8_t rhport;
uint8_t event_id;
@@ -100,25 +100,28 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num); // Wake up host
void dcd_remote_wakeup(uint8_t rhport);
-/*------------------------------------------------------------------*/
-/* Endpoint API
- * - open : Configure endpoint's registers
- * - xfer : Submit a transfer. When complete dcd_event_xfer_complete
- * must be called to notify the stack
- * - busy : Check if endpoint transferring is complete (TODO remove)
- * - stall : stall endpoint
- * - clear_stall : clear stall, data toggle is also reset to DATA0
- *------------------------------------------------------------------*/
+//--------------------------------------------------------------------+
+// Endpoint API
+//--------------------------------------------------------------------+
+
+// Configure endpoint's registers according to descriptor
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
+
+// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
+
+// Check if endpoint transferring is complete (TODO remove)
bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr);
+// Stall endpoint
void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
+
+// clear stall, data toggle is also reset to DATA0
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
/*------------------------------------------------------------------*/
/* Event Function
- * Called by DCD to notify USBD
+ * Called by DCD to notify device stack
*------------------------------------------------------------------*/
void dcd_event_handler(dcd_event_t const * event, bool in_isr);
diff --git a/src/device/usbd.c b/src/device/usbd.c index 56d895388..a68953151 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -31,6 +31,7 @@ #include "tusb.h"
#include "usbd.h"
#include "device/usbd_pvt.h"
+#include "dcd.h"
#ifndef CFG_TUD_TASK_QUEUE_SZ
#define CFG_TUD_TASK_QUEUE_SZ 16
@@ -40,7 +41,7 @@ // Device Data
//--------------------------------------------------------------------+
typedef struct {
- struct ATTR_PACKED
+ struct TU_ATTR_PACKED
{
volatile uint8_t connected : 1;
volatile uint8_t configured : 1;
@@ -51,8 +52,8 @@ typedef struct { uint8_t self_powered : 1; // configuration descriptor's attribute
};
-// uint8_t ep_busy_mask[2]; // bit mask for busy endpoint
- uint8_t ep_stall_mask[2]; // bit mask for stalled endpoint
+ uint8_t ep_busy_map[2]; // bit mask for busy endpoint
+ uint8_t ep_stall_map[2]; // bit map for stalled endpoint
uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid )
@@ -287,6 +288,10 @@ void tud_task (void) {
// Invoke the class callback associated with the endpoint address
uint8_t const ep_addr = event.xfer_complete.ep_addr;
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ _usbd_dev.ep_busy_map[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_busy_map[dir], epnum);
if ( 0 == tu_edpt_number(ep_addr) )
{
@@ -621,7 +626,7 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) case DCD_EVENT_SUSPEND:
// NOTE: When plugging/unplugging device, the D+/D- state are unstable and can accidentally meet the
- // SUSPEND condition ( Idle for 3ms ). Some MCUs such as samd don't distinguish suspend vs disconnect as well.
+ // SUSPEND condition ( Idle for 3ms ). Some MCUs such as SAMD doesn't distinguish suspend vs disconnect as well.
// We will skip handling SUSPEND/RESUME event if not currently connected
if ( _usbd_dev.connected )
{
@@ -643,7 +648,8 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) break;
case DCD_EVENT_XFER_COMPLETE:
- // skip zero-length control status complete event, should dcd notifies us.
+ // skip zero-length control status complete event, should DCD notify us.
+ // TODO could cause issue with actual zero length data used by class such as DFU
if ( (0 == tu_edpt_number(event->xfer_complete.ep_addr)) && (event->xfer_complete.len == 0) ) break;
osal_queue_send(_usbd_q, event, in_isr);
@@ -733,13 +739,37 @@ void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr) //--------------------------------------------------------------------+
// USBD Endpoint API
//--------------------------------------------------------------------+
+
+bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
+{
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ TU_VERIFY( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) );
+
+ _usbd_dev.ep_busy_map[dir] = (uint8_t) tu_bit_set(_usbd_dev.ep_busy_map[dir], epnum);
+
+ return true;
+}
+
+bool usbd_edpt_busy(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_busy_map[dir], epnum);
+}
+
+
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] = (uint8_t) tu_bit_set(_usbd_dev.ep_stall_mask[dir], epnum);
+ _usbd_dev.ep_stall_map[dir] = (uint8_t) tu_bit_set(_usbd_dev.ep_stall_map[dir], epnum);
}
void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
@@ -748,7 +778,7 @@ void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) uint8_t const dir = tu_edpt_dir(ep_addr);
dcd_edpt_clear_stall(rhport, ep_addr);
- _usbd_dev.ep_stall_mask[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_stall_mask[dir], epnum);
+ _usbd_dev.ep_stall_map[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_stall_map[dir], epnum);
}
bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr)
@@ -758,7 +788,7 @@ bool usbd_edpt_stalled(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);
- return tu_bit_test(_usbd_dev.ep_stall_mask[dir], epnum);
+ return tu_bit_test(_usbd_dev.ep_stall_map[dir], epnum);
}
#endif
diff --git a/src/device/usbd.h b/src/device/usbd.h index 46143a215..47e0e531c 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -35,7 +35,7 @@ #endif
#include "common/tusb_common.h"
-#include "device/dcd.h"
+#include "dcd.h"
//--------------------------------------------------------------------+
// Application API
@@ -76,17 +76,17 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index); uint16_t const* tud_descriptor_string_cb(uint8_t index);
// Invoked when device is mounted (configured)
-ATTR_WEAK void tud_mount_cb(void);
+TU_ATTR_WEAK void tud_mount_cb(void);
// Invoked when device is unmounted
-ATTR_WEAK void tud_umount_cb(void);
+TU_ATTR_WEAK void tud_umount_cb(void);
// Invoked when usb bus is suspended
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
-ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
+TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
// Invoked when usb bus is resumed
-ATTR_WEAK void tud_resume_cb(void);
+TU_ATTR_WEAK void tud_resume_cb(void);
//--------------------------------------------------------------------+
// Interface Descriptor Template
diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index 1b5ee9f91..49293b832 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -30,6 +30,7 @@ #include "tusb.h"
#include "device/usbd_pvt.h"
+#include "dcd.h"
enum
{
diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index f20532627..706857d45 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -33,10 +33,11 @@ extern "C" { #endif +bool usbd_init (void); + //--------------------------------------------------------------------+ -// INTERNAL API for stack management +// USBD Endpoint API //--------------------------------------------------------------------+ -bool usbd_init (void); // Carry out Data and Status stage of control transfer // - If len = 0, it is equivalent to sending status only @@ -46,6 +47,12 @@ 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); +// Submit a usb transfer +bool usbd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); + +// Check if endpoint transferring is complete +bool usbd_edpt_busy (uint8_t rhport, uint8_t ep_addr); + 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); |
