最全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:1 @0 O& A& Q- E) g) b. E# [* D
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev){' T- V% L$ Z+ s% P1 l: N3 S/ u
GPIO_InitTypeDef GPIO_InitStructure;9 E& S6 D. _; @* y& g+ Z
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);3 }: o6 X) v5 b! Y
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | * j; o$ s. v' C4 c0 l% k
GPIO_Pin_13 |
GPIO_Pin_14 | @' }; T( k7 v" A; I! c8 p' h4 L
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) ; 8 I* i2 \, b3 U. a/ e9 X( | M
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; 2 m2 ?- p5 S1 r: ^- [6 G+ r
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) ; 8 w% A. v' |: W# ^
//-----------------------------------------------------------------% c; _% m( `2 j
//Vbus_EN 根据实际电路修改!
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE , ENABLE) ; 4 a i+ K- I* X
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);! K# R- {6 m2 x- A& x T* k/ ~4 l
' q( X; q/ H- {
GPIO_ResetBits(GPIOE, GPIO_Pin_0);9 i8 P" h4 ?5 H/ P& s/ `7 y
//------------------------------------------------------------------- Q0 M- k- Z9 |6 o2 U: J; r
USB_OTG_BSP_TimeInit();( T, N/ J4 z- ~ L& n
USB_OTG_BSP_mDelay(500); //Delay is need for stabilising the Vbus Low - \$ r" x5 Z: Z3 |$ E2 _
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
#ifndef USE_USB_OTG_HS
#define USE_USB_OTG_HS) \& f, u5 _" o: N5 h* A6 A
#endif /* USE_USB_OTG_HS */
#ifndef USE_ULPI_PHY
//#define USE_ULPI_PHY0 T% B. z0 b2 X# U; ]! l
#endif /* USE_ULPI_PHY */# A8 d9 w) J' u$ [/ L
#ifndef USE_EMBEDDED_PHY- }1 o' s/ c! C5 r% ?
#define USE_EMBEDDED_PHY" E- a) l8 K3 }' U" W3 b" n
#endif /* USE_EMBEDDED_PHY */
#ifdef USE_USB_OTG_HS
#define USB_OTG_HS_CORE
#endif
/*******************************************************************************
* FIFO Size Configuration in Host mode6 y4 Y' i$ g3 s2 g
* " H! S2 [7 b( G& G
* (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 g7 l# E! G7 d7 Y$ T
* channels are enabled* n5 t+ X, v% `4 v
*
* (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 9 c! n. h! N W% h( K
* that channel.1 s1 c) }6 ?6 X! K- l' l
*******************************************************************************/
/****************** USB OTG HS CONFIGURATION **********************************/
#ifdef USB_OTG_HS_CORE6 ~6 k1 W' }: u0 j, n# r4 ?: b2 R+ L
#define RX_FIFO_HS_SIZE 512
#define TXH_NP_HS_FIFOSIZ 2561 W' {: z8 L2 j0 Q2 {+ j
#define TXH_P_HS_FIFOSIZ 2565 \7 Z& L) [$ \9 g/ J
// #define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT. W& A! B+ K3 a P
// #define USB_OTG_HS_SOF_OUTPUT_ENABLED1 s+ ?, ^) S$ T; o: T, }
#ifdef USE_ULPI_PHY8 l) h7 b% g% W* y# F& z* n
#define USB_OTG_ULPI_PHY_ENABLED2 i. B! r( k2 P. P# y7 Y5 a
#endif6 S) j3 D! q. e6 [/ N
#ifdef USE_EMBEDDED_PHY
#define USB_OTG_EMBEDDED_PHY_ENABLED! E% w) \8 |4 n0 M% H8 B
#endif
#define USB_OTG_HS_INTERNAL_DMA_ENABLED
#define USB_OTG_EXTERNAL_VBUS_ENABLED
//#define USB_OTG_INTERNAL_VBUS_ENABLED
#endif% L* A+ y' r* c8 L. Z- n _5 P
/****************** USB OTG FS CONFIGURATION **********************************/( s4 b. q' Q0 R- c2 a
#ifdef USB_OTG_FS_CORE' v. E2 P4 _, ?$ h0 {
#define RX_FIFO_FS_SIZE 1281 r$ E- ~& K$ ~7 n' a
#define TXH_NP_FS_FIFOSIZ 96
#define TXH_P_FS_FIFOSIZ 96
// #define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT9 e, r( z- |4 s) j9 B( ~5 n) V. m( a
// #define USB_OTG_FS_SOF_OUTPUT_ENABLED
#endif
/****************** USB OTG MODE CONFIGURATION ********************************/3 d- {/ {9 K C* F
#define USE_HOST_MODE
//#define USE_DEVICE_MODE3 e- v. D9 P( b* [
//#define USE_OTG_MODE
#ifndef USB_OTG_FS_CORE9 a" }+ E" w( N
#ifndef USB_OTG_HS_CORE
#error "USB_OTG_HS_CORE or USB_OTG_FS_CORE should be defined"7 t. c% D! `" @+ Q) ]% Z; `# z2 r
#endif4 P- O. e; T# ^: E$ e
#endif# K0 a `( B2 ^' i# _
#ifndef USE_DEVICE_MODE2 t: C0 ^0 y) b0 B) `- u; p
#ifndef USE_HOST_MODE1 @5 c* P( F+ Y! {
#error "USE_DEVICE_MODE or USE_HOST_MODE should be defined"2 r, m8 { h5 N' c" w' T$ X
#endif
#endif2 F: A1 H6 h. v+ W# l9 U: A: ^
#ifndef USE_USB_OTG_HS
#ifndef USE_USB_OTG_FS0 M9 i7 R! n5 p3 l4 J
#error "USE_USB_OTG_HS or USE_USB_OTG_FS should be defined"
#endif
#else //USE_USB_OTG_HS4 x# K- B# T' V+ r* _; d
#ifndef USE_ULPI_PHY. i9 u0 Y: x1 D3 L
#ifndef USE_EMBEDDED_PHY4 S6 J0 m) @3 z9 u+ k; \
#error "USE_ULPI_PHY or USE_EMBEDDED_PHY should be defined"
#endif1 p9 L: f J# }! g& ]+ G6 t% ?
#endif3 ^2 Y% X! G. y4 O2 Z' f* B
#endif
/****************** C Compilers dependant keywords ****************************/, G* r' Z$ |3 M! s7 l+ P+ k
/* In HS mode and when the DMA is used, all variables and data structures dealing5 h8 ? J0 o6 P+ X/ l$ T- i, {' [
with the DMA during the transaction process should be 4-bytes aligned */ - m7 R+ M5 C2 P' H$ h+ Z
#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED& F& _2 s; E4 c/ A4 f% B8 g7 ~
#if defined (__GNUC__) /* GNU Compiler */" C1 S) W9 r; b9 V3 S/ T
#define __ALIGN_END __attribute__ ((aligned (4)))2 Q, e# F" P9 u- S; m6 U9 l
#define __ALIGN_BEGIN & ]" V$ R) p* T$ E- J& V8 x- m/ n, S4 b
#else
#define __ALIGN_END6 J( X! z" q1 n9 C( s
#if defined (__CC_ARM) /* ARM Compiler */$ `# k3 U, M9 }0 u! s, v/ p* q
#define __ALIGN_BEGIN __align(4)
#elif defined (__ICCARM__) /* IAR Compiler */
#define __ALIGN_BEGIN 6 \' Q% z; @ }$ T. E
#elif defined (__TASKING__) /* TASKING Compiler */9 z. \2 t& e) J2 O! N2 p" j
#define __ALIGN_BEGIN __align(4) ! V; B1 x' z9 w" M2 r- {
#endif /* __CC_ARM */ 2 N5 S0 o" d2 o1 o- O; h* @3 w
#endif /* __GNUC__ */
#else
#define __ALIGN_BEGIN
#define __ALIGN_END
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */6 \9 o' N! W8 n3 v M- Q1 Y( m! q
/* __packed keyword used to decrease the data type alignment to 1-byte */' ^. B; a; z0 {. V4 d
#if defined (__CC_ARM) /* ARM Compiler */
#define __packed __packed
#elif defined (__ICCARM__) /* IAR Compiler */" Q% ^, {* Y9 w# K2 p6 E
#define __packed __packed1 P" }5 L: f3 T/ p$ o) C3 }
#elif defined ( __GNUC__ ) /* GNU Compiler */ 5 x' L9 d+ z5 a V
#define __packed __attribute__ ((__packed__))( R% G# i0 q# [; ?' O8 z
#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" i. i" |. \/ h2 }: x, j1 K8 S
system_stm32fxxx.c file
*/
/* Init Host Library */
USBH_Init(&USB_OTG_Core,
USB_OTG_HS_CORE_ID,9 f8 u# m5 }3 Y/ O3 f$ R
&USB_Host,2 R- a6 o" L1 \5 U9 ^% Q
&USBH_MSC_cb,
&USR_cb);
while (1){
/* Host Task handler */
USBH_Process(&USB_OTG_Core, &USB_Host);
if(iapRun==0){
IAP_URN_UserProgrammer();
}" H! K' L* Z! Q$ |1 y
}4 }4 ]' J* D5 ~- k6 [
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
case HOST_USR_INPUT:
/*The function should return user response true to move to class state */8 F9 P1 l' C3 |2 \$ S: U- L: R
//修改:添加MSC-HID自动判别,然后再初始化CALSS-CB! [* _1 N# h& Z$ H6 V
if(JB_MSC_HID(phost,0)){ //0:判别MSC,1:判别HID
SetHOST_USB_MODE(0);) q& j: b- R7 W3 p$ f
phost->class_cb=&USBH_MSC_cb;
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){0 T( w& W& P7 }" `! p- E6 l3 s3 v
phost->gState = HOST_CLASS_REQUEST; / ^# w+ D$ B6 d) ]! h1 i% s
} ' y2 I( x- E) ^6 x2 X) W( G& z. ~; t
}else{
if(JB_MSC_HID(phost,1)){ //0:判别MSC,1:判别HID
SetHOST_USB_MODE(1);( O9 y$ c7 B% j6 p" E
phost->class_cb=&HID_cb;
//phost->usr_cb=&USR_Callbacks;//???????
if((phost->class_cb->Init(pdev, phost)) == USBH_OK){
phost->gState = HOST_CLASS_REQUEST;
} , S$ q6 s1 a/ O( U5 |
}8 n2 Y7 N- Z8 M' C( b W
} # \0 e' P+ x8 }7 O- K: ^
break;
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
/* Check for enumeration status */ & D/ {& t9 z* @% E7 u R
if ( USBH_HandleEnum(pdev , phost) == USBH_OK){ ( X I# t% A# q3 i% ^% |
/* The function shall return USBH_OK when full enumeration is complete *// U% r2 g/ `& W3 v
) h, s m1 k% p: r. }5 y7 J
/* user callback for end of device basic enumeration */$ G# [" @5 T1 u- j
phost->usr_cb->EnumerationDone();8 j. D- T# s1 `. P9 _4 C5 e7 q( }. |
4 A* t: e! N" e& V& F0 }
phost->gState = HOST_USR_INPUT;
}/ C# N Z' k6 }( y; X9 z
break;
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
//修改:添加MSC-HID自动判别,然后再初始化CALSS-CB: Y# x9 u8 N8 E
unsigned char JB_MSC_HID(USBH_HOST *phost,unsigned char flag){//0:判别MSC,1:判别HID1 V8 j5 P+ |& v! K
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;% u' z- N; W# O. b* _' [
}' }( Q; N7 T, d( y
}else{0 D! _ Y6 p$ A
if(phost->device_prop.Itf_Desc[0].bInterfaceSubClass == HID_BOOT_CODE) return 1;& e# m, @; O! _& t8 x: i8 ~
}
return 0;; Y9 t8 {4 p: G. K- Z
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
/**: m6 U3 ]8 j# H; }
* @brief USBH_CtlReq T5 F! v/ f3 z
* USBH_CtlReq sends a control request and provide the status after
* completion of the request" r1 x. F+ R7 u* C8 B; [$ u
* @param pdev: Selected device
* @param req: Setup Request Structure
* @param buff: data buffer address to store the response
* @param length: length of the response5 d" a) ~/ N/ n& O. K
* @retval Status
*/- O0 D7 @. ]2 r' C: z. a
USBH_Status USBH_CtlReq (USB_OTG_CORE_HANDLE *pdev,
USBH_HOST *phost, 8 I2 n' ^# n0 `" P2 D3 r" u
uint8_t *buff,: x, v9 w% t0 `$ w" i% K
uint16_t length)
{
USBH_Status status;& j" t$ r$ H$ {+ f. }: x6 O/ T
status = USBH_BUSY;: M/ ^8 X3 G# ~ T& K
' c/ c* d% d( g7 [" x$ c8 `0 {
switch (phost->RequestState)
{8 z- i: B# V1 `% u+ L }
case CMD_SEND:
/* Start a SETUP transfer */
USBH_SubmitSetupRequest(phost, buff, length);
phost->RequestState = CMD_WAIT;5 W y3 U% r- j8 u8 X, k" l
status = USBH_BUSY;
break;1 i1 A# n2 u0 Y3 ~0 f
6 g2 Z7 X7 U$ y) b
case CMD_WAIT:- y# r+ m5 c1 ], u
if(Get_HOST_USB_MODE()==0){ //--2014-3-30 添加2种模式判别!!!
if (status == URB_DONE){6 P( N) T* d0 o! I. e, I( t
/* Commands successfully sent and Response Received */
phost->RequestState = CMD_SEND;+ {- c! K) Z; W
status = USBH_OK;
}. [+ P8 l( E" p
else if (status == URB_ERROR){/ }0 x: r a. ]* w
/* Failure Mode */
phost->RequestState = CMD_SEND;1 v6 |9 Y- [& @$ T. J3 P
status = USBH_FAIL;* ]0 G* {+ E8 @! H" Z; O5 }
}
else if (status == URB_STALL){
/* Commands successfully sent and Response Received */
phost->RequestState = CMD_SEND;
status = USBH_NOT_SUPPORTED;
}3 ^3 b( p& y! u r9 b7 Y
}else{
if (phost->Control.state == CTRL_COMPLETE ){//HID 处理!!* d5 t& z- Y: ]
// Commands successfully sent and Response Received
phost->RequestState = CMD_SEND;
phost->Control.state =CTRL_IDLE; ; r' z+ g- T7 I' {
status = USBH_OK;
}$ X) F! R. @7 @8 T/ s" E! Z
else if (phost->Control.state == CTRL_ERROR)/ W0 h6 u3 P. R+ r* R! }
{
// Failure Mode
phost->RequestState = CMD_SEND;
status = USBH_FAIL;/ r2 \# N; a( [" s1 M
} 0 ]) ~% J, ~' Q% e: h
else if (phost->Control.state == CTRL_STALLED ). X8 {0 [7 l( E7 S2 x
{+ \- o0 k Y/ D8 A7 t U. U8 \+ h( f8 p
// Commands successfully sent and Response Received
phost->RequestState = CMD_SEND;; n- F* i3 ?, ]
status = USBH_NOT_SUPPORTED;' ~( U: L5 |. C- f6 u3 t! m$ `7 p9 ^
}& r3 Y2 { a+ s
}
break;
//-------------------------------------* v% h3 i; g5 I, I/ h
default:
break; ; Z& ?# p# v: V1 ~
}
return status;
}
RE:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发
回复:【MCU实战经验】STM32F207的host USB的MSC和HID的合并开发