最全USB HID开发资料,悉心整理一个月,亲自测试
【源码】STLINK-V3MINI 高速USB仿真器,成功改刷【高速CMSIS-DAP】
入门嵌入式,为什么STM32是“优选起步”?
STM32和Arduino对比,谁更耐打?
STM32串口自动识别波特率的原理
STM32 USB CDC 虚拟多串口
实战经验 | 选择USBX模块生成USB CDC ACM无PD的项目
STM32超详细学习笔记
新手必看!STM32单片机应该怎么学?
如果你解决了这些问题,就可以直接学STM32
微信公众号
手机版
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:: C+ Y* F2 d5 K& [
3 b. k) J- ]) W/ [ x
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev){
GPIO_InitTypeDef GPIO_InitStructure;% B7 ~: @/ g M4 w k
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);0 t7 T+ r* Z5 d4 e5 e! y7 t5 y
# e" s% i4 u4 |) p1 v0 X9 T- C
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | 2 c5 \5 @ H$ o$ s+ P" U
GPIO_Pin_13 |, {' t/ D6 X/ }: c2 j
GPIO_Pin_14 |
GPIO_Pin_15;
7 T' w! s+ g$ g- N( c4 `
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;( p. \" {3 X' H4 F n6 i1 L1 {
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOB, &GPIO_InitStructure); - o. H% ~0 l5 F6 t( w# `8 s
GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; ( t8 g) |, H4 m5 F& c( d! L% `
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; 6 y* K3 s; E; B) X
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; 1 D- |" p0 y6 {- ^# K; ~
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ;
//---------------------------------------------------------------+ ^# P. k2 \; x
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;
//-----------------------------------------------------------------9 o: F7 v& Z* A; W4 I
//Vbus_EN 根据实际电路修改!0 U' f* D1 P, f, c% v0 `5 X
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 ; s# Q$ V1 h' k. w
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;' }, q% H. G: M c; z; U+ `
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_ResetBits(GPIOE, GPIO_Pin_0);
//-------------------------------------------------------------------8 g; O/ K3 a3 n
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( M3 }8 w `3 A3 B9 V7 C
#endif /* USE_USB_OTG_HS */
#ifndef USE_ULPI_PHY1 z3 {6 Z5 Z6 k9 O! y1 [2 x2 f
//#define USE_ULPI_PHY# \6 R6 d! d) \+ A% P$ o; A
#endif /* USE_ULPI_PHY */
#ifndef USE_EMBEDDED_PHY
#define USE_EMBEDDED_PHY9 I" L; D8 Q1 C3 Z, X
#endif /* USE_EMBEDDED_PHY */
#ifdef USE_USB_OTG_HS " C& {# @" V- \6 y, k: v
#define USB_OTG_HS_CORE
#endif. `" h$ _, I& R8 q3 `9 x1 c6 J9 `
/*******************************************************************************0 U+ b; _* f1 f8 n
* FIFO Size Configuration in Host mode( J& K2 [; p1 G. _( n
* : J: c4 V0 `6 S8 s0 E# i
* (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* V, \2 f; I1 [6 U0 S
*
* (ii) For the host nonperiodic Transmit FIFO is the largest maximum packet size * G$ f4 B3 \: f; ~5 u) x" v& c
* for all supported nonperiodic OUT channels. Typically, a space ( j! b4 @2 m& u+ }
* corresponding to two Largest Packet Size is recommended., C; D3 M6 h9 n4 x, ]. v# d# z# R% B
*/ e' s* g) {& S' q; U8 E
* (iii) The minimum amount of RAM required for Host periodic Transmit FIFO is 3 C" ~! c Q6 [( E; D, l; N) W
* 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 1 m4 A- h2 d; } t
* that channel.( y' l8 h8 V9 ~* s8 X
*******************************************************************************/
1 L# \3 d% r4 y6 m1 H
/****************** USB OTG HS CONFIGURATION **********************************/8 L( [+ k% P( \
#ifdef USB_OTG_HS_CORE
#define RX_FIFO_HS_SIZE 5125 U3 ^ Z/ h% s! l1 J
#define TXH_NP_HS_FIFOSIZ 256. P5 {3 w2 i/ F* K C- n+ ~
#define TXH_P_HS_FIFOSIZ 2564 _+ t6 K* e( x! T- Q3 f
// #define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT
// #define USB_OTG_HS_SOF_OUTPUT_ENABLED8 U+ M5 W6 ?! R- `& X5 s8 r
#ifdef USE_ULPI_PHY
#define USB_OTG_ULPI_PHY_ENABLED. E* e) j# i- k4 m, h* U
#endif9 f( ~: }& I( ~$ Z W0 O
#ifdef USE_EMBEDDED_PHY
#define USB_OTG_EMBEDDED_PHY_ENABLED+ P0 I- I! U. E* ?# E
#endif
#define USB_OTG_HS_INTERNAL_DMA_ENABLED
#define USB_OTG_EXTERNAL_VBUS_ENABLED. N3 ?" ^0 J; N U2 O. h% _8 E" n6 [
//#define USB_OTG_INTERNAL_VBUS_ENABLED
#endif& m3 P8 y, c3 G4 L' j
/****************** 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, c" N+ [" I6 I4 H: e
/****************** USB OTG MODE CONFIGURATION ********************************/2 C) d2 m( r9 V; f/ K
#define USE_HOST_MODE( E, j) f4 d8 c+ Q$ X( G/ q
//#define USE_DEVICE_MODE
//#define USE_OTG_MODE* c' B% k; g2 N8 M1 H
#ifndef USB_OTG_FS_CORE3 K9 I( N- f+ D4 P& ]
#ifndef USB_OTG_HS_CORE* b0 o! X4 w2 V, y+ w
#error "USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined"4 \6 k E6 L* h) H8 ?) e7 o
#endif" B) q% w( z; y1 j( x* l4 f
#endif
#ifndef USE_DEVICE_MODE4 v, U; t9 t$ C, Q' }
#ifndef USE_HOST_MODE) `9 C9 ` t( C! O% S6 [: g
#error "USE_DEVICE_MODE or USE_HOST_MODE should be defined"' l; T4 G2 H; E$ ?" i; ?- g/ L
#endif1 e) o( H7 F0 L
#endif% Z* z* J4 q( T& Z5 y: T
#ifndef USE_USB_OTG_HS( N7 H1 h# E7 h3 b
#ifndef USE_USB_OTG_FS+ ~6 `% O! g$ t1 R `( [
#error "USE_USB_OTG_HS or USE_USB_OTG_FS should be defined"! y/ _& i2 v: s- A: Z
#endif
#else //USE_USB_OTG_HS* S2 z% O9 f" o1 x" ?
#ifndef USE_ULPI_PHY
#ifndef USE_EMBEDDED_PHY
#error "USE_ULPI_PHY or USE_EMBEDDED_PHY should be defined"
#endif. M+ c& n; l$ o; w( r5 e; J( d: b
#endif
#endif
/****************** C Compilers dependant keywords ****************************/
/* In HS mode and when the DMA is used, all variables and data structures dealing/ S% ?5 y6 v+ g' h) [$ z! \5 z% f
with the DMA during the transaction process should be 4-bytes aligned */ ; t Q: V& i( V
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
#if defined (__GNUC__) /* GNU Compiler */: N6 U+ u; [+ Y* S2 w/ q* f
#define __ALIGN_END __attribute__ ((aligned (4)))
#define __ALIGN_BEGIN
#else 0 c% S4 y3 N" a) b! s
#define __ALIGN_END9 y6 E9 D4 B0 g- I
#if defined (__CC_ARM) /* ARM Compiler */
#define __ALIGN_BEGIN __align(4) 4 I8 m9 ?. Z9 c4 i% |
#elif defined (__ICCARM__) /* IAR Compiler */9 l3 y( Y& t( \
#define __ALIGN_BEGIN , e) t h' k6 h' U5 \6 W5 [( Q- ?/ Q
#elif defined (__TASKING__) /* TASKING Compiler */- z5 @9 T4 a& t3 b6 _
#define __ALIGN_BEGIN __align(4)
#endif /* __CC_ARM */ " h' m3 `# m _- b; D
#endif /* __GNUC__ */ 9 T1 w' N7 ?# j# A6 [% w* H3 O! }
#else
#define __ALIGN_BEGIN
#define __ALIGN_END
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */, b2 [, h+ L8 `( v# X
/* __packed keyword used to decrease the data type alignment to 1-byte */
#if defined (__CC_ARM) /* ARM Compiler */& e0 H! _+ O! ? s) ~6 I8 T3 h: \) K
#define __packed __packed
#elif defined (__ICCARM__) /* IAR Compiler */5 z% o# l6 k$ i+ D' ?4 L
#define __packed __packed
#elif defined ( __GNUC__ ) /* GNU Compiler */ $ e, X$ \; [( ]1 J3 ?
#define __packed __attribute__ ((__packed__))
#elif defined (__TASKING__) /* TASKING Compiler */& d3 B) v4 a4 G; x
#define __packed __unaligned
#endif /* __CC_ARM */
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
int main(void){
0 b7 p; @- o' _: y& U. T
/*!< At this stage the microcontroller clock setting is already configured, ' ^" N7 l& t5 j; s
this is done through SystemInit() function which is called from startup
file (startup_stm32fxxx_xx.s) before to branch to application main.: s0 `6 l* Z. U- ~3 e1 M
To reconfigure the default setting of SystemInit() function, refer to
system_stm32fxxx.c file# h* S ~$ i5 f# l* l
*/ 2 v- p( ?6 {2 g
/* Init Host Library */# |2 ?. j3 J; L3 `2 a. Q0 ]
USBH_Init(&USB_OTG_Core, * W" i9 F* y4 U
USB_OTG_HS_CORE_ID,6 j0 F! ~' J: T, [8 z- X
&USB_Host,
&USBH_MSC_cb,
&USR_cb);
3 _' U$ Q7 W' B, ^) }3 o4 C0 e& \
while (1){( w3 _& j+ v+ R" r0 q) t; C
/* Host Task handler */ {' I, P! O4 @; m1 P0 `
USBH_Process(&USB_OTG_Core, &USB_Host);
if(iapRun==0){) b- z! I/ H2 L' ~% g
IAP_URN_UserProgrammer();
}* P. A4 W1 @* a4 |
}
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
case HOST_USR_INPUT: + u" t5 ~2 s# b% ~7 A
/*The function should return user response true to move to class state */
//修改:添加MSC-HID自动判别,然后再初始化CALSS-CB% A3 x' A2 R! H
if(JB_MSC_HID(phost,0)){ //0:判别MSC,1:判别HID! f: L6 t9 x7 s: b8 H
SetHOST_USB_MODE(0);9 f6 e' s" W D9 v. r
phost->class_cb=&USBH_MSC_cb;
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){- [5 C8 E8 E9 t
phost->gState = HOST_CLASS_REQUEST; / {. {3 y; l- p: w7 j- \
} 9 G5 S" _* Y6 C
}else{. Y! F U5 E) @: J1 O4 i. K6 o
if(JB_MSC_HID(phost,1)){ //0:判别MSC,1:判别HID
SetHOST_USB_MODE(1);( |) q2 ^% _2 R1 N/ U+ l
phost->class_cb=&HID_cb; E( g" N! b/ [4 W- e0 P
//phost->usr_cb=&USR_Callbacks;//???????
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){: G5 K. I5 P7 t' @# }
phost->gState = HOST_CLASS_REQUEST;
} / y! l7 Y1 E! h* y
}
} ( ?$ A5 S+ P+ {& r$ [
break;
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
/* Check for enumeration status */
if ( USBH_HandleEnum(pdev , phost) == USBH_OK){ 0 i9 L8 L" H/ [3 ]% {; Y/ H7 V3 r
/* The function shall return USBH_OK when full enumeration is complete */8 y: b3 D. Z. r' Z" Y
/* user callback for end of device basic enumeration */
phost->usr_cb->EnumerationDone();$ X5 Y! K7 [+ {# B
: s2 Y; \' q+ A) X
phost->gState = HOST_USR_INPUT;
}$ t# ^& I6 N+ T
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% _* ^4 Y3 G& g% z8 Z
. J" `8 G% ^3 D
if(flag==0){3 Q" r' o; g; p: Y! e6 s& _% t) U
if(phost->device_prop.Itf_Desc[0].bInterfaceClass == MSC_CLASS){- R+ R7 V2 h5 C- u B* Y
if(phost->device_prop.Itf_Desc[0].bInterfaceProtocol == MSC_PROTOCOL) return 1;7 x( {' H; ?; f f( g9 G
} W& T! D% ]" P; p' H6 P
}else{ l+ i; e5 \' [5 M
if(phost->device_prop.Itf_Desc[0].bInterfaceSubClass == HID_BOOT_CODE) return 1;
}( Q+ B7 A7 N$ v. w- d
return 0;7 w5 b! Y: N2 Z5 B( ~8 h9 p
}
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 ' C/ A0 E& o0 ~; o; X
* 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 Status6 N% d# ~$ K, q, ]2 u) F+ y
*/3 L, C. M2 {4 D9 l; s
USBH_Status USBH_CtlReq (USB_OTG_CORE_HANDLE *pdev, * `1 Q3 |. I3 U
USBH_HOST *phost, - O& N, f" Q$ G8 p( C0 g6 |" E. m
uint8_t *buff,
uint16_t length)
{
USBH_Status status;
status = USBH_BUSY;
switch (phost->RequestState)
{) x" B0 c( d8 z1 P g
case CMD_SEND:
/* Start a SETUP transfer */
USBH_SubmitSetupRequest(phost, buff, length);, R+ f- Y* N7 h1 G
phost->RequestState = CMD_WAIT;
status = USBH_BUSY; ]/ [3 P, g+ E$ b9 _3 a! F
break;
case CMD_WAIT:* L% F6 b9 j* h
if(Get_HOST_USB_MODE()==0){ //--2014-3-30 添加2种模式判别!!!/ r. z# c2 |/ O- |
if (status == URB_DONE){! `! S) i' h( p# b+ f$ ^' B
/* Commands successfully sent and Response Received */
phost->RequestState = CMD_SEND;+ A) N& a0 m! z1 h- R) ~$ l
status = USBH_OK;: S5 Q; ?6 A4 Q( M$ g8 W! c) {
}' P6 D& C% W5 l' z
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;* b5 |& e( E) ~/ Y( t2 Y7 h
}" I* I1 ]6 z" }" q+ O
}else{" s- P E7 z5 \* H
if (phost->Control.state == CTRL_COMPLETE ){//HID 处理!!4 K$ s; W2 x/ |
// 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)
{ n" V' i# G+ i' ~9 }& v' T* Z9 u2 _% A
// Failure Mode : _' y+ L- P' t# r
phost->RequestState = CMD_SEND;
status = USBH_FAIL;
}
else if (phost->Control.state == CTRL_STALLED )
{! s$ X) |8 T4 g4 }
// Commands successfully sent and Response Received
phost->RequestState = CMD_SEND;8 B1 l8 Q5 _4 s& l( `
status = USBH_NOT_SUPPORTED;
}
}
break;
//-------------------------------------: C2 `* y M9 P6 W! s; y
default:! K* R. I% f) x7 L$ L' q7 x
break;
}( ~' X7 g; i9 F- s* x A- b7 g! m5 F
return status;! o8 \, D- y: a3 r5 R
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
回复:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发