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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/**
* USBPERIPHERAL Peripheral Header File
* @file usb_peripheral.h
* @defgroup usb_peripheral USB Peripheral Hardware Abstraction Layer (HAL)
* @brief Interface for a USB peripheral module that needs to be implemented by a device-specific USB module driver.
* @version USB Device Stack HAL Driver Version 1.0.0
*/
/*
(c) 2021 Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software and any
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
FOR A PARTICULAR PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#ifndef USB_PERIPHERAL_H
// cppcheck-suppress misra-c2012-2.5
#define USB_PERIPHERAL_H
#include <stdbool.h>
#include <stdint.h>
#include <usb_common_elements.h>
#include <usb_peripheral_endpoint.h>
#include <usb_peripheral_read_write.h>
#include <usb_protocol_headers.h>
/**
* @ingroup usb_peripheral
* @struct USB_CONTROL_TRANSFER_t
* @brief The data structure for internally handling control transfers, either IN or OUT.
*/
typedef struct USB_CONTROL_TRANSFER_struct
{
uint8_t buffer[64]; /**<Default buffer for control data transfers*/
volatile USB_CONTROL_STATUS_t status; /**<The status of a transfer on this pipe*/
uint8_t *transferDataPtr; /**<Location in RAM to send or fill during transfer*/
uint16_t transferDataSize; /**<Number of bytes to transfer to or from RAM location*/
uint16_t totalBytesTransferred; /**<Number of data transfered last transaction*/
USB_SETUP_PROCESS_CALLBACK_t processSetupCallback; /**<Callback to call during setup process*/
USB_SETUP_OVERUNDERRUN_CALLBACK_t overUnderRunCallback; /**<Callback to call on a control overrun or underrun*/
USB_SETUP_ENDOFREQUEST_CALLBACK_t endOfRequestCallback; /**<Callback to call when a setup request is complete*/
USB_SETUP_REQUEST_t setupRequest; /**<Setup request packet*/
} USB_CONTROL_TRANSFER_t;
/**
* @ingroup usb_peripheral
* @brief Detects if the Setup event was received.
* @param None.
* @retval 0 - Setup event was not received
* @retval 1 - Setup event was received
*/
bool USB_SetupIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Detects if the Start-of-Frame (SOF) event was received.
* @param None.
* @retval 0 - SOF event was not received
* @retval 1 - SOF event was received
*/
bool USB_EventSOFIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Clears the SOF event.
* @param None.
* @return None.
*/
void USB_EventSOFClear(void);
/**
* @ingroup usb_peripheral
* @brief Detects if the Reset event was received.
* @param None.
* @retval 1 - Reset event was received
* @retval 0 - Reset event was not received
*/
bool USB_EventResetIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Clears the Reset event.
* @param None.
* @return None.
*/
void USB_EventResetClear(void);
/**
* @ingroup usb_peripheral
* @brief Detects if an Overflow and/or Underflow event was received.
* @param None.
* @return A value representing the events received
*/
uint8_t USB_EventOverUnderflowIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Detects if an Overflow and/or Underflow event was received on the control endpoints.
* @param None.
* @return A value representing the events received
*/
uint8_t USB_ControlOverUnderflowIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Clears the Over/Underflow event.
* @param None.
* @return None.
*/
void USB_EventOverUnderflowClear(void);
/**
* @ingroup usb_peripheral
* @brief Detects if a Suspend event was received.
* @param None.
* @return A boolean value representing the Suspend event received condition
* @retval 0 - Suspend event was not received
* @retval 1 - Suspend event was received
*/
bool USB_EventSuspendIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Clears the Suspend event.
* @param None.
* @return None.
*/
void USB_EventSuspendClear(void);
/**
* @ingroup usb_peripheral
* @brief Detects if a Resume event was received.
* @param None.
* @retval 0 - Resume event was not received
* @retval 1 - Resume event was received
*/
bool USB_EventResumeIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Clears the Resume event.
* @param None.
* @return None.
*/
void USB_EventResumeClear(void);
/**
* @ingroup usb_peripheral
* @brief Detects if a Stalled event was received.
* @param None.
* @retval 0 - Stalled event was not received
* @retval 1 - Stalled event was received
*/
bool USB_EventStalledIsReceived(void);
/**
* @ingroup usb_peripheral
* @brief Clears the Stalled event.
* @param None.
* @return None.
*/
void USB_EventStalledClear(void);
/**
* @ingroup usb_peripheral
* @brief Attaches the device to the USB bus.
* @param None.
* @return None.
*/
void USB_BusAttach(void);
/**
* @ingroup usb_peripheral
* @brief Detaches the device from the USB bus.
* @param None.
* @return None.
*/
void USB_BusDetach(void);
/**
* @ingroup usb_peripheral
* @brief Checks if the device is attached to the USB bus not.
* @param None.
* @retval 0 - USB bus is not attached
* @retval 1 - USB bus is attached
*/
bool USB_IsBusAttached(void);
/**
* @ingroup usb_peripheral
* @brief Sets the device address.
* @param deviceAddress - Device address to set
* @return None.
*/
void USB_DeviceAddressConfigure(uint8_t deviceAddress);
/**
* @ingroup usb_peripheral
* @brief Gets the current frame number.
* @param None.
* @return 15-bit frame number
*/
uint16_t USB_FrameNumberGet(void);
/**
* @ingroup usb_peripheral
* @brief Ensures correct control endpoint initialization.
* @param None.
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlEndpointsInit(void);
/**
* @ingroup usb_peripheral
* @brief Verifies the received control setup.
* @param None.
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlSetupReceived(void);
/**
* @ingroup usb_peripheral
* @brief Handles completed transactions on the control endpoints. Checks and verifies data OUT, data IN, ZLP OUT and ZLP IN.
* @param pipe - A combination of endpoint address and direction
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlTransactionComplete(USB_PIPE_t pipe);
/**
* @ingroup usb_peripheral
* @brief Sends ZLP OUT and ZLP IN transactions on the control endpoints.
* @param direction - The endpoint direction to send the ZLP
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlTransferZLP(uint8_t direction);
/**
* @ingroup usb_peripheral
* @brief Ensures correct control transfer reset.
* @param None.
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlTransferReset(void);
/**
* @ingroup usb_peripheral
* @brief Updates the transfer data pointer and size in ControlTransfer.
* @param *dataPtr - Pointer to new data
* @param dataSize - Number of elements in the array
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlTransferDataSet(uint8_t *dataPtr, uint16_t dataSize);
/**
* @ingroup usb_peripheral
* @brief Copies data to the transfer buffer and sets the transfer data pointer and size in ControlTransfer.
* @param *dataPtr - Pointer to data to copy
* @param dataSize - Number of elements in the array
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlTransferDataWriteBuffer(uint8_t *dataPtr, uint8_t dataSize);
/**
* @ingroup usb_peripheral
* @brief Sets the callback for end of a control request.
* @param callback - The function to call for the end of a control request
* @return None.
*/
void USB_ControlEndOfRequestCallbackRegister(USB_SETUP_ENDOFREQUEST_CALLBACK_t callback);
/**
* @ingroup usb_peripheral
* @brief Sets the callback for the setup processing.
* @param callback - The function to call for the setup processing
* @return None.
*/
void USB_ControlProcessSetupCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback);
/**
* @ingroup usb_peripheral
* @brief Sets the callback for a control overrun or underrun.
* @param callback - The function to call on a control overrun or underrun
* @return None.
*/
void USB_ControlOverUnderRunCallbackRegister(USB_SETUP_OVERUNDERRUN_CALLBACK_t callback);
/**
* @ingroup usb_peripheral
* @brief Handles the control Over/Underflow events.
* @param overunderflow - A value representing overflow or underflow
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_ControlProcessOverUnderflow(uint8_t overunderflow);
/**
* @ingroup usb_peripheral
* @brief Handles the Stall events.
* @param pipe - A combination of endpoint address and direction
* @return SUCCESS or an Error code according to RETURN_CODE_t
*/
RETURN_CODE_t USB_HandleEventStalled(USB_PIPE_t pipe);
/**
* @ingroup usb_peripheral
* @brief Enables the peripheral and the frame number, enables and resets FIFO, sets the endpoint table address and max endpoints.
* @param None.
* @return None.
*/
void USB_PeripheralInitialize(void);
/**
* @ingroup usb_peripheral
* @brief Disables the USB peripheral and aborts any ongoing transaction.
* @param None.
* @return None.
*/
void USB_PeripheralDisable(void);
#endif /* USB_PERIPHERAL_H */
|