主机环境:Windows 7 SP1
开发环境:MDK5.14
目标板:STM32F103C8T6
开发库:STM32F1Cube库和STM32_USB_Device_Library
前面分析了USB的描述符文件,现在分析一下usbd_conf文件,usbd_conf.h文件是配置文件,如下:
/* 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 */
配置文件很简单,指明最大接口数为1,最大配置数为1,不支持用户字符串,支持自供电,调试级别为0,在USB通信中USB设备支持两种供电方式:自供电(设备有自己独立电源),总线供电(设备由USB主机提供电源)。同时,在这里采用静态空间分配方案,分配的静态空间大小为140字节。调试界别为0是没有调试信息输出的,根据自己需求来使能调试功能。usbd_conf.c文件要复杂一些,用于实现USB设备库的回调函数以及MCU的一些底层初始化。各个中断的服务例程在stm32f1xx_it.c文件中,如下:
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);
}
中断服务例程很简单提供了一个IRQHandler接口,通过该接口会调用一些回调函数来实现具体的处理,其中各个接口可以在库函数说明中找到,如下:
USB底层API如下:
以上这些接口是需要在usbd_conf.c文件中实现的,此外还有一个USB接口的硬件IO初始化,如下:
/**
* @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.P