/** ****************************************************************************** * @file : usbd_conf.c * @brief : This file implements the board support package for the USB device library ****************************************************************************** * * COPYRIGHT(c) 2015 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ #include "os_io_seproxyhal.h" #include "os_helpers.h" /* Includes ------------------------------------------------------------------*/ #include "usbd_def.h" #include "usbd_core.h" /******************************************************************************* LL Driver Interface (USB Device Library --> PCD) *******************************************************************************/ unsigned int ep_in_stall; unsigned int ep_out_stall; /** * @brief Initializes the Low Level portion of the Device driver. * @param pdev: Device handle * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) { UNUSED(pdev); ep_in_stall = 0; ep_out_stall = 0; return USBD_OK; } /** * @brief De-Initializes the Low Level portion of the Device driver. * @param pdev: Device handle * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev) { uint8_t buffer[4]; UNUSED(pdev); // usb off buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 1; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_DISCONNECT; io_seproxyhal_spi_send(buffer, 4); return USBD_OK; } /** * @brief Starts the Low Level portion of the Device driver. * @param pdev: Device handle * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) { uint8_t buffer[5]; UNUSED(pdev); // reset address buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 2; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_ADDR; buffer[4] = 0; io_seproxyhal_spi_send(buffer, 5); // start usb operation buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 1; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_CONNECT; io_seproxyhal_spi_send(buffer, 4); return USBD_OK; } /** * @brief Stops the Low Level portion of the Device driver. * @param pdev: Device handle * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev) { UNUSED(pdev); uint8_t buffer[4]; buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 1; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_DISCONNECT; io_seproxyhal_spi_send(buffer, 4); return USBD_OK; } /** * @brief Opens an endpoint of the Low Level Driver. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @param ep_type: Endpoint Type * @param ep_mps: Endpoint Max Packet Size * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps) { uint8_t buffer[8]; UNUSED(pdev); ep_in_stall = 0; ep_out_stall = 0; buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 5; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_ENDPOINTS; buffer[4] = 1; buffer[5] = ep_addr; buffer[6] = 0; switch(ep_type) { case USBD_EP_TYPE_CTRL: buffer[6] = SEPROXYHAL_TAG_USB_CONFIG_TYPE_CONTROL; break; case USBD_EP_TYPE_ISOC: buffer[6] = SEPROXYHAL_TAG_USB_CONFIG_TYPE_ISOCHRONOUS; break; case USBD_EP_TYPE_BULK: buffer[6] = SEPROXYHAL_TAG_USB_CONFIG_TYPE_BULK; break; case USBD_EP_TYPE_INTR: buffer[6] = SEPROXYHAL_TAG_USB_CONFIG_TYPE_INTERRUPT; break; } buffer[7] = ep_mps; io_seproxyhal_spi_send(buffer, 8); return USBD_OK; } /** * @brief Closes an endpoint of the Low Level Driver. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) { UNUSED(pdev); uint8_t buffer[8]; buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 5; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_ENDPOINTS; buffer[4] = 1; buffer[5] = ep_addr; buffer[6] = SEPROXYHAL_TAG_USB_CONFIG_TYPE_DISABLED; buffer[7] = 0; io_seproxyhal_spi_send(buffer, 8); return USBD_OK; } /** * @brief Flushes an endpoint of the Low Level Driver. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) { UNUSED(pdev); UNUSED(ep_addr); //HAL_PCD_EP_Flush(pdev->pData, ep_addr); return USBD_OK; } /** * @brief Sets a Stall condition on an endpoint of the Low Level Driver. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) { UNUSED(pdev); uint8_t buffer[6]; buffer[0] = SEPROXYHAL_TAG_USB_EP_PREPARE; buffer[1] = 0; buffer[2] = 3; buffer[3] = ep_addr; buffer[4] = SEPROXYHAL_TAG_USB_EP_PREPARE_DIR_STALL; buffer[5] = 0; io_seproxyhal_spi_send(buffer, 6); if (ep_addr & 0x80) { ep_in_stall |= (1<<(ep_addr&0x7F)); } else { ep_out_stall |= (1<<(ep_addr&0x7F)); } return USBD_OK; } /** * @brief Clears a Stall condition on an endpoint of the Low Level Driver. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) { UNUSED(pdev); uint8_t buffer[6]; buffer[0] = SEPROXYHAL_TAG_USB_EP_PREPARE; buffer[1] = 0; buffer[2] = 3; buffer[3] = ep_addr; buffer[4] = SEPROXYHAL_TAG_USB_EP_PREPARE_DIR_UNSTALL; buffer[5] = 0; io_seproxyhal_spi_send(buffer, 6); if (ep_addr & 0x80) { ep_in_stall &= ~(1<<(ep_addr&0x7F)); } else { ep_out_stall &= ~(1<<(ep_addr&0x7F)); } return USBD_OK; } /** * @brief Returns Stall condition. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @retval Stall (1: Yes, 0: No) */ uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) { UNUSED(pdev); if((ep_addr & 0x80) == 0x80) { return ep_in_stall & (1<<(ep_addr&0x7F)); } else { return ep_out_stall & (1<<(ep_addr&0x7F)); } } /** * @brief Assigns a USB address to the device. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr) { UNUSED(pdev); uint8_t buffer[5]; buffer[0] = SEPROXYHAL_TAG_USB_CONFIG; buffer[1] = 0; buffer[2] = 2; buffer[3] = SEPROXYHAL_TAG_USB_CONFIG_ADDR; buffer[4] = dev_addr; io_seproxyhal_spi_send(buffer, 5); return USBD_OK; } /** * @brief Transmits data over an endpoint. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @param pbuf: Pointer to data to be sent * @param size: Data size * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size) { UNUSED(pdev); uint8_t buffer[6]; buffer[0] = SEPROXYHAL_TAG_USB_EP_PREPARE; buffer[1] = (3+size)>>8; buffer[2] = (3+size); buffer[3] = ep_addr; buffer[4] = SEPROXYHAL_TAG_USB_EP_PREPARE_DIR_IN; buffer[5] = size; io_seproxyhal_spi_send(buffer, 6); io_seproxyhal_spi_send(pbuf, size); return USBD_OK; } /** * @brief Prepares an endpoint for reception. * @param pdev: Device handle * @param ep_addr: Endpoint Number * @param pbuf: Pointer to data to be received * @param size: Data size * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint16_t size) { UNUSED(pdev); uint8_t buffer[6]; buffer[0] = SEPROXYHAL_TAG_USB_EP_PREPARE; buffer[1] = (3/*+size*/)>>8; buffer[2] = (3/*+size*/); buffer[3] = ep_addr; buffer[4] = SEPROXYHAL_TAG_USB_EP_PREPARE_DIR_OUT; buffer[5] = size; // expected size, not transmitted here ! io_seproxyhal_spi_send(buffer, 6); return USBD_OK; } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/