gpio_ll_arch.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2024 Marian Buschsieweke
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
19 #include "cpu.h"
20 #include "periph_cpu.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #ifndef DOXYGEN /* hide implementation specific details from Doxygen */
27 
28 /* the memory layout of all GPIO peripherals is compatible, but the location
29  * in the address space is pretty much random */
30 
31 #define GPIO_PORT_1 ((gpio_port_t)&PORT_1.base)
32 #define GPIO_PORT_2 ((gpio_port_t)&PORT_2.base)
33 #define GPIO_PORT_3 ((gpio_port_t)&PORT_3.base)
34 #define GPIO_PORT_4 ((gpio_port_t)&PORT_4.base)
35 #define GPIO_PORT_5 ((gpio_port_t)&PORT_5.base)
36 #define GPIO_PORT_6 ((gpio_port_t)&PORT_6.base)
37 /* Port 7 and 8 have different memory layout and are only available on F2xx/G2xx
38  * MCUs */
39 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
40 # define GPIO_PORT_7 ((gpio_port_t)&PORT_7)
41 # define GPIO_PORT_8 ((gpio_port_t)&PORT_8)
42 #endif
43 
44 /* IMPORTANT IMPLEMENTATION INFO
45  * =============================
46  *
47  * - MSP430 F2xx/G2xx do have PORT 7 and PORT 8, but those have an incompatible
48  * memory layout compared to the other ports. Hence, they need extra handling.
49  * However, constant folding should get ride of the branch and overhead if the
50  * GPIO port is a compile time constant
51  * - MSP430 has bit manipulation instructions that work on memory. E.g.
52  * `BIC.B %[mask], @%[ptr]` will implement `*ptr &= ~(mask)` in a single
53  * instruction. Same for setting or XORing bits. Hence, the code below
54  * may often look like it is missing `irq_disable()` ... `irq_restore()`, but
55  * in fact will be atomic due to the MSP430 instruction set.
56  */
57 
59 
60 static inline uword_t gpio_ll_read(gpio_port_t port)
61 {
62 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
63  if (port >= (uintptr_t)(&PORT_7)) {
64  const msp430_port_p7_p8_t *p = (void *)port;
65  return p->IN;
66  }
67 #endif
68  const msp430_port_t *p = (void *)port;
69  return p->IN;
70 }
71 
72 static inline uword_t gpio_ll_read_output(gpio_port_t port)
73 {
74 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
75  if (port >= (uintptr_t)(&PORT_7)) {
76  const msp430_port_p7_p8_t *p = (void *)port;
77  return p->OD;
78  }
79 #endif
80  const msp430_port_t *p = (void *)port;
81  return p->OD;
82 }
83 
84 static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
85 {
86 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
87  if (port >= (uintptr_t)(&PORT_7)) {
88  msp430_port_p7_p8_t *p = (void *)port;
89  p->OD |= mask;
90  return;
91  }
92 #endif
93  msp430_port_t *p = (void *)port;
94  p->OD |= mask;
95 }
96 
97 static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
98 {
99 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
100  if (port >= (uintptr_t)(&PORT_7)) {
101  msp430_port_p7_p8_t *p = (void *)port;
102  p->OD &= ~(mask);
103  return;
104  }
105 #endif
106  msp430_port_t *p = (void *)port;
107  p->OD &= ~(mask);
108 }
109 
110 static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
111 {
112 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
113  if (port >= (uintptr_t)(&PORT_7)) {
114  msp430_port_p7_p8_t *p = (void *)port;
115  p->OD ^= mask;
116  return;
117  }
118 #endif
119  msp430_port_t *p = (void *)port;
120  p->OD ^= mask;
121 }
122 
123 static inline void gpio_ll_write(gpio_port_t port, uword_t value)
124 {
125 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
126  if (port >= (uintptr_t)(&PORT_7)) {
127  msp430_port_p7_p8_t *p = (void *)port;
128  p->OD = value;
129  return;
130  }
131 #endif
132  msp430_port_t *p = (void *)port;
133  p->OD = value;
134 }
135 
136 static inline gpio_port_t gpio_get_port(gpio_t pin)
137 {
138  return gpio_port(gpio_get_pin_num(pin));
139 }
140 
141 static inline uint8_t gpio_get_pin_num(gpio_t pin)
142 {
143  return pin >> 8;
144 }
145 
146 static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs)
147 {
148 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
149  if (port >= (uintptr_t)(&PORT_7)) {
150  msp430_port_p7_p8_t *p = (void *)port;
151  p->DIR |= outputs;
152  return;
153  }
154 #endif
155  msp430_port_t *p = (void *)port;
156  p->DIR |= outputs;
157 }
158 
159 static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs)
160 {
161 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
162  if (port >= (uintptr_t)(&PORT_7)) {
163  msp430_port_p7_p8_t *p = (void *)port;
164  p->DIR &= ~(inputs);
165  return;
166  }
167 #endif
168  msp430_port_t *p = (void *)port;
169  p->DIR &= ~(inputs);
170 }
171 
172 static inline gpio_port_t gpio_port_pack_addr(void *addr)
173 {
174  return (gpio_port_t)addr;
175 }
176 
177 static inline void * gpio_port_unpack_addr(gpio_port_t port)
178 {
179  if (port < RAMSTART) {
180  return NULL;
181  }
182 
183  return (void *)port;
184 }
185 
186 static inline bool is_gpio_port_num_valid(uint_fast8_t num)
187 {
188 #if defined(CPU_FAM_MSP430_F2XX_G2XX)
189  return (num > 0) && (num <= 8);
190 #else
191  return (num > 0) && (num <= 6);
192 #endif
193 }
194 
196 
197 #endif /* DOXYGEN */
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
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 void gpio_ll_switch_dir_output(gpio_port_t port, uword_t pins)
Turn GPIO pins specified by pins (obtained from gpio_ll_prepare_switch_dir) to outputs.
static void gpio_ll_switch_dir_input(gpio_port_t port, uword_t pins)
Turn GPIO pins specified by pins (obtained from gpio_ll_prepare_switch_dir) to inputs.
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
msp430_port_p7_p8_t PORT_7
Register map of GPIO PORT 7.
#define RAMSTART
Lowest address of the RAM, peripherals are below.
Native CPU header.
GPIO Port 7/8 (different register layout than Ports 1-6)
Definition: msp430_regs.h:71
REG8 IN
input data
Definition: msp430_regs.h:72
REG8 OD
output data
Definition: msp430_regs.h:74
REG8 DIR
pin direction
Definition: msp430_regs.h:76
Common MSP GPIO Port Registers.
REG8 OD
output data
REG8 IN
input data
REG8 DIR
pin direction