Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_stack.h"
...
_ux_host_stack_device_address_set(UX_DEVICE *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesusbxcommon/core/src/ux_host_stack_device_address_set.c
 
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
26
27
28
29
30
31
32
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** USBX Component */ /** */ /** Host Stack */ /** */... /**************************************************************************/ /**************************************************************************/ /* Include necessary system files. */ #define UX_SOURCE_CODE #include "ux_api.h" #include "ux_host_stack.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_host_stack_device_address_set PORTABLE C */ /* 6.1.10 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function sets the device address to the new device. */ /* */ /* INPUT */ /* */ /* device Pointer to device */ /* */ /* OUTPUT */ /* */ /* Completion Status */ /* */ /* CALLS */ /* */ /* _ux_utility_delay_ms Thread sleep */ /* _ux_host_stack_transfer_request Process transfer request */ /* */ /* CALLED BY */ /* */ /* USBX Components */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* optimized based on compile */ /* definitions, */ /* resulting in version 6.1 */ /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */ /* added standalone support, */ /* resulting in version 6.1.10 */ /* */... /**************************************************************************/ UINT _ux_host_stack_device_address_set(UX_DEVICE *device) { UINT status = UX_ERROR; UX_TRANSFER *transfer_request; UX_ENDPOINT *control_endpoint; USHORT device_address; #if UX_MAX_DEVICES > 1 UX_HCD *hcd; UINT address_byte_index; UINT address_bit_index; UCHAR device_address_byte;/* ... */ #endif /* Retrieve the pointer to the control endpoint. */ control_endpoint = &device -> ux_device_control_endpoint; /* Retrieve the transfer request pointer. */ transfer_request = &control_endpoint -> ux_endpoint_transfer_request; /* Initialize device address to 1. */ device_address = 1; #if UX_MAX_DEVICES > 1 /* We need the HCD pointer as well. */ hcd = UX_DEVICE_HCD_GET(device); /* Calculate the new address of this device. We start with address 1. */ for (address_byte_index = 0; address_byte_index < 16; address_byte_index++) { /* Get the address mask byte. */ device_address_byte = hcd -> ux_hcd_address[address_byte_index]; /* Scan each bit for an empty spot. */ for (address_bit_index = 0; address_bit_index < 8; address_bit_index++) { if ((device_address_byte & (1 << address_bit_index)) == 0) { /* We have found an empty spot. Reserve this address. */ device_address_byte = (UCHAR)((UCHAR)device_address_byte | (UCHAR)(1 << address_bit_index)); /* Store the address mask byte. */ hcd -> ux_hcd_address[address_byte_index] = device_address_byte; /* OK, apply address. */ status = UX_SUCCESS; break; }if ((device_address_byte & (1 << address_bit_index)) == 0) { ... } /* This address was already taken, increment to the next address. */ device_address++; }for (address_bit_index = 0; address_bit_index < 8; address_bit_index++) { ... } /* If address found, break the loop. */ if (status == UX_SUCCESS) { break; }if (status == UX_SUCCESS) { ... } }for (address_byte_index = 0; address_byte_index < 16; address_byte_index++) { ... } if (status == UX_ERROR) /* We should never get here! */ return(UX_ERROR);/* ... */ #endif /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_ADDRESS_SET, device, device_address, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0) /* Create a transfer request for the SET_ADDRESS request. */ transfer_request -> ux_transfer_request_data_pointer = UX_NULL; transfer_request -> ux_transfer_request_requested_length = 0; transfer_request -> ux_transfer_request_function = UX_SET_ADDRESS; transfer_request -> ux_transfer_request_type = UX_REQUEST_OUT | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE; transfer_request -> ux_transfer_request_value = device_address; transfer_request -> ux_transfer_request_index = 0; #if defined(UX_HOST_STANDALONE) device -> ux_device_enum_trans = transfer_request; status = UX_SUCCESS; return(status);/* ... */ #else /* Send request to HCD layer. */ status = _ux_host_stack_transfer_request(transfer_request); /* Now, this address will be the one used in future transfers. The transfer may have failed and therefore all the device resources including the new address will be free.*//* ... */ device -> ux_device_address = (ULONG) device_address; /* Check completion status. */ if (status == UX_SUCCESS) { /* Mark the device as ADDRESSED now. */ device -> ux_device_state = UX_DEVICE_ADDRESSED; /* Some devices need some time to accept this address. */ _ux_utility_delay_ms(UX_DEVICE_ADDRESS_SET_WAIT); /* Return successful status. */ return(status); }if (status == UX_SUCCESS) { ... } else { /* We have an error at the first device transaction. This is mostly due to the device having failed on the reset after power up. we will try again either at the root hub or regular hub. *//* ... */ return(status); }else { ... } /* ... */#endif }{ ... }