gpio_ll_arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
3  * 2015-2016 Freie Universität Berlin
4  * 2019 Inria
5  *
6  * This file is subject to the terms and conditions of the GNU Lesser
7  * General Public License v2.1. See the file LICENSE in the top level
8  * directory for more details.
9  */
10 
29 #ifndef GPIO_LL_ARCH_H
30 #define GPIO_LL_ARCH_H
31 
32 #include <assert.h>
33 
34 #include "cpu.h"
35 #include "irq.h"
36 #include "periph_cpu.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #ifndef DOXYGEN /* hide implementation specific details from Doxygen */
43 
44 #define PORT_BIT (1 << 5)
45 #define PIN_MASK (0x1f)
46 #define NRF5X_IO_AREA_START (0x40000000UL)
47 
48 /* Compatibility wrapper defines for nRF9160 */
49 #ifdef NRF_P0_S
50 #define NRF_P0 NRF_P0_S
51 #endif
52 
53 #if defined(CPU_FAM_NRF51)
54 # define GPIO_PORT_0 ((gpio_port_t)NRF_GPIO)
55 #else
56 # if defined(NRF_P1)
57 # define GPIO_PORT_1 ((gpio_port_t)NRF_P1)
58 # endif
59 # define GPIO_PORT_0 ((gpio_port_t)NRF_P0)
60 #endif
61 
62 static inline gpio_port_t gpio_port(uword_t num)
63 {
64  (void)num;
65 #ifdef GPIO_PORT_1
66  if (num == 1) {
67  return GPIO_PORT_1;
68  }
69 #endif
70 
71  return GPIO_PORT_0;
72 }
73 
74 static inline uword_t gpio_port_num(gpio_port_t port)
75 {
76  (void)port;
77 #ifdef GPIO_PORT_1
78  if (port == GPIO_PORT_1) {
79  return 1;
80  }
81 #endif
82  return 0;
83 }
84 
85 static inline uword_t gpio_ll_read(gpio_port_t port)
86 {
87  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
88  return p->IN;
89 }
90 
91 static inline uword_t gpio_ll_read_output(gpio_port_t port)
92 {
93  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
94  return p->OUT;
95 }
96 
97 static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
98 {
99  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
100  p->OUTSET = mask;
101 }
102 
103 static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
104 {
105  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
106  p->OUTCLR = mask;
107 }
108 
109 static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
110 {
111  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
112  unsigned state = irq_disable();
113  p->OUT ^= mask;
114  irq_restore(state);
115 }
116 
117 static inline void gpio_ll_write(gpio_port_t port, uword_t value)
118 {
119  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
120  p->OUT = value;
121 }
122 
123 static inline gpio_port_t gpio_get_port(gpio_t pin)
124 {
125 #if defined(NRF_P1)
126  return gpio_port(pin >> 5);
127 #else
128  (void)pin;
129  return GPIO_PORT_0;
130 #endif
131 }
132 
133 static inline uint8_t gpio_get_pin_num(gpio_t pin)
134 {
135 #if defined(NRF_P1)
136  return pin & PIN_MASK;
137 #else
138  return (uint8_t)pin;
139 #endif
140 }
141 
142 static inline gpio_port_t gpio_port_pack_addr(void *addr)
143 {
144  return (gpio_port_t)addr;
145 }
146 
147 static inline void * gpio_port_unpack_addr(gpio_port_t port)
148 {
149  /* NRF5X_IO_AREA_START is the start of the memory mapped I/O area. Both data
150  * and flash are mapped before it. So if it is an I/O address, it
151  * cannot be a packed data address and (hopefully) is a GPIO port */
152  if (port >= NRF5X_IO_AREA_START) {
153  return NULL;
154  }
155 
156  return (void *)port;
157 }
158 
159 static inline bool is_gpio_port_num_valid(uint_fast8_t num)
160 {
161  switch (num) {
162  default:
163  return false;
164  case 0:
165 #if defined(NRF_P1)
166  case 1:
167 #endif
168  return true;
169  }
170 }
171 
172 #endif /* DOXYGEN */
173 #ifdef __cplusplus
174 }
175 #endif
176 
177 #endif /* GPIO_LL_ARCH_H */
POSIX.1-2008 compliant version of the assert macro.
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
#define PIN_MASK(n)
Generate a bit mask in which only the specified bit is high.
Definition: cc2538_gpio.h:58
#define GPIO_PORT_0
Get the gpio_port_t value of the port labeled 0.
Definition: gpio_ll.h:122
static uint8_t gpio_get_pin_num(gpio_t pin)
Extract the pin number from a gpio_t
static void * gpio_port_unpack_addr(gpio_port_t port)
Extract a data pointer that was packed by gpio_port_pack_addr.
static void gpio_ll_set(gpio_port_t port, uword_t mask)
Perform an reg |= mask operation on the I/O register of the port.
gpio_port_t gpio_port(uword_t num)
Get the gpio_port_t value of the port number num.
static gpio_port_t gpio_port_pack_addr(void *addr)
Pack a pointer into a gpio_port_t.
static uword_t gpio_ll_read(gpio_port_t port)
Get the current input value of all GPIO pins of the given port as bitmask.
static gpio_port_t gpio_get_port(gpio_t pin)
Extract the gpio_port_t from a gpio_t
uword_t gpio_port_num(gpio_port_t port)
Get the number of the GPIO port port refers to.
static bool is_gpio_port_num_valid(uint_fast8_t num)
Check if the given number is a valid argument for gpio_port.
static uword_t gpio_ll_read_output(gpio_port_t port)
Get the current output value of all GPIO pins of the given port as bitmask.
static void gpio_ll_clear(gpio_port_t port, uword_t mask)
Perform an reg &= ~mask operation on the I/O register of the port.
static void gpio_ll_toggle(gpio_port_t port, uword_t mask)
Perform an reg ^= mask operation on the I/O register of the port.
static void gpio_ll_write(gpio_port_t port, uword_t state)
Perform a masked write operation on the I/O register of the port.
uintptr_t gpio_port_t
GPIO port type.
Definition: gpio_ll.h:87
uint< NUM > _t uword_t
Word sized unsigned integer.
Definition: architecture.h:70
IRQ driver interface.