1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
28
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
156
171
172
175
176
181
185
186
191
195
196
201
205
206
211
215
216
217
222
226
227
234
240
243
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
297
302
303
308
313
314
319
324
325
330
354
355
360
365
366
372
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
412
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
483
484
485
486
487
488
489
490
491
492
493
494
495
496
508
509
510
511
521
522
523
524
525
526
533
545
546
552
553
554
555
556
560
564
565
566
567
572
/* ... */
#include "stm32f4xx_hal.h"
#include "usbh_core.h"
#include "stm324x9i_eval_io.h"
HCD_HandleTypeDef hhcd;
/* ... */
/* ... */
void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
{
GPIO_InitTypeDef GPIO_InitStruct;
BSP_IO_Init();
if(hhcd->Instance == USB_OTG_FS)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
BSP_IO_ConfigPin(OTG_FS1_POWER_SWITCH_PIN, IO_MODE_OUTPUT);
}if (hhcd->Instance == USB_OTG_FS) { ... }
else if(hhcd->Instance == USB_OTG_HS)
{
#ifdef USE_USB_HS_IN_FS
BSP_IO_ConfigPin(OTG_FS2_POWER_SWITCH_PIN, IO_MODE_OUTPUT);
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF12_OTG_HS_FS;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* ... */
#else
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |\
GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();/* ... */
#endif
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
HAL_NVIC_SetPriority(OTG_HS_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
}else if (hhcd->Instance == USB_OTG_HS) { ... }
}{ ... }
/* ... */
void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
{
if(hhcd->Instance == USB_OTG_FS)
{
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
}if (hhcd->Instance == USB_OTG_FS) { ... }
else if(hhcd->Instance == USB_OTG_HS)
{
__HAL_RCC_USB_OTG_HS_CLK_DISABLE();
__HAL_RCC_USB_OTG_HS_ULPI_CLK_DISABLE();
}else if (hhcd->Instance == USB_OTG_HS) { ... }
}{ ... }
/* ... */
/* ... */
void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_IncTimer(hhcd->pData);
}{ ... }
/* ... */
void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_Connect(hhcd->pData);
}{ ... }
/* ... */
void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_Disconnect(hhcd->pData);
}{ ... }
/* ... */
void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_PortEnabled(hhcd->pData);
}{ ... }
/* ... */
void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_PortDisabled(hhcd->pData);
}{ ... }
/* ... */
void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
{
#if (USBH_USE_OS == 1)
USBH_LL_NotifyURBChange(hhcd->pData);
#endif
}{ ... }
/* ... */
/* ... */
USBH_StatusTypeDef USBH_LL_Init(USBH_HandleTypeDef *phost)
{
#ifdef USE_USB_FS
hhcd.Instance = USB_OTG_FS;
hhcd.Init.Host_channels = 11;
hhcd.Init.dma_enable = 0;
hhcd.Init.low_power_enable = 0;
hhcd.Init.phy_itface = HCD_PHY_EMBEDDED;
hhcd.Init.Sof_enable = 0;
hhcd.Init.speed = HCD_SPEED_FULL;
hhcd.pData = phost;
phost->pData = &hhcd;
HAL_HCD_Init(&hhcd);/* ... */
#endif
#ifdef USE_USB_HS
hhcd.Instance = USB_OTG_HS;
hhcd.Init.Host_channels = 11;
hhcd.Init.dma_enable = 1;
hhcd.Init.low_power_enable = 0;
#ifdef USE_USB_HS_IN_FS
hhcd.Init.phy_itface = HCD_PHY_EMBEDDED;
#else
hhcd.Init.phy_itface = HCD_PHY_ULPI;
#endif
hhcd.Init.Sof_enable = 0;
hhcd.Init.speed = HCD_SPEED_HIGH;
hhcd.Init.use_external_vbus = 1;
hhcd.pData = phost;
phost->pData = &hhcd;
HAL_HCD_Init(&hhcd);
/* ... */
#endif
USBH_LL_SetTimer(phost, HAL_HCD_GetCurrentFrame(&hhcd));
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_DeInit(USBH_HandleTypeDef *phost)
{
HAL_HCD_DeInit(phost->pData);
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_Start(USBH_HandleTypeDef *phost)
{
HAL_HCD_Start(phost->pData);
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_Stop(USBH_HandleTypeDef *phost)
{
HAL_HCD_Stop(phost->pData);
return USBH_OK;
}{ ... }
/* ... */
USBH_SpeedTypeDef USBH_LL_GetSpeed(USBH_HandleTypeDef *phost)
{
USBH_SpeedTypeDef speed = USBH_SPEED_FULL;
switch (HAL_HCD_GetCurrentSpeed(phost->pData))
{
case 0:
speed = USBH_SPEED_HIGH;
break;
case 0:
case 1:
speed = USBH_SPEED_FULL;
break;
case 1:
case 2:
speed = USBH_SPEED_LOW;
break;
case 2:
default:
speed = USBH_SPEED_FULL;
break; default
}switch (HAL_HCD_GetCurrentSpeed(phost->pData)) { ... }
return speed;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_ResetPort (USBH_HandleTypeDef *phost)
{
HAL_HCD_ResetPort(phost->pData);
return USBH_OK;
}{ ... }
/* ... */
uint32_t USBH_LL_GetLastXferSize(USBH_HandleTypeDef *phost, uint8_t pipe)
{
return HAL_HCD_HC_GetXferCount(phost->pData, pipe);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_OpenPipe(USBH_HandleTypeDef *phost,
uint8_t pipe,
uint8_t epnum,
uint8_t dev_address,
uint8_t speed,
uint8_t ep_type,
uint16_t mps)
{
HAL_HCD_HC_Init(phost->pData,
pipe,
epnum,
dev_address,
speed,
ep_type,
mps);
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_ClosePipe(USBH_HandleTypeDef *phost, uint8_t pipe)
{
HAL_HCD_HC_Halt(phost->pData, pipe);
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_SubmitURB(USBH_HandleTypeDef *phost,
uint8_t pipe,
uint8_t direction,
uint8_t ep_type,
uint8_t token,
uint8_t* pbuff,
uint16_t length,
uint8_t do_ping)
{
HAL_HCD_HC_SubmitRequest(phost->pData,
pipe,
direction,
ep_type,
token,
pbuff,
length,
do_ping);
return USBH_OK;
}{ ... }
/* ... */
USBH_URBStateTypeDef USBH_LL_GetURBState(USBH_HandleTypeDef *phost, uint8_t pipe)
{
return (USBH_URBStateTypeDef)HAL_HCD_HC_GetURBState (phost->pData, pipe);
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_DriverVBUS(USBH_HandleTypeDef *phost, uint8_t state)
{
#ifdef USE_USB_FS
if(state == 0)
{
BSP_IO_WritePin(OTG_FS1_POWER_SWITCH_PIN, RESET);
}if (state == 0) { ... }
else
{
BSP_IO_WritePin(OTG_FS1_POWER_SWITCH_PIN, SET);
}else { ... }
/* ... */
#endif
#ifdef USE_USB_HS_IN_FS
if(state == 0)
{
BSP_IO_WritePin(OTG_FS2_POWER_SWITCH_PIN, RESET);
}if (state == 0) { ... }
else
{
BSP_IO_WritePin(OTG_FS2_POWER_SWITCH_PIN, SET);
}else { ... } /* ... */
#endif
HAL_Delay(200);
return USBH_OK;
}{ ... }
/* ... */
USBH_StatusTypeDef USBH_LL_SetToggle(USBH_HandleTypeDef *phost, uint8_t pipe, uint8_t toggle)
{
if(hhcd.hc[pipe].ep_is_in)
{
hhcd.hc[pipe].toggle_in = toggle;
}if (hhcd.hc[pipe].ep_is_in) { ... }
else
{
hhcd.hc[pipe].toggle_out = toggle;
}else { ... }
return USBH_OK;
}{ ... }
/* ... */
uint8_t USBH_LL_GetToggle(USBH_HandleTypeDef *phost, uint8_t pipe)
{
uint8_t toggle = 0;
if(hhcd.hc[pipe].ep_is_in)
{
toggle = hhcd.hc[pipe].toggle_in;
}if (hhcd.hc[pipe].ep_is_in) { ... }
else
{
toggle = hhcd.hc[pipe].toggle_out;
}else { ... }
return toggle;
}{ ... }
/* ... */
void USBH_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}{ ... }