HPM SDK
HPMicro Software Development Kit
hpm_usb_device.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2025 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_USB_DEVICE_H
9 #define HPM_USB_DEVICE_H
10 
11 /*---------------------------------------------------------------------
12  * Includes
13  *---------------------------------------------------------------------
14  */
15 #include "hpm_usb_drv.h"
16 #include "hpm_soc_feature.h"
17 /*---------------------------------------------------------------------
18  * Macro Constant Declarations
19  *---------------------------------------------------------------------
20  */
21 
22 /*---------------------------------------------------------------------
23  * Macro Typedef Declaration
24  *---------------------------------------------------------------------
25  */
26 
27 /* Queue Transfer Descriptor */
28 typedef struct {
29  /* Word 0: Next QTD Pointer */
30  volatile uint32_t next; /* Next link pointer This field contains the physical memory address of the next dTD to be processed */
31 
32  /* Word 1: qTQ Token */
33  union {
34  volatile uint32_t token;
35  struct {
36  volatile uint32_t : 3;
37  volatile uint32_t xact_err : 1;
38  volatile uint32_t : 1;
39  volatile uint32_t buffer_err : 1;
40  volatile uint32_t halted : 1;
41  volatile uint32_t active : 1;
42  volatile uint32_t : 2;
43  volatile uint32_t iso_mult_override : 2; /* This field can be used for transmit ISOs to override the MULT field in the dQH. This field must be zero for all packet types that are not transmit-ISO. */
44  volatile uint32_t : 3;
45  volatile uint32_t int_on_complete : 1;
46  volatile uint32_t total_bytes : 15;
47  volatile uint32_t : 1;
48  };
49  };
50 
51  /* Word 2-6: Buffer Page Pointer List, Each element in the list is a 4K page aligned, physical memory address. The lower 12 bits in each pointer are reserved (except for the first one) as each memory pointer must reference the start of a 4K page */
52  volatile uint32_t buffer[USB_SOC_DCD_QHD_BUFFER_COUNT];
53 
54  /*------------- DCD Area -------------*/
55  volatile uint16_t expected_bytes;
56  volatile bool in_use;
57  volatile uint8_t reserved;
58 } dcd_qtd_t;
59 
60 /* Queue Head */
61 typedef struct {
62  /* Word 0: Capabilities and Characteristics */
63 
64  volatile uint32_t : 15 ; /* Number of packets executed per transaction descriptor 00 - Execute N transactions as demonstrated by the USB variable length protocol where N is computed using Max_packet_length and the Total_bytes field in the dTD. 01 - Execute one transaction 10 - Execute two transactions 11 - Execute three transactions Remark: Non-isochronous endpoints must set MULT = 00. Remark: Isochronous endpoints must set MULT = 01, 10, or 11 as needed. */
65  volatile uint32_t int_on_setup : 1 ; /* Interrupt on setup This bit is used on control type endpoints to indicate if USBINT is set in response to a setup being received. */
66  volatile uint32_t max_packet_size : 11 ; /* This directly corresponds to the maximum packet size of the associated endpoint (wMaxPacketSize) */
67  volatile uint32_t : 2;
68  volatile uint32_t zero_length_termination : 1 ; /* This bit is used for non-isochronous endpoints to indicate when a zero-length packet is received to terminate transfers in case the total transfer length is “multiple”. 0 - Enable zero-length packet to terminate transfers equal to a multiple of Max_packet_length (default). 1 - Disable zero-length packet on transfers that are equal in length to a multiple Max_packet_length. */
69  volatile uint32_t iso_mult : 2;
70  volatile uint32_t : 0;
71 
72  /* Word 1: Current qTD Pointer */
73  volatile uint32_t qtd_addr;
74 
75  /* Word 2-9: Transfer Overlay */
77 
78  /* Word 10-11: Setup request (control OUT only) */
80 
81  /*--------------------------------------------------------------------
82  * Due to the fact QHD is 64 bytes aligned but occupies only 48 bytes
83  * thus there are 16 bytes padding free that we can make use of.
84  *--------------------------------------------------------------------
85  */
86  volatile uint32_t attached_buffer;
87  dcd_qtd_t * volatile attached_qtd;
88  volatile uint8_t reserved[8];
89 } dcd_qhd_t;
90 
91 typedef struct {
94 } dcd_data_t;
95 
96 typedef struct {
100 
101 #if defined(__cplusplus)
102 extern "C" {
103 #endif /* __cplusplus */
104 
105 /*---------------------------------------------------------------------
106  * Exported Function Declarations
107  *---------------------------------------------------------------------
108  */
109 /* Get a qhd of the specifed endpoint */
110 dcd_qhd_t *usb_device_qhd_get(usb_device_handle_t *handle, uint8_t ep_idx);
111 
112 /* Get a qtd of the specifed endpoint */
113 dcd_qtd_t *usb_device_qtd_get(usb_device_handle_t *handle, uint8_t ep_idx);
114 
115 /* USB bus reset */
116 void usb_device_bus_reset(usb_device_handle_t *handle, uint16_t ep0_max_packet_size);
117 
118 /* Initialize controller to device mode */
119 bool usb_device_init(usb_device_handle_t *handle, uint32_t int_mask);
120 
121 /* De-initialize controller */
123 
124 /* Set Address request */
125 void usb_device_set_address(usb_device_handle_t *handle, uint8_t dev_addr);
126 
127 /* Get device address */
129 
130 /* Wake up host */
132 
133 /* Connect by enabling internal pull-up resistor on D+/D- */
135 
136 /* Disconnect by disabling internal pull-up resistor on D+/D- */
138 
139 /* Configure an endpoint */
141 
142 /* Submit a transfe */
143 bool usb_device_edpt_xfer(usb_device_handle_t *handle, uint8_t ep_addr, uint8_t *buffer, uint32_t total_bytes);
144 
145 /* Stall endpoint */
146 void usb_device_edpt_stall(usb_device_handle_t *handle, uint8_t ep_addr);
147 
148 /* clear stall */
149 void usb_device_edpt_clear_stall(usb_device_handle_t *handle, uint8_t ep_addr);
150 
151 /* check stall */
152 bool usb_device_edpt_check_stall(usb_device_handle_t *handle, uint8_t ep_addr);
153 
154 /* close a specified endpoint */
155 void usb_device_edpt_close(usb_device_handle_t *handle, uint8_t ep_addr);
156 
158 
160 
161 void usb_device_clear_status_flags(usb_device_handle_t *handle, uint32_t mask);
162 
164 
166 
168 
170 
172 
174 
176 
178 
179 void usb_device_clear_setup_status(usb_device_handle_t *handle, uint32_t mask);
180 
181 #if defined(__cplusplus)
182 }
183 #endif /* __cplusplus */
184 #endif /* HPM_USB_DEVICE_H */
#define USB_SOC_DCD_QHD_BUFFER_COUNT
Definition: hpm_soc_feature.h:84
#define USB_SOS_DCD_MAX_QHD_COUNT
Definition: hpm_soc_feature.h:90
#define USB_SOC_DCD_MAX_QTD_COUNT
Definition: hpm_soc_feature.h:89
usb_control_request_t
Definition: hpm_usb_drv.h:126
bool usb_device_get_port_ccs(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:182
uint8_t usb_device_get_port_speed(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:144
uint32_t usb_device_get_setup_status(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:202
void usb_device_remote_wakeup(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:167
uint32_t usb_device_interrupts(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:139
void usb_device_disconnect(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:177
uint8_t usb_device_get_address(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:162
void usb_device_clear_edpt_complete_status(usb_device_handle_t *handle, uint32_t mask)
Definition: hpm_usb_device.c:197
void usb_device_edpt_stall(usb_device_handle_t *handle, uint8_t ep_addr)
Definition: hpm_usb_device.c:336
dcd_qhd_t * usb_device_qhd_get(usb_device_handle_t *handle, uint8_t ep_idx)
Definition: hpm_usb_device.c:63
void usb_device_edpt_clear_stall(usb_device_handle_t *handle, uint8_t ep_addr)
Definition: hpm_usb_device.c:341
void usb_device_clear_setup_status(usb_device_handle_t *handle, uint32_t mask)
Definition: hpm_usb_device.c:207
void usb_device_deinit(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:117
void usb_device_edpt_close_all(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:356
void usb_device_connect(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:172
bool usb_device_edpt_check_stall(usb_device_handle_t *handle, uint8_t ep_addr)
Definition: hpm_usb_device.c:346
uint32_t usb_device_status_flags(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:129
uint8_t usb_device_get_suspend_status(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:149
void usb_device_set_address(usb_device_handle_t *handle, uint8_t dev_addr)
Definition: hpm_usb_device.c:154
bool usb_device_get_port_reset_status(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:187
uint32_t usb_device_get_edpt_complete_status(usb_device_handle_t *handle)
Definition: hpm_usb_device.c:192
void usb_device_edpt_close(usb_device_handle_t *handle, uint8_t ep_addr)
Definition: hpm_usb_device.c:351
void usb_device_clear_status_flags(usb_device_handle_t *handle, uint32_t mask)
Definition: hpm_usb_device.c:134
bool usb_device_edpt_xfer(usb_device_handle_t *handle, uint8_t ep_addr, uint8_t *buffer, uint32_t total_bytes)
Definition: hpm_usb_device.c:248
void usb_device_bus_reset(usb_device_handle_t *handle, uint16_t ep0_max_packet_size)
Definition: hpm_usb_device.c:73
dcd_qtd_t * usb_device_qtd_get(usb_device_handle_t *handle, uint8_t ep_idx)
Definition: hpm_usb_device.c:68
bool usb_device_edpt_open(usb_device_handle_t *handle, usb_endpoint_config_t *config)
Definition: hpm_usb_device.c:216
bool usb_device_init(usb_device_handle_t *handle, uint32_t int_mask)
Definition: hpm_usb_device.c:91
Definition: hpm_usb_regs.h:12
Definition: hpm_usb_device.h:91
Definition: hpm_usb_device.h:61
volatile uint32_t int_on_setup
Definition: hpm_usb_device.h:65
volatile uint32_t max_packet_size
Definition: hpm_usb_device.h:66
volatile dcd_qtd_t qtd_overlay
Definition: hpm_usb_device.h:76
volatile usb_control_request_t setup_request
Definition: hpm_usb_device.h:79
dcd_qtd_t *volatile attached_qtd
Definition: hpm_usb_device.h:87
volatile uint32_t iso_mult
Definition: hpm_usb_device.h:69
volatile uint32_t attached_buffer
Definition: hpm_usb_device.h:86
volatile uint32_t
Definition: hpm_usb_device.h:64
volatile uint32_t zero_length_termination
Definition: hpm_usb_device.h:68
volatile uint32_t qtd_addr
Definition: hpm_usb_device.h:73
Definition: hpm_usb_device.h:28
volatile bool in_use
Definition: hpm_usb_device.h:56
volatile uint32_t next
Definition: hpm_usb_device.h:30
volatile uint32_t xact_err
Definition: hpm_usb_device.h:37
volatile uint8_t reserved
Definition: hpm_usb_device.h:57
volatile uint32_t active
Definition: hpm_usb_device.h:41
volatile uint32_t halted
Definition: hpm_usb_device.h:40
volatile uint32_t total_bytes
Definition: hpm_usb_device.h:46
volatile uint16_t expected_bytes
Definition: hpm_usb_device.h:55
volatile uint32_t int_on_complete
Definition: hpm_usb_device.h:45
volatile uint32_t buffer_err
Definition: hpm_usb_device.h:39
volatile uint32_t iso_mult_override
Definition: hpm_usb_device.h:43
volatile uint32_t token
Definition: hpm_usb_device.h:34
volatile uint32_t
Definition: hpm_usb_device.h:36
Definition: hpm_usb_device.h:96
dcd_data_t * dcd_data
Definition: hpm_usb_device.h:98
USB_Type * regs
Definition: hpm_usb_device.h:97
Endpoint config structure.
Definition: hpm_usb_drv.h:131