aboutsummaryrefslogtreecommitdiff
path: root/src/gpio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpio.c')
-rw-r--r--src/gpio.c35
1 files changed, 28 insertions, 7 deletions
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.
}
}