File indexing completed on 2025-05-11 08:23:38
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef STM32H7xx_LL_USB_H
0021 #define STM32H7xx_LL_USB_H
0022
0023 #ifdef __cplusplus
0024 extern "C" {
0025 #endif
0026
0027
0028 #include "stm32h7xx_hal_def.h"
0029
0030 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #ifndef HAL_USB_TIMEOUT
0041 #define HAL_USB_TIMEOUT 0xF000000U
0042 #endif
0043
0044 #ifndef HAL_USB_CURRENT_MODE_MAX_DELAY_MS
0045 #define HAL_USB_CURRENT_MODE_MAX_DELAY_MS 200U
0046 #endif
0047
0048
0049
0050
0051
0052 typedef enum
0053 {
0054 USB_DEVICE_MODE = 0,
0055 USB_HOST_MODE = 1,
0056 USB_DRD_MODE = 2
0057 } USB_ModeTypeDef;
0058
0059
0060
0061
0062 typedef enum
0063 {
0064 URB_IDLE = 0,
0065 URB_DONE,
0066 URB_NOTREADY,
0067 URB_NYET,
0068 URB_ERROR,
0069 URB_STALL
0070 } USB_URBStateTypeDef;
0071
0072
0073
0074
0075 typedef enum
0076 {
0077 HC_IDLE = 0,
0078 HC_XFRC,
0079 HC_HALTED,
0080 HC_ACK,
0081 HC_NAK,
0082 HC_NYET,
0083 HC_STALL,
0084 HC_XACTERR,
0085 HC_BBLERR,
0086 HC_DATATGLERR
0087 } USB_HCStateTypeDef;
0088
0089
0090
0091
0092
0093 typedef struct
0094 {
0095 uint8_t dev_endpoints;
0096
0097
0098
0099 uint8_t Host_channels;
0100
0101
0102
0103 uint8_t dma_enable;
0104
0105
0106 uint8_t speed;
0107
0108
0109
0110 uint8_t ep0_mps;
0111
0112 uint8_t phy_itface;
0113
0114
0115 uint8_t Sof_enable;
0116
0117 uint8_t low_power_enable;
0118
0119 uint8_t lpm_enable;
0120
0121 uint8_t battery_charging_enable;
0122
0123 uint8_t vbus_sensing_enable;
0124
0125 uint8_t use_dedicated_ep1;
0126
0127 uint8_t use_external_vbus;
0128
0129 } USB_CfgTypeDef;
0130
0131 typedef struct
0132 {
0133 uint8_t num;
0134
0135
0136 uint8_t is_in;
0137
0138
0139 uint8_t is_stall;
0140
0141
0142 uint8_t is_iso_incomplete;
0143
0144
0145 uint8_t type;
0146
0147
0148 uint8_t data_pid_start;
0149
0150
0151 uint32_t maxpacket;
0152
0153
0154 uint8_t *xfer_buff;
0155
0156 uint32_t xfer_len;
0157
0158 uint32_t xfer_count;
0159
0160 uint8_t even_odd_frame;
0161
0162
0163 uint16_t tx_fifo_num;
0164
0165
0166 uint32_t dma_addr;
0167
0168 uint32_t xfer_size;
0169 } USB_EPTypeDef;
0170
0171 typedef struct
0172 {
0173 uint8_t dev_addr;
0174
0175
0176 uint8_t ch_num;
0177
0178
0179 uint8_t ep_num;
0180
0181
0182 uint8_t ep_is_in;
0183
0184
0185 uint8_t speed;
0186
0187
0188
0189 uint8_t do_ping;
0190 uint8_t do_ssplit;
0191 uint8_t do_csplit;
0192 uint8_t ep_ss_schedule;
0193 uint32_t iso_splt_xactPos;
0194
0195 uint8_t hub_port_nbr;
0196 uint8_t hub_addr;
0197
0198 uint8_t ep_type;
0199
0200
0201 uint16_t max_packet;
0202
0203
0204 uint8_t data_pid;
0205
0206
0207 uint8_t *xfer_buff;
0208
0209 uint32_t XferSize;
0210
0211 uint32_t xfer_len;
0212
0213 uint32_t xfer_count;
0214
0215 uint8_t toggle_in;
0216
0217
0218 uint8_t toggle_out;
0219
0220
0221 uint32_t dma_addr;
0222
0223 uint32_t ErrCnt;
0224 uint32_t NyetErrCnt;
0225
0226 USB_URBStateTypeDef urb_state;
0227
0228
0229 USB_HCStateTypeDef state;
0230
0231 } USB_HCTypeDef;
0232
0233 typedef USB_ModeTypeDef USB_OTG_ModeTypeDef;
0234 typedef USB_CfgTypeDef USB_OTG_CfgTypeDef;
0235 typedef USB_EPTypeDef USB_OTG_EPTypeDef;
0236 typedef USB_URBStateTypeDef USB_OTG_URBStateTypeDef;
0237 typedef USB_HCStateTypeDef USB_OTG_HCStateTypeDef;
0238 typedef USB_HCTypeDef USB_OTG_HCTypeDef;
0239
0240
0241
0242
0243
0244
0245
0246
0247 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0248
0249
0250
0251
0252 #define USB_OTG_CORE_ID_300A 0x4F54300AU
0253 #define USB_OTG_CORE_ID_310A 0x4F54310AU
0254
0255
0256
0257
0258
0259
0260
0261
0262 #define USB_OTG_MODE_DEVICE 0U
0263 #define USB_OTG_MODE_HOST 1U
0264 #define USB_OTG_MODE_DRD 2U
0265
0266
0267
0268
0269
0270
0271
0272
0273 #define USB_OTG_SPEED_HIGH 0U
0274 #define USB_OTG_SPEED_HIGH_IN_FULL 1U
0275 #define USB_OTG_SPEED_FULL 3U
0276
0277
0278
0279
0280
0281
0282
0283
0284 #define USB_OTG_ULPI_PHY 1U
0285 #define USB_OTG_EMBEDDED_PHY 2U
0286
0287
0288
0289
0290
0291
0292
0293
0294 #ifndef USBD_HS_TRDT_VALUE
0295 #define USBD_HS_TRDT_VALUE 9U
0296 #endif
0297 #ifndef USBD_FS_TRDT_VALUE
0298 #define USBD_FS_TRDT_VALUE 5U
0299 #define USBD_DEFAULT_TRDT_VALUE 9U
0300 #endif
0301
0302
0303
0304
0305
0306
0307
0308
0309 #define USB_OTG_HS_MAX_PACKET_SIZE 512U
0310 #define USB_OTG_FS_MAX_PACKET_SIZE 64U
0311 #define USB_OTG_MAX_EP0_SIZE 64U
0312
0313
0314
0315
0316
0317
0318
0319
0320 #define DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ (0U << 1)
0321 #define DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ (1U << 1)
0322 #define DSTS_ENUMSPD_FS_PHY_48MHZ (3U << 1)
0323
0324
0325
0326
0327
0328
0329
0330
0331 #define DCFG_FRAME_INTERVAL_80 0U
0332 #define DCFG_FRAME_INTERVAL_85 1U
0333 #define DCFG_FRAME_INTERVAL_90 2U
0334 #define DCFG_FRAME_INTERVAL_95 3U
0335
0336
0337
0338 #endif
0339
0340
0341
0342
0343 #define EP_MPS_64 0U
0344 #define EP_MPS_32 1U
0345 #define EP_MPS_16 2U
0346 #define EP_MPS_8 3U
0347
0348
0349
0350
0351
0352
0353
0354
0355 #define EP_TYPE_CTRL 0U
0356 #define EP_TYPE_ISOC 1U
0357 #define EP_TYPE_BULK 2U
0358 #define EP_TYPE_INTR 3U
0359 #define EP_TYPE_MSK 3U
0360
0361
0362
0363
0364
0365
0366
0367
0368 #define EP_SPEED_LOW 0U
0369 #define EP_SPEED_FULL 1U
0370 #define EP_SPEED_HIGH 2U
0371
0372
0373
0374
0375
0376
0377
0378
0379 #define HC_PID_DATA0 0U
0380 #define HC_PID_DATA2 1U
0381 #define HC_PID_DATA1 2U
0382 #define HC_PID_SETUP 3U
0383
0384
0385
0386
0387
0388
0389
0390
0391 #define USBD_HS_SPEED 0U
0392 #define USBD_HSINFS_SPEED 1U
0393 #define USBH_HS_SPEED 0U
0394 #define USBD_FS_SPEED 2U
0395 #define USBH_FSLS_SPEED 1U
0396
0397
0398
0399
0400 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0401
0402
0403
0404
0405 #define STS_GOUT_NAK 1U
0406 #define STS_DATA_UPDT 2U
0407 #define STS_XFER_COMP 3U
0408 #define STS_SETUP_COMP 4U
0409 #define STS_SETUP_UPDT 6U
0410
0411
0412
0413
0414
0415
0416
0417
0418 #define HCFG_30_60_MHZ 0U
0419 #define HCFG_48_MHZ 1U
0420 #define HCFG_6_MHZ 2U
0421
0422
0423
0424
0425
0426
0427
0428
0429 #define HFIR_6_MHZ 6000U
0430 #define HFIR_60_MHZ 60000U
0431 #define HFIR_48_MHZ 48000U
0432
0433
0434
0435
0436
0437
0438
0439
0440 #define HPRT0_PRTSPD_HIGH_SPEED 0U
0441 #define HPRT0_PRTSPD_FULL_SPEED 1U
0442 #define HPRT0_PRTSPD_LOW_SPEED 2U
0443
0444
0445
0446
0447 #define HCCHAR_CTRL 0U
0448 #define HCCHAR_ISOC 1U
0449 #define HCCHAR_BULK 2U
0450 #define HCCHAR_INTR 3U
0451
0452 #define GRXSTS_PKTSTS_IN 2U
0453 #define GRXSTS_PKTSTS_IN_XFER_COMP 3U
0454 #define GRXSTS_PKTSTS_DATA_TOGGLE_ERR 5U
0455 #define GRXSTS_PKTSTS_CH_HALTED 7U
0456
0457 #define CLEAR_INTERRUPT_MASK 0xFFFFFFFFU
0458
0459 #define HC_MAX_PKT_CNT 256U
0460 #define ISO_SPLT_MPS 188U
0461
0462 #define HCSPLT_BEGIN 1U
0463 #define HCSPLT_MIDDLE 2U
0464 #define HCSPLT_END 3U
0465 #define HCSPLT_FULL 4U
0466
0467 #define TEST_J 1U
0468 #define TEST_K 2U
0469 #define TEST_SE0_NAK 3U
0470 #define TEST_PACKET 4U
0471 #define TEST_FORCE_EN 5U
0472
0473 #define USBx_PCGCCTL *(__IO uint32_t *)((uint32_t)USBx_BASE + USB_OTG_PCGCCTL_BASE)
0474 #define USBx_HPRT0 *(__IO uint32_t *)((uint32_t)USBx_BASE + USB_OTG_HOST_PORT_BASE)
0475
0476 #define USBx_DEVICE ((USB_OTG_DeviceTypeDef *)(USBx_BASE + USB_OTG_DEVICE_BASE))
0477 #define USBx_INEP(i) ((USB_OTG_INEndpointTypeDef *)(USBx_BASE\
0478 + USB_OTG_IN_ENDPOINT_BASE + ((i) * USB_OTG_EP_REG_SIZE)))
0479
0480 #define USBx_OUTEP(i) ((USB_OTG_OUTEndpointTypeDef *)(USBx_BASE\
0481 + USB_OTG_OUT_ENDPOINT_BASE + ((i) * USB_OTG_EP_REG_SIZE)))
0482
0483 #define USBx_DFIFO(i) *(__IO uint32_t *)(USBx_BASE + USB_OTG_FIFO_BASE + ((i) * USB_OTG_FIFO_SIZE))
0484
0485 #define USBx_HOST ((USB_OTG_HostTypeDef *)(USBx_BASE + USB_OTG_HOST_BASE))
0486 #define USBx_HC(i) ((USB_OTG_HostChannelTypeDef *)(USBx_BASE\
0487 + USB_OTG_HOST_CHANNEL_BASE\
0488 + ((i) * USB_OTG_HOST_CHANNEL_SIZE)))
0489
0490
0491 #define EP_ADDR_MSK 0xFU
0492 #endif
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0503 #define USB_MASK_INTERRUPT(__INSTANCE__, __INTERRUPT__) ((__INSTANCE__)->GINTMSK &= ~(__INTERRUPT__))
0504 #define USB_UNMASK_INTERRUPT(__INSTANCE__, __INTERRUPT__) ((__INSTANCE__)->GINTMSK |= (__INTERRUPT__))
0505
0506 #define CLEAR_IN_EP_INTR(__EPNUM__, __INTERRUPT__) (USBx_INEP(__EPNUM__)->DIEPINT = (__INTERRUPT__))
0507 #define CLEAR_OUT_EP_INTR(__EPNUM__, __INTERRUPT__) (USBx_OUTEP(__EPNUM__)->DOEPINT = (__INTERRUPT__))
0508 #endif
0509
0510
0511
0512
0513
0514
0515
0516
0517 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0518 HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg);
0519 HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg);
0520 HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx);
0521 HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx);
0522 HAL_StatusTypeDef USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef *USBx, uint32_t hclk, uint8_t speed);
0523 HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_OTG_ModeTypeDef mode);
0524 HAL_StatusTypeDef USB_SetDevSpeed(const USB_OTG_GlobalTypeDef *USBx, uint8_t speed);
0525 HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx);
0526 HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num);
0527 HAL_StatusTypeDef USB_ActivateEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep);
0528 HAL_StatusTypeDef USB_DeactivateEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep);
0529 HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep);
0530 HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep);
0531 HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep, uint8_t dma);
0532 HAL_StatusTypeDef USB_WritePacket(const USB_OTG_GlobalTypeDef *USBx, uint8_t *src,
0533 uint8_t ch_ep_num, uint16_t len, uint8_t dma);
0534
0535 void *USB_ReadPacket(const USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len);
0536 HAL_StatusTypeDef USB_EPSetStall(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep);
0537 HAL_StatusTypeDef USB_EPClearStall(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep);
0538 HAL_StatusTypeDef USB_EPStopXfer(const USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep);
0539 HAL_StatusTypeDef USB_SetDevAddress(const USB_OTG_GlobalTypeDef *USBx, uint8_t address);
0540 HAL_StatusTypeDef USB_DevConnect(const USB_OTG_GlobalTypeDef *USBx);
0541 HAL_StatusTypeDef USB_DevDisconnect(const USB_OTG_GlobalTypeDef *USBx);
0542 HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx);
0543 HAL_StatusTypeDef USB_ActivateSetup(const USB_OTG_GlobalTypeDef *USBx);
0544 HAL_StatusTypeDef USB_EP0_OutStart(const USB_OTG_GlobalTypeDef *USBx, uint8_t dma, const uint8_t *psetup);
0545 uint8_t USB_GetDevSpeed(const USB_OTG_GlobalTypeDef *USBx);
0546 uint32_t USB_GetMode(const USB_OTG_GlobalTypeDef *USBx);
0547 uint32_t USB_ReadInterrupts(USB_OTG_GlobalTypeDef const *USBx);
0548 uint32_t USB_ReadChInterrupts(const USB_OTG_GlobalTypeDef *USBx, uint8_t chnum);
0549 uint32_t USB_ReadDevAllOutEpInterrupt(const USB_OTG_GlobalTypeDef *USBx);
0550 uint32_t USB_ReadDevOutEPInterrupt(const USB_OTG_GlobalTypeDef *USBx, uint8_t epnum);
0551 uint32_t USB_ReadDevAllInEpInterrupt(const USB_OTG_GlobalTypeDef *USBx);
0552 uint32_t USB_ReadDevInEPInterrupt(const USB_OTG_GlobalTypeDef *USBx, uint8_t epnum);
0553 void USB_ClearInterrupts(USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt);
0554
0555 HAL_StatusTypeDef USB_HostInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg);
0556 HAL_StatusTypeDef USB_InitFSLSPClkSel(const USB_OTG_GlobalTypeDef *USBx, uint8_t freq);
0557 HAL_StatusTypeDef USB_ResetPort(const USB_OTG_GlobalTypeDef *USBx);
0558 HAL_StatusTypeDef USB_DriveVbus(const USB_OTG_GlobalTypeDef *USBx, uint8_t state);
0559 uint32_t USB_GetHostSpeed(USB_OTG_GlobalTypeDef const *USBx);
0560 uint32_t USB_GetCurrentFrame(USB_OTG_GlobalTypeDef const *USBx);
0561 HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num,
0562 uint8_t epnum, uint8_t dev_address, uint8_t speed,
0563 uint8_t ep_type, uint16_t mps);
0564 HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx,
0565 USB_OTG_HCTypeDef *hc, uint8_t dma);
0566
0567 uint32_t USB_HC_ReadInterrupt(const USB_OTG_GlobalTypeDef *USBx);
0568 HAL_StatusTypeDef USB_HC_Halt(const USB_OTG_GlobalTypeDef *USBx, uint8_t hc_num);
0569 HAL_StatusTypeDef USB_DoPing(const USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num);
0570 HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx);
0571 HAL_StatusTypeDef USB_ActivateRemoteWakeup(const USB_OTG_GlobalTypeDef *USBx);
0572 HAL_StatusTypeDef USB_DeActivateRemoteWakeup(const USB_OTG_GlobalTypeDef *USBx);
0573 #endif
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590 #endif
0591
0592 #ifdef __cplusplus
0593 }
0594 #endif
0595
0596
0597 #endif