API cleanup. Get all builds working.

This commit is contained in:
Savanni D'Gerinel 2022-07-12 22:42:42 -04:00
parent f76bcb18a2
commit c34886bad5
11 changed files with 70 additions and 78 deletions

View File

@ -14,26 +14,27 @@ You should have received a copy of the GNU General Public License along with Lum
#define __BASE_H__ #define __BASE_H__
#include <avr/io.h> #include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h> #include <util/delay.h>
#define LINE_OUT 1 #define LINE_OUT 1
#define LINE_IN 0 #define LINE_IN 0
typedef struct GPIO { typedef struct DIO {
volatile uint8_t *ddr; volatile uint8_t *ddr;
volatile uint8_t *port; volatile uint8_t *port;
volatile uint8_t *pin; volatile uint8_t *pin;
uint8_t addr; uint8_t addr;
} gpio_t; } dio_t;
/* /*
#define GPIOA(addr) (gpio_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr } #define DIO(addr) (dio_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr }
#define GPIOB(addr) (gpio_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr } #define DIO(addr) (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr }
#define GPIOC(addr) (gpio_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr } #define DIO(addr) (dio_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr }
#define GPIOD(addr) (gpio_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr } #define DIO(addr) (dio_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr }
*/ */
inline void set_line_direction(gpio_t *line, int direction) { inline void dio_set_direction(dio_t *line, int direction) {
if (direction == LINE_OUT) { if (direction == LINE_OUT) {
*(line->ddr) |= _BV(line->addr); *(line->ddr) |= _BV(line->addr);
} else { } else {
@ -41,15 +42,16 @@ inline void set_line_direction(gpio_t *line, int direction) {
} }
} }
inline void set_line(gpio_t *line) { inline void dio_set(dio_t *line, bool value) {
if (value) {
*(line->port) |= _BV(line->addr); *(line->port) |= _BV(line->addr);
} }
else {
inline void clear_line(gpio_t *line) {
*(line->port) &= ~(_BV(line->addr)); *(line->port) &= ~(_BV(line->addr));
}
} }
inline int read_line(gpio_t *line) { inline int dio_read(dio_t *line) {
return (*(line->pin) & _BV(line->addr)) > 0; return (*(line->pin) & _BV(line->addr)) > 0;
} }

View File

@ -19,10 +19,10 @@ You should have received a copy of the GNU General Public License along with Lum
#define ENABLE _BV(5) #define ENABLE _BV(5)
#define CHARCODE _BV(4) #define CHARCODE _BV(4)
void ping(gpio_t *light) { void ping(dio_t *light) {
set_line(light); dio_set(light, 1);
_delay_ms(100); _delay_ms(100);
clear_line(light); dio_set(light, 0);
_delay_ms(100); _delay_ms(100);
} }

View File

@ -36,7 +36,7 @@ typedef struct {
uint8_t duration; uint8_t duration;
} animation_t; } animation_t;
animation_t animation_new() { animation_t animation_new(void) {
return (animation_t){ return (animation_t){
.lamp = (rgb_t){ .brightness = 9, .r = 212, .g = 50, .b = 0 }, .lamp = (rgb_t){ .brightness = 9, .r = 212, .g = 50, .b = 0 },
.color_scheme = normal, .color_scheme = normal,
@ -96,8 +96,8 @@ int main (void) {
PORTB = 0; PORTB = 0;
_delay_ms(50); _delay_ms(50);
io_pin_t data_pin = { .port = &PORTB, .pin = 0 }; dio_t data_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 };
io_pin_t clock_pin = { .port = &PORTB, .pin = 2 }; dio_t clock_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 };
rng_t rng = rng_new(0); rng_t rng = rng_new(0);
animation_t animation = animation_new(); animation_t animation = animation_new();

View File

@ -78,8 +78,8 @@ void rfm_init(rfm_t *rfm, uint8_t *sync_word, size_t length, rfm_error_e *error)
if (!error) return; if (!error) return;
spi_initialize(&rfm->spi); spi_initialize(&rfm->spi);
set_line_direction(&rfm->irq, LINE_IN); dio_set_direction(&rfm->irq, LINE_IN);
set_line_direction(&rfm->reset, LINE_OUT); dio_set_direction(&rfm->reset, LINE_OUT);
rfm_reset(rfm); rfm_reset(rfm);
rfm_sleep(rfm); rfm_sleep(rfm);
@ -157,9 +157,9 @@ uint8_t sync_word(rfm_t *rfm, uint8_t sync_word[8]) {
} }
void rfm_reset(rfm_t *rfm) { void rfm_reset(rfm_t *rfm) {
set_line(&rfm->reset); dio_set(&rfm->reset, 1);
_delay_us(100); _delay_us(100);
clear_line(&rfm->reset); dio_set(&rfm->reset, 0);
_delay_us(5); _delay_us(5);
} }

View File

@ -25,8 +25,8 @@ typedef enum RFM_ERROR_TYPES {
typedef struct RFM { typedef struct RFM {
spi_t spi; spi_t spi;
gpio_t irq; dio_t irq;
gpio_t reset; dio_t reset;
} rfm_t; } rfm_t;
typedef enum packet_format { typedef enum packet_format {

View File

@ -14,37 +14,37 @@ You should have received a copy of the GNU General Public License along with Lum
#include <base.h> #include <base.h>
#include <shift_register.h> #include <shift_register.h>
void sr_strobe_line(gpio_t *line) { void sr_strobe_line(dio_t *line) {
set_line(line); dio_set(line, 1);
_delay_us(1); _delay_us(1);
clear_line(line); dio_set(line, 0);
} }
void sr_initialize(shift_register_t *reg) { void sr_initialize(shift_register_t *reg) {
set_line_direction(&reg->output, LINE_OUT); dio_set_direction(&reg->output, LINE_OUT);
set_line_direction(&reg->shift_clock, LINE_OUT); dio_set_direction(&reg->shift_clock, LINE_OUT);
set_line_direction(&reg->latch_clock, LINE_OUT); dio_set_direction(&reg->latch_clock, LINE_OUT);
clear_line(&reg->output); dio_set(&reg->output, 0);
clear_line(&reg->shift_clock); dio_set(&reg->shift_clock, 0);
clear_line(&reg->latch_clock); dio_set(&reg->latch_clock, 0);
} }
void sr_send_lsb(shift_register_t *reg, uint8_t byte) { void sr_send_lsb(shift_register_t *reg, uint8_t byte) {
for (uint8_t i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; i++) {
byte & _BV(i) ? set_line(&reg->output) : clear_line(&reg->output); byte & _BV(i) ? dio_set(&reg->output, 1) : dio_set(&reg->output, 0);
sr_strobe_line(&reg->shift_clock); sr_strobe_line(&reg->shift_clock);
} }
clear_line(&reg->output); dio_set(&reg->output, 0);
sr_strobe_line(&reg->latch_clock); sr_strobe_line(&reg->latch_clock);
} }
void sr_send_msb(shift_register_t *reg, uint8_t byte) { void sr_send_msb(shift_register_t *reg, uint8_t byte) {
for (int8_t i = 7; i >= 0; i--) { for (int8_t i = 7; i >= 0; i--) {
byte & _BV(i) ? set_line(&reg->output) : clear_line(&reg->output); byte & _BV(i) ? dio_set(&reg->output, 1) : dio_set(&reg->output, 0);
sr_strobe_line(&reg->shift_clock); sr_strobe_line(&reg->shift_clock);
} }
clear_line(&reg->output); dio_set(&reg->output, 0);
sr_strobe_line(&reg->latch_clock); sr_strobe_line(&reg->latch_clock);
} }

View File

@ -17,9 +17,9 @@ You should have received a copy of the GNU General Public License along with Lum
#include <base.h> #include <base.h>
typedef struct SHIFT_REGISTER { typedef struct SHIFT_REGISTER {
gpio_t output; dio_t output;
gpio_t shift_clock; dio_t shift_clock;
gpio_t latch_clock; dio_t latch_clock;
} shift_register_t; } shift_register_t;

View File

@ -12,33 +12,30 @@ You should have received a copy of the GNU General Public License along with Lum
#include "sk9822.h" #include "sk9822.h"
void send_byte(io_pin_t data_pin, io_pin_t clock_pin, uint8_t byte) { void send_byte(dio_t data_pin, dio_t clock_pin, uint8_t byte) {
for (int i = 7; i >= 0; i--) { for (int i = 7; i >= 0; i--) {
if (byte & _BV(i)) { dio_set(&data_pin, byte & _BV(i));
(*data_pin.port) |= _BV(data_pin.pin);
} else { dio_set(&clock_pin, 1);
(*data_pin.port) &= ~(_BV(data_pin.pin)); dio_set(&clock_pin, 0);
}
(*clock_pin.port) |= _BV(clock_pin.pin);
(*clock_pin.port) &= ~(_BV(clock_pin.pin));
} }
} }
void send_start(io_pin_t data_pin, io_pin_t clock_pin) { void send_start(dio_t data_pin, dio_t clock_pin) {
send_byte(data_pin, clock_pin, 0); send_byte(data_pin, clock_pin, 0);
send_byte(data_pin, clock_pin, 0); send_byte(data_pin, clock_pin, 0);
send_byte(data_pin, clock_pin, 0); send_byte(data_pin, clock_pin, 0);
send_byte(data_pin, clock_pin, 0); send_byte(data_pin, clock_pin, 0);
} }
void send_term(io_pin_t data_pin, io_pin_t clock_pin) { void send_term(dio_t data_pin, dio_t clock_pin) {
send_byte(data_pin, clock_pin, 0xff); send_byte(data_pin, clock_pin, 0xff);
send_byte(data_pin, clock_pin, 0xff); send_byte(data_pin, clock_pin, 0xff);
send_byte(data_pin, clock_pin, 0xff); send_byte(data_pin, clock_pin, 0xff);
send_byte(data_pin, clock_pin, 0xff); send_byte(data_pin, clock_pin, 0xff);
} }
void send_pixels(io_pin_t data_pin, io_pin_t clock_pin, rgb_t * pixels, uint8_t count) { void send_pixels(dio_t data_pin, dio_t clock_pin, rgb_t * pixels, uint8_t count) {
send_start(data_pin, clock_pin); send_start(data_pin, clock_pin);
for (uint8_t i = 0; i < count; i++) { for (uint8_t i = 0; i < count; i++) {
send_byte(data_pin, clock_pin, 0xe0 + pixels[i].brightness); send_byte(data_pin, clock_pin, 0xe0 + pixels[i].brightness);

View File

@ -23,6 +23,6 @@ typedef struct RGB_s {
uint8_t b; uint8_t b;
} rgb_t; } rgb_t;
void send_pixels(io_pin_t data_pin, io_pin_t clock_pin, rgb_t *pixels, uint8_t count); void send_pixels(dio_t data_pin, dio_t clock_pin, rgb_t *pixels, uint8_t count);
#endif #endif

View File

@ -15,20 +15,20 @@ You should have received a copy of the GNU General Public License along with Lum
#include <util/delay.h> #include <util/delay.h>
void spi_initialize(spi_t *spi) { void spi_initialize(spi_t *spi) {
set_line_direction(&spi->clock, LINE_OUT); dio_set_direction(&spi->clock, LINE_OUT);
set_line_direction(&spi->data_out, LINE_OUT); dio_set_direction(&spi->data_out, LINE_OUT);
set_line_direction(&spi->data_in, LINE_IN); dio_set_direction(&spi->data_in, LINE_IN);
set_line_direction(&spi->chip_select, LINE_OUT); dio_set_direction(&spi->chip_select, LINE_OUT);
set_line(&spi->chip_select); dio_set(&spi->chip_select, 1);
} }
void spi_acquire(spi_t *spi) { void spi_acquire(spi_t *spi) {
clear_line(&spi->chip_select); dio_set(&spi->chip_select, 0);
} }
void spi_release(spi_t *spi) { void spi_release(spi_t *spi) {
set_line(&spi->chip_select); dio_set(&spi->chip_select, 1);
} }
uint8_t spi_transceive(spi_t *spi, uint8_t output) { uint8_t spi_transceive(spi_t *spi, uint8_t output) {
@ -36,26 +36,19 @@ uint8_t spi_transceive(spi_t *spi, uint8_t output) {
int input_bit; int input_bit;
for (int i = 7; i >= 0; i--) { for (int i = 7; i >= 0; i--) {
clear_line(&spi->clock); dio_set(&spi->clock, 0);
if (output & _BV(i)) { if (output & _BV(i)) {
set_line(&spi->data_out); dio_set(&spi->data_out, 1);
} else { } else {
clear_line(&spi->data_out); dio_set(&spi->data_out, 0);
} }
_delay_us(10); _delay_us(10);
set_line(&spi->clock); dio_set(&spi->clock, 1);
input_bit = read_line(&spi->data_in); input_bit = dio_read(&spi->data_in);
input = (input << 1) | input_bit; input = (input << 1) | input_bit;
_delay_us(10); _delay_us(10);
} }
clear_line(&spi->clock); dio_set(&spi->clock, 0);
_delay_us(10); _delay_us(10);
return input; return input;
} }
/*
void send_bytes(spi_t *line, u8 *byte, size_t length, u8 *input, size_t input_length) {
}
*/

View File

@ -18,10 +18,10 @@ You should have received a copy of the GNU General Public License along with Lum
#include <stdlib.h> #include <stdlib.h>
typedef struct SPI { typedef struct SPI {
gpio_t clock; dio_t clock;
gpio_t data_out; dio_t data_out;
gpio_t data_in; dio_t data_in;
gpio_t chip_select; dio_t chip_select;
} spi_t; } spi_t;
void spi_initialize(spi_t *spi); void spi_initialize(spi_t *spi);