SLS AVR Lib 0.1a
 
Loading...
Searching...
No Matches
defs.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// ---------------------------------------------------------------------------+
55#include <stdint.h>
56
57#ifndef SLS_AVR_DEFS_H_
58# define SLS_AVR_DEFS_H_
59
60#ifndef SLS_AVR_AVR_H_
61# error "Include <sls-avr/avr.h> instead of this file."
62#endif
63
64typedef uint8_t byte_t;
65
67typedef struct {
68 byte_t lo : 4;
69 byte_t up : 4;
70} nibble_t;
71
73typedef struct {
74 byte_t b0 : 1;
75 byte_t b1 : 1;
76 byte_t b2 : 1;
77 byte_t b3 : 1;
78 byte_t b4 : 1;
79 byte_t b5 : 1;
80 byte_t b6 : 1;
81 byte_t b7 : 1;
82} flags_t;
83
90
93typedef volatile byte_t *const ddr_t;
94typedef volatile byte_t *const port_t;
95typedef volatile byte_t *const pin_t;
96
97// ---------------------------------------------------------------------------+
98// Creating missing references
99// ---------------------------------------------------------------------------+
100
101#define pin_2_ddr(_ref) *((_ref) + 1)
102#define pin_2_port(_ref) *((_ref) + 2)
103
104#define ddr_2_pin(_ref) *((_ref) - 1)
105#define ddr_2_port(_ref) *((_ref) + 1)
106
107#define port_2_ddr(_ref) *((_ref) - 1)
108#define port_2_pin(_ref) *((_ref) - 2)
109
110// ---------------------------------------------------------------------------+
111// Helpers
112// ---------------------------------------------------------------------------+
118#define NIBBLE_SWAP(_b) (((_b) << 4) | ((_b) >> 4))
119
126static inline byte_t nibble_swap(byte_t byte) {
127 return
128 #if (__BUILTIN_AVR_SWAP == 1)
129 __builtin_avr_swap(byte);
130 #else
131 NIBBLE_SWAP(byte);
132 #endif // __BUILTIN_AVR_SWAP
133}
134
141#define flag_is_set(_val, _b) ((_val) & _BV((_b)))
142
149#define flag_is_clear(_val, _b) (!((_val) & _BV((_b))))
150
155#define make_port_mask1(_p0) (_BV((_p0)))
156
161#define make_port_mask2(_p0, _p1) (_BV((_p0)) & _BV((_p1)))
162
167#define make_port_mask3(_p0, _p1, _p2) (_BV((_p0)) & _BV((_p1)) & _BV((_p2)))
168
173#define make_port_mask4(_p0, _p1, _p2, _p3) (_BV((_p0)) & _BV((_p1)) & _BV((_p2)) & _BV((_p3)))
174
179#define make_port_mask5(_p0, _p1, _p2, _p3, _p4) (_BV((_p0)) & _BV((_p1)) & _BV((_p2)) & _BV((_p3)) & _BV((_p4)))
180
185#define make_port_mask6(_p0, _p1, _p2, _p3, _p4, _p5) (_BV((_p0)) & _BV((_p1)) & _BV((_p2)) & _BV((_p3)) & _BV((_p4)) & _BV((_p5)))
186
191#define make_port_mask7(_p0, _p1, _p2, _p3, _p4, _p5, _p6) (_BV((_p0)) & _BV((_p1)) & _BV((_p2)) & _BV((_p3)) & _BV((_p4)) & _BV((_p5)) & _BV((_p6)))
192
197#define make_port_mask8(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7) (_BV((_p0)) & _BV((_p1)) & _BV((_p2)) & _BV((_p3)) & _BV((_p4)) & _BV((_p5)) & _BV((_p6)) & _BV((_p7)))
198
199#endif // SLS_AVR_DEFS_H_
uint8_t byte_t
8-bit type
Definition defs.h:64
static byte_t nibble_swap(byte_t byte)
Swaps the high and low nibbles (half bytes) of a byte.
Definition defs.h:126
byte_t pin_bit_t
The type for specifying the pin bit.
Definition defs.h:91
volatile byte_t *const ddr_t
Type for passing a reference to a direct register.
Definition defs.h:93
volatile byte_t *const port_t
Type for passing a reference to a port register.
Definition defs.h:94
byte_t port_mask_t
The type for specifying the port mask.
Definition defs.h:92
volatile byte_t *const pin_t
Type for passing a reference to a pin register.
Definition defs.h:95
#define NIBBLE_SWAP(_b)
Swaps the high and low nibbles (half bytes) of a byte.
Definition defs.h:118
Structure for working with bits.
Definition defs.h:73
byte_t b7
The highest 7-bit.
Definition defs.h:81
byte_t b3
The 3-bit.
Definition defs.h:77
byte_t b0
The lowest 0-bit.
Definition defs.h:74
byte_t b2
The 2-bit.
Definition defs.h:76
byte_t b1
The 1-bit.
Definition defs.h:75
byte_t b5
The 5-bit.
Definition defs.h:79
byte_t b6
The 6-bit.
Definition defs.h:80
byte_t b4
The 4-bit.
Definition defs.h:78
Structure for working with upper and lower nibbles.
Definition defs.h:67
byte_t up
The upper nibble.
Definition defs.h:69
byte_t lo
The lower nibble.
Definition defs.h:68
Universal type for byte.
Definition defs.h:85
nibble_t nibble
Nibbles.
Definition defs.h:87
byte_t byte
Directly byte.
Definition defs.h:86
flags_t flags
Bits.
Definition defs.h:88