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 /include/clock.h | |
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 'include/clock.h')
-rw-r--r-- | include/clock.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/include/clock.h b/include/clock.h new file mode 100644 index 0000000..42e2edd --- /dev/null +++ b/include/clock.h @@ -0,0 +1,45 @@ +#pragma once + +#include <stdint.h> +enum clock_selection { + CLOCK_SELECTION_PLL = 0, + CLOCK_SELECTION_LSE, + CLOCK_SELECTION_LSI, + CLOCK_SELECTION_HSE, +}; + +typedef struct { + enum clock_selection sel; + + union { + /* Divisor for the PLL. If set to 0, the default value of 5 is used. */ + uint8_t pll_clock_divisor; + + /* Divisor for the HSE. If set to 0, the default value of 5 is used. */ + uint8_t hse_clock_divisor; + }; +} clock_cfg_t; + +// Called when the system clock is changed. +typedef struct { + void (*on_clk_change)(const clock_cfg_t* cfg); +} clock_chg_evt_listener_t; + +/* Set the system clock configuration. */ +int set_system_clock(const clock_cfg_t* clk_cfg); + +/* Returns the clock configuration the system is currently in. */ +int get_system_clock(clock_cfg_t* out); + +/* Returns the frequency for the provided clock config. */ +uint32_t get_clock_freq(const clock_cfg_t* clk_cfg); + +void configure_lse(void); + +#define on_system_clock_changed(name) \ + void on_system_clock_changed__##name(const clock_cfg_t*); \ + __attribute__(( \ + __section__(".clock_change_listeners"))) clock_chg_evt_listener_t name = \ + ((clock_chg_evt_listener_t){.on_clk_change = \ + on_system_clock_changed__##name}); \ + void on_system_clock_changed__##name |