2014-11-01 17:02:44 +00:00
|
|
|
/*
|
2016-03-18 10:29:35 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
|
2014-11-01 17:02:44 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2016-04-02 07:59:24 +00:00
|
|
|
* @file hal_usb_lld.h
|
2014-11-02 15:24:30 +00:00
|
|
|
* @brief PLATFORM USB subsystem low level driver header.
|
2014-11-01 17:02:44 +00:00
|
|
|
*
|
|
|
|
* @addtogroup USB
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2016-04-02 07:59:24 +00:00
|
|
|
#ifndef HAL_USB_LLD_H
|
|
|
|
#define HAL_USB_LLD_H
|
2014-11-01 17:02:44 +00:00
|
|
|
|
2015-03-09 10:48:08 +00:00
|
|
|
#if (HAL_USE_USB == TRUE) || defined(__DOXYGEN__)
|
2014-11-01 17:02:44 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Maximum endpoint address.
|
|
|
|
*/
|
|
|
|
#define USB_MAX_ENDPOINTS 4
|
|
|
|
|
2014-11-02 15:24:30 +00:00
|
|
|
/**
|
|
|
|
* @brief Status stage handling method.
|
|
|
|
*/
|
|
|
|
#define USB_EP0_STATUS_STAGE USB_EP0_STATUS_STAGE_SW
|
|
|
|
|
2014-11-01 17:02:44 +00:00
|
|
|
/**
|
|
|
|
* @brief The address can be changed immediately upon packet reception.
|
|
|
|
*/
|
2014-11-02 15:24:30 +00:00
|
|
|
#define USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS
|
2014-11-01 17:02:44 +00:00
|
|
|
|
2015-03-08 21:19:58 +00:00
|
|
|
/**
|
|
|
|
* @brief Method for set address acknowledge.
|
|
|
|
*/
|
|
|
|
#define USB_SET_ADDRESS_ACK_HANDLING USB_SET_ADDRESS_ACK_SW
|
|
|
|
|
2014-11-01 17:02:44 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2014-12-11 14:45:16 +00:00
|
|
|
* @name PLATFORM configuration options
|
2014-11-01 17:02:44 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief USB driver enable switch.
|
|
|
|
* @details If set to @p TRUE the support for USB1 is included.
|
2015-03-09 10:48:08 +00:00
|
|
|
* @note The default is @p FALSE.
|
2014-11-01 17:02:44 +00:00
|
|
|
*/
|
|
|
|
#if !defined(PLATFORM_USB_USE_USB1) || defined(__DOXYGEN__)
|
2014-11-02 15:24:30 +00:00
|
|
|
#define PLATFORM_USB_USE_USB1 FALSE
|
2014-11-01 17:02:44 +00:00
|
|
|
#endif
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of an IN endpoint state structure.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* @brief Requested transmit transfer size.
|
|
|
|
*/
|
|
|
|
size_t txsize;
|
|
|
|
/**
|
|
|
|
* @brief Transmitted bytes so far.
|
|
|
|
*/
|
|
|
|
size_t txcnt;
|
2015-12-25 08:59:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Pointer to the transmission linear buffer.
|
|
|
|
*/
|
|
|
|
const uint8_t *txbuf;
|
2016-01-04 14:09:52 +00:00
|
|
|
#if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__)
|
|
|
|
/**
|
|
|
|
* @brief Waiting thread.
|
|
|
|
*/
|
|
|
|
thread_reference_t thread;
|
|
|
|
#endif
|
2014-11-02 15:24:30 +00:00
|
|
|
/* End of the mandatory fields.*/
|
2014-11-01 17:02:44 +00:00
|
|
|
} USBInEndpointState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of an OUT endpoint state structure.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* @brief Requested receive transfer size.
|
|
|
|
*/
|
|
|
|
size_t rxsize;
|
|
|
|
/**
|
|
|
|
* @brief Received bytes so far.
|
|
|
|
*/
|
|
|
|
size_t rxcnt;
|
2015-12-25 08:59:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Pointer to the receive linear buffer.
|
|
|
|
*/
|
|
|
|
uint8_t *rxbuf;
|
2016-01-04 14:09:52 +00:00
|
|
|
#if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__)
|
|
|
|
/**
|
|
|
|
* @brief Waiting thread.
|
|
|
|
*/
|
|
|
|
thread_reference_t thread;
|
|
|
|
#endif
|
2014-11-02 15:24:30 +00:00
|
|
|
/* End of the mandatory fields.*/
|
2014-11-01 17:02:44 +00:00
|
|
|
} USBOutEndpointState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of an USB endpoint configuration structure.
|
|
|
|
* @note Platform specific restrictions may apply to endpoints.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* @brief Type and mode of the endpoint.
|
|
|
|
*/
|
|
|
|
uint32_t ep_mode;
|
|
|
|
/**
|
|
|
|
* @brief Setup packet notification callback.
|
|
|
|
* @details This callback is invoked when a setup packet has been
|
|
|
|
* received.
|
|
|
|
* @post The application must immediately call @p usbReadPacket() in
|
|
|
|
* order to access the received packet.
|
|
|
|
* @note This field is only valid for @p USB_EP_MODE_TYPE_CTRL
|
|
|
|
* endpoints, it should be set to @p NULL for other endpoint
|
|
|
|
* types.
|
|
|
|
*/
|
|
|
|
usbepcallback_t setup_cb;
|
|
|
|
/**
|
|
|
|
* @brief IN endpoint notification callback.
|
|
|
|
* @details This field must be set to @p NULL if the IN endpoint is not
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
usbepcallback_t in_cb;
|
|
|
|
/**
|
|
|
|
* @brief OUT endpoint notification callback.
|
|
|
|
* @details This field must be set to @p NULL if the OUT endpoint is not
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
usbepcallback_t out_cb;
|
|
|
|
/**
|
|
|
|
* @brief IN endpoint maximum packet size.
|
|
|
|
* @details This field must be set to zero if the IN endpoint is not
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
uint16_t in_maxsize;
|
|
|
|
/**
|
|
|
|
* @brief OUT endpoint maximum packet size.
|
|
|
|
* @details This field must be set to zero if the OUT endpoint is not
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
uint16_t out_maxsize;
|
|
|
|
/**
|
|
|
|
* @brief @p USBEndpointState associated to the IN endpoint.
|
|
|
|
* @details This structure maintains the state of the IN endpoint.
|
|
|
|
*/
|
|
|
|
USBInEndpointState *in_state;
|
|
|
|
/**
|
|
|
|
* @brief @p USBEndpointState associated to the OUT endpoint.
|
|
|
|
* @details This structure maintains the state of the OUT endpoint.
|
|
|
|
*/
|
|
|
|
USBOutEndpointState *out_state;
|
|
|
|
/* End of the mandatory fields.*/
|
|
|
|
} 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 event_cb;
|
|
|
|
/**
|
|
|
|
* @brief Device GET_DESCRIPTOR request callback.
|
|
|
|
* @note This callback is mandatory and cannot be set to @p NULL.
|
|
|
|
*/
|
|
|
|
usbgetdescriptor_t 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 requests_hook_cb;
|
|
|
|
/**
|
|
|
|
* @brief Start Of Frame callback.
|
|
|
|
*/
|
|
|
|
usbcallback_t sof_cb;
|
|
|
|
/* End of the mandatory fields.*/
|
|
|
|
} USBConfig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Structure representing an USB driver.
|
|
|
|
*/
|
|
|
|
struct USBDriver {
|
|
|
|
/**
|
|
|
|
* @brief Driver state.
|
|
|
|
*/
|
|
|
|
usbstate_t state;
|
|
|
|
/**
|
|
|
|
* @brief Current configuration data.
|
|
|
|
*/
|
|
|
|
const USBConfig *config;
|
|
|
|
/**
|
|
|
|
* @brief Bit map of the transmitting IN endpoints.
|
|
|
|
*/
|
|
|
|
uint16_t transmitting;
|
|
|
|
/**
|
|
|
|
* @brief Bit map of the receiving OUT endpoints.
|
|
|
|
*/
|
|
|
|
uint16_t receiving;
|
|
|
|
/**
|
|
|
|
* @brief Active endpoints configurations.
|
|
|
|
*/
|
|
|
|
const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1];
|
|
|
|
/**
|
|
|
|
* @brief Fields available to user, it can be used to associate an
|
|
|
|
* application-defined handler to an IN endpoint.
|
|
|
|
* @note The base index is one, the endpoint zero does not have a
|
|
|
|
* reserved element in this array.
|
|
|
|
*/
|
|
|
|
void *in_params[USB_MAX_ENDPOINTS];
|
|
|
|
/**
|
|
|
|
* @brief Fields available to user, it can be used to associate an
|
|
|
|
* application-defined handler to an OUT endpoint.
|
|
|
|
* @note The base index is one, the endpoint zero does not have a
|
|
|
|
* reserved element in this array.
|
|
|
|
*/
|
|
|
|
void *out_params[USB_MAX_ENDPOINTS];
|
|
|
|
/**
|
|
|
|
* @brief Endpoint 0 state.
|
|
|
|
*/
|
|
|
|
usbep0state_t ep0state;
|
|
|
|
/**
|
|
|
|
* @brief Next position in the buffer to be transferred through endpoint 0.
|
|
|
|
*/
|
|
|
|
uint8_t *ep0next;
|
|
|
|
/**
|
|
|
|
* @brief Number of bytes yet to be transferred through endpoint 0.
|
|
|
|
*/
|
|
|
|
size_t ep0n;
|
|
|
|
/**
|
|
|
|
* @brief Endpoint 0 end transaction callback.
|
|
|
|
*/
|
|
|
|
usbcallback_t ep0endcb;
|
|
|
|
/**
|
|
|
|
* @brief Setup packet buffer.
|
|
|
|
*/
|
|
|
|
uint8_t setup[8];
|
|
|
|
/**
|
|
|
|
* @brief Current USB device status.
|
|
|
|
*/
|
|
|
|
uint16_t status;
|
|
|
|
/**
|
|
|
|
* @brief Assigned USB address.
|
|
|
|
*/
|
|
|
|
uint8_t address;
|
|
|
|
/**
|
|
|
|
* @brief Current USB device configuration.
|
|
|
|
*/
|
|
|
|
uint8_t configuration;
|
|
|
|
#if defined(USB_DRIVER_EXT_FIELDS)
|
|
|
|
USB_DRIVER_EXT_FIELDS
|
|
|
|
#endif
|
|
|
|
/* End of the mandatory fields.*/
|
|
|
|
};
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2014-11-02 15:24:30 +00:00
|
|
|
/**
|
|
|
|
* @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) 0
|
|
|
|
|
2014-11-01 17:02:44 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the exact size of a receive transaction.
|
|
|
|
* @details The received size can be different from the size specified in
|
|
|
|
* @p usbStartReceiveI() because the last packet could have a size
|
|
|
|
* different from the expected one.
|
|
|
|
* @pre The OUT endpoint must have been configured in transaction mode
|
|
|
|
* in order to use this function.
|
|
|
|
*
|
|
|
|
* @param[in] usbp pointer to the @p USBDriver object
|
|
|
|
* @param[in] ep endpoint number
|
|
|
|
* @return Received data size.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define usb_lld_get_transaction_size(usbp, ep) \
|
|
|
|
((usbp)->epc[ep]->out_state->rxcnt)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Connects the USB device.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define usb_lld_connect_bus(usbp)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disconnect the USB device.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define usb_lld_disconnect_bus(usbp)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-03-09 10:48:08 +00:00
|
|
|
#if (PLATFORM_USB_USE_USB1 == TRUE) && !defined(__DOXYGEN__)
|
2014-11-01 17:02:44 +00:00
|
|
|
extern USBDriver USBD1;
|
|
|
|
#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);
|
|
|
|
void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_disable_endpoints(USBDriver *usbp);
|
|
|
|
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_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf);
|
|
|
|
void usb_lld_prepare_receive(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_start_out(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_start_in(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_stall_out(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_stall_in(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_clear_out(USBDriver *usbp, usbep_t ep);
|
|
|
|
void usb_lld_clear_in(USBDriver *usbp, usbep_t ep);
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-09 10:48:08 +00:00
|
|
|
#endif /* HAL_USE_USB == TRUE */
|
2014-11-01 17:02:44 +00:00
|
|
|
|
2016-04-02 07:59:24 +00:00
|
|
|
#endif /* HAL_USB_LLD_H */
|
2014-11-01 17:02:44 +00:00
|
|
|
|
|
|
|
/** @} */
|