STM32 USB學習筆記4
主機環境:Windows 7 SP1
開發環境:MDK5.14
目標板:STM32F103C8T6
開發庫:STM32F1Cube庫和STM32_USB_Device_Library
前面分析了USB的描述符檔案,現在分析一下usbd_conf檔案,usbd_conf.h檔案是配置檔案,如下:
配置檔案很簡單,指明最大介面數為1,最大配置數為1,不支援使用者字串,支援自供電,除錯級別為0,在USB通訊中USB裝置支援兩種供電方式:自供電(裝置有自己獨立電源),匯流排供電(裝置由USB主機提供電源)。同時,在這裡採用靜態空間分配方案,分配的靜態空間大小為140位元組。除錯界別為0是沒有除錯資訊輸出的,根據自己需求來使能除錯功能。usbd_conf.c檔案要複雜一些,用於實現USB裝置庫的回撥函式以及MCU的一些底層初始化。各個中斷的服務例程在stm32f1xx_it.c檔案中,如下:/* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __USBD_CONF_H #define __USBD_CONF_H /* Includes ------------------------------------------------------------------*/ #include "stm32f1xx_hal.h" #include <stdio.h> #include <stdlib.h> #include <string.h> /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ /* Common Config */ #define USBD_MAX_NUM_INTERFACES 1 #define USBD_MAX_NUM_CONFIGURATION 1 #define USBD_MAX_STR_DESC_SIZ 0x100 #define USBD_SUPPORT_USER_STRING 0 #define USBD_SELF_POWERED 1 #define USBD_DEBUG_LEVEL 0 /* Exported macro ------------------------------------------------------------*/ /* Memory management macros */ /* For footprint reasons and since only one allocation is handled in the CDC class driver, the malloc/free is changed into a static allocation method */ void *USBD_static_malloc(uint32_t size); void USBD_static_free(void *p); #define MAX_STATIC_ALLOC_SIZE 140 /* CDC Class Driver Structure size */ #define USBD_malloc (uint32_t *)USBD_static_malloc #define USBD_free USBD_static_free #define USBD_memset /* Not used */ #define USBD_memcpy /* Not used */ /* DEBUG macros */ #if (USBD_DEBUG_LEVEL > 0) #define USBD_UsrLog(...) printf(__VA_ARGS__);\ printf("\n"); #else #define USBD_UsrLog(...) #endif #if (USBD_DEBUG_LEVEL > 1) #define USBD_ErrLog(...) printf("ERROR: ") ;\ printf(__VA_ARGS__);\ printf("\n"); #else #define USBD_ErrLog(...) #endif #if (USBD_DEBUG_LEVEL > 2) #define USBD_DbgLog(...) printf("DEBUG : ") ;\ printf(__VA_ARGS__);\ printf("\n"); #else #define USBD_DbgLog(...) #endif /* Exported functions ------------------------------------------------------- */ #endif /* __USBD_CONF_H */
中斷服務例程很簡單提供了一個IRQHandler介面,通過該介面會呼叫一些回撥函式來實現具體的處理,其中各個介面可以在庫函式說明中找到,如下:void SysTick_Handler(void) { Toggle_Leds(); HAL_IncTick(); } /******************************************************************************/ /* STM32F1xx Peripherals Interrupt Handlers */ /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */ /* available peripheral interrupt handler's name please refer to the startup */ /* file (startup_stm32f1xx.s). */ /******************************************************************************/ /** * @brief This function handles USB Handler. * @param None * @retval None */ void USB_LP_CAN1_RX0_IRQHandler(void) { HAL_PCD_IRQHandler(&hpcd); } /** * @brief This function handles DMA interrupt request. * @param None * @retval None */ void USARTx_DMA_TX_IRQHandler(void) { HAL_DMA_IRQHandler(UartHandle.hdmatx); } /** * @brief This function handles UART interrupt request. * @param None * @retval None */ void USARTx_IRQHandler(void) { HAL_UART_IRQHandler(&UartHandle); } /** * @brief This function handles TIM interrupt request. * @param None * @retval None */ void TIMx_IRQHandler(void) { HAL_TIM_IRQHandler(&TimHandle); }
USB底層API如下:
以上這些介面是需要在usbd_conf.c檔案中實現的,此外還有一個USB介面的硬體IO初始化,如下:
設定USB的硬體IO口為複用模式,並設定中斷優先順序且使能USB中斷,USB中斷號有三個:USB低優先順序中斷(20),USB高優先順序中斷(19),USB喚醒中斷(42),這裡是使能的USB低優先順序中斷,所有的USB事件均可觸發該中斷。USB底層回撥函式介面實現如下:/** * @brief Initializes the PCD MSP. * @param hpcd: PCD handle * @retval None */ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable the GPIOA clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* Configure USB DM/DP pins */ GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); GPIO_InitStruct.Mode = GPIO_MODE_AF_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Enable USB Clock */ __HAL_RCC_USB_CLK_ENABLE(); /* Set USB Interrupt priority */ HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 7, 0); /* Enable USB Interrupt */ HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); } /** * @brief De-Initializes the PCD MSP. * @param hpcd: PCD handle * @retval None */ void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) { /* Disable USB FS Clock */ __HAL_RCC_USB_CLK_DISABLE(); }
/*******************************************************************************
LL Driver Callbacks (PCD -> USB Device Library)
*******************************************************************************/
/**
* @brief SetupStage callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
}
/**
* @brief DataOut Stage callback.
* @param hpcd: PCD handle
* @param epnum: Endpoint Number
* @retval None
*/
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
}
/**
* @brief DataIn Stage callback.
* @param hpcd: PCD handle
* @param epnum: Endpoint Number
* @retval None
*/
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
}
/**
* @brief SOF callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
}
/**
* @brief Reset callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, USBD_SPEED_FULL);
/* Reset Device */
USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
}
/**
* @brief Suspend callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
{
/* Inform USB library that core enters in suspend Mode */
USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
}
/**
* @brief Resume callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
}
/**
* @brief ISOOUTIncomplete callback.
* @param hpcd: PCD handle
* @param epnum: Endpoint Number
* @retval None
*/
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
}
/**
* @brief ISOINIncomplete callback.
* @param hpcd: PCD handle
* @param epnum: Endpoint Number
* @retval None
*/
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
}
/**
* @brief ConnectCallback callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
}
/**
* @brief Disconnect callback.
* @param hpcd: PCD handle
* @retval None
*/
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
}
每一個回撥函式都是呼叫的usbd_core.c中的底層函式,對於不同的USB類該回調函式的介面實現是類似的,分別對應了觸發USB中斷的各個情況,跟串列埠中斷不同,串列埠中斷我們一般關心接收和傳送中斷即可。而USB的中斷源比較多,從回撥函式的個數就可以看得出來,即使這樣,但這些回撥函式裡面有幾個是為空的,如同步傳輸完成的回撥函式以及裝置連線的回撥函式,此外,HAL_PCD_IRQHandler()裡是沒有呼叫同步傳輸完成回撥函式的,在OTG模組的中斷服務例程裡才有呼叫。既然在全速模式下回調函式為空就不必刻意關注該回調函數了。重點關注Setup、DataIn、DataOut等即可,與之相關的是USB通訊中的Setup階段、資料階段,所有的USB通訊請求都是由USB主機發起,一般分為三個階段:設定階段、資料階段、狀態階段。其中資料階段是可以省略的。具體內容在後面遇到再分析,至此,USB中斷底層回撥函式瀏覽完畢,還剩下底層的裝置驅動API,在分析之前需要注意兩個個重要的結構體PCD_HandleTypeDef和USBD_HandleTypeDef,前者是STM32Cube庫提供的跟暫存器相關的資料結構,後者是USB器件庫提供的跟USB協議相關的資料結構,在例程中兩者相互引用,PCD_HandleTypeDef結構如下:
/**
* @brief PCD Handle Structure definition
*/
typedef struct
{
PCD_TypeDef *Instance; /*!< Register base address */
PCD_InitTypeDef Init; /*!< PCD required parameters */
__IO uint8_t USB_Address; /*!< USB Address: not used by USB OTG FS */
PCD_EPTypeDef IN_ep[15]; /*!< IN endpoint parameters */
PCD_EPTypeDef OUT_ep[15]; /*!< OUT endpoint parameters */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
__IO PCD_StateTypeDef State; /*!< PCD communication state */
uint32_t Setup[12]; /*!< Setup packet buffer */
void *pData; /*!< Pointer to upper stack Handler */
} PCD_HandleTypeDef;
/**
* @brief PCD State structure definition
*/
typedef enum
{
HAL_PCD_STATE_RESET = 0x00,
HAL_PCD_STATE_READY = 0x01,
HAL_PCD_STATE_ERROR = 0x02,
HAL_PCD_STATE_BUSY = 0x03,
HAL_PCD_STATE_TIMEOUT = 0x04
} PCD_StateTypeDef;
#if defined (USB)
typedef USB_TypeDef PCD_TypeDef;
typedef USB_CfgTypeDef PCD_InitTypeDef;
typedef USB_EPTypeDef PCD_EPTypeDef;
#endif /* USB */
/**
* @brief Universal Serial Bus Full Speed Device
*/
typedef struct
{
__IO uint16_t EP0R; /*!< USB Endpoint 0 register, Address offset: 0x00 */
__IO uint16_t RESERVED0; /*!< Reserved */
__IO uint16_t EP1R; /*!< USB Endpoint 1 register, Address offset: 0x04 */
__IO uint16_t RESERVED1; /*!< Reserved */
__IO uint16_t EP2R; /*!< USB Endpoint 2 register, Address offset: 0x08 */
__IO uint16_t RESERVED2; /*!< Reserved */
__IO uint16_t EP3R; /*!< USB Endpoint 3 register, Address offset: 0x0C */
__IO uint16_t RESERVED3; /*!< Reserved */
__IO uint16_t EP4R; /*!< USB Endpoint 4 register, Address offset: 0x10 */
__IO uint16_t RESERVED4; /*!< Reserved */
__IO uint16_t EP5R; /*!< USB Endpoint 5 register, Address offset: 0x14 */
__IO uint16_t RESERVED5; /*!< Reserved */
__IO uint16_t EP6R; /*!< USB Endpoint 6 register, Address offset: 0x18 */
__IO uint16_t RESERVED6; /*!< Reserved */
__IO uint16_t EP7R; /*!< USB Endpoint 7 register, Address offset: 0x1C */
__IO uint16_t RESERVED7[17]; /*!< Reserved */
__IO uint16_t CNTR; /*!< Control register, Address offset: 0x40 */
__IO uint16_t RESERVED8; /*!< Reserved */
__IO uint16_t ISTR; /*!< Interrupt status register, Address offset: 0x44 */
__IO uint16_t RESERVED9; /*!< Reserved */
__IO uint16_t FNR; /*!< Frame number register, Address offset: 0x48 */
__IO uint16_t RESERVEDA; /*!< Reserved */
__IO uint16_t DADDR; /*!< Device address register, Address offset: 0x4C */
__IO uint16_t RESERVEDB; /*!< Reserved */
__IO uint16_t BTABLE; /*!< Buffer Table address register, Address offset: 0x50 */
__IO uint16_t RESERVEDC; /*!< Reserved */
} USB_TypeDef;
#if defined (USB)
/**
* @brief USB Initialization Structure definition
*/
typedef struct
{
uint32_t dev_endpoints; /*!< Device Endpoints number.
This parameter depends on the used USB core.
This parameter must be a number between Min_Data = 1 and Max_Data = 15 */
uint32_t speed; /*!< USB Core speed.
This parameter can be any value of @ref USB_Core_Speed */
uint32_t ep0_mps; /*!< Set the Endpoint 0 Max Packet size.
This parameter can be any value of @ref USB_EP0_MPS */
uint32_t phy_itface; /*!< Select the used PHY interface.
This parameter can be any value of @ref USB_Core_PHY */
uint32_t Sof_enable; /*!< Enable or disable the output of the SOF signal. */
uint32_t low_power_enable; /*!< Enable or disable Low Power mode */
uint32_t lpm_enable; /*!< Enable or disable Battery charging. */
uint32_t battery_charging_enable; /*!< Enable or disable Battery charging. */
} USB_CfgTypeDef;
typedef struct
{
uint8_t num; /*!< Endpoint number
This parameter must be a number between Min_Data = 1 and Max_Data = 15 */
uint8_t is_in; /*!< Endpoint direction
This parameter must be a number between Min_Data = 0 and Max_Data = 1 */
uint8_t is_stall; /*!< Endpoint stall condition
This parameter must be a number between Min_Data = 0 and Max_Data = 1 */
uint8_t type; /*!< Endpoint type
This parameter can be any value of @ref USB_EP_Type */
uint16_t pmaadress; /*!< PMA Address
This parameter can be any value between Min_addr = 0 and Max_addr = 1K */
uint16_t pmaaddr0; /*!< PMA Address0
This parameter can be any value between Min_addr = 0 and Max_addr = 1K */
uint16_t pmaaddr1; /*!< PMA Address1
This parameter can be any value between Min_addr = 0 and Max_addr = 1K */
uint8_t doublebuffer; /*!< Double buffer enable
This parameter can be 0 or 1 */
uint16_t tx_fifo_num; /*!< This parameter is not required by USB Device FS peripheral, it is used
only by USB OTG FS peripheral
This parameter is added to ensure compatibility across USB peripherals */
uint32_t maxpacket; /*!< Endpoint Max packet size
This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */
uint8_t *xfer_buff; /*!< Pointer to transfer buffer */
uint32_t xfer_len; /*!< Current transfer length */
uint32_t xfer_count; /*!< Partial transfer length in case of multi packet transfer */
} USB_EPTypeDef;
#endif /* USB */
其中USB_TypeDef結構跟STM32暫存器相關,可通過檢視STM32F103C8T6參考手冊來熟悉USB相關暫存器。剩餘的兩個結構USB_CfgTypeDef和USB_EPTypeDef跟STM32中USB模組的特性相關,因此需要多多熟悉相對應的參考手冊,STM32F103C8T6可提供16個單向端點,至於PCD_HandleTypeDef中Setup陣列大小為12,還不清楚為什麼是12,USB協議中Setup包大小是8個位元組,而這裡是48個位元組,後面有眉目了再說吧。對於USB器件庫的使用來說PCD_HandleTypeDef結構體沒有USBD_HandleTypeDef結構體重要,USBD_HandleTypeDef結構體是USB器件庫中核心結構體,如下:
/* USB Device handle structure */
typedef struct _USBD_HandleTypeDef
{
uint8_t id;
uint32_t dev_config;
uint32_t dev_default_config;
uint32_t dev_config_status;
USBD_SpeedTypeDef dev_speed;
USBD_EndpointTypeDef ep_in[15];
USBD_EndpointTypeDef ep_out[15];
uint32_t ep0_state;
uint32_t ep0_data_len;
uint8_t dev_state;
uint8_t dev_old_state;
uint8_t dev_address;
uint8_t dev_connection_status;
uint8_t dev_test_mode;
uint32_t dev_remote_wakeup;
USBD_SetupReqTypedef request;
USBD_DescriptorsTypeDef *pDesc;
USBD_ClassTypeDef *pClass;
void *pClassData;
void *pUserData;
void *pData;
} USBD_HandleTypeDef;
/* Following USB Device Speed */
typedef enum
{
USBD_SPEED_HIGH = 0,
USBD_SPEED_FULL = 1,
USBD_SPEED_LOW = 2,
}USBD_SpeedTypeDef;
/* USB Device handle structure */
typedef struct
{
uint32_t status;
uint32_t total_length;
uint32_t rem_length;
uint32_t maxpacket;
} USBD_EndpointTypeDef;
/** @defgroup USBD_DEF_Exported_TypesDefinitions
* @{
*/
typedef struct usb_setup_req
{
uint8_t bmRequest;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
}USBD_SetupReqTypedef;
typedef struct _Device_cb
{
uint8_t (*Init) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx);
uint8_t (*DeInit) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx);
/* Control Endpoints*/
uint8_t (*Setup) (struct _USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req);
uint8_t (*EP0_TxSent) (struct _USBD_HandleTypeDef *pdev );
uint8_t (*EP0_RxReady) (struct _USBD_HandleTypeDef *pdev );
/* Class Specific Endpoints*/
uint8_t (*DataIn) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
uint8_t (*DataOut) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
uint8_t (*SOF) (struct _USBD_HandleTypeDef *pdev);
uint8_t (*IsoINIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
uint8_t (*IsoOUTIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum);
uint8_t *(*GetHSConfigDescriptor)(uint16_t *length);
uint8_t *(*GetFSConfigDescriptor)(uint16_t *length);
uint8_t *(*GetOtherSpeedConfigDescriptor)(uint16_t *length);
uint8_t *(*GetDeviceQualifierDescriptor)(uint16_t *length);
#if (USBD_SUPPORT_USER_STRING == 1)
uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length);
#endif
} USBD_ClassTypeDef;
一個USB匯流排最多可支援1128個裝置,地址範圍為0~127,因此這裡用一個位元組的dev_address來儲存唯一的裝置地址。而USBD_SetupReqTypedef結構體跟USB協議相關,USB協議中定義的Setup請求格式如下:
可以看到二者相匹配,獲取setup請求更多詳情,請參考USB2.0協議規範中第九章節。同時可以看到在USBD_HandleTypeDef結構體中包含了之前提到的描述符結構體USBD_DescriptorTypeDef,此外還包含了一個新的結構體裝置類結構體USBD_ClassTypeDef,它裡面是各個回撥函式,在USB中斷的回撥函式中會呼叫裝置類中的回撥函式來實現具體功能,USB器件庫手冊中說明了裝置類回撥函式的作用,如下:
這些介面的實現是在CDC介面檔案中實現,後面再分析,PCD_HandleTypeDef和USBD_HandleTypeDef結構通過void指標相互引用。
USB模組中靜態空間分配方案實現如下:
/**
* @brief static single allocation.
* @param size: size of allocated memory
* @retval None
*/
void *USBD_static_malloc(uint32_t size)
{
static uint32_t mem[MAX_STATIC_ALLOC_SIZE];
return mem;
}
/**
* @brief Dummy memory free
* @param *p pointer to allocated memory address
* @retval None
*/
void USBD_static_free(void *p)
{
}
實現很簡單申請一塊靜態空間並返回給呼叫者。USB裝置驅動的底層API實現如下:
/*******************************************************************************
LL Driver Interface (USB Device Library --> PCD)
*******************************************************************************/
/**
* @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)
{
/* Set LL Driver parameters */
hpcd.Instance = USB;
hpcd.Init.dev_endpoints = 8; //STM32F103C8T6Ö§³Ö8¸öË«Ïò¶Ëµã
hpcd.Init.ep0_mps = PCD_EP0MPS_64; //¶Ëµã0Ö§³ÖµÄ×î´ó·Ö×é×Ö½ÚΪ64
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
hpcd.Init.speed = PCD_SPEED_FULL; //USB2.0È«ËÙÉ豸
hpcd.Init.low_power_enable = 0; //²»Ê¹Äܵ͹¦ºÄģʽ
/* Link The driver to the stack */
hpcd.pData = pdev;
pdev->pData = &hpcd;
/* Initialize LL Driver */
HAL_PCD_Init((PCD_HandleTypeDef*)pdev->pData);
HAL_PCDEx_PMAConfig(pdev->pData , 0x00 , PCD_SNG_BUF, 0x40); //OUT¶Ëµã
HAL_PCDEx_PMAConfig(pdev->pData , 0x80 , PCD_SNG_BUF, 0x80); //IN¶Ëµã
HAL_PCDEx_PMAConfig(pdev->pData , CDC_IN_EP , PCD_SNG_BUF, 0xC0);
HAL_PCDEx_PMAConfig(pdev->pData , CDC_OUT_EP , PCD_SNG_BUF, 0x110);
HAL_PCDEx_PMAConfig(pdev->pData , CDC_CMD_EP , PCD_SNG_BUF, 0x100);
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)
{
HAL_PCD_DeInit((PCD_HandleTypeDef*)pdev->pData);
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)
{
HAL_PCD_Start((PCD_HandleTypeDef*)pdev->pData);
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)
{
HAL_PCD_Stop((PCD_HandleTypeDef*)pdev->pData);
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)
{
HAL_PCD_EP_Open((PCD_HandleTypeDef*)pdev->pData,
ep_addr,
ep_mps,
ep_type);
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)
{
HAL_PCD_EP_Close((PCD_HandleTypeDef*)pdev->pData, ep_addr);
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)
{
HAL_PCD_EP_Flush((PCD_HandleTypeDef*)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)
{
HAL_PCD_EP_SetStall((PCD_HandleTypeDef*)pdev->pData, ep_addr);
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)
{
HAL_PCD_EP_ClrStall((PCD_HandleTypeDef*)pdev->pData, ep_addr);
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)
{
PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*)pdev->pData;
if ((ep_addr & 0x80) == 0x80)
{
return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
}
else
{
return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
}
}
/**
* @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)
{
HAL_PCD_SetAddress((PCD_HandleTypeDef*)pdev->pData, dev_addr);
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)
{
HAL_PCD_EP_Transmit((PCD_HandleTypeDef*)pdev->pData, ep_addr, 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,
uint8_t *pbuf,
uint16_t size)
{
HAL_PCD_EP_Receive((PCD_HandleTypeDef*)pdev->pData, ep_addr, pbuf, size);
return USBD_OK;
}
/**
* @brief Returns the last transferred packet size.
* @param pdev: Device handle
* @param ep_addr: Endpoint Number
* @retval Recived Data Size
*/
uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
{
return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*)pdev->pData, ep_addr);
}
/**
* @brief Delays routine for the USB Device Library.
* @param Delay: Delay in ms
* @retval None
*/
void USBD_LL_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}
這些底層驅動的實現大部分都是呼叫Cube庫中USB介面,其具體實現不必刻意關注,如果有空閒可以靜心研究其暫存器的操作,我們一般只關心底層介面即可,比較複雜的一個介面是USBD_LL_Init(),USB中端點分為IN端點和OUT端點,共16個端點,在USB器件庫中使用一個位元組來表示,最高位為0是OUT端點,最高位為1是IN端點,USBD_LL_Init()中設定端點數為8,端點0最大包大小為64位元組,為全速裝置,不支援低功耗模式,並通過void指標pData來實現USBD_HandleTypeDef和PCD_HandleTypeDef結構的相互引用,HAL_PCDEx_PMAConfig()函式來設定端點緩衝區大小,這裡使用了5個單向端點,有關分組緩衝的的描述可以在對應的參考手冊上查詢到,示例如下:
STM32中有單獨的512個位元組是作為USB分組緩衝區,該區域的地址為0x4000_6000~0x4000_63FF,USB和CAN模組共用該區域,因此二者不能同時訪問,可以互斥訪問,有關分組緩衝區有兩類暫存器:USB分組緩衝區描述表地址暫存器(USB_BTABLE
);緩衝區描述表暫存器。如上圖所示緩衝區描述表暫存器是幾組暫存器的集合(一共8個端點共8組集合,每組裡包含了4個暫存器:ADDRn_TX、COUNTn_TX、ADDRn_RX、COUNTn_RX)。這些暫存器的地址是可變的,在0x4000_6000~0x4000_63FF中,具體地址由USB_BTABLE暫存器指定,在本例中USB_BTABLE指定為0,如下:
#define BTABLE_ADDRESS (0x000)
/*Set Btable Address*/
USBx->BTABLE = BTABLE_ADDRESS;
對於每個端點的緩衝區地址及大小則由緩衝區描述表中的對應的4個暫存器來指定。分組緩衝區的訪問是32bit的,所以雖然0x4000_0000~0x4000_63FF(共1K位元組),但實際上只用了512位元組。此外,這也是為何USB有兩種地址表示方式,一個是USB本地地址,另一個是應用程式訪問USB分組緩衝地址。在示例中設定預設的控制端點的分組大小為64位元組,這也和端點0的最大包大小為64相符,且都是使用的單緩衝端點,至此,usbd_conf檔案分析完畢。