SLS AVR Lib 0.1a
 
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------------+
2// This file is part of SLS AVR Library
3// https://github.com/SimonLitt/sls-avr-lib
4// ---------------------------------------------------------------------------+
5// Copyright (C) 2025 Simon Litt <simon@1itt.net> https://coding.1itt.net,
6// https://github.com/SimonLitt
7//
8// This program is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by the Free
10// Software Foundation, version 3.
11//
12// This program is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15// more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program. If not, see <https://www.gnu.org/licenses/>.
19// ---------------------------------------------------------------------------+
74#ifndef SLS_AVR_GPIO_H_
75#define SLS_AVR_GPIO_H_
76
77#ifndef SLS_AVR_AVR_H_
78# error "Include <sls-avr/avr.h> instead of this file."
79#endif
80
81#include <sls-common/defines.h>
82
84#define MAKE_PORT_NAME(_p) MAKE_GLUE_X2(PORT, _p)
86#define MAKE_DDR_NAME(_p) MAKE_GLUE_X2(DDR, _p)
88#define MAKE_PIN_NAME(_p) MAKE_GLUE_X2(PIN, _p)
89
91#define MAKE_MASK_x0 0x00
93#define MAKE_MASK_x1 _BV
95#define MAKE_MASK_x2(_b1, _b2) (_BV((_b1)) | _BV((_b2)))
97#define MAKE_MASK_x3(_b1, _b2, _b3) (_BV((_b1)) | _BV((_b2)) | _BV((_b3)))
99#define MAKE_MASK_x4(_b1, _b2, _b3, _b4) (_BV((_b1)) | _BV((_b2)) | _BV((_b3)) | _BV((_b4)))
101#define MAKE_MASK_x5(_b1, _b2, _b3, _b4, _b5) (_BV((_b1)) | _BV((_b2)) | _BV((_b3)) | _BV((_b4)) | _BV((_b5)))
103#define MAKE_MASK_x6(_b1, _b2, _b3, _b4, _b5, _b6) (_BV((_b1)) | _BV((_b2)) | _BV((_b3)) | _BV((_b4)) | _BV((_b5)) | _BV((_b6)))
105#define MAKE_MASK_x7(_b1, _b2, _b3, _b4, _b5, _b6, _b7) (_BV((_b1)) | _BV((_b2)) | _BV((_b3)) | _BV((_b4)) | _BV((_b5)) | _BV((_b6)) | _BV((_b7)))
107#define MAKE_MASK_x8 0xFF
108
109// ---------------------------------------------------------------------------+
110// The entire port
111// ---------------------------------------------------------------------------+
116#define gpio_init(_ddr, _val) ((_ddr) = (_val))
117#define GPIO_INIT(_p, _val) gpio_init(MAKE_DDR_NAME(_p), (_val))
118
123#define gpio_set(_port, _val) ((_port) = (_val))
124#define GPIO_SET(_p, _val) gpio_set(MAKE_PORT_NAME(_p), (_val))
125
126
130#define gpio_switch(_port) ((_port) = ~(_port))
131#define GPIO_SWITCH(_p) gpio_switch(MAKE_PORT_NAME(_p))
132
138#define gpio_byte(_pin) (_pin)
139#define GPIO_BYTE(_p) gpio_byte(MAKE_PIN_NAME(_p))
140
144#define gpio_set_in(_ddr) gpio_init((_ddr), 0x00);
145#define GPIO_SET_IN(_p) gpio_set_in(MAKE_DDR_NAME(_p))
146
151#define gpio_set_in_z(_ddr, _port) gpio_set_in((_ddr)); gpio_set_lo((_port))
152#define GPIO_SET_IN_Z(_p) gpio_set_in_z(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
153
158#define gpio_set_in_pu(_ddr, _port) gpio_set_in((_ddr)); gpio_pull_up((_port))
159#define GPIO_SET_IN_PU(_p) gpio_set_in_pu(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
160
164#define gpio_set_out(_ddr) gpio_init((_ddr), 0xFF);
165#define GPIO_SET_OUT(_p) gpio_set_out(MAKE_DDR_NAME(_p))
166
173#define gpio_set_out_d_lo(_ddr, _port) gpio_set_out((_ddr)); gpio_set_lo((_port))
174#define GPIO_SET_OUT_D_LO(_p) gpio_set_out_d_lo(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
175
182#define gpio_set_out_d_hi(_ddr, _port) gpio_set_out((_ddr)); gpio_set_hi((_port))
183#define GPIO_SET_OUT_D_HI(_p) gpio_set_out_d_hi(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
184
190#define gpio_set_hi(_port) gpio_set((_port), 0xFF)
191#define GPIO_SET_HI(_p) gpio_set_hi(MAKE_PORT_NAME(_p))
192
198#define gpio_set_lo(_port) gpio_set((_port), 0x00)
199#define GPIO_SET_LO(_p) gpio_set_lo(MAKE_PORT_NAME(_p))
200
201// ---------------------------------------------------------------------------+
202// The entire port aliases
203// ---------------------------------------------------------------------------+
204#define gpio_read gpio_byte
205#define GPIO_READ GPIO_BYTE
206
207#define read_byte gpio_byte
208#define READ_BYTE GPIO_BYTE
209
210#define gpio_to_read gpio_set_in
211#define GPIO_TO_READ GPIO_SET_IN
212
213#define gpio_to_read_z gpio_set_in_z
214#define GPIO_TO_READ_Z GPIO_SET_IN_Z
215
216#define gpio_to_read_pu gpio_set_in_pu
217#define GPIO_TO_READ_PU GPIO_SET_IN_PU
218
219#define gpio_pull_up gpio_set_hi
220#define GPIO_PULL_UP GPIO_SET_HI
221
222#define gpio_to_write gpio_set_out
223#define GPIO_TO_WRITE GPIO_SET_OUT
224
225#define gpio_to_write_d_lo gpio_set_out_d_lo
226#define GPIO_TO_WRITE_D_LO GPIO_SET_OUT_D_LO
227
228#define gpio_to_write_d_hi gpio_set_out_d_hi
229#define GPIO_TO_WRITE_D_HI GPIO_SET_OUT_D_HI
230
231#define gpio_write gpio_set
232#define GPIO_WRITE GPIO_SET
233
234#define write_byte gpio_set
235#define WRITE_BYTE GPIO_SET
236
237// ---------------------------------------------------------------------------+
238// The port pin set
239// ---------------------------------------------------------------------------+
240#define port_set_in(_ddr, _pinset) ((_ddr) &= ~(_pinset))
241#define PORT_SET_IN(_p, _pinset) port_set_in(MAKE_DDR_NAME(_p), (_pinset))
242
243#define port_set_out(_ddr, _pinset) ((_ddr) |= (_pinset))
244#define PORT_SET_OUT(_p, _pinset) port_set_out(MAKE_DDR_NAME(_p), (_pinset))
245
246#define port_set(_port, _pinset) ((_port) |= (_pinset))
247#define PORT_SET(_p, _pinset) port_set(MAKE_PORT_NAME(_p), (_pinset))
248
249#define port_clear(_port, _pinset) ((_port) &= ~(_pinset))
250#define PORT_CLEAR(_p, _pinset) port_clear(MAKE_PORT_NAME(_p), (_pinset))
251
252#define port_switch(_port, _pinset) ((_port) ^= (_pinset))
253#define PORT_SWITCH(_p, _pinset) port_switch(MAKE_PORT_NAME(_p), (_pinset))
254
255#define port_replace(_port, _pinset, _val) ((_port) = ((_port) & (~(_pinset))) | ((_val) & (_pinset)))
256#define PORT_REPLACE(_p, _pinset, _val) port_replace(MAKE_PORT_NAME(_p), (_pinset), (_val))
257
258#define port_read(_pin, _pinset) (read_byte((_pin)) & (_pinset))
259#define PORT_READ(_p, _pinset) port_read(MAKE_PIN_NAME(_p), (_pinset))
260
261#define port_set_in_z(_ddr, _port, _pinset) port_set_in((_ddr), (_pinset)); port_clear((_port), (_pinset))
262#define PORT_SET_IN_Z(_p, _pinset) port_set_in_z(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_pinset))
263
264#define port_set_in_pu(_ddr, _port, _pinset) port_set_in((_ddr), (_pinset)); port_pull_up((_port), (_pinset))
265#define PORT_SET_IN_PU(_p, _pinset) port_set_in_pu(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_pinset))
266
267#define port_set_out_d_lo(_ddr, _port, _pinset) port_set_out((_ddr), (_pinset)); port_clear((_port), (_pinset))
268#define PORT_SET_OUT_D_LO(_p, _pinset) port_set_out_d_lo(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_pinset))
269
270#define port_set_out_d_hi(_ddr, _port, _pinset) port_set_out((_ddr), (_pinset)); port_set((_port), (_pinset))
271#define PORT_SET_OUT_D_HI(_p, _pinset) port_set_out_d_hi(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_pinset))
272
273// ---------------------------------------------------------------------------+
274// The port pin set aliases
275// ---------------------------------------------------------------------------+
276#define port_set_hi port_set
277#define PORT_SET_HI PORT_SET
278
279#define port_pull_up port_set
280#define PORT_PULL_UP PORT_SET
281
282#define port_set_lo port_clear
283#define PORT_SET_LO PORT_CLEAR
284
285#define port_to_read port_set_in
286#define PORT_TO_READ PORT_SET_IN
287
288#define port_to_read_pu port_set_in_pu
289#define PORT_TO_READ_PU PORT_SET_IN_PU
290
291#define port_to_read_z port_set_in_z
292#define PORT_TO_READ_Z PORT_SET_IN_Z
293
294#define port_to_write port_set_out
295#define PORT_TO_WRITE PORT_SET_OUT
296
297#define port_write port_replace
298#define PORT_WRITE PORT_REPLACE
299
300#define port_to_write_d_lo port_set_out_d_lo
301#define PORT_TO_WRITE_D_LO PORT_SET_OUT_D_LO
302
303#define port_to_write_d_hi port_set_out_d_hi
304#define PORT_TO_WRITE_D_HI PORT_SET_OUT_D_HI
305
306#define NIBBLE_LO_PINSET 0x0F
307
308#define nibble_lo_set_in(_ddr) port_set_in((_ddr), NIBBLE_LO_PINSET)
309#define NIBBLE_LO_SET_IN(_p) nibble_lo_set_in(MAKE_DDR_NAME(_p))
310
311#define nibble_lo_set_out(_ddr) port_set_out((_ddr), NIBBLE_LO_PINSET)
312#define NIBBLE_LO_SET_OUT(_p) nibble_lo_set_out(MAKE_DDR_NAME(_p))
313
314#define nibble_lo_set(_port) port_set((_port), NIBBLE_LO_PINSET)
315#define NIBBLE_LO_SET(_p) nibble_lo_set(MAKE_PORT_NAME(_p))
316
317#define nibble_lo_clear(_port) port_clear((_port), NIBBLE_LO_PINSET)
318#define NIBBLE_LO_CLEAR(_p) nibble_lo_clear(MAKE_PORT_NAME(_p))
319
320#define nibble_lo_switch(_port) port_switch((_port), NIBBLE_LO_PINSET)
321#define NIBBLE_LO_SWITCH(_p) nibble_lo_switch(MAKE_PORT_NAME(_p)))
322
323#define nibble_lo_replace(_port, _val) port_replace(MAKE_PORT_NAME(_p), NIBBLE_LO_PINSET, (_val))
324#define NIBBLE_LO_REPLACE(_p, _val) nibble_lo_replace(MAKE_PORT_NAME(_p), (_val))
325
326#define nibble_lo_read(_pin) port_read((_pin), (NIBBLE_LO_PINSET))
327#define NIBBLE_LO_READ(_p) nibble_lo_read(MAKE_PIN_NAME(_p))
328
329#define nibble_lo_set_in_z(_ddr, _port) port_set_in_z((_ddr), (_port), NIBBLE_LO_PINSET)
330#define NIBBLE_LO_SET_IN_Z(_p) nibble_lo_set_in_z(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
331
332#define nibble_lo_set_in_pu(_ddr, _port) port_set_in_pu((_ddr), (_port), NIBBLE_LO_PINSET)
333#define NIBBLE_LO_SET_IN_PU(_p) nibble_lo_set_in_pu(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
334
335#define nibble_lo_set_out_d_lo(_ddr, _port) port_set_out_d_lo((_ddr), (_port), NIBBLE_LO_PINSET)
336#define NIBBLE_LO_SET_OUT_D_LO(_p) nibble_lo_set_out_d_lo(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
337
338#define nibble_lo_set_out_d_hi(_ddr, _port) port_set_out_d_hi((_ddr), (_port), NIBBLE_LO_PINSET)
339#define NIBBLE_LO_SET_OUT_D_HI(_p) nibble_lo_set_out_d_hi(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
340
341// ---------------------------------------------------------------------------+
342// The lower nibble pin set aliases
343// ---------------------------------------------------------------------------+
344#define nibble_lo_set_hi nibble_lo_set
345#define NIBBLE_LO_SET_HI NIBBLE_LO_SET
346
347#define nibble_lo_pull_up nibble_lo_set
348#define NIBBLE_LO_PULL_UP NIBBLE_LO_SET
349
350#define nibble_lo_set_lo nibble_lo_clear
351#define NIBBLE_LO_SET_LO NIBBLE_LO_CLEAR
352
353#define nibble_lo_to_read nibble_lo_set_in
354#define NIBBLE_LO_TO_READ NIBBLE_LO_SET_IN
355
356#define nibble_lo_to_read_pu nibble_lo_set_in_pu
357#define NIBBLE_LO_TO_READ_PU NIBBLE_LO_SET_IN_PU
358
359#define nibble_lo_to_read_z nibble_lo_set_in_z
360#define NIBBLE_LO_TO_READ_Z NIBBLE_LO_SET_IN_Z
361
362#define nibble_lo_to_write nibble_lo_set_out
363#define NIBBLE_LO_TO_WRITE NIBBLE_LO_SET_OUT
364
365#define nibble_lo_write nibble_lo_replace
366#define NIBBLE_LO_WRITE NIBBLE_LO_REPLACE
367
368#define nibble_lo_to_write_d_lo nibble_lo_set_out_d_lo
369#define NIBBLE_LO_TO_WRITE_D_LO NIBBLE_LO_SET_OUT_D_LO
370
371#define nibble_lo_to_write_d_hi nibble_lo_set_out_d_hi
372#define NIBBLE_LO_TO_WRITE_D_HI NIBBLE_LO_SET_OUT_D_HI
373
374#define NIBBLE_UP_PINSET 0xF0
375
376#define nibble_up_set_in(_ddr) port_set_in((_ddr), NIBBLE_UP_PINSET)
377#define NIBBLE_UP_SET_IN(_p) nibble_up_set_in(MAKE_DDR_NAME(_p))
378
379#define nibble_up_set_out(_ddr) port_set_out((_ddr), NIBBLE_UP_PINSET)
380#define NIBBLE_UP_SET_OUT(_p) nibble_up_set_out(MAKE_DDR_NAME(_p))
381
382#define nibble_up_set(_port) port_set((_port), NIBBLE_UP_PINSET)
383#define NIBBLE_UP_SET(_p) nibble_up_set(MAKE_PORT_NAME(_p))
384
385#define nibble_up_clear(_port) port_clear((_port), NIBBLE_UP_PINSET)
386#define NIBBLE_UP_CLEAR(_p) nibble_up_clear(MAKE_PORT_NAME(_p))
387
388#define nibble_up_switch(_port) port_switch((_port), NIBBLE_UP_PINSET)
389#define NIBBLE_UP_SWITCH(_p) nibble_up_switch(MAKE_PORT_NAME(_p)))
390
391#define nibble_up_replace(_port, _val) port_replace(MAKE_PORT_NAME(_p), NIBBLE_UP_PINSET, (_val))
392#define NIBBLE_UP_REPLACE(_p, _val) nibble_up_replace(MAKE_PORT_NAME(_p), (_val))
393
394#define nibble_up_read(_pin) port_read((_pin), (NIBBLE_UP_PINSET))
395#define NIBBLE_UP_READ(_p) nibble_up_read(MAKE_PIN_NAME(_p))
396
397#define nibble_up_set_in_z(_ddr, _port) port_set_in_z((_ddr), (_port), NIBBLE_UP_PINSET)
398#define NIBBLE_UP_SET_IN_Z(_p) nibble_up_set_in_z(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
399
400#define nibble_up_set_in_pu(_ddr, _port) port_set_in_pu((_ddr), (_port), NIBBLE_UP_PINSET)
401#define NIBBLE_UP_SET_IN_PU(_p) nibble_up_set_in_pu(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
402
403#define nibble_up_set_out_d_lo(_ddr, _port) port_set_out_d_lo((_ddr), (_port), NIBBLE_UP_PINSET)
404#define NIBBLE_UP_SET_OUT_D_LO(_p) nibble_up_set_out_d_lo(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
405
406#define nibble_up_set_out_d_hi(_ddr, _port) port_set_out_d_hi((_ddr), (_port), NIBBLE_UP_PINSET)
407#define NIBBLE_UP_SET_OUT_D_HI(_p) nibble_up_set_out_d_hi(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p))
408
409// ---------------------------------------------------------------------------+
410// The upper nibble pin set aliases
411// ---------------------------------------------------------------------------+
412#define nibble_up_set_hi nibble_up_set
413#define NIBBLE_UP_SET_HI NIBBLE_UP_SET
414
415#define nibble_up_pull_up nibble_up_set
416#define NIBBLE_UP_PULL_UP NIBBLE_UP_SET
417
418#define nibble_up_set_lo nibble_up_clear
419#define NIBBLE_UP_SET_LO NIBBLE_UP_CLEAR
420
421#define nibble_up_to_read nibble_up_set_in
422#define NIBBLE_UP_TO_READ NIBBLE_UP_SET_IN
423
424#define nibble_up_to_read_pu nibble_up_set_in_pu
425#define NIBBLE_UP_TO_READ_PU NIBBLE_UP_SET_IN_PU
426
427#define nibble_up_to_read_z nibble_up_set_in_z
428#define NIBBLE_UP_TO_READ_Z NIBBLE_UP_SET_IN_Z
429
430#define nibble_up_to_write nibble_up_set_out
431#define NIBBLE_UP_TO_WRITE NIBBLE_UP_SET_OUT
432
433#define nibble_up_write nibble_up_replace
434#define NIBBLE_UP_WRITE NIBBLE_UP_REPLACE
435
436#define nibble_up_to_write_d_lo nibble_up_set_out_d_lo
437#define NIBBLE_UP_TO_WRITE_D_LO NIBBLE_UP_SET_OUT_D_LO
438
439#define nibble_up_to_write_d_hi nibble_up_set_out_d_hi
440#define NIBBLE_UP_TO_WRITE_D_HI NIBBLE_UP_SET_OUT_D_HI
441
442// ---------------------------------------------------------------------------+
443// The distinct pin set
444// ---------------------------------------------------------------------------+
445#define pin_set_in(_ddr, _b) port_set_in((_ddr), _BV((_b)))
446#define PIN_SET_IN(_p, _b) pin_set_in(MAKE_DDR_NAME(_p), (_b))
447
448#define pin_set_out(_ddr, _b) port_set_out((_ddr), _BV((_b)))
449#define PIN_SET_OUT(_p, _b) pin_set_out(MAKE_DDR_NAME(_p), (_b))
450
451#define pin_set(_port, _b) port_set((_port), _BV((_b)))
452#define PIN_SET(_p, _b) pin_set(MAKE_PORT_NAME(_p), (_b))
453
454#define pin_clear(_port, _b) port_clear((_port), _BV((_b)))
455#define PIN_CLEAR(_p, _b) pin_clear(MAKE_PORT_NAME(_p), (_b))
456
457#define pin_switch(_port, _b) port_switch((_port), _BV((_b)))
458#define PIN_SWITCH(_p, _b) pin_switch(MAKE_PORT_NAME(_p), (_b))
459
460#define pin_replace(_port, _b, _val) port_replace((_port), _BV((_b)), (_val))
461#define PIN_REPLACE(_p, _b, _val) pin_replace(MAKE_PORT_NAME(_p), (_b), (_val))
462
463#define pin_read(_pin, _b) port_read((_pin), (_BV((_b))))
464#define PIN_READ(_p, _b) pin_read(MAKE_PIN_NAME(_p), (_b))
465
466#define pin_set_in_z(_ddr, _port, _b) port_set_in_z((_ddr), (_port), _BV((_b)))
467#define PIN_SET_IN_Z(_p, _b) pin_set_in_z(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_b))
468
469#define pin_set_in_pu(_ddr, _port, _b) port_set_in_pu((_ddr), (_port), _BV((_b)))
470#define PIN_SET_IN_PU(_p, _b) pin_set_in_pu(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_b))
471
472#define pin_set_out_d_lo(_ddr, _port, _b) port_set_out_d_lo((_ddr), (_port), _BV((_b)))
473#define PIN_SET_OUT_D_LO(_p, _b) pin_set_out_d_lo(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_b))
474
475#define pin_set_out_d_hi(_ddr, _port, _b) port_set_out_d_hi((_ddr), (_port), _BV((_b)))
476#define PIN_SET_OUT_D_HI(_p, _b) pin_set_out_d_hi(MAKE_DDR_NAME(_p), MAKE_PORT_NAME(_p), (_b))
477
478// ---------------------------------------------------------------------------+
479// The distinct pin aliases
480// ---------------------------------------------------------------------------+
481#define pin_set_hi pin_set
482#define PIN_SET_HI PIN_SET
483
484#define pin_on pin_set
485#define PIN_ON PIN_SET
486
487#define pin_pull_up pin_set
488#define PIN_PULL_UP PIN_SET
489
490#define pin_set_lo pin_clear
491#define PIN_SET_LO PIN_CLEAR
492
493#define pin_off pin_clear
494#define PIN_OFF PIN_CLEAR
495
496#define pin_to_read pin_set_in
497#define PIN_TO_READ PIN_SET_IN
498
499#define pin_to_read_pu pin_set_in_pu
500#define PIN_TO_READ_PU PIN_SET_IN_PU
501
502#define pin_to_read_z pin_set_in_z
503#define PIN_TO_READ_Z PIN_SET_IN_Z
504
505#define pin_to_write pin_set_out
506#define PIN_TO_WRITE PIN_SET_OUT
507
508#define pin_write pin_replace
509#define PIN_WRITE PIN_REPLACE
510
511#define pin_to_write_d_lo pin_set_out_d_lo
512#define PIN_TO_WRITE_D_LO PIN_SET_OUT_D_LO
513
514#define pin_to_write_d_hi pin_set_out_d_hi
515#define PIN_TO_WRITE_D_HI PIN_SET_OUT_D_HI
516
517#endif /* SLS_AVR_GPIO_H_ */
SLS СС Library.