最全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的合并开发
继续发图:6 i! t. V ?7 d! o; T' W
电路很简单,占用F207的HS-usb:
: D G9 I+ W1 }
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev){7 |/ C% d- K" Z1 s1 X
GPIO_InitTypeDef GPIO_InitStructure;" [" u# G& [, R6 q; A2 f5 Y; h2 @
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);" S8 X/ [4 t! {$ z& N
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |
GPIO_Pin_13 |% ^' ]& [, ]/ p; R
GPIO_Pin_14 |
GPIO_Pin_15;+ v6 ]* w( b; ~+ Z# `! H
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;$ a: g3 r3 U# ?& A4 M: n% q6 U
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;4 V1 K8 V* R$ m f
GPIO_Init(GPIOB, &GPIO_InitStructure); % O% f3 J% {) l# w9 ~; {- r% P1 I
3 W2 ^+ o) V. Y; c2 ^
GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; ' Z( N# B2 ?* b" d$ q0 v- [
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; ( p, T G) P- g. r1 ^, h
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ;; |( Y; L6 ^; W+ L' T: b
//---------------------------------------------------------------) }! u( [4 @, U, g2 Q: V
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; : s6 G7 n; ?9 \; }7 B1 g$ _$ f" s$ g
//-----------------------------------------------------------------9 |' G+ m. Q5 T5 b" h
//Vbus_EN 根据实际电路修改!
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE , ENABLE) ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;- S! `) H+ `& N h. y7 }
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;: R- U) i* M# f$ {/ v' J
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;3 x5 ?) X, i c" E; h% v( |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; : W0 {7 D8 Q: Z' E9 C
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);+ r" @5 H% u& z& a* r8 q" ]- N
4 A8 j# \, M2 P- g9 u' e: b7 f
GPIO_ResetBits(GPIOE, GPIO_Pin_0);
//-------------------------------------------------------------------9 p! V: u$ s4 x7 s
USB_OTG_BSP_TimeInit();* P+ k2 Q' n+ W% X+ w* [ Y" ]3 S
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_HS3 P3 R3 j* t/ O( A
#endif /* USE_USB_OTG_HS */+ h) ^( x* |/ D/ S. Y6 O9 Y
#ifndef USE_ULPI_PHY
//#define USE_ULPI_PHY
#endif /* USE_ULPI_PHY */9 @( V& b _1 ]$ m" e4 f6 ?% P6 ~
#ifndef USE_EMBEDDED_PHY% q( I8 d+ }! O$ e0 d0 y6 o- ^& D7 e% Z
#define USE_EMBEDDED_PHY3 M8 _# Q; p B0 ?1 @6 J
#endif /* USE_EMBEDDED_PHY */
#ifdef USE_USB_OTG_HS
#define USB_OTG_HS_CORE
#endif
/*******************************************************************************( F Q8 Z4 u9 R3 ~
* FIFO Size Configuration in Host mode) L) k% t- i) N
* & E# A- \; w" ?' r
* (i) Receive data FIFO size = (Largest Packet Size / 4) + 1 or
* 2x (Largest Packet Size / 4) + 1, If a 8 i/ g5 g7 P) j7 M
* high-bandwidth channel or multiple isochronous
* channels are enabled
*
* (ii) For the host nonperiodic Transmit FIFO is the largest maximum packet size ' a' @- n" |' e$ Q# X( R' d
* for all supported nonperiodic OUT channels. Typically, a space
* corresponding to two Largest Packet Size is recommended.1 H9 t: ]# s$ q) n% ?; W
*5 N# c' B3 z) W0 ^/ K5 J+ ~( a
* (iii) The minimum amount of RAM required for Host periodic Transmit FIFO is 2 l3 | d& L r% t
* the largest maximum packet size for all supported periodic OUT channels.( [& {. \# z. D7 o$ S+ V" n
* 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 9 T7 ?& Q7 |" z# |- g! P7 }
* that channel./ h) @' s" D5 s& I( R
*******************************************************************************/
: v$ r+ _' u. \1 u3 E
/****************** USB OTG HS CONFIGURATION **********************************/# K. r; q! Q3 Z! v, t
#ifdef USB_OTG_HS_CORE: g: A3 D# F& x1 e
#define RX_FIFO_HS_SIZE 5129 d) f3 r1 s( t4 P
#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) Q) }1 `3 s0 n) ~4 a4 y ?
#ifdef USE_ULPI_PHY
#define USB_OTG_ULPI_PHY_ENABLED
#endif& I' s* s& R" U$ N) X3 H) i5 M
#ifdef USE_EMBEDDED_PHY/ t0 X( K. N1 r4 A* \
#define USB_OTG_EMBEDDED_PHY_ENABLED
#endif {! S4 o7 D/ U4 n( j
#define USB_OTG_HS_INTERNAL_DMA_ENABLED% u$ f3 g. \8 ^7 T1 C
#define USB_OTG_EXTERNAL_VBUS_ENABLED9 f$ ^; q# l: F3 T& G W
//#define USB_OTG_INTERNAL_VBUS_ENABLED
#endif5 e4 T2 `, y! a5 i
/****************** USB OTG FS CONFIGURATION **********************************/7 \& u Q& D& L) G, |
#ifdef USB_OTG_FS_CORE
#define RX_FIFO_FS_SIZE 128
#define TXH_NP_FS_FIFOSIZ 96. [4 `+ G9 d- g! o
#define TXH_P_FS_FIFOSIZ 96( L6 C. K( _ o c+ M0 E
// #define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT0 y6 |6 P* ?2 x& \
// #define USB_OTG_FS_SOF_OUTPUT_ENABLED
#endif
/****************** USB OTG MODE CONFIGURATION ********************************/; A1 O' E4 V+ f
#define USE_HOST_MODE$ F( U- Q, @/ h3 @0 `0 n5 O/ M
//#define USE_DEVICE_MODE. A( f9 @* v, L9 ?% h& \
//#define USE_OTG_MODE2 s u/ O% V% _4 `- s6 F' f$ t j
#ifndef USB_OTG_FS_CORE
#ifndef USB_OTG_HS_CORE* F! ^5 c# l# i" D
#error "USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined"
#endif
#endif, p6 J4 i, n$ F6 @1 T; |
#ifndef USE_DEVICE_MODE
#ifndef USE_HOST_MODE ]* `" U. T) t4 D1 ~
#error "USE_DEVICE_MODE or USE_HOST_MODE should be defined"
#endif
#endif0 c d, w, l: P: F% Z& B: r- v
#ifndef USE_USB_OTG_HS0 ?) Z- ~8 T6 [- U+ F% B N# _
#ifndef USE_USB_OTG_FS
#error "USE_USB_OTG_HS or USE_USB_OTG_FS should be defined"
#endif
#else //USE_USB_OTG_HS5 v$ v0 W$ Z' p8 q% W- |) i. `
#ifndef USE_ULPI_PHY* r% ?6 c+ z! d
#ifndef USE_EMBEDDED_PHY( B+ N1 d8 |2 L
#error "USE_ULPI_PHY or USE_EMBEDDED_PHY should be defined"+ }; t* l* q0 w/ L* h7 a0 q
#endif
#endif
#endif
/****************** C Compilers dependant keywords ****************************/- O5 d, D+ E1 C; ^+ W" W: V/ k
/* In HS mode and when the DMA is used, all variables and data structures dealing! W: u. S# q1 Q- e! z
with the DMA during the transaction process should be 4-bytes aligned */
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED/ u/ g; {; \+ h5 L/ e* V
#if defined (__GNUC__) /* GNU Compiler */' i+ T s0 m. ~7 U) q
#define __ALIGN_END __attribute__ ((aligned (4))). w( ]( @! y6 p, Y2 J
#define __ALIGN_BEGIN 9 M4 f( `* l+ `. x- u! ]9 G
#else
#define __ALIGN_END3 Q8 g4 }( D! {: t$ [# P o4 x6 h! ~
#if defined (__CC_ARM) /* ARM Compiler */3 Z: A7 D" G8 M# D8 G P* J
#define __ALIGN_BEGIN __align(4)
#elif defined (__ICCARM__) /* IAR Compiler */. f: l3 E& I% {
#define __ALIGN_BEGIN
#elif defined (__TASKING__) /* TASKING Compiler */) z! R) B! u- p8 W0 f" U9 _
#define __ALIGN_BEGIN __align(4) 5 A+ P0 \3 ]) c4 U
#endif /* __CC_ARM */
#endif /* __GNUC__ */ % O; v3 `' n& v r H5 H( l
#else6 z2 B- [2 h* b. \. M
#define __ALIGN_BEGIN
#define __ALIGN_END
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */, r% Z9 a7 v, `: W
/* __packed keyword used to decrease the data type alignment to 1-byte */% n* M' t* b2 u" H1 K
#if defined (__CC_ARM) /* ARM Compiler */
#define __packed __packed f4 t& t9 u4 x' B
#elif defined (__ICCARM__) /* IAR Compiler */
#define __packed __packed; t4 L6 _5 X8 R) e) e
#elif defined ( __GNUC__ ) /* GNU Compiler */ 7 G; K" s: K7 t+ `3 @) P
#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, 2 N: }8 n# ^+ f7 Q
this is done through SystemInit() function which is called from startup2 I& S4 G, K* {
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: H2 ?. O8 l# U: ^3 R) h
*/ . Q# y7 n+ d i6 D0 j/ f4 z% K
/ G3 i( T5 c2 t8 C0 o: ]
/* Init Host Library */
USBH_Init(&USB_OTG_Core, * a" \( w! G4 V d
USB_OTG_HS_CORE_ID,
&USB_Host,
&USBH_MSC_cb,
&USR_cb);9 z& o: Y' @4 @4 w' T a/ d
while (1){: n) u/ s+ q3 j/ i, }( c. Q
/* Host Task handler */, \7 ~. I. V8 C3 p5 a4 n
USBH_Process(&USB_OTG_Core, &USB_Host);
if(iapRun==0){3 s( t- e1 l8 t2 m
IAP_URN_UserProgrammer();
}2 r, R6 t1 e$ d: i: A% r' h; r
}
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
case HOST_USR_INPUT: + S7 K5 t* O2 `0 a
/*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* N3 x* W7 I) E. |2 C: g4 ?) S
SetHOST_USB_MODE(0);& P- H& C N4 @- e+ p& H
phost->class_cb=&USBH_MSC_cb;: y7 }$ x! ~; w6 H
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){. a6 D, n9 ]6 `" a+ P
phost->gState = HOST_CLASS_REQUEST; # f' I. R2 L5 Z8 k- j2 X
}
}else{
if(JB_MSC_HID(phost,1)){ //0:判别MSC,1:判别HID* W( H' \7 U# B) B/ Y: y7 A
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;
}
}0 d/ |" L& c; R) @
}
break;
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
/* Check for enumeration status */
if ( USBH_HandleEnum(pdev , phost) == USBH_OK){ - E7 Z+ K; Q5 p0 `' C V9 m" h
/* The function shall return USBH_OK when full enumeration is complete */
- o8 B1 i: b" @9 Z* t* V8 K% \' o
/* user callback for end of device basic enumeration */
phost->usr_cb->EnumerationDone();/ H) P- B3 F7 \/ c( W' X
c& ?- r g/ r2 s+ c2 {, Z9 v9 G
phost->gState = HOST_USR_INPUT; 9 N! R$ X) G2 m) |
}
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- W# d' Q& l2 y$ M4 a7 r! ~
if(flag==0){
if(phost->device_prop.Itf_Desc[0].bInterfaceClass == MSC_CLASS){% Q' I# K) Z$ ~ W# q; x- W
if(phost->device_prop.Itf_Desc[0].bInterfaceProtocol == MSC_PROTOCOL) return 1;" D' ~0 s2 K! G5 L& p, Q! ?* B
}& U5 }9 \; S1 ]9 I& Z: L
}else{
if(phost->device_prop.Itf_Desc[0].bInterfaceSubClass == HID_BOOT_CODE) return 1; z6 {3 j0 r+ ~, C4 Q
}
return 0;! }1 G) B2 d8 A- F
}
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 ! S* t2 V* A+ g3 ~% i b
* completion of the request! c+ w, F6 D3 }+ v$ D% B; V
* @param pdev: Selected device
* @param req: Setup Request Structure
* @param buff: data buffer address to store the response8 {6 j5 ?) S: h8 X; t: S
* @param length: length of the response, R$ b& \7 ^' f; s+ U: `/ v9 [& j
* @retval Status0 T, y0 A/ S1 O6 K' c: o
*/
USBH_Status USBH_CtlReq (USB_OTG_CORE_HANDLE *pdev,
USBH_HOST *phost, 9 e- U% m V6 C& h% m3 @, a
uint8_t *buff,
uint16_t length)* A' Z$ U- r& J% m4 h6 `
{* y% `5 `6 l; T4 A( I4 U' V
USBH_Status status;
status = USBH_BUSY;2 k: {* Y* s# x0 A5 B
switch (phost->RequestState)) m7 r9 \9 C0 @3 `3 e5 {/ h
{
case CMD_SEND:$ \+ R; J4 e, Q- Y- z
/* Start a SETUP transfer */& J2 E7 v# B# z% i- Q
USBH_SubmitSetupRequest(phost, buff, length);
phost->RequestState = CMD_WAIT;6 \6 C% u4 J. |" {" {: e
status = USBH_BUSY;7 M/ a$ Q, |; ^/ `
break;! E7 ], x# J2 L8 P
8 A/ H9 ?: }3 h
case CMD_WAIT:
if(Get_HOST_USB_MODE()==0){ //--2014-3-30 添加2种模式判别!!!
if (status == URB_DONE){
/* Commands successfully sent and Response Received */ Z3 b2 m$ ^, H# e! }) P
phost->RequestState = CMD_SEND;
status = USBH_OK;- J8 X' G- f& `1 D- y+ g1 f( E1 W. @
}
else if (status == URB_ERROR){
/* Failure Mode */( Y Z0 X( D; w; R& U
phost->RequestState = CMD_SEND;
status = USBH_FAIL;
} z5 {. ^4 d0 P4 S
else if (status == URB_STALL){, z- [* T! d+ e. A. G
/* Commands successfully sent and Response Received */ 7 \! Q; K1 y. ?$ E2 h' w( H6 q
phost->RequestState = CMD_SEND;9 ]* H8 v6 `1 A5 T+ X5 Z
status = USBH_NOT_SUPPORTED;/ t+ \0 u% Q( X6 k6 [% _% n+ `
}
}else{
if (phost->Control.state == CTRL_COMPLETE ){//HID 处理!!
// Commands successfully sent and Response Received $ }9 o+ m) j; E- @6 |% c* A8 C6 |
phost->RequestState = CMD_SEND;; u% J, [5 R+ g& h2 y5 r4 @' _
phost->Control.state =CTRL_IDLE; ! c: H+ p9 k5 _9 b5 A6 |1 C
status = USBH_OK;
}
else if (phost->Control.state == CTRL_ERROR)5 c0 \& ~) q3 Y6 j* s2 i
{
// Failure Mode " R1 x& R! ~( i$ b
phost->RequestState = CMD_SEND;4 Q! k1 t7 E& ? E
status = USBH_FAIL;6 O, r6 T7 j. @- ]1 Y8 c+ ]
} ; `4 V" s7 K# j$ O6 k7 `) K+ D
else if (phost->Control.state == CTRL_STALLED )
{2 l% L: x& u# O9 a: b& z
// Commands successfully sent and Response Received
phost->RequestState = CMD_SEND;6 i. D- b& s. a; T* J( g
status = USBH_NOT_SUPPORTED;5 R; ]8 S j0 M
}8 R$ q! z, b2 i% X9 t
}
break;3 k- W s4 L- b" Z7 k
//-------------------------------------
default:' P U2 C7 n4 l, @+ j. f
break;
}
return status;
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
回复:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发