aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-12-04 01:28:33 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-12-04 10:09:02 -0700
commit05db4276d42471d78f2a00a90c8567e3c1c2e002 (patch)
treeb9c7642e02913df67a83494d848236f1fe3eacb4 /src/main.c
parent03b84b89f6361df556749a4c73679a6cbcedd28a (diff)
downloadch573-main.tar.gz
ch573-main.tar.bz2
ch573-main.zip
Proper timing for leds using systick and interrupts.HEADmain
This means a new "frame" is written every 30th of a second.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c119
1 files changed, 19 insertions, 100 deletions
diff --git a/src/main.c b/src/main.c
index c80ab30..33728ea 100644
--- a/src/main.c
+++ b/src/main.c
@@ -60,92 +60,14 @@ uint32_t collatz(uint32_t n)
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");
- }
-}
-
-static void basic_delay()
-{
- set_sysled(1);
- for (int i = 0; i < 30000; ++i) {
- asm volatile("");
- }
- set_sysled(0);
-}
-
-static void fast_delay()
-{
- for (int i = 0; i < 1000; ++i) {
- asm volatile("");
+ panic(0xff);
}
}
@@ -185,12 +107,6 @@ rgb_t get_rgb(uint32_t time, size_t x)
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;
@@ -200,6 +116,15 @@ rgb_t get_rgb(uint32_t time, size_t x)
return color;
}
+
+static volatile uint32_t time = 0;
+static volatile int spin_lock = 0;
+
+On_SysTick() {
+ time ++;
+ spin_lock = 0;
+}
+
/*
* Main routine. This is called on_reset once everything else has been set up.
*/
@@ -211,12 +136,7 @@ int main(void)
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();
@@ -228,29 +148,28 @@ int main(void)
ws_buf.byte_order = BYTE_ORDER_GRB;
rgb_t color;
- uint32_t time = 0;
GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 7);
+
while (1) {
- // Fill the buffer with calculated rgb colors.
ws_buf.cur = 0;
for (int i = 0; i < N_LEDS; ++i) {
rgb_t rgb = 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) {
+ set_sysled(1); // Setting the sysled helps me to measure down time.
+ }
+ set_sysled(0);
+
+ spin_lock = 1;
+
+ 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");
-}