gossamer 0.0.1
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
hal_gpio.h
1/*
2 * Copyright (c) 2014-2016, Alex Taradov <alex@taradov.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdbool.h>
30#include <stdint.h>
31#include "sam.h"
32
33#pragma once
34
35/*- Definitions -------------------------------------------------------------*/
36#define HAL_GPIO_PORTA 0
37#define HAL_GPIO_PORTB 1
38#define HAL_GPIO_PORTC 2
39#define HAL_GPIO_PORTD 3
40
41#define HAL_GPIO_PMUX_A 0
42#define HAL_GPIO_PMUX_B 1
43#define HAL_GPIO_PMUX_C 2
44#define HAL_GPIO_PMUX_D 3
45#define HAL_GPIO_PMUX_E 4
46#define HAL_GPIO_PMUX_F 5
47#define HAL_GPIO_PMUX_G 6
48#define HAL_GPIO_PMUX_H 7
49#define HAL_GPIO_PMUX_I 8
50#define HAL_GPIO_PMUX_J 9
51#define HAL_GPIO_PMUX_K 10
52#define HAL_GPIO_PMUX_L 11
53#define HAL_GPIO_PMUX_M 12
54#define HAL_GPIO_PMUX_N 13
55
56#if __EMSCRIPTEN__
57
58extern bool pin_levels[4][32];
59
60#define HAL_GPIO_PIN(name, port, pin) \
61 static inline void HAL_GPIO_##name##_set(void) \
62 { \
63 pin_levels[HAL_GPIO_PORT##port][pin] = true; \
64 (void)HAL_GPIO_##name##_set; \
65 } \
66 \
67 static inline void HAL_GPIO_##name##_clr(void) \
68 { \
69 pin_levels[HAL_GPIO_PORT##port][pin] = false; \
70 (void)HAL_GPIO_##name##_clr; \
71 } \
72 \
73 static inline void HAL_GPIO_##name##_toggle(void) \
74 { \
75 pin_levels[HAL_GPIO_PORT##port][pin] = !pin_levels[HAL_GPIO_PORT##port][pin]; \
76 (void)HAL_GPIO_##name##_toggle; \
77 } \
78 \
79 static inline void HAL_GPIO_##name##_write(int value) \
80 { \
81 pin_levels[HAL_GPIO_PORT##port][pin] = !!value; \
82 (void)HAL_GPIO_##name##_write; \
83 } \
84 \
85 static inline void HAL_GPIO_##name##_drvstr(int value) \
86 { \
87 (void)HAL_GPIO_##name##_write; \
88 } \
89 \
90 static inline void HAL_GPIO_##name##_in(void) \
91 { \
92 (void)HAL_GPIO_##name##_in; \
93 } \
94 \
95 static inline void HAL_GPIO_##name##_out(void) \
96 { \
97 (void)HAL_GPIO_##name##_out; \
98 } \
99 \
100 static inline void HAL_GPIO_##name##_off(void) \
101 { \
102 (void)HAL_GPIO_##name##_off; \
103 } \
104 \
105 static inline void HAL_GPIO_##name##_pullup(void) \
106 { \
107 (void)HAL_GPIO_##name##_pullup; \
108 } \
109 \
110 static inline void HAL_GPIO_##name##_pulldown(void) \
111 { \
112 (void)HAL_GPIO_##name##_pulldown; \
113 } \
114 \
115 static inline int HAL_GPIO_##name##_read(void) \
116 { \
117 return pin_levels[HAL_GPIO_PORT##port][pin]; \
118 (void)HAL_GPIO_##name##_read; \
119 } \
120 \
121 static inline int HAL_GPIO_##name##_state(void) \
122 { \
123 return 0; \
124 (void)HAL_GPIO_##name##_state; \
125 } \
126 \
127 static inline void HAL_GPIO_##name##_pmuxen(int mux) \
128 { \
129 (void)HAL_GPIO_##name##_pmuxen; \
130 } \
131 \
132 static inline void HAL_GPIO_##name##_pmuxdis(void) \
133 { \
134 (void)HAL_GPIO_##name##_pmuxdis; \
135 } \
136 \
137 static inline uint8_t HAL_GPIO_##name##_pin(void) \
138 { \
139 return ((HAL_GPIO_PORT##port << 5) | (pin & 0x1f)); \
140 (void)HAL_GPIO_##name##_pin; \
141 } \
142 \
143
144#else
145
146#define HAL_GPIO_PIN(name, port, pin) \
147 static inline void HAL_GPIO_##name##_set(void) \
148 { \
149 PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
150 (void)HAL_GPIO_##name##_set; \
151 } \
152 \
153 static inline void HAL_GPIO_##name##_clr(void) \
154 { \
155 PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
156 (void)HAL_GPIO_##name##_clr; \
157 } \
158 \
159 static inline void HAL_GPIO_##name##_toggle(void) \
160 { \
161 PORT->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin); \
162 (void)HAL_GPIO_##name##_toggle; \
163 } \
164 \
165 static inline void HAL_GPIO_##name##_write(int value) \
166 { \
167 if (value) \
168 PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
169 else \
170 PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
171 (void)HAL_GPIO_##name##_write; \
172 } \
173 \
174 static inline void HAL_GPIO_##name##_drvstr(int value) \
175 { \
176 if (value) \
177 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_DRVSTR; \
178 else \
179 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_DRVSTR; \
180 (void)HAL_GPIO_##name##_write; \
181 } \
182 \
183 static inline void HAL_GPIO_##name##_in(void) \
184 { \
185 PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
186 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
187 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN; \
188 (void)HAL_GPIO_##name##_in; \
189 } \
190 \
191 static inline void HAL_GPIO_##name##_out(void) \
192 { \
193 PORT->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin); \
194 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
195 (void)HAL_GPIO_##name##_out; \
196 } \
197 \
198 static inline void HAL_GPIO_##name##_off(void) \
199 { \
200 PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
201 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~(PORT_PINCFG_PULLEN | PORT_PINCFG_INEN); \
202 (void)HAL_GPIO_##name##_off; \
203 } \
204 \
205 static inline void HAL_GPIO_##name##_pullup(void) \
206 { \
207 PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
208 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
209 (void)HAL_GPIO_##name##_pullup; \
210 } \
211 \
212 static inline void HAL_GPIO_##name##_pulldown(void) \
213 { \
214 PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
215 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
216 (void)HAL_GPIO_##name##_pulldown; \
217 } \
218 \
219 static inline int HAL_GPIO_##name##_read(void) \
220 { \
221 return (PORT->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0; \
222 (void)HAL_GPIO_##name##_read; \
223 } \
224 \
225 static inline int HAL_GPIO_##name##_state(void) \
226 { \
227 return (PORT->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0; \
228 (void)HAL_GPIO_##name##_state; \
229 } \
230 \
231 static inline void HAL_GPIO_##name##_pmuxen(int mux) \
232 { \
233 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN; \
234 if (pin & 1) \
235 PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux; \
236 else \
237 PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux; \
238 (void)HAL_GPIO_##name##_pmuxen; \
239 } \
240 \
241 static inline void HAL_GPIO_##name##_pmuxdis(void) \
242 { \
243 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN; \
244 (void)HAL_GPIO_##name##_pmuxdis; \
245 } \
246 \
247 static inline uint8_t HAL_GPIO_##name##_pin(void) \
248 { \
249 return ((HAL_GPIO_PORT##port << 5) | (pin & 0x1f)); \
250 (void)HAL_GPIO_##name##_pin; \
251 } \
252 \
253
254#endif // __EMSCRIPTEN__
255
256/*- Runtime GPIO helpers ----------------------------------------------------*/
257/* These operate on packed pin values returned by HAL_GPIO_*_pin(). */
258/* Encoding: (port << 5) | (pin & 0x1f) */
259
260#ifndef __EMSCRIPTEN__
261
262static inline void hal_gpio_set(uint8_t packed) {
263 PORT->Group[packed >> 5].OUTSET.reg = (1 << (packed & 0x1f));
264}
265
266static inline void hal_gpio_clr(uint8_t packed) {
267 PORT->Group[packed >> 5].OUTCLR.reg = (1 << (packed & 0x1f));
268}
269
270static inline void hal_gpio_out(uint8_t packed) {
271 uint8_t port = packed >> 5;
272 uint8_t pin = packed & 0x1f;
273 PORT->Group[port].DIRSET.reg = (1 << pin);
274 PORT->Group[port].PINCFG[pin].reg |= PORT_PINCFG_INEN;
275}
276
277static inline void hal_gpio_in(uint8_t packed) {
278 uint8_t port = packed >> 5;
279 uint8_t pin = packed & 0x1f;
280 PORT->Group[port].DIRCLR.reg = (1 << pin);
281 PORT->Group[port].PINCFG[pin].reg |= PORT_PINCFG_INEN;
282 PORT->Group[port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN;
283}
284
285static inline int hal_gpio_read(uint8_t packed) {
286 return (PORT->Group[packed >> 5].IN.reg & (1 << (packed & 0x1f))) != 0;
287}
288
289#endif // !__EMSCRIPTEN__
290
291HAL_GPIO_PIN(SWCLK, A, 30)
292HAL_GPIO_PIN(SWDIO, A, 31)
293
294#ifdef APP_USES_TINYUSB
295HAL_GPIO_PIN(USB_N, A, 24)
296HAL_GPIO_PIN(USB_P, A, 25)
297#endif