From 22c5b3e1dc4e3cf7de3f73ebbf5b59542f207f4b Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sun, 17 Nov 2024 23:04:11 -0700 Subject: 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. --- include/clock.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/system.h | 14 ++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 include/clock.h create mode 100644 include/system.h (limited to 'include') 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 +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 diff --git a/include/system.h b/include/system.h new file mode 100644 index 0000000..ca29ade --- /dev/null +++ b/include/system.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#define SAFE_ACCESS_SIG_REG (*((volatile uint32_t*)0x40001040)) + +#define SAFE_ACCESS_1 0x57 +#define SAFE_ACCESS_2 0xA8 + +inline static void enter_safe_mode() +{ + SAFE_ACCESS_SIG_REG = SAFE_ACCESS_1; + SAFE_ACCESS_SIG_REG = SAFE_ACCESS_2; +} -- cgit