impl vector_table generation for wch ch32
parent
b5d657a009
commit
263528061d
@ -0,0 +1,24 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const mz = @import("microzig");
|
||||||
|
const periph = mz.chip.peripherals;
|
||||||
|
|
||||||
|
const RCC = periph.RCC;
|
||||||
|
const FLASH = periph.FLASH;
|
||||||
|
|
||||||
|
pub fn rcc_init_hsi_pll() void {
|
||||||
|
const CFG0_PLL_TRIM: *u8 = @ptrFromInt(0x1FFFF7D4); // Factory HSI clock trim value
|
||||||
|
if (CFG0_PLL_TRIM.* != 0xFF) {
|
||||||
|
RCC.CTLR.modify(.{ .HSITRIM = @as(u5, @truncate(CFG0_PLL_TRIM.*)) });
|
||||||
|
}
|
||||||
|
|
||||||
|
FLASH.ACTLR.modify(.{ .LATENCY = 1 }); // Flash wait state 1 for 48MHz clock
|
||||||
|
|
||||||
|
RCC.CFGR0.modify(.{
|
||||||
|
.PLLSRC = 0, // HSI
|
||||||
|
.HPRE = 0, // Prescaler off
|
||||||
|
});
|
||||||
|
RCC.CTLR.modify(.{ .PLLON = 1 });
|
||||||
|
while (RCC.CTLR.read().PLLRDY != 1) {}
|
||||||
|
RCC.CFGR0.modify(.{ .SW = 0b10 }); // Select PLL clock source
|
||||||
|
while (RCC.CFGR0.read().SWS != 0b10) {} // Spin until PLL selected
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const mz = @import("microzig");
|
||||||
|
const periph = mz.chip.peripherals;
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
mz.hal.rcc_init_hsi_pll();
|
||||||
|
periph.RCC.APB2PCENR.modify(.{ .IOPCEN = 1 });
|
||||||
|
periph.GPIOC.CFGLR.modify(.{ .CNF1 = 0b00, .MODE1 = 0b11 });
|
||||||
|
|
||||||
|
var on: u1 = 0;
|
||||||
|
while (true) {
|
||||||
|
on ^= 1;
|
||||||
|
periph.GPIOC.OUTDR.modify(.{ .ODR1 = on });
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
//! codegen specific to riscv
|
||||||
|
const std = @import("std");
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
|
||||||
|
const Database = @import("../Database.zig");
|
||||||
|
const Arch = Database.Arch;
|
||||||
|
const EntityId = Database.EntityId;
|
||||||
|
|
||||||
|
const gen = @import("../gen.zig");
|
||||||
|
const InterruptWithIndexAndName = @import("InterruptWithIndexAndName.zig");
|
||||||
|
|
||||||
|
const log = std.log.scoped(.@"gen.riscv");
|
||||||
|
|
||||||
|
pub fn write_interrupt_vector(
|
||||||
|
db: Database,
|
||||||
|
device_id: EntityId,
|
||||||
|
writer: anytype,
|
||||||
|
) !void {
|
||||||
|
assert(db.entity_is("instance.device", device_id));
|
||||||
|
const arch = db.instances.devices.get(device_id).?.arch;
|
||||||
|
assert(arch.is_riscv());
|
||||||
|
|
||||||
|
try writer.writeAll(
|
||||||
|
\\pub const VectorTable = extern struct {
|
||||||
|
\\ const Handler = micro.interrupt.Handler;
|
||||||
|
\\ const unhandled = micro.interrupt.unhandled;
|
||||||
|
\\
|
||||||
|
);
|
||||||
|
|
||||||
|
var index: i32 = 0;
|
||||||
|
|
||||||
|
if (arch == .qingke_v2) { // CPU specific vectors
|
||||||
|
try writer.writeAll(
|
||||||
|
\\ reserved1: [1]u32 = undefined,
|
||||||
|
\\ NMI: Handler = unhandled,
|
||||||
|
\\ EXC: Handler = unhandled,
|
||||||
|
\\ reserved4: [8]u32 = undefined,
|
||||||
|
\\ SysTick: Handler = unhandled,
|
||||||
|
\\ reserved13: [1]u32 = undefined,
|
||||||
|
\\ SWI: Handler = unhandled,
|
||||||
|
\\ reserved15: [1]u32 = undefined,
|
||||||
|
\\
|
||||||
|
);
|
||||||
|
index = 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (db.children.interrupts.get(device_id)) |interrupt_set| {
|
||||||
|
var interrupts = std.ArrayList(InterruptWithIndexAndName).init(db.gpa);
|
||||||
|
defer interrupts.deinit();
|
||||||
|
|
||||||
|
var it = interrupt_set.iterator();
|
||||||
|
while (it.next()) |entry| {
|
||||||
|
const interrupt_id = entry.key_ptr.*;
|
||||||
|
const interrupt_index = db.instances.interrupts.get(interrupt_id).?;
|
||||||
|
const name = db.attrs.name.get(interrupt_id) orelse continue;
|
||||||
|
|
||||||
|
try interrupts.append(.{
|
||||||
|
.id = interrupt_id,
|
||||||
|
.name = name,
|
||||||
|
.index = interrupt_index,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std.sort.insertion(
|
||||||
|
InterruptWithIndexAndName,
|
||||||
|
interrupts.items,
|
||||||
|
{},
|
||||||
|
InterruptWithIndexAndName.less_than,
|
||||||
|
);
|
||||||
|
|
||||||
|
for (interrupts.items) |interrupt| {
|
||||||
|
if (index < interrupt.index) {
|
||||||
|
try writer.print("reserved{}: [{}]u32 = undefined,\n", .{
|
||||||
|
index,
|
||||||
|
interrupt.index - index,
|
||||||
|
});
|
||||||
|
index = interrupt.index;
|
||||||
|
} else if (index > interrupt.index) {
|
||||||
|
log.warn("skipping interrupt: {s}", .{interrupt.name});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (db.attrs.description.get(interrupt.id)) |description|
|
||||||
|
try gen.write_comment(db.gpa, description, writer);
|
||||||
|
|
||||||
|
try writer.print("{}: Handler = unhandled,\n", .{
|
||||||
|
std.zig.fmtId(interrupt.name),
|
||||||
|
});
|
||||||
|
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try writer.writeAll("};\n\n");
|
||||||
|
}
|
Loading…
Reference in New Issue