1 /**
2  * \file
3  *
4  * \brief SAM USB Driver
5  *
6  * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef USB_H_INCLUDED
47 #define USB_H_INCLUDED
48 
49 #include <compiler.h>
50 #include <clock.h>
51 #include <gclk.h>
52 #include <pinmux.h>
53 #include <system_interrupt.h>
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 /**
60  * \defgroup asfdoc_sam0_usb_group SAM Universal Serial Bus (USB)
61  *
62  * The Universal Serial Bus (USB) module complies with the USB 2.1 specification.
63  *
64  * The following peripherals are used by this module:
65  *  - USB (Universal Serial Bus)
66  *
67  * The following devices can use this module:
68  *  - Atmel | SMART SAM D21
69  *  - Atmel | SMART SAM R21
70  *  - Atmel | SMART SAM D11 (Only USB device support on SAM D11 device)
71  *  - Atmel | SMART SAM L21
72  *  - Atmel | SMART SAM L22 (Only USB device support on SAM L22 device)
73  *  - Atmel | SMART SAM DA1
74  *
75  * The USB module covers following mode:
76  * \if USB_DEVICE_MODE
77  *  - USB Device Mode
78  * \endif
79  * \if USB_HOST_MODE
80  *  - USB Host Mode
81  * \endif
82  *
83  * The USB module covers following speed:
84  * \if USB_HS_MODE
85  *  - USB High Speed (480Mbit/s)
86  * \endif
87  *  - USB Full Speed (12Mbit/s)
88  * \if USB_LS_MODE
89  *  - USB Low Speed (1.5Mbit/s)
90  * \endif
91  *
92  * \if USB_LPM_MODE
93  * The USB module supports Link Power Management (LPM-L1) protocol.
94  * \endif
95  *
96  * USB support needs whole set of enumeration process, to make the device
97  * recognizable and usable. The USB driver is designed to interface to the
98  * USB Stack in Atmel Software Framework (ASF).
99  *
100  * \if USB_DEVICE_MODE
101  * \section asfdoc_sam0_usb_device USB Device Mode
102  * The ASF USB Device Stack has defined the USB Device Driver (UDD) interface,
103  * to support USB device operations. The USB module device driver complies with
104  * this interface, so that the USB Device Stack can work based on the
105  * USB module.
106  *
107  * Refer to <a href="http://www.atmel.com/images/doc8360.pdf">
108  * "ASF - USB Device Stack"</a> for more details.
109  * \endif
110  *
111  * \if USB_HOST_MODE
112  * \section adfdoc_sam0_usb_host USB Host Mode
113  * The ASF USB Host Stack has defined the USB Host Driver (UHD) interface,
114  * to support USB host operations. The USB module host driver complies with
115  * this interface, so that the USB Host Stack can work based on the USB module.
116  *
117  * Refer to <a href="http://www.atmel.com/images/doc8486.pdf">
118  * "ASF - USB Host Stack"</a> for more details.
119  * \endif
120  */
121 
122 /** Enum for the speed status for the USB module */
123 enum usb_speed {
124 	USB_SPEED_LOW,
125 	USB_SPEED_FULL,
126 };
127 
128 /** Enum for the possible callback types for the USB in host module */
129 enum usb_host_callback {
130 	USB_HOST_CALLBACK_SOF,
131 	USB_HOST_CALLBACK_RESET,
132 	USB_HOST_CALLBACK_WAKEUP,
133 	USB_HOST_CALLBACK_DNRSM,
134 	USB_HOST_CALLBACK_UPRSM,
135 	USB_HOST_CALLBACK_RAMACER,
136 	USB_HOST_CALLBACK_CONNECT,
137 	USB_HOST_CALLBACK_DISCONNECT,
138 	USB_HOST_CALLBACK_N,
139 };
140 
141 /** Enum for the possible callback types for the USB pipe in host module */
142 enum usb_host_pipe_callback {
143 	USB_HOST_PIPE_CALLBACK_TRANSFER_COMPLETE,
144 	USB_HOST_PIPE_CALLBACK_ERROR,
145 	USB_HOST_PIPE_CALLBACK_SETUP,
146 	USB_HOST_PIPE_CALLBACK_STALL,
147 	USB_HOST_PIPE_CALLBACK_N,
148 };
149 
150 /**
151  * \brief Host pipe types.
152  */
153 enum usb_host_pipe_type {
154 	USB_HOST_PIPE_TYPE_DISABLE,
155 	USB_HOST_PIPE_TYPE_CONTROL,
156 	USB_HOST_PIPE_TYPE_ISO,
157 	USB_HOST_PIPE_TYPE_BULK,
158 	USB_HOST_PIPE_TYPE_INTERRUPT,
159 	USB_HOST_PIPE_TYPE_EXTENDED,
160 };
161 
162 /**
163  * \brief Host pipe token types.
164  */
165 enum usb_host_pipe_token {
166 	USB_HOST_PIPE_TOKEN_SETUP,
167 	USB_HOST_PIPE_TOKEN_IN,
168 	USB_HOST_PIPE_TOKEN_OUT,
169 };
170 
171 /**
172  * \brief Enumeration for the possible callback types for the USB in device module
173  */
174 enum usb_device_callback {
175 	USB_DEVICE_CALLBACK_SOF,
176 	USB_DEVICE_CALLBACK_RESET,
177 	USB_DEVICE_CALLBACK_WAKEUP,
178 	USB_DEVICE_CALLBACK_RAMACER,
179 	USB_DEVICE_CALLBACK_SUSPEND,
180 	USB_DEVICE_CALLBACK_LPMNYET,
181 	USB_DEVICE_CALLBACK_LPMSUSP,
182 	USB_DEVICE_CALLBACK_N,
183 };
184 
185 /**
186  * \brief Enumeration for the possible callback types for the USB endpoint in device module
187  */
188 enum usb_device_endpoint_callback {
189 	USB_DEVICE_ENDPOINT_CALLBACK_TRCPT,
190 	USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL,
191 	USB_DEVICE_ENDPOINT_CALLBACK_RXSTP,
192 	USB_DEVICE_ENDPOINT_CALLBACK_STALL,
193 	USB_DEVICE_EP_CALLBACK_N,
194 };
195 
196 /**
197  * \brief Device Endpoint types.
198  */
199 enum usb_device_endpoint_type {
200 	USB_DEVICE_ENDPOINT_TYPE_DISABLE,
201 	USB_DEVICE_ENDPOINT_TYPE_CONTROL,
202 	USB_DEVICE_ENDPOINT_TYPE_ISOCHRONOUS,
203 	USB_DEVICE_ENDPOINT_TYPE_BULK,
204 	USB_DEVICE_ENDPOINT_TYPE_INTERRUPT,
205 };
206 
207 /**
208  * \brief Endpoint Size
209  */
210 enum usb_endpoint_size {
211 	USB_ENDPOINT_8_BYTE,
212 	USB_ENDPOINT_16_BYTE,
213 	USB_ENDPOINT_32_BYTE,
214     USB_ENDPOINT_64_BYTE,
215 	USB_ENDPOINT_128_BYTE,
216 	USB_ENDPOINT_256_BYTE,
217 	USB_ENDPOINT_512_BYTE,
218 	USB_ENDPOINT_1023_BYTE,
219 };
220 
221 /**
222  * \brief Link Power Management Handshake.
223  */
224 enum usb_device_lpm_mode {
225 	USB_DEVICE_LPM_NOT_SUPPORT,
226 	USB_DEVICE_LPM_ACK,
227 	USB_DEVICE_LPM_NYET,
228 };
229 
230 /**
231  * \brief Module structure
232  */
233 struct usb_module;
234 
235 /**
236  * \name Host Callback Functions Types
237  * @{
238  */
239 typedef void (*usb_host_callback_t)(struct usb_module *module_inst);
240 typedef void (*usb_host_pipe_callback_t)(struct usb_module *module_inst, void *);
241 /** @} */
242 
243 /**
244  * \name Device Callback Functions Types
245  * @{
246  */
247 typedef void (*usb_device_callback_t)(struct usb_module *module_inst, void* pointer);
248 typedef void (*usb_device_endpoint_callback_t)(struct usb_module *module_inst, void* pointer);
249 /** @} */
250 
251 
252 /** USB configurations */
253 struct usb_config {
254 	/** \c true for host, \c false for device. */
255 	bool select_host_mode;
256 	/** When \c true the module is enabled during standby. */
257 	bool run_in_standby;
258 	/** Generic Clock Generator source channel. */
259 	enum gclk_generator source_generator;
260 	/** Speed mode */
261 	enum usb_speed speed_mode;
262 };
263 
264 /**
265  * \brief USB software module instance structure.
266  *
267  * USB software module instance structure, used to retain software state
268  * information of an associated hardware module instance.
269  *
270  */
271 struct usb_module {
272 	/** Hardware module pointer of the associated USB peripheral. */
273 	Usb *hw;
274 
275 #if !SAMD11 && !SAML22
276 	/** Array to store host related callback functions */
277 	usb_host_callback_t host_callback[USB_HOST_CALLBACK_N];
278 	usb_host_pipe_callback_t host_pipe_callback[USB_PIPE_NUM][USB_HOST_PIPE_CALLBACK_N];
279 	/** Bit mask for host callbacks registered */
280 	uint8_t host_registered_callback_mask;
281 	/** Bit mask for host callbacks enabled */
282 	uint8_t host_enabled_callback_mask;
283 	/** Bit mask for host pipe callbacks registered */
284 	uint8_t host_pipe_registered_callback_mask[USB_PIPE_NUM];
285 	/** Bit mask for host pipe callbacks enabled */
286 	uint8_t host_pipe_enabled_callback_mask[USB_PIPE_NUM];
287 #endif
288 
289 	/** Array to store device related callback functions */
290 	usb_device_callback_t device_callback[USB_DEVICE_CALLBACK_N];
291 	usb_device_endpoint_callback_t device_endpoint_callback[USB_EPT_NUM][USB_DEVICE_EP_CALLBACK_N];
292 	/** Bit mask for device callbacks registered */
293 	uint16_t device_registered_callback_mask;
294 	/** Bit mask for device callbacks enabled */
295 	uint16_t device_enabled_callback_mask;
296 	/** Bit mask for device endpoint callbacks registered */
297 	uint8_t device_endpoint_registered_callback_mask[USB_EPT_NUM];
298 	/** Bit mask for device endpoint callbacks enabled */
299 	uint8_t device_endpoint_enabled_callback_mask[USB_EPT_NUM];
300 };
301 
302 /** USB host pipe configurations */
303 struct usb_host_pipe_config {
304 	/** device address */
305 	uint8_t device_address;
306 	/** endpoint address  */
307 	uint8_t endpoint_address;
308 	/** Pipe type */
309 	enum usb_host_pipe_type pipe_type;
310 	/** interval */
311 	uint8_t binterval;
312 	/** pipe size */
313 	uint16_t size;
314 };
315 
316 /** USB device endpoint configurations */
317 struct usb_device_endpoint_config {
318 	/** device address */
319 	uint8_t ep_address;
320 	/** endpoint size */
321 	enum usb_endpoint_size ep_size;
322 	/** automatic zero length packet mode, \c true to enable */
323 	bool auto_zlp;
324 	/** type of endpoint with Bank */
325 	enum usb_device_endpoint_type ep_type;
326 };
327 
328 /** USB host pipe callback status parameter structure */
329 struct usb_pipe_callback_parameter {
330 	/** current pipe number */
331 	uint8_t pipe_num;
332 	/** pipe error status */
333 	uint8_t pipe_error_status;
334 	/** actual transferred data size */
335 	uint16_t transfered_size;
336 	/** required data size */
337 	uint16_t required_size;
338 };
339 
340 /** USB device endpoint callback status parameter structure */
341 struct usb_endpoint_callback_parameter {
342 	uint16_t received_bytes;
343 	uint16_t sent_bytes;
344 	uint16_t out_buffer_size;
345 	uint8_t endpoint_address;
346 };
347 
348 void usb_enable(struct usb_module *module_inst);
349 void usb_disable(struct usb_module *module_inst);
350 
351 /**
352  * \brief Get the status of USB module's state machine
353  *
354  * \param module_inst Pointer to USB module instance
355  */
usb_get_state_machine_status(struct usb_module * module_inst)356 static inline uint8_t usb_get_state_machine_status(struct usb_module *module_inst)
357 {
358 	/* Sanity check arguments */
359 	Assert(module_inst);
360 	Assert(module_inst->hw);
361 
362 	return module_inst->hw->DEVICE.FSMSTATUS.reg;
363 }
364 
365 void usb_get_config_defaults(struct usb_config *module_config);
366 enum status_code usb_init(struct usb_module *module_inst, Usb *const hw,
367 		struct usb_config *module_config);
368 
369 #if !SAMD11 && !SAML22
370 /**
371  * \brief Enable the USB host by setting the VBUS OK
372  *
373  * \param module_inst Pointer to USB software instance struct
374  */
usb_host_enable(struct usb_module * module_inst)375 static inline void usb_host_enable(struct usb_module *module_inst)
376 {
377 	/* Sanity check arguments */
378 	Assert(module_inst);
379 	Assert(module_inst->hw);
380 
381 	module_inst->hw->HOST.CTRLB.bit.VBUSOK = 1;
382 }
383 
384 /**
385  * \brief Send the USB reset
386  *
387  * \param module_inst Pointer to USB software instance struct
388  */
usb_host_send_reset(struct usb_module * module_inst)389 static inline void usb_host_send_reset(struct usb_module *module_inst)
390 {
391 	/* Sanity check arguments */
392 	Assert(module_inst);
393 	Assert(module_inst->hw);
394 
395 	module_inst->hw->HOST.CTRLB.bit.BUSRESET = 1;
396 }
397 
398 /**
399  * \brief Enable the USB SOF generation
400  *
401  * \param module_inst Pointer to USB software instance struct
402  */
usb_host_enable_sof(struct usb_module * module_inst)403 static inline void usb_host_enable_sof(struct usb_module *module_inst)
404 {
405 	/* Sanity check arguments */
406 	Assert(module_inst);
407 	Assert(module_inst->hw);
408 
409 	module_inst->hw->HOST.CTRLB.bit.SOFE = 1;
410 }
411 
412 /**
413  * \brief Disable the USB SOF generation
414  *
415  * \param module_inst Pointer to USB software instance struct
416  */
usb_host_disable_sof(struct usb_module * module_inst)417 static inline void usb_host_disable_sof(struct usb_module *module_inst)
418 {
419 	/* Sanity check arguments */
420 	Assert(module_inst);
421 	Assert(module_inst->hw);
422 
423 	module_inst->hw->HOST.CTRLB.bit.SOFE = 0;
424 }
425 
426 /**
427  * \brief Check the USB SOF generation status
428  *
429  * \param module_inst Pointer to USB software instance struct
430  *
431  * \return USB SOF generation status, \c true if SOF generation is ON.
432  */
usb_host_is_sof_enabled(struct usb_module * module_inst)433 static inline bool usb_host_is_sof_enabled(struct usb_module *module_inst)
434 {
435 	/* Sanity check arguments */
436 	Assert(module_inst);
437 	Assert(module_inst->hw);
438 
439 	return module_inst->hw->HOST.CTRLB.bit.SOFE;
440 }
441 
442 /**
443  * \brief Send the USB host resume
444  *
445  * \param module_inst Pointer to USB software instance struct
446  */
usb_host_send_resume(struct usb_module * module_inst)447 static inline void usb_host_send_resume(struct usb_module *module_inst)
448 {
449 	/* Sanity check arguments */
450 	Assert(module_inst);
451 	Assert(module_inst->hw);
452 
453 	module_inst->hw->HOST.CTRLB.bit.RESUME= 1;
454 }
455 
456 /**
457  * \brief Send the USB host LPM resume
458  *
459  * \param module_inst Pointer to USB software instance struct
460  */
usb_host_send_l1_resume(struct usb_module * module_inst)461 static inline void usb_host_send_l1_resume(struct usb_module *module_inst)
462 {
463 	/* Sanity check arguments */
464 	Assert(module_inst);
465 	Assert(module_inst->hw);
466 
467 	module_inst->hw->HOST.CTRLB.bit.L1RESUME = 1;
468 }
469 
470 /**
471  * \brief Get the speed mode of USB host
472  *
473  * \param module_inst Pointer to USB module instance struct
474  *
475  * \return USB speed mode (\ref usb_speed).
476  */
usb_host_get_speed(struct usb_module * module_inst)477 static inline enum usb_speed usb_host_get_speed(struct usb_module *module_inst)
478 {
479 	/* Sanity check arguments */
480 	Assert(module_inst);
481 	Assert(module_inst->hw);
482 
483 	if (module_inst->hw->HOST.STATUS.bit.SPEED == 0) {
484 		return USB_SPEED_FULL;
485 	} else {
486 		return USB_SPEED_LOW;
487 	}
488 }
489 
490 /**
491  * \brief Get the frame number
492  *
493  * \param module_inst Pointer to USB software instance struct
494  *
495  * \return frame number value.
496  */
usb_host_get_frame_number(struct usb_module * module_inst)497 static inline uint16_t usb_host_get_frame_number(struct usb_module *module_inst)
498 {
499 	/* Sanity check arguments */
500 	Assert(module_inst);
501 	Assert(module_inst->hw);
502 
503 	return (uint16_t)(module_inst->hw->HOST.FNUM.bit.FNUM);
504 }
505 #endif
506 
507 /**
508  * \brief Attach USB device to the bus
509  *
510  * \param module_inst Pointer to USB device module instance
511  */
usb_device_attach(struct usb_module * module_inst)512 static inline void usb_device_attach(struct usb_module *module_inst)
513 {
514 	module_inst->hw->DEVICE.CTRLB.reg &= ~USB_DEVICE_CTRLB_DETACH;
515 }
516 
517 /**
518  * \brief Detach USB device from the bus
519  *
520  * \param module_inst Pointer to USB device module instance
521  */
usb_device_detach(struct usb_module * module_inst)522 static inline void usb_device_detach(struct usb_module *module_inst)
523 {
524 	module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_DETACH;
525 }
526 
527 /**
528  * \brief Get the speed mode of USB device
529  *
530  * \param module_inst Pointer to USB device module instance
531  * \return USB Speed mode (\ref usb_speed).
532  */
usb_device_get_speed(struct usb_module * module_inst)533 static inline enum usb_speed usb_device_get_speed(struct usb_module *module_inst)
534 {
535 	if (!(module_inst->hw->DEVICE.STATUS.reg & USB_DEVICE_STATUS_SPEED_Msk)) {
536 		return USB_SPEED_FULL;
537 	} else {
538 		return USB_SPEED_LOW;
539 	}
540 }
541 
542 /**
543  * \brief Get the address of USB device
544  *
545  * \param module_inst Pointer to USB device module instance
546  * \return USB device address value.
547  */
usb_device_get_address(struct usb_module * module_inst)548 static inline uint8_t usb_device_get_address(struct usb_module *module_inst)
549 {
550 	return ((uint8_t)(module_inst->hw->DEVICE.DADD.bit.DADD));
551 }
552 
553 /**
554  * \brief Set the speed mode of USB device
555  *
556  * \param module_inst Pointer to USB device module instance
557  * \param address     USB device address value
558  */
usb_device_set_address(struct usb_module * module_inst,uint8_t address)559 static inline void usb_device_set_address(struct usb_module *module_inst, uint8_t address)
560 {
561 	module_inst->hw->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | address;
562 }
563 
564 /**
565  * \brief Get the frame number of USB device
566  *
567  * \param module_inst Pointer to USB device module instance
568  * \return USB device frame number value.
569  */
usb_device_get_frame_number(struct usb_module * module_inst)570 static inline uint16_t usb_device_get_frame_number(struct usb_module *module_inst)
571 {
572 	return ((uint16_t)(module_inst->hw->DEVICE.FNUM.bit.FNUM));
573 }
574 
575 /**
576  * \brief Get the micro-frame number of USB device
577  *
578  * \param module_inst Pointer to USB device module instance
579  * \return USB device micro-frame number value.
580  */
usb_device_get_micro_frame_number(struct usb_module * module_inst)581 static inline uint16_t usb_device_get_micro_frame_number(struct usb_module *module_inst)
582 {
583 	return ((uint16_t)(module_inst->hw->DEVICE.FNUM.reg));
584 }
585 
586 /**
587  * \brief USB device send the resume wakeup
588  *
589  * \param module_inst Pointer to USB device module instance
590  */
usb_device_send_remote_wake_up(struct usb_module * module_inst)591 static inline void usb_device_send_remote_wake_up(struct usb_module *module_inst)
592 {
593 	module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_UPRSM;
594 }
595 
596 /**
597  * \brief USB device set the LPM mode
598  *
599  * \param module_inst Pointer to USB device module instance
600  * \param lpm_mode    LPM mode
601  */
usb_device_set_lpm_mode(struct usb_module * module_inst,enum usb_device_lpm_mode lpm_mode)602 static inline void usb_device_set_lpm_mode(struct usb_module *module_inst,
603 		enum usb_device_lpm_mode lpm_mode)
604 {
605 	module_inst->hw->DEVICE.CTRLB.bit.LPMHDSK = lpm_mode;
606 }
607 
608 /**
609  * \name USB Host Callback Management
610  * @{
611  */
612 enum status_code usb_host_register_callback(struct usb_module *module_inst,
613 		enum usb_host_callback callback_type,
614 		usb_host_callback_t callback_func);
615 enum status_code usb_host_unregister_callback(struct usb_module *module_inst,
616 		enum usb_host_callback callback_type);
617 enum status_code usb_host_enable_callback(struct usb_module *module_inst,
618 		enum usb_host_callback callback_type);
619 enum status_code usb_host_disable_callback(struct usb_module *module_inst,
620 		enum usb_host_callback callback_type);
621 /** @} */
622 
623 /**
624  * \name USB Device Callback Management
625  * @{
626  */
627 enum status_code usb_device_register_callback(struct usb_module *module_inst,
628 		enum usb_device_callback callback_type,
629 		usb_device_callback_t callback_func);
630 enum status_code usb_device_unregister_callback(struct usb_module *module_inst,
631 		enum usb_device_callback callback_type);
632 enum status_code usb_device_enable_callback(struct usb_module *module_inst,
633 		enum usb_device_callback callback_type);
634 enum status_code usb_device_disable_callback(struct usb_module *module_inst,
635 		enum usb_device_callback callback_type);
636 /** @} */
637 
638 /**
639  * \name USB Host Pipe Configuration
640  * @{
641  */
642 void usb_host_pipe_get_config_defaults(struct usb_host_pipe_config *ep_config);
643 enum status_code usb_host_pipe_set_config(struct usb_module *module_inst, uint8_t pipe_num,
644 		struct usb_host_pipe_config *ep_config);
645 enum status_code usb_host_pipe_get_config(struct usb_module *module_inst, uint8_t pipe_num,
646 		struct usb_host_pipe_config *ep_config);
647 /** @} */
648 
649 /**
650  * \name USB Device Endpoint Configuration
651  * @{
652  */
653 void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config);
654 enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst,
655 		struct usb_device_endpoint_config *ep_config);
656 bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep);
657 /** @} */
658 
659 /**
660  * \name USB Host Pipe Callback Management
661  * @{
662  */
663 enum status_code usb_host_pipe_register_callback(
664 		struct usb_module *module_inst, uint8_t pipe_num,
665 		enum usb_host_pipe_callback callback_type,
666 		usb_host_pipe_callback_t callback_func);
667 enum status_code usb_host_pipe_unregister_callback(
668 		struct usb_module *module_inst, uint8_t pipe_num,
669 		enum usb_host_pipe_callback callback_type);
670 enum status_code usb_host_pipe_enable_callback(
671 		struct usb_module *module_inst, uint8_t pipe_num,
672 		enum usb_host_pipe_callback callback_type);
673 enum status_code usb_host_pipe_disable_callback(
674 		struct usb_module *module_inst, uint8_t pipe_num,
675 		enum usb_host_pipe_callback callback_type);
676 /** @} */
677 
678 /**
679  * \name USB Device Endpoint Callback Management
680  * @{
681  */
682 enum status_code usb_device_endpoint_register_callback(
683 		struct usb_module *module_inst, uint8_t ep_num,
684 		enum usb_device_endpoint_callback callback_type,
685 		usb_device_endpoint_callback_t callback_func);
686 enum status_code usb_device_endpoint_unregister_callback(
687 		struct usb_module *module_inst, uint8_t ep_num,
688 		enum usb_device_endpoint_callback callback_type);
689 enum status_code usb_device_endpoint_enable_callback(
690 		struct usb_module *module_inst, uint8_t ep,
691 		enum usb_device_endpoint_callback callback_type);
692 enum status_code usb_device_endpoint_disable_callback(
693 		struct usb_module *module_inst, uint8_t ep,
694 		enum usb_device_endpoint_callback callback_type);
695 /** @} */
696 
697 /**
698  * \name USB Host Pipe Job Management
699  * @{
700  */
701 enum status_code usb_host_pipe_setup_job(struct usb_module *module_inst,
702 		uint8_t pipe_num, uint8_t *buf);
703 enum status_code usb_host_pipe_read_job(struct usb_module *module_inst,
704 		uint8_t pipe_num, uint8_t *buf, uint32_t buf_size);
705 enum status_code usb_host_pipe_write_job(struct usb_module *module_inst,
706 		uint8_t pipe_num, uint8_t *buf, uint32_t buf_size);
707 enum status_code usb_host_pipe_abort_job(struct usb_module *module_inst, uint8_t pipe_num);
708 enum status_code usb_host_pipe_lpm_job(struct usb_module *module_inst,
709 		uint8_t pipe_num, bool b_remotewakeup, uint8_t hird);
710 /** @} */
711 
712 /**
713  * \name USB Device Endpoint Job Management
714  * @{
715  */
716 enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst,uint8_t ep_num,
717 		uint8_t* pbuf, uint32_t buf_size);
718 enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst,uint8_t ep_num,
719 		uint8_t* pbuf, uint32_t buf_size);
720 enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst,
721 		uint8_t* pbuf);
722 void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep);
723 /** @} */
724 
725 #if !SAMD11 && !SAML22
726 /**
727  * \name USB Host Pipe Operations
728  * @{
729  */
730 
731 /**
732  * \brief Freeze a pipe
733  *
734  * \param module_inst Pointer to USB module instance
735  * \param pipe_num    Pipe number
736  */
usb_host_pipe_freeze(struct usb_module * module_inst,uint8_t pipe_num)737 static inline void usb_host_pipe_freeze(struct usb_module *module_inst, uint8_t pipe_num)
738 {
739 	/* Sanity check arguments */
740 	Assert(module_inst);
741 	Assert(module_inst->hw);
742 
743 	module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSSET.reg = USB_HOST_PSTATUSSET_PFREEZE;
744 }
745 
746 /**
747  * \brief Unfreeze a pipe
748  *
749  * \param module_inst Pointer to USB module instance
750  * \param pipe_num    Pipe number
751  */
usb_host_pipe_unfreeze(struct usb_module * module_inst,uint8_t pipe_num)752 static inline void usb_host_pipe_unfreeze(struct usb_module *module_inst, uint8_t pipe_num)
753 {
754 	/* Sanity check arguments */
755 	Assert(module_inst);
756 	Assert(module_inst->hw);
757 
758 	module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_PFREEZE;
759 }
760 
761 /**
762  * \brief Check if the pipe is frozen
763  *
764  * \param module_inst Pointer to USB module instance
765  * \param pipe_num    Pipe number
766  */
usb_host_pipe_is_frozen(struct usb_module * module_inst,uint8_t pipe_num)767 static inline bool usb_host_pipe_is_frozen(struct usb_module *module_inst, uint8_t pipe_num)
768 {
769 	/* Sanity check arguments */
770 	Assert(module_inst);
771 	Assert(module_inst->hw);
772 
773 	return (module_inst->hw->HOST.HostPipe[pipe_num].PSTATUS.bit.PFREEZE == 1);
774 }
775 
776 /**
777  * \brief Set the data toggle bit of pipe
778  *
779  * \param module_inst Pointer to USB module instance
780  * \param pipe_num    Pipe number
781  */
usb_host_pipe_set_toggle(struct usb_module * module_inst,uint8_t pipe_num)782 static inline void usb_host_pipe_set_toggle(struct usb_module *module_inst, uint8_t pipe_num)
783 {
784 	/* Sanity check arguments */
785 	Assert(module_inst);
786 	Assert(module_inst->hw);
787 
788 	module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSSET.reg = USB_HOST_PSTATUSSET_DTGL;
789 }
790 
791 /**
792  * \brief Clear the data toggle bit of pipe
793  *
794  * \param module_inst Pointer to USB module instance
795  * \param pipe_num    Pipe number
796  */
usb_host_pipe_clear_toggle(struct usb_module * module_inst,uint8_t pipe_num)797 static inline void usb_host_pipe_clear_toggle(struct usb_module *module_inst, uint8_t pipe_num)
798 {
799 	/* Sanity check arguments */
800 	Assert(module_inst);
801 	Assert(module_inst->hw);
802 
803 	module_inst->hw->HOST.HostPipe[pipe_num].PSTATUSCLR.reg = USB_HOST_PSTATUSCLR_DTGL;
804 }
805 
806 /**
807  * \brief Set the auto zero length packet of pipe
808  *
809  * \param module_inst Pointer to USB module instance
810  * \param pipe_num    Pipe number
811  * \param value       \c true to enable auto ZLP and \c false to disable
812  */
813 void usb_host_pipe_set_auto_zlp(struct usb_module *module_inst, uint8_t pipe_num, bool value);
814 
815 /** @} */
816 #endif
817 
818 /**
819  * \name USB Device Endpoint Operations
820  * @{
821  */
822 
823 bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep);
824 void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep);
825 void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep);
826 
827 /** @} */
828 
829 #ifdef __cplusplus
830 }
831 #endif
832 
833 #endif /* USB_H_INCLUDED */
834