summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/class/hid/hid_device.c29
-rw-r--r--src/class/usbtmc/usbtmc.h316
-rw-r--r--src/class/usbtmc/usbtmc_device.c863
-rw-r--r--src/class/usbtmc/usbtmc_device.h123
-rw-r--r--src/common/tusb_common.h2
-rw-r--r--src/common/tusb_compiler.h71
-rw-r--r--src/common/tusb_types.h3
-rw-r--r--src/common/tusb_verify.h30
-rw-r--r--src/device/usbd.c184
-rw-r--r--src/device/usbd.h31
-rw-r--r--src/device/usbd_control.c2
-rw-r--r--src/portable/nxp/lpc17_40/dcd_lpc17_40.c2
-rw-r--r--src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c290
-rw-r--r--src/portable/st/stm32_fsdev/dcd_stm32_fsdev_pvt_st.h383
-rw-r--r--src/portable/st/synopsys/dcd_synopsys.c51
-rw-r--r--src/tusb.h4
-rw-r--r--src/tusb_option.h14
17 files changed, 2024 insertions, 374 deletions
diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c
index 7270904ca..ff8e7dc9a 100644
--- a/src/class/hid/hid_device.c
+++ b/src/class/hid/hid_device.c
@@ -46,12 +46,13 @@ typedef struct
uint8_t boot_protocol; // Boot mouse or keyboard
bool boot_mode; // default = false (Report)
uint8_t idle_rate; // up to application to handle idle rate
- uint16_t reprot_desc_len;
+ uint16_t report_desc_len;
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_BUFSIZE];
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_BUFSIZE];
-}hidd_interface_t;
+ tusb_hid_descriptor_hid_t const * hid_descriptor;
+} hidd_interface_t;
CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID];
@@ -167,8 +168,8 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t
//------------- HID descriptor -------------//
p_desc = tu_desc_next(p_desc);
- tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
- TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType);
+ p_hid->hid_descriptor = (tusb_hid_descriptor_hid_t const *) p_desc;
+ TU_ASSERT(HID_DESC_TYPE_HID == p_hid->hid_descriptor->bDescriptorType);
//------------- Endpoint Descriptor -------------//
p_desc = tu_desc_next(p_desc);
@@ -178,7 +179,7 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t
p_hid->boot_mode = false; // default mode is REPORT
p_hid->itf_num = desc_itf->bInterfaceNumber;
- memcpy(&p_hid->reprot_desc_len, &desc_hid->wReportLength, 2);
+ memcpy(&p_hid->report_desc_len, &(p_hid->hid_descriptor->wReportLength), 2);
*p_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
@@ -192,6 +193,10 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t
// return false to stall control endpoint (e.g unsupported request)
bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request)
{
+ if (p_request->bmRequestType_bit.recipient != TUSB_REQ_RCPT_INTERFACE)
+ {
+ return false;
+ }
hidd_interface_t* p_hid = get_interface_by_itfnum( (uint8_t) p_request->wIndex );
TU_ASSERT(p_hid);
@@ -202,11 +207,17 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque
uint8_t const desc_index = tu_u16_low (p_request->wValue);
(void) desc_index;
- if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
+ if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID)
+ {
+ TU_VERIFY(p_hid->hid_descriptor != NULL);
+ TU_VERIFY(tud_control_xfer(rhport, p_request, (void*) p_hid->hid_descriptor, p_hid->hid_descriptor->bLength));
+ }
+ else if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
{
uint8_t const * desc_report = tud_hid_descriptor_report_cb();
- tud_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len);
- }else
+ tud_control_xfer(rhport, p_request, (void*) desc_report, p_hid->report_desc_len);
+ }
+ else
{
return false; // stall unsupported request
}
@@ -252,7 +263,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque
case HID_REQ_CONTROL_GET_PROTOCOL:
{
- uint8_t protocol = 1-p_hid->boot_mode; // 0 is Boot, 1 is Report protocol
+ uint8_t protocol = (uint8_t)(1-p_hid->boot_mode); // 0 is Boot, 1 is Report protocol
tud_control_xfer(rhport, p_request, &protocol, 1);
}
break;
diff --git a/src/class/usbtmc/usbtmc.h b/src/class/usbtmc/usbtmc.h
new file mode 100644
index 000000000..7d7005c2e
--- /dev/null
+++ b/src/class/usbtmc/usbtmc.h
@@ -0,0 +1,316 @@
+
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 N Conrad
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#ifndef _TUSB_USBTMC_H__
+#define _TUSB_USBTMC_H__
+
+#include "common/tusb_common.h"
+
+
+/* Implements USBTMC Revision 1.0, April 14, 2003
+
+ String descriptors must have a "LANGID=0x409"/US English string.
+ Characters must be 0x20 (' ') to 0x7E ('~') ASCII,
+ But MUST not contain: "/:?\*
+ Also must not have leading or trailing space (' ')
+ Device descriptor must state USB version 0x0200 or greater
+
+ If USB488DeviceCapabilites.D2 = 1 (SR1), then there must be a INT endpoint.
+*/
+
+#define USBTMC_VERSION 0x0100
+#define USBTMC_488_VERSION 0x0100
+
+typedef enum {
+ USBTMC_MSGID_DEV_DEP_MSG_OUT = 1u,
+ USBTMC_MSGID_DEV_DEP_MSG_IN = 2u,
+ USBTMC_MSGID_VENDOR_SPECIFIC_MSG_OUT = 126u,
+ USBTMC_MSGID_VENDOR_SPECIFIC_IN = 127u,
+ USBTMC_MSGID_USB488_TRIGGER = 128u,
+} usbtmc_msgid_enum;
+
+/// \brief Message header (For BULK OUT and BULK IN); 4 bytes
+typedef struct TU_ATTR_PACKED
+{
+ uint8_t MsgID ; ///< Message type ID (usbtmc_msgid_enum)
+ uint8_t bTag ; ///< Transfer ID 1<=bTag<=255
+ uint8_t bTagInverse ; ///< Complement of the tag
+ uint8_t _reserved ; ///< Must be 0x00
+} usbtmc_msg_header_t;
+
+typedef struct TU_ATTR_PACKED
+{
+ usbtmc_msg_header_t header;
+ uint8_t data[8];
+} usbtmc_msg_generic_t;
+
+/* Uses on the bulk-out endpoint: */
+// Next 8 bytes are message-specific
+typedef struct TU_ATTR_PACKED {
+ usbtmc_msg_header_t header ; ///< Header
+ uint32_t TransferSize ; ///< Transfer size; LSB first
+ struct TU_ATTR_PACKED
+ {
+ unsigned int EOM : 1 ; ///< EOM set on last byte
+ } bmTransferAttributes;
+ uint8_t _reserved[3];
+} usbtmc_msg_request_dev_dep_out;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_msg_request_dev_dep_out) == 12u, "struct wrong length");
+
+// Next 8 bytes are message-specific
+typedef struct TU_ATTR_PACKED
+{
+ usbtmc_msg_header_t header ; ///< Header
+ uint32_t TransferSize ; ///< Transfer size; LSB first
+ struct TU_ATTR_PACKED
+ {
+ unsigned int TermCharEnabled : 1 ; ///< "The Bulk-IN transfer must terminate on the specified TermChar."; CAPABILITIES must list TermChar
+ } bmTransferAttributes;
+ uint8_t TermChar;
+ uint8_t _reserved[2];
+} usbtmc_msg_request_dev_dep_in;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_msg_request_dev_dep_in) == 12u, "struct wrong length");
+
+/* Bulk-in headers */
+
+typedef struct TU_ATTR_PACKED
+{
+ usbtmc_msg_header_t header;
+ uint32_t TransferSize;
+ struct TU_ATTR_PACKED
+ {
+ uint8_t EOM: 1; ///< Last byte of transfer is the end of the message
+ uint8_t UsingTermChar: 1; ///< Support TermChar && Request.TermCharEnabled && last char in transfer is TermChar
+ } bmTransferAttributes;
+ uint8_t _reserved[3];
+} usbtmc_msg_dev_dep_msg_in_header_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_msg_dev_dep_msg_in_header_t) == 12u, "struct wrong length");
+
+/* Unsupported vendor things.... Are these ever used?*/
+
+typedef struct TU_ATTR_PACKED
+{
+ usbtmc_msg_header_t header ; ///< Header
+ uint32_t TransferSize ; ///< Transfer size; LSB first
+ uint8_t _reserved[4];
+} usbtmc_msg_request_vendor_specific_out;
+
+
+TU_VERIFY_STATIC(sizeof(usbtmc_msg_request_vendor_specific_out) == 12u, "struct wrong length");
+
+typedef struct TU_ATTR_PACKED
+{
+ usbtmc_msg_header_t header ; ///< Header
+ uint32_t TransferSize ; ///< Transfer size; LSB first
+ uint8_t _reserved[4];
+} usbtmc_msg_request_vendor_specific_in;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_msg_request_vendor_specific_in) == 12u, "struct wrong length");
+
+// Control request type should use tusb_control_request_t
+
+/*
+typedef struct TU_ATTR_PACKED {
+ struct {
+ unsigned int Recipient : 5 ; ///< EOM set on last byte
+ unsigned int Type : 2 ; ///< EOM set on last byte
+ unsigned int DirectionToHost : 1 ; ///< 0 is OUT, 1 is IN
+ } bmRequestType;
+ uint8_t bRequest ; ///< If bmRequestType.Type = Class, see usmtmc_request_type_enum
+ uint16_t wValue ;
+ uint16_t wIndex ;
+ uint16_t wLength ; // Number of bytes in data stage
+} usbtmc_class_specific_control_req;
+
+*/
+// bulk-in protocol errors
+enum {
+ USBTMC_BULK_IN_ERR_INCOMPLETE_HEADER = 1u,
+ USBTMC_BULK_IN_ERR_UNSUPPORTED = 2u,
+ USBTMC_BULK_IN_ERR_BAD_PARAMETER = 3u,
+ USBTMC_BULK_IN_ERR_DATA_TOO_SHORT = 4u,
+ USBTMC_BULK_IN_ERR_DATA_TOO_LONG = 5u,
+};
+// bult-in halt errors
+enum {
+ USBTMC_BULK_IN_ERR = 1u, ///< receives a USBTMC command message that expects a response while a
+ /// Bulk-IN transfer is in progress
+};
+
+typedef enum {
+ USBTMC_bREQUEST_INITIATE_ABORT_BULK_OUT = 1u,
+ USBTMC_bREQUEST_CHECK_ABORT_BULK_OUT_STATUS = 2u,
+ USBTMC_bREQUEST_INITIATE_ABORT_BULK_IN = 3u,
+ USBTMC_bREQUEST_CHECK_ABORT_BULK_IN_STATUS = 4u,
+ USBTMC_bREQUEST_INITIATE_CLEAR = 5u,
+ USBTMC_bREQUEST_CHECK_CLEAR_STATUS = 6u,
+ USBTMC_bREQUEST_GET_CAPABILITIES = 7u,
+
+ USBTMC_bREQUEST_INDICATOR_PULSE = 64u, // Optional
+
+ /****** USBTMC 488 *************/
+ USB488_bREQUEST_READ_STATUS_BYTE = 128u,
+ USB488_bREQUEST_REN_CONTROL = 160u,
+ USB488_bREQUEST_GO_TO_LOCAL = 161u,
+ USB488_bREQUEST_LOCAL_LOCKOUT = 162u,
+
+} usmtmc_request_type_enum;
+
+typedef enum {
+ USBTMC_STATUS_SUCCESS = 0x01,
+ USBTMC_STATUS_PENDING = 0x02,
+ USBTMC_STATUS_FAILED = 0x80,
+ USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS = 0x81,
+ USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS = 0x82,
+ USBTMC_STATUS_SPLIT_IN_PROGRESS = 0x83
+} usbtmc_status_enum;
+
+/************************************************************
+ * Control Responses
+ */
+
+typedef struct TU_ATTR_PACKED {
+ uint8_t USBTMC_status; ///< usbtmc_status_enum
+ uint8_t _reserved;
+ uint16_t bcdUSBTMC; ///< USBTMC_VERSION
+
+ struct TU_ATTR_PACKED
+ {
+ unsigned int listenOnly :1;
+ unsigned int talkOnly :1;
+ unsigned int supportsIndicatorPulse :1;
+ } bmIntfcCapabilities;
+ struct TU_ATTR_PACKED
+ {
+ unsigned int canEndBulkInOnTermChar :1;
+ } bmDevCapabilities;
+ uint8_t _reserved2[6];
+ uint8_t _reserved3[12];
+} usbtmc_response_capabilities_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_response_capabilities_t) == 0x18, "struct wrong length");
+
+typedef struct TU_ATTR_PACKED
+{
+ uint8_t USBTMC_status;
+ struct TU_ATTR_PACKED
+ {
+ unsigned int BulkInFifoBytes :1;
+ } bmClear;
+} usbtmc_get_clear_status_rsp_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_get_clear_status_rsp_t) == 2u, "struct wrong length");
+
+// Used for both abort bulk IN and bulk OUT
+typedef struct TU_ATTR_PACKED
+{
+ uint8_t USBTMC_status;
+ uint8_t bTag;
+} usbtmc_initiate_abort_rsp_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_get_clear_status_rsp_t) == 2u, "struct wrong length");
+
+// Used for both check_abort_bulk_in_status and check_abort_bulk_out_status
+typedef struct TU_ATTR_PACKED
+{
+ uint8_t USBTMC_status;
+ struct TU_ATTR_PACKED
+ {
+ unsigned int BulkInFifoBytes : 1; ///< Has queued data or a short packet that is queued
+ } bmAbortBulkIn;
+ uint8_t _reserved[2]; ///< Must be zero
+ uint32_t NBYTES_RXD_TXD;
+} usbtmc_check_abort_bulk_rsp_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_check_abort_bulk_rsp_t) == 8u, "struct wrong length");
+
+typedef struct TU_ATTR_PACKED
+{
+ uint8_t USBTMC_status; ///< usbtmc_status_enum
+ uint8_t _reserved;
+ uint16_t bcdUSBTMC; ///< USBTMC_VERSION
+
+ struct TU_ATTR_PACKED
+ {
+ unsigned int listenOnly :1;
+ unsigned int talkOnly :1;
+ unsigned int supportsIndicatorPulse :1;
+ } bmIntfcCapabilities;
+
+ struct TU_ATTR_PACKED
+ {
+ unsigned int canEndBulkInOnTermChar :1;
+ } bmDevCapabilities;
+
+ uint8_t _reserved2[6];
+ uint16_t bcdUSB488;
+
+ struct TU_ATTR_PACKED
+ {
+ unsigned int is488_2 :1;
+ unsigned int supportsREN_GTL_LLO :1;
+ unsigned int supportsTrigger :1;
+ } bmIntfcCapabilities488;
+
+ struct TU_ATTR_PACKED
+ {
+ unsigned int SCPI :1;
+ unsigned int SR1 :1;
+ unsigned int RL1 :1;
+ unsigned int DT1 :1;
+ } bmDevCapabilities488;
+ uint8_t _reserved3[8];
+} usbtmc_response_capabilities_488_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_response_capabilities_488_t) == 0x18, "struct wrong length");
+
+typedef struct TU_ATTR_PACKED
+{
+ uint8_t USBTMC_status;
+ uint8_t bTag;
+ uint8_t statusByte;
+} usbtmc_read_stb_rsp_488_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_read_stb_rsp_488_t) == 3u, "struct wrong length");
+
+typedef struct TU_ATTR_PACKED
+{
+ struct TU_ATTR_PACKED
+ {
+ unsigned int bTag : 7;
+ unsigned int one : 1;
+ } bNotify1;
+ uint8_t StatusByte;
+} usbtmc_read_stb_interrupt_488_t;
+
+TU_VERIFY_STATIC(sizeof(usbtmc_read_stb_interrupt_488_t) == 2u, "struct wrong length");
+
+#endif
+
diff --git a/src/class/usbtmc/usbtmc_device.c b/src/class/usbtmc/usbtmc_device.c
new file mode 100644
index 000000000..c8e742fc5
--- /dev/null
+++ b/src/class/usbtmc/usbtmc_device.c
@@ -0,0 +1,863 @@
+/*
+ * usbtmc.c
+ *
+ * Created on: Sep 9, 2019
+ * Author: nconrad
+ */
+
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Nathan Conrad
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+/*
+ * This library is not fully reentrant, though it is reentrant from the view
+ * of either the application layer or the USB stack. Due to its locking,
+ * it is not safe to call its functions from interrupts.
+ *
+ * The one exception is that its functions may not be called from the application
+ * until the USB stack is initialized. This should not be a problem since the
+ * device shouldn't be sending messages until it receives a request from the
+ * host.
+ */
+
+
+/*
+ * In the case of single-CPU "no OS", this task is never preempted other than by
+ * interrupts, and the USBTMC code isn't called by interrupts, so all is OK. For "no OS",
+ * the mutex structure's main effect is to disable the USB interrupts.
+ * With an OS, this class driver uses the OSAL to perform locking. The code uses a single lock
+ * and does not call outside of this class with a lock held, so deadlocks won't happen.
+ */
+
+//Limitations:
+// "vendor-specific" commands are not handled.
+// Dealing with "termchar" must be handled by the application layer,
+// though additional error checking is does in this module.
+// talkOnly and listenOnly are NOT supported. They're not permitted
+// in USB488, anyway.
+
+/* Supported:
+ *
+ * Notification pulse
+ * Trigger
+ * Read status byte (both by interrupt endpoint and control message)
+ *
+ */
+
+
+// TODO:
+// USBTMC 3.2.2 error conditions not strictly followed
+// No local lock-out, REN, or GTL.
+// Clear message available status byte at the correct time? (488 4.3.1.3)
+
+
+#include "tusb_option.h"
+
+#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_USBTMC)
+
+#include <string.h>
+#include "usbtmc.h"
+#include "usbtmc_device.h"
+#include "device/dcd.h"
+#include "device/usbd.h"
+#include "osal/osal.h"
+
+// FIXME: I shouldn't need to include _pvt headers, but it is necessary for usbd_edpt_xfer, _stall, and _busy
+#include "device/usbd_pvt.h"
+
+#ifdef xDEBUG
+#include "uart_util.h"
+static char logMsg[150];
+#endif
+
+/*
+ * The state machine does not allow simultaneous reading and writing. This is
+ * consistent with USBTMC.
+ */
+
+typedef enum
+{
+ STATE_CLOSED, // Endpoints have not yet been opened since USB reset
+ STATE_NAK, // Bulk-out endpoint is in NAK state.
+ STATE_IDLE, // Bulk-out endpoint is waiting for CMD.
+ STATE_RCV, // Bulk-out is receiving DEV_DEP message
+ STATE_TX_REQUESTED,
+ STATE_TX_INITIATED,
+ STATE_TX_SHORTED,
+ STATE_CLEARING,
+ STATE_ABORTING_BULK_IN,
+ STATE_ABORTING_BULK_IN_SHORTED, // aborting, and short packet has been queued for transmission
+ STATE_ABORTING_BULK_IN_ABORTED, // aborting, and short packet has been transmitted
+ STATE_ABORTING_BULK_OUT,
+ STATE_NUM_STATES
+} usbtmcd_state_enum;
+
+#if (CFG_TUD_USBTMC_ENABLE_488)
+ typedef usbtmc_response_capabilities_488_t usbtmc_capabilities_specific_t;
+#else
+ typedef usbtmc_response_capabilities_t usbtmc_capabilities_specific_t;
+#endif
+
+
+typedef struct
+{
+ volatile usbtmcd_state_enum state;
+
+ uint8_t itf_id;
+ uint8_t rhport;
+ uint8_t ep_bulk_in;
+ uint8_t ep_bulk_out;
+ uint8_t ep_int_in;
+ // IN buffer is only used for first packet, not the remainder
+ // in order to deal with prepending header
+ uint8_t ep_bulk_in_buf[USBTMCD_MAX_PACKET_SIZE];
+ // OUT buffer receives one packet at a time
+ uint8_t ep_bulk_out_buf[USBTMCD_MAX_PACKET_SIZE];
+ uint32_t transfer_size_remaining; // also used for requested length for bulk IN.
+ uint32_t transfer_size_sent; // To keep track of data bytes that have been queued in FIFO (not header bytes)
+
+ uint8_t lastBulkOutTag; // used for aborts (mostly)
+ uint8_t lastBulkInTag; // used for aborts (mostly)
+
+ uint8_t const * devInBuffer; // pointer to application-layer used for transmissions
+
+ usbtmc_capabilities_specific_t const * capabilities;
+} usbtmc_interface_state_t;
+
+static usbtmc_interface_state_t usbtmc_state =
+{
+ .itf_id = 0xFF,
+};
+
+// We need all headers to fit in a single packet in this implementation.
+TU_VERIFY_STATIC(USBTMCD_MAX_PACKET_SIZE >= 32u,"USBTMC dev EP packet size too small");
+TU_VERIFY_STATIC(
+ (sizeof(usbtmc_state.ep_bulk_in_buf) % USBTMCD_MAX_PACKET_SIZE) == 0,
+ "packet buffer must be a multiple of the packet size");
+
+static bool handle_devMsgOutStart(uint8_t rhport, void *data, size_t len);
+static bool handle_devMsgOut(uint8_t rhport, void *data, size_t len, size_t packetLen);
+
+static uint8_t termChar;
+static uint8_t termCharRequested = false;
+
+
+osal_mutex_def_t usbtmcLockBuffer;
+static osal_mutex_t usbtmcLock;
+
+// Our own private lock, mostly for the state variable.
+#define criticalEnter() do {osal_mutex_lock(usbtmcLock,OSAL_TIMEOUT_WAIT_FOREVER); } while (0)
+#define criticalLeave() do {osal_mutex_unlock(usbtmcLock); } while (0)
+
+bool atomicChangeState(usbtmcd_state_enum expectedState, usbtmcd_state_enum newState)
+{
+ bool ret = true;
+ criticalEnter();
+ usbtmcd_state_enum oldState = usbtmc_state.state;
+ if (oldState == expectedState)
+ {
+ usbtmc_state.state = newState;
+ }
+ else
+ {
+ ret = false;
+ }
+ criticalLeave();
+ return ret;
+}
+
+// called from app
+// We keep a reference to the buffer, so it MUST not change until the app is
+// notified that the transfer is complete.
+// length of data is specified in the hdr.
+
+// We can't just send the whole thing at once because we need to concatanate the
+// header with the data.
+bool tud_usbtmc_transmit_dev_msg_data(
+ const void * data, size_t len,
+ bool endOfMessage,
+ bool usingTermChar)
+{
+ const unsigned int txBufLen = sizeof(usbtmc_state.ep_bulk_in_buf);
+
+#ifndef NDEBUG
+ TU_ASSERT(len > 0u);
+ TU_ASSERT(len <= usbtmc_state.transfer_size_remaining);
+ TU_ASSERT(usbtmc_state.transfer_size_sent == 0u);
+ if(usingTermChar)
+ {
+ TU_ASSERT(usbtmc_state.capabilities->bmDevCapabilities.canEndBulkInOnTermChar);
+ TU_ASSERT(termCharRequested);
+ TU_ASSERT(((uint8_t*)data)[len-1u] == termChar);
+ }
+#endif
+
+ TU_VERIFY(usbtmc_state.state == STATE_TX_REQUESTED);
+ usbtmc_msg_dev_dep_msg_in_header_t *hdr = (usbtmc_msg_dev_dep_msg_in_header_t*)usbtmc_state.ep_bulk_in_buf;
+ tu_varclr(hdr);
+ hdr->header.MsgID = USBTMC_MSGID_DEV_DEP_MSG_IN;
+ hdr->header.bTag = usbtmc_state.lastBulkInTag;
+ hdr->header.bTagInverse = (uint8_t)~(usbtmc_state.lastBulkInTag);
+ hdr->TransferSize = len;
+ hdr->bmTransferAttributes.EOM = endOfMessage;
+ hdr->bmTransferAttributes.UsingTermChar = usingTermChar;
+
+ // Copy in the header
+ const size_t headerLen = sizeof(*hdr);
+ const size_t dataLen = ((headerLen + hdr->TransferSize) <= txBufLen) ?
+ len : (txBufLen - headerLen);
+ const size_t packetLen = headerLen + dataLen;
+
+ memcpy((uint8_t*)(usbtmc_state.ep_bulk_in_buf) + headerLen, data, dataLen);
+ usbtmc_state.transfer_size_remaining = len - dataLen;
+ usbtmc_state.transfer_size_sent = dataLen;
+ usbtmc_state.devInBuffer = (uint8_t*)data + (dataLen);
+
+ bool stateChanged =
+ atomicChangeState(STATE_TX_REQUESTED, (packetLen >= txBufLen) ? STATE_TX_INITIATED : STATE_TX_SHORTED);
+ TU_VERIFY(stateChanged);
+ TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf, (uint16_t)packetLen));
+ return true;
+}
+
+void usbtmcd_init_cb(void)
+{
+ usbtmc_state.capabilities = tud_usbtmc_get_capabilities_cb();
+#ifndef NDEBUG
+# if CFG_TUD_USBTMC_ENABLE_488
+ if(usbtmc_state.capabilities->bmIntfcCapabilities488.supportsTrigger)
+ TU_ASSERT(&tud_usbtmc_msg_trigger_cb != NULL,);
+ // Per USB488 spec: table 8
+ TU_ASSERT(!usbtmc_state.capabilities->bmIntfcCapabilities.listenOnly,);
+ TU_ASSERT(!usbtmc_state.capabilities->bmIntfcCapabilities.talkOnly,);
+# endif
+ if(usbtmc_state.capabilities->bmIntfcCapabilities.supportsIndicatorPulse)
+ TU_ASSERT(&tud_usbtmc_indicator_pulse_cb != NULL,);
+#endif
+
+ usbtmcLock = osal_mutex_create(&usbtmcLockBuffer);
+}
+
+bool usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length)
+{
+ (void)rhport;
+ TU_ASSERT(usbtmc_state.state == STATE_CLOSED);
+ uint8_t const * p_desc;
+ uint8_t found_endpoints = 0;
+
+#ifndef NDEBUG
+ TU_ASSERT(itf_desc->bInterfaceClass == TUD_USBTMC_APP_CLASS);
+ TU_ASSERT(itf_desc->bInterfaceSubClass == TUD_USBTMC_APP_SUBCLASS);
+ // Only 2 or 3 endpoints are allowed for USBTMC.
+ TU_ASSERT((itf_desc->bNumEndpoints == 2) || (itf_desc->bNumEndpoints ==3));
+#endif
+
+ // Interface
+ (*p_length) = 0u;
+ p_desc = (uint8_t const *) itf_desc;
+
+ usbtmc_state.itf_id = itf_desc->bInterfaceNumber;
+ usbtmc_state.rhport = rhport;
+
+ while (found_endpoints < itf_desc->bNumEndpoints)
+ {
+ if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE])
+ {
+ tusb_desc_endpoint_t const *ep_desc = (tusb_desc_endpoint_t const *)p_desc;
+ switch(ep_desc->bmAttributes.xfer) {
+ case TUSB_XFER_BULK:
+ TU_ASSERT(ep_desc->wMaxPacketSize.size == USBTMCD_MAX_PACKET_SIZE);
+ if (tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN)
+ {
+ usbtmc_state.ep_bulk_in = ep_desc->bEndpointAddress;
+ } else {
+ usbtmc_state.ep_bulk_out = ep_desc->bEndpointAddress;
+ }
+
+ break;
+ case TUSB_XFER_INTERRUPT:
+#ifndef NDEBUG
+ TU_ASSERT(tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN);
+ TU_ASSERT(usbtmc_state.ep_int_in == 0);
+#endif
+ usbtmc_state.ep_int_in = ep_desc->bEndpointAddress;
+ break;
+ default:
+ TU_ASSERT(false);
+ }
+ TU_VERIFY( dcd_edpt_open(rhport, ep_desc));
+ found_endpoints++;
+ }
+ (*p_length) = (uint8_t)((*p_length) + p_desc[DESC_OFFSET_LEN]);
+ p_desc = tu_desc_next(p_desc);
+ }
+
+ // bulk endpoints are required, but interrupt IN is optional
+#ifndef NDEBUG
+ TU_ASSERT(usbtmc_state.ep_bulk_in != 0);
+ TU_ASSERT(usbtmc_state.ep_bulk_out != 0);
+ if (itf_desc->bNumEndpoints == 2)
+ {
+ TU_ASSERT(usbtmc_state.ep_int_in == 0);
+ }
+ else if (itf_desc->bNumEndpoints == 3)
+ {
+ TU_ASSERT(usbtmc_state.ep_int_in != 0);
+ }
+#if (CFG_TUD_USBTMC_ENABLE_488)
+ if(usbtmc_state.capabilities->bmIntfcCapabilities488.is488_2 ||
+ usbtmc_state.capabilities->bmDevCapabilities488.SR1)
+ {
+ TU_ASSERT(usbtmc_state.ep_int_in != 0);
+ }
+#endif
+#endif
+ atomicChangeState(STATE_CLOSED, STATE_NAK);
+ tud_usbtmc_open_cb(itf_desc->iInterface);
+
+ return true;
+}
+// Tell USBTMC class to set its bulk-in EP to ACK so that it can
+// receive USBTMC commands.
+// Returns false if it was already in an ACK state or is busy
+// processing a command (such as a clear). Returns true if it was
+// in the NAK state and successfully transitioned to the ACK wait
+// state.
+bool tud_usbtmc_start_bus_read()
+{
+ usbtmcd_state_enum oldState = usbtmc_state.state;
+ switch(oldState)
+ {
+ // These may transition to IDLE
+ case STATE_NAK:
+ case STATE_ABORTING_BULK_IN_ABORTED:
+ TU_VERIFY(atomicChangeState(oldState, STATE_IDLE));
+ break;
+ // When receiving, let it remain receiving
+ case STATE_RCV:
+ break;
+ default:
+ TU_VERIFY(false);
+ }
+ TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_out, usbtmc_state.ep_bulk_out_buf, 64));
+ return true;
+}
+
+void usbtmcd_reset_cb(uint8_t rhport)
+{
+ (void)rhport;
+ usbtmc_capabilities_specific_t const * capabilities = tud_usbtmc_get_capabilities_cb();
+
+ criticalEnter();
+ tu_varclr(&usbtmc_state);
+ usbtmc_state.capabilities = capabilities;
+ usbtmc_state.itf_id = 0xFFu;
+ criticalLeave();
+}
+
+static bool handle_devMsgOutStart(uint8_t rhport, void *data, size_t len)
+{
+ (void)rhport;
+ // return true upon failure, as we can assume error is being handled elsewhere.
+ TU_VERIFY(atomicChangeState(STATE_IDLE, STATE_RCV), true);
+ usbtmc_state.transfer_size_sent = 0u;
+
+ // must be a header, should have been confirmed before calling here.
+ usbtmc_msg_request_dev_dep_out *msg = (usbtmc_msg_request_dev_dep_out*)data;
+ usbtmc_state.transfer_size_remaining = msg->TransferSize;
+ TU_VERIFY(tud_usbtmc_msgBulkOut_start_cb(msg));
+
+ TU_VERIFY(handle_devMsgOut(rhport, (uint8_t*)data + sizeof(*msg), len - sizeof(*msg), len));
+ usbtmc_state.lastBulkOutTag = msg->header.bTag;
+ return true;
+}
+
+static bool handle_devMsgOut(uint8_t rhport, void *data, size_t len, size_t packetLen)
+{
+ (void)rhport;
+ // return true upon failure, as we can assume error is being handled elsewhere.
+ TU_VERIFY(usbtmc_state.state == STATE_RCV,true);
+
+ bool shortPacket = (packetLen < USBTMCD_MAX_PACKET_SIZE);
+
+ // Packet is to be considered complete when we get enough data or at a short packet.
+ bool atEnd = false;
+ if(len >= usbtmc_state.transfer_size_remaining || shortPacket)
+ {
+ atEnd = true;
+ TU_VERIFY(atomicChangeState(STATE_RCV, STATE_NAK));
+ }
+
+ len = tu_min32(len, usbtmc_state.transfer_size_remaining);
+
+ usbtmc_state.transfer_size_remaining -= len;
+ usbtmc_state.transfer_size_sent += len;
+
+ // App may (should?) call the wait_for_bus() command at this point
+ if(!tud_usbtmc_msg_data_cb(data, len, atEnd))
+ {
+ // TODO: Go to an error state upon failure other than just stalling the EP?
+ return false;
+ }
+
+
+ return true;
+}
+
+static bool handle_devMsgIn(void *data, size_t len)
+{
+ TU_VERIFY(len == sizeof(usbtmc_msg_request_dev_dep_in));
+ usbtmc_msg_request_dev_dep_in *msg = (usbtmc_msg_request_dev_dep_in*)data;
+ bool stateChanged = atomicChangeState(STATE_IDLE, STATE_TX_REQUESTED);
+ TU_VERIFY(stateChanged);
+ usbtmc_state.lastBulkInTag = msg->header.bTag;
+ usbtmc_state.transfer_size_remaining = msg->TransferSize;
+ usbtmc_state.transfer_size_sent = 0u;
+
+ termCharRequested = msg->bmTransferAttributes.TermCharEnabled;
+ termChar = msg->TermChar;
+
+ if(termCharRequested)
+ TU_VERIFY(usbtmc_state.capabilities->bmDevCapabilities.canEndBulkInOnTermChar);
+
+ TU_VERIFY(tud_usbtmc_msgBulkIn_request_cb(msg));
+ return true;
+}
+
+bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
+{
+ TU_VERIFY(result == XFER_RESULT_SUCCESS);
+ //uart_tx_str_sync("TMC XFER CB\r\n");
+ if(usbtmc_state.state == STATE_CLEARING) {
+ return true; /* I think we can ignore everything here */
+ }
+
+ if(ep_addr == usbtmc_state.ep_bulk_out)
+ {
+ usbtmc_msg_generic_t *msg = NULL;
+
+ switch(usbtmc_state.state)
+ {
+ case STATE_IDLE:
+ TU_VERIFY(xferred_bytes >= sizeof(usbtmc_msg_generic_t));
+ msg = (usbtmc_msg_generic_t*)(usbtmc_state.ep_bulk_out_buf);
+ uint8_t invInvTag = (uint8_t)~(msg->header.bTagInverse);
+ TU_VERIFY(msg->header.bTag == invInvTag);
+ TU_VERIFY(msg->header.bTag != 0x00);
+
+ switch(msg->header.MsgID) {
+ case USBTMC_MSGID_DEV_DEP_MSG_OUT:
+ if(!handle_devMsgOutStart(rhport, msg, xferred_bytes))
+ {
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ TU_VERIFY(false);
+ }
+ break;
+
+ case USBTMC_MSGID_DEV_DEP_MSG_IN:
+ TU_VERIFY(handle_devMsgIn(msg, xferred_bytes));
+ break;
+
+#if (CFG_TUD_USBTMC_ENABLE_488)
+ case USBTMC_MSGID_USB488_TRIGGER:
+ // Spec says we halt the EP if we didn't declare we support it.
+ TU_VERIFY(usbtmc_state.capabilities->bmIntfcCapabilities488.supportsTrigger);
+ TU_VERIFY(tud_usbtmc_msg_trigger_cb(msg));
+
+ break;
+#endif
+ case USBTMC_MSGID_VENDOR_SPECIFIC_MSG_OUT:
+ case USBTMC_MSGID_VENDOR_SPECIFIC_IN:
+ default:
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ TU_VERIFY(false);
+ return false;
+ }
+ return true;
+
+ case STATE_RCV:
+ if(!handle_devMsgOut(rhport, usbtmc_state.ep_bulk_out_buf, xferred_bytes, xferred_bytes))
+ {
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ TU_VERIFY(false);
+ }
+ return true;
+
+ case STATE_ABORTING_BULK_OUT:
+ TU_VERIFY(false);
+ return false; // Should be stalled by now, shouldn't have received a packet.
+ case STATE_TX_REQUESTED:
+ case STATE_TX_INITIATED:
+ case STATE_ABORTING_BULK_IN:
+ case STATE_ABORTING_BULK_IN_SHORTED:
+ case STATE_ABORTING_BULK_IN_ABORTED:
+ default:
+
+ TU_VERIFY(false);
+ }
+ }
+ else if(ep_addr == usbtmc_state.ep_bulk_in)
+ {
+ switch(usbtmc_state.state) {
+ case STATE_TX_SHORTED:
+ TU_VERIFY(atomicChangeState(STATE_TX_SHORTED, STATE_NAK));
+ TU_VERIFY(tud_usbtmc_msgBulkIn_complete_cb());
+ break;
+
+ case STATE_TX_INITIATED:
+ if(usbtmc_state.transfer_size_remaining >=sizeof(usbtmc_state.ep_bulk_in_buf))
+ {
+ // FIXME! This removes const below!
+ TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in,
+ (void*)usbtmc_state.devInBuffer,sizeof(usbtmc_state.ep_bulk_in_buf)));
+ usbtmc_state.devInBuffer += sizeof(usbtmc_state.ep_bulk_in_buf);
+ usbtmc_state.transfer_size_remaining -= sizeof(usbtmc_state.ep_bulk_in_buf);
+ usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.ep_bulk_in_buf);
+ }
+ else // last packet
+ {
+ size_t packetLen = usbtmc_state.transfer_size_remaining;
+ memcpy(usbtmc_state.ep_bulk_in_buf, usbtmc_state.devInBuffer, usbtmc_state.transfer_size_remaining);
+ usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.transfer_size_remaining);
+ usbtmc_state.transfer_size_remaining = 0;
+ usbtmc_state.devInBuffer = NULL;
+ TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)packetLen));
+ if(((packetLen % USBTMCD_MAX_PACKET_SIZE) != 0) || (packetLen == 0 ))
+ {
+ usbtmc_state.state = STATE_TX_SHORTED;
+ }
+ }
+ return true;
+ case STATE_ABORTING_BULK_IN:
+ // need to send short packet (ZLP?)
+ TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)0u));
+ usbtmc_state.state = STATE_ABORTING_BULK_IN_SHORTED;
+ return true;
+ case STATE_ABORTING_BULK_IN_SHORTED:
+ /* Done. :)*/
+ usbtmc_state.state = STATE_ABORTING_BULK_IN_ABORTED;
+ return true;
+ default:
+ TU_ASSERT(false);
+ return false;
+ }
+ }
+ else if (ep_addr == usbtmc_state.ep_int_in) {
+ // Good?
+ return true;
+ }
+ return false;
+}
+
+bool usbtmcd_control_request_cb(uint8_t rhport, tusb_control_request_t const * request) {
+
+ uint8_t tmcStatusCode = USBTMC_STATUS_FAILED;
+#if (CFG_TUD_USBTMC_ENABLE_488)
+ uint8_t bTag;
+#endif
+
+ if((request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) &&
+ (request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_ENDPOINT) &&
+ (request->bRequest == TUSB_REQ_CLEAR_FEATURE) &&
+ (request->wValue == TUSB_REQ_FEATURE_EDPT_HALT))
+ {
+ uint32_t ep_addr = (request->wIndex);
+
+ if(ep_addr == usbtmc_state.ep_bulk_out)
+ {
+ criticalEnter();
+ usbtmc_state.state = STATE_NAK; // USBD core has placed EP in NAK state for us
+ criticalLeave();
+ tud_usmtmc_bulkOut_clearFeature_cb();
+ }
+ else if (ep_addr == usbtmc_state.ep_bulk_in)
+ {
+ tud_usbtmc_bulkIn_clearFeature_cb();
+ }
+ else
+ {
+ return false;
+ }
+ return true;
+ }
+
+ // Otherwise, we only handle class requests.
+ if(request->bmRequestType_bit.type != TUSB_REQ_TYPE_CLASS)
+ {
+ return false;
+ }
+
+ // Verification that we own the interface is unneeded since it's been routed to us specifically.
+
+ switch(request->bRequest)
+ {
+ // USBTMC required requests
+ case USBTMC_bREQUEST_INITIATE_ABORT_BULK_OUT:
+ {
+ usbtmc_initiate_abort_rsp_t rsp = {
+ .bTag = usbtmc_state.lastBulkOutTag,
+ };
+ TU_VERIFY(request->bmRequestType == 0xA2); // in,class,interface
+ TU_VERIFY(request->wLength == sizeof(rsp));
+ TU_VERIFY(request->wIndex == usbtmc_state.ep_bulk_out);
+
+ // wValue is the requested bTag to abort
+ if(usbtmc_state.state != STATE_RCV)
+ {
+ rsp.USBTMC_status = USBTMC_STATUS_FAILED;
+ }
+ else if(usbtmc_state.lastBulkOutTag == (request->wValue & 0x7Fu))
+ {
+ rsp.USBTMC_status = USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
+ }
+ else
+ {
+ rsp.USBTMC_status = USBTMC_STATUS_SUCCESS;
+ // Check if we've queued a short packet
+ criticalEnter();
+ usbtmc_state.state = STATE_ABORTING_BULK_OUT;
+ criticalLeave();
+ TU_VERIFY(tud_usbtmc_initiate_abort_bulk_out_cb(&(rsp.USBTMC_status)));
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ }
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&rsp,sizeof(rsp)));
+ return true;
+ }
+
+ case USBTMC_bREQUEST_CHECK_ABORT_BULK_OUT_STATUS:
+ {
+ usbtmc_check_abort_bulk_rsp_t rsp = {
+ .USBTMC_status = USBTMC_STATUS_SUCCESS,
+ .NBYTES_RXD_TXD = usbtmc_state.transfer_size_sent
+ };
+ TU_VERIFY(request->bmRequestType == 0xA2); // in,class,EP
+ TU_VERIFY(request->wLength == sizeof(rsp));
+ TU_VERIFY(request->wIndex == usbtmc_state.ep_bulk_out);
+ TU_VERIFY(tud_usbtmc_check_abort_bulk_out_cb(&rsp));
+ TU_VERIFY(usbd_edpt_xfer(rhport, 0u, (void*)&rsp,sizeof(rsp)));
+ return true;
+ }
+
+ case USBTMC_bREQUEST_INITIATE_ABORT_BULK_IN:
+ {
+ usbtmc_initiate_abort_rsp_t rsp = {
+ .bTag = usbtmc_state.lastBulkInTag,
+ };
+ TU_VERIFY(request->bmRequestType == 0xA2); // in,class,interface
+ TU_VERIFY(request->wLength == sizeof(rsp));
+ TU_VERIFY(request->wIndex == usbtmc_state.ep_bulk_in);
+ // wValue is the requested bTag to abort
+ if((usbtmc_state.state == STATE_TX_REQUESTED || usbtmc_state.state == STATE_TX_INITIATED) &&
+ usbtmc_state.lastBulkInTag == (request->wValue & 0x7Fu))
+ {
+ rsp.USBTMC_status = USBTMC_STATUS_SUCCESS;
+ usbtmc_state.transfer_size_remaining = 0u;
+ // Check if we've queued a short packet
+ criticalEnter();
+ usbtmc_state.state = ((usbtmc_state.transfer_size_sent % USBTMCD_MAX_PACKET_SIZE) == 0) ?
+ STATE_ABORTING_BULK_IN : STATE_ABORTING_BULK_IN_SHORTED;
+ criticalLeave();
+ if(usbtmc_state.transfer_size_sent == 0)
+ {
+ // Send short packet, nothing is in the buffer yet
+ TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)0u));
+ usbtmc_state.state = STATE_ABORTING_BULK_IN_SHORTED;
+ }
+ TU_VERIFY(tud_usbtmc_initiate_abort_bulk_in_cb(&(rsp.USBTMC_status)));
+ }
+ else if((usbtmc_state.state == STATE_TX_REQUESTED || usbtmc_state.state == STATE_TX_INITIATED))
+ { // FIXME: Unsure how to check if the OUT endpoint fifo is non-empty....
+ rsp.USBTMC_status = USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
+ }
+ else
+ {
+ rsp.USBTMC_status = USBTMC_STATUS_FAILED;
+ }
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&rsp,sizeof(rsp)));
+ return true;
+ }
+
+ case USBTMC_bREQUEST_CHECK_ABORT_BULK_IN_STATUS:
+ {
+ TU_VERIFY(request->bmRequestType == 0xA2); // in,class,EP
+ TU_VERIFY(request->wLength == 8u);
+
+ usbtmc_check_abort_bulk_rsp_t rsp =
+ {
+ .USBTMC_status = USBTMC_STATUS_FAILED,
+ .bmAbortBulkIn =
+ {
+ .BulkInFifoBytes = (usbtmc_state.state != STATE_ABORTING_BULK_IN_ABORTED)
+ },
+ .NBYTES_RXD_TXD = usbtmc_state.transfer_size_sent,
+ };
+ TU_VERIFY(tud_usbtmc_check_abort_bulk_in_cb(&rsp));
+ criticalEnter();
+ switch(usbtmc_state.state)
+ {
+ case STATE_ABORTING_BULK_IN_ABORTED:
+ rsp.USBTMC_status = USBTMC_STATUS_SUCCESS;
+ usbtmc_state.state = STATE_IDLE;
+ break;
+ case STATE_ABORTING_BULK_IN:
+ case STATE_ABORTING_BULK_OUT:
+ rsp.USBTMC_status = USBTMC_STATUS_PENDING;
+ break;
+ default:
+ break;
+ }
+ criticalLeave();
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&rsp,sizeof(rsp)));
+
+ return true;
+ }
+
+ case USBTMC_bREQUEST_INITIATE_CLEAR:
+ {
+ TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
+ TU_VERIFY(request->wLength == sizeof(tmcStatusCode));
+ // After receiving an INITIATE_CLEAR request, the device must Halt the Bulk-OUT endpoint, queue the
+ // control endpoint response shown in Table 31, and clear all input buffers and output buffers.
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ usbtmc_state.transfer_size_remaining = 0;
+ criticalEnter();
+ usbtmc_state.state = STATE_CLEARING;
+ criticalLeave();
+ TU_VERIFY(tud_usbtmc_initiate_clear_cb(&tmcStatusCode));
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&tmcStatusCode,sizeof(tmcStatusCode)));
+ return true;
+ }
+
+ case USBTMC_bREQUEST_CHECK_CLEAR_STATUS:
+ {
+ TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
+ usbtmc_get_clear_status_rsp_t clearStatusRsp = {0};
+ TU_VERIFY(request->wLength == sizeof(clearStatusRsp));
+
+ if(usbd_edpt_busy(rhport, usbtmc_state.ep_bulk_in))
+ {
+ // Stuff stuck in TX buffer?
+ clearStatusRsp.bmClear.BulkInFifoBytes = 1;
+ clearStatusRsp.USBTMC_status = USBTMC_STATUS_PENDING;
+ }
+ else
+ {
+ // Let app check if it's clear
+ TU_VERIFY(tud_usbtmc_check_clear_cb(&clearStatusRsp));
+ }
+ if(clearStatusRsp.USBTMC_status == USBTMC_STATUS_SUCCESS)
+ {
+ criticalEnter();
+ usbtmc_state.state = STATE_IDLE;
+ criticalLeave();
+ }
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&clearStatusRsp,sizeof(clearStatusRsp)));
+ return true;
+ }
+
+ case USBTMC_bREQUEST_GET_CAPABILITIES:
+ {
+ TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
+ TU_VERIFY(request->wLength == sizeof(*(usbtmc_state.capabilities)));
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)usbtmc_state.capabilities, sizeof(*usbtmc_state.capabilities)));
+ return true;
+ }
+ // USBTMC Optional Requests
+
+ case USBTMC_bREQUEST_INDICATOR_PULSE: // Optional
+ {
+ TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
+ TU_VERIFY(request->wLength == sizeof(tmcStatusCode));
+ TU_VERIFY(usbtmc_state.capabilities->bmIntfcCapabilities.supportsIndicatorPulse);
+ TU_VERIFY(tud_usbtmc_indicator_pulse_cb(request, &tmcStatusCode));
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&tmcStatusCode, sizeof(tmcStatusCode)));
+ return true;
+ }
+#if (CFG_TUD_USBTMC_ENABLE_488)
+
+ // USB488 required requests
+ case USB488_bREQUEST_READ_STATUS_BYTE:
+ {
+ usbtmc_read_stb_rsp_488_t rsp;
+ TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
+ TU_VERIFY(request->wLength == sizeof(rsp)); // in,class,interface
+
+ bTag = request->wValue & 0x7F;
+ TU_VERIFY(request->bmRequestType == 0xA1);
+ TU_VERIFY((request->wValue & (~0x7F)) == 0u); // Other bits are required to be zero
+ TU_VERIFY(bTag >= 0x02 && bTag <= 127);
+ TU_VERIFY(request->wIndex == usbtmc_state.itf_id);
+ TU_VERIFY(request->wLength == 0x0003);
+ rsp.bTag = (uint8_t)bTag;
+ if(usbtmc_state.ep_int_in != 0)
+ {
+ rsp.USBTMC_status = USBTMC_STATUS_SUCCESS;
+ rsp.statusByte = 0x00; // Use interrupt endpoint, instead.
+
+ usbtmc_read_stb_interrupt_488_t intMsg =
+ {
+ .bNotify1 = {
+ .one = 1,
+ .bTag = bTag & 0x7Fu,
+ },
+ .StatusByte = tud_usbtmc_get_stb_cb(&(rsp.USBTMC_status))
+ };
+ usbd_edpt_xfer(rhport, usbtmc_state.ep_int_in, (void*)&intMsg, sizeof(intMsg));
+ }
+ else
+ {
+ rsp.statusByte = tud_usbtmc_get_stb_cb(&(rsp.USBTMC_status));
+ }
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)&rsp, sizeof(rsp)));
+ return true;
+ }
+ // USB488 optional requests
+ case USB488_bREQUEST_REN_CONTROL:
+ case USB488_bREQUEST_GO_TO_LOCAL:
+ case USB488_bREQUEST_LOCAL_LOCKOUT:
+ {
+ TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
+ TU_VERIFY(false);
+ return false;
+ }
+#endif
+
+ default:
+ TU_VERIFY(false);
+ return false;
+ }
+ TU_VERIFY(false);
+}
+
+bool usbtmcd_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request)
+{
+ (void)rhport;
+ //------------- Class Specific Request -------------//
+ TU_ASSERT (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
+
+ return true;
+}
+
+#endif /* CFG_TUD_TSMC */
diff --git a/src/class/usbtmc/usbtmc_device.h b/src/class/usbtmc/usbtmc_device.h
new file mode 100644
index 000000000..4c52989c2
--- /dev/null
+++ b/src/class/usbtmc/usbtmc_device.h
@@ -0,0 +1,123 @@
+/*
+ * usbtmc_device.h
+ *
+ * Created on: Sep 10, 2019
+ * Author: nconrad
+ */
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 N Conrad
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+
+#ifndef CLASS_USBTMC_USBTMC_DEVICE_H_
+#define CLASS_USBTMC_USBTMC_DEVICE_H_
+
+#include "usbtmc.h"
+
+// Enable 488 mode by default
+#if !defined(CFG_TUD_USBTMC_ENABLE_488)
+#define CFG_TUD_USBTMC_ENABLE_488 (1)
+#endif
+
+// USB spec says that full-speed must be 8,16,32, or 64.
+// However, this driver implementation requires it to be >=32
+#define USBTMCD_MAX_PACKET_SIZE (64u)
+
+/***********************************************
+ * Functions to be implemeted by the class implementation
+ */
+
+// In order to proceed, app must call call tud_usbtmc_start_bus_read(rhport) during or soon after:
+// * tud_usbtmc_open_cb
+// * tud_usbtmc_msg_data_cb
+// * tud_usbtmc_msgBulkIn_complete_cb
+// * tud_usbtmc_msg_trigger_cb
+// * (successful) tud_usbtmc_check_abort_bulk_out_cb
+// * (successful) tud_usbtmc_check_abort_bulk_in_cb
+// * (successful) tud_usmtmc_bulkOut_clearFeature_cb
+
+#if (CFG_TUD_USBTMC_ENABLE_488)
+usbtmc_response_capabilities_488_t const * tud_usbtmc_get_capabilities_cb(void);
+#else
+usbtmc_response_capabilities_t const * tud_usbtmc_get_capabilities_cb(void);
+#endif
+
+void tud_usbtmc_open_cb(uint8_t interface_id);
+
+bool tud_usbtmc_msgBulkOut_start_cb(usbtmc_msg_request_dev_dep_out const * msgHeader);
+// transfer_complete does not imply that a message is complete.
+bool tud_usbtmc_msg_data_cb( void *data, size_t len, bool transfer_complete);
+void tud_usmtmc_bulkOut_clearFeature_cb(void); // Notice to clear and abort the pending BULK out transfer
+
+bool tud_usbtmc_msgBulkIn_request_cb(usbtmc_msg_request_dev_dep_in const * request);
+bool tud_usbtmc_msgBulkIn_complete_cb(void);
+void tud_usbtmc_bulkIn_clearFeature_cb(void); // Notice to clear and abort the pending BULK out transfer
+
+bool tud_usbtmc_initiate_abort_bulk_in_cb(uint8_t *tmcResult);
+bool tud_usbtmc_initiate_abort_bulk_out_cb(uint8_t *tmcResult);
+bool tud_usbtmc_initiate_clear_cb(uint8_t *tmcResult);
+
+bool tud_usbtmc_check_abort_bulk_in_cb(usbtmc_check_abort_bulk_rsp_t *rsp);
+bool tud_usbtmc_check_abort_bulk_out_cb(usbtmc_check_abort_bulk_rsp_t *rsp);
+bool tud_usbtmc_check_clear_cb(usbtmc_get_clear_status_rsp_t *rsp);
+
+// Indicator pulse should be 0.5 to 1.0 seconds long
+TU_ATTR_WEAK bool tud_usbtmc_indicator_pulse_cb(tusb_control_request_t const * msg, uint8_t *tmcResult);
+
+#if (CFG_TUD_USBTMC_ENABLE_488)
+uint8_t tud_usbtmc_get_stb_cb(uint8_t *tmcResult);
+TU_ATTR_WEAK bool tud_usbtmc_msg_trigger_cb(usbtmc_msg_generic_t* msg);
+//TU_ATTR_WEAK bool tud_usbtmc_app_go_to_local_cb();
+#endif
+
+/*******************************************
+ * Called from app
+ *
+ * We keep a reference to the buffer, so it MUST not change until the app is
+ * notified that the transfer is complete.
+ ******************************************/
+
+bool tud_usbtmc_transmit_dev_msg_data(
+ const void * data, size_t len,
+ bool endOfMessage, bool usingTermChar);
+
+bool tud_usbtmc_start_bus_read(void);
+
+
+/* "callbacks" from USB device core */
+
+bool usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length);
+void usbtmcd_reset_cb(uint8_t rhport);
+bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
+bool usbtmcd_control_request_cb(uint8_t rhport, tusb_control_request_t const * request);
+bool usbtmcd_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request);
+void usbtmcd_init_cb(void);
+
+/************************************************************
+ * USBTMC Descriptor Templates
+ *************************************************************/
+
+
+#endif /* CLASS_USBTMC_USBTMC_DEVICE_H_ */
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h
index 4f0105d13..57c6e2fcf 100644
--- a/src/common/tusb_common.h
+++ b/src/common/tusb_common.h
@@ -38,7 +38,7 @@
//--------------------------------------------------------------------+
// Macros Helper
//--------------------------------------------------------------------+
-#define TU_ARRAY_SZIE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
+#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
#define TU_MIN(_x, _y) ( (_x) < (_y) ) ? (_x) : (_y) )
#define TU_MAX(_x, _y) ( (_x) > (_y) ) ? (_x) : (_y) )
diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h
index 09768ef88..58732b871 100644
--- a/src/common/tusb_compiler.h
+++ b/src/common/tusb_compiler.h
@@ -53,6 +53,9 @@
// for declaration of reserved field, make use of _TU_COUNTER_
#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
+#define TU_LITTLE_ENDIAN (0x12u)
+#define TU_BIG_ENDIAN (0x21u)
+
//--------------------------------------------------------------------+
// Compiler porting with Attribute and Endian
//--------------------------------------------------------------------+
@@ -67,20 +70,68 @@
// Endian conversion use well-known host to network (big endian) naming
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
- #define tu_htonl(u32) __builtin_bswap32(u32)
- #define tu_ntohl(u32) __builtin_bswap32(u32)
-
- #define tu_htons(u16) __builtin_bswap16(u16)
- #define tu_ntohs(u16) __builtin_bswap16(u16)
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
#else
- #define tu_htonl(u32) (u32)
- #define tu_ntohl(u32) (u32)
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
+ #endif
+
+#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
+#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
- #define tu_htons(u16) (u16)
- #define tu_ntohs(u16) (u16)
+#elif defined(__TI_COMPILER_VERSION__)
+ #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
+ #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
+ #define TU_ATTR_PACKED __attribute__ ((packed))
+ #define TU_ATTR_PREPACKED
+ #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
+
+ // __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
+ #if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
+ #else
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
#endif
+
+ #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
+ #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
+
+#else
+ #error "Compiler attribute porting is required"
+#endif
+
+#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
+
+ #define tu_htons(u16) (TU_BSWAP16(u16))
+ #define tu_ntohs(u16) (TU_BSWAP16(u16))
+
+ #define tu_htonl(u32) (TU_BSWAP32(u32))
+ #define tu_ntohl(u32) (TU_BSWAP32(u32))
+
+ #define tu_htole16(u16) (u16)
+ #define tu_le16toh(u16) (u16)
+
+ #define tu_htole32(u32) (u32)
+ #define tu_le32toh(u32) (u32)
+
+#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
+
+ #define tu_htons(u16) (u16)
+ #define tu_ntohs(u16) (u16)
+
+ #define tu_htonl(u32) (u32)
+ #define tu_ntohl(u32) (u32)
+
+
+ #define tu_htole16(u16) (tu_bswap16(u16))
+ #define tu_le16toh(u16) (tu_bswap16(u16))
+
+ #define tu_htole32(u32) (tu_bswap32(u32))
+ #define tu_le32toh(u32) (tu_bswap32(u32))
+
#else
- #error "Compiler attribute porting are required"
+ #error Byte order is undefined
#endif
#endif /* _TUSB_COMPILER_H_ */
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h
index a50e89934..ad42baad7 100644
--- a/src/common/tusb_types.h
+++ b/src/common/tusb_types.h
@@ -125,7 +125,8 @@ typedef enum
{
TUSB_REQ_TYPE_STANDARD = 0,
TUSB_REQ_TYPE_CLASS,
- TUSB_REQ_TYPE_VENDOR
+ TUSB_REQ_TYPE_VENDOR,
+ TUSB_REQ_TYPE_INVALID
} tusb_request_type_t;
typedef enum
diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h
index 2727ce043..fae0c88ae 100644
--- a/src/common/tusb_verify.h
+++ b/src/common/tusb_verify.h
@@ -36,10 +36,34 @@
* as C++ for the sake of code simplicity. Beware of a headache macro
* manipulation that you are told to stay away.
*
- * e.g
*
- * - TU_VERIFY( cond ) will return false if cond is false
- * - TU_VERIFY( cond, err) will return err instead if cond is false
+ * This contains macros for both VERIFY and ASSERT:
+ *
+ * VERIFY: Used when there is an error condition which is not the
+ * fault of the MCU. For example, bounds checking on data
+ * sent to the micro over USB should use this function.
+ * Another example is checking for buffer overflows, where
+ * returning from the active function causes a NAK.
+ *
+ * ASSERT: Used for error conditions that are caused by MCU firmware
+ * bugs. This is used to discover bugs in the code more
+ * quickly. One example would be adding assertions in library
+ * function calls to confirm a function's (untainted)
+ * parameters are valid.
+ *
+ *
+ * The difference in behaviour is that ASSERT triggers a breakpoint while
+ * verify does not.
+ *
+ * #define TU_VERIFY(cond) if(cond) return false;
+ * #define TU_VERIFY(cond,ret) if(cond) return ret;
+ *
+ * #define TU_VERIFY_HDLR(cond,handler) if(cond) {handler; return false;}
+ * #define TU_VERIFY_HDLR(cond,ret,handler) if(cond) {handler; return ret;}
+ *
+ * #define TU_ASSERT(cond) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return false;}
+ * #define TU_ASSERT(cond,ret) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return ret;}
+ *
*------------------------------------------------------------------*/
#ifdef __cplusplus
diff --git a/src/device/usbd.c b/src/device/usbd.c
index a6eda79de..3700fb988 100644
--- a/src/device/usbd.c
+++ b/src/device/usbd.c
@@ -61,6 +61,9 @@ typedef struct {
static usbd_device_t _usbd_dev = { 0 };
+// Invalid driver ID in itf2drv[] ep2drv[][] mapping
+enum { DRVID_INVALID = 0xFFu };
+
//--------------------------------------------------------------------+
// Class Driver
//--------------------------------------------------------------------+
@@ -142,9 +145,26 @@ static usbd_class_driver_t const usbd_class_drivers[] =
.sof = NULL
},
#endif
+
+ #if CFG_TUD_USBTMC
+ // Presently USBTMC is the only defined class with the APP_SPECIFIC class code.
+ // We maybe need to add subclass codes here, or a callback to ask if a driver can
+ // handle a particular interface.
+ {
+ .class_code = TUD_USBTMC_APP_CLASS,
+ //.subclass_code = TUD_USBTMC_APP_SUBCLASS
+ .init = usbtmcd_init_cb,
+ .reset = usbtmcd_reset_cb,
+ .open = usbtmcd_open_cb,
+ .control_request = usbtmcd_control_request_cb,
+ .control_complete = usbtmcd_control_complete_cb,
+ .xfer_cb = usbtmcd_xfer_cb,
+ .sof = NULL
+ },
+ #endif
};
-enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SZIE(usbd_class_drivers) };
+enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(usbd_class_drivers) };
//--------------------------------------------------------------------+
// DCD Event
@@ -213,8 +233,8 @@ static void usbd_reset(uint8_t rhport)
{
tu_varclr(&_usbd_dev);
- memset(_usbd_dev.itf2drv, 0xff, sizeof(_usbd_dev.itf2drv)); // invalid mapping
- memset(_usbd_dev.ep2drv , 0xff, sizeof(_usbd_dev.ep2drv )); // invalid mapping
+ memset(_usbd_dev.itf2drv, DRVID_INVALID, sizeof(_usbd_dev.itf2drv)); // invalid mapping
+ memset(_usbd_dev.ep2drv , DRVID_INVALID, sizeof(_usbd_dev.ep2drv )); // invalid mapping
usbd_control_reset(rhport);
@@ -287,19 +307,19 @@ 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);
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const ep_dir = tu_edpt_dir(ep_addr);
- _usbd_dev.ep_busy_map[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_busy_map[dir], epnum);
+ _usbd_dev.ep_busy_map[ep_dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_busy_map[ep_dir], epnum);
- if ( 0 == tu_edpt_number(ep_addr) )
+ 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[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)];
+ uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
@@ -346,6 +366,8 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
{
usbd_control_set_complete_callback(NULL);
+ TU_ASSERT(p_request->bmRequestType_bit.type < TUSB_REQ_TYPE_INVALID);
+
// Vendor request
if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR )
{
@@ -435,74 +457,122 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
case TUSB_REQ_RCPT_INTERFACE:
{
uint8_t const itf = tu_u16_low(p_request->wIndex);
- uint8_t const drvid = _usbd_dev.itf2drv[itf];
+ TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
+ uint8_t const drvid = _usbd_dev.itf2drv[itf];
TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT);
- switch ( p_request->bRequest )
+ if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD)
{
- case TUSB_REQ_GET_INTERFACE:
+ switch ( p_request->bRequest )
{
- // TODO not support alternate interface yet
- uint8_t alternate = 0;
- tud_control_xfer(rhport, p_request, &alternate, 1);
- }
- break;
+ case TUSB_REQ_GET_INTERFACE:
+ {
+ // TODO not support alternate interface yet
+ uint8_t alternate = 0;
+ tud_control_xfer(rhport, p_request, &alternate, 1);
+ }
+ break;
- case TUSB_REQ_SET_INTERFACE:
- {
- uint8_t alternate = (uint8_t) p_request->wValue;
+ case TUSB_REQ_SET_INTERFACE:
+ {
+ uint8_t const alternate = (uint8_t) p_request->wValue;
- // TODO not support alternate interface yet
- TU_ASSERT(alternate == 0);
+ // TODO not support alternate interface yet
+ TU_ASSERT(alternate == 0);
+ tud_control_status(rhport, p_request);
+ }
+ break;
- tud_control_status(rhport, p_request);
+ default:
+ // forward to class driver: "STD request to Interface"
+ // 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_ASSERT(usbd_class_drivers[drvid].control_request != NULL &&
+ usbd_class_drivers[drvid].control_request(rhport, p_request));
+ break;
}
- break;
-
- default:
- // forward to class driver
- // stall control endpoint if driver return false
- usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete);
- TU_ASSERT(usbd_class_drivers[drvid].control_request(rhport, p_request));
- break;
+ }else
+ {
+ // 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_ASSERT(usbd_class_drivers[drvid].control_request != NULL &&
+ usbd_class_drivers[drvid].control_request(rhport, p_request));
}
}
break;
//------------- Endpoint Request -------------//
case TUSB_REQ_RCPT_ENDPOINT:
- // Non standard request is not supported
- TU_VERIFY( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type );
+ {
+ uint8_t const ep_addr = tu_u16_low(p_request->wIndex);
+ uint8_t const ep_num = tu_edpt_number(ep_addr);
+ uint8_t const ep_dir = tu_edpt_dir(ep_addr);
- switch ( p_request->bRequest )
+ TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) );
+
+ uint8_t const drv_id = _usbd_dev.ep2drv[ep_num][ep_dir];
+ TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT);
+
+ bool ret = false;
+
+ if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
{
- case TUSB_REQ_GET_STATUS:
- {
- uint16_t status = usbd_edpt_stalled(rhport, tu_u16_low(p_request->wIndex)) ? 0x0001 : 0x0000;
- tud_control_xfer(rhport, p_request, &status, 2);
- }
- break;
+ // complete callback is also capable of stalling/acking the request
+ usbd_control_set_complete_callback(usbd_class_drivers[drv_id].control_complete);
+ }
- case TUSB_REQ_CLEAR_FEATURE:
- if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
- {
- usbd_edpt_clear_stall(rhport, tu_u16_low(p_request->wIndex));
- }
- tud_control_status(rhport, p_request);
- break;
+ // Then handle if it is standard request
+ if ( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type )
+ {
+ // force return true for standard request
+ ret = true;
- case TUSB_REQ_SET_FEATURE:
- if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
+ switch ( p_request->bRequest )
+ {
+ case TUSB_REQ_GET_STATUS:
{
- usbd_edpt_stall(rhport, tu_u16_low(p_request->wIndex));
+ uint16_t status = usbd_edpt_stalled(rhport, ep_addr) ? 0x0001 : 0x0000;
+ tud_control_xfer(rhport, p_request, &status, 2);
}
- tud_control_status(rhport, p_request);
- break;
+ break;
- // Unknown/Unsupported request
- default: TU_BREAKPOINT(); return false;
+ case TUSB_REQ_CLEAR_FEATURE:
+ if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
+ {
+ usbd_edpt_clear_stall(rhport, ep_addr);
+ }
+ tud_control_status(rhport, p_request);
+ break;
+
+ case TUSB_REQ_SET_FEATURE:
+ if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
+ {
+ usbd_edpt_stall(rhport, ep_addr);
+ }
+ tud_control_status(rhport, p_request);
+ break;
+
+ // Unknown/Unsupported request
+ default: TU_BREAKPOINT(); return false;
+ }
+ }
+ // Some classes such as TMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request
+ // We will forward all request targeted endpoint to its class driver
+ // For class-type requests: must (call tud_control_status(); return true) or (return false)
+ // 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)
+
+ if ( usbd_class_drivers[drv_id].control_request &&
+ usbd_class_drivers[drv_id].control_request(rhport, p_request))
+ {
+ ret = true;
}
+ return ret;
+ }
break;
// Unknown recipient
@@ -548,7 +618,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT );
// Interface number must not be used already TODO alternate interface
- TU_ASSERT( 0xff == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
+ TU_ASSERT( DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
uint16_t itf_len=0;
@@ -581,7 +651,7 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc,
ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = driver_id;
}
- len += tu_desc_len(p_desc);
+ len = (uint16_t)(len + tu_desc_len(p_desc));
p_desc = tu_desc_next(p_desc);
}
}
@@ -752,7 +822,7 @@ bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count
{
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
- TU_VERIFY(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && xfer_type == desc_ep->bmAttributes.xfer);
+ TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && xfer_type == desc_ep->bmAttributes.xfer);
TU_ASSERT(dcd_edpt_open(rhport, desc_ep));
if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN )
@@ -818,6 +888,7 @@ void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
dcd_edpt_stall(rhport, ep_addr);
_usbd_dev.ep_stall_map[dir] = (uint8_t) tu_bit_set(_usbd_dev.ep_stall_map[dir], epnum);
+ _usbd_dev.ep_busy_map[dir] = (uint8_t) tu_bit_set(_usbd_dev.ep_busy_map[dir], epnum);
}
void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
@@ -826,6 +897,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_busy_map[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_busy_map[dir], epnum);
_usbd_dev.ep_stall_map[dir] = (uint8_t) tu_bit_clear(_usbd_dev.ep_stall_map[dir], epnum);
}
diff --git a/src/device/usbd.h b/src/device/usbd.h
index 5f96e55a4..f0887f0db 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -261,6 +261,37 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
/* MS Endpoint (connected to embedded jack out) */\
5, TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL, 1, 3
+//------------- TUD_USBTMC/USB488 -------------//
+#define TUD_USBTMC_APP_CLASS (TUSB_CLASS_APPLICATION_SPECIFIC)
+#define TUD_USBTMC_APP_SUBCLASS 0x03u
+
+#define TUD_USBTMC_PROTOCOL_STD 0x00u
+#define TUD_USBTMC_PROTOCOL_USB488 0x01u
+
+// Interface number, number of endpoints, EP string index, USB_TMC_PROTOCOL*, bulk-out endpoint ID,
+// bulk-in endpoint ID
+#define TUD_USBTMC_IF_DESCRIPTOR(_itfnum, _bNumEndpoints, _stridx, _itfProtocol) \
+/* Interface */ \
+ 0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, _bNumEndpoints, TUD_USBTMC_APP_CLASS, TUD_USBTMC_APP_SUBCLASS, _itfProtocol, _stridx
+
+#define TUD_USBTMC_IF_DESCRIPTOR_LEN 9u
+
+#define TUD_USBTMC_BULK_DESCRIPTORS(_epout, _epin, _bulk_epsize) \
+/* Endpoint Out */ \
+7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u, \
+/* Endpoint In */ \
+7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u
+
+#define TUD_USBTMC_BULK_DESCRIPTORS_LEN (7u+7u)
+
+/* optional interrupt endpoint */ \
+// _int_pollingInterval : for LS/FS, expressed in frames (1ms each). 16 may be a good number?
+#define TUD_USBTMC_INT_DESCRIPTOR(_ep_interrupt, _ep_interrupt_size, _int_pollingInterval ) \
+7, TUSB_DESC_ENDPOINT, _ep_interrupt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_interrupt_size), 0x16
+
+#define TUD_USBTMC_INT_DESCRIPTOR_LEN (7u)
+
+
//------------- Vendor -------------//
#define TUD_VENDOR_DESC_LEN (9+7+7)
diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c
index e54d04456..5f8751b57 100644
--- a/src/device/usbd_control.c
+++ b/src/device/usbd_control.c
@@ -129,7 +129,7 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result
}
_control_state.total_transferred += xferred_bytes;
- _control_state.buffer += xferred_bytes;
+ _control_state.buffer = ((uint8_t*)_control_state.buffer) + xferred_bytes;
if ( (_control_state.requested_len == _control_state.total_transferred) || xferred_bytes < CFG_TUD_ENDOINT0_SIZE )
{
diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c
index 2b49f52e9..d5f90d2a7 100644
--- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c
+++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c
@@ -293,7 +293,7 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
break;
case TUSB_XFER_ISOCHRONOUS:
- TU_ASSERT((epnum % 3) == 3 && (epnum != 15));
+ TU_ASSERT((epnum % 3) == 0 && (epnum != 0) && (epnum != 15));
break;
default:
diff --git a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
index 96579c75b..00f5b1239 100644
--- a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
+++ b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
@@ -30,10 +30,7 @@
/**********************************************
* This driver has been tested with the following MCUs:
- *
- *
- * STM32F070RB
- *
+ * - F070, F072, L053
*
* It also should work with minimal changes for any ST MCU with an "USB A"/"PCD"/"HCD" peripheral. This
* covers:
@@ -106,14 +103,17 @@
#include "tusb_option.h"
+#define STM32F1_FSDEV ( \
+ defined(STM32F102x6) || defined(STM32F102xB) || \
+ defined(STM32F103x6) || defined(STM32F103xB) || \
+ defined(STM32F103xE) || defined(STM32F103xG) \
+)
+
#if (TUSB_OPT_DEVICE_ENABLED) && ( \
- ((CFG_TUSB_MCU) == OPT_MCU_STM32F0) || \
- (((CFG_TUSB_MCU) == OPT_MCU_STM32F1) && ( \
- defined(stm32f102x6) || defined(stm32f102xb) || \
- defined(stm32f103x6) || defined(stm32f103xb) || \
- defined(stm32f103xe) || defined(stm32f103xg) \
- )) || \
- ((CFG_TUSB_MCU) == OPT_MCU_STM32F3) \
+ (CFG_TUSB_MCU == OPT_MCU_STM32F0 ) || \
+ (CFG_TUSB_MCU == OPT_MCU_STM32F1 && STM32F1_FSDEV ) || \
+ (CFG_TUSB_MCU == OPT_MCU_STM32F3 ) || \
+ (CFG_TUSB_MCU == OPT_MCU_STM32L0 ) \
)
// In order to reduce the dependance on HAL, we undefine this.
@@ -147,31 +147,33 @@
* Checks, structs, defines, function definitions, etc.
*/
-TU_VERIFY_STATIC((MAX_EP_COUNT) <= STFSDEV_EP_COUNT,"Only 8 endpoints supported on the hardware");
+TU_VERIFY_STATIC((MAX_EP_COUNT) <= STFSDEV_EP_COUNT, "Only 8 endpoints supported on the hardware");
TU_VERIFY_STATIC(((DCD_STM32_BTABLE_BASE) + (DCD_STM32_BTABLE_LENGTH))<=(PMA_LENGTH),
"BTABLE does not fit in PMA RAM");
TU_VERIFY_STATIC(((DCD_STM32_BTABLE_BASE) % 8) == 0, "BTABLE base must be aligned to 8 bytes");
-// Max size of a USB FS packet is 64...
-#define MAX_PACKET_SIZE 64
-
-
// One of these for every EP IN & OUT, uses a bit of RAM....
typedef struct
{
uint8_t * buffer;
uint16_t total_len;
uint16_t queued_len;
+ uint16_t max_packet_size;
} xfer_ctl_t;
-static xfer_ctl_t xfer_status[MAX_EP_COUNT][2];
-#define XFER_CTL_BASE(_epnum, _dir) &xfer_status[_epnum][_dir]
+static xfer_ctl_t xfer_status[MAX_EP_COUNT][2];
+
+static inline xfer_ctl_t* xfer_ctl_ptr(uint32_t epnum, uint32_t dir)
+{
+ return &xfer_status[epnum][dir];
+}
static TU_ATTR_ALIGNED(4) uint32_t _setup_packet[6];
static uint8_t newDADDR; // Used to set the new device address during the CTR IRQ handler
+static uint8_t remoteWakeCountdown; // When wake is requested
// EP Buffers assigned from end of memory location, to minimize their chance of crashing
// into the stack.
@@ -224,7 +226,7 @@ void dcd_init (uint8_t rhport)
for(uint32_t i=0; i<STFSDEV_EP_COUNT; i++)
{
// This doesn't clear all bits since some bits are "toggle", but does set the type to DISABLED.
- PCD_GET_ENDPOINT(USB,i) = 0u;
+ pcd_set_endpoint(USB,i,0u);
}
// Initialize the BTABLE for EP0 at this point (though setting up the EP0R is unneeded)
@@ -233,7 +235,7 @@ void dcd_init (uint8_t rhport)
{
pma[PMA_STRIDE*(DCD_STM32_BTABLE_BASE + i)] = 0u;
}
- USB->CNTR |= USB_CNTR_RESETM | USB_CNTR_SOFM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
+ USB->CNTR |= USB_CNTR_RESETM | USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
dcd_handle_bus_reset();
// And finally enable pull-up, which may trigger the RESET IRQ if the host is connected.
@@ -250,13 +252,10 @@ void dcd_init (uint8_t rhport)
void dcd_int_enable (uint8_t rhport)
{
(void)rhport;
-#if defined(STM32F0)
- NVIC_SetPriority(USB_IRQn, 0);
+
+#if CFG_TUSB_MCU == OPT_MCU_STM32F0 || CFG_TUSB_MCU == OPT_MCU_STM32L0
NVIC_EnableIRQ(USB_IRQn);
-#elif defined(STM32F3)
- NVIC_SetPriority(USB_HP_CAN_TX_IRQn, 0);
- NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 0);
- NVIC_SetPriority(USBWakeUp_IRQn, 0);
+#elif CFG_TUSB_MCU == OPT_MCU_STM32F3
NVIC_EnableIRQ(USB_HP_CAN_TX_IRQn);
NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn);
NVIC_EnableIRQ(USBWakeUp_IRQn);
@@ -267,15 +266,20 @@ void dcd_int_enable (uint8_t rhport)
void dcd_int_disable(uint8_t rhport)
{
(void)rhport;
-#if defined(STM32F0)
+
+#if CFG_TUSB_MCU == OPT_MCU_STM32F0 || CFG_TUSB_MCU == OPT_MCU_STM32L0
NVIC_DisableIRQ(USB_IRQn);
-#elif defined(STM32F3)
+#elif CFG_TUSB_MCU == OPT_MCU_STM32F3
NVIC_DisableIRQ(USB_HP_CAN_TX_IRQn);
NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn);
NVIC_DisableIRQ(USBWakeUp_IRQn);
#else
-#error Unknown arch in USB driver
+ #error Unknown arch in USB driver
#endif
+ // I'm not convinced that memory synchronization is completely necessary, but
+ // it isn't a bad idea.
+ __DSB();
+ __ISB();
}
// Receive Set Address request, mcu port must also include status IN response
@@ -302,6 +306,9 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num)
void dcd_remote_wakeup(uint8_t rhport)
{
(void) rhport;
+
+ USB->CNTR |= (uint16_t) USB_CNTR_RESUME;
+ remoteWakeCountdown = 4u; // required to be 1 to 15 ms, ESOF should trigger every 1ms.
}
// I'm getting a weird warning about missing braces here that I don't
@@ -324,7 +331,9 @@ static const tusb_desc_endpoint_t ep0IN_desc =
.bEndpointAddress = 0x80
};
+#if defined(__GNUC__) && (__GNUC__ >= 7)
#pragma GCC diagnostic pop
+#endif
static void dcd_handle_bus_reset(void)
{
@@ -334,7 +343,7 @@ static void dcd_handle_bus_reset(void)
// Clear all EPREG (or maybe this is automatic? I'm not sure)
for(uint32_t i=0; i<STFSDEV_EP_COUNT; i++)
{
- PCD_GET_ENDPOINT(USB,i) = 0u;
+ pcd_set_endpoint(USB,i,0u);
}
ep_buf_ptr = DCD_STM32_BTABLE_BASE + 8*MAX_EP_COUNT; // 8 bytes per endpoint (two TX and two RX words, each)
@@ -342,13 +351,12 @@ static void dcd_handle_bus_reset(void)
dcd_edpt_open (0, &ep0IN_desc);
newDADDR = 0u;
USB->DADDR = USB_DADDR_EF; // Set enable flag, and leaving the device address as zero.
- PCD_SET_EP_RX_STATUS(USB, 0, USB_EP_RX_VALID); // And start accepting SETUP on EP0
}
// FIXME: Defined to return uint16 so that ASSERT can be used, even though a return value is not needed.
static uint16_t dcd_ep_ctr_handler(void)
{
- uint16_t count=0U;
+ uint32_t count=0U;
uint8_t EPindex;
__IO uint16_t wIstr;
__IO uint16_t wEPVal = 0U;
@@ -370,9 +378,9 @@ static uint16_t dcd_ep_ctr_handler(void)
{
/* DIR = 0 => IN int */
/* DIR = 0 implies that (EP_CTR_TX = 1) always */
- PCD_CLEAR_TX_EP_CTR(USB, 0);
+ pcd_clear_tx_ep_ctr(USB, 0);
- xfer_ctl_t * xfer = XFER_CTL_BASE(EPindex,TUSB_DIR_IN);
+ xfer_ctl_t * xfer = xfer_ctl_ptr(EPindex,TUSB_DIR_IN);
if((xfer->total_len == xfer->queued_len))
{
@@ -381,12 +389,12 @@ static uint16_t dcd_ep_ctr_handler(void)
{
// Delayed setting of the DADDR after the 0-len DATA packet acking the request is sent.
reg16_clear_bits(&USB->DADDR, USB_DADDR_ADD);
- USB->DADDR |= (uint16_t)newDADDR; // leave the enable bit set
+ USB->DADDR = (uint16_t)(USB->DADDR | newDADDR); // leave the enable bit set
newDADDR = 0;
}
if(xfer->total_len == 0) // Probably a status message?
{
- PCD_CLEAR_RX_DTOG(USB,EPindex);
+ pcd_clear_rx_dtog(USB,EPindex);
}
}
else
@@ -399,10 +407,10 @@ static uint16_t dcd_ep_ctr_handler(void)
/* DIR = 1 & CTR_RX => SETUP or OUT int */
/* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
- xfer_ctl_t *xfer = XFER_CTL_BASE(EPindex,TUSB_DIR_OUT);
+ xfer_ctl_t *xfer = xfer_ctl_ptr(EPindex,TUSB_DIR_OUT);
//ep = &hpcd->OUT_ep[0];
- wEPVal = PCD_GET_ENDPOINT(USB, EPindex);
+ wEPVal = pcd_get_endpoint(USB, EPindex);
if ((wEPVal & USB_EP_SETUP) != 0U) // SETUP
{
@@ -410,69 +418,66 @@ static uint16_t dcd_ep_ctr_handler(void)
// user memory, to allow for the 32-bit access that memcpy performs.
uint8_t userMemBuf[8];
/* Get SETUP Packet*/
- count = PCD_GET_EP_RX_CNT(USB, EPindex);
+ count = pcd_get_ep_rx_cnt(USB, EPindex);
//TU_ASSERT_ERR(count == 8);
- dcd_read_packet_memory(userMemBuf, *PCD_EP_RX_ADDRESS_PTR(USB,EPindex), 8);
+ dcd_read_packet_memory(userMemBuf, *pcd_ep_rx_address_ptr(USB,EPindex), 8);
/* SETUP bit kept frozen while CTR_RX = 1*/
dcd_event_setup_received(0, (uint8_t*)userMemBuf, true);
- PCD_CLEAR_RX_EP_CTR(USB, EPindex);
+ pcd_clear_rx_ep_ctr(USB, EPindex);
}
else if ((wEPVal & USB_EP_CTR_RX) != 0U) // OUT
{
- PCD_CLEAR_RX_EP_CTR(USB, EPindex);
+ pcd_clear_rx_ep_ctr(USB, EPindex);
/* Get Control Data OUT Packet */
- count = PCD_GET_EP_RX_CNT(USB,EPindex);
+ count = pcd_get_ep_rx_cnt(USB,EPindex);
if (count != 0U)
{
- dcd_read_packet_memory(xfer->buffer, *PCD_EP_RX_ADDRESS_PTR(USB,EPindex), count);
+ dcd_read_packet_memory(xfer->buffer, *pcd_ep_rx_address_ptr(USB,EPindex), count);
xfer->queued_len = (uint16_t)(xfer->queued_len + count);
}
/* Process Control Data OUT status Packet*/
if(EPindex == 0u && xfer->total_len == 0u)
{
- PCD_CLEAR_EP_KIND(USB,0); // Good, so allow non-zero length packets now.
+ pcd_clear_ep_kind(USB,0); // Good, so allow non-zero length packets now.
}
dcd_event_xfer_complete(0, EPindex, xfer->total_len, XFER_RESULT_SUCCESS, true);
- PCD_SET_EP_RX_CNT(USB, EPindex, CFG_TUD_ENDPOINT0_SIZE);
+ pcd_set_ep_rx_cnt(USB, EPindex, CFG_TUD_ENDPOINT0_SIZE);
if(EPindex == 0u && xfer->total_len == 0u)
{
- PCD_SET_EP_RX_STATUS(USB, EPindex, USB_EP_RX_VALID);// Await next SETUP
+ pcd_set_ep_rx_status(USB, EPindex, USB_EP_RX_VALID);// Await next SETUP
}
-
}
-
}
}
else /* Decode and service non control endpoints interrupt */
{
-
/* process related endpoint register */
- wEPVal = PCD_GET_ENDPOINT(USB, EPindex);
+ wEPVal = pcd_get_endpoint(USB, EPindex);
if ((wEPVal & USB_EP_CTR_RX) != 0U) // OUT
{
/* clear int flag */
- PCD_CLEAR_RX_EP_CTR(USB, EPindex);
+ pcd_clear_rx_ep_ctr(USB, EPindex);
- xfer_ctl_t * xfer = XFER_CTL_BASE(EPindex,TUSB_DIR_OUT);
+ xfer_ctl_t * xfer = xfer_ctl_ptr(EPindex,TUSB_DIR_OUT);
//ep = &hpcd->OUT_ep[EPindex];
- count = PCD_GET_EP_RX_CNT(USB, EPindex);
+ count = pcd_get_ep_rx_cnt(USB, EPindex);
if (count != 0U)
{
dcd_read_packet_memory(&(xfer->buffer[xfer->queued_len]),
- *PCD_EP_RX_ADDRESS_PTR(USB,EPindex), count);
+ *pcd_ep_rx_address_ptr(USB,EPindex), count);
}
/*multi-packet on the NON control OUT endpoint */
xfer->queued_len = (uint16_t)(xfer->queued_len + count);
- if ((count < 64) || (xfer->queued_len == xfer->total_len))
+ if ((count < xfer->max_packet_size) || (xfer->queued_len == xfer->total_len))
{
/* RX COMPLETE */
dcd_event_xfer_complete(0, EPindex, xfer->queued_len, XFER_RESULT_SUCCESS, true);
@@ -481,14 +486,14 @@ static uint16_t dcd_ep_ctr_handler(void)
}
else
{
- uint16_t remaining = (uint16_t)(xfer->total_len - xfer->queued_len);
- if(remaining >=64) {
- PCD_SET_EP_RX_CNT(USB, EPindex,64);
+ uint32_t remaining = (uint32_t)xfer->total_len - (uint32_t)xfer->queued_len;
+ if(remaining >= xfer->max_packet_size) {
+ pcd_set_ep_rx_cnt(USB, EPindex,xfer->max_packet_size);
} else {
- PCD_SET_EP_RX_CNT(USB, EPindex,remaining);
+ pcd_set_ep_rx_cnt(USB, EPindex,remaining);
}
- PCD_SET_EP_RX_STATUS(USB, EPindex, USB_EP_RX_VALID);
+ pcd_set_ep_rx_status(USB, EPindex, USB_EP_RX_VALID);
}
} /* if((wEPVal & EP_CTR_RX) */
@@ -496,9 +501,9 @@ static uint16_t dcd_ep_ctr_handler(void)
if ((wEPVal & USB_EP_CTR_TX) != 0U) // IN
{
/* clear int flag */
- PCD_CLEAR_TX_EP_CTR(USB, EPindex);
+ pcd_clear_tx_ep_ctr(USB, EPindex);
- xfer_ctl_t * xfer = XFER_CTL_BASE(EPindex,TUSB_DIR_IN);
+ xfer_ctl_t * xfer = xfer_ctl_ptr(EPindex,TUSB_DIR_IN);
if (xfer->queued_len != xfer->total_len) // data remaining in transfer?
{
@@ -514,8 +519,22 @@ static uint16_t dcd_ep_ctr_handler(void)
static void dcd_fs_irqHandler(void) {
- uint16_t int_status = USB->ISTR;
- // unused IRQs: (USB_ISTR_PMAOVR | USB_ISTR_ERR | USB_ISTR_WKUP | USB_ISTR_SUSP | USB_ISTR_ESOF | USB_ISTR_L1REQ )
+ uint32_t int_status = USB->ISTR;
+ //const uint32_t handled_ints = USB_ISTR_CTR | USB_ISTR_RESET | USB_ISTR_WKUP
+ // | USB_ISTR_SUSP | USB_ISTR_SOF | USB_ISTR_ESOF;
+ // unused IRQs: (USB_ISTR_PMAOVR | USB_ISTR_ERR | USB_ISTR_L1REQ )
+
+ // The ST driver loops here on the CTR bit, but that loop has been moved into the
+ // dcd_ep_ctr_handler(), so less need to loop here. The other interrupts shouldn't
+ // be triggered repeatedly.
+
+ if(int_status & USB_ISTR_RESET) {
+ // USBRST is start of reset.
+ reg16_clear_bits(&USB->ISTR, USB_ISTR_RESET);
+ dcd_handle_bus_reset();
+ dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
+ return; // Don't do the rest of the things here; perhaps they've been cleared?
+ }
if (int_status & USB_ISTR_CTR)
{
@@ -524,34 +543,45 @@ static void dcd_fs_irqHandler(void) {
dcd_ep_ctr_handler();
reg16_clear_bits(&USB->ISTR, USB_ISTR_CTR);
}
- if(int_status & USB_ISTR_RESET) {
- // USBRST is start of reset.
- reg16_clear_bits(&USB->ISTR, USB_ISTR_RESET);
- dcd_handle_bus_reset();
- dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
- }
+
if (int_status & USB_ISTR_WKUP)
{
-
reg16_clear_bits(&USB->CNTR, USB_CNTR_LPMODE);
reg16_clear_bits(&USB->CNTR, USB_CNTR_FSUSP);
reg16_clear_bits(&USB->ISTR, USB_ISTR_WKUP);
+ dcd_event_bus_signal(0, DCD_EVENT_RESUME, true);
}
if (int_status & USB_ISTR_SUSP)
{
+ /* Suspend is asserted for both suspend and unplug events. without Vbus monitoring,
+ * these events cannot be differentiated, so we only trigger suspend. */
+
/* Force low-power mode in the macrocell */
USB->CNTR |= USB_CNTR_FSUSP;
USB->CNTR |= USB_CNTR_LPMODE;
/* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
reg16_clear_bits(&USB->ISTR, USB_ISTR_SUSP);
+ dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true);
}
if(int_status & USB_ISTR_SOF) {
reg16_clear_bits(&USB->ISTR, USB_ISTR_SOF);
dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
}
+
+ if(int_status & USB_ISTR_ESOF) {
+ if(remoteWakeCountdown == 1u)
+ {
+ USB->CNTR &= (uint16_t)(~USB_CNTR_RESUME);
+ }
+ if(remoteWakeCountdown > 0u)
+ {
+ remoteWakeCountdown--;
+ }
+ reg16_clear_bits(&USB->ISTR, USB_ISTR_ESOF);
+ }
}
//--------------------------------------------------------------------+
@@ -566,47 +596,55 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc
(void)rhport;
uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress);
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
-
+ const uint16_t epMaxPktSize = p_endpoint_desc->wMaxPacketSize.size;
// Isochronous not supported (yet), and some other driver assumptions.
+
TU_ASSERT(p_endpoint_desc->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS);
- TU_ASSERT(p_endpoint_desc->wMaxPacketSize.size <= MAX_PACKET_SIZE);
TU_ASSERT(epnum < MAX_EP_COUNT);
- TU_ASSERT((p_endpoint_desc->wMaxPacketSize.size %2) == 0);
-
- // __IO uint16_t * const epreg = &(EPREG(epnum));
// Set type
switch(p_endpoint_desc->bmAttributes.xfer) {
case TUSB_XFER_CONTROL:
- PCD_SET_EPTYPE(USB, epnum, USB_EP_CONTROL); break;
- case TUSB_XFER_ISOCHRONOUS:
- PCD_SET_EPTYPE(USB, epnum, USB_EP_ISOCHRONOUS); break;
+ pcd_set_eptype(USB, epnum, USB_EP_CONTROL);
+ break;
+#if (0)
+ case TUSB_XFER_ISOCHRONOUS: // FIXME: Not yet supported
+ pcd_set_eptype(USB, epnum, USB_EP_ISOCHRONOUS); break;
+ break;
+#endif
+
case TUSB_XFER_BULK:
- PCD_SET_EPTYPE(USB, epnum, USB_EP_BULK); break;
+ pcd_set_eptype(USB, epnum, USB_EP_BULK);
+ break;
+
case TUSB_XFER_INTERRUPT:
- PCD_SET_EPTYPE(USB, epnum, USB_EP_INTERRUPT); break;
+ pcd_set_eptype(USB, epnum, USB_EP_INTERRUPT);
+ break;
+
default:
TU_ASSERT(false);
+ return false;
}
- PCD_SET_EP_ADDRESS(USB, epnum, epnum);
- PCD_CLEAR_EP_KIND(USB,0); // Be normal, for now, instead of only accepting zero-byte packets
+ pcd_set_ep_address(USB, epnum, epnum);
+ pcd_clear_ep_kind(USB,0); // Be normal, for now, instead of only accepting zero-byte packets
if(dir == TUSB_DIR_IN)
{
- *PCD_EP_TX_ADDRESS_PTR(USB, epnum) = ep_buf_ptr;
- PCD_SET_EP_RX_CNT(USB, epnum, p_endpoint_desc->wMaxPacketSize.size);
- PCD_CLEAR_TX_DTOG(USB, epnum);
- PCD_SET_EP_TX_STATUS(USB,epnum,USB_EP_TX_NAK);
+ *pcd_ep_tx_address_ptr(USB, epnum) = ep_buf_ptr;
+ pcd_set_ep_tx_cnt(USB, epnum, p_endpoint_desc->wMaxPacketSize.size);
+ pcd_clear_tx_dtog(USB, epnum);
+ pcd_set_ep_tx_status(USB,epnum,USB_EP_TX_NAK);
}
else
{
- *PCD_EP_RX_ADDRESS_PTR(USB, epnum) = ep_buf_ptr;
- PCD_SET_EP_RX_CNT(USB, epnum, p_endpoint_desc->wMaxPacketSize.size);
- PCD_CLEAR_RX_DTOG(USB, epnum);
- PCD_SET_EP_RX_STATUS(USB, epnum, USB_EP_RX_NAK);
+ *pcd_ep_rx_address_ptr(USB, epnum) = ep_buf_ptr;
+ pcd_set_ep_rx_cnt(USB, epnum, p_endpoint_desc->wMaxPacketSize.size);
+ pcd_clear_rx_dtog(USB, epnum);
+ pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_NAK);
}
+ xfer_ctl_ptr(epnum, dir)->max_packet_size = epMaxPktSize;
ep_buf_ptr = (uint16_t)(ep_buf_ptr + p_endpoint_desc->wMaxPacketSize.size); // increment buffer pointer
return true;
@@ -618,15 +656,16 @@ static void dcd_transmit_packet(xfer_ctl_t * xfer, uint16_t ep_ix)
{
uint16_t len = (uint16_t)(xfer->total_len - xfer->queued_len);
- if(len > 64u) // max packet size for FS transfer
+ if(len > xfer->max_packet_size) // max packet size for FS transfer
{
- len = 64u;
+ len = xfer->max_packet_size;
}
- dcd_write_packet_memory(*PCD_EP_TX_ADDRESS_PTR(USB,ep_ix), &(xfer->buffer[xfer->queued_len]), len);
+ uint16_t oldAddr = *pcd_ep_tx_address_ptr(USB,ep_ix);
+ dcd_write_packet_memory(oldAddr, &(xfer->buffer[xfer->queued_len]), len);
xfer->queued_len = (uint16_t)(xfer->queued_len + len);
- PCD_SET_EP_TX_CNT(USB,ep_ix,len);
- PCD_SET_EP_TX_STATUS(USB, ep_ix, USB_EP_TX_VALID);
+ pcd_set_ep_tx_cnt(USB,ep_ix,len);
+ pcd_set_ep_tx_status(USB, ep_ix, USB_EP_TX_VALID);
}
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
@@ -636,7 +675,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
uint8_t const epnum = tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr);
- xfer_ctl_t * xfer = XFER_CTL_BASE(epnum,dir);
+ xfer_ctl_t * xfer = xfer_ctl_ptr(epnum,dir);
xfer->buffer = buffer;
xfer->total_len = total_bytes;
@@ -649,15 +688,15 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
if (epnum == 0 && buffer == NULL)
{
xfer->buffer = (uint8_t*)_setup_packet;
- PCD_SET_EP_KIND(USB,0); // Expect a zero-byte INPUT
+ pcd_set_ep_kind(USB,0); // Expect a zero-byte INPUT
}
- if(total_bytes > 64)
+ if(total_bytes > xfer->max_packet_size)
{
- PCD_SET_EP_RX_CNT(USB,epnum,64);
+ pcd_set_ep_rx_cnt(USB,epnum,xfer->max_packet_size);
} else {
- PCD_SET_EP_RX_CNT(USB,epnum,total_bytes);
+ pcd_set_ep_rx_cnt(USB,epnum,total_bytes);
}
- PCD_SET_EP_RX_STATUS(USB, epnum, USB_EP_RX_VALID);
+ pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_VALID);
}
else // IN
{
@@ -670,41 +709,35 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
{
(void)rhport;
- if (ep_addr == 0) { // CTRL EP0 (OUT for setup)
- PCD_SET_EP_TX_STATUS(USB,ep_addr, USB_EP_TX_STALL);
+ if (ep_addr & 0x80)
+ { // IN
+ pcd_set_ep_tx_status(USB, ep_addr & 0x7F, USB_EP_TX_STALL);
}
-
- if (ep_addr & 0x80) { // IN
- ep_addr &= 0x7F;
- PCD_SET_EP_TX_STATUS(USB,ep_addr, USB_EP_TX_STALL);
- } else { // OUT
- PCD_SET_EP_RX_STATUS(USB,ep_addr, USB_EP_RX_STALL);
+ else
+ { // OUT
+ pcd_set_ep_rx_status(USB, ep_addr, USB_EP_RX_STALL);
}
}
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
{
(void)rhport;
- if (ep_addr == 0)
- {
- PCD_SET_EP_TX_STATUS(USB,ep_addr, USB_EP_TX_NAK);
- }
if (ep_addr & 0x80)
{ // IN
ep_addr &= 0x7F;
- PCD_SET_EP_TX_STATUS(USB,ep_addr, USB_EP_TX_NAK);
+ pcd_set_ep_tx_status(USB,ep_addr, USB_EP_TX_NAK);
/* Reset to DATA0 if clearing stall condition. */
- PCD_CLEAR_TX_DTOG(USB,ep_addr);
+ pcd_clear_tx_dtog(USB,ep_addr);
}
else
{ // OUT
/* Reset to DATA0 if clearing stall condition. */
- PCD_CLEAR_RX_DTOG(USB,ep_addr);
+ pcd_clear_rx_dtog(USB,ep_addr);
- PCD_SET_EP_RX_STATUS(USB,ep_addr, USB_EP_RX_VALID);
+ pcd_set_ep_rx_status(USB,ep_addr, USB_EP_RX_NAK);
}
}
@@ -721,18 +754,11 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
*/
static bool dcd_write_packet_memory(uint16_t dst, const void *__restrict src, size_t wNBytes)
{
- uint32_t n = ((uint32_t)((uint32_t)wNBytes + 1U)) >> 1U;
+ uint32_t n = ((uint32_t)wNBytes + 1U) >> 1U;
uint32_t i;
uint16_t temp1, temp2;
const uint8_t * srcVal;
-#ifdef DEBUG
-# if (DCD_STM32_BTABLE_BASE > 0u)
- TU_ASSERT(dst >= DCD_STM32_BTABLE_BASE);
-# endif
- TU_ASSERT(((dst%2) == 0) && (dst + wNBytes) <= (DCD_STM32_BTABLE_BASE + DCD_STM32_BTABLE_LENGTH));
-#endif
-
// The GCC optimizer will combine access to 32-bit sizes if we let it. Force
// it volatile so that it won't do that.
__IO uint16_t *pdwVal;
@@ -767,14 +793,6 @@ static bool dcd_read_packet_memory(void *__restrict dst, uint16_t src, size_t wN
__IO const uint16_t *pdwVal;
uint32_t temp;
-#ifdef DEBUG
-# if (DCD_STM32_BTABLE_BASE > 0u)
- TU_ASSERT(src >= DCD_STM32_BTABLE_BASE);
-# endif
- TU_ASSERT(((src%2) == 0) && (src + wNBytes) <= (DCD_STM32_BTABLE_BASE + DCD_STM32_BTABLE_LENGTH));
-#endif
-
-
pdwVal = &pma[PMA_STRIDE*(src>>1)];
uint8_t *dstVal = (uint8_t*)dst;
@@ -797,7 +815,7 @@ static bool dcd_read_packet_memory(void *__restrict dst, uint16_t src, size_t wN
// Interrupt handlers
-#if (CFG_TUSB_MCU) == (OPT_MCU_STM32F0)
+#if CFG_TUSB_MCU == OPT_MCU_STM32F0 || CFG_TUSB_MCU == OPT_MCU_STM32L0
void USB_IRQHandler(void)
{
dcd_fs_irqHandler();
diff --git a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev_pvt_st.h b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev_pvt_st.h
index 586d94abf..30538541a 100644
--- a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev_pvt_st.h
+++ b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev_pvt_st.h
@@ -41,51 +41,56 @@
#ifndef PORTABLE_ST_STM32F0_DCD_STM32F0_FSDEV_PVT_ST_H_
#define PORTABLE_ST_STM32F0_DCD_STM32F0_FSDEV_PVT_ST_H_
-#if defined(STM32F042x6) | \
- defined(STM32F070x6) | defined(STM32F070xB) | \
- defined(STM32F072xB) | \
+#if defined(STM32F042x6) || \
+ defined(STM32F070x6) || defined(STM32F070xB) || \
+ defined(STM32F072xB) || \
defined(STM32F078xx)
-#include "stm32f0xx.h"
-#define PMA_LENGTH 1024
-// F0x2 models are crystal-less
-// All have internal D+ pull-up
-// 070RB: 2 x 16 bits/word memory LPM Support, BCD Support
-// PMA dedicated to USB (no sharing with CAN)
-#elif defined(STM32F102x6) | defined(STM32F102x6) | \
- defined(STM32F103x6) | defined(STM32F103xB) | \
- defined(STM32F103xE) | defined(STM32F103xB)
-#include "stm32f1xx.h"
-#define PMA_LENGTH 512u
-// NO internal Pull-ups
-// *B, and *C: 2 x 16 bits/word
-#error The F102/F103 driver is expected not to work, but it might? Try it?
+ #include "stm32f0xx.h"
+ #define PMA_LENGTH (1024u)
+ // F0x2 models are crystal-less
+ // All have internal D+ pull-up
+ // 070RB: 2 x 16 bits/word memory LPM Support, BCD Support
+ // PMA dedicated to USB (no sharing with CAN)
-#elif defined(STM32F302xB) | defined(STM32F302xC) | \
- defined(STM32F303xB) | defined(STM32F303xC) | \
+#elif STM32F1_FSDEV
+ #include "stm32f1xx.h"
+ #define PMA_LENGTH (512u)
+ // NO internal Pull-ups
+ // *B, and *C: 2 x 16 bits/word
+ #error The F102/F103 driver is expected not to work, but it might? Try it?
+
+#elif defined(STM32F302xB) || defined(STM32F302xC) || \
+ defined(STM32F303xB) || defined(STM32F303xC) || \
defined(STM32F373xC)
-#include "stm32f3xx.h"
-#define PMA_LENGTH 512u
-// NO internal Pull-ups
-// *B, and *C: 1 x 16 bits/word
-// PMA dedicated to USB (no sharing with CAN)
-#elif defined(STM32F302x6) | defined(STM32F302x8) | \
- defined(STM32F302xD) | defined(STM32F302xE) | \
- defined(STM32F303xD) | defined(STM32F303xE) | \
-#include "stm32f3xx.h"
-#define PMA_LENGTH 1024u
-// NO internal Pull-ups
-// *6, *8, *D, and *E: 2 x 16 bits/word LPM Support
-// When CAN clock is enabled, USB can use first 768 bytes ONLY.
+ #include "stm32f3xx.h"
+ #define PMA_LENGTH (512u)
+ // NO internal Pull-ups
+ // *B, and *C: 1 x 16 bits/word
+ // PMA dedicated to USB (no sharing with CAN)
+
+#elif defined(STM32F302x6) || defined(STM32F302x8) || \
+ defined(STM32F302xD) || defined(STM32F302xE) || \
+ defined(STM32F303xD) || defined(STM32F303xE)
+ #include "stm32f3xx.h"
+ #define PMA_LENGTH (1024u)
+ // NO internal Pull-ups
+ // *6, *8, *D, and *E: 2 x 16 bits/word LPM Support
+ // When CAN clock is enabled, USB can use first 768 bytes ONLY.
+
+#elif CFG_TUSB_MCU == OPT_MCU_STM32L0
+ #include "stm32l0xx.h"
+ #define PMA_LENGTH (1024u)
+
#else
-#error You are using an untested or unimplemented STM32 variant. Please update the driver.
-// This includes L0x2, L0x3, L1x0, L1x1, L1x2, L4x2 and L4x3, G1x1, G1x3, and G1x4
+ #error You are using an untested or unimplemented STM32 variant. Please update the driver.
+ // This includes L1x0, L1x1, L1x2, L4x2 and L4x3, G1x1, G1x3, and G1x4
#endif
// For purposes of accessing the packet
#if ((PMA_LENGTH) == 512u)
-# define PMA_STRIDE (2u)
+ #define PMA_STRIDE (2u)
#elif ((PMA_LENGTH) == 1024u)
-# define PMA_STRIDE (1u)
+ #define PMA_STRIDE (1u)
#endif
// And for type-safety create a new macro for the volatile address of PMAADDR
@@ -93,32 +98,75 @@
// Volatile is also needed to prevent the optimizer from changing access to 32-bit (as 32-bit access is forbidden)
static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
+// prototypes
+static inline __IO uint16_t* pcd_ep_rx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum);
+static inline __IO uint16_t* pcd_ep_tx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum);
+static inline void pcd_set_endpoint(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wRegValue);
+
+
/* SetENDPOINT */
-#define PCD_SET_ENDPOINT(USBx, bEpNum,wRegValue) (*((__IO uint16_t *)(((uint32_t)(&(USBx)->EP0R + (bEpNum) * 2U))))= (uint16_t)(wRegValue))
+static inline void pcd_set_endpoint(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wRegValue)
+{
+ __O uint16_t *reg = (__O uint16_t *)((&USBx->EP0R) + bEpNum*2u);
+ *reg = (uint16_t)wRegValue;
+}
+
/* GetENDPOINT */
-#define PCD_GET_ENDPOINT(USBx, bEpNum) (*((__IO uint16_t *)(((uint32_t)(&(USBx)->EP0R + (bEpNum) * 2U)))))
-#define PCD_SET_EPTYPE(USBx, bEpNum,wType) (PCD_SET_ENDPOINT((USBx), (bEpNum),\
- (((((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & ((uint32_t)(USB_EP_T_MASK))) | ((uint32_t)(wType))) | USB_EP_CTR_RX | USB_EP_CTR_TX)))
-#define PCD_GET_EPTYPE(USBx, bEpNum) (((uint16_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EP_T_FIELD)
+static inline uint16_t pcd_get_endpoint(USB_TypeDef * USBx, uint32_t bEpNum) {
+ __I uint16_t *reg = (__I uint16_t *)((&USBx->EP0R) + bEpNum*2u);
+ return *reg;
+}
+
+static inline void pcd_set_eptype(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wType)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= (uint32_t)USB_EP_T_MASK;
+ regVal |= wType;
+ regVal |= USB_EP_CTR_RX | USB_EP_CTR_TX; // These clear on write0, so must set high
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+}
+static inline uint32_t pcd_get_eptype(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EP_T_FIELD;
+ return regVal;
+}
/**
* @brief Clears bit CTR_RX / CTR_TX in the endpoint register.
* @param USBx USB peripheral instance register address.
* @param bEpNum Endpoint Number.
* @retval None
*/
-#define PCD_CLEAR_RX_EP_CTR(USBx, bEpNum) (PCD_SET_ENDPOINT((USBx), (bEpNum),\
- PCD_GET_ENDPOINT((USBx), (bEpNum)) & 0x7FFFU & USB_EPREG_MASK))
-#define PCD_CLEAR_TX_EP_CTR(USBx, bEpNum) (PCD_SET_ENDPOINT((USBx), (bEpNum),\
- PCD_GET_ENDPOINT((USBx), (bEpNum)) & 0xFF7FU & USB_EPREG_MASK))
+static inline void pcd_clear_rx_ep_ctr(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= 0x7FFFu & USB_EPREG_MASK;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+}
+static inline void pcd_clear_tx_ep_ctr(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= regVal & 0xFF7FU & USB_EPREG_MASK;
+ pcd_set_endpoint(USBx, bEpNum,regVal);
+}
/**
* @brief gets counter of the tx buffer.
* @param USBx USB peripheral instance register address.
* @param bEpNum Endpoint Number.
* @retval Counter value
*/
-#define PCD_GET_EP_TX_CNT(USBx, bEpNum)((uint16_t)(*PCD_EP_TX_CNT_PTR((USBx), (bEpNum))) & 0x3ffU)
-#define PCD_GET_EP_RX_CNT(USBx, bEpNum)((uint16_t)(*PCD_EP_RX_CNT_PTR((USBx), (bEpNum))) & 0x3ffU)
+static inline uint32_t pcd_get_ep_tx_cnt(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ __I uint16_t *regPtr = pcd_ep_tx_cnt_ptr(USBx, bEpNum);
+ return *regPtr & 0x3ffU;
+}
+
+static inline uint32_t pcd_get_ep_rx_cnt(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ __I uint16_t *regPtr = pcd_ep_rx_cnt_ptr(USBx, bEpNum);
+ return *regPtr & 0x3ffU;
+}
/**
* @brief Sets counter of rx buffer with no. of blocks.
@@ -127,38 +175,30 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param wNBlocks no. of Blocks.
* @retval None
*/
-#define PCD_CALC_BLK32(dwReg,wCount,wNBlocks) {\
- (wNBlocks) = (uint32_t)((wCount) >> 5U);\
- if(((wCount) & 0x1fU) == 0U)\
- { \
- (wNBlocks)--;\
- } \
- *pdwReg = (uint16_t)((uint16_t)((wNBlocks) << 10U) | (uint16_t)0x8000U); \
- }/* PCD_CALC_BLK32 */
-
-
-#define PCD_CALC_BLK2(dwReg,wCount,wNBlocks) {\
- (wNBlocks) = (uint32_t)((wCount) >> 1U); \
- if(((wCount) & 0x1U) != 0U)\
- { \
- (wNBlocks)++;\
- } \
- *pdwReg = (uint16_t)((wNBlocks) << 10U);\
- }/* PCD_CALC_BLK2 */
-
-
-#define PCD_SET_EP_CNT_RX_REG(dwReg,wCount) {\
- uint32_t wNBlocks;\
- if((wCount) > 62U) \
- { \
- PCD_CALC_BLK32((dwReg),(wCount),wNBlocks) \
- } \
- else \
- { \
- PCD_CALC_BLK2((dwReg),(wCount),wNBlocks) \
- } \
- }/* PCD_SET_EP_CNT_RX_REG */
+static inline void pcd_set_ep_cnt_rx_reg(__O uint16_t * pdwReg, size_t wCount) {
+ uint32_t wNBlocks;
+ if(wCount > 62u)
+ {
+ wNBlocks = wCount >> 5u;
+ if((wCount & 0x1fU) == 0u)
+ {
+ wNBlocks--;
+ }
+ wNBlocks = wNBlocks << 10u;
+ wNBlocks |= 0x8000u; // Mark block size as 32byte
+ *pdwReg = (uint16_t)wNBlocks;
+ }
+ else
+ {
+ wNBlocks = wCount >> 1u;
+ if((wCount & 0x1U) != 0u)
+ {
+ wNBlocks++;
+ }
+ *pdwReg = (uint16_t)((wNBlocks) << 10u);
+ }
+}
/**
@@ -168,23 +208,52 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param bAddr Address.
* @retval None
*/
-#define PCD_SET_EP_ADDRESS(USBx, bEpNum,bAddr) PCD_SET_ENDPOINT((USBx), (bEpNum),\
- USB_EP_CTR_RX|USB_EP_CTR_TX|(((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EPREG_MASK) | (bAddr))
+static inline void pcd_set_ep_address(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t bAddr)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EPREG_MASK;
+ regVal |= bAddr;
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
+ pcd_set_endpoint(USBx, bEpNum,regVal);
+}
-#define PCD_BTABLE_WORD_PTR(USBx,x) (&(pma[PMA_STRIDE*((((USBx)->BTABLE)>>1) + x)]))
+static inline __IO uint16_t * pcd_btable_word_ptr(USB_TypeDef * USBx, size_t x)
+{
+ size_t total_word_offset = (((USBx)->BTABLE)>>1) + x;
+ total_word_offset *= PMA_STRIDE;
+ return &(pma[total_word_offset]);
+}
// Pointers to the PMA table entries (using the ARM address space)
-#define PCD_EP_TX_ADDRESS_PTR(USBx, bEpNum) (PCD_BTABLE_WORD_PTR(USBx,(bEpNum)*4u + 0u))
-#define PCD_EP_TX_CNT_PTR(USBx, bEpNum) (PCD_BTABLE_WORD_PTR(USBx,(bEpNum)*4u + 1u))
+static inline __IO uint16_t* pcd_ep_tx_address_ptr(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ return pcd_btable_word_ptr(USBx,(bEpNum)*4u + 0u);
+}
+static inline __IO uint16_t* pcd_ep_tx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ return pcd_btable_word_ptr(USBx,(bEpNum)*4u + 1u);
+}
+
+static inline __IO uint16_t* pcd_ep_rx_address_ptr(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ return pcd_btable_word_ptr(USBx,(bEpNum)*4u + 2u);
+}
-#define PCD_EP_RX_ADDRESS_PTR(USBx, bEpNum) (PCD_BTABLE_WORD_PTR(USBx,(bEpNum)*4u + 2u))
-#define PCD_EP_RX_CNT_PTR(USBx, bEpNum) (PCD_BTABLE_WORD_PTR(USBx,(bEpNum)*4u + 3u))
+static inline __IO uint16_t* pcd_ep_rx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ return pcd_btable_word_ptr(USBx,(bEpNum)*4u + 3u);
+}
-#define PCD_SET_EP_TX_CNT(USBx, bEpNum,wCount) (*PCD_EP_TX_CNT_PTR((USBx), (bEpNum)) = (wCount))
-#define PCD_SET_EP_RX_CNT(USBx, bEpNum,wCount) do {\
- __IO uint16_t *pdwReg =PCD_EP_RX_CNT_PTR((USBx),(bEpNum)); \
- PCD_SET_EP_CNT_RX_REG((pdwReg), (wCount))\
- } while(0)
+static inline void pcd_set_ep_tx_cnt(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wCount)
+{
+ *pcd_ep_tx_cnt_ptr(USBx, bEpNum) = (uint16_t)wCount;
+}
+
+static inline void pcd_set_ep_rx_cnt(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wCount)
+{
+ __IO uint16_t *pdwReg = pcd_ep_rx_cnt_ptr((USBx),(bEpNum));
+ pcd_set_ep_cnt_rx_reg(pdwReg, wCount);
+}
/**
* @brief sets the status for tx transfer (bits STAT_TX[1:0]).
@@ -193,21 +262,24 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param wState new state
* @retval None
*/
-#define PCD_SET_EP_TX_STATUS(USBx, bEpNum, wState) { register uint16_t _wRegVal;\
- \
- _wRegVal = (uint32_t) (((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EPTX_DTOGMASK);\
- /* toggle first bit ? */ \
- if((USB_EPTX_DTOG1 & (wState))!= 0U)\
- { \
- _wRegVal ^=(uint16_t) USB_EPTX_DTOG1; \
- } \
- /* toggle second bit ? */ \
- if((USB_EPTX_DTOG2 & ((uint32_t)(wState)))!= 0U) \
- { \
- _wRegVal ^=(uint16_t) USB_EPTX_DTOG2; \
- } \
- PCD_SET_ENDPOINT((USBx), (bEpNum), (((uint32_t)(_wRegVal)) | USB_EP_CTR_RX|USB_EP_CTR_TX));\
- } /* PCD_SET_EP_TX_STATUS */
+static inline void pcd_set_ep_tx_status(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wState)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EPTX_DTOGMASK;
+
+ /* toggle first bit ? */
+ if((USB_EPTX_DTOG1 & (wState))!= 0U)
+ {
+ regVal ^= USB_EPTX_DTOG1;
+ }
+ /* toggle second bit ? */
+ if((USB_EPTX_DTOG2 & ((uint32_t)(wState)))!= 0U)
+ {
+ regVal ^= USB_EPTX_DTOG2;
+ }
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+} /* pcd_set_ep_tx_status */
/**
* @brief sets the status for rx transfer (bits STAT_TX[1:0])
@@ -216,22 +288,25 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param wState new state
* @retval None
*/
-#define PCD_SET_EP_RX_STATUS(USBx, bEpNum,wState) {\
- register uint16_t _wRegVal; \
- \
- _wRegVal = (uint32_t) (((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EPRX_DTOGMASK);\
- /* toggle first bit ? */ \
- if((USB_EPRX_DTOG1 & (wState))!= 0U) \
- { \
- _wRegVal ^= (uint16_t) USB_EPRX_DTOG1; \
- } \
- /* toggle second bit ? */ \
- if((USB_EPRX_DTOG2 & ((uint32_t)(wState)))!= 0U) \
- { \
- _wRegVal ^= (uint16_t) USB_EPRX_DTOG2; \
- } \
- PCD_SET_ENDPOINT((USBx), (bEpNum), (((uint32_t)(_wRegVal)) | USB_EP_CTR_RX|USB_EP_CTR_TX)); \
- } /* PCD_SET_EP_RX_STATUS */
+
+static inline void pcd_set_ep_rx_status(USB_TypeDef * USBx, uint32_t bEpNum, uint32_t wState)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EPRX_DTOGMASK;
+
+ /* toggle first bit ? */
+ if((USB_EPRX_DTOG1 & wState)!= 0U)
+ {
+ regVal ^= USB_EPRX_DTOG1;
+ }
+ /* toggle second bit ? */
+ if((USB_EPRX_DTOG2 & wState)!= 0U)
+ {
+ regVal ^= USB_EPRX_DTOG2;
+ }
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+} /* pcd_set_ep_rx_status */
/**
* @brief Toggles DTOG_RX / DTOG_TX bit in the endpoint register.
@@ -239,10 +314,21 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param bEpNum Endpoint Number.
* @retval None
*/
-#define PCD_RX_DTOG(USBx, bEpNum) (PCD_SET_ENDPOINT((USBx), (bEpNum), \
- USB_EP_CTR_RX|USB_EP_CTR_TX|USB_EP_DTOG_RX | (((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EPREG_MASK)))
-#define PCD_TX_DTOG(USBx, bEpNum) (PCD_SET_ENDPOINT((USBx), (bEpNum), \
- USB_EP_CTR_RX|USB_EP_CTR_TX|USB_EP_DTOG_TX | (((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EPREG_MASK)))
+static inline void pcd_rx_dtog(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EPREG_MASK;
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX|USB_EP_DTOG_RX;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+}
+
+static inline void pcd_tx_dtog(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EPREG_MASK;
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX|USB_EP_DTOG_TX;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+}
/**
* @brief Clears DTOG_RX / DTOG_TX bit in the endpoint register.
@@ -250,14 +336,24 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param bEpNum Endpoint Number.
* @retval None
*/
-#define PCD_CLEAR_RX_DTOG(USBx, bEpNum) if((((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EP_DTOG_RX) != 0)\
- { \
- PCD_RX_DTOG((USBx),(bEpNum));\
- }
-#define PCD_CLEAR_TX_DTOG(USBx, bEpNum) if((((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EP_DTOG_TX) != 0)\
- {\
- PCD_TX_DTOG((USBx),(bEpNum));\
- }
+
+static inline void pcd_clear_rx_dtog(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ if((regVal & USB_EP_DTOG_RX) != 0)
+ {
+ pcd_rx_dtog(USBx,bEpNum);
+ }
+}
+
+static inline void pcd_clear_tx_dtog(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ if((regVal & USB_EP_DTOG_TX) != 0)
+ {
+ pcd_tx_dtog(USBx,bEpNum);
+ }
+}
/**
* @brief set & clear EP_KIND bit.
@@ -265,11 +361,22 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
* @param bEpNum Endpoint Number.
* @retval None
*/
-#define PCD_SET_EP_KIND(USBx, bEpNum) (PCD_SET_ENDPOINT((USBx), (bEpNum), \
- (USB_EP_CTR_RX|USB_EP_CTR_TX|((((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) | USB_EP_KIND) & USB_EPREG_MASK))))
-#define PCD_CLEAR_EP_KIND(USBx, bEpNum) (PCD_SET_ENDPOINT((USBx), (bEpNum), \
- (USB_EP_CTR_RX|USB_EP_CTR_TX|((((uint32_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)))) & USB_EPKIND_MASK)))))
+static inline void pcd_set_ep_kind(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal |= USB_EP_KIND;
+ regVal &= USB_EPREG_MASK;
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+}
+static inline void pcd_clear_ep_kind(USB_TypeDef * USBx, uint32_t bEpNum)
+{
+ uint32_t regVal = pcd_get_endpoint(USBx, bEpNum);
+ regVal &= USB_EPKIND_MASK;
+ regVal |= USB_EP_CTR_RX|USB_EP_CTR_TX;
+ pcd_set_endpoint(USBx, bEpNum, regVal);
+}
// This checks if the device has "LPM"
#if defined(USB_ISTR_L1REQ)
@@ -282,6 +389,6 @@ static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
USB_ISTR_RESET | USB_ISTR_SOF | USB_ISTR_ESOF | USB_ISTR_L1REQ_FORCED )
// Number of endpoints in hardware
-#define STFSDEV_EP_COUNT (8)
+#define STFSDEV_EP_COUNT (8u)
#endif /* PORTABLE_ST_STM32F0_DCD_STM32F0_FSDEV_PVT_ST_H_ */
diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c
index 71c691223..1a7f86219 100644
--- a/src/portable/st/synopsys/dcd_synopsys.c
+++ b/src/portable/st/synopsys/dcd_synopsys.c
@@ -27,10 +27,20 @@
#include "tusb_option.h"
-#if TUSB_OPT_DEVICE_ENABLED && ( CFG_TUSB_MCU == OPT_MCU_STM32F2 || \
- CFG_TUSB_MCU == OPT_MCU_STM32F4 || \
- CFG_TUSB_MCU == OPT_MCU_STM32H7 || \
- CFG_TUSB_MCU == OPT_MCU_STM32F7)
+#define STM32L4_SYNOPSYS ( \
+ defined (STM32L475xx) || defined (STM32L476xx) || \
+ defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || \
+ defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || \
+ defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx) \
+)
+
+#if TUSB_OPT_DEVICE_ENABLED && \
+ ( CFG_TUSB_MCU == OPT_MCU_STM32F2 || \
+ CFG_TUSB_MCU == OPT_MCU_STM32F4 || \
+ CFG_TUSB_MCU == OPT_MCU_STM32F7 || \
+ CFG_TUSB_MCU == OPT_MCU_STM32H7 || \
+ (CFG_TUSB_MCU == OPT_MCU_STM32L4 && STM32L4_SYNOPSYS) \
+ )
// TODO Support OTG_HS
// EP_MAX : Max number of bi-directional endpoints including EP0
@@ -52,6 +62,10 @@
#include "stm32f7xx.h"
#define EP_MAX 6
#define EP_FIFO_SIZE 1280
+#elif CFG_TUSB_MCU == OPT_MCU_STM32L4
+ #include "stm32l4xx.h"
+ #define EP_MAX 6
+ #define EP_FIFO_SIZE 1280
#else
#error "Unsupported MCUs"
#endif
@@ -61,10 +75,10 @@
/*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/
-#define DEVICE_BASE (USB_OTG_DeviceTypeDef *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_DEVICE_BASE)
-#define OUT_EP_BASE (USB_OTG_OUTEndpointTypeDef *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_OUT_ENDPOINT_BASE)
-#define IN_EP_BASE (USB_OTG_INEndpointTypeDef *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_IN_ENDPOINT_BASE)
-#define FIFO_BASE(_x) (volatile uint32_t *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_FIFO_BASE + (_x) * USB_OTG_FIFO_SIZE)
+#define DEVICE_BASE (USB_OTG_DeviceTypeDef *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_DEVICE_BASE)
+#define OUT_EP_BASE (USB_OTG_OUTEndpointTypeDef *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_OUT_ENDPOINT_BASE)
+#define IN_EP_BASE (USB_OTG_INEndpointTypeDef *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_IN_ENDPOINT_BASE)
+#define FIFO_BASE(_x) ((volatile uint32_t *) (USB_OTG_FS_PERIPH_BASE + USB_OTG_FIFO_BASE + (_x) * USB_OTG_FIFO_SIZE))
static TU_ATTR_ALIGNED(4) uint32_t _setup_packet[6];
static uint8_t _setup_offs; // We store up to 3 setup packets.
@@ -255,20 +269,21 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress);
uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress);
- // Unsupported endpoint numbers/size.
- if((desc_edpt->wMaxPacketSize.size > 64) || (epnum > EP_MAX)) {
- return false;
- }
+ TU_ASSERT(desc_edpt->wMaxPacketSize.size <= 64);
+ TU_ASSERT(epnum < EP_MAX);
xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
xfer->max_size = desc_edpt->wMaxPacketSize.size;
- if(dir == TUSB_DIR_OUT) {
+ if(dir == TUSB_DIR_OUT)
+ {
out_ep[epnum].DOEPCTL |= (1 << USB_OTG_DOEPCTL_USBAEP_Pos) | \
desc_edpt->bmAttributes.xfer << USB_OTG_DOEPCTL_EPTYP_Pos | \
desc_edpt->wMaxPacketSize.size << USB_OTG_DOEPCTL_MPSIZ_Pos;
dev->DAINTMSK |= (1 << (USB_OTG_DAINTMSK_OEPM_Pos + epnum));
- } else {
+ }
+ else
+ {
// "USB Data FIFOs" section in reference manual
// Peripheral FIFO architecture
//
@@ -290,18 +305,22 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
// Since OUT FIFO = GRXFSIZ, FIFO 0 = 16, for simplicity, we equally allocated for the rest of endpoints
// - Size : (FIFO_SIZE/4 - GRXFSIZ - 16) / (EP_MAX-1)
// - Offset: GRXFSIZ + 16 + Size*(epnum-1)
+ // - IN EP 1 gets FIFO 1, IN EP "n" gets FIFO "n".
in_ep[epnum].DIEPCTL |= (1 << USB_OTG_DIEPCTL_USBAEP_Pos) | \
- (epnum - 1) << USB_OTG_DIEPCTL_TXFNUM_Pos | \
+ epnum << USB_OTG_DIEPCTL_TXFNUM_Pos | \
desc_edpt->bmAttributes.xfer << USB_OTG_DIEPCTL_EPTYP_Pos | \
(desc_edpt->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS ? USB_OTG_DOEPCTL_SD0PID_SEVNFRM : 0) | \
desc_edpt->wMaxPacketSize.size << USB_OTG_DIEPCTL_MPSIZ_Pos;
dev->DAINTMSK |= (1 << (USB_OTG_DAINTMSK_IEPM_Pos + epnum));
- // Both TXFD and TXSA are in unit of 32-bit words
+ // Both TXFD and TXSA are in unit of 32-bit words.
+ // IN FIFO 0 was configured during enumeration, hence the "+ 16".
uint16_t const allocated_size = (USB_OTG_FS->GRXFSIZ & 0x0000ffff) + 16;
uint16_t const fifo_size = (EP_FIFO_SIZE/4 - allocated_size) / (EP_MAX-1);
uint32_t const fifo_offset = allocated_size + fifo_size*(epnum-1);
+
+ // DIEPTXF starts at FIFO #1.
USB_OTG_FS->DIEPTXF[epnum - 1] = (fifo_size << USB_OTG_DIEPTXF_INEPTXFD_Pos) | fifo_offset;
}
diff --git a/src/tusb.h b/src/tusb.h
index fe8673726..1a6ff0b10 100644
--- a/src/tusb.h
+++ b/src/tusb.h
@@ -83,6 +83,10 @@
#if CFG_TUD_VENDOR
#include "class/vendor/vendor_device.h"
#endif
+
+ #if CFG_TUD_USBTMC
+ #include "class/usbtmc/usbtmc_device.h"
+ #endif
#endif
diff --git a/src/tusb_option.h b/src/tusb_option.h
index eccb4245c..76ea59758 100644
--- a/src/tusb_option.h
+++ b/src/tusb_option.h
@@ -35,6 +35,8 @@
/** \defgroup group_mcu Supported MCU
* \ref CFG_TUSB_MCU must be defined to one of these
* @{ */
+
+// LPC
#define OPT_MCU_LPC11UXX 1 ///< NXP LPC11Uxx
#define OPT_MCU_LPC13XX 3 ///< NXP LPC13xx
#define OPT_MCU_LPC175X_6X 4 ///< NXP LPC175x, LPC176x
@@ -46,12 +48,14 @@
#define OPT_MCU_LPC54XXX 10 ///< NXP LPC54xxx
#define OPT_MCU_LPC55XX 11 ///< NXP LPC55xx
+// NRF
#define OPT_MCU_NRF5X 100 ///< Nordic nRF5x series
+// SAM
#define OPT_MCU_SAMD21 200 ///< MicroChip SAMD21
#define OPT_MCU_SAMD51 201 ///< MicroChip SAMD51
-// ST Synopsis OTG devices
+// STM32
#define OPT_MCU_STM32F0 300 ///< ST STM32F0
#define OPT_MCU_STM32F1 301 ///< ST STM32F1
#define OPT_MCU_STM32F2 302 ///< ST STM32F2
@@ -59,7 +63,9 @@
#define OPT_MCU_STM32F4 304 ///< ST STM32F4
#define OPT_MCU_STM32F7 305 ///< ST STM32F7
#define OPT_MCU_STM32H7 306 ///< ST STM32H7
-
+#define OPT_MCU_STM32L0 307 ///< ST STM32L0
+#define OPT_MCU_STM32L1 308 ///< ST STM32L1
+#define OPT_MCU_STM32L4 309 ///< ST STM32L4
/** @} */
@@ -175,6 +181,10 @@
#define CFG_TUD_VENDOR 0
#endif
+#ifndef CFG_TUD_USBTMC
+ #define CFG_TUD_USBTMC 0
+#endif
+
//--------------------------------------------------------------------
// HOST OPTIONS