Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/stmicro/stm32/src/stm32f1xx/advanced_adc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ fn adc_to_temp(val: usize) f32 {
}

pub fn main() !void {
try rcc.apply_clock(.{ .ADCprescaler = .RCC_ADCPCLK2_DIV2 });
_ = try rcc.apply(.{
.ADCPresc = .RCC_ADCPCLK2_DIV2,
.flags = .{
.USE_ADC1 = true,
},
});

rcc.enable_clock(.DMA1);
rcc.enable_clock(.TIM2);
Expand Down
12 changes: 8 additions & 4 deletions examples/stmicro/stm32/src/stm32f1xx/gpio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ const gpio = stm32.gpio;
const time = stm32.time;

pub fn main() !void {
try rcc.apply_clock(.{
.SysClkSource = .RCC_SYSCLKSOURCE_PLLCLK,
_ = try rcc.apply(.{
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
.PLLSource = .RCC_PLLSOURCE_HSE,
.PLLMUL = .RCC_PLL_MUL9,
.APB1Prescaler = .RCC_HCLK_DIV2,
.RTCClkSource = .RCC_RTCCLKSOURCE_LSI,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.RTCClockSelection = .RCC_RTCCLKSOURCE_LSI,
.flags = .{
.RTCUsed_ForRCC = true,
.HSEOscillator = true,
},
});
rcc.enable_clock(.GPIOC);

Expand Down
15 changes: 10 additions & 5 deletions examples/stmicro/stm32/src/stm32f1xx/rcc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ pub const microzig_options = microzig.Options{

const clk_config = rcc.Config{
.PLLSource = .RCC_PLLSOURCE_HSE,
.HSEDivPLL = .RCC_HSE_PREDIV_DIV2,
.HSEDivPLL = .RCC_HSE_PREDIV_DIV1,
.PLLMUL = .RCC_PLL_MUL2,
.SysClkSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1Prescaler = .RCC_HCLK_DIV1,
.MCOMult = .RCC_MCO1SOURCE_SYSCLK,
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.RCC_MCOSource = .RCC_MCO1SOURCE_SYSCLK,
.flags = .{
.HSEOscillator = true,
.MCOUsed_ForRCC = true,
.MCOConfig = true,
},
};

pub fn main() !void {
try rcc.apply_clock(clk_config);
_ = try rcc.apply(clk_config);
rcc.enable_clock(.GPIOA);
rcc.enable_clock(.AFIO);
rcc.enable_clock(.USART1);
Expand Down
8 changes: 6 additions & 2 deletions examples/stmicro/stm32/src/stm32f1xx/rtc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub fn main() !void {
//by the system reset, so we need to check if it is already running.
//If it is not running, we will configure it and enable it.
if (fresh_start()) {
try rcc.apply_clock(.{ .RTCClkSource = .RCC_RTCCLKSOURCE_LSE });
_ = try rcc.apply(.{
.RTCClockSelection = .RCC_RTCCLKSOURCE_LSE,
.flags = .{ .RTCUsed_ForRCC = true, .LSEOscillator = true },
});
rcc.enable_clock(.PWR);
rcc.enable_clock(.BKP);

Expand Down Expand Up @@ -66,9 +69,10 @@ pub fn main() !void {
}

fn fresh_start() bool {
const power_down: bool = hal.Reset_Reason == .POR_or_PDR;
rcc.enable_clock(.PWR);
rcc.enable_clock(.BKP);
const data: u32 = (@as(u32, bkp.BackupData1[1].data) << 16) | bkp.BackupData1[0].data;
rcc.disable_all_clocks();
return data != 0xDEADBEEF;
return (data != 0xDEADBEEF) or power_down;
}
9 changes: 8 additions & 1 deletion examples/stmicro/stm32/src/stm32f1xx/timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const gpio = stm32.gpio;
const GPTimer = stm32.timer.GPTimer;
const time = stm32.time;

//pub const microzig_options: microzig.Options = .{
// .interrupts = .{ .TIM3 = .{ .c = time.TIM_handler } },
//};

//gpios
const ch1 = gpio.Pin.from_port(.A, 0);
const ch2 = gpio.Pin.from_port(.A, 1);
Expand All @@ -23,7 +27,10 @@ pub fn main() !void {
//first we need to enable the clocks for the GPIO and TIM peripherals

//use HSE as system clock source, more stable than HSI
try rcc.apply_clock(.{ .SysClkSource = .RCC_SYSCLKSOURCE_HSE });
_ = try rcc.apply(.{
.SYSCLKSource = .RCC_SYSCLKSOURCE_HSE,
.flags = .{ .HSEOscillator = true },
});

//enable GPIOA and TIM2, TIM3, AFIO clocks
//AFIO is needed for alternate function remapping, not used in this example but eneble for easy remapping
Expand Down
5 changes: 4 additions & 1 deletion examples/stmicro/stm32/src/stm32f1xx/timer_capture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ pub fn main() !void {
//first we need to enable the clocks for the GPIO and TIM peripherals

//use HSE as system clock source, more stable than HSI
try rcc.apply_clock(.{ .SysClkSource = .RCC_SYSCLKSOURCE_HSE });
_ = try rcc.apply(.{
.SYSCLKSource = .RCC_SYSCLKSOURCE_HSE,
.flags = .{ .HSEOscillator = true },
});

//enable GPIOA and TIM2, TIM3, AFIO clocks
//AFIO is needed for alternate function remapping, not used in this example but eneble for easy remapping
Expand Down
10 changes: 7 additions & 3 deletions examples/stmicro/stm32/src/stm32f1xx/usb_cdc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,16 @@ fn CDC_read(buf: []u8, timeout: ?Duration) ![]const u8 {
}

pub fn main() !void {
try rcc.apply_clock(.{
_ = try rcc.apply(.{
.PLLSource = .RCC_PLLSOURCE_HSE,
.PLLMUL = .RCC_PLL_MUL9,
.SysClkSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1Prescaler = .RCC_HCLK_DIV2,
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.USBPrescaler = .RCC_USBCLKSOURCE_PLL_DIV1_5,
.flags = .{
.HSEOscillator = true,
.USBUsed_ForRCC = true,
},
});

rcc.enable_clock(.GPIOA);
Expand Down
7 changes: 4 additions & 3 deletions examples/stmicro/stm32/src/stm32f1xx/usb_hid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,13 @@ fn report(keys: []const u8) void {
}

pub fn main() !void {
try rcc.apply_clock(.{
_ = try rcc.apply(.{
.PLLSource = .RCC_PLLSOURCE_HSE,
.PLLMUL = .RCC_PLL_MUL9,
.SysClkSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1Prescaler = .RCC_HCLK_DIV2,
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.USBPrescaler = .RCC_USBCLKSOURCE_PLL_DIV1_5,
.flags = .{ .HSEOscillator = true, .USBUsed_ForRCC = true },
});

rcc.enable_clock(.GPIOA);
Expand Down
14 changes: 14 additions & 0 deletions port/stmicro/stm32/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ pub fn build(b: *std.Build) !void {
});
generate_exe.root_module.addImport("regz", regz);

const clocktree_test = b.addTest(.{
.name = "ClockTree Test",
.root_module = b.createModule(.{
.root_source_file = b.path("stm32-clocks/lib.zig"),
.target = b.graph.host,
.optimize = .ReleaseSafe,
}),
});

const clocktree_test_run = b.addRunArtifact(clocktree_test);

const generate_run = b.addRunArtifact(generate_exe);
generate_run.max_stdio_size = std.math.maxInt(usize);
generate_run.addFileArg(stm32_data_generated.path("."));
Expand All @@ -77,4 +88,7 @@ pub fn build(b: *std.Build) !void {
generate_step.dependOn(&generate_run.step);

_ = b.step("test", "Run platform agnostic unit tests");

const clocktree_step = b.step("test_clocktree", "Run clocktree unit tests");
clocktree_step.dependOn(&clocktree_test_run.step);
}
Loading
Loading