Loading...
Searching...
No Matches
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
39extern "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(num) ((gpio_port_t)NRF_GPIO)
55#define GPIO_PORT_NUM(port) 0
56#elif defined(NRF_P1)
57#define GPIO_PORT(num) ((num) ? (gpio_port_t)NRF_P1 : (gpio_port_t)NRF_P0)
58#define GPIO_PORT_NUM(port) ((port == (gpio_port_t)NRF_P1) ? 1 : 0)
59#else
60#define GPIO_PORT(num) ((gpio_port_t)NRF_P0)
61#define GPIO_PORT_NUM(port) 0
62#endif
63
64static inline uword_t gpio_ll_read(gpio_port_t port)
65{
66 NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
67 return p->IN;
68}
69
70static inline uword_t gpio_ll_read_output(gpio_port_t port)
71{
72 NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
73 return p->OUT;
74}
75
76static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
77{
78 NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
79 p->OUTSET = mask;
80}
81
82static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
83{
84 NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
85 p->OUTCLR = mask;
86}
87
88static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
89{
90 NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
91 unsigned state = irq_disable();
92 p->OUT ^= mask;
93 irq_restore(state);
94}
95
96static inline void gpio_ll_write(gpio_port_t port, uword_t value)
97{
98 NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
99 p->OUT = value;
100}
101
102static inline gpio_port_t gpio_get_port(gpio_t pin)
103{
104#if defined(NRF_P1)
105 return GPIO_PORT(pin >> 5);
106#else
107 (void)pin;
108 return GPIO_PORT(0);
109#endif
110}
111
112static inline uint8_t gpio_get_pin_num(gpio_t pin)
113{
114#if defined(NRF_P1)
115 return pin & PIN_MASK;
116#else
117 return (uint8_t)pin;
118#endif
119}
120
121static inline gpio_port_t gpio_port_pack_addr(void *addr)
122{
123 return (gpio_port_t)addr;
124}
125
126static inline void * gpio_port_unpack_addr(gpio_port_t port)
127{
128 /* NRF5X_IO_AREA_START is the start of the memory mapped I/O area. Both data
129 * and flash are mapped before it. So if it is an I/O address, it
130 * cannot be a packed data address and (hopefully) is a GPIO port */
131 if (port >= NRF5X_IO_AREA_START) {
132 return NULL;
133 }
134
135 return (void *)port;
136}
137
138static inline bool is_gpio_port_num_valid(uint_fast8_t num)
139{
140 switch (num) {
141 default:
142 return false;
143 case 0:
144#if defined(NRF_P1)
145 case 1:
146#endif
147 return true;
148 }
149}
150
151#endif /* DOXYGEN */
152#ifdef __cplusplus
153}
154#endif
155
156#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
static uint8_t gpio_get_pin_num(gpio_t pin)
Extract the pin number from a gpio_t
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.
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
static void * gpio_port_unpack_addr(gpio_port_t port)
Extract a data pointer that was packed by gpio_port_pack_addr.
#define GPIO_PORT(num)
Get the gpio_port_t value of the port identified by num.
Definition gpio_ll.h:106
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.
IRQ driver interface.