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
40#define HAL_GPIO_PMUX_A 0
41#define HAL_GPIO_PMUX_B 1
42#define HAL_GPIO_PMUX_C 2
43#define HAL_GPIO_PMUX_D 3
44#define HAL_GPIO_PMUX_E 4
45#define HAL_GPIO_PMUX_F 5
46#define HAL_GPIO_PMUX_G 6
47#define HAL_GPIO_PMUX_H 7
48#define HAL_GPIO_PMUX_I 8
49#define HAL_GPIO_PMUX_J 9
50#define HAL_GPIO_PMUX_K 10
51#define HAL_GPIO_PMUX_L 11
52#define HAL_GPIO_PMUX_M 12
53#define HAL_GPIO_PMUX_N 13
54
55#if __EMSCRIPTEN__
56
57extern bool pin_levels[3][32];
58
59#define HAL_GPIO_PIN(name, port, pin) \
60 static inline void HAL_GPIO_##name##_set(void) \
61 { \
62 pin_levels[HAL_GPIO_PORT##port][pin] = true; \
63 (void)HAL_GPIO_##name##_set; \
64 } \
65 \
66 static inline void HAL_GPIO_##name##_clr(void) \
67 { \
68 pin_levels[HAL_GPIO_PORT##port][pin] = false; \
69 (void)HAL_GPIO_##name##_clr; \
70 } \
71 \
72 static inline void HAL_GPIO_##name##_toggle(void) \
73 { \
74 pin_levels[HAL_GPIO_PORT##port][pin] = !pin_levels[HAL_GPIO_PORT##port][pin]; \
75 (void)HAL_GPIO_##name##_toggle; \
76 } \
77 \
78 static inline void HAL_GPIO_##name##_write(int value) \
79 { \
80 pin_levels[HAL_GPIO_PORT##port][pin] = !!value; \
81 (void)HAL_GPIO_##name##_write; \
82 } \
83 \
84 static inline void HAL_GPIO_##name##_drvstr(int value) \
85 { \
86 (void)HAL_GPIO_##name##_write; \
87 } \
88 \
89 static inline void HAL_GPIO_##name##_in(void) \
90 { \
91 (void)HAL_GPIO_##name##_in; \
92 } \
93 \
94 static inline void HAL_GPIO_##name##_out(void) \
95 { \
96 (void)HAL_GPIO_##name##_out; \
97 } \
98 \
99 static inline void HAL_GPIO_##name##_off(void) \
100 { \
101 (void)HAL_GPIO_##name##_off; \
102 } \
103 \
104 static inline void HAL_GPIO_##name##_pullup(void) \
105 { \
106 (void)HAL_GPIO_##name##_pullup; \
107 } \
108 \
109 static inline void HAL_GPIO_##name##_pulldown(void) \
110 { \
111 (void)HAL_GPIO_##name##_pulldown; \
112 } \
113 \
114 static inline int HAL_GPIO_##name##_read(void) \
115 { \
116 return pin_levels[HAL_GPIO_PORT##port][pin]; \
117 (void)HAL_GPIO_##name##_read; \
118 } \
119 \
120 static inline int HAL_GPIO_##name##_state(void) \
121 { \
122 return 0; \
123 (void)HAL_GPIO_##name##_state; \
124 } \
125 \
126 static inline void HAL_GPIO_##name##_pmuxen(int mux) \
127 { \
128 (void)HAL_GPIO_##name##_pmuxen; \
129 } \
130 \
131 static inline void HAL_GPIO_##name##_pmuxdis(void) \
132 { \
133 (void)HAL_GPIO_##name##_pmuxdis; \
134 } \
135 \
136 static inline uint8_t HAL_GPIO_##name##_pin(void) \
137 { \
138 return ((HAL_GPIO_PORT##port << 5) | (pin & 0x1f)); \
139 (void)HAL_GPIO_##name##_pin; \
140 } \
141 \
142
143#else
144
145#define HAL_GPIO_PIN(name, port, pin) \
146 static inline void HAL_GPIO_##name##_set(void) \
147 { \
148 PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
149 (void)HAL_GPIO_##name##_set; \
150 } \
151 \
152 static inline void HAL_GPIO_##name##_clr(void) \
153 { \
154 PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
155 (void)HAL_GPIO_##name##_clr; \
156 } \
157 \
158 static inline void HAL_GPIO_##name##_toggle(void) \
159 { \
160 PORT->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin); \
161 (void)HAL_GPIO_##name##_toggle; \
162 } \
163 \
164 static inline void HAL_GPIO_##name##_write(int value) \
165 { \
166 if (value) \
167 PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
168 else \
169 PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
170 (void)HAL_GPIO_##name##_write; \
171 } \
172 \
173 static inline void HAL_GPIO_##name##_drvstr(int value) \
174 { \
175 if (value) \
176 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_DRVSTR; \
177 else \
178 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_DRVSTR; \
179 (void)HAL_GPIO_##name##_write; \
180 } \
181 \
182 static inline void HAL_GPIO_##name##_in(void) \
183 { \
184 PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
185 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
186 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN; \
187 (void)HAL_GPIO_##name##_in; \
188 } \
189 \
190 static inline void HAL_GPIO_##name##_out(void) \
191 { \
192 PORT->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin); \
193 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \
194 (void)HAL_GPIO_##name##_out; \
195 } \
196 \
197 static inline void HAL_GPIO_##name##_off(void) \
198 { \
199 PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \
200 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~(PORT_PINCFG_PULLEN | PORT_PINCFG_INEN); \
201 (void)HAL_GPIO_##name##_off; \
202 } \
203 \
204 static inline void HAL_GPIO_##name##_pullup(void) \
205 { \
206 PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \
207 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
208 (void)HAL_GPIO_##name##_pullup; \
209 } \
210 \
211 static inline void HAL_GPIO_##name##_pulldown(void) \
212 { \
213 PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \
214 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \
215 (void)HAL_GPIO_##name##_pulldown; \
216 } \
217 \
218 static inline int HAL_GPIO_##name##_read(void) \
219 { \
220 return (PORT->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0; \
221 (void)HAL_GPIO_##name##_read; \
222 } \
223 \
224 static inline int HAL_GPIO_##name##_state(void) \
225 { \
226 return (PORT->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0; \
227 (void)HAL_GPIO_##name##_state; \
228 } \
229 \
230 static inline void HAL_GPIO_##name##_pmuxen(int mux) \
231 { \
232 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN; \
233 if (pin & 1) \
234 PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux; \
235 else \
236 PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux; \
237 (void)HAL_GPIO_##name##_pmuxen; \
238 } \
239 \
240 static inline void HAL_GPIO_##name##_pmuxdis(void) \
241 { \
242 PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN; \
243 (void)HAL_GPIO_##name##_pmuxdis; \
244 } \
245 \
246 static inline uint8_t HAL_GPIO_##name##_pin(void) \
247 { \
248 return ((HAL_GPIO_PORT##port << 5) | (pin & 0x1f)); \
249 (void)HAL_GPIO_##name##_pin; \
250 } \
251 \
252
253#endif // __EMSCRIPTEN__
254
255HAL_GPIO_PIN(SWCLK, A, 30)
256HAL_GPIO_PIN(SWDIO, A, 31)
257
258#ifdef APP_USES_TINYUSB
259HAL_GPIO_PIN(USB_N, A, 24)
260HAL_GPIO_PIN(USB_P, A, 25)
261#endif