RecursiveError/ClockHelper-zig
ClockHelper is a utility to facilitate clock management on STM32 microcontrollers.
refsClockHelper is a utility to facilitate clock management on STM32 microcontrollers.
ClockHelper is in the early stages of development, configurations may be incorrect/incomplete. In case of errors, feel free to open an issue.
clock helper comes with a utility to transcribe configurations generated by CubeMX (.ioc files) into configurations for embedded Zig projects
to create an executable of this tool: zig build clock_helper --release=safe
usage: clock_helper -i <ioc file> -o <out file>
To use it, it's very simple:
Choose a controller from the list:
const ClockHelper = @import("ClockHelper");
const STM32F103x = ClockHelper.@"STM32F103C(8-B)Tx";
Create a clock using the settings you want to use:
const config = STM32F103x.Config{
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
//.PLLSource = .RCC_PLLSOURCE_HSI_DIV2, USB force Pllsource to be HSE
.PLLMUL = .RCC_PLL_MUL6,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.flags = .{ .USBUsed_ForRCC = true, .HSEOscillator = true },
};
const out = STM32F103x.get_clocks(config) catch unreachable;
const Clock = out.clock;
And that's it — ClockHelper will provide the clock value for each peripheral for this configuration. It also analyzes and builds all RCC configurations, returning comprehensive error messages if any errors occur.
const std = @import("std");
const ClockHelper = @import("ClockHelper");
const STM32F103x = ClockHelper.@"STM32F103C(8-B)Tx";
const config = STM32F103x.Config{
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
//.PLLSource = .RCC_PLLSOURCE_HSI_DIV2, USB force Pllsource to be HSE
.PLLMUL = .RCC_PLL_MUL6,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.flags = .{ .USBUsed_ForRCC = true, .HSEOscillator = true },
};
const out = STM32F103x.get_clocks(config) catch unreachable;
const Clock = out.clock;
pub fn main() !void {
@compileLog(std.fmt.comptimePrint(
\\Corrent OutClocks:
\\SysClock: {d}Mhz
\\AHB Clock: {d}Mhz
\\APB1 Clock: {d}Mhz
\\TimAPB1: {d}Mhz
\\APB2 Clock: {d}Mhz
\\TimAPB2: {d}Mhz
\\USB: {d}Mhz
\\
, .{
(Clock.SysCLKOutput / 1_000_000),
(Clock.AHBOutput / 1_000_000),
(Clock.APB1Output / 1_000_000),
(Clock.TimPrescOut1 / 1_000_000),
(Clock.APB2Output / 1_000_000),
(Clock.TimPrescOut2 / 1_000_000),
(Clock.USBoutput / 1_000_000),
}));
}