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