diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2024-11-17 23:04:11 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2024-11-17 23:04:11 -0700 |
commit | 22c5b3e1dc4e3cf7de3f73ebbf5b59542f207f4b (patch) | |
tree | 259efcee1a6b438988e0afa95f80821b37b16ae3 /src/main.c | |
parent | 7d64711cf7cbdf81d5a692044161ddc69e3dc33f (diff) | |
download | ch573-22c5b3e1dc4e3cf7de3f73ebbf5b59542f207f4b.tar.gz ch573-22c5b3e1dc4e3cf7de3f73ebbf5b59542f207f4b.tar.bz2 ch573-22c5b3e1dc4e3cf7de3f73ebbf5b59542f207f4b.zip |
System clock is sort of working.
It appears the frequency divider does not work. I've followed the data
sheet, but no matter what I set the frequency divider to it appears to
not work. It's possible maybe the GPIO is using an un-divided clock, but
I'm not sure.
Also the 32khz clock does not work I think. It might be an issue with
the board. The waveform is jagged and looks awful.
But I can switch from the HSE to the PLL.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 129 |
1 files changed, 127 insertions, 2 deletions
@@ -4,6 +4,8 @@ #include "ch573/gpio.h" #include "ch573/pwr.h" #include "ch573/uart.h" +#include "clock.h" +#include "system.h" #define GPIO_PORT_A ch573_gpio__gpio_port_a #define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF @@ -30,6 +32,93 @@ 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() +{ + for (int i = 0; i < 100000; ++i) { + asm volatile(""); + } +} + +static void fast_delay() +{ + for (int i = 0; i < 1000; ++i) { + asm volatile(""); + } +} + /* * Main routine. This is called on_reset once everything else has been set up. */ @@ -37,8 +126,44 @@ int main(void) { GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8); GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 8); + GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + + GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 11); + GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 11); + + GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 10); + GPIO_PORT.pd_drv.set(GPIO_PORT_A, PD_DRV_OPEN_DRAIN, 11); + + set_system_clock_6Mhz(); + uint32_t reg = (*(uint32_t*)0x40001008); + reg &= ~0x1f; + reg |= 10; + enter_safe_mode(); + (*(uint32_t*)0x40001008) = reg; + + + for (int i = 0;; ++i) { + GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); + GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + } + + // clock_cfg_t cfg; + // for (int i = 0;; ++i) { + // if (i % 16 == 0) { + // if (cur_clock) { + // set_system_clock_60Mhz(); + // } else { + // set_system_clock_6Mhz(); + // } + // get_system_clock(&cfg); + // printf("Cur Clock Mhz: %d\n", get_clock_freq(&cfg)); + // cur_clock = !cur_clock; + // } - printf("Hello, %s!\n", "World"); - for (;;); + // basic_delay(); + // GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); + // basic_delay(); + // GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + // } return 0; } |