aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/exc.c47
-rw-r--r--src/gpio.c35
-rw-r--r--src/init.c16
-rw-r--r--src/ir.c9
-rw-r--r--src/main.c52
-rw-r--r--src/patterns/candycane.c53
-rw-r--r--src/patterns/twinkle.c148
-rw-r--r--src/spi.c61
-rw-r--r--src/voltdisc.c44
-rw-r--r--src/ws2812b.c52
10 files changed, 370 insertions, 147 deletions
diff --git a/src/exc.c b/src/exc.c
index 657ddb8..9b7242b 100644
--- a/src/exc.c
+++ b/src/exc.c
@@ -1,43 +1,50 @@
#include <stdio.h>
+#include "ch573/gpio.h"
#include "isr_vector.h"
#include "panic.h"
-
-#include "ch573/gpio.h"
+#include "risc-v.h"
#define GPIO_PORT_A ch573_gpio__gpio_port_a
#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF
void delay();
-IRQ(exc)
+struct exc_info {
+ uint32_t mtval;
+ uint32_t mepc;
+ uint32_t mcause;
+};
+
+static inline void get_exc_info(struct exc_info* out)
{
- uint32_t mcause, mepc, mtval, *sp;
+ out->mtval = MTVAL;
+ out->mepc = MEPC;
+ out->mcause = MCAUSE;
+}
- asm volatile("csrr %0, mcause" : "=r"(mcause));
- asm volatile("csrr %0, mepc" : "=r"(mepc));
- asm volatile("csrr %0, mtval" : "=r"(mtval));
+IRQ(exc)
+{
+ struct exc_info ei;
+ get_exc_info(&ei);
printf("Hardware Exception Caught:\n");
- printf(" mcause: 0x%08x\n", mcause);
- printf(" mepc: 0x%08x\n", mepc);
- printf(" mtval: 0x%08x\n", mtval);
+ printf(" mcause: 0x%08x\n", ei.mcause);
+ printf(" mepc: 0x%08x\n", ei.mepc);
+ printf(" mtval: 0x%08x\n", ei.mtval);
- panic(mcause);
+ panic(ei.mcause);
}
IRQ(nmi)
{
- uint32_t mcause, mepc, mtval, *sp;
-
- asm volatile("csrr %0, mcause" : "=r"(mcause));
- asm volatile("csrr %0, mepc" : "=r"(mepc));
- asm volatile("csrr %0, mtval" : "=r"(mtval));
+ struct exc_info ei;
+ get_exc_info(&ei);
printf("Unhandled NMI Caught:\n");
- printf(" mcause: 0x%08x\n", mcause);
- printf(" mepc: 0x%08x\n", mepc);
- printf(" mtval: 0x%08x\n", mtval);
+ printf(" mcause: 0x%08x\n", ei.mcause);
+ printf(" mepc: 0x%08x\n", ei.mepc);
+ printf(" mtval: 0x%08x\n", ei.mtval);
- panic(mcause);
+ panic(ei.mcause);
}
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.
}
}
diff --git a/src/init.c b/src/init.c
index 3aacc66..5f276cb 100644
--- a/src/init.c
+++ b/src/init.c
@@ -1,11 +1,11 @@
#include <stddef.h>
#include <stdint.h>
-#include <stdio.h>
#include "ch573/systick.h"
-#include "clock.h"
+#include "gpio.h"
#include "io.h"
#include "isr_vector.h"
+#include "risc-v.h"
void on_reset(void);
@@ -67,13 +67,6 @@ extern uint32_t DATA_SEGMENT_STOP;
extern uint32_t BSS_START;
extern uint32_t BSS_STOP;
-static inline void set_mtvec(void* vector_table)
-{
- uint32_t mtvec = (uint32_t)vector_table;
- mtvec |= 1; // Set interrupt table mode to "VECTORED"
- asm volatile("csrw mtvec, %0" : : "r"(mtvec));
-}
-
/*
* Initialize the data segment and the bss segment.
*
@@ -115,9 +108,12 @@ static __attribute((__section__(".sinit.1"))) void start(void)
/* Initialize the data segments. */
init_data_segments();
/* Set the mtvec to the isr_vector. */
- set_mtvec(&isr_vector);
+ set_mtvec(&isr_vector, MODE_VECTORED);
enable_interrupts();
+ /* Initialize GPIO pins so they don't create unnecessary electrical noise. */
+ gpio_init();
+
/* Initialize stdout. */
init_uart1_for_stdout();
diff --git a/src/ir.c b/src/ir.c
new file mode 100644
index 0000000..46ca2bd
--- /dev/null
+++ b/src/ir.c
@@ -0,0 +1,9 @@
+#include "ch573/gpio.h"
+
+#define GPIO_PORT_A ch573_gpio__gpio_port_a
+#define GPIO_PORT_B ch573_gpio__gpio_port_b
+#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF
+#define GPIO_I CH573_GPIO__GPIO_T_INTF
+#define GPIO ch573_gpio__gpio
+
+
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();
diff --git a/src/patterns/candycane.c b/src/patterns/candycane.c
index dbf80de..42a9ad7 100644
--- a/src/patterns/candycane.c
+++ b/src/patterns/candycane.c
@@ -1,23 +1,49 @@
#include "byte_math.h"
#include "pattern.h"
-static rgb_t get_rgb(uint32_t time, size_t x)
+static rgb_t white(uint32_t time, size_t x)
+{
+ int w = calc_w((time & 0xff) + x * 4);
+ rgb_t color;
+ color.color = 0x00ffffff;
+ color.a = 255 - w;
+ return color;
+}
+
+static rgb_t candycane_bg(uint32_t time, size_t x)
{
int r = 0, g = 0, b = 0;
uint8_t time8 = time & 0xff;
- int w = calc_w(time8 + x * 4);
r = 0xff;
g = byte_scale(byte_sin(time8 + x * 2), 0x90);
b = byte_scale(byte_sin(time8 + x * 2), 0x20);
- rgb_t color;
- color.color = 0;
- color.r = clip(r + w);
- color.g = clip(byte_scale(g, AS_BYTE(0.75)) + w);
- color.b = clip(byte_scale(b, AS_BYTE(0.75)) + w);
- return color;
+ rgb_t bg;
+ bg.color = 0;
+ bg.r = r;
+ bg.g = byte_scale(g, AS_BYTE(0.75));
+ bg.b = byte_scale(b, AS_BYTE(0.75));
+
+ return blend(white(time, x), bg);
+}
+
+static rgb_t candycane_twinkle(uint32_t time, size_t x)
+{
+ rgb_t twink = more_twinkle(time / 4, x);
+ int a = twink.a;
+
+ if (time & 0x3) {
+ // Linear interpolate.
+ int amt = time % 4;
+ a = (a * (4 - amt) + more_twinkle((time / 4) + 1, x).a * amt) / 4;
+ }
+
+ rgb_t bg = candycane_bg(time, x);
+
+ bg.a = a;
+ return bg;
}
static void reset(void)
@@ -25,4 +51,13 @@ static void reset(void)
}
__attribute__((__section__(".patterns"))) volatile static pattern_t pat =
- ((pattern_t){.get_rgb = get_rgb, .name = "CandyCane", .reset = reset});
+ ((pattern_t){.get_rgb = candycane_bg, .name = "CandyCane", .reset = reset});
+
+__attribute__((__section__(".patterns"))) volatile static pattern_t naked_pat =
+ ((pattern_t){.get_rgb = white, .name = "Naked White", .reset = reset});
+
+__attribute__((
+ __section__(".patterns"))) volatile static pattern_t candycane_twinkle_pat =
+ ((pattern_t){.get_rgb = candycane_twinkle,
+ .name = "Candycane Twinkle",
+ .reset = reset});
diff --git a/src/patterns/twinkle.c b/src/patterns/twinkle.c
index 564a30a..0c3f985 100644
--- a/src/patterns/twinkle.c
+++ b/src/patterns/twinkle.c
@@ -1,54 +1,144 @@
#include <stdio.h>
+
#include "byte_math.h"
#include "pattern.h"
-static uint8_t pseudo_random(uint8_t x) {
- return (1103515245 * x + 12345) & 0xFF; // & 0xFF ensures the result is 0-255
+static uint8_t pseudo_random(uint8_t x)
+{
+ return (1103515245 * x + 12345) & 0xFF; // & 0xFF ensures the result is 0-255
+}
+
+rgb_t twinkle(uint32_t time, size_t x)
+{
+ uint8_t time_offset = pseudo_random(x);
+
+ uint32_t adj_time = time - time_offset;
+ uint32_t time8 = adj_time & 0xff;
+ uint32_t speed = pseudo_random((adj_time >> 8) + x) & 0x3;
+
+ time8 *= speed;
+ uint8_t w = 0;
+ if (time8 <= 255) {
+ w = calc_w(time8);
+ } else {
+ w = 0;
+ }
+
+ rgb_t r;
+ r.r = 0xff;
+ r.g = 0xff;
+ r.b = 0xff;
+ r.a = 0xff - w;
+ return r;
}
+rgb_t more_twinkle(uint32_t time, size_t x)
+{
+ uint8_t time_offset = pseudo_random(x);
+
+#define N 91
+
+ uint32_t adj_time = time - time_offset;
+ uint32_t time8 = adj_time % N;
+ uint32_t speed = pseudo_random((adj_time / N) + x) % 3;
+
+ time8 *= speed;
+ time8 += (255 - N);
+ uint8_t w = 0;
+ if (time8 <= 255) {
+ w = calc_w(time8);
+ } else {
+ w = 0;
+ }
+
+ rgb_t r;
+ r.r = 0xff;
+ r.g = 0xff;
+ r.b = 0xff;
+ r.a = 0xff - w;
+ return r;
+}
static rgb_t get_rgb(uint32_t time, size_t x)
{
- int r = 0, g = 0, b = 0;
+ uint8_t time8 = time & 0xff;
+
+ rgb_t twink = twinkle(time, x);
+ rgb_t bg;
+ bg.color = 0;
+ bg.r = 0xff;
+ bg.g = byte_scale(byte_sin(time8 + x * 2), 0x60);
+ bg.b = 0;
+ bg.a = 0xa0;
+
+ return blend(twink, bg);
+}
+
+static rgb_t blue_twinkle(uint32_t time, size_t x)
+{
+ rgb_t bg;
uint8_t time8 = time & 0xff;
- uint8_t time_offset = pseudo_random(x);
- uint32_t adjusted_time = time - time_offset;
+ bg.color = 0;
+ bg.r = byte_sin((time8 * 2 + x * 2 + 0x80) * 2);
+ bg.g = byte_sin(time8 + x * 2);
+ bg.b = 0xff;
+ bg.a = 0xc0;
- // The random number for this "metaframe." The metaframe is enough to show 1
- // flash. It's used to keep the same LED's from consistently repeating.
- uint32_t metaframe = adjusted_time >> 8;
- uint8_t rand = pseudo_random(x + metaframe);
- uint8_t speed = pseudo_random(rand) % 16;
- uint8_t off = pseudo_random(rand + speed);
+ return blend(twinkle(time, x), bg);
+}
- if (speed > 4) {
- speed = 0;
- }
+static rgb_t warm_twinkle(uint32_t time, size_t x)
+{
+ uint8_t time8 = time & 0xff;
- uint32_t w_index = ((adjusted_time - off) * speed) & 0xff;
- int w = 0;
- if (w_index < 255) {
- w = calc_w(w_index);
- }
+ rgb_t twink = twinkle(time, x);
+ rgb_t bg;
+ bg.color = 0;
+ bg.r = 0xff;
+ bg.g = byte_scale(byte_sin(time8 + x * 2), 0x60);
+ bg.b = 0;
+ bg.a = twink.a;
- r = 0xff;
- g = byte_scale(byte_sin(time8 + x * 2), 0x60);
- b = 0;
+ return bg;
+}
+
+static rgb_t cool_twinkle(uint32_t time, size_t x)
+{
+ rgb_t bg;
+ uint8_t time8 = time & 0xff;
+ rgb_t twink = twinkle(time, x);
+
+ bg.color = 0;
+ bg.r = byte_sin((time8 * 2 + x * 2 + 0x80) * 2);
+ bg.g = byte_sin(time8 + x * 2);
+ bg.b = 0xff;
+ bg.a = twink.a;
- rgb_t color;
- color.color = 0;
- color.r = clip(r + w);
- color.g = clip(byte_scale(g, AS_BYTE(0.75)) + w);
- color.b = clip(byte_scale(b, AS_BYTE(0.75)) + w);
- return color;
+ return bg;
}
static void reset(void)
{
}
-__attribute__((__section__(".patterns"))) volatile static pattern_t pat =
+__attribute__((__section__(".patterns"))) volatile static pattern_t p08_pat =
((pattern_t){.get_rgb = get_rgb, .name = "Twinkle", .reset = reset});
+
+__attribute__((
+ __section__(".patterns"))) volatile static pattern_t p09_naked_pat =
+ ((pattern_t){.get_rgb = twinkle, .name = "Naked Twinkle", .reset = reset});
+
+__attribute__((__section__(
+ ".patterns"))) volatile static pattern_t p10_naked_pat = ((pattern_t){
+ .get_rgb = warm_twinkle, .name = "Warm Twinkle", .reset = reset});
+
+__attribute__((__section__(".patterns"))) volatile static pattern_t p11_cool_pat =
+ ((pattern_t){
+ .get_rgb = cool_twinkle, .name = "Cool Twinkle", .reset = reset});
+
+__attribute__((__section__(".patterns"))) volatile static pattern_t blue_pat =
+ ((pattern_t){
+ .get_rgb = blue_twinkle, .name = "Blue Twinkle", .reset = reset});
diff --git a/src/spi.c b/src/spi.c
index a416014..d19b2bb 100644
--- a/src/spi.c
+++ b/src/spi.c
@@ -4,6 +4,7 @@
#include "ch573/gpio.h"
#include "ch573/spi.h"
+#include "gpio.h"
#include "sysled.h"
#define MAX_SPI_FIFO 8
@@ -17,57 +18,29 @@
void enable_spi(void)
{
- GPIO_I.pin_alternate.pin_spi0.set(GPIO, ON);
+ gpio_enable_alternate_function(AF_SPI, 1);
+ gpio_configure_pin(PIN_PB14, PIN_CFG_OUTPUT_20mA);
+ gpio_configure_pin(PIN_PB12, PIN_CFG_OUTPUT_20mA);
- GPIO_PORT.out.set(GPIO_PORT_A, ON, 12);
- GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 12);
- GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 14);
- GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 12);
- GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 14);
+ gpio_configure_pin(PIN_PB15, PIN_CFG_INPUT_PULL_DOWN);
- GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 12);
- GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 14);
- GPIO_PORT.pd_drv.set(GPIO_PORT_B, 0, 12);
- GPIO_PORT.pd_drv.set(GPIO_PORT_B, 0, 14);
- GPIO_PORT.out.set(GPIO_PORT_B, OFF, 14);
- GPIO_PORT.out.set(GPIO_PORT_B, ON, 12);
+ gpio_configure_pin(PIN_PA14, PIN_CFG_OUTPUT_20mA);
+ gpio_configure_pin(PIN_PA12, PIN_CFG_OUTPUT_20mA);
- GPIO_PORT.pu.set(GPIO_PORT_B, ENABLED, 14);
-
- GPIO_PORT.dir.set(GPIO_PORT_B, DIR_IN, 13);
- GPIO_PORT.dir.set(GPIO_PORT_B, DIR_IN, 15);
- GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 13);
- GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 15);
+ gpio_configure_pin(PIN_PA15, PIN_CFG_INPUT_PULL_DOWN);
SPI.clock_div.set(SPI0, 25);
SPI.ctrl_mod.all_clear.set(SPI0, 1);
- // SPI.ctrl_mod.set(SPI0, 0xe0); // Set mosi and sck
SPI.ctrl_mod.all_clear.set(SPI0, 0);
- SPI.ctrl_mod.pin_enable.set(SPI0, 0x2); // Set mosi and sck
+ SPI.ctrl_mod.pin_enable.set(SPI0, 0x7); // Set mosi and sck
SPI.ctrl_cfg.auto_if.set(SPI0, 1);
SPI.ctrl_cfg.dma_enable.set(SPI0, OFF);
+ SPI.ctrl_mod.mst_sck_mod.set(SPI0, 0);
}
-// void write_color(uint8_t r, uint8_t g, uint8_t b)
-// {
-//
-//
-// while (!SPI.int_flag.if_fifo_hf.get(SPI0));
-// SPI.fifo.set(SPI0, r);
-// SPI.fifo.set(SPI0, g);
-// SPI.fifo.set(SPI0, b);
-// }
-
void dma_transfer(void* output_buffer, size_t len)
{
- // Clear everything.
- // SPI.ctrl_mod.all_clear.set(SPI0, 1);
- // SPI.ctrl_mod.all_clear.set(SPI0, 0);
- // SPI.ctrl_cfg.dma_enable.set(SPI0, OFF);
-
- // printf("Start DMA: %p, %zu\n", output_buffer, len);
-
SPI.ctrl_mod.fifo_dir.set(SPI0, FIFO_DIR_OUTPUT);
SPI.dma_beg.set(SPI0, (uint32_t)output_buffer);
SPI.dma_end.set(SPI0, (uint32_t)(output_buffer + len));
@@ -85,17 +58,3 @@ void wait_for_dma()
}
set_sysled(0);
}
-
-void run_spi(void)
-{
- GPIO_PORT.out.set(GPIO_PORT_A, ON, 12);
- while (1) {
- SPI.total_count.set(SPI0, 1024);
- // GPIO_PORT_A->clr |= 1 << 12;
-
- while (SPI.fifo_count.get(SPI0) == 8);
-
- SPI.fifo.set(SPI0, 0xaa);
- SPI.total_count.set(SPI0, 1024);
- }
-}
diff --git a/src/voltdisc.c b/src/voltdisc.c
new file mode 100644
index 0000000..034fdf8
--- /dev/null
+++ b/src/voltdisc.c
@@ -0,0 +1,44 @@
+#include "voltdisc.h"
+
+#include "ch573/adc.h"
+#include "gpio.h"
+#include "risc-v.h"
+
+#define ADC_I CH573_ADC__ADC_T_INTF
+#define ADC ch573_adc__adc
+
+#define VOLTAGE_DIVIDER_COEF (11)
+
+millivolts_t get_input_voltage()
+{
+ // Pull down the voltage of the pin. Do this in case the MCU is in a chassis
+ // which does not have the voltage divider.
+ gpio_configure_pin(PIN_PA4, PIN_CFG_INPUT_PULL_DOWN);
+
+ // Wait for the pin to discharge.
+ for (int i = 0; i < 10; ++i) {
+ __nop();
+ }
+
+ // Configure the pin to "floating".
+ gpio_configure_pin(PIN_PA4, PIN_CFG_INPUT_FLOATING);
+
+ // Configure the ADC for reading.
+ ADC_I.cfg.adc_buf_en.set(ADC, ENABLED);
+ ADC_I.cfg.adc_power_on.set(ADC, ENABLED);
+ ADC_I.channel_select.set(ADC, 0);
+ ADC_I.dma_interrupt_control.auto_en.set(ADC, 1);
+ ADC_I.convert.adc_start.set(ADC, ON);
+
+ uint32_t raw_value;
+ // Wait until the ADC is ready for reading.
+ while (!ADC_I.interrupt_flag.if_eoc.get(ADC));
+ raw_value = ADC_I.data.get(ADC);
+
+ ADC_I.convert.adc_start.set(ADC, OFF);
+ ADC_I.cfg.adc_power_on.set(ADC, DISABLED);
+
+ // Attempt to calculate the original voltage
+ return (millivolts_t)(raw_value * 1.05 / 2048.0 * 1000) *
+ VOLTAGE_DIVIDER_COEF;
+}
diff --git a/src/ws2812b.c b/src/ws2812b.c
index 1dea9f1..692e8ad 100644
--- a/src/ws2812b.c
+++ b/src/ws2812b.c
@@ -2,6 +2,7 @@
#include <string.h>
+#include "byte_math.h"
#include "spi.h"
#include "sysled.h"
@@ -38,9 +39,9 @@ inline static void complie_color_inline(
color.g = tmp;
}
- color.r = color.r;
- color.g = color.g;
- color.b = color.b;
+ color.r = byte_scale(color.r, 255 - color.a);
+ color.g = byte_scale(color.g, 255 - color.a);
+ color.b = byte_scale(color.b, 255 - color.a);
out->first_bits =
BYTE_1(COLOR_BYTE_1(color.r)) | BYTE_2(COLOR_BYTE_2(color.r)) |
@@ -79,7 +80,8 @@ int write_rgb(struct ws2812b_buf* out, rgb_t color)
while ((dma_loc - CLIP_TO_DMA_BITS(out->buf + out->cur) <
TOTAL_BYTES_PER_LED) &&
(dma_loc < dma_end)) {
- set_sysled(1); // Little indication to tell if we are waiting for the DMA.
+ set_sysled(
+ 1); // Little indication to tell if we are waiting for the DMA.
dma_loc = *dma_now;
}
set_sysled(0);
@@ -120,3 +122,45 @@ void compiled_color_dbg_str(struct rgb_compiled* c, char* out)
}
*(out++) = 0;
}
+
+rgb_t blend(rgb_t c1, rgb_t c2)
+{
+ rgb_t r;
+
+ uint8_t prop = 255 - c1.a;
+ uint8_t rprop = 255 - prop;
+
+ r.r = clip((int)byte_scale(c1.r, prop) + byte_scale(c2.r, rprop));
+ r.g = clip((int)byte_scale(c1.g, prop) + byte_scale(c2.g, rprop));
+ r.b = clip((int)byte_scale(c1.b, prop) + byte_scale(c2.b, rprop));
+ r.a = 255 - (prop + byte_scale(255 - c2.a, rprop));
+
+ return r;
+}
+
+rgb_t mat_mul(rgb_t rgb, const rgb_mat_t* mat)
+{
+ rgb_t ret;
+
+ ret.r = byte_scale(rgb.r, mat->mat[0][0]) +
+ byte_scale(rgb.g, mat->mat[0][1]) +
+ byte_scale(rgb.b, mat->mat[0][2]) +
+ byte_scale(rgb.a, mat->mat[0][3]);
+
+ ret.g = byte_scale(rgb.r, mat->mat[1][0]) +
+ byte_scale(rgb.g, mat->mat[1][1]) +
+ byte_scale(rgb.b, mat->mat[1][2]) +
+ byte_scale(rgb.a, mat->mat[1][3]);
+
+ ret.b = byte_scale(rgb.r, mat->mat[2][0]) +
+ byte_scale(rgb.g, mat->mat[2][1]) +
+ byte_scale(rgb.b, mat->mat[2][2]) +
+ byte_scale(rgb.a, mat->mat[2][3]);
+
+ ret.a = byte_scale(rgb.r, mat->mat[3][0]) +
+ byte_scale(rgb.g, mat->mat[3][1]) +
+ byte_scale(rgb.b, mat->mat[3][2]) +
+ byte_scale(rgb.a, mat->mat[3][3]);
+
+ return ret;
+}