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
4 changes: 2 additions & 2 deletions examples/stmicro/stm32/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn build(b: *std.Build) void {
const stm32 = mb.ports.stm32;

const available_examples = [_]Example{
.{ .target = stm32.boards.stm32f303nucleo, .name = "stm32f303nucleo_Blinky", .file = "src/blinky.zig" },
.{ .target = stm32.boards.stm32f3discovery, .name = "stm32f3discovery", .file = "src/blinky.zig" },
.{ .target = stm32.boards.stm32f303nucleo, .name = "STM32F303nucleo_Blinky", .file = "src/blinky.zig" },
.{ .target = stm32.boards.stm32f3discovery, .name = "STM32F3discovery", .file = "src/blinky.zig" },
// TODO: stm32.pins.GlobalConfiguration is not available on those targets
// .{ .target = stm32.chips.stm32f407vg, .name = "stm32f407vg", .file = "src/blinky.zig" },
// .{ .target = stm32.chips.stm32f429zit6u, .name = "stm32f429zit6u", .file = "src/blinky.zig" },
Expand Down
7 changes: 5 additions & 2 deletions examples/stmicro/stm32/src/hts221.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub const microzig_options: microzig.Options = .{
.cpu = .{
.ram_vector_table = true,
},
.hal = if (std.mem.eql(u8, microzig.config.chip_name, "STM32F303VC")) .{
.rcc_clock_config = board.rcc_medium_speed,
} else .{},
};

pub fn init() void {
Expand All @@ -24,8 +27,8 @@ pub fn init() void {
pub fn main() !void {
std.log.info("Starting main", .{});
const clock = try stm32.systick_timer.clock_device();
var i2c1 = board.i2c1();
try i2c1.apply();
var i2c1 = try board.i2c1();
i2c1.apply();
var device = i2c1.i2c_device();

var hts221 = HTS221.init(&device);
Expand Down
2 changes: 2 additions & 0 deletions port/stmicro/stm32/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn init(dep: *std.Build.Dependency) Self {
},
.hal = microzig.HardwareAbstractionLayer{
.root_source_file = b.path("src/hals/STM32F303.zig"),
.imports = hal_imports,
},
.stack = .{ .ram_region_name = "CCMRAM" },
}),
Expand All @@ -54,6 +55,7 @@ pub fn init(dep: *std.Build.Dependency) Self {
},
.hal = microzig.HardwareAbstractionLayer{
.root_source_file = b.path("src/hals/STM32F303.zig"),
.imports = hal_imports,
},
.stack = .{ .ram_region_name = "CCMRAM" },
}),
Expand Down
4 changes: 2 additions & 2 deletions port/stmicro/stm32/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
.hash = "N-V-__8AAFi8WBlOh-NikHFVBjzQE0F1KixgKjVWYnlijPNm",
},
.ClockHelper = .{
.url = "git+https://github.com/ZigEmbeddedGroup/ClockHelper#7fd073b1be9544941c15f9a63032ed06149ddb70",
.hash = "ClockHelper-2.0.0-RcMaOSniGQHXH_qeoZbQDG64XThqpXTVPMfJ6P7LHpYY",
.url = "git+https://github.com/ZigEmbeddedGroup/ClockHelper#922c35eb54f0417318ccfcc32367bdcf11823ede",
.hash = "ClockHelper-2.0.0-RcMaOUMGIwEUEHQJ0C2Q-KbWdKXN2GqzQcs9MpVSTgm3",
},
},
.paths = .{
Expand Down
44 changes: 33 additions & 11 deletions port/stmicro/stm32/src/boards/STM32F3DISCOVERY.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
const std = @import("std");

pub const microzig = @import("microzig");
const RCC = microzig.chip.peripherals.RCC;

pub const hal = microzig.hal;
pub const rcc = hal.rcc;
const rcc = hal.rcc;

pub const rcc_high_speed: rcc.Config = .{
.PRESCALERUSB = .RCC_USBCLKSOURCE_PLL_DIV1_5,
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.PLLSource = .RCC_PLLSOURCE_HSE,
.PLLMUL = .RCC_PLL_MUL9,
.flags = .{
.HSEByPass = true,
.HSEOscillator = true,
.USART1Used_ForRCC = true,
.I2C1Used_ForRCC = true,
},
};

pub const rcc_medium_speed: rcc.Config = .{
.SYSCLKSource = .RCC_SYSCLKSOURCE_PLLCLK,
.APB1CLKDivider = .RCC_HCLK_DIV2,
.PLLSource = .RCC_PLLSOURCE_HSE,
.PLLMUL = .RCC_PLL_MUL6,
.flags = .{
.HSEByPass = true,
.HSEOscillator = true,
.USART1Used_ForRCC = true,
.I2C1Used_ForRCC = true,
},
};

pub const uart_logger = hal.uart.UARTLogger(.USART1);

Expand All @@ -21,13 +49,7 @@ pub const leds_config = (hal.pins.GlobalConfiguration{
});

pub fn init() void {
rcc.enable_hse(8_000_000);
rcc.enable_pll(.HSE, .Div1, .Mul5) catch {
@panic("PLL faile to enable");
};
rcc.select_pll_for_sysclk() catch {
@panic("Faile to select sysclk");
};
rcc.apply();
}

// Init should come first or the baud_rate would be too fast for the default HSI.
Expand All @@ -39,12 +61,12 @@ pub fn init_log() void {
},
}).apply();
uart_logger.init(.{
.baud_rate = 115200,
.baud_rate = 9600,
.dma = hal.dma.DMA1_Channel4.get_channel(),
});
}

pub fn i2c1() hal.i2c.I2C_Device {
pub fn i2c1() !hal.i2c.I2C_Device {
_ = (hal.pins.GlobalConfiguration{
.GPIOB = .{
// I2C
Expand All @@ -57,5 +79,5 @@ pub fn i2c1() hal.i2c.I2C_Device {
},
}).apply();

return hal.i2c.I2C_Device.init(.I2C1);
return try hal.i2c.I2C_Device.init(.I2C1);
}
4 changes: 2 additions & 2 deletions port/stmicro/stm32/src/boards/STM32L476DISCOVERY.zig
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub const Lcd = struct {
}
};

pub fn i2c1() hal.i2c.I2C_Device {
pub fn i2c1() !hal.i2c.I2C_Device {
_ = (hal.pins.GlobalConfiguration{
.GPIOB = .{
// I2C
Expand All @@ -214,7 +214,7 @@ pub fn i2c1() hal.i2c.I2C_Device {
},
}).apply();

return hal.i2c.I2C_Device.init(.I2C1);
return try hal.i2c.I2C_Device.init(.I2C1);
}

pub const uart_logger = hal.uart.UARTLogger(.USART2);
Expand Down
8 changes: 6 additions & 2 deletions port/stmicro/stm32/src/hals/STM32F303.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ pub const systick_timer = @import("./common/systick_timer.zig");
pub const systick = @import("./common/systick.zig");

pub fn get_sys_clk() u32 {
return rcc.current_clock.h_clk;
return @intFromFloat(rcc.current_clocks.clock.HCLKOutput);
}

pub fn get_systick_clk() u32 {
return rcc.current_clock.h_clk / 8;
return @as(u32, @intFromFloat(rcc.current_clocks.clock.HCLKOutput)) / 8;
}

pub const HAL_Options = struct {
rcc_clock_config: rcc.Config = .{},
};
Loading
Loading