
【源码】STLINK-V3MINI 高速USB仿真器,成功改刷【高速CMSIS-DAP】
STM32硬件结构学习
最全USB HID开发资料,悉心整理一个月,亲自测试
实战经验 | 选择USBX模块生成USB CDC ACM无PD的项目
STM32 USB HID键盘例程
刘氓兔的杂谈【001】-片上USB 高速PHY
【经验分享】在进行 USB CDC 类开发时,无法发送 64整数倍的数据
在线直播|无需编写任何代码即可在STM32上实现USB-C Power Delivery
STM32 USB CDC 虚拟多串口
圈圈发布USB图书第二版有感,以及分享一些我学习USB过程...
RE:STM32F207的host USB的MSC和HID的合并开发
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
电路很简单,占用F207的HS-usb:
回复:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
继续发图:
电路很简单,占用F207的HS-usb:
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |
GPIO_Pin_13 |
GPIO_Pin_14 |
GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ;
//---------------------------------------------------------------
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;
//-----------------------------------------------------------------
//Vbus_EN 根据实际电路修改!
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE , ENABLE) ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_ResetBits(GPIOE, GPIO_Pin_0);
//-------------------------------------------------------------------
USB_OTG_BSP_TimeInit();
USB_OTG_BSP_mDelay(500); //Delay is need for stabilising the Vbus Low
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
#ifndef USE_USB_OTG_HS
#define USE_USB_OTG_HS
#endif /* USE_USB_OTG_HS */
#ifndef USE_ULPI_PHY
//#define USE_ULPI_PHY
#endif /* USE_ULPI_PHY */
#ifndef USE_EMBEDDED_PHY
#define USE_EMBEDDED_PHY
#endif /* USE_EMBEDDED_PHY */
#ifdef USE_USB_OTG_HS
#define USB_OTG_HS_CORE
#endif
/*******************************************************************************
* FIFO Size Configuration in Host mode
*
* (i) Receive data FIFO size = (Largest Packet Size / 4) + 1 or
* 2x (Largest Packet Size / 4) + 1, If a
* high-bandwidth channel or multiple isochronous
* channels are enabled
*
* (ii) For the host nonperiodic Transmit FIFO is the largest maximum packet size
* for all supported nonperiodic OUT channels. Typically, a space
* corresponding to two Largest Packet Size is recommended.
*
* (iii) The minimum amount of RAM required for Host periodic Transmit FIFO is
* the largest maximum packet size for all supported periodic OUT channels.
* If there is at least one High Bandwidth Isochronous OUT endpoint,
* then the space must be at least two times the maximum packet size for
* that channel.
*******************************************************************************/
/****************** USB OTG HS CONFIGURATION **********************************/
#ifdef USB_OTG_HS_CORE
#define RX_FIFO_HS_SIZE 512
#define TXH_NP_HS_FIFOSIZ 256
#define TXH_P_HS_FIFOSIZ 256
// #define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT
// #define USB_OTG_HS_SOF_OUTPUT_ENABLED
#ifdef USE_ULPI_PHY
#define USB_OTG_ULPI_PHY_ENABLED
#endif
#ifdef USE_EMBEDDED_PHY
#define USB_OTG_EMBEDDED_PHY_ENABLED
#endif
#define USB_OTG_HS_INTERNAL_DMA_ENABLED
#define USB_OTG_EXTERNAL_VBUS_ENABLED
//#define USB_OTG_INTERNAL_VBUS_ENABLED
#endif
/****************** USB OTG FS CONFIGURATION **********************************/
#ifdef USB_OTG_FS_CORE
#define RX_FIFO_FS_SIZE 128
#define TXH_NP_FS_FIFOSIZ 96
#define TXH_P_FS_FIFOSIZ 96
// #define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT
// #define USB_OTG_FS_SOF_OUTPUT_ENABLED
#endif
/****************** USB OTG MODE CONFIGURATION ********************************/
#define USE_HOST_MODE
//#define USE_DEVICE_MODE
//#define USE_OTG_MODE
#ifndef USB_OTG_FS_CORE
#ifndef USB_OTG_HS_CORE
#error "USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined"
#endif
#endif
#ifndef USE_DEVICE_MODE
#ifndef USE_HOST_MODE
#error "USE_DEVICE_MODE or USE_HOST_MODE should be defined"
#endif
#endif
#ifndef USE_USB_OTG_HS
#ifndef USE_USB_OTG_FS
#error "USE_USB_OTG_HS or USE_USB_OTG_FS should be defined"
#endif
#else //USE_USB_OTG_HS
#ifndef USE_ULPI_PHY
#ifndef USE_EMBEDDED_PHY
#error "USE_ULPI_PHY or USE_EMBEDDED_PHY should be defined"
#endif
#endif
#endif
/****************** C Compilers dependant keywords ****************************/
/* In HS mode and when the DMA is used, all variables and data structures dealing
with the DMA during the transaction process should be 4-bytes aligned */
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
#if defined (__GNUC__) /* GNU Compiler */
#define __ALIGN_END __attribute__ ((aligned (4)))
#define __ALIGN_BEGIN
#else
#define __ALIGN_END
#if defined (__CC_ARM) /* ARM Compiler */
#define __ALIGN_BEGIN __align(4)
#elif defined (__ICCARM__) /* IAR Compiler */
#define __ALIGN_BEGIN
#elif defined (__TASKING__) /* TASKING Compiler */
#define __ALIGN_BEGIN __align(4)
#endif /* __CC_ARM */
#endif /* __GNUC__ */
#else
#define __ALIGN_BEGIN
#define __ALIGN_END
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
/* __packed keyword used to decrease the data type alignment to 1-byte */
#if defined (__CC_ARM) /* ARM Compiler */
#define __packed __packed
#elif defined (__ICCARM__) /* IAR Compiler */
#define __packed __packed
#elif defined ( __GNUC__ ) /* GNU Compiler */
#define __packed __attribute__ ((__packed__))
#elif defined (__TASKING__) /* TASKING Compiler */
#define __packed __unaligned
#endif /* __CC_ARM */
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
int main(void){
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32fxxx_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32fxxx.c file
*/
/* Init Host Library */
USBH_Init(&USB_OTG_Core,
USB_OTG_HS_CORE_ID,
&USB_Host,
&USBH_MSC_cb,
&USR_cb);
while (1){
/* Host Task handler */
USBH_Process(&USB_OTG_Core, &USB_Host);
if(iapRun==0){
IAP_URN_UserProgrammer();
}
}
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
case HOST_USR_INPUT:
/*The function should return user response true to move to class state */
//修改:添加MSC-HID自动判别,然后再初始化CALSS-CB
if(JB_MSC_HID(phost,0)){ //0:判别MSC,1:判别HID
SetHOST_USB_MODE(0);
phost->class_cb=&USBH_MSC_cb;
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){
phost->gState = HOST_CLASS_REQUEST;
}
}else{
if(JB_MSC_HID(phost,1)){ //0:判别MSC,1:判别HID
SetHOST_USB_MODE(1);
phost->class_cb=&HID_cb;
//phost->usr_cb=&USR_Callbacks;//???????
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){
phost->gState = HOST_CLASS_REQUEST;
}
}
}
break;
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
/* Check for enumeration status */
if ( USBH_HandleEnum(pdev , phost) == USBH_OK){
/* The function shall return USBH_OK when full enumeration is complete */
/* user callback for end of device basic enumeration */
phost->usr_cb->EnumerationDone();
phost->gState = HOST_USR_INPUT;
}
break;
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
//修改:添加MSC-HID自动判别,然后再初始化CALSS-CB
unsigned char JB_MSC_HID(USBH_HOST *phost,unsigned char flag){//0:判别MSC,1:判别HID
if(flag==0){
if(phost->device_prop.Itf_Desc[0].bInterfaceClass == MSC_CLASS){
if(phost->device_prop.Itf_Desc[0].bInterfaceProtocol == MSC_PROTOCOL) return 1;
}
}else{
if(phost->device_prop.Itf_Desc[0].bInterfaceSubClass == HID_BOOT_CODE) return 1;
}
return 0;
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
/**
* @brief USBH_CtlReq
* USBH_CtlReq sends a control request and provide the status after
* completion of the request
* @param pdev: Selected device
* @param req: Setup Request Structure
* @param buff: data buffer address to store the response
* @param length: length of the response
* @retval Status
*/
USBH_Status USBH_CtlReq (USB_OTG_CORE_HANDLE *pdev,
USBH_HOST *phost,
uint8_t *buff,
uint16_t length)
{
USBH_Status status;
status = USBH_BUSY;
switch (phost->RequestState)
{
case CMD_SEND:
/* Start a SETUP transfer */
USBH_SubmitSetupRequest(phost, buff, length);
phost->RequestState = CMD_WAIT;
status = USBH_BUSY;
break;
case CMD_WAIT:
if(Get_HOST_USB_MODE()==0){ //--2014-3-30 添加2种模式判别!!!
if (status == URB_DONE){
/* Commands successfully sent and Response Received */
phost->RequestState = CMD_SEND;
status = USBH_OK;
}
else if (status == URB_ERROR){
/* Failure Mode */
phost->RequestState = CMD_SEND;
status = USBH_FAIL;
}
else if (status == URB_STALL){
/* Commands successfully sent and Response Received */
phost->RequestState = CMD_SEND;
status = USBH_NOT_SUPPORTED;
}
}else{
if (phost->Control.state == CTRL_COMPLETE ){//HID 处理!!
// Commands successfully sent and Response Received
phost->RequestState = CMD_SEND;
phost->Control.state =CTRL_IDLE;
status = USBH_OK;
}
else if (phost->Control.state == CTRL_ERROR)
{
// Failure Mode
phost->RequestState = CMD_SEND;
status = USBH_FAIL;
}
else if (phost->Control.state == CTRL_STALLED )
{
// Commands successfully sent and Response Received
phost->RequestState = CMD_SEND;
status = USBH_NOT_SUPPORTED;
}
}
break;
//-------------------------------------
default:
break;
}
return status;
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
回复:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发