2 * The Clear BSD License
3 * Copyright (c) 2015, Freescale Semiconductor, Inc.
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted (subject to the limitations in the disclaimer below) provided
9 * that the following conditions are met:
11 * o Redistributions of source code must retain the above copyright notice, this list
12 * of conditions and the following disclaimer.
14 * o Redistributions in binary form must reproduce the above copyright notice, this
15 * list of conditions and the following disclaimer in the documentation and/or
16 * other materials provided with the distribution.
18 * o Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef __USB_DEVICE_KHCI_H__
36 #define __USB_DEVICE_KHCI_H__
39 * @addtogroup usb_device_controller_khci_driver
43 /*******************************************************************************
45 ******************************************************************************/
47 /*! @brief The maximum value of ISO maximum packet size for FS in USB specification 2.0 */
48 #define USB_DEVICE_MAX_FS_ISO_MAX_PACKET_SIZE (1023U)
50 /*! @brief The maximum value of non-ISO maximum packet size for FS in USB specification 2.0 */
51 #define USB_DEVICE_MAX_FS_NONE_ISO_MAX_PACKET_SIZE (64U)
53 /*! @brief Set BDT buffer address */
54 #define USB_KHCI_BDT_SET_ADDRESS(bdt_base, ep, direction, odd, address) \
55 *((volatile uint32_t *)((bdt_base & 0xfffffe00U) | (((uint32_t)ep & 0x0fU) << 5U) | \
56 (((uint32_t)direction & 1U) << 4U) | (((uint32_t)odd & 1U) << 3U)) + \
59 /*! @brief Set BDT control fields*/
60 #define USB_KHCI_BDT_SET_CONTROL(bdt_base, ep, direction, odd, control) \
61 *(volatile uint32_t *)((bdt_base & 0xfffffe00U) | (((uint32_t)ep & 0x0fU) << 5U) | \
62 (((uint32_t)direction & 1U) << 4U) | (((uint32_t)odd & 1U) << 3U)) = control
64 /*! @brief Get BDT buffer address*/
65 #define USB_KHCI_BDT_GET_ADDRESS(bdt_base, ep, direction, odd) \
66 (*((volatile uint32_t *)((bdt_base & 0xfffffe00U) | (((uint32_t)ep & 0x0fU) << 5U) | \
67 (((uint32_t)direction & 1U) << 4U) | (((uint32_t)odd & 1U) << 3U)) + \
70 /*! @brief Get BDT control fields*/
71 #define USB_KHCI_BDT_GET_CONTROL(bdt_base, ep, direction, odd) \
72 (*(volatile uint32_t *)((bdt_base & 0xfffffe00U) | (((uint32_t)ep & 0x0fU) << 5U) | \
73 (((uint32_t)direction & 1U) << 4U) | (((uint32_t)odd & 1U) << 3U)))
75 /*! @brief Endpoint state structure */
76 typedef struct _usb_device_khci_endpoint_state_struct
78 uint8_t *transferBuffer; /*!< Address of buffer containing the data to be transmitted */
79 uint32_t transferLength; /*!< Length of data to transmit. */
80 uint32_t transferDone; /*!< The data length has been transferred*/
83 uint32_t state; /*!< The state of the endpoint */
86 uint32_t maxPacketSize : 10U; /*!< The maximum packet size of the endpoint */
87 uint32_t stalled : 1U; /*!< The endpoint is stalled or not */
88 uint32_t data0 : 1U; /*!< The data toggle of the transaction */
89 uint32_t bdtOdd : 1U; /*!< The BDT toggle of the endpoint */
90 uint32_t dmaAlign : 1U; /*!< Whether the transferBuffer is DMA aligned or not */
91 uint32_t transferring : 1U; /*!< The endpoint is transferring */
92 uint32_t zlt : 1U; /*!< zlt flag */
95 } usb_device_khci_endpoint_state_struct_t;
97 /*! @brief KHCI state structure */
98 typedef struct _usb_device_khci_state_struct
100 usb_device_struct_t *deviceHandle; /*!< Device handle used to identify the device object belongs to */
101 uint8_t *bdt; /*!< BDT buffer address */
102 USB_Type *registerBase; /*!< The base address of the register */
103 uint8_t setupPacketBuffer[USB_SETUP_PACKET_SIZE * 2]; /*!< The setup request buffer */
104 uint8_t *dmaAlignBuffer; /*!< This buffer is used to fix the transferBuffer or transferLength does
105 not align to 4-bytes when the function USB_DeviceKhciRecv is called.
106 The macro USB_DEVICE_CONFIG_KHCI_DMA_ALIGN is used to enable or disable this feature.
107 If the feature is enabled, when the transferBuffer or transferLength does not align to
109 the transferLength is not more than USB_DEVICE_CONFIG_KHCI_DMA_ALIGN_BUFFER_LENGTH, and
110 the flag isDmaAlignBufferInusing is zero, the dmaAlignBuffer is used to receive data
111 and the flag isDmaAlignBufferInusing is set to 1.
112 When the transfer is done, the received data, kept in dmaAlignBuffer, is copied
113 to the transferBuffer, and the flag isDmaAlignBufferInusing is cleared.
115 usb_device_khci_endpoint_state_struct_t
116 endpointState[USB_DEVICE_CONFIG_ENDPOINTS * 2]; /*!< Endpoint state structures */
117 uint8_t isDmaAlignBufferInusing; /*!< The dmaAlignBuffer is used or not */
118 uint8_t isResetting; /*!< Is doing device reset or not */
119 uint8_t controllerId; /*!< Controller ID */
120 uint8_t setupBufferIndex; /*!< A valid setup buffer flag */
121 #if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
124 } usb_device_khci_state_struct_t;
126 #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
127 (defined(FSL_FEATURE_SOC_USBDCD_COUNT) && (FSL_FEATURE_SOC_USBDCD_COUNT > 0U))
128 typedef struct _usb_device_dcd_state_struct
130 usb_device_struct_t *deviceHandle; /*!< Device handle used to identify the device object belongs to */
131 USBDCD_Type *dcdRegisterBase; /*!< The base address of the dcd module */
132 uint8_t controllerId; /*!< Controller ID */
133 } usb_device_dcd_state_struct_t;
136 #if defined(__cplusplus)
141 * @name USB device KHCI functions
145 /*******************************************************************************
147 ******************************************************************************/
150 * @brief Initializes the USB device KHCI instance.
152 * This function initializes the USB device KHCI module specified by the controllerId.
154 * @param[in] controllerId The controller ID of the USB IP. See the enumeration type usb_controller_index_t.
155 * @param[in] handle Pointer of the device handle used to identify the device object belongs to.
156 * @param[out] khciHandle An out parameter used to return the pointer of the device KHCI handle to the caller.
158 * @return A USB error code or kStatus_USB_Success.
160 usb_status_t USB_DeviceKhciInit(uint8_t controllerId,
161 usb_device_handle handle,
162 usb_device_controller_handle *khciHandle);
165 * @brief Deinitializes the USB device KHCI instance.
167 * This function deinitializes the USB device KHCI module.
169 * @param[in] khciHandle Pointer of the device KHCI handle.
171 * @return A USB error code or kStatus_USB_Success.
173 usb_status_t USB_DeviceKhciDeinit(usb_device_controller_handle khciHandle);
176 * @brief Sends data through a specified endpoint.
178 * This function sends data through a specified endpoint.
180 * @param[in] khciHandle Pointer of the device KHCI handle.
181 * @param[in] endpointAddress Endpoint index.
182 * @param[in] buffer The memory address to hold the data need to be sent.
183 * @param[in] length The data length need to be sent.
185 * @return A USB error code or kStatus_USB_Success.
187 * @note The return value indicates whether the sending request is successful or not. The transfer completion is
189 * corresponding callback function.
190 * Currently, only one transfer request can be supported for a specific endpoint.
191 * If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
192 * should implement a queue in the application level.
193 * The subsequent transfer can begin only when the previous transfer is done (a notification is obtained through the
197 usb_status_t USB_DeviceKhciSend(usb_device_controller_handle khciHandle,
198 uint8_t endpointAddress,
203 * @brief Receives data through a specified endpoint.
205 * This function receives data through a specified endpoint.
207 * @param[in] khciHandle Pointer of the device KHCI handle.
208 * @param[in] endpointAddress Endpoint index.
209 * @param[in] buffer The memory address to save the received data.
210 * @param[in] length The data length to be received.
212 * @return A USB error code or kStatus_USB_Success.
214 * @note The return value indicates whether the receiving request is successful or not. The transfer completion is
216 * corresponding callback function.
217 * Currently, only one transfer request can be supported for a specific endpoint.
218 * If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
219 * should implement a queue in the application level.
220 * The subsequent transfer can begin only when the previous transfer is done (a notification is obtained through the
224 usb_status_t USB_DeviceKhciRecv(usb_device_controller_handle khciHandle,
225 uint8_t endpointAddress,
230 * @brief Cancels the pending transfer in a specified endpoint.
232 * The function is used to cancel the pending transfer in a specified endpoint.
234 * @param[in] khciHandle Pointer of the device KHCI handle.
235 * @param[in] ep Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
237 * @return A USB error code or kStatus_USB_Success.
239 usb_status_t USB_DeviceKhciCancel(usb_device_controller_handle khciHandle, uint8_t ep);
242 * @brief Controls the status of the selected item.
244 * The function is used to control the status of the selected item.
246 * @param[in] khciHandle Pointer of the device KHCI handle.
247 * @param[in] type The selected item. See enumeration type usb_device_control_type_t.
248 * @param[in,out] param The parameter type is determined by the selected item.
250 * @return A USB error code or kStatus_USB_Success.
252 usb_status_t USB_DeviceKhciControl(usb_device_controller_handle khciHandle,
253 usb_device_control_type_t type,
258 #if defined(__cplusplus)
264 #endif /* __USB_DEVICE_KHCI_H__ */