1 /*****************************************************************************
2 *
3 * \file
4 *
5 * \brief GPIO software driver interface for AVR UC3.
6 *
7 * Copyright (c) 2010-2018 Microchip Technology Inc. and its subsidiaries.
8 *
9 * \asf_license_start
10 *
11 * \page License
12 *
13 * Subject to your compliance with these terms, you may use Microchip
14 * software and any derivatives exclusively with Microchip products.
15 * It is your responsibility to comply with third party license terms applicable
16 * to your use of third party software (including open source software) that
17 * may accompany Microchip software.
18 *
19 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
20 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
21 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
22 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
23 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
24 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
25 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
26 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
27 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
28 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
29 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
30 *
31 * \asf_license_stop
32 *
33 *****************************************************************************/
34 /*
35 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
36 */
37
38 #ifndef _GPIO_H_
39 #define _GPIO_H_
40
41 /**
42 * \defgroup group_avr32_drivers_gpio GPIO - General-Purpose Input/Output
43 *
44 * GPIO gives access to the MCU pins.
45 *
46 * @{
47 */
48
49 #include <avr32/io.h>
50 #include "compiler.h"
51
52 /** \name Return Values of the GPIO API
53 * @{ */
54 #define GPIO_SUCCESS 0 /**< Function successfully completed. */
55 #define GPIO_INVALID_ARGUMENT 1 /**< Input parameters are out of range. */
56 /** @} */
57
58 /** \name Interrupt Trigger Modes
59 * @{ */
60 #define GPIO_PIN_CHANGE 0 /**< Interrupt triggered upon pin change. */
61 #define GPIO_RISING_EDGE 1 /**< Interrupt triggered upon rising edge. */
62 #define GPIO_FALLING_EDGE 2 /**< Interrupt triggered upon falling edge. */
63 /** @} */
64
65 /** \name Common defines for GPIO_FLAGS parameter
66 * @{ */
67 #define GPIO_DIR_INPUT (0 << 0) /**< Pin is Input */
68 #define GPIO_DIR_OUTPUT (1 << 0) /**< Pin is Output */
69 #define GPIO_INIT_LOW (0 << 1) /**< Initial Output State is Low */
70 #define GPIO_INIT_HIGH (1 << 1) /**< Initial Output State is High */
71 #define GPIO_PULL_UP (1 << 2) /**< Pull-Up (when input) */
72 #define GPIO_PULL_DOWN (2 << 2) /**< Pull-Down (when input) */
73 #define GPIO_BUSKEEPER (3 << 2) /**< Bus Keeper */
74 #define GPIO_DRIVE_MIN (0 << 4) /**< Drive Min Configuration */
75 #define GPIO_DRIVE_LOW (1 << 4) /**< Drive Low Configuration */
76 #define GPIO_DRIVE_HIGH (2 << 4) /**< Drive High Configuration */
77 #define GPIO_DRIVE_MAX (3 << 4) /**< Drive Max Configuration */
78 #define GPIO_OPEN_DRAIN (1 << 6) /**< Open-Drain (when output) */
79 #define GPIO_INTERRUPT (1 << 7) /**< Enable Pin/Group Interrupt */
80 #define GPIO_BOTHEDGES (3 << 7) /**< Sense Both Edges */
81 #define GPIO_RISING (5 << 7) /**< Sense Rising Edge */
82 #define GPIO_FALLING (7 << 7) /**< Sense Falling Edge */
83 /** @} */
84
85 /** A type definition of pins and modules connectivity. */
86 typedef struct {
87 uint32_t pin; /**< Module pin. */
88 uint32_t function; /**< Module function. */
89 } gpio_map_t[];
90
91 /** \name Peripheral Bus Interface
92 *
93 * Low-speed interface with a non-deterministic number of clock cycles per
94 * access.
95 *
96 * This interface operates with lower clock frequencies (fPB <= fCPU), and its
97 * timing is not deterministic since it needs to access a shared bus which may
98 * be heavily loaded.
99 *
100 * \note This interface is immediately available without initialization.
101 *
102 * @{
103 */
104
105 uint32_t gpio_enable_module(const gpio_map_t gpiomap, uint32_t size);
106
107 uint32_t gpio_enable_module_pin(uint32_t pin, uint32_t function);
108
109 void gpio_enable_gpio(const gpio_map_t gpiomap, uint32_t size);
110
111 void gpio_enable_gpio_pin(uint32_t pin);
112
113 void gpio_enable_pin_pull_up(uint32_t pin);
114
115 void gpio_disable_pin_pull_up(uint32_t pin);
116
117 #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
118 defined(AVR32_GPIO_212_H_INCLUDED)
119
120 void gpio_enable_pin_pull_down(uint32_t pin);
121
122 void gpio_disable_pin_pull_down(uint32_t pin);
123
124 void gpio_enable_pin_buskeeper(uint32_t pin);
125
126 void gpio_disable_pin_buskeeper(uint32_t pin);
127
128 #endif
129
130 void gpio_configure_pin(uint32_t pin, uint32_t flags);
131
132 void gpio_configure_group(uint32_t port, uint32_t mask, uint32_t flags);
133
134 bool gpio_get_pin_value(uint32_t pin);
135
136 /**
137 * \brief Check if the pin is in low logical level.
138 *
139 * \param pin The pin number.
140 * \return bool \c true if the pin is in low logical level
141 * \c false if the pin is not in low logical level
142 */
gpio_pin_is_low(uint32_t pin)143 __always_inline static bool gpio_pin_is_low(uint32_t pin)
144 {
145 return (gpio_get_pin_value(pin) == 0);
146 }
147
148 /**
149 * \brief Check if the pin is in high logical level.
150 *
151 * \param pin The pin number.
152 * \return bool \c true if the pin is in high logical level
153 * \c false if the pin is not in high logical level
154 */
gpio_pin_is_high(uint32_t pin)155 __always_inline static bool gpio_pin_is_high(uint32_t pin)
156 {
157 return (gpio_get_pin_value(pin) != 0);
158 }
159
160 bool gpio_get_gpio_pin_output_value(uint32_t pin);
161
162 bool gpio_get_gpio_open_drain_pin_output_value(uint32_t pin);
163
164 void gpio_set_gpio_pin(uint32_t pin);
165
166 void gpio_set_pin_high(uint32_t pin);
167
168 void gpio_set_group_high(uint32_t port, uint32_t mask);
169
170 void gpio_clr_gpio_pin(uint32_t pin);
171
172 void gpio_set_pin_low(uint32_t pin);
173
174 void gpio_set_group_low(uint32_t port, uint32_t mask);
175
176 void gpio_tgl_gpio_pin(uint32_t pin);
177
178 void gpio_toggle_pin(uint32_t pin);
179
180 void gpio_toggle_group(uint32_t port, uint32_t mask);
181
182 void gpio_set_gpio_open_drain_pin(uint32_t pin);
183
184 void gpio_clr_gpio_open_drain_pin(uint32_t pin);
185
186 void gpio_tgl_gpio_open_drain_pin(uint32_t pin);
187
188 void gpio_enable_pin_glitch_filter(uint32_t pin);
189
190 void gpio_disable_pin_glitch_filter(uint32_t pin);
191
192 uint32_t gpio_enable_pin_interrupt(uint32_t pin, uint32_t mode);
193
194 void gpio_disable_pin_interrupt(uint32_t pin);
195
196 bool gpio_get_pin_interrupt_flag(uint32_t pin);
197
198 void gpio_clear_pin_interrupt_flag(uint32_t pin);
199
200 /** @} */
201
202 #if (defined AVR32_GPIO_LOCAL_ADDRESS) || defined(__DOXYGEN__)
203
204 /** \name Local Bus Interface
205 *
206 * High-speed interface with only one clock cycle per access.
207 *
208 * This interface operates with high clock frequency (fCPU), and its timing is
209 * deterministic since it does not need to access a shared bus which may be
210 * heavily loaded.
211 *
212 * \warning To use this interface, the clock frequency of the peripheral bus on
213 * which the GPIO peripheral is connected must be set to the CPU clock
214 * frequency (fPB = fCPU).
215 *
216 * \note This interface has to be initialized in order to be available.
217 *
218 * @{
219 */
220
221 /** \brief Enables the local bus interface for GPIO.
222 *
223 * \note This function must have been called at least once before using other
224 * functions in this interface.
225 */
gpio_local_init(void)226 __always_inline static void gpio_local_init(void)
227 {
228 Set_system_register(AVR32_CPUCR,
229 Get_system_register(AVR32_CPUCR) | AVR32_CPUCR_LOCEN_MASK);
230 }
231
232 /** \brief Enables the output driver of a pin.
233 *
234 * \param pin The pin number.
235 *
236 * \note \ref gpio_local_init must have been called beforehand.
237 *
238 * \note This function does not enable the GPIO mode of the pin.
239 * \ref gpio_enable_gpio_pin can be called for this purpose.
240 */
gpio_local_enable_pin_output_driver(uint32_t pin)241 __always_inline static void gpio_local_enable_pin_output_driver(uint32_t pin)
242 {
243 AVR32_GPIO_LOCAL.port[pin >> 5].oders = 1 << (pin & 0x1F);
244 }
245
246 /** \brief Disables the output driver of a pin.
247 *
248 * \param pin The pin number.
249 *
250 * \note \ref gpio_local_init must have been called beforehand.
251 */
gpio_local_disable_pin_output_driver(uint32_t pin)252 __always_inline static void gpio_local_disable_pin_output_driver(uint32_t pin)
253 {
254 AVR32_GPIO_LOCAL.port[pin >> 5].oderc = 1 << (pin & 0x1F);
255 }
256
257 /** \brief Returns the value of a pin.
258 *
259 * \param pin The pin number.
260 *
261 * \return The pin value.
262 *
263 * \note \ref gpio_local_init must have been called beforehand.
264 */
gpio_local_get_pin_value(uint32_t pin)265 __always_inline static bool gpio_local_get_pin_value(uint32_t pin)
266 {
267 return (AVR32_GPIO_LOCAL.port[pin >> 5].pvr >> (pin & 0x1F)) & 1;
268 }
269
270 /** \brief Drives a GPIO pin to 1.
271 *
272 * \param pin The pin number.
273 *
274 * \note \ref gpio_local_init must have been called beforehand.
275 *
276 * \note This function does not enable the GPIO mode of the pin nor its output
277 * driver. \ref gpio_enable_gpio_pin and
278 * \ref gpio_local_enable_pin_output_driver can be called for this
279 * purpose.
280 */
gpio_local_set_gpio_pin(uint32_t pin)281 __always_inline static void gpio_local_set_gpio_pin(uint32_t pin)
282 {
283 AVR32_GPIO_LOCAL.port[pin >> 5].ovrs = 1 << (pin & 0x1F);
284 }
285
286 /** \brief Drives a GPIO pin to 0.
287 *
288 * \param pin The pin number.
289 *
290 * \note \ref gpio_local_init must have been called beforehand.
291 *
292 * \note This function does not enable the GPIO mode of the pin nor its output
293 * driver. \ref gpio_enable_gpio_pin and
294 * \ref gpio_local_enable_pin_output_driver can be called for this
295 * purpose.
296 */
gpio_local_clr_gpio_pin(uint32_t pin)297 __always_inline static void gpio_local_clr_gpio_pin(uint32_t pin)
298 {
299 AVR32_GPIO_LOCAL.port[pin >> 5].ovrc = 1 << (pin & 0x1F);
300 }
301
302 /** \brief Toggles a GPIO pin.
303 *
304 * \param pin The pin number.
305 *
306 * \note \ref gpio_local_init must have been called beforehand.
307 *
308 * \note This function does not enable the GPIO mode of the pin nor its output
309 * driver. \ref gpio_enable_gpio_pin and
310 * \ref gpio_local_enable_pin_output_driver can be called for this
311 * purpose.
312 */
gpio_local_tgl_gpio_pin(uint32_t pin)313 __always_inline static void gpio_local_tgl_gpio_pin(uint32_t pin)
314 {
315 AVR32_GPIO_LOCAL.port[pin >> 5].ovrt = 1 << (pin & 0x1F);
316 }
317
318 /** \brief Initializes the configuration of a GPIO pin so that it can be used
319 * with GPIO open-drain functions.
320 *
321 * \note This function must have been called at least once before using
322 * \ref gpio_local_set_gpio_open_drain_pin,
323 * \ref gpio_local_clr_gpio_open_drain_pin or
324 * \ref gpio_local_tgl_gpio_open_drain_pin.
325 */
gpio_local_init_gpio_open_drain_pin(uint32_t pin)326 __always_inline static void gpio_local_init_gpio_open_drain_pin(uint32_t pin)
327 {
328 AVR32_GPIO_LOCAL.port[pin >> 5].ovrc = 1 << (pin & 0x1F);
329 }
330
331 /** \brief Drives a GPIO pin to 1 using open drain.
332 *
333 * \param pin The pin number.
334 *
335 * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
336 * have been called beforehand.
337 *
338 * \note This function does not enable the GPIO mode of the pin.
339 * \ref gpio_enable_gpio_pin can be called for this purpose.
340 */
gpio_local_set_gpio_open_drain_pin(uint32_t pin)341 __always_inline static void gpio_local_set_gpio_open_drain_pin(uint32_t pin)
342 {
343 AVR32_GPIO_LOCAL.port[pin >> 5].oderc = 1 << (pin & 0x1F);
344 }
345
346 /** \brief Drives a GPIO pin to 0 using open drain.
347 *
348 * \param pin The pin number.
349 *
350 * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
351 * have been called beforehand.
352 *
353 * \note This function does not enable the GPIO mode of the pin.
354 * \ref gpio_enable_gpio_pin can be called for this purpose.
355 */
gpio_local_clr_gpio_open_drain_pin(uint32_t pin)356 __always_inline static void gpio_local_clr_gpio_open_drain_pin(uint32_t pin)
357 {
358 AVR32_GPIO_LOCAL.port[pin >> 5].oders = 1 << (pin & 0x1F);
359 }
360
361 /** \brief Toggles a GPIO pin using open drain.
362 *
363 * \param pin The pin number.
364 *
365 * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
366 * have been called beforehand.
367 *
368 * \note This function does not enable the GPIO mode of the pin.
369 * \ref gpio_enable_gpio_pin can be called for this purpose.
370 */
gpio_local_tgl_gpio_open_drain_pin(uint32_t pin)371 __always_inline static void gpio_local_tgl_gpio_open_drain_pin(uint32_t pin)
372 {
373 AVR32_GPIO_LOCAL.port[pin >> 5].odert = 1 << (pin & 0x1F);
374 }
375
376 /** @} */
377 #endif /* AVR32_GPIO_LOCAL_ADDRESS */
378
379 #if UC3L
380
381 /** \name Peripheral Event System support
382 *
383 * The GPIO can be programmed to output peripheral events whenever an interrupt
384 * condition is detected, such as pin value change, or only when a rising or
385 * falling edge is detected.
386 *
387 * @{
388 */
389
390 /** \brief Enables the peripheral event generation of a pin.
391 *
392 * \param pin The pin number.
393 *
394 */
gpio_enable_pin_periph_event(uint32_t pin)395 __always_inline static void gpio_enable_pin_periph_event(uint32_t pin)
396 {
397 AVR32_GPIO.port[pin >> 5].oderc = 1 << (pin & 0x1F); /* The GPIO output
398 * driver is
399 * disabled for
400 * that pin. */
401 AVR32_GPIO.port[pin >> 5].evers = 1 << (pin & 0x1F);
402 }
403
404 /** \brief Disables the peripheral event generation of a pin.
405 *
406 * \param pin The pin number.
407 *
408 */
gpio_disable_pin_periph_event(uint32_t pin)409 __always_inline static void gpio_disable_pin_periph_event(uint32_t pin)
410 {
411 AVR32_GPIO.port[pin >> 5].everc = 1 << (pin & 0x1F);
412 }
413
414 uint32_t gpio_configure_pin_periph_event_mode(uint32_t pin, uint32_t mode,
415 uint32_t use_igf);
416
417 /** @} */
418
419 #endif
420
421 /** @} */
422
423 #endif /* _GPIO_H_ */
424