gpio_ll_arch.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Jan Wagner <mail@jwagner.eu>
3  * SPDX-FileCopyrightText: 2015-2016 Freie Universität Berlin
4  * SPDX-FileCopyrightText: 2019 Inria
5  * SPDX-License-Identifier: LGPL-2.1-only
6  */
7 
8 #pragma once
9 
28 #include <assert.h>
29 
30 #include "cpu.h"
31 #include "irq.h"
32 #include "periph_cpu.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifndef DOXYGEN /* hide implementation specific details from Doxygen */
39 
40 #define PORT_BIT (1 << 5)
41 #define PIN_MASK (0x1f)
42 #define NRF5X_IO_AREA_START (0x40000000UL)
43 
44 /* Compatibility wrapper defines for nRF9160 */
45 #ifdef NRF_P0_S
46 #define NRF_P0 NRF_P0_S
47 #endif
48 
49 #if defined(CPU_FAM_NRF51)
50 # define GPIO_PORT_0 ((gpio_port_t)NRF_GPIO)
51 #else
52 # if defined(NRF_P1)
53 # define GPIO_PORT_1 ((gpio_port_t)NRF_P1)
54 # endif
55 # define GPIO_PORT_0 ((gpio_port_t)NRF_P0)
56 #endif
57 
58 static inline gpio_port_t gpio_port(uword_t num)
59 {
60  (void)num;
61 #ifdef GPIO_PORT_1
62  if (num == 1) {
63  return GPIO_PORT_1;
64  }
65 #endif
66 
67  return GPIO_PORT_0;
68 }
69 
70 static inline uword_t gpio_port_num(gpio_port_t port)
71 {
72  (void)port;
73 #ifdef GPIO_PORT_1
74  if (port == GPIO_PORT_1) {
75  return 1;
76  }
77 #endif
78  return 0;
79 }
80 
81 static inline uword_t gpio_ll_read(gpio_port_t port)
82 {
83  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
84  return p->IN;
85 }
86 
87 static inline uword_t gpio_ll_read_output(gpio_port_t port)
88 {
89  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
90  return p->OUT;
91 }
92 
93 static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
94 {
95  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
96  p->OUTSET = mask;
97 }
98 
99 static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
100 {
101  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
102  p->OUTCLR = mask;
103 }
104 
105 static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
106 {
107  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
108  unsigned state = irq_disable();
109  p->OUT ^= mask;
110  irq_restore(state);
111 }
112 
113 static inline void gpio_ll_write(gpio_port_t port, uword_t value)
114 {
115  NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
116  p->OUT = value;
117 }
118 
119 static inline gpio_port_t gpio_get_port(gpio_t pin)
120 {
121 #if defined(NRF_P1)
122  return gpio_port(pin >> 5);
123 #else
124  (void)pin;
125  return GPIO_PORT_0;
126 #endif
127 }
128 
129 static inline uint8_t gpio_get_pin_num(gpio_t pin)
130 {
131 #if defined(NRF_P1)
132  return pin & PIN_MASK;
133 #else
134  return (uint8_t)pin;
135 #endif
136 }
137 
138 static inline gpio_port_t gpio_port_pack_addr(void *addr)
139 {
140  return (gpio_port_t)addr;
141 }
142 
143 static inline void * gpio_port_unpack_addr(gpio_port_t port)
144 {
145  /* NRF5X_IO_AREA_START is the start of the memory mapped I/O area. Both data
146  * and flash are mapped before it. So if it is an I/O address, it
147  * cannot be a packed data address and (hopefully) is a GPIO port */
148  if (port >= NRF5X_IO_AREA_START) {
149  return NULL;
150  }
151 
152  return (void *)port;
153 }
154 
155 static inline bool is_gpio_port_num_valid(uint_fast8_t num)
156 {
157  switch (num) {
158  default:
159  return false;
160  case 0:
161 #if defined(NRF_P1)
162  case 1:
163 #endif
164  return true;
165  }
166 }
167 
168 #endif /* DOXYGEN */
169 #ifdef __cplusplus
170 }
171 #endif
172 
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:54
#define GPIO_PORT_0
Get the gpio_port_t value of the port labeled 0.
Definition: gpio_ll.h:129
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:95
uint< NUM > _t uword_t
Word sized unsigned integer.
Definition: architecture.h:69
IRQ driver interface.