diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/btsel.c | 28 | ||||
| -rw-r--r-- | src/exc.c | 47 | ||||
| -rw-r--r-- | src/gpio.c | 189 | ||||
| -rw-r--r-- | src/init.c | 16 | ||||
| -rw-r--r-- | src/ir.c | 9 | ||||
| -rw-r--r-- | src/main.c | 261 | ||||
| -rw-r--r-- | src/patterns/candycane.c | 63 | ||||
| -rw-r--r-- | src/patterns/hearth.c | 28 | ||||
| -rw-r--r-- | src/patterns/twinkle.c | 144 | ||||
| -rw-r--r-- | src/spi.c | 61 | ||||
| -rw-r--r-- | src/systick.c | 4 | ||||
| -rw-r--r-- | src/voltdisc.c | 44 | ||||
| -rw-r--r-- | src/ws2812b.c | 52 |
13 files changed, 704 insertions, 242 deletions
diff --git a/src/btsel.c b/src/btsel.c new file mode 100644 index 0000000..c9c7e06 --- /dev/null +++ b/src/btsel.c @@ -0,0 +1,28 @@ +#include "btsel.h" + +#include "ch573/gpio.h" +#include "isr_vector.h" + +#define GPIO ch573_gpio__gpio +#define GPIO_I CH573_GPIO__GPIO_T_INTF +#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF +#define GPIO_PORT_A ch573_gpio__gpio_port_a +#define GPIO_PORT_B ch573_gpio__gpio_port_b + +extern bootsel_cb_t BOOTSEL_LISTENERS_START; +extern bootsel_cb_t BOOTSEL_LISTENERS_END; + +#define BOOTSEL_PIN 7 + +void enable_bootsel_button(void) +{ + GPIO_PORT.dir.set(GPIO_PORT_B, DIR_IN, BOOTSEL_PIN); + GPIO_PORT.pu.set(GPIO_PORT_B, ENABLED, BOOTSEL_PIN); + // GPIO_PORT.pd_drv.set(GPIO_PORT_B, PD_DRV_OPEN_DRAIN, BOOTSEL_PIN); + GPIO_PORT.out.set(GPIO_PORT_B, OFF, BOOTSEL_PIN); + GPIO_PORT.clr.set(GPIO_PORT_B, 1 << BOOTSEL_PIN); + + GPIO_I.pb_interrupt_mode.set(GPIO, 1 << BOOTSEL_PIN); + GPIO_I.pb_interrupt_flag.set(GPIO, 1 << BOOTSEL_PIN); + GPIO_I.pb_interrupt_enable.set(GPIO, 1 << BOOTSEL_PIN); +} @@ -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 new file mode 100644 index 0000000..b093b94 --- /dev/null +++ b/src/gpio.c @@ -0,0 +1,189 @@ +#include "gpio.h" + +#include "ch573/pfic.h" +#include "isr_vector.h" + +/* 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; + if (pin & IS_PORT_B) { + port = GPIO_PORT_B; + ipin &= ~IS_PORT_B; + } else { + port = GPIO_PORT_A; + } + + switch (cfg) { + case PIN_CFG_INPUT_FLOATING: + GPIO_PORT.dir.set(port, DIR_IN, ipin); + GPIO_PORT.pu.set(port, DISABLED, ipin); + GPIO_PORT.pd_drv.set(port, 0, ipin); + break; + case PIN_CFG_INPUT_PULL_UP: + GPIO_PORT.dir.set(port, DIR_IN, ipin); + GPIO_PORT.pu.set(port, ENABLED, ipin); + GPIO_PORT.pd_drv.set(port, 0, ipin); + break; + case PIN_CFG_INPUT_PULL_DOWN: + GPIO_PORT.dir.set(port, DIR_IN, ipin); + GPIO_PORT.pu.set(port, DISABLED, ipin); + GPIO_PORT.pd_drv.set(port, 1, ipin); + break; + case PIN_CFG_OUTPUT_5mA: + GPIO_PORT.dir.set(port, DIR_OUT, ipin); + GPIO_PORT.pu.set(port, DISABLED, ipin); + GPIO_PORT.pd_drv.set(port, 0, ipin); + break; + case PIN_CFG_OUTPUT_20mA: + GPIO_PORT.dir.set(port, DIR_OUT, ipin); + GPIO_PORT.pu.set(port, DISABLED, ipin); + GPIO_PORT.pd_drv.set(port, 1, ipin); + break; + } +} + +int gpio_read(gpio_pin_t pin) +{ + int ipin = pin; + struct ch573_gpio__gpio_port_t* port; + if (pin & IS_PORT_B) { + port = GPIO_PORT_B; + ipin &= ~IS_PORT_B; + } else { + port = GPIO_PORT_A; + } + + return !!GPIO_PORT.pin.in.get(port, ipin); +} + +void gpio_set(gpio_pin_t pin, int high) +{ + int ipin = pin; + struct ch573_gpio__gpio_port_t* port; + if (pin & IS_PORT_B) { + port = GPIO_PORT_B; + ipin &= ~IS_PORT_B; + } else { + port = GPIO_PORT_A; + } + + GPIO_PORT.out.set(port, !!high, ipin); +} + +void gpio_enable_alternate_function(alternate_function_t afn, int enable) +{ + if (enable) { + GPIO_I.pin_alternate.set(GPIO, 1 << afn); + } else { + GPIO->pin_alternate &= ~(1 << afn); + }; +} + +#define PFIC ch573_pfic__pfic +static void enable_gpio_pfic(int irq) +{ + PFIC->interrupt_priority_threshold = 0x10; + PFIC->interrupt_enable |= irq; +} + +void gpio_enable_interrupt(gpio_pin_t pin, interrupt_mode_t int_mode) +{ + int ipin = pin; + struct ch573_gpio__gpio_port_t* port; + + volatile uint16_t* port_int_mode; + volatile uint16_t* port_int_enable; + volatile uint16_t* port_int_flag; + + if (pin & IS_PORT_B) { + port = GPIO_PORT_B; + ipin &= ~IS_PORT_B; + port_int_mode = &GPIO->pb_interrupt_mode; + port_int_flag = &GPIO->pb_interrupt_flag; + port_int_enable = &GPIO->pb_interrupt_enable; + + enable_gpio_pfic(IRQ_GpioB); + } else { + port = GPIO_PORT_A; + port_int_mode = &GPIO->pa_interrupt_mode; + port_int_flag = &GPIO->pa_interrupt_flag; + port_int_enable = &GPIO->pa_interrupt_enable; + + enable_gpio_pfic(IRQ_GpioA); + } + + switch (int_mode) { + case INT_MODE_FALLING_EDGE: + *port_int_mode |= 1 << ipin; + GPIO_PORT.clr.set(port, 1 << ipin); + break; + + case INT_MODE_RISING_EDGE: + *port_int_mode |= 1 << ipin; + GPIO_PORT.out.set(port, 1, ipin); + break; + + case INT_MODE_LEVEL_HIGH: + *port_int_mode &= ~(1 << ipin); + GPIO_PORT.out.set(port, 1, ipin); + break; + + case INT_MODE_LEVEL_LOW: + *port_int_mode &= ~(1 << ipin); + GPIO_PORT.clr.set(port, 1 << ipin); + break; + } + + *port_int_flag = 1 << ipin; + *port_int_enable |= 1 << ipin; +} + +extern gpio_callback_t GPIO_LISTENERS_START; +extern gpio_callback_t GPIO_LISTENERS_END; +void fire_irq(volatile uint16_t* int_flag_ptr, int is_port_b) +{ + gpio_callback_t* cur = &GPIO_LISTENERS_START; + + uint16_t pin = 0; + uint16_t intflag = *int_flag_ptr; + + for (pin = 0; pin < 16; ++pin) { + if (intflag & (1 << pin)) { + // If this pin triggered an interrupt, fire the GPIO listeners. + 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 = 0xffff; +} + +IRQ(gpio_b) +{ + fire_irq(&GPIO->pb_interrupt_flag, IS_PORT_B); +} + +IRQ(gpio_a) +{ + fire_irq(&GPIO->pa_interrupt_flag, 0); +} @@ -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 + + @@ -2,26 +2,52 @@ #include <stdint.h> #include <stdio.h> +#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" +#ifndef LED_BYTE_ORDER +#define LED_BYTE_ORDER GRB +#endif + +#define PASTER(x, y) x##y +#define CONCAT(x, y) PASTER(x, y) +#define LED_BYTE_ORDER_ENUM CONCAT(BYTE_ORDER_, LED_BYTE_ORDER) + +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) +#ifndef DEFAULT_PATTERN +#define DEFAULT_PATTERN CandyCane +#endif + #define PFIC_I CH573_PFIC__PFIC_T_INTF #define PFIC ch573_pfic__pfic #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 + +#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 @@ -29,8 +55,6 @@ #define PWR1 ch573_pwr__pwr_mgmt #define PWR CH573_PWR__PWR_MGMT_T_INTF -#define AS_BYTE(n) ((n) * 256) - #define min(a, b) (a) < (b) ? (a) : (b) uint8_t amp(uint8_t in, uint8_t n) @@ -44,160 +68,53 @@ uint8_t amp(uint8_t in, uint8_t n) return min(out, 255); } -uint32_t collatz(uint32_t n) -{ - uint32_t c = 0; - - while (n > 1) { - if (n % 2 == 0) { - n /= 2; - } else { - n = n * 3 + 1; - } - c++; - } - - return c; -} - -static void print_reg(uint32_t reg) -{ - // 0x40001008 - printf("register @0x%08x = 0x%08x\n", reg, *(uint32_t*)reg); -} - -static void set_system_clock_6Mhz(void) -{ - print_reg(0x40001008); - printf("Setting system clock to 6Mhz...\n"); - clock_cfg_t clk_cfg = {0}; - clk_cfg.sel = CLOCK_SELECTION_HSE; - clk_cfg.pll_clock_divisor = 5; - if (set_system_clock(&clk_cfg)) { - printf("Failed to set system clock.\n"); - } - print_reg(0x40001008); - printf("Done\n"); -} - -static void set_system_clock_3Mhz(void) -{ - print_reg(0x40001008); - printf("Setting system clock to 3Mhz...\n"); - clock_cfg_t clk_cfg = {0}; - clk_cfg.sel = CLOCK_SELECTION_HSE; - clk_cfg.pll_clock_divisor = 10; - if (set_system_clock(&clk_cfg)) { - printf("Failed to set system clock.\n"); - } - print_reg(0x40001008); - printf("Done\n"); -} - static void set_system_clock_60Mhz(void) { - print_reg(0x40001008); - printf("Setting system clock to 60Mhz...\n"); clock_cfg_t clk_cfg = {0}; clk_cfg.sel = CLOCK_SELECTION_PLL; clk_cfg.pll_clock_divisor = 8; if (set_system_clock(&clk_cfg)) { printf("Failed to set system clock.\n"); - } - print_reg(0x40001008); - printf("Done\n"); -} - -static void set_system_clock_30Mhz(void) -{ - print_reg(0x40001008); - printf("Setting system clock to 30Mhz...\n"); - clock_cfg_t clk_cfg = {0}; - clk_cfg.sel = CLOCK_SELECTION_PLL; - clk_cfg.pll_clock_divisor = 16; - if (set_system_clock(&clk_cfg)) { - printf("Failed to set system clock.\n"); - } - print_reg(0x40001008); - printf("Done\n"); -} - -static void set_system_clock_32kHz() -{ - print_reg(0x40001008); - printf("Setting system clock to 32kHz...\n"); - clock_cfg_t clk_cfg = {0}; - clk_cfg.sel = CLOCK_SELECTION_LSE; - if (set_system_clock(&clk_cfg)) { - printf("Failed to set system clock.\n"); + panic(0xff); } } -static void basic_delay() -{ - set_sysled(1); - for (int i = 0; i < 30000; ++i) { - asm volatile(""); - } - set_sysled(0); -} +#define N_LEDS 255 +#define N_DMA_XFER 4 -static void fast_delay() -{ - for (int i = 0; i < 1000; ++i) { - asm volatile(""); - } -} +extern int uart1_FILE_get(FILE* f); -#define N_LEDS 150 -#define N_DMA_XFER 2 +#define TIME_STEP 1 -extern int uart1_FILE_get(FILE* f); +static volatile uint32_t time = 0; +static volatile int spin_lock = 0; -static inline uint8_t byte_scale(uint8_t v, uint8_t scale) -{ - uint16_t acc = v; - return (acc * scale) >> 8; -} +static size_t current_pattern_idx = 0; -uint8_t clip(int x) +static size_t debounce = 0; +On_SysTick() { - if (x > 240) { - return 240; - } - - if (x < 0) { - return 0; + if (debounce > 0) { + debounce--; } - - return (uint8_t)x; + time++; + spin_lock = 0; } -#define TIME_STEP 1 - -rgb_t get_rgb(uint32_t time, size_t x) +On_Gpio(gpio_pin_t pin) { - int r = 0, g = 0, b = 0; - - uint8_t time8 = time & 0xff; - int w = calc_w(time8 * TIME_STEP + x * 4); - - r = 0xff; - g = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), 0x90); - b = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), 0x20); - // r = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), AS_BYTE(0.25)) + - // AS_BYTE(0.75); - // b = 0x40; - // g = byte_scale( - // 0xff - byte_sin(time8 * TIME_STEP + x * 7 + 0x80), AS_BYTE(0.5)) + - // AS_BYTE(0.5); - - 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; + if (pin == PIN_PB7) { + if (debounce > 0) return; + debounce = 10; + + size_t tmp = current_pattern_idx; + tmp++; + if (tmp >= n_patterns) { + tmp = 0; + } + current_pattern_idx = tmp; + printf("Switch to '%s'\n", patterns[current_pattern_idx].name); + } } /* @@ -208,49 +125,79 @@ int main(void) char buf[N_LEDS * TOTAL_BYTES_PER_LED]; PFIC_I.vector_table_control.set(PFIC, 1); - PFIC->interrupt_priority_threshold = 0x10; - PFIC->interrupt_enable |= IRQ_SysTick; - - printf( - "PFIC enable status 1: %08x\n", - (uint32_t)(PFIC->interrupt_enable_status)); set_system_clock_60Mhz(); - set_systick(250000); 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, pattern_name) == 0) { + break; + } + } + + if (current_pattern_idx == n_patterns) { + printf( + "Could not find a pattern with the name '%s'.\n", + TOSTRING(DEFAULT_PATTERN)); + panic(0xaa); + } + + printf("\nRunning Pattern '%s'\n", patterns[current_pattern_idx].name); + size_t n = sizeof(buf); struct ws2812b_buf ws_buf; make_wsb2812b(&ws_buf, buf, n); - ws_buf.byte_order = BYTE_ORDER_GRB; - rgb_t color; - uint32_t time = 0; + 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_configure_pin(PIN_PA10, PIN_CFG_OUTPUT_5mA); - GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 7); while (1) { - // Fill the buffer with calculated rgb colors. + pattern_t* current_pattern = &patterns[current_pattern_idx]; ws_buf.cur = 0; for (int i = 0; i < N_LEDS; ++i) { - rgb_t rgb = get_rgb(time, i); + rgb_t rgb = current_pattern->get_rgb(time, i); write_rgb(&ws_buf, rgb); } - time++; - GPIO_PORT.out.set(GPIO_PORT_B, ON, 7); - for (int i = 0; i < N_DMA_XFER; ++i) { + while (spin_lock) { + gpio_set(PIN_PA10, ON); + wfi(); + } + spin_lock = 1; + gpio_set(PIN_PA10, OFF); + + for (int j = 0; j < N_DMA_XFER; ++j) { wait_for_dma(); start_dma(&ws_buf); } - basic_delay(); } return 0; } - -On_SysTick() { - printf("Systick BOI!\n"); -} diff --git a/src/patterns/candycane.c b/src/patterns/candycane.c new file mode 100644 index 0000000..42a9ad7 --- /dev/null +++ b/src/patterns/candycane.c @@ -0,0 +1,63 @@ +#include "byte_math.h" +#include "pattern.h" + +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; + + r = 0xff; + g = byte_scale(byte_sin(time8 + x * 2), 0x90); + b = byte_scale(byte_sin(time8 + x * 2), 0x20); + + 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) +{ +} + +__attribute__((__section__(".patterns"))) volatile static pattern_t pat = + ((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/hearth.c b/src/patterns/hearth.c new file mode 100644 index 0000000..373562a --- /dev/null +++ b/src/patterns/hearth.c @@ -0,0 +1,28 @@ +#include "byte_math.h" +#include "pattern.h" + +static rgb_t get_rgb(uint32_t time, size_t x) +{ + int r = 0, g = 0, b = 0; + + uint8_t time8 = time & 0xff; + int w = 0 ; // calc_w(time8 + x * 10); + + r = 0xff; + g = byte_scale(byte_sin(time8 + x * 2), 0x60); + b = 0; + + 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; +} + +static void reset(void) +{ +} + +__attribute__((__section__(".patterns"))) volatile static pattern_t pat = + ((pattern_t){.get_rgb = get_rgb, .name = "Hearth", .reset = reset}); diff --git a/src/patterns/twinkle.c b/src/patterns/twinkle.c new file mode 100644 index 0000000..0c3f985 --- /dev/null +++ b/src/patterns/twinkle.c @@ -0,0 +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 +} + +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) +{ + 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; + + 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; + + return blend(twinkle(time, x), bg); +} + +static rgb_t warm_twinkle(uint32_t time, size_t x) +{ + 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 = twink.a; + + 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; + + return bg; +} + +static void reset(void) +{ +} + +__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}); @@ -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/systick.c b/src/systick.c index 3cd7bfa..4b57702 100644 --- a/src/systick.c +++ b/src/systick.c @@ -3,10 +3,12 @@ #include <stdio.h> #include "ch573/systick.h" +#include "ch573/pfic.h" #include "isr_vector.h" #define SYSTICK_I CH573_SYSTICK__SYSTICK_T_INTF #define SYSTICK ch573_systick__systick +#define PFIC ch573_pfic__pfic void set_systick(uint64_t systick) { @@ -15,6 +17,8 @@ void set_systick(uint64_t systick) SYSTICK_I.cfg.interrupt_enable.set(SYSTICK, 1); SYSTICK_I.cfg.enabled.set(SYSTICK, ENABLED); + PFIC->interrupt_priority_threshold = 0x10; + PFIC->interrupt_enable |= IRQ_SysTick; } uint64_t get_systick() 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; +} |