EVE 1.0
gpio.h
Go to the documentation of this file.
1 #ifndef DRIVER_GPIO_H
2 #define DRIVER_GPIO_H
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2015-2016, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief Driver for port initialisazion and IO functions for nRF52 uC.
36  *
37  * @author PE, Jetro AS
38  * @author DT, Jetro AS
39  */ /******************************************************************/
40 #include <stdbool.h>
41 #include <hal/nrf_gpio.h>
42 #include <hal/nrf_gpiote.h>
43 #include <dev/eve-module.h>
44 
45 /**
46  * This GPIO driver has functions for:
47  * - initialization of ports with default values.
48  * - debouncing of input pins
49  */
50 
51 /**
52  * \defgroup nrf_gpio nRF52 internal GPIO driver
53  * \ingroup nrf52
54  * \{
55  */
56 
57 /***********************************************************************
58  * General mode definitions
59  **********************************************************************/
60 #define GPIO_LOW 0UL
61 #define GPIO_HIGH 1UL
62 #define GPIO_PIN_ID_WIDTH (6)
63 #define GPIO_PIN_NC (32UL) /**< Not connected */
64 
65 /**********************************************************************/
66 /**
67  * @brief Name: GpioUnpackPin\n
68  * Converts packed pin number to the format, accepted by hardware blocks
69  *
70  * @param Pin Pin number or GPIO_PIN_NC if not connected
71  * @return Pin number or 0x8XXXXXXX if not connected
72  ***********************************************************************/
73 static inline uint32_t GpioUnpackPin(uint32_t Pin)
74 {
75  return (Pin | (Pin << 26)) /*& 0x8000001F*/;
76 }
77 
78 struct port_init_t
79 {
80  uint32_t Cfg;
81 };
82 
83 extern const struct port_init_t PortInitTable[32];
84 extern const uint32_t PortInitValue;
85 
86 #if defined(DRIVER_PORT_INIT_TABLE)
87  #include <dev/port-init-generator.h>
88 #endif
89 
90 #define GPIO_PIN_CNF(Direction, Input, Pull, Drive) \
91  (((Direction) << GPIO_PIN_CNF_DIR_Pos) \
92  | ((Input) << GPIO_PIN_CNF_INPUT_Pos) \
93  | ((Pull) << GPIO_PIN_CNF_PULL_Pos) \
94  | ((Drive) << GPIO_PIN_CNF_DRIVE_Pos))
95 
96 /** Pin mode. */
98 {
99  /** Input disabled. */
101  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Input, GPIO_PIN_CNF_INPUT_Disconnect,
102  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_S0S1),
103 
104  /** Input disabled and pulled down */
106  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Input, GPIO_PIN_CNF_INPUT_Disconnect,
107  GPIO_PIN_CNF_PULL_Pulldown, GPIO_PIN_CNF_DRIVE_S0S1),
108 
109  /** Input disabled and pulled up */
111  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Input, GPIO_PIN_CNF_INPUT_Disconnect,
112  GPIO_PIN_CNF_PULL_Pullup, GPIO_PIN_CNF_DRIVE_S0S1),
113 
114  /** Input enabled. */
116  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Input, GPIO_PIN_CNF_INPUT_Connect,
117  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_S0S1),
118 
119  /** Input enabled and pulled down. */
121  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Input, GPIO_PIN_CNF_INPUT_Connect,
122  GPIO_PIN_CNF_PULL_Pulldown, GPIO_PIN_CNF_DRIVE_S0S1),
123 
124  /** Input enabled and pulled up. */
126  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Input, GPIO_PIN_CNF_INPUT_Connect,
127  GPIO_PIN_CNF_PULL_Pullup, GPIO_PIN_CNF_DRIVE_S0S1),
128 
129  /** Push-pull output */
131  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
132  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_S0S1),
133 
134  /** Push-pull output with drive-strength set by DRIVEMODE */
136  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
137  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_H0H1),
138 
139  /** Wired-or output */
141  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
142  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_D0S1),
143 
144  /** Wired-or output high-drive */
146  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
147  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_D0H1),
148 
149  /** Wired-or output with pull-down */
151  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
152  GPIO_PIN_CNF_PULL_Pulldown, GPIO_PIN_CNF_DRIVE_D0S1),
153 
154  /** Open-drain output */
156  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
157  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_S0D1),
158 
159  /** Open-drain output with pullup */
161  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
162  GPIO_PIN_CNF_PULL_Pullup, GPIO_PIN_CNF_DRIVE_S0D1),
163 
164  /** Open-drain output with drive-strength set by DRIVEMODE */
166  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
167  GPIO_PIN_CNF_PULL_Disabled, GPIO_PIN_CNF_DRIVE_H0D1),
168 
169  /** Open-drain output with pullup and drive-strength set by DRIVEMODE */
171  = GPIO_PIN_CNF(GPIO_PIN_CNF_DIR_Output, GPIO_PIN_CNF_INPUT_Connect,
172  GPIO_PIN_CNF_PULL_Pullup, GPIO_PIN_CNF_DRIVE_H0D1),
173 };
174 #undef GPIO_PIN_CNF
175 
176  /**
177  * @name Functions called from application programs
178  * @{
179  */
180 
181 /**********************************************************************/
182 /**
183  * @brief Name: GpioInit\n
184  * Initializes ports to default values based on port-init.h.
185  **********************************************************************/
186 extern void GpioInit(void);
187 
188 /**********************************************************************/
189 /**
190  * @brief Name: GpioPinConfig\n
191  * Configures a GPIO pin to the desired mode, sets value for the pin
192  *
193  * @param Pin The pin number to read.
194  * @param Mode Pin mode.
195  ***********************************************************************/
196 static inline void GpioPinConfig(unsigned Pin, enum gpio_mode_t Mode)
197 {
198  NRF_GPIO->PIN_CNF[Pin] = Mode;
199 }
200 
201 /**********************************************************************/
202 /**
203  * @brief Name: GpioPinGet\n
204  * Reads the pad value for a single pin in a GPIO port.
205  *
206  * @param Pin The pin number to read.
207  * @return The pin value, 0 or 1.
208  ***********************************************************************/
209 static inline bool GpioPinGet(unsigned Pin)
210 {
211  return nrf_gpio_pin_read(Pin);
212 }
213 
214 /**********************************************************************/
215 /**
216  * @brief Name: GpioPinClear\n
217  * Sets the pad value for a single pin in a GPIO port to 0
218  *
219  * @param Pin The pin number to modify.
220  ***********************************************************************/
221 static inline void GpioPinClear(unsigned Pin)
222 {
223  nrf_gpio_pin_clear(Pin);
224 }
225 
226 /**********************************************************************/
227 /**
228  * @brief Name: GpioPinSet\n
229  * Sets the pad value for a single pin in a GPIO port to 1
230  *
231  * @param Pin The pin number to modify.
232  ***********************************************************************/
233 static inline void GpioPinSet(unsigned Pin)
234 {
235  nrf_gpio_pin_set(Pin);
236 }
237 
238 /**********************************************************************/
239 /**
240  * @brief Name: GpioPinToggle\n
241  * Toggles a single pin in a GPIO port
242  *
243  * @param Pin The pin number to modify.
244  ***********************************************************************/
245 static inline void GpioPinToggle(unsigned Pin)
246 {
247  nrf_gpio_pin_toggle(Pin);
248 }
249 
250 /**********************************************************************/
251 /**
252  * @brief Name: GpioPinSetValue\n
253  * Sets the pad value for a single pin in a GPIO port to the value provided.
254  *
255  * @param Pin The pin number to modify.
256  * @param Value Value to be set.
257  ***********************************************************************/
258 static inline void GpioPinSetValue(unsigned Pin, bool Value)
259 {
260  nrf_gpio_pin_write(Pin, Value);
261 }
262 
263 /**********************************************************************/
264 /**
265  * @brief Name: GpioPortGet\n
266  * Reads the pad values for all the pins in a GPIO port.
267  *
268  * @return 32-bit bitmap, where LSB stands for the pin 0.
269  ***********************************************************************/
270 static inline uint32_t GpioPortGet(void)
271 {
272  return nrf_gpio_pins_read();
273 }
274 
275 /**********************************************************************/
276 /**
277  * @brief Name: GpioPortClear\n
278  * Sets the pad values for pins specified by a mask to 0
279  *
280  * @param Mask The bitmaps of the pins to modify.
281  ***********************************************************************/
282 static inline void GpioPortClear(uint32_t Mask)
283 {
284  nrf_gpio_pins_clear(Mask);
285 }
286 
287 /**********************************************************************/
288 /**
289  * @brief Name: GpioPortSet\n
290  * Sets the pad values for pins specified by a mask to 1
291  *
292  * @param Mask The bitmaps of the pins to modify.
293  ***********************************************************************/
294 static inline void GpioPortSet(uint32_t Mask)
295 {
296  nrf_gpio_pins_set(Mask);
297 }
298 
299 /**********************************************************************/
300 /**
301  * @brief Name: GpioPortToggle\n
302  * Toggles pins specified by a mask in a GPIO port
303  *
304  * @param Mask The bitmaps of the pins to modify.
305  ***********************************************************************/
306 static inline void GpioPortToggle(uint32_t Mask)
307 {
308  nrf_gpio_pins_toggle(Mask);
309 }
310 
311 /**********************************************************************/
312 /**
313  * @brief Name: GpioPortSetValue\n
314  * Sets the pad value for a single pin in a GPIO port to the value provided.
315  *
316  * @param Value The bitmap, containing values for the pins.
317  * @param Mask The bitmaps of the pins to modify.
318  ***********************************************************************/
319 static inline void GpioPortSetValue(uint32_t Value, uint32_t Mask)
320 {
321  NRF_GPIO->OUTSET = (Mask & Value);
322  NRF_GPIO->OUTCLR = (Mask & ~Value);
323 }
324 
325 /**********************************************************************/
326 /**
327  * @brief Name: GpioGetDebouncedInput
328  * Reads and debounces input pins.
329  *
330  * @param BitMask BitMask for actual bits to read
331  * @param BitInvert Bits to be inverted (for example if active 'L' input
332  * should return 'H', then resp. bit in BitInvert must be set)
333  * @param Count Required number of equal readings
334  * @param ShiftRightCount Spesifies right adjustment (0-31)
335  * @return Returns port input status after Count equal readings.
336  ***********************************************************************/
337 extern uint32_t GpioGetDebouncedInput(uint32_t BitMask, uint32_t BitInvert, uint8_t Count, uint8_t ShiftRightCount);
338 
339 /** @} */
340 
341 /** \} nrf_gpio */
342 
343 
344 #endif //DRIVER_GPIO_H
static void GpioPinSet(unsigned Pin)
Name: GpioPinSet Sets the pad value for a single pin in a GPIO port to 1.
Definition: gpio.h:233
Port init table generator.
EVE module stub.
static void GpioPinToggle(unsigned Pin)
Name: GpioPinToggle Toggles a single pin in a GPIO port.
Definition: gpio.h:245
static void GpioPinConfig(unsigned Pin, enum gpio_mode_t Mode)
Name: GpioPinConfig Configures a GPIO pin to the desired mode, sets value for the pin...
Definition: gpio.h:196
static void GpioPortClear(uint32_t Mask)
Name: GpioPortClear Sets the pad values for pins specified by a mask to 0.
Definition: gpio.h:282
static uint32_t GpioPortGet(void)
Name: GpioPortGet Reads the pad values for all the pins in a GPIO port.
Definition: gpio.h:270
gpio_mode_t
Definition: gpio.h:97
static uint32_t GpioUnpackPin(uint32_t Pin)
Name: GpioUnpackPin Converts packed pin number to the format, accepted by hardware blocks...
Definition: gpio.h:73
static bool GpioPinGet(unsigned Pin)
Name: GpioPinGet Reads the pad value for a single pin in a GPIO port.
Definition: gpio.h:209
static void GpioPortSet(uint32_t Mask)
Name: GpioPortSet Sets the pad values for pins specified by a mask to 1.
Definition: gpio.h:294
void GpioInit(void)
Name: GpioInit Initializes ports to default values based on port-init.h.
uint32_t GpioGetDebouncedInput(uint32_t BitMask, uint32_t BitInvert, uint8_t Count, uint8_t ShiftRightCount)
Name: GpioGetDebouncedInput Reads and debounces input pins.
static void GpioPortToggle(uint32_t Mask)
Name: GpioPortToggle Toggles pins specified by a mask in a GPIO port.
Definition: gpio.h:306
static void GpioPortSetValue(uint32_t Value, uint32_t Mask)
Name: GpioPortSetValue Sets the pad value for a single pin in a GPIO port to the value provided...
Definition: gpio.h:319
static void GpioPinClear(unsigned Pin)
Name: GpioPinClear Sets the pad value for a single pin in a GPIO port to 0.
Definition: gpio.h:221
static void GpioPinSetValue(unsigned Pin, bool Value)
Name: GpioPinSetValue Sets the pad value for a single pin in a GPIO port to the value provided...
Definition: gpio.h:258