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
1 change: 1 addition & 0 deletions port/stmicro/stm32/src/hals/STM32F103.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const backup = @import("./STM32F103/backup.zig");
pub const rtc = @import("./STM32F103/rtc.zig");
pub const dma = @import("./STM32F103/dma.zig");
pub const time = @import("./STM32F103/time.zig");
pub const enums = @import("./common/enums.zig");
const util = @import("./common/util.zig");

//temporary solution
Expand Down
2 changes: 1 addition & 1 deletion port/stmicro/stm32/src/hals/STM32F303.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub const i2c = @import("STM32F303/i2c.zig");
pub const spi = @import("STM32F303/spi.zig");
pub const pins = @import("STM32F303/pins.zig");
pub const dma = @import("./STM32F303/dma.zig");
pub const enums = @import("./common//enums.zig");
pub const enums = @import("./common/enums.zig");
pub const systick_timer = @import("./common/systick_timer.zig");
pub const systick = @import("./common/systick.zig");

Expand Down
7 changes: 3 additions & 4 deletions port/stmicro/stm32/src/hals/STM32F429.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const microzig = @import("microzig");
const peripherals = microzig.chip.peripherals;
const RCC = peripherals.RCC;

const Digital_IO = microzig.drivers.base.Digital_IO;
pub const pins = @import("./common/pins_v2.zig");
pub const enums = @import("./common/enums.zig");

pub const pins = @import("./common/pins_v2.zig");

Expand All @@ -50,9 +51,7 @@ pub const rcc = struct {
const util = @import("common/util.zig");

// Any peripheral that must be enable in RCC.
pub const Peripherals = util.create_peripheral_enum(&.{
"GPIO",
});
pub const Peripherals = enum.Peripherals;

///configure the power and clock registers before enabling the RTC
///this function also can be called from `rtc.enable()`
Expand Down
93 changes: 46 additions & 47 deletions port/stmicro/stm32/src/hals/common/enums.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,60 @@ const util = @import("util.zig");
const microzig = @import("microzig");

// Any peripheral that must be enable in RCC.
pub const Peripherals = util.create_peripheral_enum(&.{
"DMA",
"USBRAM",
"FLASH",
"CRC",
"SDIO",
"AFIO",
"GPIO",
"ADC",
"TIM",
"SPI",
"USART",
"WWDG",
"UART",
"I2C",
"CAN",
"BKP",
"PWR",
"DAC",
"RTC",
"USB",
"LPUART",
});

pub const UART_Type = util.sub_peripheral_enum(Peripherals, &.{ "USART", "UART", "LPUART" }, null);
pub const I2C_Type = util.sub_peripheral_enum(Peripherals, &.{"I2C"}, null);
pub const SPI_Type = util.sub_peripheral_enum(Peripherals, &.{"SPI"}, null);
pub const DMA_Type = util.sub_peripheral_enum(Peripherals, &.{"DMA"}, null);
pub const TIMGP16_Type = util.sub_peripheral_enum(Peripherals, &.{"TIM"}, "TIM_GP16");
pub const ADC_Type = util.sub_peripheral_enum(Peripherals, &.{"ADC"}, null);
pub const Peripherals = util.create_peripheral_enum();

pub const UART_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{ "USART", "UART", "LPUART" }, null);
pub const I2C_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"I2C"}, null);
pub const SPI_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"SPI"}, null);
pub const DMA_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"DMA"}, null);
pub const TIMGP16_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"TIM"}, "TIM_GP16");
pub const ADC_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"ADC"}, null);
pub const CAN_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"CAN"}, null);
pub const SAI_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"SAI"}, null);
pub const I2S_Type = util.sub_peripheral_enum(Peripherals, &[_][]const u8{"I2S"}, null);

fn is_peripheral(comptime val: anytype) bool {
inline for ([_]type{ UART_Type, I2C_Type, SPI_Type, DMA_Type, TIMGP16_Type, ADC_Type, CAN_Type, SAI_Type, I2S_Type }) |T| {
if (@TypeOf(val) == T) {
return true;
}
}
return false;
}

pub fn to_peripheral(comptime val: anytype) Peripherals {
return switch (@TypeOf(val)) {
UART_Type,
I2C_Type,
SPI_Type,
DMA_Type,
TIMGP16_Type,
ADC_Type,
=> @as(Peripherals, @fromBackingInt(@backingInt(val))),
else => @panic("Value must be one of the sur peripheral enum define below"),
};
if (is_peripheral(val)) {
return @as(Peripherals, @fromBackingInt(@backingInt(val)));
}
@panic("Value must be one of the sur peripheral enum define below");
}

pub fn get_regs(comptime T: type, comptime val: anytype) *volatile T {
const periph_enum = comptime to_peripheral(val);
return @field(microzig.chip.peripherals, @tagName(periph_enum));
}

fn is_sub_peripheral(comptime val: anytype) bool {
inline for ([_]type{ UART_Type, I2C_Type, SPI_Type, DMA_Type, ADC_Type, CAN_Type, SAI_Type, I2S_Type }) |T| {
if (@TypeOf(val) == T) {
return true;
}
}
return false;
}

fn get_field_index(comptime T: type, comptime val: T) u32 {
inline for (0.., @typeInfo(T).@"enum".field_values) |i, value| {
if (value == @backingInt(val)) {
return i;
}
}
}

// This function is basically useless, it doesn't even give the number of the paripheral
pub fn base_perihperal_index(comptime val: anytype) u32 {
return switch (@TypeOf(val)) {
UART_Type => @backingInt(val) - @backingInt(Peripherals.USART1),
I2C_Type => @backingInt(val) - @backingInt(Peripherals.I2C1),
SPI_Type => @backingInt(val) - @backingInt(Peripherals.SPI1),
DMA_Type => @backingInt(val) - @backingInt(Peripherals.DMA1),
else => @panic("Index peripheral is only for multiple index peripherals"),
};
if (is_sub_peripheral(val)) {
return get_field_index(@TypeOf(val), val);
}
@panic("Index peripheral is only for multiple index peripherals"); // TODO: Whats against implementing this for all?
}
Loading
Loading