avr/base/base.h

59 lines
1.7 KiB
C
Raw Permalink Normal View History

2022-06-26 16:45:27 +00:00
/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
Lumeto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Lumeto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
*/
2022-04-17 04:43:50 +00:00
#ifndef __BASE_H__
#define __BASE_H__
2022-06-26 15:38:20 +00:00
#include <avr/io.h>
2022-07-13 02:42:42 +00:00
#include <stdbool.h>
2022-06-28 00:01:25 +00:00
#include <util/delay.h>
2022-06-26 15:38:20 +00:00
#define LINE_OUT 1
#define LINE_IN 0
2022-07-13 02:42:42 +00:00
typedef struct DIO {
volatile uint8_t *ddr;
2022-04-17 04:43:50 +00:00
volatile uint8_t *port;
volatile uint8_t *pin;
uint8_t addr;
2022-07-13 02:42:42 +00:00
} dio_t;
2022-04-17 04:43:50 +00:00
/*
2022-07-13 02:42:42 +00:00
#define DIO(addr) (dio_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr }
#define DIO(addr) (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr }
#define DIO(addr) (dio_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr }
#define DIO(addr) (dio_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr }
*/
2022-07-13 02:42:42 +00:00
inline void dio_set_direction(dio_t *line, int direction) {
if (direction == LINE_OUT) {
*(line->ddr) |= _BV(line->addr);
} else {
*(line->ddr) &= ~(_BV(line->addr));
}
}
2022-07-13 02:42:42 +00:00
inline void dio_set(dio_t *line, bool value) {
if (value) {
*(line->port) |= _BV(line->addr);
}
else {
*(line->port) &= ~(_BV(line->addr));
}
2022-04-17 04:43:50 +00:00
}
2022-07-13 02:42:42 +00:00
inline int dio_read(dio_t *line) {
2022-06-28 00:01:25 +00:00
return (*(line->pin) & _BV(line->addr)) > 0;
}
2022-04-17 04:43:50 +00:00
#endif