1 /**
2  * \file
3  *
4  * \brief Port related functionality declaration.
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 #ifndef _HPL_GPIO_H_INCLUDED
45 #define _HPL_GPIO_H_INCLUDED
46 
47 /**
48  * \addtogroup HPL Port
49  *
50  * \section hpl_port_rev Revision History
51  * - v1.0.0 Initial Release
52  *
53  *@{
54  */
55 
56 #include <compiler.h>
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 /**
62  * \brief Macros for the pin and port group, lower 5
63  * bits stands for pin number in the group, higher 3
64  * bits stands for port group
65  */
66 #define GPIO_PIN(n) (((n)&0x1Fu) << 0)
67 #define GPIO_PORT(n) ((n) >> 5)
68 #define GPIO(port, pin) ((((port)&0x7u) << 5) + ((pin)&0x1Fu))
69 #define GPIO_PIN_FUNCTION_OFF 0xffffffff
70 
71 /**
72  * \brief PORT pull mode settings
73  */
74 enum gpio_pull_mode { GPIO_PULL_OFF, GPIO_PULL_UP, GPIO_PULL_DOWN };
75 
76 /**
77  * \brief PORT direction settins
78  */
79 enum gpio_direction { GPIO_DIRECTION_OFF, GPIO_DIRECTION_IN, GPIO_DIRECTION_OUT };
80 
81 /**
82  * \brief PORT group abstraction
83  */
84 
85 enum gpio_port { GPIO_PORTA, GPIO_PORTB, GPIO_PORTC, GPIO_PORTD, GPIO_PORTE };
86 
87 /**
88  * \name HPL functions
89  */
90 //@{
91 /**
92  * \brief Port initialization function
93  *
94  * Port initialization function should setup the port module based
95  * on a static configuration file, this function should normally
96  * not be called directly, but is a part of hal_init()
97  */
98 void _gpio_init(void);
99 
100 /**
101  * \brief Set direction on port with mask
102  *
103  * Set data direction for each pin, or disable the pin
104  *
105  * \param[in] port      Ports are grouped into groups of maximum 32 pins,
106  *                      GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
107  * \param[in] mask      Bit mask where 1 means apply direction setting to the
108  *                      corresponding pin
109  * \param[in] direction GPIO_DIRECTION_OFF  = set pin direction to input
110  *                      and disable input buffer to disable the pin
111  *                      GPIO_DIRECTION_IN   = set pin direction to input
112  *                      and enable input buffer to enable the pin
113  *                      GPIO_DIRECTION_OUT  = set pin direction to output
114  *                      and disable input buffer
115  */
116 static inline void _gpio_set_direction(const enum gpio_port port, const uint32_t mask,
117                                        const enum gpio_direction direction);
118 
119 /**
120  * \brief Set output level on port with mask
121  *
122  * Sets output state on pin to high or low with pin masking
123  *
124  * \param[in] port  Ports are grouped into groups of maximum 32 pins,
125  *                  GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
126  * \param[in] mask  Bit mask where 1 means apply direction setting to
127  *                  the corresponding pin
128  * \param[in] level true  = pin level is set to 1
129  *                  false = pin level is set to 0
130  */
131 static inline void _gpio_set_level(const enum gpio_port port, const uint32_t mask, const bool level);
132 
133 /**
134  * \brief Change output level to the opposite with mask
135  *
136  * Change pin output level to the opposite with pin masking
137  *
138  * \param[in] port  Ports are grouped into groups of maximum 32 pins,
139  *                  GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
140  * \param[in] mask  Bit mask where 1 means apply direction setting to
141  *                  the corresponding pin
142  */
143 static inline void _gpio_toggle_level(const enum gpio_port port, const uint32_t mask);
144 
145 /**
146  * \brief Get input levels on all port pins
147  *
148  * Get input level on all port pins, will read IN register if configured to
149  * input and OUT register if configured as output
150  *
151  * \param[in] port  Ports are grouped into groups of maximum 32 pins,
152  *                  GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
153  */
154 static inline uint32_t _gpio_get_level(const enum gpio_port port);
155 
156 /**
157  * \brief Set pin pull mode
158  *
159  * Set pull mode on a single pin
160  *
161  * \notice This function will automatically change pin direction to input
162  *
163  * \param[in] port      Ports are grouped into groups of maximum 32 pins,
164  *                      GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
165  * \param[in] pin       The pin in the group that pull mode should be selected
166  *                      for
167  * \param[in] pull_mode GPIO_PULL_OFF  = pull resistor on pin is disabled
168  *                      GPIO_PULL_DOWN = pull resistor on pin will pull pin
169  *                      level to ground level
170  *                      GPIO_PULL_UP   = pull resistor on pin will pull pin
171  *                      level to VCC
172  */
173 static inline void _gpio_set_pin_pull_mode(const enum gpio_port port, const uint8_t pin,
174                                            const enum gpio_pull_mode pull_mode);
175 
176 /**
177  * \brief Set gpio function
178  *
179  * Select which function a gpio is used for
180  *
181  * \param[in] gpio     The gpio to set function for
182  * \param[in] function The gpio function is given by a 32-bit wide bitfield
183  *                     found in the header files for the device
184  *
185  */
186 static inline void _gpio_set_pin_function(const uint32_t gpio, const uint32_t function);
187 
188 #include <hpl_gpio_base.h>
189 //@}
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 /**@}*/
195 #endif /* _HPL_GPIO_H_INCLUDED */
196