From c34886bad579067d29bd6ad8ba85f339f475776e Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 12 Jul 2022 22:42:42 -0400 Subject: [PATCH] API cleanup. Get all builds working. --- base/base.h | 30 ++++++++++++++++-------------- display/display.c | 6 +++--- flame/main.c | 6 +++--- rfm69hcw/rfm.c | 8 ++++---- rfm69hcw/rfm.h | 4 ++-- shift_register/shift_register.c | 26 +++++++++++++------------- shift_register/shift_register.h | 6 +++--- sk9822/sk9822.c | 19 ++++++++----------- sk9822/sk9822.h | 2 +- spi/spi.c | 33 +++++++++++++-------------------- spi/spi.h | 8 ++++---- 11 files changed, 70 insertions(+), 78 deletions(-) diff --git a/base/base.h b/base/base.h index 55ddee2..2f0bb4c 100644 --- a/base/base.h +++ b/base/base.h @@ -14,26 +14,27 @@ You should have received a copy of the GNU General Public License along with Lum #define __BASE_H__ #include +#include #include #define LINE_OUT 1 #define LINE_IN 0 -typedef struct GPIO { +typedef struct DIO { volatile uint8_t *ddr; volatile uint8_t *port; volatile uint8_t *pin; uint8_t addr; -} gpio_t; +} dio_t; /* -#define GPIOA(addr) (gpio_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr } -#define GPIOB(addr) (gpio_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr } -#define GPIOC(addr) (gpio_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr } -#define GPIOD(addr) (gpio_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr } +#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 } */ -inline void set_line_direction(gpio_t *line, int direction) { +inline void dio_set_direction(dio_t *line, int direction) { if (direction == LINE_OUT) { *(line->ddr) |= _BV(line->addr); } else { @@ -41,15 +42,16 @@ inline void set_line_direction(gpio_t *line, int direction) { } } -inline void set_line(gpio_t *line) { - *(line->port) |= _BV(line->addr); +inline void dio_set(dio_t *line, bool value) { + if (value) { + *(line->port) |= _BV(line->addr); + } + else { + *(line->port) &= ~(_BV(line->addr)); + } } -inline void clear_line(gpio_t *line) { - *(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; } diff --git a/display/display.c b/display/display.c index dc88305..615d89f 100644 --- a/display/display.c +++ b/display/display.c @@ -19,10 +19,10 @@ You should have received a copy of the GNU General Public License along with Lum #define ENABLE _BV(5) #define CHARCODE _BV(4) -void ping(gpio_t *light) { - set_line(light); +void ping(dio_t *light) { + dio_set(light, 1); _delay_ms(100); - clear_line(light); + dio_set(light, 0); _delay_ms(100); } diff --git a/flame/main.c b/flame/main.c index 77d5f12..d52b56a 100644 --- a/flame/main.c +++ b/flame/main.c @@ -36,7 +36,7 @@ typedef struct { uint8_t duration; } animation_t; -animation_t animation_new() { +animation_t animation_new(void) { return (animation_t){ .lamp = (rgb_t){ .brightness = 9, .r = 212, .g = 50, .b = 0 }, .color_scheme = normal, @@ -96,8 +96,8 @@ int main (void) { PORTB = 0; _delay_ms(50); - io_pin_t data_pin = { .port = &PORTB, .pin = 0 }; - io_pin_t clock_pin = { .port = &PORTB, .pin = 2 }; + dio_t data_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }; + dio_t clock_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }; rng_t rng = rng_new(0); animation_t animation = animation_new(); diff --git a/rfm69hcw/rfm.c b/rfm69hcw/rfm.c index 953ba03..ebd1c2a 100644 --- a/rfm69hcw/rfm.c +++ b/rfm69hcw/rfm.c @@ -78,8 +78,8 @@ void rfm_init(rfm_t *rfm, uint8_t *sync_word, size_t length, rfm_error_e *error) if (!error) return; spi_initialize(&rfm->spi); - set_line_direction(&rfm->irq, LINE_IN); - set_line_direction(&rfm->reset, LINE_OUT); + dio_set_direction(&rfm->irq, LINE_IN); + dio_set_direction(&rfm->reset, LINE_OUT); rfm_reset(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) { - set_line(&rfm->reset); + dio_set(&rfm->reset, 1); _delay_us(100); - clear_line(&rfm->reset); + dio_set(&rfm->reset, 0); _delay_us(5); } diff --git a/rfm69hcw/rfm.h b/rfm69hcw/rfm.h index 3ae1343..5b94ccd 100644 --- a/rfm69hcw/rfm.h +++ b/rfm69hcw/rfm.h @@ -25,8 +25,8 @@ typedef enum RFM_ERROR_TYPES { typedef struct RFM { spi_t spi; - gpio_t irq; - gpio_t reset; + dio_t irq; + dio_t reset; } rfm_t; typedef enum packet_format { diff --git a/shift_register/shift_register.c b/shift_register/shift_register.c index 61fd08c..50d12ec 100644 --- a/shift_register/shift_register.c +++ b/shift_register/shift_register.c @@ -14,37 +14,37 @@ You should have received a copy of the GNU General Public License along with Lum #include #include -void sr_strobe_line(gpio_t *line) { - set_line(line); +void sr_strobe_line(dio_t *line) { + dio_set(line, 1); _delay_us(1); - clear_line(line); + dio_set(line, 0); } void sr_initialize(shift_register_t *reg) { - set_line_direction(®->output, LINE_OUT); - set_line_direction(®->shift_clock, LINE_OUT); - set_line_direction(®->latch_clock, LINE_OUT); + dio_set_direction(®->output, LINE_OUT); + dio_set_direction(®->shift_clock, LINE_OUT); + dio_set_direction(®->latch_clock, LINE_OUT); - clear_line(®->output); - clear_line(®->shift_clock); - clear_line(®->latch_clock); + dio_set(®->output, 0); + dio_set(®->shift_clock, 0); + dio_set(®->latch_clock, 0); } void sr_send_lsb(shift_register_t *reg, uint8_t byte) { for (uint8_t i = 0; i < 8; i++) { - byte & _BV(i) ? set_line(®->output) : clear_line(®->output); + byte & _BV(i) ? dio_set(®->output, 1) : dio_set(®->output, 0); sr_strobe_line(®->shift_clock); } - clear_line(®->output); + dio_set(®->output, 0); sr_strobe_line(®->latch_clock); } void sr_send_msb(shift_register_t *reg, uint8_t byte) { for (int8_t i = 7; i >= 0; i--) { - byte & _BV(i) ? set_line(®->output) : clear_line(®->output); + byte & _BV(i) ? dio_set(®->output, 1) : dio_set(®->output, 0); sr_strobe_line(®->shift_clock); } - clear_line(®->output); + dio_set(®->output, 0); sr_strobe_line(®->latch_clock); } diff --git a/shift_register/shift_register.h b/shift_register/shift_register.h index 8fa46c5..e257d90 100644 --- a/shift_register/shift_register.h +++ b/shift_register/shift_register.h @@ -17,9 +17,9 @@ You should have received a copy of the GNU General Public License along with Lum #include typedef struct SHIFT_REGISTER { - gpio_t output; - gpio_t shift_clock; - gpio_t latch_clock; + dio_t output; + dio_t shift_clock; + dio_t latch_clock; } shift_register_t; diff --git a/sk9822/sk9822.c b/sk9822/sk9822.c index 980f000..84e25de 100644 --- a/sk9822/sk9822.c +++ b/sk9822/sk9822.c @@ -12,33 +12,30 @@ You should have received a copy of the GNU General Public License along with Lum #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--) { - if (byte & _BV(i)) { - (*data_pin.port) |= _BV(data_pin.pin); - } else { - (*data_pin.port) &= ~(_BV(data_pin.pin)); - } - (*clock_pin.port) |= _BV(clock_pin.pin); - (*clock_pin.port) &= ~(_BV(clock_pin.pin)); + dio_set(&data_pin, byte & _BV(i)); + + dio_set(&clock_pin, 1); + dio_set(&clock_pin, 0); } } -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); } -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); } -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); for (uint8_t i = 0; i < count; i++) { send_byte(data_pin, clock_pin, 0xe0 + pixels[i].brightness); diff --git a/sk9822/sk9822.h b/sk9822/sk9822.h index a64b6a1..9313dd0 100644 --- a/sk9822/sk9822.h +++ b/sk9822/sk9822.h @@ -23,6 +23,6 @@ typedef struct RGB_s { uint8_t b; } 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 diff --git a/spi/spi.c b/spi/spi.c index 4bed945..c9f3855 100644 --- a/spi/spi.c +++ b/spi/spi.c @@ -15,20 +15,20 @@ You should have received a copy of the GNU General Public License along with Lum #include void spi_initialize(spi_t *spi) { - set_line_direction(&spi->clock, LINE_OUT); - set_line_direction(&spi->data_out, LINE_OUT); - set_line_direction(&spi->data_in, LINE_IN); - set_line_direction(&spi->chip_select, LINE_OUT); + dio_set_direction(&spi->clock, LINE_OUT); + dio_set_direction(&spi->data_out, LINE_OUT); + dio_set_direction(&spi->data_in, LINE_IN); + 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) { - clear_line(&spi->chip_select); + dio_set(&spi->chip_select, 0); } 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) { @@ -36,26 +36,19 @@ uint8_t spi_transceive(spi_t *spi, uint8_t output) { int input_bit; for (int i = 7; i >= 0; i--) { - clear_line(&spi->clock); + dio_set(&spi->clock, 0); if (output & _BV(i)) { - set_line(&spi->data_out); + dio_set(&spi->data_out, 1); } else { - clear_line(&spi->data_out); + dio_set(&spi->data_out, 0); } _delay_us(10); - set_line(&spi->clock); - input_bit = read_line(&spi->data_in); + dio_set(&spi->clock, 1); + input_bit = dio_read(&spi->data_in); input = (input << 1) | input_bit; _delay_us(10); } - clear_line(&spi->clock); + dio_set(&spi->clock, 0); _delay_us(10); return input; } - -/* -void send_bytes(spi_t *line, u8 *byte, size_t length, u8 *input, size_t input_length) { - -} -*/ - diff --git a/spi/spi.h b/spi/spi.h index e8b6767..ea49e7e 100644 --- a/spi/spi.h +++ b/spi/spi.h @@ -18,10 +18,10 @@ You should have received a copy of the GNU General Public License along with Lum #include typedef struct SPI { - gpio_t clock; - gpio_t data_out; - gpio_t data_in; - gpio_t chip_select; + dio_t clock; + dio_t data_out; + dio_t data_in; + dio_t chip_select; } spi_t; void spi_initialize(spi_t *spi);