From 290d784950b6248782b049cd9831bd6e034fd538 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 30 Sep 2025 13:06:16 -0600 Subject: Add a bunch. Neglected doing commits. Add many different patterns, and organize it. Add a voltage discovery subsystem to allow the ch573 to detect if it's on 12v or 5v lights. --- src/gpio.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'src/gpio.c') diff --git a/src/gpio.c b/src/gpio.c index e016179..b093b94 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -3,7 +3,24 @@ #include "ch573/pfic.h" #include "isr_vector.h" -void configure_gpio(gpio_pin_t pin, gpio_config_t cfg) +/* Sets all the GPIO pins to INPUT_PULL_UP configuration. This is to reduce + * electrical noise in the system. */ +void gpio_init() +{ + // Set GPIO's to input. + GPIO_PORT.dir_reg.set(GPIO_PORT_A, 0x0); + GPIO_PORT.dir_reg.set(GPIO_PORT_B, 0x0); + + // Turn off all the pull-down resistors. + GPIO_PORT.pd_drv_reg.set(GPIO_PORT_A, 0x0); + GPIO_PORT.pd_drv_reg.set(GPIO_PORT_B, 0x0); + + // Turn on the pull up resistors. + GPIO_PORT.pu_reg.set(GPIO_PORT_A, 0xffffffff); + GPIO_PORT.pu_reg.set(GPIO_PORT_B, 0xffffffff); +} + +void gpio_configure_pin(gpio_pin_t pin, gpio_config_t cfg) { int ipin = pin; struct ch573_gpio__gpio_port_t* port; @@ -43,7 +60,7 @@ void configure_gpio(gpio_pin_t pin, gpio_config_t cfg) } } -int read_gpio(gpio_pin_t pin) +int gpio_read(gpio_pin_t pin) { int ipin = pin; struct ch573_gpio__gpio_port_t* port; @@ -57,7 +74,7 @@ int read_gpio(gpio_pin_t pin) return !!GPIO_PORT.pin.in.get(port, ipin); } -void set_gpio(gpio_pin_t pin, int high) +void gpio_set(gpio_pin_t pin, int high) { int ipin = pin; struct ch573_gpio__gpio_port_t* port; @@ -71,9 +88,13 @@ void set_gpio(gpio_pin_t pin, int high) GPIO_PORT.out.set(port, !!high, ipin); } -void enable_alternate_function(alternate_function_t afn) +void gpio_enable_alternate_function(alternate_function_t afn, int enable) { - GPIO_I.pin_alternate.set(GPIO, 1 << afn); + if (enable) { + GPIO_I.pin_alternate.set(GPIO, 1 << afn); + } else { + GPIO->pin_alternate &= ~(1 << afn); + }; } #define PFIC ch573_pfic__pfic @@ -83,7 +104,7 @@ static void enable_gpio_pfic(int irq) PFIC->interrupt_enable |= irq; } -void enable_gpio_interrupt(gpio_pin_t pin, interrupt_mode_t int_mode) +void gpio_enable_interrupt(gpio_pin_t pin, interrupt_mode_t int_mode) { int ipin = pin; struct ch573_gpio__gpio_port_t* port; @@ -150,7 +171,7 @@ void fire_irq(volatile uint16_t* int_flag_ptr, int is_port_b) for (cur = &GPIO_LISTENERS_START; cur < &GPIO_LISTENERS_END; ++cur) { (*cur)(pin | is_port_b); } - *int_flag_ptr |= 1 << pin; // Clear the interrupt for this pin. + *int_flag_ptr |= 1 << pin; // Clear the interrupt for this pin. } } -- cgit