From 03a69276e4c3db2b8713a355aaf6e04af28b5fbc Mon Sep 17 00:00:00 2001 From: uestczyh222 Date: Fri, 15 Dec 2017 17:46:52 +0800 Subject: [BSP][STM32F4xx-HAL][USB HOST]Add USB host drivers and rename device drivers --- bsp/stm32f4xx-HAL/Kconfig | 19 +++ bsp/stm32f4xx-HAL/drivers/SConscript | 5 +- bsp/stm32f4xx-HAL/drivers/drv_usb.c | 296 ----------------------------------- bsp/stm32f4xx-HAL/drivers/drv_usb.h | 20 --- bsp/stm32f4xx-HAL/drivers/drv_usbd.c | 296 +++++++++++++++++++++++++++++++++++ bsp/stm32f4xx-HAL/drivers/drv_usbd.h | 20 +++ bsp/stm32f4xx-HAL/drivers/drv_usbh.c | 279 +++++++++++++++++++++++++++++++++ bsp/stm32f4xx-HAL/drivers/drv_usbh.h | 20 +++ 8 files changed, 637 insertions(+), 318 deletions(-) delete mode 100644 bsp/stm32f4xx-HAL/drivers/drv_usb.c delete mode 100644 bsp/stm32f4xx-HAL/drivers/drv_usb.h create mode 100644 bsp/stm32f4xx-HAL/drivers/drv_usbd.c create mode 100644 bsp/stm32f4xx-HAL/drivers/drv_usbd.h create mode 100644 bsp/stm32f4xx-HAL/drivers/drv_usbh.c create mode 100644 bsp/stm32f4xx-HAL/drivers/drv_usbh.h (limited to 'bsp') diff --git a/bsp/stm32f4xx-HAL/Kconfig b/bsp/stm32f4xx-HAL/Kconfig index 61c4bfc9be..94242ca0f0 100644 --- a/bsp/stm32f4xx-HAL/Kconfig +++ b/bsp/stm32f4xx-HAL/Kconfig @@ -339,6 +339,25 @@ if RT_USING_USB_DEVICE default 0x5750 endif +if RT_USING_USB_HOST +menu "USB Host Config" + config USBH_USING_CONTROLLABLE_POWER + bool "using a gpio control usb power" + default y + if USBH_USING_CONTROLLABLE_POWER + config USBH_POWER_PIN + int "power control pin" + default 15 + config USBH_CURRENT_PIN + int "current check pin" + default 86 + endif + config USBH_USING_VBUS + bool "using vbus check pin" + default n +endmenu +endif + diff --git a/bsp/stm32f4xx-HAL/drivers/SConscript b/bsp/stm32f4xx-HAL/drivers/SConscript index b3e0ec1932..cd204cc0ab 100644 --- a/bsp/stm32f4xx-HAL/drivers/SConscript +++ b/bsp/stm32f4xx-HAL/drivers/SConscript @@ -26,11 +26,12 @@ if GetDepend(['RT_USING_WDT']): src += ['drv_iwg.c'] if GetDepend(['RT_USING_USB_DEVICE']): - src += ['drv_usb.c'] + src += ['drv_usbd.c'] if GetDepend(['RT_USING_RTC']): src += ['drv_rtc.c'] - +if GetDepend(['RT_USING_USB_HOST']): + src += ['drv_usbh.c'] CPPPATH = [cwd] group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usb.c b/bsp/stm32f4xx-HAL/drivers/drv_usb.c deleted file mode 100644 index c3d5fe253c..0000000000 --- a/bsp/stm32f4xx-HAL/drivers/drv_usb.c +++ /dev/null @@ -1,296 +0,0 @@ -/* - * File : stm32_usb.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2015, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2017-10-30 ZYH the first version - */ -#include "drv_usb.h" -#include -#include -#include "board.h" - -//#define USB_DISCONNECT_PIN 30 //PA9 - -static PCD_HandleTypeDef _stm_pcd; -static struct udcd _stm_udc; -static struct ep_id _ep_pool[] = -{ - {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED }, - {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED}, - {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED}, - {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED}, - {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED}, - {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED}, - {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED}, - {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED }, -}; - -void OTG_FS_IRQHandler(void) -{ - rt_interrupt_enter(); - - HAL_PCD_IRQHandler(&_stm_pcd); - - /* leave interrupt */ - rt_interrupt_leave(); - -} - -void HAL_PCD_ResetCallback(PCD_HandleTypeDef *pcd) -{ - /* open ep0 OUT and IN */ - HAL_PCD_EP_Open(pcd, 0x00, 0x40, EP_TYPE_CTRL); - HAL_PCD_EP_Open(pcd, 0x80, 0x40, EP_TYPE_CTRL); - rt_usbd_reset_handler(&_stm_udc); -} - -void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) -{ - rt_usbd_ep0_setup_handler(&_stm_udc, (struct urequest*)hpcd->Setup); -} - - -void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - if (epnum == 0) - { - rt_usbd_ep0_in_handler(&_stm_udc); - } - else - { - rt_usbd_ep_in_handler(&_stm_udc, 0x80|epnum, hpcd->IN_ep[epnum].xfer_count); - } -} - -void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) -{ - rt_usbd_connect_handler(&_stm_udc); -} - -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) -{ - rt_usbd_sof_handler(&_stm_udc); -} - -void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) -{ - rt_usbd_disconnect_handler(&_stm_udc); -} - -void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - if (epnum != 0) - { - rt_usbd_ep_out_handler(&_stm_udc, epnum, hpcd->OUT_ep[epnum].xfer_count); - } - else - { - rt_usbd_ep0_out_handler(&_stm_udc,hpcd->OUT_ep[0].xfer_count); - } -} - - -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) -{ - if(state == 1) - { - } - else - { - } -} - -void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) -{ - GPIO_InitTypeDef GPIO_InitStruct; - if(pcdHandle->Instance==USB_OTG_FS) - { - /* USER CODE BEGIN USB_MspInit 0 */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* Peripheral clock enable */ - __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); - - /* Peripheral interrupt init */ - HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0); - HAL_NVIC_EnableIRQ(OTG_FS_IRQn); - } -} - -void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) -{ - if(pcdHandle->Instance==USB_OTG_FS) - { - /* Peripheral clock disable */ - __HAL_RCC_USB_OTG_FS_CLK_DISABLE(); - - /**USB_OTG_FS GPIO Configuration - PA11 ------> USB_OTG_FS_DM - PA12 ------> USB_OTG_FS_DP - */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); - - /* Peripheral interrupt Deinit*/ - HAL_NVIC_DisableIRQ(OTG_FS_IRQn); - } -} - -static rt_err_t _ep_set_stall(rt_uint8_t address) -{ - HAL_PCD_EP_SetStall(&_stm_pcd, address); - return RT_EOK; -} - -static rt_err_t _ep_clear_stall(rt_uint8_t address) -{ - HAL_PCD_EP_ClrStall(&_stm_pcd, address); - return RT_EOK; -} - -static rt_err_t _set_address(rt_uint8_t address) -{ - HAL_PCD_SetAddress(&_stm_pcd, address); - return RT_EOK; -} - -static rt_err_t _set_config(rt_uint8_t address) -{ - return RT_EOK; -} - -static rt_err_t _ep_enable(uep_t ep) -{ - RT_ASSERT(ep != RT_NULL); - RT_ASSERT(ep->ep_desc != RT_NULL); - HAL_PCD_EP_Open(&_stm_pcd, ep->ep_desc->bEndpointAddress, - ep->ep_desc->wMaxPacketSize, ep->ep_desc->bmAttributes); - - return RT_EOK; -} - -static rt_err_t _ep_disable(uep_t ep) -{ - RT_ASSERT(ep != RT_NULL); - RT_ASSERT(ep->ep_desc != RT_NULL); - HAL_PCD_EP_Close(&_stm_pcd, ep->ep_desc->bEndpointAddress); - return RT_EOK; -} - -static rt_size_t _ep_read(rt_uint8_t address, void *buffer) -{ - rt_size_t size = 0; - - RT_ASSERT(buffer != RT_NULL); - - return size; -} - -static rt_size_t _ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size) -{ - HAL_PCD_EP_Receive(&_stm_pcd, address, buffer, size); - return size; -} - -static rt_size_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size) -{ - HAL_PCD_EP_Transmit(&_stm_pcd, address, buffer, size); - return size; -} - -static rt_err_t _ep0_send_status(void) -{ - HAL_PCD_EP_Transmit(&_stm_pcd, 0x00, NULL, 0); - return RT_EOK; -} - -static rt_err_t _suspend(void) -{ - return RT_EOK; -} - -static rt_err_t _wakeup(void) -{ - return RT_EOK; -} - -static rt_err_t _init(rt_device_t device) -{ - PCD_HandleTypeDef *pcd; - - /* Set LL Driver parameters */ - pcd = (PCD_HandleTypeDef*)device->user_data; - - pcd->Instance = USB_OTG_FS; - pcd->Init.dev_endpoints = 4; - pcd->Init.speed = PCD_SPEED_FULL; - pcd->Init.dma_enable = DISABLE; - pcd->Init.ep0_mps = DEP0CTL_MPS_64; - pcd->Init.phy_itface = PCD_PHY_EMBEDDED; - pcd->Init.Sof_enable = DISABLE; - pcd->Init.low_power_enable = DISABLE; - pcd->Init.lpm_enable = DISABLE; - pcd->Init.vbus_sensing_enable = DISABLE; - pcd->Init.use_dedicated_ep1 = DISABLE; - - /* Initialize LL Driver */ - HAL_PCD_Init(pcd); - - HAL_PCDEx_SetRxFiFo(pcd, 0x80); - HAL_PCDEx_SetTxFiFo(pcd, 0, 0x40); - HAL_PCDEx_SetTxFiFo(pcd, 1, 0x40); - HAL_PCDEx_SetTxFiFo(pcd, 2, 0x40); - HAL_PCDEx_SetTxFiFo(pcd, 3, 0x40); - HAL_PCD_Start(pcd); - - return RT_EOK; -} - -const static struct udcd_ops _udc_ops = -{ - _set_address, - _set_config, - _ep_set_stall, - _ep_clear_stall, - _ep_enable, - _ep_disable, - _ep_read_prepare, - _ep_read, - _ep_write, - _ep0_send_status, - _suspend, - _wakeup, -}; - - -int stm_usbd_register(void) -{ - rt_memset((void *)&_stm_udc, 0, sizeof(struct udcd)); - - _stm_udc.parent.type = RT_Device_Class_USBDevice; - _stm_udc.parent.init = _init; - _stm_udc.parent.user_data = &_stm_pcd; - _stm_udc.ops = &_udc_ops; - /* Register endpoint infomation */ - _stm_udc.ep_pool = _ep_pool; - _stm_udc.ep0.id = &_ep_pool[0]; - - rt_device_register((rt_device_t)&_stm_udc, "usbd", 0); - rt_usb_device_init(); - return RT_EOK; -} -INIT_DEVICE_EXPORT(stm_usbd_register); - diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usb.h b/bsp/stm32f4xx-HAL/drivers/drv_usb.h deleted file mode 100644 index 35d1ea033a..0000000000 --- a/bsp/stm32f4xx-HAL/drivers/drv_usb.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * File : stm32_usb.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2015, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2017-10-30 ZYH the first version - */ -#ifndef __STM32_USB_H__ -#define __STM32_USB_H__ -#include - -int stm_usbd_register(void); - -#endif diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usbd.c b/bsp/stm32f4xx-HAL/drivers/drv_usbd.c new file mode 100644 index 0000000000..f828b6d0f5 --- /dev/null +++ b/bsp/stm32f4xx-HAL/drivers/drv_usbd.c @@ -0,0 +1,296 @@ +/* + * File : stm32_usbd.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2015, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2017-10-30 ZYH the first version + */ +#include "drv_usbd.h" +#include +#include +#include "board.h" + +//#define USB_DISCONNECT_PIN 30 //PA9 + +static PCD_HandleTypeDef _stm_pcd; +static struct udcd _stm_udc; +static struct ep_id _ep_pool[] = +{ + {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED }, + {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED}, + {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED}, + {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED}, + {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED}, + {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED}, + {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED}, + {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED }, +}; + +void OTG_FS_IRQHandler(void) +{ + rt_interrupt_enter(); + + HAL_PCD_IRQHandler(&_stm_pcd); + + /* leave interrupt */ + rt_interrupt_leave(); + +} + +void HAL_PCD_ResetCallback(PCD_HandleTypeDef *pcd) +{ + /* open ep0 OUT and IN */ + HAL_PCD_EP_Open(pcd, 0x00, 0x40, EP_TYPE_CTRL); + HAL_PCD_EP_Open(pcd, 0x80, 0x40, EP_TYPE_CTRL); + rt_usbd_reset_handler(&_stm_udc); +} + +void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) +{ + rt_usbd_ep0_setup_handler(&_stm_udc, (struct urequest*)hpcd->Setup); +} + + +void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) +{ + if (epnum == 0) + { + rt_usbd_ep0_in_handler(&_stm_udc); + } + else + { + rt_usbd_ep_in_handler(&_stm_udc, 0x80|epnum, hpcd->IN_ep[epnum].xfer_count); + } +} + +void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) +{ + rt_usbd_connect_handler(&_stm_udc); +} + +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) +{ + rt_usbd_sof_handler(&_stm_udc); +} + +void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) +{ + rt_usbd_disconnect_handler(&_stm_udc); +} + +void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) +{ + if (epnum != 0) + { + rt_usbd_ep_out_handler(&_stm_udc, epnum, hpcd->OUT_ep[epnum].xfer_count); + } + else + { + rt_usbd_ep0_out_handler(&_stm_udc,hpcd->OUT_ep[0].xfer_count); + } +} + + +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) +{ + if(state == 1) + { + } + else + { + } +} + +void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) +{ + GPIO_InitTypeDef GPIO_InitStruct; + if(pcdHandle->Instance==USB_OTG_FS) + { + /* USER CODE BEGIN USB_MspInit 0 */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* Peripheral clock enable */ + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + + /* Peripheral interrupt init */ + HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(OTG_FS_IRQn); + } +} + +void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) +{ + if(pcdHandle->Instance==USB_OTG_FS) + { + /* Peripheral clock disable */ + __HAL_RCC_USB_OTG_FS_CLK_DISABLE(); + + /**USB_OTG_FS GPIO Configuration + PA11 ------> USB_OTG_FS_DM + PA12 ------> USB_OTG_FS_DP + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); + + /* Peripheral interrupt Deinit*/ + HAL_NVIC_DisableIRQ(OTG_FS_IRQn); + } +} + +static rt_err_t _ep_set_stall(rt_uint8_t address) +{ + HAL_PCD_EP_SetStall(&_stm_pcd, address); + return RT_EOK; +} + +static rt_err_t _ep_clear_stall(rt_uint8_t address) +{ + HAL_PCD_EP_ClrStall(&_stm_pcd, address); + return RT_EOK; +} + +static rt_err_t _set_address(rt_uint8_t address) +{ + HAL_PCD_SetAddress(&_stm_pcd, address); + return RT_EOK; +} + +static rt_err_t _set_config(rt_uint8_t address) +{ + return RT_EOK; +} + +static rt_err_t _ep_enable(uep_t ep) +{ + RT_ASSERT(ep != RT_NULL); + RT_ASSERT(ep->ep_desc != RT_NULL); + HAL_PCD_EP_Open(&_stm_pcd, ep->ep_desc->bEndpointAddress, + ep->ep_desc->wMaxPacketSize, ep->ep_desc->bmAttributes); + + return RT_EOK; +} + +static rt_err_t _ep_disable(uep_t ep) +{ + RT_ASSERT(ep != RT_NULL); + RT_ASSERT(ep->ep_desc != RT_NULL); + HAL_PCD_EP_Close(&_stm_pcd, ep->ep_desc->bEndpointAddress); + return RT_EOK; +} + +static rt_size_t _ep_read(rt_uint8_t address, void *buffer) +{ + rt_size_t size = 0; + + RT_ASSERT(buffer != RT_NULL); + + return size; +} + +static rt_size_t _ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size) +{ + HAL_PCD_EP_Receive(&_stm_pcd, address, buffer, size); + return size; +} + +static rt_size_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size) +{ + HAL_PCD_EP_Transmit(&_stm_pcd, address, buffer, size); + return size; +} + +static rt_err_t _ep0_send_status(void) +{ + HAL_PCD_EP_Transmit(&_stm_pcd, 0x00, NULL, 0); + return RT_EOK; +} + +static rt_err_t _suspend(void) +{ + return RT_EOK; +} + +static rt_err_t _wakeup(void) +{ + return RT_EOK; +} + +static rt_err_t _init(rt_device_t device) +{ + PCD_HandleTypeDef *pcd; + + /* Set LL Driver parameters */ + pcd = (PCD_HandleTypeDef*)device->user_data; + + pcd->Instance = USB_OTG_FS; + pcd->Init.dev_endpoints = 4; + pcd->Init.speed = PCD_SPEED_FULL; + pcd->Init.dma_enable = DISABLE; + pcd->Init.ep0_mps = DEP0CTL_MPS_64; + pcd->Init.phy_itface = PCD_PHY_EMBEDDED; + pcd->Init.Sof_enable = DISABLE; + pcd->Init.low_power_enable = DISABLE; + pcd->Init.lpm_enable = DISABLE; + pcd->Init.vbus_sensing_enable = DISABLE; + pcd->Init.use_dedicated_ep1 = DISABLE; + + /* Initialize LL Driver */ + HAL_PCD_Init(pcd); + + HAL_PCDEx_SetRxFiFo(pcd, 0x80); + HAL_PCDEx_SetTxFiFo(pcd, 0, 0x40); + HAL_PCDEx_SetTxFiFo(pcd, 1, 0x40); + HAL_PCDEx_SetTxFiFo(pcd, 2, 0x40); + HAL_PCDEx_SetTxFiFo(pcd, 3, 0x40); + HAL_PCD_Start(pcd); + + return RT_EOK; +} + +const static struct udcd_ops _udc_ops = +{ + _set_address, + _set_config, + _ep_set_stall, + _ep_clear_stall, + _ep_enable, + _ep_disable, + _ep_read_prepare, + _ep_read, + _ep_write, + _ep0_send_status, + _suspend, + _wakeup, +}; + + +int stm_usbd_register(void) +{ + rt_memset((void *)&_stm_udc, 0, sizeof(struct udcd)); + + _stm_udc.parent.type = RT_Device_Class_USBDevice; + _stm_udc.parent.init = _init; + _stm_udc.parent.user_data = &_stm_pcd; + _stm_udc.ops = &_udc_ops; + /* Register endpoint infomation */ + _stm_udc.ep_pool = _ep_pool; + _stm_udc.ep0.id = &_ep_pool[0]; + + rt_device_register((rt_device_t)&_stm_udc, "usbd", 0); + rt_usb_device_init(); + return RT_EOK; +} +INIT_DEVICE_EXPORT(stm_usbd_register); + diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usbd.h b/bsp/stm32f4xx-HAL/drivers/drv_usbd.h new file mode 100644 index 0000000000..14a7365230 --- /dev/null +++ b/bsp/stm32f4xx-HAL/drivers/drv_usbd.h @@ -0,0 +1,20 @@ +/* + * File : stm32_usbd.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2015, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2017-10-30 ZYH the first version + */ +#ifndef __STM32_USBD_H__ +#define __STM32_USBD_H__ +#include + +int stm_usbd_register(void); + +#endif diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usbh.c b/bsp/stm32f4xx-HAL/drivers/drv_usbh.c new file mode 100644 index 0000000000..c35aa4a16c --- /dev/null +++ b/bsp/stm32f4xx-HAL/drivers/drv_usbh.c @@ -0,0 +1,279 @@ +/* + * File : stm32_usbh.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2015, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2017-10-30 ZYH the first version + */ +#include "drv_usbh.h" +#include +#include +#include "board.h" +#define OTG_FS_PORT 1 +static HCD_HandleTypeDef _stm_hhcd_fs; +static struct rt_completion urb_completion; +void HAL_HCD_MspInit(HCD_HandleTypeDef *hcdHandle) +{ + GPIO_InitTypeDef GPIO_InitStruct; + if (hcdHandle->Instance == USB_OTG_FS) + { + /**USB_OTG_FS GPIO Configuration + PA9 ------> USB_OTG_FS_VBUS + PA10 ------> USB_OTG_FS_ID + PA11 ------> USB_OTG_FS_DM + PA12 ------> USB_OTG_FS_DP + */ + __HAL_RCC_GPIOA_CLK_ENABLE(); +#ifdef USBH_USING_VBUS + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#endif + GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* Peripheral clock enable */ + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + + /* Peripheral interrupt init */ + HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(OTG_FS_IRQn); + } +} + +void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hcdHandle) +{ + if (hcdHandle->Instance == USB_OTG_FS) + { + /* Peripheral clock disable */ + __HAL_RCC_USB_OTG_FS_CLK_DISABLE(); + + /**USB_OTG_FS GPIO Configuration + PA9 ------> USB_OTG_FS_VBUS + PA10 ------> USB_OTG_FS_ID + PA11 ------> USB_OTG_FS_DM + PA12 ------> USB_OTG_FS_DP + */ +#ifdef USBH_USING_VBUS + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9); +#endif + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12 | GPIO_PIN_11); + + /* Peripheral interrupt Deinit*/ + HAL_NVIC_DisableIRQ(OTG_FS_IRQn); + } +} +void OTG_FS_IRQHandler(void) +{ + rt_interrupt_enter(); + HAL_HCD_IRQHandler(&_stm_hhcd_fs); + rt_interrupt_leave(); +} +void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)//提供定时器 +{ + //rt_kprintf("sof callback\n"); +} +static __IO rt_bool_t connect_status = RT_FALSE; +void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd) +{ + uhcd_t hcd = (uhcd_t)hhcd->pData; + if (!connect_status) + { + connect_status = RT_TRUE; + RT_DEBUG_LOG(RT_DEBUG_USB, ("connected\n")); + rt_usbh_root_hub_connect_handler(hcd, OTG_FS_PORT, RT_FALSE); + } +} +void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd) +{ + uhcd_t hcd = (uhcd_t)hhcd->pData; + if (connect_status) + { + connect_status = RT_FALSE; + RT_DEBUG_LOG(RT_DEBUG_USB, ("disconnnect\n")); + rt_usbh_root_hub_disconnect_handler(hcd, OTG_FS_PORT); + } +} +void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state) +{ + //rt_kprintf("NotifyURBChange_Callback\n"); + rt_completion_done(&urb_completion); +} +static rt_err_t drv_reset_port(rt_uint8_t port) +{ + RT_DEBUG_LOG(RT_DEBUG_USB, ("reset port\n")); + HAL_HCD_ResetPort(&_stm_hhcd_fs); + return RT_EOK; +} + +static int drv_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeout) +{ + while (1) + { + if(!connect_status) + { + return -1; + } + rt_completion_init(&urb_completion); + HAL_HCD_HC_SubmitRequest(&_stm_hhcd_fs, + pipe->pipe_index, + (pipe->ep.bEndpointAddress & 0x80) >> 7, + pipe->ep.bmAttributes, + token, + buffer, + nbytes, + 0); + rt_completion_wait(&urb_completion, timeout); + if (HAL_HCD_HC_GetState(&_stm_hhcd_fs, pipe->pipe_index) == HC_NAK) + { + RT_DEBUG_LOG(RT_DEBUG_USB, ("nak\n")); + if (pipe->ep.bmAttributes == USB_EP_ATTR_INT) + { + rt_thread_delay((pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) > 0 ? (pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) : 1); + } + HAL_HCD_HC_Halt(&_stm_hhcd_fs, pipe->pipe_index); + HAL_HCD_HC_Init(&_stm_hhcd_fs, + pipe->pipe_index, + pipe->ep.bEndpointAddress, + pipe->inst->address, + USB_OTG_SPEED_FULL, + pipe->ep.bmAttributes, + pipe->ep.wMaxPacketSize); + continue; + } + else if(HAL_HCD_HC_GetState(&_stm_hhcd_fs, pipe->pipe_index) == HC_STALL) + { + RT_DEBUG_LOG(RT_DEBUG_USB, ("stall\n")); + pipe->status = UPIPE_STATUS_STALL; + if (pipe->callback != RT_NULL) + { + pipe->callback(pipe); + } + return -1; + } + else if(HAL_HCD_HC_GetState(&_stm_hhcd_fs, pipe->pipe_index) == URB_ERROR) + { + RT_DEBUG_LOG(RT_DEBUG_USB, ("error\n")); + pipe->status = UPIPE_STATUS_ERROR; + if (pipe->callback != RT_NULL) + { + pipe->callback(pipe); + } + return -1; + } + else if (HAL_HCD_HC_GetURBState(&_stm_hhcd_fs, pipe->pipe_index) != URB_NOTREADY && + HAL_HCD_HC_GetURBState(&_stm_hhcd_fs, pipe->pipe_index) != URB_NYET) + { + RT_DEBUG_LOG(RT_DEBUG_USB, ("ok\n")); + pipe->status = UPIPE_STATUS_OK; + if (pipe->callback != RT_NULL) + { + pipe->callback(pipe); + } + return nbytes; + } + return -1; + } +} +static rt_uint16_t pipe_index = 0; +static rt_uint8_t drv_get_free_pipe_index() +{ + rt_uint8_t idx; + for (idx = 1; idx < 16; idx++) + { + if (!(pipe_index & (0x01 << idx))) + { + pipe_index |= (0x01 << idx); + return idx; + } + } + return 0xff; +} +static void drv_free_pipe_index(rt_uint8_t index) +{ + pipe_index &= ~(0x01 << index); +} +static rt_err_t drv_open_pipe(upipe_t pipe) +{ + pipe->pipe_index = drv_get_free_pipe_index(); + + HAL_HCD_HC_Init(&_stm_hhcd_fs, + pipe->pipe_index, + pipe->ep.bEndpointAddress, + pipe->inst->address, + USB_OTG_SPEED_FULL, + pipe->ep.bmAttributes, + pipe->ep.wMaxPacketSize); + /* Set DATA0 PID token*/ + if(_stm_hhcd_fs.hc[pipe->pipe_index].ep_is_in) + { + _stm_hhcd_fs.hc[pipe->pipe_index].toggle_in = 0; + } + else + { + _stm_hhcd_fs.hc[pipe->pipe_index].toggle_out = 0; + } + return RT_EOK; +} +static rt_err_t drv_close_pipe(upipe_t pipe) +{ + HAL_HCD_HC_Halt(&_stm_hhcd_fs, pipe->pipe_index); + drv_free_pipe_index(pipe->pipe_index); + return RT_EOK; +} + +struct uhcd_ops _uhcd_ops = +{ + drv_reset_port, + drv_pipe_xfer, + drv_open_pipe, + drv_close_pipe, +}; + +static rt_err_t _init(rt_device_t device) +{ + HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)device->user_data; + hhcd->Instance = USB_OTG_FS; + hhcd->Init.Host_channels = 8; + hhcd->Init.speed = HCD_SPEED_FULL; + hhcd->Init.dma_enable = DISABLE; + hhcd->Init.phy_itface = HCD_PHY_EMBEDDED; + hhcd->Init.Sof_enable = DISABLE; + RT_ASSERT(HAL_HCD_Init(hhcd) == HAL_OK); + HAL_HCD_Start(hhcd); +#ifdef USBH_USING_CONTROLLABLE_POWER + rt_pin_mode(USBH_POWER_PIN, PIN_MODE_OUTPUT); + + rt_pin_write(USBH_POWER_PIN, PIN_LOW); +#endif + return RT_EOK; +} +int stm_usbh_register(void) +{ + uhcd_t uhcd = (uhcd_t)rt_malloc(sizeof(struct uhcd)); + RT_ASSERT(uhcd != RT_NULL); + rt_memset((void *)uhcd, 0, sizeof(struct uhcd)); + + uhcd->parent.type = RT_Device_Class_USBHost; + uhcd->parent.init = _init; + uhcd->parent.user_data = &_stm_hhcd_fs; + uhcd->ops = &_uhcd_ops; + uhcd->num_ports = 1; + _stm_hhcd_fs.pData = uhcd; + + rt_device_register((rt_device_t)uhcd, "usbh", 0); + rt_usb_host_init(); + return RT_EOK; +} +INIT_DEVICE_EXPORT(stm_usbh_register); diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usbh.h b/bsp/stm32f4xx-HAL/drivers/drv_usbh.h new file mode 100644 index 0000000000..aba86e0b1d --- /dev/null +++ b/bsp/stm32f4xx-HAL/drivers/drv_usbh.h @@ -0,0 +1,20 @@ +/* + * File : stm32_usbh.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2015, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2017-12-12 ZYH the first version + */ +#ifndef __STM32_USBH_H__ +#define __STM32_USBH_H__ +#include + +int stm_usbh_register(void); + +#endif -- cgit v1.3.1