aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2025-09-30 13:06:16 -0600
committerJosh Rahm <joshuarahm@gmail.com>2025-09-30 13:06:16 -0600
commit290d784950b6248782b049cd9831bd6e034fd538 (patch)
treec231c02e92ed83a02b234a85b3c07bcc5e44b168 /src/main.c
parentf89f75b5616de99865448f41b068a2783cd3648e (diff)
downloadch573-290d784950b6248782b049cd9831bd6e034fd538.tar.gz
ch573-290d784950b6248782b049cd9831bd6e034fd538.tar.bz2
ch573-290d784950b6248782b049cd9831bd6e034fd538.zip
Add a bunch. Neglected doing commits.HEADmain
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.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/src/main.c b/src/main.c
index 525db53..6a536d1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,21 +4,24 @@
#include "btsel.h"
#include "byte_math.h"
+#include "ch573/adc.h"
#include "ch573/gpio.h"
#include "ch573/pfic.h"
#include "ch573/pwr.h"
#include "ch573/uart.h"
#include "clock.h"
+#include "gpio.h"
#include "isr_vector.h"
#include "panic.h"
#include "pattern.h"
+#include "risc-v.h"
#include "spi.h"
#include "string.h"
#include "sysled.h"
#include "system.h"
#include "systick.h"
+#include "voltdisc.h"
#include "ws2812b.h"
-#include "gpio.h"
#ifndef LED_BYTE_ORDER
#define LED_BYTE_ORDER GRB
@@ -43,6 +46,9 @@
#define GPIO_I CH573_GPIO__GPIO_T_INTF
#define GPIO ch573_gpio__gpio
+#define ADC_I CH573_ADC__ADC_T_INTF
+#define ADC ch573_adc__adc
+
#define UART1 ch573_uart__uart1
#define UART CH573_UART__UART_T_INTF
@@ -126,11 +132,20 @@ int main(void)
enable_sysled();
enable_spi();
+ gpio_configure_pin(PIN_PB7, PIN_CFG_INPUT_PULL_UP);
+ gpio_enable_interrupt(PIN_PB7, INT_MODE_FALLING_EDGE);
+
+ const char* pattern_name;
+ if (gpio_read(PIN_PB7) == 0) {
+ // Holding pin PB7 during restart will set the default pattern to a
+ // different one.
+ pattern_name = "Warm Twinkle";
+ } else {
+ pattern_name = TOSTRING(DEFAULT_PATTERN);
+ }
for (current_pattern_idx = 0; current_pattern_idx < n_patterns;
++current_pattern_idx) {
- if (strcmp(
- patterns[current_pattern_idx].name, "" TOSTRING(DEFAULT_PATTERN)) ==
- 0) {
+ if (strcmp(patterns[current_pattern_idx].name, pattern_name) == 0) {
break;
}
}
@@ -144,20 +159,24 @@ int main(void)
printf("\nRunning Pattern '%s'\n", patterns[current_pattern_idx].name);
- configure_gpio(PIN_PB7, PIN_CFG_INPUT_PULL_UP);
- enable_gpio_interrupt(PIN_PB7, INT_MODE_FALLING_EDGE);
- // enable_bootsel_button();
-
size_t n = sizeof(buf);
struct ws2812b_buf ws_buf;
make_wsb2812b(&ws_buf, buf, n);
- ws_buf.byte_order = LED_BYTE_ORDER_ENUM;
-
- rgb_t color;
- GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 10);
+ millivolts_t voltage = get_input_voltage();
+ printf("Measured input voltage at %u mV\n", voltage);
+
+ if (voltage > 7000) {
+ // Assuming byte order RGB
+ ws_buf.byte_order = BYTE_ORDER_RGB;
+ printf("Using RGB byte order.\n");
+ } else {
+ // Assuming byte order GRB
+ ws_buf.byte_order = BYTE_ORDER_GRB;
+ printf("Using GRB byte order.\n");
+ }
- // GPIO->pb_interrupt_mode |= (1 << 7) | (1 << 8);
+ gpio_configure_pin(PIN_PA10, PIN_CFG_OUTPUT_5mA);
while (1) {
pattern_t* current_pattern = &patterns[current_pattern_idx];
@@ -168,12 +187,11 @@ int main(void)
}
while (spin_lock) {
- // set_sysled(1); // Setting the sysled helps me to measure down time.
- GPIO_PORT.out.set(GPIO_PORT_A, ON, 10);
+ gpio_set(PIN_PA10, ON);
+ wfi();
}
- GPIO_PORT.out.set(GPIO_PORT_A, OFF, 10);
-
spin_lock = 1;
+ gpio_set(PIN_PA10, OFF);
for (int j = 0; j < N_DMA_XFER; ++j) {
wait_for_dma();