blob: fbfa2d4aec54fc0c8b689a0d3f5905934a4efccf [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/**
2 * \file
3 *
4 * \brief Port
5 *
6 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 */
32#ifndef _HAL_GPIO_INCLUDED_
33#define _HAL_GPIO_INCLUDED_
34
35#include <hpl_gpio.h>
36#include <utils_assert.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/**
43 * \brief Set gpio pull mode
44 *
45 * Set pin pull mode, non existing pull modes throws an fatal assert
46 *
47 * \param[in] pin The pin number for device
48 * \param[in] pull_mode GPIO_PULL_DOWN = Pull pin low with internal resistor
49 * GPIO_PULL_UP = Pull pin high with internal resistor
50 * GPIO_PULL_OFF = Disable pin pull mode
51 */
52static inline void gpio_set_pin_pull_mode(const uint8_t pin, const enum gpio_pull_mode pull_mode)
53{
54 _gpio_set_pin_pull_mode((enum gpio_port)GPIO_PORT(pin), pin & 0x1F, pull_mode);
55}
56
57/**
58 * \brief Set pin function
59 *
60 * Select which function a pin will be used for
61 *
62 * \param[in] pin The pin number for device
63 * \param[in] function The pin function is given by a 32-bit wide bitfield
64 * found in the header files for the device
65 *
66 */
67static inline void gpio_set_pin_function(const uint32_t pin, uint32_t function)
68{
69 _gpio_set_pin_function(pin, function);
70}
71
72/**
73 * \brief Set port data direction
74 *
75 * Select if the pin data direction is input, output or disabled.
76 * If disabled state is not possible, this function throws an assert.
77 *
78 * \param[in] port Ports are grouped into groups of maximum 32 pins,
79 * GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
80 * \param[in] mask Bit mask where 1 means apply direction setting to the
81 * corresponding pin
82 * \param[in] direction GPIO_DIRECTION_IN = Data direction in
83 * GPIO_DIRECTION_OUT = Data direction out
84 * GPIO_DIRECTION_OFF = Disables the pin
85 * (low power state)
86 */
87static inline void gpio_set_port_direction(const enum gpio_port port, const uint32_t mask,
88 const enum gpio_direction direction)
89{
90 _gpio_set_direction(port, mask, direction);
91}
92
93/**
94 * \brief Set gpio data direction
95 *
96 * Select if the pin data direction is input, output or disabled.
97 * If disabled state is not possible, this function throws an assert.
98 *
99 * \param[in] pin The pin number for device
100 * \param[in] direction GPIO_DIRECTION_IN = Data direction in
101 * GPIO_DIRECTION_OUT = Data direction out
102 * GPIO_DIRECTION_OFF = Disables the pin
103 * (low power state)
104 */
105static inline void gpio_set_pin_direction(const uint8_t pin, const enum gpio_direction direction)
106{
107 _gpio_set_direction((enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin), direction);
108}
109
110/**
111 * \brief Set port level
112 *
113 * Sets output level on the pins defined by the bit mask
114 *
115 * \param[in] port Ports are grouped into groups of maximum 32 pins,
116 * GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
117 * \param[in] mask Bit mask where 1 means apply port level to the corresponding
118 * pin
119 * \param[in] level true = Pin levels set to "high" state
120 * false = Pin levels set to "low" state
121 */
122static inline void gpio_set_port_level(const enum gpio_port port, const uint32_t mask, const bool level)
123{
124 _gpio_set_level(port, mask, level);
125}
126
127/**
128 * \brief Set gpio level
129 *
130 * Sets output level on a pin
131 *
132 * \param[in] pin The pin number for device
133 * \param[in] level true = Pin level set to "high" state
134 * false = Pin level set to "low" state
135 */
136static inline void gpio_set_pin_level(const uint8_t pin, const bool level)
137{
138 _gpio_set_level((enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin), level);
139}
140
141/**
142 * \brief Toggle out level on pins
143 *
144 * Toggle the pin levels on pins defined by bit mask
145 *
146 * \param[in] port Ports are grouped into groups of maximum 32 pins,
147 * GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
148 * \param[in] mask Bit mask where 1 means toggle pin level to the corresponding
149 * pin
150 */
151static inline void gpio_toggle_port_level(const enum gpio_port port, const uint32_t mask)
152{
153 _gpio_toggle_level(port, mask);
154}
155
156/**
157 * \brief Toggle output level on pin
158 *
159 * Toggle the pin levels on pins defined by bit mask
160 *
161 * \param[in] pin The pin number for device
162 */
163static inline void gpio_toggle_pin_level(const uint8_t pin)
164{
165 _gpio_toggle_level((enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin));
166}
167
168/**
169 * \brief Get input level on pins
170 *
171 * Read the input level on pins connected to a port
172 *
173 * \param[in] port Ports are grouped into groups of maximum 32 pins,
174 * GPIO_PORTA = group 0, GPIO_PORTB = group 1, etc
175 */
176static inline uint32_t gpio_get_port_level(const enum gpio_port port)
177{
178 return _gpio_get_level(port);
179}
180
181/**
182 * \brief Get level on pin
183 *
184 * Reads the level on pins connected to a port
185 *
186 * \param[in] pin The pin number for device
187 */
188static inline bool gpio_get_pin_level(const uint8_t pin)
189{
190 return (bool)(_gpio_get_level((enum gpio_port)GPIO_PORT(pin)) & (0x01U << GPIO_PIN(pin)));
191}
192/**
193 * \brief Get current driver version
194 */
195uint32_t gpio_get_version(void);
196
197#ifdef __cplusplus
198}
199#endif
200
201#endif