aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--os/hal/hal.mk4
-rw-r--r--os/hal/include/hal.h2
-rw-r--r--os/hal/include/serial_usb.h178
-rw-r--r--os/hal/include/usb.h329
-rw-r--r--os/hal/include/usb_cdc.h73
-rw-r--r--os/hal/platforms/STM32/platform.mk1
-rw-r--r--os/hal/platforms/STM32/stm32_usb.h221
-rw-r--r--os/hal/platforms/STM32/usb_lld.c507
-rw-r--r--os/hal/platforms/STM32/usb_lld.h284
-rw-r--r--os/hal/src/hal.c10
-rw-r--r--os/hal/src/serial_usb.c328
-rw-r--r--os/hal/src/usb.c541
-rw-r--r--readme.txt7
13 files changed, 2482 insertions, 3 deletions
diff --git a/os/hal/hal.mk b/os/hal/hal.mk
index 2a6c6826b..233ac66d1 100644
--- a/os/hal/hal.mk
+++ b/os/hal/hal.mk
@@ -10,7 +10,9 @@ HALSRC = ${CHIBIOS}/os/hal/src/hal.c \
${CHIBIOS}/os/hal/src/serial.c \
${CHIBIOS}/os/hal/src/spi.c \
${CHIBIOS}/os/hal/src/uart.c \
- ${CHIBIOS}/os/hal/src/mmc_spi.c
+ ${CHIBIOS}/os/hal/src/usb.c \
+ ${CHIBIOS}/os/hal/src/mmc_spi.c \
+ ${CHIBIOS}/os/hal/src/serial_usb.c
# Required include directories
HALINC = ${CHIBIOS}/os/hal/include
diff --git a/os/hal/include/hal.h b/os/hal/include/hal.h
index 2039036b1..822dafc35 100644
--- a/os/hal/include/hal.h
+++ b/os/hal/include/hal.h
@@ -42,7 +42,9 @@
#include "serial.h"
#include "spi.h"
#include "uart.h"
+#include "usb.h"
#include "mmc_spi.h"
+#include "serial_usb.h"
/*===========================================================================*/
/* External declarations. */
diff --git a/os/hal/include/serial_usb.h b/os/hal/include/serial_usb.h
new file mode 100644
index 000000000..b75f6fe59
--- /dev/null
+++ b/os/hal/include/serial_usb.h
@@ -0,0 +1,178 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file serial_usb.h
+ * @brief Serial over USB Driver macros and structures.
+ *
+ * @addtogroup SERIAL_USB
+ * @{
+ */
+
+#ifndef _SERIAL_USB_H_
+#define _SERIAL_USB_H_
+
+#if HAL_USE_SERIAL_USB || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @brief Serial over USB buffers size.
+ * @details Configuration parameter, the buffer size must be a multiple of
+ * the USB data endpoint maximum packet size.
+ * @note The default is 64 bytes for both the transmission and receive
+ * buffers.
+ */
+#if !defined(SERIAL_USB_BUFFERS_SIZE) || defined(__DOXYGEN__)
+#define SERIAL_USB_BUFFERS_SIZE 64
+#endif
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+#if !HAL_USE_USB && !CH_USE_EVENTS
+#error "Serial over USB Driver requires HAL_USE_USB and CH_USE_EVENTS"
+#endif
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Driver state machine possible states.
+ */
+typedef enum {
+ SDU_UNINIT = 0, /**< Not initialized. */
+ SDU_STOP = 1, /**< Stopped. */
+ SDU_READY = 2 /**< Ready. */
+} sdustate_t;
+
+/**
+ * @brief Structure representing a serial over USB driver.
+ */
+typedef struct SerialUSBDriver SerialUSBDriver;
+
+/**
+ * @brief Serial over USB Driver configuration structure.
+ * @details An instance of this structure must be passed to @p sduStart()
+ * in order to configure and start the driver operations.
+ */
+typedef struct {
+ /**
+ * @brief USB driver to use.
+ */
+ USBDriver *usbp;
+ /**
+ * @brief USB driver configuration structure.
+ */
+ USBConfig usb_config;
+ /*
+ * @brief Endpoint used for data transmission.
+ */
+ usbep_t data_request_ep;
+ /*
+ * @brief Endpoint used for data reception.
+ */
+ usbep_t data_available_ep;
+ /*
+ * @brief Endpoint used for interrupt request.
+ */
+ usbep_t interrupt_request_ep;
+} SerialUSBConfig;
+
+/**
+ * @brief @p SerialDriver specific data.
+ */
+#define _serial_usb_driver_data \
+ _base_asynchronous_channel_data \
+ /* Driver state.*/ \
+ sdustate_t state; \
+ /* Input queue.*/ \
+ InputQueue iqueue; \
+ /* Output queue.*/ \
+ OutputQueue oqueue; \
+ /* Input circular buffer.*/ \
+ uint8_t ib[SERIAL_USB_BUFFERS_SIZE]; \
+ /* Output circular buffer.*/ \
+ uint8_t ob[SERIAL_USB_BUFFERS_SIZE]; \
+ /* End of the mandatory fields.*/ \
+ /* Current configuration data.*/ \
+ const SerialUSBConfig *config;
+
+/**
+ * @brief @p SerialUSBDriver specific methods.
+ */
+#define _serial_usb_driver_methods \
+ _base_asynchronous_channel_methods
+
+/**
+ * @brief @p SerialDriver virtual methods table.
+ */
+struct SerialUSBDriverVMT {
+ _serial_usb_driver_methods
+};
+
+/**
+ * @extends BaseAsynchronousChannel
+ *
+ * @brief Full duplex serial driver class.
+ * @details This class extends @p BaseAsynchronousChannel by adding physical
+ * I/O queues.
+ */
+struct SerialUSBDriver {
+ /** @brief Virtual Methods Table.*/
+ const struct SerialUSBDriverVMT *vmt;
+ _serial_usb_driver_data
+};
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void sduInit(void);
+ void sduObjectInit(SerialUSBDriver *sdp);
+ void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config);
+ void sduStop(SerialUSBDriver *sdup);
+ bool_t sduRequestsHook(USBDriver *usbp);
+ void sduDataRequest(USBDriver *usbp, usbep_t ep);
+ void sduDataAvailable(USBDriver *usbp, usbep_t ep);
+ void sduInterruptRequest(USBDriver *usbp, usbep_t ep);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_SERIAL_USB */
+
+#endif /* _SERIAL_USB_H_ */
+
+/** @} */
diff --git a/os/hal/include/usb.h b/os/hal/include/usb.h
new file mode 100644
index 000000000..e6fc8b868
--- /dev/null
+++ b/os/hal/include/usb.h
@@ -0,0 +1,329 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file usb.h
+ * @brief USB Driver macros and structures.
+ *
+ * @addtogroup USB
+ * @{
+ */
+
+#ifndef _USB_H_
+#define _USB_H_
+
+#if HAL_USE_USB || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+#define USB_RTYPE_DIR_MASK 0x80
+#define USB_RTYPE_DIR_HOST2DEV 0x00
+#define USB_RTYPE_DIR_DEV2HOST 0x80
+#define USB_RTYPE_TYPE_MASK 0x60
+#define USB_RTYPE_TYPE_STD 0x00
+#define USB_RTYPE_TYPE_CLASS 0x20
+#define USB_RTYPE_TYPE_VENDOR 0x40
+#define USB_RTYPE_TYPE_RESERVED 0x60
+#define USB_RTYPE_RECIPIENT_MASK 0x1F
+#define USB_RTYPE_RECIPIENT_DEVICE 0x00
+#define USB_RTYPE_RECIPIENT_INTERFACE 0x01
+#define USB_RTYPE_RECIPIENT_ENDPOINT 0x02
+#define USB_RTYPE_RECIPIENT_OTHER 0x03
+
+#define USB_REQ_GET_STATUS 0
+#define USB_REQ_CLEAR_FEATURE 1
+#define USB_REQ_SET_FEATURE 3
+#define USB_REQ_SET_ADDRESS 5
+#define USB_REQ_GET_DESCRIPTOR 6
+#define USB_REQ_SET_DESCRIPTOR 7
+#define USB_REQ_GET_CONFIGURATION 8
+#define USB_REQ_SET_CONFIGURATION 9
+#define USB_REQ_GET_INTERFACE 10
+#define USB_REQ_SET_INTERFACE 11
+#define USB_REQ_SYNCH_FRAME 12
+
+#define USB_DESCRIPTOR_DEVICE 1
+#define USB_DESCRIPTOR_CONFIGURATION 2
+#define USB_DESCRIPTOR_STRING 3
+#define USB_DESCRIPTOR_INTERFACE 4
+#define USB_DESCRIPTOR_ENDPOINT 5
+#define USB_DESCRIPTOR_DEVICE_QUALIFIER 6
+#define USB_DESCRIPTOR_OTHER_SPEED_CFG 7
+#define USB_DESCRIPTOR_INTERFACE_POWER 8
+
+#define USB_FEATURE_ENDPOINT_HALT 0
+#define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1
+#define USB_FEATURE_TEST_MODE 2
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Type of a structure representing an USB driver.
+ */
+typedef struct USBDriver USBDriver;
+
+/**
+ * @brief Type of an endpoint identifier.
+ */
+typedef uint8_t usbep_t;
+
+/**
+ * @brief Type of a driver state machine possible states.
+ */
+typedef enum {
+ USB_UNINIT = 0, /**< Not initialized. */
+ USB_STOP = 1, /**< Stopped. */
+ USB_READY = 2, /**< Ready, after bus reset. */
+ USB_SELECTED = 3, /**< Address assigned. */
+ USB_ACTIVE = 4, /**< Active, configuration selected.*/
+} usbstate_t;
+
+/**
+ * @brief Type of an endpoint type.
+ */
+typedef enum {
+ EP_TYPE_CTRL = 0, /**< Control endpoint. */
+ EP_TYPE_ISOC = 1, /**< Isochronous endpoint. */
+ EP_TYPE_BULK = 2, /**< Bulk endpoint. */
+ EP_TYPE_INTR = 3 /**< Interrupt endpoint. */
+} usbeptype_t;
+
+/**
+ * @brief Type of an endpoint status.
+ */
+typedef enum {
+ EP_STATUS_DISABLED = 0, /**< Endpoint not opened. */
+ EP_STATUS_STALLED = 1, /**< Endpoint opened but stalled. */
+ EP_STATUS_ACTIVE = 2 /**< Active endpoint. */
+} usbepstatus_t;
+
+/**
+ * @brief Type of an endpoint zero state machine states.
+ */
+typedef enum {
+ USB_EP0_WAITING_SETUP, /**< Waiting for SETUP data. */
+ USB_EP0_TX, /**< Trasmitting. */
+ USB_EP0_WAITING_STS, /**< Waiting status. */
+ USB_EP0_RX, /**< Receiving. */
+ USB_EP0_SENDING_STS /**< Sending status. */
+} usbep0state_t;
+
+/**
+ * @brief Type of an enumeration of the possible USB events.
+ */
+typedef enum {
+ USB_EVENT_RESET = 0, /**< Driver has been reset by host. */
+ USB_EVENT_ADDRESS = 1, /**< Address assigned. */
+ USB_EVENT_CONFIGURED = 2, /**< Configuration selected. */
+ USB_EVENT_SUSPEND = 3, /**< Entering suspend mode. */
+ USB_EVENT_RESUME = 4, /**< Leaving suspend mode. */
+ USB_EVENT_STALLED = 5, /**< Endpoint 0 error, stalled. */
+} usbevent_t;
+
+/**
+ * @brief Type of an USB descriptor.
+ */
+typedef struct {
+ /**
+ * @brief Descriptor size in unicode characters.
+ */
+ size_t ud_size;
+ /**
+ * @brief Pointer to the descriptor.
+ */
+ const uint8_t *ud_string;
+} USBDescriptor;
+
+/**
+ * @brief Type of an USB generic notification callback.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object triggering the
+ * callback
+ */
+typedef void (*usbcallback_t)(USBDriver *usbp);
+
+/**
+ * @brief Type of an USB endpoint callback.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object triggering the
+ * callback
+ * @param[in] ep endpoint number
+ */
+typedef void (*usbepcallback_t)(USBDriver *usbp, usbep_t ep);
+
+/**
+ * @brief Type of an USB event notification callback.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object triggering the
+ * callback
+ * @param[in] event event type
+ */
+typedef void (*usbeventcb_t)(USBDriver *usbp, usbevent_t event);
+
+/**
+ * @brief Type of a requests handler callback.
+ * @details The request is encoded in the @p usb_setup buffer.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object triggering the
+ * callback
+ * @return The request handling exit code.
+ * @retval FALSE Request not recognized by the handler.
+ * @retval TRUE Request handled.
+ */
+typedef bool_t (*usbreqhandler_t)(USBDriver *usbp);
+
+/**
+ * @brief Type of an USB descriptor-retrieving callback.
+ */
+typedef const USBDescriptor * (*usbgetdescriptor_t)(USBDriver *usbp,
+ uint8_t dtype,
+ uint8_t dindex,
+ uint16_t lang);
+
+#include "usb_lld.h"
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+/**
+ * @brief Returns the current frame number.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @return The current frame number.
+ *
+ * @notapi
+ */
+#define usbGetFrameNumber(usbp) usb_lld_get_frame_number(usbp)
+
+/**
+ * @brief Returns the number of bytes readable from the receive packet
+ * buffer.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @return The number of bytes that are effectively available.
+ * @retval 0 Data not yet available.
+ *
+ * @iclass
+ */
+#define usbGetReadableI(usbp, ep) usb_lld_get_readable(usbp, ep)
+
+/**
+ * @brief Endpoint read.
+ * @details The buffered packet is copied into the user buffer and then
+ * the endpoint is brought to the valid state in order to allow
+ * reception of more data.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @param[out] buf buffer where to copy the endpoint data
+ * @param[in] n maximum number of bytes to copy
+ * @return The number of bytes that were effectively available.
+ * @retval 0 Data not yet available.
+ *
+ * @iclass
+ */
+#define usbReadI(usbp, ep, buf, n) usb_lld_read(usbp, ep, buf, n)
+
+/**
+ * @brief Returns the number of bytes writeable to the transmit packet
+ * buffer.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @return The number of bytes that can be written.
+ * @retval 0 Endpoint not ready for transmission.
+ *
+ * @iclass
+ */
+#define usbGetWriteableI(usbp, ep) usb_lld_get_readable(usbp, ep)
+
+/**
+ * @brief Endpoint write.
+ * @details The user data is copied in the packer memory and then
+ * the endpoint is brought to the valid state in order to allow
+ * transmission.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object triggering the
+ * callback
+ * @param[in] ep endpoint number
+ * @param[in] buf buffer where to copy the endpoint data
+ * @param[in] n maximum number of bytes to copy
+ * @return The number of bytes that were effectively written.
+ * @retval 0 Endpoint not ready for transmission.
+ *
+ * @iclass
+ */
+#define usbWriteI(usbp, ep, buf, n) usb_lld_write(usbp, ep, buf, n)
+
+/**
+ * @brief Request transfer setup.
+ * @details This macro is used by the request handling callbacks in order to
+ * prepare a transaction over the endpoint zero.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] buf pointer to a buffer for the transaction data
+ * @param[in] n number of bytes to be transferred
+ * @param[in] endcb transfer complete callback
+ *
+ * @api
+ */
+#define usbSetupTransfer(usbp, buf, n, endcb) { \
+ (usbp)->usb_ep0next = (buf); \
+ (usbp)->usb_ep0n = (n); \
+ (usbp)->usb_ep0endcb = (endcb); \
+}
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void usbInit(void);
+ void usbObjectInit(USBDriver *usbp);
+ void usbStart(USBDriver *usbp, const USBConfig *config);
+ void usbStop(USBDriver *usbp);
+ void usbEnableEndpointI(USBDriver *usbp, usbep_t ep,
+ const USBEndpointConfig *epcp);
+ void _usb_reset(USBDriver *usbp);
+ void _usb_ep0in(USBDriver *usbp, usbep_t ep);
+ void _usb_ep0out(USBDriver *usbp, usbep_t ep);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_USB */
+
+#endif /* _USB_H_ */
+
+/** @} */
diff --git a/os/hal/include/usb_cdc.h b/os/hal/include/usb_cdc.h
new file mode 100644
index 000000000..c1d3da3e7
--- /dev/null
+++ b/os/hal/include/usb_cdc.h
@@ -0,0 +1,73 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file usb_cdc.h
+ * @brief USB Communication Device Class support header.
+ *
+ * @addtogroup USB_CDC
+ * @{
+ */
+
+#ifndef _USB_CDC_H_
+#define _USB_CDC_H_
+
+#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
+#define CDC_GET_ENCAPSULATED_RESPONSE 0x01
+#define CDC_SET_COMM_FEATURE 0x02
+#define CDC_GET_COMM_FEATURE 0x03
+#define CDC_CLEAR_COMM_FEATURE 0x04
+#define CDC_SET_AUX_LINE_STATE 0x10
+#define CDC_SET_HOOK_STATE 0x11
+#define CDC_PULSE_SETUP 0x12
+#define CDC_SEND_PULSE 0x13
+#define CDC_SET_PULSE_TIME 0x14
+#define CDC_RING_AUX_JACK 0x15
+#define CDC_SET_LINE_CODING 0x20
+#define CDC_GET_LINE_CODING 0x21
+#define CDC_SET_CONTROL_LINE_STATE 0x22
+#define CDC_SEND_BREAK 0x23
+#define CDC_SET_RINGER_PARMS 0x30
+#define CDC_GET_RINGER_PARMS 0x31
+#define CDC_SET_OPERATION_PARMS 0x32
+#define CDC_GET_OPERATION_PARMS 0x33
+
+/**
+ * @brief Type of Line Coding structure.
+ */
+typedef struct {
+ uint8_t dwDTERate[4];
+ uint8_t bCharFormat;
+ uint8_t bParityType;
+ uint8_t bDataBits;
+} cdc_linecoding_t;
+
+#define LC_STOP_1 0
+#define LC_STOP_1P5 1
+#define LC_STOP_2 2
+
+#define LC_PARITY_NONE 0
+#define LC_PARITY_ODD 1
+#define LC_PARITY_EVEN 2
+#define LC_PARITY_MARK 3
+#define LC_PARITY_SPACE 4
+
+#endif /* _USB_CDC_H_ */
+
+/** @} */
diff --git a/os/hal/platforms/STM32/platform.mk b/os/hal/platforms/STM32/platform.mk
index 35e732116..ceadcea62 100644
--- a/os/hal/platforms/STM32/platform.mk
+++ b/os/hal/platforms/STM32/platform.mk
@@ -7,6 +7,7 @@ PLATFORMSRC = ${CHIBIOS}/os/hal/platforms/STM32/hal_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/serial_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/spi_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/uart_lld.c \
+ ${CHIBIOS}/os/hal/platforms/STM32/usb_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/stm32_dma.c
# Required include directories
diff --git a/os/hal/platforms/STM32/stm32_usb.h b/os/hal/platforms/STM32/stm32_usb.h
new file mode 100644
index 000000000..8c04ba85f
--- /dev/null
+++ b/os/hal/platforms/STM32/stm32_usb.h
@@ -0,0 +1,221 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file stm32_usb.h
+ * @brief STM32 USB registers layout header.
+ * @note This file requires definitions from the ST STM32 header file
+ * stm3232f10x.h.
+ *
+ * @addtogroup STM32_USB
+ * @{
+ */
+
+#ifndef _STM32_USB_H_
+#define _STM32_USB_H_
+
+/**
+ * @brief Number of the available endpoints.
+ * @details This value does not include the endpoint 0 which is always present.
+ */
+#define USB_ENDOPOINTS_NUMBER 7
+
+/**
+ * @brief USB registers block.
+ */
+typedef struct {
+ /**
+ * @brief Endpoint registers.
+ */
+ volatile uint32_t EPR[USB_ENDOPOINTS_NUMBER + 1];
+ /*
+ * @brief Reserved space.
+ */
+ volatile uint32_t _r20[8];
+ /*
+ * @brief Control Register.
+ */
+ volatile uint32_t CNTR;
+ /*
+ * @brief Interrupt Status Register.
+ */
+ volatile uint32_t ISTR;
+ /*
+ * @brief Frame Number Register.
+ */
+ volatile uint32_t FNR;
+ /*
+ * @brief Device Address Register.
+ */
+ volatile uint32_t DADDR;
+ /*
+ * @brief Buffer Table Address.
+ */
+ volatile uint32_t BTABLE;
+} stm32_usb_t;
+
+/**
+ * @brief USB descriptor registers block.
+ */
+typedef struct {
+ /**
+ * @brief TX buffer offset register.
+ */
+ volatile uint32_t TXADDR;
+ /**
+ * @brief TX counter register.
+ */
+ volatile uint32_t TXCOUNT;
+ /**
+ * @brief RX buffer offset register.
+ */
+ volatile uint32_t RXADDR;
+ /**
+ * @brief RX counter register.
+ */
+ volatile uint32_t RXCOUNT;
+} stm32_usb_descriptor_t;
+
+/**
+ * @brief USB registers block numeric address.
+ */
+#define STM32_USB_BASE (APB1PERIPH_BASE + 0x5C00)
+
+/**
+ * @brief USB RAM numeric address.
+ */
+#define STM32_USBRAM_BASE (APB1PERIPH_BASE + 0x6000)
+
+/**
+ * @brief Pointer to the USB registers block.
+ */
+#define STM32_USB ((stm32_usb_t *)STM32_USB_BASE)
+
+/**
+ * @brief Pointer to the USB RAM.
+ */
+#define STM32_USBRAM ((uint32_t *)STM32_USBRAM_BASE)
+
+/**
+ * @brief Mask of all the toggling bits in the EPR register.
+ */
+#define EPR_TOGGLE_MASK (EPR_STAT_TX_MASK | EPR_DTOG_TX | \
+ EPR_STAT_RX_MASK | EPR_DTOG_RX | \
+ EPR_SETUP)
+
+#define EPR_EA_MASK 0x000F
+#define EPR_STAT_TX_MASK 0x0030
+#define EPR_STAT_TX_DIS 0x0000
+#define EPR_STAT_TX_STALL 0x0010
+#define EPR_STAT_TX_NAK 0x0020
+#define EPR_STAT_TX_VALID 0x0030
+#define EPR_DTOG_TX 0x0040
+#define EPR_CTR_TX 0x0080
+#define EPR_EP_KIND 0x0100
+#define EPR_EP_TYPE_MASK 0x0600
+#define EPR_EP_TYPE_BULK 0x0000
+#define EPR_EP_TYPE_CONTROL 0x0200
+#define EPR_EP_TYPE_ISO 0x0400
+#define EPR_EP_TYPE_INTERRUPT 0x0600
+#define EPR_SETUP 0x0800
+#define EPR_STAT_RX_MASK 0x3000
+#define EPR_STAT_RX_DIS 0x0000
+#define EPR_STAT_RX_STALL 0x1000
+#define EPR_STAT_RX_NAK 0x2000
+#define EPR_STAT_RX_VALID 0x3000
+#define EPR_DTOG_RX 0x4000
+#define EPR_CTR_RX 0x8000
+
+#define CNTR_FRES 0x0001
+#define CNTR_PDWN 0x0002
+#define CNTR_LP_MODE 0x0004
+#define CNTR_FSUSP 0x0008
+#define CNTR_RESUME 0x0010
+#define CNTR_ESOFM 0x0100
+#define CNTR_SOFM 0x0200
+#define CNTR_RESETM 0x0400
+#define CNTR_SUSPM 0x0800
+#define CNTR_WKUPM 0x1000
+#define CNTR_ERRM 0x2000
+#define CNTR_PMAOVRM 0x4000
+#define CNTR_CTRM 0x8000
+
+#define ISTR_EP_ID_MASK 0x000F
+#define ISTR_DIR 0x0010
+#define ISTR_ESOF 0x0100
+#define ISTR_SOF 0x0200
+#define ISTR_RESET 0x0400
+#define ISTR_SUSP 0x0800
+#define ISTR_WKUP 0x1000
+#define ISTR_ERR 0x2000
+#define ISTR_PMAOVR 0x4000
+#define ISTR_CTR 0x8000
+
+#define FNR_FN_MASK 0x07FF
+#define FNR_LSOF 0x1800
+#define FNR_LCK 0x2000
+#define FNR_RXDM 0x4000
+#define FNR_RXDP 0x8000
+
+#define DADDR_ADD_MASK 0x007F
+#define DADDR_EF 0x0080
+
+#define RXCOUNT_COUNT_MASK 0x03FF
+#define TXCOUNT_COUNT_MASK 0x03FF
+
+#define EPR_SET(ep, epr) \
+ STM32_USB->EPR[ep] = (epr) & ~EPR_TOGGLE_MASK
+
+#define EPR_TOGGLE(ep, epr) \
+ STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] ^ ((epr) & EPR_TOGGLE_MASK))
+
+#define EPR_SET_STAT_RX(ep, epr) \
+ STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] & \
+ ~(EPR_TOGGLE_MASK & ~EPR_STAT_RX_MASK)) ^ \
+ (epr)
+
+#define EPR_SET_STAT_TX(ep, epr) \
+ STM32_USB->EPR[ep] = (STM32_USB->EPR[ep] & \
+ ~(EPR_TOGGLE_MASK & ~EPR_STAT_TX_MASK)) ^ \
+ (epr)
+
+#define EPR_CLEAR_CTR_RX(ep) \
+ STM32_USB->EPR[ep] &= ~EPR_CTR_RX & ~EPR_TOGGLE_MASK
+
+#define EPR_CLEAR_CTR_TX(ep) \
+ STM32_USB->EPR[ep] &= ~EPR_CTR_TX & ~EPR_TOGGLE_MASK
+
+/**
+ * @brief Returns an endpoint descriptor pointer.
+ */
+#define USB_GET_DESCRIPTOR(ep) \
+ ((stm32_usb_descriptor_t *)((uint32_t)STM32_USBRAM_BASE + \
+ (uint32_t)STM32_USB->BTABLE * 2 + \
+ (uint32_t)(ep) * \
+ sizeof(stm32_usb_descriptor_t)))
+
+/**
+ * @brief Converts from a PMA address to a physical address.
+ */
+#define USB_ADDR2PTR(addr) \
+ ((uint32_t *)((addr) * 2 + STM32_USBRAM_BASE))
+
+#endif /* _STM32_USB_H_ */
+
+/** @} */
diff --git a/os/hal/platforms/STM32/usb_lld.c b/os/hal/platforms/STM32/usb_lld.c
new file mode 100644
index 000000000..91be0f625
--- /dev/null
+++ b/os/hal/platforms/STM32/usb_lld.c
@@ -0,0 +1,507 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file STM32/usb_lld.c
+ * @brief STM32 USB subsystem low level driver source.
+ *
+ * @addtogroup USB
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+#include "usb.h"
+
+#if HAL_USE_USB || defined(__DOXYGEN__)
+
+#define BTABLE_ADDR 0x0000
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/** @brief USB1 driver identifier.*/
+#if STM32_USB_USE_USB1 || defined(__DOXYGEN__)
+USBDriver USBD1;
+#endif
+
+/**
+ * @brief EP0 initialization structure.
+ */
+const USBEndpointConfig usb_lld_ep0config = {
+ _usb_ep0in,
+ _usb_ep0out,
+ 0x40,
+ 0x40,
+ EPR_EP_TYPE_CONTROL | EPR_STAT_TX_STALL | EPR_STAT_RX_VALID,
+ 0x40,
+ 0x80
+};
+
+/*===========================================================================*/
+/* Driver local variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+#if STM32_USB_USE_USB1 || defined(__DOXYGEN__)
+/**
+ * @brief USB high priority interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(USB_HP_IRQHandler) {
+
+ CH_IRQ_PROLOGUE();
+
+ CH_IRQ_EPILOGUE();
+}
+
+/**
+ * @brief USB low priority interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(USB_LP_IRQHandler) {
+ uint32_t istr;
+ USBDriver *usbp = &USBD1;
+
+ CH_IRQ_PROLOGUE();
+
+ istr = STM32_USB->ISTR;
+
+ /* USB bus reset condition handling.*/
+ if (istr & ISTR_RESET) {
+ _usb_reset(usbp);
+ if (usbp->usb_config->uc_event_cb)
+ usbp->usb_config->uc_event_cb(usbp, USB_EVENT_RESET);
+ STM32_USB->ISTR = ~ISTR_RESET;
+ }
+
+ /* SOF handling.*/
+ if (istr & ISTR_SOF) {
+ if (usbp->usb_config->uc_sof_cb)
+ usbp->usb_config->uc_sof_cb(usbp);
+ STM32_USB->ISTR = ~ISTR_SOF;
+ }
+
+ /* Endpoint events handling.*/
+ while (istr & ISTR_CTR) {
+ uint32_t ep;
+ uint32_t epr = STM32_USB->EPR[ep = istr & ISTR_EP_ID_MASK];
+
+ if (epr & EPR_CTR_TX) {
+ /* IN endpoint, transmission.*/
+ EPR_CLEAR_CTR_TX(ep);
+ if (usbp->usb_epc[ep]->uepc_in_cb)
+ usbp->usb_epc[ep]->uepc_in_cb(usbp, ep);
+ }
+ if (epr & EPR_CTR_RX) {
+ /* OUT endpoint, receive.*/
+ EPR_CLEAR_CTR_RX(ep);
+ if (usbp->usb_epc[ep]->uepc_out_cb)
+ usbp->usb_epc[ep]->uepc_out_cb(usbp, ep);
+ }
+ istr = STM32_USB->ISTR;
+ }
+
+ CH_IRQ_EPILOGUE();
+}
+#endif
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Low level USB driver initialization.
+ *
+ * @notapi
+ */
+void usb_lld_init(void) {
+
+ /* USB reset, ensures reset state in order to avoid trouble with JTAGs.*/
+ RCC->APB1RSTR = RCC_APB1RSTR_USBRST;
+ RCC->APB1RSTR = 0;
+
+ /* Driver initialization.*/
+ usbObjectInit(&USBD1);
+}
+
+/**
+ * @brief Configures and activates the USB peripheral.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @notapi
+ */
+void usb_lld_start(USBDriver *usbp) {
+
+ if (usbp->usb_state == USB_STOP) {
+ /* Clock activation.*/
+#if STM32_USB_USE_USB1
+ if (&USBD1 == usbp) {
+ /* USB clock enabled.*/
+ RCC->APB1ENR |= RCC_APB1ENR_USBEN;
+ /* Powers up the transceiver while holding the USB in reset state.*/
+ STM32_USB->CNTR = CNTR_FRES;
+ /* Enabling the USB IRQ vectors, this also gives enough time to allow
+ the transceiver power up (1uS).*/
+ NVICEnableVector(USB_HP_CAN1_TX_IRQn,
+ CORTEX_PRIORITY_MASK(STM32_USB_USB1_HP_IRQ_PRIORITY));
+ NVICEnableVector(USB_LP_CAN1_RX0_IRQn,
+ CORTEX_PRIORITY_MASK(STM32_USB_USB1_LP_IRQ_PRIORITY));
+
+ /* Reset procedure enforced on driver start.*/
+ _usb_reset(&USBD1);
+ }
+#endif
+ }
+ /* Configuration.*/
+}
+
+/**
+ * @brief Deactivates the USB peripheral.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @notapi
+ */
+void usb_lld_stop(USBDriver *usbp) {
+
+ /* If in ready state then disables the USB clock.*/
+ if (usbp->usb_state == USB_STOP) {
+#if STM32_ADC_USE_ADC1
+ if (&USBD1 == usbp) {
+ NVICDisableVector(USB_HP_CAN1_TX_IRQn);
+ NVICDisableVector(USB_LP_CAN1_RX0_IRQn);
+ RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
+ }
+#endif
+ }
+}
+
+/**
+ * @brief USB low level reset routine.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @notapi
+ */
+void usb_lld_reset(USBDriver *usbp) {
+ uint32_t cntr;
+
+ /* Powers up the transceiver while holding the USB in reset state.*/
+ STM32_USB->CNTR = CNTR_FRES;
+
+ /* Releases the USB reset, BTABLE is reset to zero.*/
+ STM32_USB->CNTR = 0;
+ STM32_USB->ISTR = 0;
+ STM32_USB->DADDR = DADDR_EF;
+ cntr = /*CNTR_ESOFM | */ CNTR_RESETM | /*CNTR_SUSPM |*/
+ /*CNTR_WKUPM | CNTR_ERRM | CNTR_PMAOVRM |*/ CNTR_CTRM;
+ /* The SOF interrupt is only enabled if a callback is defined for
+ this service because it is an high rate source.*/
+ if (usbp->usb_config->uc_sof_cb != NULL)
+ cntr |= CNTR_SOFM;
+ STM32_USB->CNTR = cntr;
+}
+
+/**
+ * @brief Sets the USB address.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] addr the USB address
+ *
+ * @notapi
+ */
+void usb_lld_set_address(USBDriver *usbp, uint8_t addr) {
+
+ STM32_USB->DADDR = (uint32_t)addr | DADDR_EF;
+}
+
+/**
+ * @brief Enables an endpoint.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @param[in] epcp the endpoint configuration
+ *
+ * @notapi
+ */
+void usb_lld_enable_endpoint(USBDriver *usbp, usbep_t ep,
+ const USBEndpointConfig *epcp) {
+ uint16_t nblocks;
+ stm32_usb_descriptor_t *dp;
+
+ /* EPxR register setup.*/
+ EPR_SET(ep, epcp->uepc_epr | ep);
+ EPR_TOGGLE(ep, epcp->uepc_epr);
+
+ /* Endpoint size and address initialization.*/
+ if (epcp->uepc_out_maxsize > 62)
+ nblocks = (((((epcp->uepc_out_maxsize - 1) | 0x1f) + 1) / 32) << 10) | 0x8000;
+ else
+ nblocks = ((((epcp->uepc_out_maxsize - 1) | 1) + 1) / 2) << 10;
+ dp = USB_GET_DESCRIPTOR(ep);
+ dp->TXCOUNT = 0;
+ dp->RXCOUNT = nblocks;
+ dp->TXADDR = epcp->uepc_inaddr;
+ dp->RXADDR = epcp->uepc_outaddr;
+
+ /* Logically enabling the endpoint in the USBDriver structure.*/
+ usbp->usb_epc[ep] = epcp;
+}
+
+/**
+ * @brief Disables all the active endpoints except the endpoint zero.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @notapi
+ */
+void usb_lld_disable_endpoints(USBDriver *usbp) {
+ unsigned i;
+
+ for (i = 1; i <= USB_ENDOPOINTS_NUMBER; i++) {
+ EPR_TOGGLE(i, 0);
+ EPR_SET(i, 0);
+ }
+
+}
+
+/**
+ * @brief Returns the number of bytes readable from the receive packet
+ * buffer.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @return The number of bytes that are effectively available.
+ * @retval 0 Data not yet available.
+ *
+ * @notapi
+ */
+size_t usb_lld_get_readable(USBDriver *usbp, usbep_t ep) {
+
+ if ((STM32_USB->EPR[ep] & EPR_STAT_RX_MASK) != EPR_STAT_RX_NAK)
+ return 0;
+ return (size_t)(USB_GET_DESCRIPTOR(ep)->RXCOUNT & RXCOUNT_COUNT_MASK);
+}
+
+/**
+ * @brief Endpoint read.
+ * @details The buffered packet is copied into the user buffer and then
+ * the endpoint is brought to the valid state in order to allow
+ * reception of more data.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @param[out] buf buffer where to copy the endpoint data
+ * @param[in] n maximum number of bytes to copy
+ * @return The number of bytes that were effectively available.
+ * @retval 0 Data not yet available.
+ *
+ * @notapi
+ */
+size_t usb_lld_read(USBDriver *usbp, usbep_t ep, uint8_t *buf, size_t n) {
+ uint32_t *pmap;
+ stm32_usb_descriptor_t *udp;
+ size_t count;
+
+ if ((STM32_USB->EPR[ep] & EPR_STAT_RX_MASK) != EPR_STAT_RX_NAK)
+ return 0;
+
+ udp = USB_GET_DESCRIPTOR(ep);
+ pmap = USB_ADDR2PTR(udp->RXADDR);
+ count = udp->RXCOUNT & RXCOUNT_COUNT_MASK;
+ if (n > count)
+ n = count;
+ count = (n + 1) / 2;
+ while (count) {
+ *(uint16_t *)buf = (uint16_t)*pmap++;
+ buf += 2;
+ count--;
+ }
+ EPR_SET_STAT_RX(ep, EPR_STAT_RX_VALID);
+ return n;
+}
+/**
+ * @brief Returns the number of bytes writeable to the transmit packet
+ * buffer.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @return The number of bytes that can be written.
+ * @retval 0 Endpoint not ready for transmission.
+ *
+ * @iclass
+ */
+size_t usb_lld_get_writeable(USBDriver *usbp, usbep_t ep) {
+
+ if ((STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) != EPR_STAT_TX_NAK)
+ return 0;
+ return (size_t)usbp->usb_epc[ep]->uepc_in_maxsize;
+}
+
+/**
+ * @brief Endpoint write.
+ * @details The user data is copied in the packer memory and then
+ * the endpoint is brought to the valid state in order to allow
+ * transmission.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @param[in] buf buffer where to copy the endpoint data
+ * @param[in] n maximum number of bytes to copy
+ * @return The number of bytes that were effectively written.
+ * @retval 0 Endpoint not ready for transmission.
+ *
+ * @notapi
+ */
+size_t usb_lld_write(USBDriver *usbp, usbep_t ep,
+ const uint8_t *buf,
+ size_t n) {
+ uint32_t *pmap;
+ stm32_usb_descriptor_t *udp;
+ size_t count;
+
+ if ((STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) != EPR_STAT_TX_NAK)
+ return 0;
+
+ udp = USB_GET_DESCRIPTOR(ep);
+ pmap = USB_ADDR2PTR(udp->TXADDR);
+ udp->TXCOUNT = n;
+ count = (n + 1) / 2;
+ while (count) {
+ *pmap++ = *(uint16_t *)buf;
+ buf += 2;
+ count--;
+ }
+ EPR_SET_STAT_TX(ep, EPR_STAT_TX_VALID);
+ return n;
+}
+
+/**
+ * @brief Returns the status of an IN endpoint.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ *
+ * @notapi
+ */
+usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) {
+
+ switch (STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) {
+ case EPR_STAT_TX_DIS:
+ return EP_STATUS_DISABLED;
+ case EPR_STAT_TX_STALL:
+ return EP_STATUS_STALLED;
+ default:
+ return EP_STATUS_ACTIVE;
+ }
+}
+
+/**
+ * @brief Returns the status of an OUT endpoint.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ *
+ * @notapi
+ */
+usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) {
+
+ switch (STM32_USB->EPR[ep] & EPR_STAT_RX_MASK) {
+ case EPR_STAT_RX_DIS:
+ return EP_STATUS_DISABLED;
+ case EPR_STAT_RX_STALL:
+ return EP_STATUS_STALLED;
+ default:
+ return EP_STATUS_ACTIVE;
+ }
+}
+
+/**
+ * @brief Brings an IN endpoint in the stalled state.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ *
+ * @notapi
+ */
+void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) {
+
+ EPR_SET_STAT_TX(ep, EPR_STAT_TX_STALL);
+}
+
+/**
+ * @brief Brings an OUT endpoint in the stalled state.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ *
+ * @notapi
+ */
+void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) {
+
+ EPR_SET_STAT_RX(ep, EPR_STAT_RX_STALL);
+}
+
+/**
+ * @brief Brings an IN endpoint in the active state.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ *
+ * @notapi
+ */
+void usb_lld_clear_in(USBDriver *usbp, usbep_t ep) {
+
+ /* Makes sure to not put to NAK an endpoint that is already
+ transferring.*/
+ if ((STM32_USB->EPR[ep] & EPR_STAT_TX_MASK) != EPR_STAT_TX_VALID)
+ EPR_SET_STAT_TX(ep, EPR_STAT_TX_NAK);
+}
+
+/**
+ * @brief Brings an OUT endpoint in the active state.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ *
+ * @notapi
+ */
+void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) {
+
+ /* Makes sure to not put to NAK an endpoint that is already
+ transferring.*/
+ if ((STM32_USB->EPR[ep] & EPR_STAT_RX_MASK) != EPR_STAT_RX_VALID)
+ EPR_SET_STAT_TX(ep, EPR_STAT_RX_NAK);
+}
+
+#endif /* HAL_USE_USB */
+
+/** @} */
diff --git a/os/hal/platforms/STM32/usb_lld.h b/os/hal/platforms/STM32/usb_lld.h
new file mode 100644
index 000000000..af306d8a5
--- /dev/null
+++ b/os/hal/platforms/STM32/usb_lld.h
@@ -0,0 +1,284 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file STM32/usb_lld.h
+ * @brief STM32 USB subsystem low level driver header.
+ *
+ * @addtogroup USB
+ * @{
+ */
+
+#ifndef _USB_LLD_H_
+#define _USB_LLD_H_
+
+#if HAL_USE_USB || defined(__DOXYGEN__)
+
+#include "stm32_usb.h"
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/**
+ * @brief Maximum endpoint address.
+ */
+#define USB_MAX_ENDPOINTS USB_ENDOPOINTS_NUMBER
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @brief USB1 driver enable switch.
+ * @details If set to @p TRUE the support for USB1 is included.
+ * @note The default is @p TRUE.
+ */
+#if !defined(STM32_USB_USE_USB1) || defined(__DOXYGEN__)
+#define STM32_USB_USE_USB1 TRUE
+#endif
+
+/**
+ * @brief USB1 interrupt priority level setting.
+ */
+#if !defined(STM32_USB_USB1_HP_IRQ_PRIORITY) || defined(__DOXYGEN__)
+#define STM32_USB_USB1_HP_IRQ_PRIORITY 6
+#endif
+
+/**
+ * @brief USB1 interrupt priority level setting.
+ */
+#if !defined(STM32_USB_USB1_LP_IRQ_PRIORITY) || defined(__DOXYGEN__)
+#define STM32_USB_USB1_LP_IRQ_PRIORITY 14
+#endif
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+#if STM32_USB_USE_USB1 && !STM32_HAS_USB
+#error "USB not present in the selected device"
+#endif
+
+#if !STM32_USB_USE_USB1
+#error "USB driver activated but no USB peripheral assigned"
+#endif
+
+#if STM32_USBCLK != 48000000
+#error "the USB driver requires a 48MHz clock"
+#endif
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Type of an USB Endpoint configuration structure.
+ * @note Platform specific restrictions may apply to endpoints.
+ */
+typedef struct {
+ /**
+ * @brief IN endpoint notification callback.
+ */
+ usbepcallback_t uepc_in_cb;
+ /**
+ * @brief OUT endpoint notification callback.
+ */
+ usbepcallback_t uepc_out_cb;
+ /**
+ * @brief IN endpoint maximum packet size.
+ */
+ uint16_t uepc_in_maxsize;
+ /**
+ * @brief OUT endpoint maximum packet size.
+ */
+ uint16_t uepc_out_maxsize;
+ /* End of the mandatory fields.*/
+ /**
+ * @brief EPxR register initialization value.
+ * @note Do not specify the EA field, leave it to zero.
+ */
+ uint16_t uepc_epr;
+ /**
+ * @brief Endpoint IN buffer address as offset in the PMA.
+ */
+ uint16_t uepc_inaddr;
+ /**
+ * @brief Endpoint OUT buffer address as offset in the PMA.
+ */
+ uint16_t uepc_outaddr;
+} USBEndpointConfig;
+
+/**
+ * @brief Type of an USB driver configuration structure.
+ */
+typedef struct {
+ /**
+ * @brief USB events callback.
+ * @details This callback is invoked when an USB driver event is registered.
+ */
+ usbeventcb_t uc_event_cb;
+ /**
+ * @brief Device GET_DESCRIPTOR request callback.
+ * @note This callback is mandatory and cannot be set to @p NULL.
+ */
+ usbgetdescriptor_t uc_get_descriptor_cb;
+ /**
+ * @brief Requests hook callback.
+ * @details This hook allows to be notified of standard requests or to
+ * handle non standard requests.
+ */
+ usbreqhandler_t uc_requests_hook_cb;
+ /**
+ * @brief Start Of Frame callback.
+ */
+ usbcallback_t uc_sof_cb;
+ /* End of the mandatory fields.*/
+} USBConfig;
+
+/**
+ * @brief Structure representing an USB driver.
+ */
+struct USBDriver {
+ /**
+ * @brief Driver state.
+ */
+ usbstate_t usb_state;
+ /**
+ * @brief Current configuration data.
+ */
+ const USBConfig *usb_config;
+ /**
+ * @brief Field available to user, it can be used to associate an
+ * application-defined handler to the USB driver.
+ */
+ void *usb_param;
+ /**
+ * @brief Active endpoints configurations.
+ */
+ const USBEndpointConfig *usb_epc[USB_MAX_ENDPOINTS + 1];
+ /**
+ * @brief Endpoint 0 state.
+ */
+ usbep0state_t usb_ep0state;
+ /**
+ * @brief Next position in the buffer to be transferred through endpoint 0.
+ */
+ uint8_t *usb_ep0next;
+ /**
+ * @brief Maximum number of bytes to be tranferred through endpoint 0.
+ */
+ size_t usb_ep0max;
+ /**
+ * @brief Number of bytes yet to be tranferred through endpoint 0.
+ */
+ size_t usb_ep0n;
+ /**
+ * @brief Size of the last packet transferred through endpoint 0.
+ */
+ size_t usb_ep0lastsize;
+ /**
+ * @brief Endpoint 0 end transaction callback.
+ */
+ usbcallback_t usb_ep0endcb;
+ /**
+ * @brief Setup packet buffer.
+ */
+ uint8_t usb_setup[8];
+ /**
+ * @brief Current USB device status.
+ */
+ uint16_t usb_status;
+ /**
+ * @brief Assigned USB address.
+ */
+ uint8_t usb_address;
+ /**
+ * @brief Current USB device configuration.
+ */
+ uint8_t usb_configuration;
+ /* End of the mandatory fields.*/
+};
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @brief Fetches a 16 bits word value from an USB message.
+ *
+ * @param[in] p pointer to the 16 bits word
+ *
+ * @notapi
+ */
+#define usb_lld_fetch_word(p) (*(uint16_t *)p)
+
+/**
+ * @brief Returns the current frame number.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @return The current frame number.
+ *
+ * @notapi
+ */
+#define usb_lld_get_frame_number(usbp) (STM32_USB->FNR & FNR_FN_MASK)
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#if STM32_USB_USE_USB1 && !defined(__DOXYGEN__)
+extern USBDriver USBD1;
+#endif
+
+#if !defined(__DOXYGEN__)
+extern const USBEndpointConfig usb_lld_ep0config;
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void usb_lld_init(void);
+ void usb_lld_start(USBDriver *usbp);
+ void usb_lld_stop(USBDriver *usbp);
+ void usb_lld_reset(USBDriver *usbp);
+ void usb_lld_set_address(USBDriver *usbp, uint8_t addr);
+ void usb_lld_enable_endpoint(USBDriver *usbp, usbep_t ep,
+ const USBEndpointConfig *epcp);
+ void usb_lld_disable_endpoints(USBDriver *usbp);
+ size_t usb_lld_get_readable(USBDriver *usbp, usbep_t ep);
+ size_t usb_lld_read(USBDriver *usbp, usbep_t ep,
+ uint8_t *buf, size_t n);
+ size_t usb_lld_write(USBDriver *usbp, usbep_t ep,
+ const uint8_t *buf, size_t n);
+ usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep);
+ usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep);
+ void usb_lld_stall_in(USBDriver *usbp, usbep_t ep);
+ void usb_lld_stall_out(USBDriver *usbp, usbep_t ep);
+ void usb_lld_clear_in(USBDriver *usbp, usbep_t ep);
+ void usb_lld_clear_out(USBDriver *usbp, usbep_t ep);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_USB */
+
+#endif /* _USB_LLD_H_ */
+
+/** @} */
diff --git a/os/hal/src/hal.c b/os/hal/src/hal.c
index 0913ad7a4..bfbaa3e84 100644
--- a/os/hal/src/hal.c
+++ b/os/hal/src/hal.c
@@ -81,11 +81,17 @@ void halInit(void) {
#if HAL_USE_SPI || defined(__DOXYGEN__)
spiInit();
#endif
+#if HAL_USE_UART || defined(__DOXYGEN__)
+ uartInit();
+#endif
+#if HAL_USE_USB || defined(__DOXYGEN__)
+ usbInit();
+#endif
#if HAL_USE_MMC_SPI || defined(__DOXYGEN__)
mmcInit();
#endif
-#if HAL_USE_UART || defined(__DOXYGEN__)
- uartInit();
+#if HAL_USE_SERIAL_USB || defined(__DOXYGEN__)
+ sduInit();
#endif
/* Board specific initialization.*/
boardInit();
diff --git a/os/hal/src/serial_usb.c b/os/hal/src/serial_usb.c
new file mode 100644
index 000000000..62a901162
--- /dev/null
+++ b/os/hal/src/serial_usb.c
@@ -0,0 +1,328 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file serial_usb.c
+ * @brief Serial over USB Driver code.
+ *
+ * @addtogroup SERIAL_USB
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#include "usb_cdc.h"
+
+#if HAL_USE_SERIAL_USB || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables. */
+/*===========================================================================*/
+
+/*
+ * Current Line Coding.
+ */
+static cdc_linecoding_t linecoding = {
+ {0x00, 0x96, 0x00, 0x00}, /* 38400. */
+ LC_STOP_1, LC_PARITY_NONE, 8
+};
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/*
+ * Interface implementation.
+ */
+
+static size_t writes(void *ip, const uint8_t *bp, size_t n) {
+
+ return chOQWriteTimeout(&((SerialDriver *)ip)->oqueue, bp,
+ n, TIME_INFINITE);
+}
+
+static size_t reads(void *ip, uint8_t *bp, size_t n) {
+
+ return chIQReadTimeout(&((SerialDriver *)ip)->iqueue, bp,
+ n, TIME_INFINITE);
+}
+
+static bool_t putwouldblock(void *ip) {
+
+ return chOQIsFullI(&((SerialDriver *)ip)->oqueue);
+}
+
+static bool_t getwouldblock(void *ip) {
+
+ return chIQIsEmptyI(&((SerialDriver *)ip)->iqueue);
+}
+
+static msg_t putt(void *ip, uint8_t b, systime_t timeout) {
+
+ return chOQPutTimeout(&((SerialDriver *)ip)->oqueue, b, timeout);
+}
+
+static msg_t gett(void *ip, systime_t timeout) {
+
+ return chIQGetTimeout(&((SerialDriver *)ip)->iqueue, timeout);
+}
+
+static size_t writet(void *ip, const uint8_t *bp, size_t n, systime_t time) {
+
+ return chOQWriteTimeout(&((SerialDriver *)ip)->oqueue, bp, n, time);
+}
+
+static size_t readt(void *ip, uint8_t *bp, size_t n, systime_t time) {
+
+ return chIQReadTimeout(&((SerialDriver *)ip)->iqueue, bp, n, time);
+}
+
+static ioflags_t getflags(void *ip) {
+ _ch_get_and_clear_flags_impl(ip);
+}
+
+static const struct SerialUSBDriverVMT vmt = {
+ writes, reads, putwouldblock, getwouldblock,
+ putt, gett, writet, readt,
+ getflags
+};
+
+/**
+ * @brief Notification of data removed from the input queue.
+ */
+static void inotify(GenericQueue *qp) {
+ SerialUSBDriver *sdup = (SerialUSBDriver *)qp->q_wrptr;
+ size_t n;
+
+ /* Writes to the input queue can only happen when the queue has been
+ emptied, then a whole packet is loaded in the queue.*/
+ if (chIQIsEmptyI(&sdup->iqueue)) {
+
+ n = usbReadI(sdup->config->usbp, sdup->config->data_available_ep,
+ sdup->iqueue.q_buffer, SERIAL_USB_BUFFERS_SIZE);
+ if (n > 0) {
+ sdup->iqueue.q_rdptr = sdup->iqueue.q_buffer;
+ chSemSetCounterI(&sdup->iqueue.q_sem, n);
+ }
+ }
+}
+
+/**
+ * @brief Notification of data inserted into the output queue.
+ */
+static void onotify(GenericQueue *qp) {
+ SerialUSBDriver *sdup = (SerialUSBDriver *)qp->q_rdptr;
+ size_t n;
+
+ /* If there is any data in the output queue then it is sent within a
+ single packet and the queue is emptied.*/
+ n = usbWriteI(sdup->config->usbp, sdup->config->data_request_ep,
+ sdup->oqueue.q_buffer, chOQGetFullI(&sdup->oqueue));
+ if (n > 0) {
+ sdup->oqueue.q_wrptr = sdup->oqueue.q_buffer;
+ chSemSetCounterI(&sdup->oqueue.q_sem, SERIAL_USB_BUFFERS_SIZE);
+ }
+}
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Serial Driver initialization.
+ * @note This function is implicitly invoked by @p halInit(), there is
+ * no need to explicitly initialize the driver.
+ *
+ * @init
+ */
+void sduInit(void) {
+}
+
+/**
+ * @brief Initializes a generic full duplex driver object.
+ * @details The HW dependent part of the initialization has to be performed
+ * outside, usually in the hardware initialization code.
+ *
+ * @param[out] sdup pointer to a @p SerialUSBDriver structure
+ * @param[in] inotify pointer to a callback function that is invoked when
+ * some data is read from the Queue. The value can be
+ * @p NULL.
+ * @param[in] onotify pointer to a callback function that is invoked when
+ * some data is written in the Queue. The value can be
+ * @p NULL.
+ *
+ * @init
+ */
+void sduObjectInit(SerialUSBDriver *sdup) {
+
+ sdup->vmt = &vmt;
+ chEvtInit(&sdup->event);
+ sdup->flags = IO_NO_ERROR;
+ sdup->state = SDU_STOP;
+ chIQInit(&sdup->iqueue, sdup->ib, SERIAL_USB_BUFFERS_SIZE, inotify);
+ chOQInit(&sdup->oqueue, sdup->ob, SERIAL_USB_BUFFERS_SIZE, onotify);
+ /* This is a dirty trick but those pointers are never used because queues
+ are accessed in block mode from the low level.*/
+ sdup->iqueue.q_wrptr = (uint8_t *)sdup;
+ sdup->oqueue.q_rdptr = (uint8_t *)sdup;
+}
+
+/**
+ * @brief Configures and starts the driver.
+ *
+ * @param[in] sdup pointer to a @p SerialUSBDriver object
+ * @param[in] config the serial over USB driver configuration
+ *
+ * @api
+ */
+void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config) {
+
+ chDbgCheck(sdup != NULL, "sduStart");
+
+ chSysLock();
+ chDbgAssert((sdup->state == SDU_STOP) || (sdup->state == SDU_READY),
+ "sduStart(), #1",
+ "invalid state");
+ sdup->config = config;
+ usbStart(config->usbp, &config->usb_config);
+ config->usbp->usb_param = sdup;
+ sdup->state = SDU_READY;
+ chSysUnlock();
+}
+
+/**
+ * @brief Stops the driver.
+ * @details Any thread waiting on the driver's queues will be awakened with
+ * the message @p Q_RESET.
+ *
+ * @param[in] sdup pointer to a @p SerialUSBDriver object
+ *
+ * @api
+ */
+void sduStop(SerialUSBDriver *sdup) {
+
+ chDbgCheck(sdup != NULL, "sdStop");
+
+ chSysLock();
+ chDbgAssert((sdup->state == SDU_STOP) || (sdup->state == SDU_READY),
+ "sduStop(), #1",
+ "invalid state");
+ usbStop(sdup->config->usbp);
+ sdup->state = SDU_STOP;
+ chSysUnlock();
+}
+
+/**
+ * @brief Default requests hook.
+ * @details Applications wanting to use the Serial over USB driver can use
+ * this function as requests hook in the USB configuration.
+ * The following requests are emulated:
+ * - CDC_GET_LINE_CODING.
+ * - CDC_SET_LINE_CODING.
+ * - CDC_SET_CONTROL_LINE_STATE.
+ * .
+ */
+bool_t sduRequestsHook(USBDriver *usbp) {
+
+ if ((usbp->usb_setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) {
+ switch (usbp->usb_setup[1]) {
+ case CDC_GET_LINE_CODING:
+ usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
+ return TRUE;
+ case CDC_SET_LINE_CODING:
+ usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
+ return TRUE;
+ case CDC_SET_CONTROL_LINE_STATE:
+ /* Nothing to do, there are no control lines.*/
+ usbSetupTransfer(usbp, NULL, 0, NULL);
+ return TRUE;
+ default:
+ return FALSE;
+ }
+ }
+ return FALSE;
+}
+
+/**
+ * @brief Default data request callback.
+ * @details The application must use this function as callback for the IN
+ * data endpoint.
+ */
+void sduDataRequest(USBDriver *usbp, usbep_t ep) {
+ SerialUSBDriver *sdup = usbp->usb_param;
+ size_t n;
+
+ chSysLockFromIsr();
+ /* If there is any data in the output queue then it is sent within a
+ single packet and the queue is emptied.*/
+ n = chOQGetFullI(&sdup->oqueue);
+ if (n > 0) {
+ n = usbWriteI(usbp, ep, sdup->oqueue.q_buffer, n);
+ if (n > 0) {
+ sdup->oqueue.q_wrptr = sdup->oqueue.q_buffer;
+ chSemSetCounterI(&sdup->oqueue.q_sem, SERIAL_USB_BUFFERS_SIZE);
+ chIOAddFlagsI(sdup, IO_OUTPUT_EMPTY);
+ }
+ }
+ chSysUnlockFromIsr();
+}
+
+/**
+ * @brief Default data available callback.
+ * @details The application must use this function as callback for the OUT
+ * data endpoint.
+ */
+void sduDataAvailable(USBDriver *usbp, usbep_t ep) {
+ SerialUSBDriver *sdup = usbp->usb_param;
+
+ chSysLockFromIsr();
+ /* Writes to the input queue can only happen when the queue has been
+ emptied, then a whole packet is loaded in the queue.*/
+ if (chIQIsEmptyI(&sdup->iqueue)) {
+ size_t n;
+
+ n = usbReadI(usbp, ep, sdup->iqueue.q_buffer, SERIAL_USB_BUFFERS_SIZE);
+ if (n > 0) {
+ sdup->iqueue.q_rdptr = sdup->iqueue.q_buffer;
+ chSemSetCounterI(&sdup->iqueue.q_sem, n);
+ chIOAddFlagsI(sdup, IO_INPUT_AVAILABLE);
+ }
+ }
+ chSysUnlockFromIsr();
+}
+
+/**
+ * @brief Default data received callback.
+ * @details The application must use this function as callback for the IN
+ * interrupt endpoint.
+ */
+void sduInterruptRequest(USBDriver *usbp, usbep_t ep) {
+
+ (void)usbp;
+ (void)ep;
+}
+
+#endif /* HAL_USE_SERIAL */
+
+/** @} */
diff --git a/os/hal/src/usb.c b/os/hal/src/usb.c
new file mode 100644
index 000000000..c89d5a8e9
--- /dev/null
+++ b/os/hal/src/usb.c
@@ -0,0 +1,541 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file usb.c
+ * @brief USB Driver code.
+ *
+ * @addtogroup USB
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+#include "usb.h"
+
+#if HAL_USE_USB || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables. */
+/*===========================================================================*/
+
+static const uint8_t zero_status[] = {0x00, 0x00};
+static const uint8_t active_status[] ={0x00, 0x00};
+static const uint8_t halted_status[] = {0x01, 0x00};
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/**
+ * @brief SET ADDRESS transaction callback.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ */
+void set_address(USBDriver *usbp) {
+
+ usbp->usb_address = usbp->usb_setup[2];
+ usb_lld_set_address(usbp, usbp->usb_address);
+ if (usbp->usb_config->uc_event_cb)
+ usbp->usb_config->uc_event_cb(usbp, USB_EVENT_ADDRESS);
+ usbp->usb_state = USB_SELECTED;
+}
+
+/**
+ * @brief Starts a receive phase on the endpoint zero.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ */
+static void start_rx_ep0(USBDriver *usbp) {
+
+ if (usbp->usb_ep0n > 0) {
+ /* The received data cannot exceed the available amount.*/
+ if (usbp->usb_ep0n > usbp->usb_ep0max)
+ usbp->usb_ep0n = usbp->usb_ep0max;
+
+ /* Determines the maximum amount that can be received using a
+ single packet.*/
+ if (usbp->usb_ep0n > usb_lld_ep0config.uepc_out_maxsize)
+ usbp->usb_ep0lastsize = usb_lld_ep0config.uepc_out_maxsize;
+ else
+ usbp->usb_ep0lastsize = usbp->usb_ep0n;
+ usbp->usb_ep0state = USB_EP0_RX;
+ }
+ else {
+ /* Sending zero sized status packet.*/
+ usb_lld_write(usbp, 0, NULL, 0);
+ usbp->usb_ep0state = USB_EP0_SENDING_STS;
+ }
+}
+
+/**
+ * @brief Starts a transmission phase on the endpoint zero.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ */
+static void start_tx_ep0(USBDriver *usbp) {
+
+ if (usbp->usb_ep0n > 0) {
+ /* The transmitted data cannot exceed the requested amount.*/
+ if (usbp->usb_ep0n > usbp->usb_ep0max)
+ usbp->usb_ep0n = usbp->usb_ep0max;
+
+ /* Determines the maximum amount that can be transmitted using a
+ single packet.*/
+ if (usbp->usb_ep0n > usb_lld_ep0config.uepc_in_maxsize)
+ usbp->usb_ep0lastsize = usb_lld_ep0config.uepc_in_maxsize;
+ else
+ usbp->usb_ep0lastsize = usbp->usb_ep0n;
+
+ /* Starts transmission.*/
+ usb_lld_write(usbp, 0, usbp->usb_ep0next, usbp->usb_ep0lastsize);
+ usbp->usb_ep0state = USB_EP0_TX;
+ }
+ else
+ usbp->usb_ep0state = USB_EP0_WAITING_STS;
+}
+
+/**
+ * @brief Standard requests handler.
+ * @details This is the standard requests default handler, most standard
+ * requests are handled here, the user can override the standard
+ * handling using the @p uc_requests_hook_cb hook in the
+ * @p USBConfig structure.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @return The request handling exit code.
+ * @retval FALSE Request not recognized by the handler or error.
+ * @retval TRUE Request handled.
+ */
+static bool_t default_handler(USBDriver *usbp) {
+ const USBDescriptor *dp;
+
+ /* Decoding the request.*/
+ switch (((usbp->usb_setup[0] & (USB_RTYPE_RECIPIENT_MASK |
+ USB_RTYPE_TYPE_MASK)) |
+ (usbp->usb_setup[1] << 8))) {
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_STATUS << 8):
+ /* Just returns the current status word.*/
+ usbSetupTransfer(usbp, (uint8_t *)&usbp->usb_status, 2, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_CLEAR_FEATURE << 8):
+ /* Only the DEVICE_REMOTE_WAKEUP is handled here, any other feature
+ number is handled as an error.*/
+ if (usbp->usb_setup[2] == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
+ usbp->usb_status &= ~2;
+ usbSetupTransfer(usbp, NULL, 0, NULL);
+ return TRUE;
+ }
+ return FALSE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_FEATURE << 8):
+ /* Only the DEVICE_REMOTE_WAKEUP is handled here, any other feature
+ number is handled as an error.*/
+ if (usbp->usb_setup[2] == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
+ usbp->usb_status |= 2;
+ usbSetupTransfer(usbp, NULL, 0, NULL);
+ return TRUE;
+ }
+ return FALSE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_ADDRESS << 8):
+ /* The handling is posponed to after the status phase in order to allow
+ the proper completion of the transaction.*/
+ usbSetupTransfer(usbp, NULL, 0, set_address);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_DESCRIPTOR << 8):
+ /* Handling descriptor requests from the host.*/
+ dp = usbp->usb_config->uc_get_descriptor_cb(
+ usbp, usbp->usb_setup[3], usbp->usb_setup[2],
+ usb_lld_fetch_word(&usbp->usb_setup[4]));
+ if (dp == NULL)
+ return FALSE;
+ usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_GET_CONFIGURATION << 8):
+ /* Returning the last selected configuration.*/
+ usbSetupTransfer(usbp, &usbp->usb_configuration, 1, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_CONFIGURATION << 8):
+ /* Handling configuration selection from the host.*/
+ usbp->usb_configuration = usbp->usb_setup[2];
+ if (usbp->usb_configuration == 0)
+ usbp->usb_state = USB_SELECTED;
+ else
+ usbp->usb_state = USB_ACTIVE;
+ if (usbp->usb_config->uc_event_cb)
+ usbp->usb_config->uc_event_cb(usbp, USB_EVENT_CONFIGURED);
+ usbSetupTransfer(usbp, NULL, 0, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_GET_STATUS << 8):
+ case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_SYNCH_FRAME << 8):
+ /* Just sending two zero bytes, the application can change the behavior
+ using a hook..*/
+ usbSetupTransfer(usbp, (uint8_t *)zero_status, 2, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_GET_STATUS << 8):
+ /* Sending the EP status.*/
+ if (usbp->usb_setup[4] & 0x80) {
+ switch (usb_lld_get_status_in(usbp, usbp->usb_setup[4] & 0x0F)) {
+ case EP_STATUS_STALLED:
+ usbSetupTransfer(usbp, (uint8_t *)halted_status, 2, NULL);
+ return TRUE;
+ case EP_STATUS_ACTIVE:
+ usbSetupTransfer(usbp, (uint8_t *)active_status, 2, NULL);
+ return TRUE;
+ default:
+ return FALSE;
+ }
+ }
+ else {
+ switch (usb_lld_get_status_out(usbp, usbp->usb_setup[4] & 0x0F)) {
+ case EP_STATUS_STALLED:
+ usbSetupTransfer(usbp, (uint8_t *)halted_status, 2, NULL);
+ return TRUE;
+ case EP_STATUS_ACTIVE:
+ usbSetupTransfer(usbp, (uint8_t *)active_status, 2, NULL);
+ return TRUE;
+ default:
+ return FALSE;
+ }
+ }
+ case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_CLEAR_FEATURE << 8):
+ /* Only ENDPOINT_HALT is handled as feature.*/
+ if (usbp->usb_setup[2] != USB_FEATURE_ENDPOINT_HALT)
+ return FALSE;
+ /* Clearing the EP status, not valid for EP0, it is ignored in that case.*/
+ if ((usbp->usb_setup[4] & 0x0F) > 0) {
+ if (usbp->usb_setup[4] & 0x80)
+ usb_lld_clear_in(usbp, usbp->usb_setup[4] & 0x0F);
+ else
+ usb_lld_clear_out(usbp, usbp->usb_setup[4] & 0x0F);
+ }
+ usbSetupTransfer(usbp, NULL, 0, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_ENDPOINT | (USB_REQ_SET_FEATURE << 8):
+ /* Only ENDPOINT_HALT is handled as feature.*/
+ if (usbp->usb_setup[2] != USB_FEATURE_ENDPOINT_HALT)
+ return FALSE;
+ /* Stalling the EP, not valid for EP0, it is ignored in that case.*/
+ if ((usbp->usb_setup[4] & 0x0F) > 0) {
+ if (usbp->usb_setup[4] & 0x80)
+ usb_lld_stall_in(usbp, usbp->usb_setup[4] & 0x0F);
+ else
+ usb_lld_stall_out(usbp, usbp->usb_setup[4] & 0x0F);
+ }
+ usbSetupTransfer(usbp, NULL, 0, NULL);
+ return TRUE;
+ case USB_RTYPE_RECIPIENT_DEVICE | (USB_REQ_SET_DESCRIPTOR << 8):
+ case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_CLEAR_FEATURE << 8):
+ case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_SET_FEATURE << 8):
+ case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_GET_INTERFACE << 8):
+ case USB_RTYPE_RECIPIENT_INTERFACE | (USB_REQ_SET_INTERFACE << 8):
+ /* All the above requests are not handled here, if you need them then
+ use the hook mechanism and provide handling.*/
+ default:
+ return FALSE;
+ }
+}
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief USB Driver initialization.
+ * @note This function is implicitly invoked by @p halInit(), there is
+ * no need to explicitly initialize the driver.
+ *
+ * @init
+ */
+void usbInit(void) {
+
+ usb_lld_init();
+}
+
+/**
+ * @brief Initializes the standard part of a @p USBDriver structure.
+ *
+ * @param[out] usbp pointer to the @p USBDriver object
+ *
+ * @init
+ */
+void usbObjectInit(USBDriver *usbp) {
+
+ usbp->usb_state = USB_STOP;
+ usbp->usb_config = NULL;
+ usbp->usb_param = NULL;
+}
+
+/**
+ * @brief Configures and activates the USB peripheral.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] config pointer to the @p USBConfig object
+ *
+ * @api
+ */
+void usbStart(USBDriver *usbp, const USBConfig *config) {
+
+ chDbgCheck((usbp != NULL) && (config != NULL), "usbStart");
+
+ chSysLock();
+ chDbgAssert((usbp->usb_state == USB_STOP) || (usbp->usb_state == USB_READY),
+ "usbStart(), #1",
+ "invalid state");
+ usbp->usb_config = config;
+ usb_lld_start(usbp);
+ usbp->usb_state = USB_READY;
+ chSysUnlock();
+}
+
+/**
+ * @brief Deactivates the USB peripheral.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @api
+ */
+void usbStop(USBDriver *usbp) {
+
+ chDbgCheck(usbp != NULL, "usbStop");
+
+ chSysLock();
+ chDbgAssert((usbp->usb_state == USB_STOP) || (usbp->usb_state == USB_READY),
+ "usbStop(), #1",
+ "invalid state");
+ usb_lld_stop(usbp);
+ usbp->usb_state = USB_STOP;
+ chSysUnlock();
+}
+
+/**
+ * @brief Enables an endpoint.
+ * @details This function enables an endpoint, both IN and/or OUT directions
+ * depending on the configuration structure.
+ * @note This function must be invoked in response of a SET_CONFIGURATION
+ * or SET_INTERFACE message.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number
+ * @param[in] epcp the endpoint configuration
+ *
+ * @iclass
+ */
+void usbEnableEndpointI(USBDriver *usbp, usbep_t ep,
+ const USBEndpointConfig *epcp) {
+
+ chDbgAssert(usbp->usb_state == USB_ACTIVE,
+ "usbEnableEndpointI(), #1", "invalid state");
+ chDbgAssert(usbp->usb_epc[ep] != NULL,
+ "usbEnableEndpointI(), #2", "already enabled");
+
+ /* Logically enabling the endpoint in the USBDriver structure.*/
+ usbp->usb_epc[ep] = epcp;
+
+ /* Low level endpoint activation.*/
+ usb_lld_enable_endpoint(usbp, ep, epcp);
+}
+
+/**
+ * @brief Disables all the active endpoints.
+ * @details This function disables all the active endpoints except the
+ * endpoint zero.
+ * @note This function must be invoked in response of a SET_CONFIGURATION
+ * message with configuration number zero.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @iclass
+ */
+void usbDisableEndpointsI(USBDriver *usbp) {
+ unsigned i;
+
+ chDbgAssert(usbp->usb_state == USB_SELECTED,
+ "usbDisableEndpointsI(), #1", "invalid state");
+
+ for (i = 1; i <= USB_MAX_ENDPOINTS; i++)
+ usbp->usb_epc[i] = NULL;
+
+ /* Low level endpoints deactivation.*/
+ usb_lld_disable_endpoints(usbp);
+}
+
+/**
+ * @brief USB reset routine.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ *
+ * @notapi
+ */
+void _usb_reset(USBDriver *usbp) {
+ unsigned i;
+
+ usbp->usb_state = USB_READY;
+ usbp->usb_status = 0;
+ usbp->usb_address = 0;
+ usbp->usb_configuration = 0;
+
+ /* Invalidates all endpoints into the USBDriver structure.*/
+ for (i = 0; i <= USB_MAX_ENDPOINTS; i++)
+ usbp->usb_epc[i] = NULL;
+
+ /* EP0 state machine initialization.*/
+ usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
+
+ /* Low level reset.*/
+ usb_lld_reset(usbp);
+
+ /* Low level endpoint zero activation.*/
+ usbp->usb_epc[0] = &usb_lld_ep0config;
+ usb_lld_enable_endpoint(usbp, 0, &usb_lld_ep0config);
+}
+
+/**
+ * @brief Default EP0 IN callback.
+ * @details This function is used by the low level driver as default handler
+ * for EP0 IN events.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number, always zero
+ *
+ * @notapi
+ */
+void _usb_ep0in(USBDriver *usbp, usbep_t ep) {
+
+ (void)ep;
+ switch (usbp->usb_ep0state) {
+ case USB_EP0_TX:
+ usbp->usb_ep0next += usbp->usb_ep0lastsize;
+ usbp->usb_ep0max -= usbp->usb_ep0lastsize;
+ usbp->usb_ep0n -= usbp->usb_ep0lastsize;
+
+ /* The final condition is when the requested size has been transmitted or
+ when a packet has been sent with size less than the maximum packet
+ size.*/
+ if ((usbp->usb_ep0max == 0) ||
+ (usbp->usb_ep0lastsize < usb_lld_ep0config.uepc_in_maxsize))
+ usbp->usb_ep0state = USB_EP0_WAITING_STS;
+ else {
+ usbp->usb_ep0lastsize = usbp->usb_ep0n > usb_lld_ep0config.uepc_in_maxsize ?
+ usb_lld_ep0config.uepc_in_maxsize : usbp->usb_ep0n;
+ usb_lld_write(usbp, 0, usbp->usb_ep0next, usbp->usb_ep0lastsize);
+ }
+ return;
+ case USB_EP0_SENDING_STS:
+ if (usbp->usb_ep0endcb)
+ usbp->usb_ep0endcb(usbp);
+
+ usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
+ return;
+ default:
+ ;
+ }
+ /* Error response.*/
+ usb_lld_stall_in(usbp, 0);
+ usb_lld_stall_out(usbp, 0);
+ if (usbp->usb_config->uc_event_cb)
+ usbp->usb_config->uc_event_cb(usbp, USB_EVENT_STALLED);
+ usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
+}
+
+/**
+ * @brief Default EP0 OUT callback.
+ * @details This function is used by the low level driver as default handler
+ * for EP0 OUT events.
+ *
+ * @param[in] usbp pointer to the @p USBDriver object
+ * @param[in] ep endpoint number, always zero
+ *
+ * @notapi
+ */
+void _usb_ep0out(USBDriver *usbp, usbep_t ep) {
+ size_t n, size;
+ uint8_t buf[1];
+
+ (void)ep;
+ switch (usbp->usb_ep0state) {
+ case USB_EP0_WAITING_SETUP:
+ /* SETUP packet handling.*/
+ n = usb_lld_read(usbp, 0, usbp->usb_setup, 8);
+ if (n != 8)
+ break;
+
+ /* First verify if the application has an handler installed for this
+ request.*/
+ if (!(usbp->usb_config->uc_requests_hook_cb) ||
+ !(usbp->usb_config->uc_requests_hook_cb(usbp))) {
+ /* Invoking the default handler, if this fails then stalls the
+ endpoint zero as error.*/
+ if (((usbp->usb_setup[0] & USB_RTYPE_TYPE_MASK) != USB_RTYPE_TYPE_STD) ||
+ !default_handler(usbp))
+ break;
+ }
+
+ /* Transfer preparation. The request handler must have populated
+ correctly the fields usb_ep0next, usb_ep0n and usb_ep0endcb using
+ the macro usbSetupTransfer().*/
+ usbp->usb_ep0max = usb_lld_fetch_word(&usbp->usb_setup[6]);
+ if ((usbp->usb_setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST)
+ start_tx_ep0(usbp);
+ else
+ start_rx_ep0(usbp);
+ return;
+ case USB_EP0_RX:
+ /* Check for buffer overflow.*/
+ n = size = usb_lld_get_readable(usbp, 0);
+ if (n > usbp->usb_ep0n)
+ n = usbp->usb_ep0n;
+ /* Fetching received data packet.*/
+ n = usb_lld_read(usbp, 0, usbp->usb_ep0next, n);
+ if (n > usbp->usb_ep0max)
+ break;
+ usbp->usb_ep0max -= size;
+ usbp->usb_ep0n -= n;
+ usbp->usb_ep0next += n;
+ if (usbp->usb_ep0max == 0) {
+ usb_lld_write(usbp, 0, NULL, 0);
+ usbp->usb_ep0state = USB_EP0_SENDING_STS;
+ }
+ return;
+ case USB_EP0_WAITING_STS:
+ /* STATUS received packet handling, it must be zero sized.*/
+ n = usb_lld_read(usbp, 0, buf, 1);
+ if (n != 0)
+ break;
+ if (usbp->usb_ep0endcb)
+ usbp->usb_ep0endcb(usbp);
+ usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
+ return;
+ default:
+ ;
+ }
+ /* Error response.*/
+ usb_lld_stall_in(usbp, 0);
+ usb_lld_stall_out(usbp, 0);
+ if (usbp->usb_config->uc_event_cb)
+ usbp->usb_config->uc_event_cb(usbp, USB_EVENT_STALLED);
+ usbp->usb_ep0state = USB_EP0_WAITING_SETUP;
+}
+
+#endif /* HAL_USE_USB */
+
+/** @} */
diff --git a/readme.txt b/readme.txt
index 1be5ae77d..db6f8a7d4 100644
--- a/readme.txt
+++ b/readme.txt
@@ -69,6 +69,13 @@
*****************************************************************************
*** 2.3.0 ***
+- NEW: Added experimental USB driver, it will certainly change in next
+ releases.
+- NEW: Added experimental "serial over USB" driver, it implements a
+ Communication Device Class exposing it as a normal serial driver to
+ applications, it will certainly change in next
+ releases.
+- NEW: Added USB CDC loopback test application.
- NEW: Implemented new event IO_TRANSMISSION_END in the generic serial
driver. This event marks the physical transmission end of a data stream.
- NEW: Implemented the new IO_TRANSMISSION_END event in the STM32 serial