diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/tusb_common.h | 44 | ||||
| -rw-r--r-- | src/common/tusb_compiler.h | 7 | ||||
| -rw-r--r-- | src/common/tusb_verify.h | 51 | ||||
| -rw-r--r-- | src/device/dcd.h | 7 | ||||
| -rw-r--r-- | src/device/usbd.c | 124 | ||||
| -rw-r--r-- | src/osal/osal.h | 3 | ||||
| -rw-r--r-- | src/tusb.c | 78 |
7 files changed, 244 insertions, 70 deletions
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 57c6e2fcf..59e1fe7fd 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -35,7 +35,7 @@ extern "C" {
#endif
- //--------------------------------------------------------------------+
+//--------------------------------------------------------------------+
// Macros Helper
//--------------------------------------------------------------------+
#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
@@ -58,7 +58,7 @@ #define TU_BIT(n) (1U << (n))
//--------------------------------------------------------------------+
-// INCLUDES
+// Includes
//--------------------------------------------------------------------+
// Standard Headers
@@ -77,7 +77,7 @@ #include "tusb_types.h"
//--------------------------------------------------------------------+
-// INLINE FUNCTION
+// Inline Functions
//--------------------------------------------------------------------+
#define tu_memclr(buffer, size) memset((buffer), 0, (size))
#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var)))
@@ -203,6 +203,44 @@ static inline bool tu_bit_test (uint32_t value, uint8_t n) { return (value & + TU_BIN8(dlsb))
#endif
+//--------------------------------------------------------------------+
+// Debug Function
+//--------------------------------------------------------------------+
+
+// CFG_TUSB_DEBUG for debugging
+// 0 : no debug
+// 1 : print when there is error
+// 2 : print out log
+#if CFG_TUSB_DEBUG
+
+void tu_print_mem(void const *buf, uint8_t size, uint16_t count);
+
+#ifndef tu_printf
+ #define tu_printf printf
+#endif
+
+// Log with debug level 1
+#define TU_LOG1 tu_printf
+#define TU_LOG1_MEM tu_print_mem
+
+// Log with debug level 2
+#if CFG_TUSB_DEBUG > 1
+ #define TU_LOG2 TU_LOG1
+ #define TU_LOG2_MEM TU_LOG1_MEM
+#endif
+
+#endif // CFG_TUSB_DEBUG
+
+#ifndef TU_LOG1
+ #define TU_LOG1(...)
+ #define TU_LOG1_MEM(...)
+#endif
+
+#ifndef TU_LOG2
+ #define TU_LOG2(...)
+ #define TU_LOG2_MEM(...)
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h index 58732b871..0bac3eae7 100644 --- a/src/common/tusb_compiler.h +++ b/src/common/tusb_compiler.h @@ -67,6 +67,7 @@ #define TU_ATTR_WEAK __attribute__ ((weak))
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
+ #define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
// Endian conversion use well-known host to network (big endian) naming
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@@ -75,8 +76,8 @@ #define TU_BYTE_ORDER TU_BIG_ENDIAN
#endif
-#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
-#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
+ #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
+ #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
#elif defined(__TI_COMPILER_VERSION__)
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
@@ -86,6 +87,7 @@ #define TU_ATTR_WEAK __attribute__ ((weak))
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
+ #define TU_ATTR_USED __attribute__ ((used))
// __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
#if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
@@ -123,7 +125,6 @@ #define tu_htonl(u32) (u32)
#define tu_ntohl(u32) (u32)
-
#define tu_htole16(u16) (tu_bswap16(u16))
#define tu_le16toh(u16) (tu_bswap16(u16))
diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h index fae0c88ae..e59481352 100644 --- a/src/common/tusb_verify.h +++ b/src/common/tusb_verify.h @@ -107,20 +107,6 @@ if ( !(_cond) ) { _handler; return _ret; } \ } while(0) -/*------------- Generator for TU_VERIFY_ERR and TU_VERIFY_ERR_HDLR -------------*/ -#define TU_VERIFY_ERR_DEF2(_error, _handler) do \ -{ \ - uint32_t _err = (uint32_t)(_error); \ - if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; } \ -} while(0) - -#define TU_VERIFY_ERR_DEF3(_error, _handler, _ret) do \ -{ \ - uint32_t _err = (uint32_t)(_error); \ - if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; } \ -} while(0) - - /*------------------------------------------------------------------*/ /* TU_VERIFY * - TU_VERIFY_1ARGS : return false if failed @@ -142,28 +128,6 @@ #define TU_VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS,UNUSED)(__VA_ARGS__) - -/*------------------------------------------------------------------*/ -/* TU_VERIFY STATUS - * - TU_VERIFY_ERR_1ARGS : return status of condition if failed - * - TU_VERIFY_ERR_2ARGS : return provided status code if failed - *------------------------------------------------------------------*/ -#define TU_VERIFY_ERR_1ARGS(_error) TU_VERIFY_ERR_DEF2(_error, ) -#define TU_VERIFY_ERR_2ARGS(_error, _ret) TU_VERIFY_ERR_DEF3(_error, ,_ret) - -#define TU_VERIFY_ERR(...) GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_ERR_2ARGS, TU_VERIFY_ERR_1ARGS,UNUSED)(__VA_ARGS__) - -/*------------------------------------------------------------------*/ -/* TU_VERIFY STATUS WITH HANDLER - * - TU_VERIFY_ERR_HDLR_2ARGS : execute handler, return status if failed - * - TU_VERIFY_ERR_HDLR_3ARGS : execute handler, return provided error if failed - *------------------------------------------------------------------*/ -#define TU_VERIFY_ERR_HDLR_2ARGS(_error, _handler) TU_VERIFY_ERR_DEF2(_error, _handler) -#define TU_VERIFY_ERR_HDLR_3ARGS(_error, _handler, _ret) TU_VERIFY_ERR_DEF3(_error, _handler, _ret) - -#define TU_VERIFY_ERR_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_ERR_HDLR_3ARGS, TU_VERIFY_ERR_HDLR_2ARGS,UNUSED)(__VA_ARGS__) - - /*------------------------------------------------------------------*/ /* ASSERT * basically TU_VERIFY with TU_BREAKPOINT() as handler @@ -175,6 +139,21 @@ #define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS,UNUSED)(__VA_ARGS__) +// TODO remove TU_ASSERT_ERR() later + +/*------------- Generator for TU_VERIFY_ERR and TU_VERIFY_ERR_HDLR -------------*/ +#define TU_VERIFY_ERR_DEF2(_error, _handler) do \ +{ \ + uint32_t _err = (uint32_t)(_error); \ + if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; } \ +} while(0) + +#define TU_VERIFY_ERR_DEF3(_error, _handler, _ret) do \ +{ \ + uint32_t _err = (uint32_t)(_error); \ + if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; } \ +} while(0) + /*------------------------------------------------------------------*/ /* ASSERT Error * basically TU_VERIFY Error with TU_BREAKPOINT() as handler diff --git a/src/device/dcd.h b/src/device/dcd.h index bef062fb1..c417ee4be 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -39,7 +39,8 @@ typedef enum
{
- DCD_EVENT_BUS_RESET = 1,
+ DCD_EVENT_INVALID = 0,
+ DCD_EVENT_BUS_RESET,
DCD_EVENT_UNPLUGGED,
DCD_EVENT_SOF,
DCD_EVENT_SUSPEND, // TODO LPM Sleep L1 support
@@ -49,7 +50,9 @@ typedef enum DCD_EVENT_XFER_COMPLETE,
// Not an DCD event, just a convenient way to defer ISR function
- USBD_EVENT_FUNC_CALL
+ USBD_EVENT_FUNC_CALL,
+
+ DCD_EVENT_COUNT
} dcd_eventid_t;
typedef struct TU_ATTR_ALIGNED(4)
diff --git a/src/device/usbd.c b/src/device/usbd.c index 14e56a9b7..f2becab95 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -192,6 +192,66 @@ void usbd_control_reset (uint8_t rhport); bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) );
+
+//--------------------------------------------------------------------+
+// Debugging
+//--------------------------------------------------------------------+
+#if CFG_TUSB_DEBUG > 1
+static char const* const _usbd_event_str[DCD_EVENT_COUNT] =
+{
+ "INVALID" ,
+ "BUS_RESET" ,
+ "UNPLUGGED" ,
+ "SOF" ,
+ "SUSPEND" ,
+ "RESUME" ,
+ "SETUP_RECEIVED" ,
+ "XFER_COMPLETE" ,
+ "FUNC_CALL"
+};
+
+// must be same driver order as usbd_class_drivers[]
+static char const* const _usbd_driver_str[USBD_CLASS_DRIVER_COUNT] =
+{
+ #if CFG_TUD_CDC
+ "CDC",
+ #endif
+ #if CFG_TUD_MSC
+ "MSC",
+ #endif
+ #if CFG_TUD_HID
+ "HID",
+ #endif
+ #if CFG_TUD_MIDI
+ "MIDI",
+ #endif
+ #if CFG_TUD_VENDOR
+ "Vendor",
+ #endif
+ #if CFG_TUD_USBTMC
+ "USBTMC"
+ #endif
+};
+
+static char const* const _tusb_std_request_str[] =
+{
+ "Get Status" ,
+ "Clear Feature" ,
+ "Reserved" ,
+ "Set Feature" ,
+ "Reserved" ,
+ "Set Address" ,
+ "Get Descriptor" ,
+ "Set Descriptor" ,
+ "Get Configuration" ,
+ "Set Configuration" ,
+ "Get Interface" ,
+ "Set Interface" ,
+ "Synch Frame"
+};
+
+#endif
+
//--------------------------------------------------------------------+
// Application API
//--------------------------------------------------------------------+
@@ -218,6 +278,8 @@ bool tud_remote_wakeup(void) //--------------------------------------------------------------------+
bool usbd_init (void)
{
+ TU_LOG2("USBD init\r\n");
+
tu_varclr(&_usbd_dev);
// Init device queue & task
@@ -225,7 +287,11 @@ bool usbd_init (void) TU_ASSERT(_usbd_q != NULL);
// Init class drivers
- for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init();
+ for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
+ {
+ TU_LOG2("%s init\r\n", _usbd_driver_str[i]);
+ usbd_class_drivers[i].init();
+ }
// Init device controller driver
dcd_init(TUD_OPT_RHPORT);
@@ -279,6 +345,8 @@ void tud_task (void) if ( !osal_queue_receive(_usbd_q, &event) ) return;
+ TU_LOG2("USBD: event %s\r\n", event.event_id < DCD_EVENT_COUNT ? _usbd_event_str[event.event_id] : "CORRUPTED");
+
switch ( event.event_id )
{
case DCD_EVENT_BUS_RESET:
@@ -293,6 +361,9 @@ void tud_task (void) break;
case DCD_EVENT_SETUP_RECEIVED:
+ TU_LOG2(" ");
+ TU_LOG2_MEM(&event.setup_received, 1, 8);
+
// Mark as connected after receiving 1st setup packet.
// But it is easier to set it every time instead of wasting time to check then set
_usbd_dev.connected = 1;
@@ -307,29 +378,30 @@ void tud_task (void) break;
case DCD_EVENT_XFER_COMPLETE:
- // Only handle xfer callback in ready state
- // if (_usbd_dev.connected && !_usbd_dev.suspended)
- {
- // 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 ep_dir = tu_edpt_dir(ep_addr);
+ {
+ // 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 ep_dir = tu_edpt_dir(ep_addr);
- _usbd_dev.ep_status[epnum][ep_dir].busy = false;
+ TU_LOG2(" Endpoint: 0x%02X, Bytes: %ld\r\n", ep_addr, event.xfer_complete.len);
- if ( 0 == epnum )
- {
- // control transfer DATA stage callback
- usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
- }
- else
- {
- uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
- TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
+ _usbd_dev.ep_status[epnum][ep_dir].busy = false;
- usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
- }
+ if ( 0 == epnum )
+ {
+ // control transfer DATA stage callback
+ usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
+ }
+ else
+ {
+ uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
+ TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
+
+ TU_LOG2(" %s xfer callback\r\n", _usbd_driver_str[drv_id]);
+ usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}
+ }
break;
case DCD_EVENT_SUSPEND:
@@ -382,6 +454,13 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const return tud_vendor_control_request_cb(rhport, p_request);
}
+#if CFG_TUSB_DEBUG > 1
+ if (TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type && p_request->bRequest <= TUSB_REQ_SYNCH_FRAME)
+ {
+ TU_LOG2(" %s\r\n", _tusb_std_request_str[p_request->bRequest]);
+ }
+#endif
+
switch ( p_request->bmRequestType_bit.recipient )
{
//------------- Device Requests e.g in enumeration -------------//
@@ -494,6 +573,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // GET HID REPORT DESCRIPTOR falls into this case
// stall control endpoint if driver return false
usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete);
+ TU_LOG2(" %s control request\r\n", _usbd_driver_str[drvid]);
TU_ASSERT(usbd_class_drivers[drvid].control_request != NULL &&
usbd_class_drivers[drvid].control_request(rhport, p_request));
break;
@@ -503,6 +583,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // forward to class driver: "non-STD request to Interface"
// stall control endpoint if driver return false
usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete);
+ TU_LOG2(" %s control request\r\n", _usbd_driver_str[drvid]);
TU_ASSERT(usbd_class_drivers[drvid].control_request != NULL &&
usbd_class_drivers[drvid].control_request(rhport, p_request));
}
@@ -570,7 +651,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // For std-type requests: non-std request codes are already discarded.
// must not call tud_control_status(), and return value will have no effect
// class driver is invoked last, so that EP already has EP stall cleared (in event of clear feature EP halt)
-
+ TU_LOG2(" %s control request\r\n", _usbd_driver_str[drv_id]);
if ( usbd_class_drivers[drv_id].control_request &&
usbd_class_drivers[drv_id].control_request(rhport, p_request))
{
@@ -627,6 +708,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
uint16_t itf_len=0;
+ TU_LOG2(" %s open\r\n", _usbd_driver_str[drv_id]);
TU_ASSERT( usbd_class_drivers[drv_id].open(rhport, desc_itf, &itf_len) );
TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) );
diff --git a/src/osal/osal.h b/src/osal/osal.h index ea655d4f1..6b1d668a4 100644 --- a/src/osal/osal.h +++ b/src/osal/osal.h @@ -86,9 +86,6 @@ static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, b #define STASK_RETURN(_error) return _error;
#define STASK_INVOKE(_subtask, _status) (_status) = _subtask
-
-// Sub Task Assert
-#define STASK_ASSERT_ERR(_err) TU_VERIFY_ERR(_err)
#define STASK_ASSERT(_cond) TU_VERIFY(_cond, TUSB_ERROR_OSAL_TASK_FAILED)
#endif
diff --git a/src/tusb.c b/src/tusb.c index 0f8eedd57..271b35f1e 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -43,11 +43,11 @@ bool tusb_init(void) if (_initialized) return true;
#if TUSB_OPT_HOST_ENABLED
- TU_VERIFY( usbh_init() ); // init host stack
+ TU_ASSERT( usbh_init() ); // init host stack
#endif
#if TUSB_OPT_DEVICE_ENABLED
- TU_VERIFY ( usbd_init() ); // init device stack
+ TU_ASSERT ( usbd_init() ); // init device stack
#endif
_initialized = true;
@@ -64,7 +64,81 @@ bool tusb_inited(void) /* Debug
*------------------------------------------------------------------*/
#if CFG_TUSB_DEBUG
+#include <ctype.h>
+
char const* const tusb_strerr[TUSB_ERROR_COUNT] = { ERROR_TABLE(ERROR_STRING) };
+
+static void dump_str_line(uint8_t const* buf, uint16_t count)
+{
+ // each line is 16 bytes
+ for(int i=0; i<count; i++)
+ {
+ const char ch = buf[i];
+ tu_printf("%c", isprint(ch) ? ch : '.');
+ }
+}
+
+// size : item size in bytes
+// count : number of item
+// print offet or not (handfy for dumping large memory)
+void tu_print_mem(void const *buf, uint8_t size, uint16_t count)
+{
+ if ( !buf || !count )
+ {
+ tu_printf("NULL\r\n");
+ return;
+ }
+
+ uint8_t const *buf8 = (uint8_t const *) buf;
+
+ char format[] = "%00lX";
+ format[2] += 2*size;
+
+ const uint8_t item_per_line = 16 / size;
+
+ for(uint32_t i=0; i<count; i++)
+ {
+ uint32_t value=0;
+
+ if ( i%item_per_line == 0 )
+ {
+ // Print Ascii
+ if ( i != 0 )
+ {
+ tu_printf(" | ");
+ dump_str_line(buf8-16, 16);
+ tu_printf("\r\n");
+ }
+
+ // print offset or absolute address
+ tu_printf("%03lX: ", 16*i/item_per_line);
+ }
+
+ memcpy(&value, buf8, size);
+ buf8 += size;
+
+ tu_printf(" ");
+ tu_printf(format, value);
+ }
+
+ // fill up last row to 16 for printing ascii
+ const uint16_t remain = count%16;
+ uint8_t nback = (remain ? remain : 16);
+
+ if ( remain )
+ {
+ for(int i=0; i< 16-remain; i++)
+ {
+ tu_printf(" ");
+ for(int j=0; j<2*size; j++) tu_printf(" ");
+ }
+ }
+
+ tu_printf(" | ");
+ dump_str_line(buf8-nback, nback);
+ tu_printf("\r\n");
+}
+
#endif
#endif // host or device enabled
|
