avr/base/base.h

54 lines
1.1 KiB
C

#include <avr/io.h>
#ifndef __BASE_H__
#define __BASE_H__
#define PIN_OUT 1
#define PIN_IN 0
typedef struct IO_PIN {
volatile uint8_t *ddr;
volatile uint8_t *port;
uint8_t addr;
} io_pin_t;
/*
#define GPIOA(addr) (io_pin_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr }
#define GPIOB(addr) (io_pin_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr }
#define GPIOC(addr) (io_pin_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr }
#define GPIOD(addr) (io_pin_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr }
*/
inline void set_pin_direction(io_pin_t *pin, int direction) {
if (direction == PIN_OUT) {
*(pin->ddr) |= _BV(pin->addr);
} else {
*(pin->ddr) &= ~(_BV(pin->addr));
}
}
inline void set_pin(io_pin_t *pin) {
*(pin->port) |= _BV(pin->addr);
}
inline void clear_pin(io_pin_t *pin) {
*(pin->port) &= ~(_BV(pin->addr));
}
inline int read_pin(io_pin_t *pin) {
return (*(pin->port) & _BV(pin->addr)) > 0;
}
typedef struct RNG {
uint8_t mod;
int8_t a;
int8_t c;
uint8_t seed;
} rng_t;
rng_t rng_new(uint8_t seed);
uint8_t rng_sample(rng_t *state);
#endif