HPM SDK
HPMicro Software Development Kit
hpm_usb_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_USB_DRV_H
9 #define HPM_USB_DRV_H
10 /*---------------------------------------------------------------------
11  * Includes
12  *---------------------------------------------------------------------
13  */
14 #include "hpm_common.h"
15 #include "hpm_usb_regs.h"
16 #include "hpm_soc_feature.h"
17 
25 /*---------------------------------------------------------------------
26  * Macro Constant Declarations
27  *---------------------------------------------------------------------
28  */
29 #define USB_PHY_INIT_DELAY_COUNT (16U)
30 #define USB_HOST_FRAMELIST_SIZE (8U)
32 /*---------------------------------------------------------------------
33  * Macro Enum Declarations
34  *---------------------------------------------------------------------
35  */
39 typedef enum {
42  usb_dir_in_mask = 0x80
44 
48 typedef enum {
54 
58 typedef enum {
63 
67 typedef enum {
70  usb_line_state2 = 2
72 
76 typedef enum {
78  usb_tran_serial = 1
80 /*---------------------------------------------------------------------
81  * Structure Declarations
82  *---------------------------------------------------------------------
83  */
87 typedef struct __attribute__ ((packed)) {
88  union {
89  struct __attribute__ ((packed)) {
90  uint8_t recipient : 5;
91  uint8_t type : 2;
92  uint8_t direction : 1;
93  } bmRequestType_bit;
94 
95  uint8_t bmRequestType;
96  };
97 
98  uint8_t bRequest;
99  uint16_t wValue;
100  uint16_t wIndex;
101  uint16_t wLength;
103 
107 typedef struct {
108  uint8_t xfer;
109  uint8_t ep_addr;
110  uint16_t max_packet_size;
112 
113 #if defined __cplusplus
114 extern "C" {
115 #endif /* __cplusplus */
116 
117 /*---------------------------------------------------------------------
118  * Common API
119  *---------------------------------------------------------------------
120  */
121 
128 static inline uint32_t usb_get_interrupts(USB_Type *ptr)
129 {
130  return ptr->USBINTR;
131 }
132 
139 static inline void usb_enable_interrupts(USB_Type *ptr, uint32_t mask)
140 {
141  ptr->USBINTR |= mask;
142 }
143 
150 static inline uint32_t usb_get_status_flags(USB_Type *ptr)
151 {
152  return ptr->USBSTS;
153 }
154 
164 static inline void usb_clear_status_flags(USB_Type *ptr, uint32_t mask)
165 {
166  ptr->USBSTS = mask;
167 }
168 
175 static inline uint8_t usb_get_suspend_status(USB_Type *ptr)
176 {
177  return USB_PORTSC1_SUSP_GET(ptr->PORTSC1);
178 }
179 
186 static inline bool usb_get_port_reset_status(USB_Type *ptr)
187 {
188  return USB_PORTSC1_PR_GET(ptr->PORTSC1);
189 }
190 
197 static inline bool usb_get_port_ccs(USB_Type *ptr)
198 {
199  return USB_PORTSC1_CCS_GET(ptr->PORTSC1);
200 }
201 
208 static inline uint8_t usb_get_port_speed(USB_Type *ptr)
209 {
210  return USB_PORTSC1_PSPD_GET(ptr->PORTSC1);
211 }
212 
218 void usb_phy_init(USB_Type *ptr);
219 
225 static inline uint8_t usb_phy_get_line_state(USB_Type *ptr)
226 {
228 }
229 
235 static inline void usb_phy_using_internal_vbus(USB_Type *ptr)
236 {
239 }
240 
246 static inline void usb_phy_using_external_vbus(USB_Type *ptr)
247 {
250 }
251 
257 static inline void usb_phy_disable_dp_dm_pulldown(USB_Type *ptr)
258 {
259  ptr->PHY_CTRL0 |= 0x001000E0u;
260 }
261 
267 static inline void usb_phy_enable_dp_dm_pulldown(USB_Type *ptr)
268 {
269  ptr->PHY_CTRL0 &= ~0x001000E0u;
270 }
271 
272 /*---------------------------------------------------------------------
273  * Device API
274  *---------------------------------------------------------------------
275  */
276 
283 void usb_dcd_bus_reset(USB_Type *ptr, uint16_t ep0_max_packet_size);
284 
290 void usb_dcd_init(USB_Type *ptr);
291 
297 void usb_dcd_deinit(USB_Type *ptr);
298 
305 
313 
320 uint8_t usb_dcd_edpt_get_type(USB_Type *ptr, uint8_t ep_addr);
321 
328 void usb_dcd_edpt_xfer(USB_Type *ptr, uint8_t ep_idx);
329 
336 void usb_dcd_edpt_stall(USB_Type *ptr, uint8_t ep_addr);
337 
344 void usb_dcd_edpt_clear_stall(USB_Type *ptr, uint8_t ep_addr);
345 
353 bool usb_dcd_edpt_check_stall(USB_Type *ptr, uint8_t ep_addr);
354 
361 void usb_dcd_edpt_close(USB_Type *ptr, uint8_t ep_addr);
362 
368 void usb_dcd_connect(USB_Type *ptr);
369 
375 void usb_dcd_disconnect(USB_Type *ptr);
376 
383 static inline uint32_t usb_dcd_get_edpt_setup_status(USB_Type *ptr)
384 {
385  return ptr->ENDPTSETUPSTAT;
386 }
387 
394 static inline void usb_dcd_clear_edpt_setup_status(USB_Type *ptr, uint32_t mask)
395 {
396  ptr->ENDPTSETUPSTAT = mask;
397 }
398 
405 static inline void usb_dcd_set_address(USB_Type *ptr, uint8_t dev_addr)
406 {
408 }
409 
416 static inline void usb_dcd_set_edpt_list_addr(USB_Type *ptr, uint32_t addr)
417 {
419 }
420 
427 static inline uint8_t usb_dcd_get_device_addr(USB_Type *ptr)
428 {
430 }
431 
438 static inline uint32_t usb_dcd_get_edpt_complete_status(USB_Type *ptr)
439 {
440  return ptr->ENDPTCOMPLETE;
441 }
442 
449 static inline void usb_dcd_clear_edpt_complete_status(USB_Type *ptr, uint32_t mask)
450 {
451  ptr->ENDPTCOMPLETE = mask;
452 }
453 
454 /*---------------------------------------------------------------------
455  * Host API
456  *---------------------------------------------------------------------
457  */
465 bool usb_hcd_init(USB_Type *ptr, uint32_t int_mask, uint16_t framelist_size);
466 
472 void usb_hcd_port_reset(USB_Type *ptr);
473 
480 static inline void usb_hcd_set_command(USB_Type *ptr, uint32_t mask)
481 {
482  ptr->USBCMD |= mask;
483 }
484 
491 static inline uint32_t usb_hcd_get_frame_index(USB_Type *ptr)
492 {
493  return ptr->FRINDEX;
494 }
495 
502 static inline bool usb_hcd_get_port_csc(USB_Type *ptr)
503 {
504  return USB_PORTSC1_CSC_GET(ptr->PORTSC1);
505 }
506 
513 static inline void usb_hcd_set_power_ctrl_polarity(USB_Type *ptr, bool high)
514 {
515  if (high) {
517  } else {
519  }
520 }
521 
527 static inline void usb_hcd_enable_port_power(USB_Type *ptr)
528 {
530 }
531 
538 static inline void usb_hcd_set_async_list_addr(USB_Type *ptr, uint32_t addr)
539 {
541 }
542 
549 static inline void usb_hcd_set_periodic_list_addr(USB_Type *ptr, uint32_t addr)
550 {
552 }
553 
559 static inline void usb_hcd_run(USB_Type *ptr)
560 {
561  ptr->USBCMD |= USB_USBCMD_RS_MASK;
562 }
563 
569 static inline void usb_hcd_stop(USB_Type *ptr)
570 {
571  ptr->USBCMD &= ~USB_USBCMD_RS_MASK;
572 }
573 
574 #if defined __cplusplus
575 }
576 #endif /* __cplusplus */
577 
579 #endif /* HPM_USB_DRV_H */
bool usb_hcd_init(USB_Type *ptr, uint32_t int_mask, uint16_t framelist_size)
Initialize controller to host mode.
Definition: hpm_usb_drv.c:293
static bool usb_get_port_reset_status(USB_Type *ptr)
Get USB reset status.
Definition: hpm_usb_drv.h:186
static uint8_t usb_get_port_speed(USB_Type *ptr)
Get USB port speed status.
Definition: hpm_usb_drv.h:208
void usb_dcd_edpt_close(USB_Type *ptr, uint8_t ep_addr)
Close a specified endpoint.
Definition: hpm_usb_drv.c:261
static uint8_t usb_get_suspend_status(USB_Type *ptr)
Get USB suspend status.
Definition: hpm_usb_drv.h:175
static bool usb_hcd_get_port_csc(USB_Type *ptr)
Get port connect status change.
Definition: hpm_usb_drv.h:502
void usb_dcd_connect(USB_Type *ptr)
Connect by enabling internal pull-up resistor on D+/D-.
Definition: hpm_usb_drv.c:177
void usb_dcd_edpt_stall(USB_Type *ptr, uint8_t ep_addr)
Stall endpoint.
Definition: hpm_usb_drv.c:235
static uint32_t usb_hcd_get_frame_index(USB_Type *ptr)
Get frame index.
Definition: hpm_usb_drv.h:491
static void usb_hcd_set_power_ctrl_polarity(USB_Type *ptr, bool high)
Set power ctrl polarity.
Definition: hpm_usb_drv.h:513
static uint32_t usb_dcd_get_edpt_setup_status(USB_Type *ptr)
Get setup status of endpoint.
Definition: hpm_usb_drv.h:383
void usb_dcd_deinit(USB_Type *ptr)
Deinitialize controller to device.
Definition: hpm_usb_drv.c:153
static void usb_hcd_set_command(USB_Type *ptr, uint32_t mask)
Initialize controller to host modeHost set command register.
Definition: hpm_usb_drv.h:480
static void usb_dcd_set_edpt_list_addr(USB_Type *ptr, uint32_t addr)
Set endpoint list address.
Definition: hpm_usb_drv.h:416
static uint32_t usb_get_status_flags(USB_Type *ptr)
Get all USB status flags.
Definition: hpm_usb_drv.h:150
void usb_dcd_bus_reset(USB_Type *ptr, uint16_t ep0_max_packet_size)
USB device bus reset.
Definition: hpm_usb_drv.c:84
static void usb_dcd_set_address(USB_Type *ptr, uint8_t dev_addr)
Set address.
Definition: hpm_usb_drv.h:405
static void usb_phy_using_external_vbus(USB_Type *ptr)
USB phy using external vbus.
Definition: hpm_usb_drv.h:246
static void usb_hcd_stop(USB_Type *ptr)
Stop hcd controller.
Definition: hpm_usb_drv.h:569
uint8_t usb_dcd_edpt_get_type(USB_Type *ptr, uint8_t ep_addr)
get a specified endpoint type
Definition: hpm_usb_drv.c:218
usb_line_state_t
USB line state.
Definition: hpm_usb_drv.h:67
static void usb_hcd_run(USB_Type *ptr)
Start hcd controller.
Definition: hpm_usb_drv.h:559
static void usb_hcd_set_periodic_list_addr(USB_Type *ptr, uint32_t addr)
Set periodic list address.
Definition: hpm_usb_drv.h:549
static void usb_phy_using_internal_vbus(USB_Type *ptr)
USB phy using internal vbus.
Definition: hpm_usb_drv.h:235
static uint32_t usb_dcd_get_edpt_complete_status(USB_Type *ptr)
Get complete status of endpoint.
Definition: hpm_usb_drv.h:438
static void usb_dcd_clear_edpt_complete_status(USB_Type *ptr, uint32_t mask)
Clear complete status of endpoint.
Definition: hpm_usb_drv.h:449
bool usb_dcd_edpt_check_stall(USB_Type *ptr, uint8_t ep_addr)
Clear stall.
Definition: hpm_usb_drv.c:253
static void usb_enable_interrupts(USB_Type *ptr, uint32_t mask)
Enable interrupts.
Definition: hpm_usb_drv.h:139
void usb_hcd_port_reset(USB_Type *ptr)
Initialize controller to host modeHost Reset port.
Definition: hpm_usb_drv.c:342
static uint32_t usb_get_interrupts(USB_Type *ptr)
Get the mask of all enabled interrupts.
Definition: hpm_usb_drv.h:128
void usb_dcd_edpt_xfer(USB_Type *ptr, uint8_t ep_idx)
Submit a transfer.
Definition: hpm_usb_drv.c:227
static bool usb_get_port_ccs(USB_Type *ptr)
Get USB current connect status.
Definition: hpm_usb_drv.h:197
void usb_dcd_edpt_open(USB_Type *ptr, usb_endpoint_config_t *config)
Open an endpoint.
Definition: hpm_usb_drv.c:206
static void usb_clear_status_flags(USB_Type *ptr, uint32_t mask)
Clear status flags.
Definition: hpm_usb_drv.h:164
void usb_dcd_disconnect(USB_Type *ptr)
Disconnect by disabling internal pull-up resistor on D+/D-.
Definition: hpm_usb_drv.c:183
static uint8_t usb_dcd_get_device_addr(USB_Type *ptr)
Get device address.
Definition: hpm_usb_drv.h:427
void usb_dcd_remote_wakeup(USB_Type *ptr)
Wakeup from host.
Definition: hpm_usb_drv.c:288
usb_transceiver_t
USB transceiver.
Definition: hpm_usb_drv.h:76
static void usb_hcd_set_async_list_addr(USB_Type *ptr, uint32_t addr)
Get port connect status changeSet async list address.
Definition: hpm_usb_drv.h:538
static void usb_phy_disable_dp_dm_pulldown(USB_Type *ptr)
USB phy disconnect dp/dm pins pulldown resistance.
Definition: hpm_usb_drv.h:257
usb_dir_t
USB transfer direction types.
Definition: hpm_usb_drv.h:39
usb_controller_mode_t
USB controller work modes.
Definition: hpm_usb_drv.h:58
usb_xfer_type_t
USB transfer types.
Definition: hpm_usb_drv.h:48
void usb_phy_init(USB_Type *ptr)
Initialize USB phy.
Definition: hpm_usb_drv.c:52
usb_control_request_t
Definition: hpm_usb_drv.h:102
static void usb_dcd_clear_edpt_setup_status(USB_Type *ptr, uint32_t mask)
Clear the setup status of all specified endpoints.
Definition: hpm_usb_drv.h:394
void usb_dcd_edpt_clear_stall(USB_Type *ptr, uint8_t ep_addr)
Clear stall.
Definition: hpm_usb_drv.c:243
static void usb_phy_enable_dp_dm_pulldown(USB_Type *ptr)
USB phy connect dp/dm pins pulldown resistance.
Definition: hpm_usb_drv.h:267
static void usb_hcd_enable_port_power(USB_Type *ptr)
Enable port power.
Definition: hpm_usb_drv.h:527
static uint8_t usb_phy_get_line_state(USB_Type *ptr)
USB phy get line status.
Definition: hpm_usb_drv.h:225
void usb_dcd_init(USB_Type *ptr)
Initialize controller to device mode.
Definition: hpm_usb_drv.c:112
@ usb_line_state1
Definition: hpm_usb_drv.h:69
@ usb_line_state2
Definition: hpm_usb_drv.h:70
@ usb_line_state0
Definition: hpm_usb_drv.h:68
@ usb_tran_serial
Definition: hpm_usb_drv.h:78
@ usb_tran_parallel
Definition: hpm_usb_drv.h:77
@ usb_dir_in_mask
Definition: hpm_usb_drv.h:42
@ usb_dir_in
Definition: hpm_usb_drv.h:41
@ usb_dir_out
Definition: hpm_usb_drv.h:40
@ usb_ctrl_mode_device
Definition: hpm_usb_drv.h:60
@ usb_ctrl_mode_host
Definition: hpm_usb_drv.h:61
@ usb_ctrl_mode_otg
Definition: hpm_usb_drv.h:59
@ usb_xfer_bulk
Definition: hpm_usb_drv.h:51
@ usb_xfer_interrupt
Definition: hpm_usb_drv.h:52
@ usb_xfer_isochronous
Definition: hpm_usb_drv.h:50
@ usb_xfer_control
Definition: hpm_usb_drv.h:49
#define USB_PORTSC1_CSC_GET(x)
Definition: hpm_usb_regs.h:1414
#define USB_PORTSC1_CCS_GET(x)
Definition: hpm_usb_regs.h:1430
#define USB_ASYNCLISTADDR_ASYBASE_MASK
Definition: hpm_usb_regs.h:922
#define USB_PHY_STATUS_LINE_STATE_GET(x)
Definition: hpm_usb_regs.h:2145
#define USB_PHY_CTRL0_VBUS_VALID_OVERRIDE_MASK
Definition: hpm_usb_regs.h:2066
#define USB_DEVICEADDR_USBADR_SET(x)
Definition: hpm_usb_regs.h:876
#define USB_PHY_CTRL0_VBUS_VALID_OVERRIDE_EN_MASK
Definition: hpm_usb_regs.h:2093
#define USB_USBCMD_RS_MASK
Definition: hpm_usb_regs.h:416
#define USB_PHY_CTRL0_SESS_VALID_OVERRIDE_MASK
Definition: hpm_usb_regs.h:2057
#define USB_OTG_CTRL0_OTG_POWER_MASK_MASK
Definition: hpm_usb_regs.h:2001
#define USB_PERIODICLISTBASE_BASEADR_MASK
Definition: hpm_usb_regs.h:907
#define USB_PORTSC1_PP_MASK
Definition: hpm_usb_regs.h:1234
#define USB_DEVICEADDR_USBADR_GET(x)
Definition: hpm_usb_regs.h:877
#define USB_PORTSC1_PSPD_GET(x)
Definition: hpm_usb_regs.h:1115
#define USB_DEVICEADDR_USBADRA_MASK
Definition: hpm_usb_regs.h:893
#define USB_PORTSC1_PR_GET(x)
Definition: hpm_usb_regs.h:1285
#define USB_ENDPTLISTADDR_EPBASE_MASK
Definition: hpm_usb_regs.h:934
#define USB_PHY_CTRL0_SESS_VALID_OVERRIDE_EN_MASK
Definition: hpm_usb_regs.h:2084
#define USB_PORTSC1_SUSP_GET(x)
Definition: hpm_usb_regs.h:1310
Definition: hpm_usb_regs.h:12
__RW uint32_t OTG_CTRL0
Definition: hpm_usb_regs.h:50
__RW uint32_t PHY_STATUS
Definition: hpm_usb_regs.h:56
__RW uint32_t FRINDEX
Definition: hpm_usb_regs.h:23
__RW uint32_t USBCMD
Definition: hpm_usb_regs.h:20
__RW uint32_t DEVICEADDR
Definition: hpm_usb_regs.h:26
__RW uint32_t ENDPTLISTADDR
Definition: hpm_usb_regs.h:31
__RW uint32_t PERIODICLISTBASE
Definition: hpm_usb_regs.h:27
__RW uint32_t USBINTR
Definition: hpm_usb_regs.h:22
__RW uint32_t ENDPTCOMPLETE
Definition: hpm_usb_regs.h:48
__RW uint32_t PORTSC1
Definition: hpm_usb_regs.h:40
__RW uint32_t PHY_CTRL0
Definition: hpm_usb_regs.h:52
__RW uint32_t ENDPTSETUPSTAT
Definition: hpm_usb_regs.h:44
__RW uint32_t USBSTS
Definition: hpm_usb_regs.h:21
__RW uint32_t ASYNCLISTADDR
Definition: hpm_usb_regs.h:30
Endpoint config structure.
Definition: hpm_usb_drv.h:107
uint8_t ep_addr
Definition: hpm_usb_drv.h:109
uint8_t xfer
Definition: hpm_usb_drv.h:108
uint16_t max_packet_size
Definition: hpm_usb_drv.h:110