1 /**
2 * \file
3 *
4 * \brief SAM USB Dual Role driver file.
5 *
6 * Copyright (C) 2014-2015 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 #include <compiler.h>
47 #include "usb_dual.h"
48
49 #ifndef UDD_ENABLE
50 # define udc_start()
51 # define udc_stop()
52 #else
53 #include <udc.h>
54 #endif
55
56 #ifndef UHD_ENABLE
57 # define uhc_start(void)
58 # define uhc_stop(b_id_stop)
59 #else
60 #include <uhc.h>
61 #endif
62
63 /* State of USB dual role initialization */
64 static bool _initialized = false;
65
66 #define _usb_is_id_device() port_pin_get_input_level(USB_ID_PIN)
67
68 #if USB_ID_EIC
69 static void usb_id_handler(void);
70
71 /**
72 * \name USB ID PAD management
73 *
74 * @{
75 */
76
77 /**
78 * USB ID pin configuration
79 */
usb_id_config(void)80 static void usb_id_config(void)
81 {
82 struct extint_chan_conf eint_chan_conf;
83 extint_chan_get_config_defaults(&eint_chan_conf);
84
85 eint_chan_conf.gpio_pin = USB_ID_PIN;
86 eint_chan_conf.gpio_pin_mux = USB_ID_EIC_MUX;
87 eint_chan_conf.detection_criteria = EXTINT_DETECT_BOTH;
88 eint_chan_conf.filter_input_signal = true;
89
90 extint_chan_disable_callback(USB_ID_EIC_LINE,
91 EXTINT_CALLBACK_TYPE_DETECT);
92 extint_chan_set_config(USB_ID_EIC_LINE, &eint_chan_conf);
93 extint_register_callback(usb_id_handler,
94 USB_ID_EIC_LINE,
95 EXTINT_CALLBACK_TYPE_DETECT);
96 extint_chan_enable_callback(USB_ID_EIC_LINE,
97 EXTINT_CALLBACK_TYPE_DETECT);
98 }
99
100 /**
101 * USB ID pin change handler
102 */
usb_id_handler(void)103 static void usb_id_handler(void)
104 {
105 extint_chan_disable_callback(USB_ID_EIC_LINE,
106 EXTINT_CALLBACK_TYPE_DETECT);
107 if (_usb_is_id_device()) {
108 uhc_stop(false);
109 UHC_MODE_CHANGE(false);
110 udc_start();
111 } else {
112 udc_stop();
113 UHC_MODE_CHANGE(true);
114 uhc_start();
115 }
116 extint_chan_enable_callback(USB_ID_EIC_LINE,
117 EXTINT_CALLBACK_TYPE_DETECT);
118 }
119 #endif
120 /** @} */
121
122 /**
123 * \brief Initialize the USB peripheral and set right role according to ID pin
124 *
125 * \return \c true if the ID pin management has been started, otherwise \c false.
126 */
usb_dual_enable(void)127 bool usb_dual_enable(void)
128 {
129 if (_initialized) {
130 return false; // Dual role already initialized
131 }
132
133 #if USB_ID_EIC
134 _initialized = true;
135
136 struct port_config pin_conf;
137 port_get_config_defaults(&pin_conf);
138
139 /* Set USB ID Pin as inputs */
140 pin_conf.direction = PORT_PIN_DIR_INPUT;
141 pin_conf.input_pull = PORT_PIN_PULL_UP;
142 port_pin_set_config(USB_ID_PIN, &pin_conf);
143
144 usb_id_config();
145 if (_usb_is_id_device()) {
146 UHC_MODE_CHANGE(false);
147 udc_start();
148 } else {
149 UHC_MODE_CHANGE(true);
150 uhc_start();
151 }
152
153 /**
154 * End of host or device startup,
155 * the current mode selected is already started now
156 */
157 return true; // ID pin management has been enabled
158 #else
159 return false; // ID pin management has not been enabled
160 #endif
161 }
162
163 /**
164 * \brief Deinitialize the dual role driver
165 */
usb_dual_disable(void)166 void usb_dual_disable(void)
167 {
168 if (!_initialized) {
169 return; // Dual role not initialized
170 }
171 _initialized = false;
172
173 #if USB_ID_EIC
174 extint_chan_disable_callback(USB_ID_EIC_LINE,
175 EXTINT_CALLBACK_TYPE_DETECT);
176 #endif
177 }
178
179