aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/include
diff options
context:
space:
mode:
Diffstat (limited to 'os/hal/include')
-rw-r--r--os/hal/include/hal.h2
-rw-r--r--os/hal/include/serial.h29
-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
5 files changed, 591 insertions, 20 deletions
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.h b/os/hal/include/serial.h
index d67c3079a..57240e78b 100644
--- a/os/hal/include/serial.h
+++ b/os/hal/include/serial.h
@@ -35,15 +35,15 @@
/*===========================================================================*/
/** @brief Parity error happened.*/
-#define SD_PARITY_ERROR 16
+#define SD_PARITY_ERROR 32
/** @brief Framing error happened.*/
-#define SD_FRAMING_ERROR 32
+#define SD_FRAMING_ERROR 64
/** @brief Overflow happened.*/
-#define SD_OVERRUN_ERROR 64
+#define SD_OVERRUN_ERROR 128
/** @brief Noise on the line.*/
-#define SD_NOISE_ERROR 128
+#define SD_NOISE_ERROR 256
/** @brief Break detected.*/
-#define SD_BREAK_DETECTED 256
+#define SD_BREAK_DETECTED 512
/*===========================================================================*/
/* Driver pre-compile time settings. */
@@ -134,10 +134,11 @@ struct SerialDriver {
* be used to check different channels implementations.
*
* @see chIOPutWouldBlock()
+ * @deprecated
*
* @api
*/
-#define sdPutWouldBlock(sdp) chOQIsFull(&(sdp)->oqueue)
+#define sdPutWouldBlock(sdp) chOQIsFullI(&(sdp)->oqueue)
/**
* @brief Direct input check on a @p SerialDriver.
@@ -146,10 +147,11 @@ struct SerialDriver {
* be used to check different channels implementations.
*
* @see chIOGetWouldBlock()
+ * @deprecated
*
* @api
*/
-#define sdGetWouldBlock(sdp) chIQIsEmpty(&(sdp)->iqueue)
+#define sdGetWouldBlock(sdp) chIQIsEmptyI(&(sdp)->iqueue)
/**
* @brief Direct write to a @p SerialDriver.
@@ -279,19 +281,6 @@ struct SerialDriver {
#define sdAsynchronousRead(sdp, b, n) \
chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE)
-/**
- * @brief Returns the status change event source.
- * @details The status change event source is broadcasted when the channel
- * status is updated, the status flags can then be fetched and
- * cleared by using @p sdGetAndClearFlags().
- *
- * @param[in] ip pointer to a @p SerialDriver object
- * @return A pointer to an @p EventSource object.
- *
- * @api
- */
-#define sdGetStatusChangeEventSource(ip) (&((ip)->vmt->sevent))
-
/*===========================================================================*/
/* 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..9f0d95837
--- /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 usbInitEndpointI(USBDriver *usbp, usbep_t ep, USBEndpointState *epp,
+ 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_ */
+
+/** @} */