aboutsummaryrefslogtreecommitdiffstats
path: root/hal/include/hpl_usb_device.h
blob: 43dd17fb34378278763189c390378b69cfe53db9 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * \file
 *
 * \brief SAM USB HPL
 *
 * Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * 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.
 *
 * \asf_license_stop
 *
 */

#ifndef _HPL_USB_DEVICE_H_INCLUDED
#define _HPL_USB_DEVICE_H_INCLUDED

#include <hpl_usb.h>
#include "hpl_usb_config.h"

#ifdef __cplusplus
extern "C" {
#endif

/** USB Device callback type. */
enum usb_d_cb_type {
	/** USB device SOF callback. */
	USB_D_CB_SOF,
	/** USB device events callbacks. */
	USB_D_CB_EVENT,
	/** Number of types of USB device callback types. */
	USB_D_CB_N
};

/** USB Device endpoint callback type. */
enum usb_d_ep_cb_type {
	/** USB device endpoint setup callback. */
	USB_D_EP_CB_SETUP,
	/** USB device endpoint more data callback. */
	USB_D_EP_CB_MORE,
	/** USB device endpoint transaction done or error callback. */
	USB_D_EP_CB_XFER,
	/** Number of types of USB device endpoint callback types. */
	USB_D_EP_CB_N
};

/** Control action for USB device LPM handshake. */
enum usb_d_lpm_ctrl {
	/** No LPM handshake, not supported. */
	USB_D_LPM_DISABLE,
	/** ACK the LPM transaction. */
	USB_D_LPM_ACK,
	/** NYET the LPM transaction. */
	USB_D_LPM_NYET
};

/**
 *  USB device transfer descriptor.
 */
struct usb_d_transfer {
	/** Pointer to data buffer to transfer.
	 *  Note that it's recommended that the buffer is 32-bit aligned since
	 *  some of USB peripheral require this.
	 */
	uint8_t *buf;
	/** Transfer size, in number of bytes.
	 *  Note that it's recommended that the buffer size is 32-bit aligned
	 *  (modeled by 4) since some of USB peripheral require this.
	 */
	uint32_t size;
	/** Endpoint address. */
	uint8_t ep;
	/** Append ZLP for IN transfer, wait ZLP for OUT transfer. */
	uint8_t zlp;
};

/** USB device transactions status structure. */
struct usb_d_trans_status {
	/** Total data size. */
	uint32_t size;
	/** Total transfered data count. */
	uint32_t count;
	/** Endpoint address. */
	uint8_t ep;
	/** Endpoint type - CTRL/ISO/INT/BULK. */
	uint8_t xtype : 2;
	/** Transactions state, busy or not. */
	uint8_t busy : 1;
	/** Transactions state, setup received or not. */
	uint8_t setup : 1;
	/** Transactions state, stall or not. */
	uint8_t stall : 1;
	/** Transactions direction. */
	uint8_t dir : 1;
};

/** Prototype function for callback that is invoked on USB device SOF. */
typedef void (*_usb_d_dev_sof_cb_t)(void);

/** Prototype function for callback that is invoked on USB device events. */
typedef void (*_usb_d_dev_event_cb_t)(const enum usb_event, const uint32_t param);

/** HPL USB device callbacks. */
struct _usb_d_dev_callbacks {
	/** Callback that is invoked on SOF. */
	_usb_d_dev_sof_cb_t sof;
	/** Callback that is invoked on USB RESET/WAKEUP/RESUME/SUSPEND. */
	_usb_d_dev_event_cb_t event;
};

/** USB device endpoint callbacks. */
enum usb_d_dev_ep_cb_type {
	/** Setup packet is received. */
	USB_D_DEV_EP_CB_SETUP,
	/** Try to require more data. */
	USB_D_DEV_EP_CB_MORE,
	/** Transaction done OK/ERROR. */
	USB_D_DEV_EP_CB_DONE,
	/** Number of device endpoint callbacks. */
	USB_D_DEV_EP_CB_N
};

/**
 *  Callback that is invoked when control SETUP packet has bee received.
 *  \ref _usb_d_dev_ep_read_req() must be invoked to read setup data, and allow
 *  IN/OUT transactions on control endpoint.
 */
typedef void (*_usb_d_dev_ep_cb_setup_t)(const uint8_t ep);

/** Callback that is invoked when buffer is done, but last packet is full size
 *  packet without ZLP. Return \c true if more data has been requested. */
typedef bool (*_usb_d_dev_ep_cb_more_t)(const uint8_t ep, const uint32_t transfered);

/** Callback that is invoked when all data is finished, including background
 *  transfer, or error happens. */
typedef void (*_usb_d_dev_ep_cb_done_t)(const uint8_t ep, const int32_t code, const uint32_t transfered);

/** Callbacks for HPL USB device endpoint. */
struct _usb_d_dev_ep_callbacks {
	/** Callback that is invoked when SETUP packet is received.
	 *  \ref _usb_d_dev_ep_read_req() must be invoked to read setup data, and
	 *  allow IN/OUT transactions on control endpoint.
	 */
	_usb_d_dev_ep_cb_setup_t setup;
	/** Callback that is invoked to check if buffer is NULL and more data is
	 *  required.
	 *  It's called when last packet is full size packet, without
	 *  auto ZLP enabled.
	 *  It could be called when background transfer is still in progress.
	 */
	_usb_d_dev_ep_cb_more_t more;
	/** Callback that is invoked when transaction is done, including background
	 *  transfer, or error occurs.
	 */
	_usb_d_dev_ep_cb_done_t done;
};

/**
 * \brief Initialize the USB device instance
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_init(void);

/**
 * \brief Deinitialize the USB device instance
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
void _usb_d_dev_deinit(void);

/**
 * \brief Register callback to handle USB device events
 * \param[in] type Callback type. See \ref usb_d_cb_type.
 * \param[in] func Pointer to callback function.
 *         Refer to \ref _usb_d_dev_callbacks for the prototypes.
 */
void _usb_d_dev_register_callback(const enum usb_d_cb_type type, const FUNC_PTR func);

/**
 * \brief Register callback to handle USB device endpoint events
 * \param[in] type Callback type. See \ref usb_d_dev_ep_cb_type.
 * \param[in] func Pointer to callback function.
 *         Refer to \ref _usb_d_dev_ep_callbacks for the prototypes.
 */
void _usb_d_dev_register_ep_callback(const enum usb_d_dev_ep_cb_type type, const FUNC_PTR func);

/**
 * \brief Enable the USB device
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_enable(void);

/**
 * \brief Disable the USB device
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_disable(void);

/**
 * \brief Attach the USB device
 */
void _usb_d_dev_attach(void);

/**
 * \brief Detach the USB device
 */
void _usb_d_dev_detach(void);

/**
 * \brief Send the USB device remote wakeup to host
 */
void _usb_d_dev_send_remotewakeup(void);

/**
 * \brief Get the USB device working speed
 * \return USB speed. See \ref usb_speed.
 */
enum usb_speed _usb_d_dev_get_speed(void);

/**
 * \brief Set the USB device address
 * \param[in] addr Address to be used.
 */
void _usb_d_dev_set_address(const uint8_t addr);

/**
 * \brief Get the USB device address
 * \return Address that is used.
 */
uint8_t _usb_d_dev_get_address(void);

/**
 * \brief Get the USB device frame number
 * \return The frame number.
 */
uint16_t _usb_d_dev_get_frame_n(void);

/**
 * \brief Get the USB device micro frame number
 * \return The micro frame number inside one frame (0~7).
 */
uint8_t _usb_d_dev_get_uframe_n(void);

/**
 * \brief Initialize and enable the USB device default endpoint 0
 * \param[in] max_pkt_siz Max endpoint size.
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_ep0_init(const uint8_t max_pkt_siz);

/**
 * \brief Initialize and enable the USB device endpoint
 * \param[in] ep Endpoint address,
 *               see endpoint descriptor details in USB spec.
 * \param[in] attr Endpoint attributes,
 *                 see endpoint descriptor details in USB spec.
 * \param[in] max_pkt_siz Endpoint size,
 *                        see endpoint descriptor details in USB spec.
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_ep_init(const uint8_t ep, const uint8_t attr, uint16_t max_pkt_siz);

/**
 * \brief Disable and deinitialize the USB device endpoint

 * \param[in] ep The endpoint to deinitialize.
 */
void _usb_d_dev_ep_deinit(const uint8_t ep);

/**
 * \brief Enable the endpoint
 * \param[in] ep The endpoint to enable.
 * \return Operation result status.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_ep_enable(const uint8_t ep);

/**
 * \brief Disable the endpoint
 * \param[in] ep The endpoint to disable.
 */
void _usb_d_dev_ep_disable(const uint8_t ep);

/**
 * \brief Set/Clear/Get USB device endpoint stall status
 * \param[in] ep Endpoint address.
 * \param[in] ctrl Operation selector. See \ref usb_ep_stall_ctrl.
 * \return Operation result or stall status.
 * \retval 0 Success or not stall.
 * \retval 1 Endpoint is stalled.
 * \retval -1 error.
 */
int32_t _usb_d_dev_ep_stall(const uint8_t ep, const enum usb_ep_stall_ctrl ctrl);

/**
 * \brief Read setup request data from specific endpoint
 * \param[in] ep Endpoint address.
 * \param[out] req_buf Pointer to buffer to locate the setup packet.
 * \return Number of bytes or error code.
 * \retval <0 error code.
 * \retval 0 No setup packet ready for read.
 * \retval >0 Size of bytes read, and ready to start IN/OUT. Note that if
 *         this number is over 8, only first 8 bytes will be copied.
 */
int32_t _usb_d_dev_ep_read_req(const uint8_t ep, uint8_t *req_buf);

/**
 * \brief Start USB device transfer
 *
 * On different USB peripheral hardware the transaction buffer address and size
 * may have different constraints. E.g., some hardware may require input address
 * 32-bit aligned, and input size 32-bit aligned. Refer to the corresponding
 * hardware usage reference documents.
 * The constraints are checked in implementation, with error code returned.
 *
 * \param[in] trans Pointer to the transaction description.
 * \return Operation result status.
 * \retval 1 Busy.
 * \retval 0 Success.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_ep_trans(const struct usb_d_transfer *trans);

/**
 * \brief Abort pending USB device transaction on specific endpoint
 * \param[in] ep Endpoint address to abort.
 */
void _usb_d_dev_ep_abort(const uint8_t ep);

/**
 * \brief Retrieve endpoint status.
 * \param[in] ep Endpoint address.
 * \param[out] stat Pointer to buffer to fill status description.
 * \return Status.
 * \retval 2 Packet writing.
 * \retval 1 Busy.
 * \retval 0 Ready.
 * \retval <0 Error code.
 */
int32_t _usb_d_dev_ep_get_status(const uint8_t ep, struct usb_d_trans_status *stat);

#ifdef __cplusplus
}
#endif

#endif /* _HPL_USB_DEVICE_H_INCLUDED */