1 /**
2  * \file
3  *
4  * \brief Port
5  *
6  * Copyright (C) 2014 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 #ifndef _HAL_GPIO_INCLUDED_
43 #define _HAL_GPIO_INCLUDED_
44 
45 #include <hpl_gpio.h>
46 #include <utils_assert.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /**
53  * \brief Set gpio pull mode
54  *
55  * Set pin pull mode, non existing pull modes throws an fatal assert
56  *
57  * \param[in] pin       The pin number for device
58  * \param[in] pull_mode GPIO_PULL_DOWN = Pull pin high with internal
59  *                      resistor
60  *                      GPIO_PULL_UP   = Pull pin low with internal resistor
61  *                      GPIO_PULL_OFF  = Disable pin pull mode
62  */
gpio_set_pin_pull_mode(const uint8_t pin,const enum gpio_pull_mode pull_mode)63 static inline void gpio_set_pin_pull_mode(const uint8_t pin, const enum gpio_pull_mode pull_mode)
64 {
65 	_gpio_set_pin_pull_mode((enum gpio_port)GPIO_PORT(pin), pin & 0x1F, pull_mode);
66 }
67 
68 /**
69  * \brief Set pin function
70  *
71  * Select which function a pin will be used for
72  *
73  * \param[in] pin       The pin number for device
74  * \param[in] function  The pin function is given by a 32-bit wide bitfield
75  *                      found in the header files for the device
76  *
77  */
gpio_set_pin_function(const uint32_t pin,uint32_t function)78 static inline void gpio_set_pin_function(const uint32_t pin, uint32_t function)
79 {
80 	_gpio_set_pin_function(pin, function);
81 }
82 
83 /**
84  * \brief Set port data direction
85  *
86  * Select if the pin data direction is input, output or disabled.
87  * If disabled state is not possible, this function throws an assert.
88  *
89  * \param[in] port      Ports are grouped into groups of maximum 32 pins,
90  *                      GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
91  * \param[in] mask      Bit mask where 1 means apply direction setting to the
92  *                      corresponding pin
93  * \param[in] direction GPIO_DIRECTION_IN  = Data direction in
94  *                      GPIO_DIRECTION_OUT = Data direction out
95  *                      GPIO_DIRECTION_OFF = Disables the pin
96  *                      (low power state)
97  */
gpio_set_port_direction(const enum gpio_port port,const uint32_t mask,const enum gpio_direction direction)98 static inline void gpio_set_port_direction(const enum gpio_port port, const uint32_t mask,
99                                            const enum gpio_direction direction)
100 {
101 	_gpio_set_direction(port, mask, direction);
102 }
103 
104 /**
105  * \brief Set gpio data direction
106  *
107  * Select if the pin data direction is input, output or disabled.
108  * If disabled state is not possible, this function throws an assert.
109  *
110  * \param[in] pin       The pin number for device
111  * \param[in] direction GPIO_DIRECTION_IN  = Data direction in
112  *                      GPIO_DIRECTION_OUT = Data direction out
113  *                      GPIO_DIRECTION_OFF = Disables the pin
114  *                      (low power state)
115  */
gpio_set_pin_direction(const uint8_t pin,const enum gpio_direction direction)116 static inline void gpio_set_pin_direction(const uint8_t pin, const enum gpio_direction direction)
117 {
118 	_gpio_set_direction((enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin), direction);
119 }
120 
121 /**
122  * \brief Set port level
123  *
124  * Sets output level on the pins defined by the bit mask
125  *
126  * \param[in] port  Ports are grouped into groups of maximum 32 pins,
127  *                  GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
128  * \param[in] mask  Bit mask where 1 means apply port level to the corresponding
129  *                  pin
130  * \param[in] level true  = Pin levels set to "high" state
131  *                  false = Pin levels set to "low" state
132  */
gpio_set_port_level(const enum gpio_port port,const uint32_t mask,const bool level)133 static inline void gpio_set_port_level(const enum gpio_port port, const uint32_t mask, const bool level)
134 {
135 	_gpio_set_level(port, mask, level);
136 }
137 
138 /**
139  * \brief Set gpio level
140  *
141  * Sets output level on a pin
142  *
143  * \param[in] pin       The pin number for device
144  * \param[in] level true  = Pin level set to "high" state
145  *                  false = Pin level set to "low" state
146  */
gpio_set_pin_level(const uint8_t pin,const bool level)147 static inline void gpio_set_pin_level(const uint8_t pin, const bool level)
148 {
149 	_gpio_set_level((enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin), level);
150 }
151 
152 /**
153  * \brief Toggle out level on pins
154  *
155  * Toggle the pin levels on pins defined by bit mask
156  *
157  * \param[in] port  Ports are grouped into groups of maximum 32 pins,
158  *                  GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
159  * \param[in] mask  Bit mask where 1 means toggle pin level to the corresponding
160  *                  pin
161  */
gpio_toggle_port_level(const enum gpio_port port,const uint32_t mask)162 static inline void gpio_toggle_port_level(const enum gpio_port port, const uint32_t mask)
163 {
164 	_gpio_toggle_level(port, mask);
165 }
166 
167 /**
168  * \brief Toggle output level on pin
169  *
170  * Toggle the pin levels on pins defined by bit mask
171  *
172  * \param[in] pin       The pin number for device
173  */
gpio_toggle_pin_level(const uint8_t pin)174 static inline void gpio_toggle_pin_level(const uint8_t pin)
175 {
176 	_gpio_toggle_level((enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin));
177 }
178 
179 /**
180  * \brief Get input level on pins
181  *
182  * Read the input level on pins connected to a port
183  *
184  * \param[in] port  Ports are grouped into groups of maximum 32 pins,
185  *                  GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
186  */
gpio_get_port_level(const enum gpio_port port)187 static inline uint32_t gpio_get_port_level(const enum gpio_port port)
188 {
189 	return _gpio_get_level(port);
190 }
191 
192 /**
193  * \brief Get level on pin
194  *
195  * Reads the level on pins connected to a port
196  *
197  * \param[in] pin       The pin number for device
198  */
gpio_get_pin_level(const uint8_t pin)199 static inline bool gpio_get_pin_level(const uint8_t pin)
200 {
201 	return (bool)(_gpio_get_level((enum gpio_port)GPIO_PORT(pin)) & (0x01U << GPIO_PIN(pin)));
202 }
203 /**
204  * \brief Get current driver version
205  */
206 uint32_t gpio_get_version(void);
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif
213