From 2b5c6096b03017748b0b5762657e5f3b2aa66533 Mon Sep 17 00:00:00 2001 From: Matt Knight Date: Tue, 27 Jun 2023 21:10:35 -0700 Subject: [PATCH] builtin type inference fix (#68) --- examples/flash_program.zig | 2 +- examples/random.zig | 4 +- examples/squarewave.zig | 29 +++++----- examples/usb_device.zig | 12 ++--- examples/usb_hid.zig | 12 ++--- examples/ws2812.zig | 2 +- src/chips/RP2040.zig | 80 ++++++++++++++-------------- src/hal/adc.zig | 14 ++--- src/hal/clocks.zig | 4 +- src/hal/dma.zig | 4 +- src/hal/flash.zig | 2 +- src/hal/gpio.zig | 12 ++--- src/hal/hw.zig | 12 ++--- src/hal/i2c.zig | 14 ++--- src/hal/multicore.zig | 2 +- src/hal/pins.zig | 6 +-- src/hal/pio.zig | 26 ++++----- src/hal/pio/assembler.zig | 2 +- src/hal/pio/assembler/Expression.zig | 14 ++--- src/hal/pio/assembler/encoder.zig | 16 +++--- src/hal/pio/assembler/tokenizer.zig | 10 ++-- src/hal/pwm.zig | 2 +- src/hal/random.zig | 4 +- src/hal/resets.zig | 10 ++-- src/hal/rom.zig | 46 ++++++++-------- src/hal/spi.zig | 6 +-- src/hal/time.zig | 8 +-- src/hal/uart.zig | 8 +-- src/hal/usb.zig | 54 +++++++++---------- 29 files changed, 210 insertions(+), 207 deletions(-) diff --git a/examples/flash_program.zig b/examples/flash_program.zig index d38d0ae..48754e1 100644 --- a/examples/flash_program.zig +++ b/examples/flash_program.zig @@ -14,7 +14,7 @@ const uart_tx_pin = gpio.num(0); const uart_rx_pin = gpio.num(1); const flash_target_offset: u32 = 256 * 1024; -const flash_target_contents = @ptrFromInt([*]const u8, rp2040.flash.XIP_BASE + flash_target_offset); +const flash_target_contents = @as([*]const u8, @ptrFromInt(rp2040.flash.XIP_BASE + flash_target_offset)); pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn { std.log.err("panic: {s}", .{message}); diff --git a/examples/random.zig b/examples/random.zig index 40d2f68..34d03d1 100644 --- a/examples/random.zig +++ b/examples/random.zig @@ -52,7 +52,7 @@ pub fn main() !void { rng.bytes(buffer[0..]); counter += 8; for (buffer) |byte| { - dist[@intCast(usize, byte)] += 1; + dist[@as(usize, @intCast(byte))] += 1; } std.log.info("Generate random number: {any}", .{buffer}); @@ -60,7 +60,7 @@ pub fn main() !void { var i: usize = 0; std.log.info("Distribution:", .{}); while (i < 256) : (i += 1) { - std.log.info("{} -> {}, {d:2}%", .{ i, dist[i], @floatFromInt(f32, dist[i]) / @floatFromInt(f32, counter) }); + std.log.info("{} -> {}, {d:2}%", .{ i, dist[i], @as(f32, @floatFromInt(dist[i])) / @as(f32, @floatFromInt(counter)) }); } } time.sleep_ms(1000); diff --git a/examples/squarewave.zig b/examples/squarewave.zig index 0564b62..0894d9a 100644 --- a/examples/squarewave.zig +++ b/examples/squarewave.zig @@ -6,19 +6,22 @@ const gpio = rp2040.gpio; const Pio = rp2040.pio.Pio; const StateMachine = rp2040.pio.StateMachine; -const squarewave_program = rp2040.pio.assemble( - \\; - \\; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. - \\; - \\; SPDX-License-Identifier: BSD-3-Clause - \\; - \\.program squarewave - \\ set pindirs, 1 ; Set pin to output - \\again: - \\ set pins, 1 [1] ; Drive pin high and then delay for one cycle - \\ set pins, 0 ; Drive pin low - \\ jmp again ; Set PC to label `again` -, .{}).get_program_by_name("squarewave"); +const squarewave_program = blk: { + @setEvalBranchQuota(2000); + break :blk rp2040.pio.assemble( + \\; + \\; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. + \\; + \\; SPDX-License-Identifier: BSD-3-Clause + \\; + \\.program squarewave + \\ set pindirs, 1 ; Set pin to output + \\again: + \\ set pins, 1 [1] ; Drive pin high and then delay for one cycle + \\ set pins, 0 ; Drive pin low + \\ jmp again ; Set PC to label `again` + , .{}).get_program_by_name("squarewave"); +}; // Pick one PIO instance arbitrarily. We're also arbitrarily picking state // machine 0 on this PIO instance (the state machines are numbered 0 to 3 diff --git a/examples/usb_device.zig b/examples/usb_device.zig index d2b243d..8f2d74e 100644 --- a/examples/usb_device.zig +++ b/examples/usb_device.zig @@ -38,7 +38,7 @@ fn ep1_out_callback(dc: *usb.DeviceConfiguration, data: []const u8) void { // add your own endpoints to... pub var EP1_OUT_CFG: usb.EndpointConfiguration = .{ .descriptor = &usb.EndpointDescriptor{ - .length = @intCast(u8, @sizeOf(usb.EndpointDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.EndpointDescriptor))), .descriptor_type = usb.DescType.Endpoint, .endpoint_address = usb.Dir.Out.endpoint(1), .attributes = @intFromEnum(usb.TransferType.Bulk), @@ -55,7 +55,7 @@ pub var EP1_OUT_CFG: usb.EndpointConfiguration = .{ pub var EP1_IN_CFG: usb.EndpointConfiguration = .{ .descriptor = &usb.EndpointDescriptor{ - .length = @intCast(u8, @sizeOf(usb.EndpointDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.EndpointDescriptor))), .descriptor_type = usb.DescType.Endpoint, .endpoint_address = usb.Dir.In.endpoint(1), .attributes = @intFromEnum(usb.TransferType.Bulk), @@ -73,7 +73,7 @@ pub var EP1_IN_CFG: usb.EndpointConfiguration = .{ // This is our device configuration pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{ .device_descriptor = &.{ - .length = @intCast(u8, @sizeOf(usb.DeviceDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.DeviceDescriptor))), .descriptor_type = usb.DescType.Device, .bcd_usb = 0x0110, .device_class = 0, @@ -89,7 +89,7 @@ pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{ .num_configurations = 1, }, .interface_descriptor = &.{ - .length = @intCast(u8, @sizeOf(usb.InterfaceDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.InterfaceDescriptor))), .descriptor_type = usb.DescType.Interface, .interface_number = 0, .alternate_setting = 0, @@ -101,9 +101,9 @@ pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{ .interface_s = 0, }, .config_descriptor = &.{ - .length = @intCast(u8, @sizeOf(usb.ConfigurationDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.ConfigurationDescriptor))), .descriptor_type = usb.DescType.Config, - .total_length = @intCast(u8, @sizeOf(usb.ConfigurationDescriptor) + @sizeOf(usb.InterfaceDescriptor) + @sizeOf(usb.EndpointDescriptor) + @sizeOf(usb.EndpointDescriptor)), + .total_length = @as(u8, @intCast(@sizeOf(usb.ConfigurationDescriptor) + @sizeOf(usb.InterfaceDescriptor) + @sizeOf(usb.EndpointDescriptor) + @sizeOf(usb.EndpointDescriptor))), .num_interfaces = 1, .configuration_value = 1, .configuration_s = 0, diff --git a/examples/usb_hid.zig b/examples/usb_hid.zig index 2eced2b..752111a 100644 --- a/examples/usb_hid.zig +++ b/examples/usb_hid.zig @@ -38,7 +38,7 @@ fn ep1_out_callback(dc: *usb.DeviceConfiguration, data: []const u8) void { // add your own endpoints to... pub var EP1_OUT_CFG: usb.EndpointConfiguration = .{ .descriptor = &usb.EndpointDescriptor{ - .length = @intCast(u8, @sizeOf(usb.EndpointDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.EndpointDescriptor))), .descriptor_type = usb.DescType.Endpoint, .endpoint_address = usb.Dir.Out.endpoint(1), .attributes = @intFromEnum(usb.TransferType.Interrupt), @@ -55,7 +55,7 @@ pub var EP1_OUT_CFG: usb.EndpointConfiguration = .{ pub var EP1_IN_CFG: usb.EndpointConfiguration = .{ .descriptor = &usb.EndpointDescriptor{ - .length = @intCast(u8, @sizeOf(usb.EndpointDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.EndpointDescriptor))), .descriptor_type = usb.DescType.Endpoint, .endpoint_address = usb.Dir.In.endpoint(1), .attributes = @intFromEnum(usb.TransferType.Interrupt), @@ -73,7 +73,7 @@ pub var EP1_IN_CFG: usb.EndpointConfiguration = .{ // This is our device configuration pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{ .device_descriptor = &.{ - .length = @intCast(u8, @sizeOf(usb.DeviceDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.DeviceDescriptor))), .descriptor_type = usb.DescType.Device, .bcd_usb = 0x0200, .device_class = 0, @@ -91,7 +91,7 @@ pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{ .num_configurations = 1, }, .interface_descriptor = &.{ - .length = @intCast(u8, @sizeOf(usb.InterfaceDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.InterfaceDescriptor))), .descriptor_type = usb.DescType.Interface, .interface_number = 0, .alternate_setting = 0, @@ -103,9 +103,9 @@ pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{ .interface_s = 0, }, .config_descriptor = &.{ - .length = @intCast(u8, @sizeOf(usb.ConfigurationDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.ConfigurationDescriptor))), .descriptor_type = usb.DescType.Config, - .total_length = @intCast(u8, @sizeOf(usb.ConfigurationDescriptor) + @sizeOf(usb.InterfaceDescriptor) + @sizeOf(usb.EndpointDescriptor) + @sizeOf(usb.EndpointDescriptor)), + .total_length = @as(u8, @intCast(@sizeOf(usb.ConfigurationDescriptor) + @sizeOf(usb.InterfaceDescriptor) + @sizeOf(usb.EndpointDescriptor) + @sizeOf(usb.EndpointDescriptor))), .num_interfaces = 1, .configuration_value = 1, .configuration_s = 0, diff --git a/examples/ws2812.zig b/examples/ws2812.zig index f61204b..64fbac2 100644 --- a/examples/ws2812.zig +++ b/examples/ws2812.zig @@ -43,7 +43,7 @@ pub fn main() void { const cycles_per_bit: comptime_int = ws2812_program.defines[0].value + //T1 ws2812_program.defines[1].value + //T2 ws2812_program.defines[2].value; //T3 - const div = @floatFromInt(f32, rp2040.clock_config.sys.?.output_freq) / + const div = @as(f32, @floatFromInt(rp2040.clock_config.sys.?.output_freq)) / (800_000 * cycles_per_bit); pio.sm_load_and_start_program(sm, ws2812_program, .{ diff --git a/src/chips/RP2040.zig b/src/chips/RP2040.zig index 74f6134..a11f255 100644 --- a/src/chips/RP2040.zig +++ b/src/chips/RP2040.zig @@ -65,9 +65,9 @@ pub const devices = struct { pub const peripherals = struct { /// System Control Space - pub const MPU = @ptrFromInt(*volatile types.peripherals.SCS, 0xd90); + pub const MPU = @as(*volatile types.peripherals.SCS, @ptrFromInt(0xd90)); /// QSPI flash execute-in-place block - pub const XIP_CTRL = @ptrFromInt(*volatile types.peripherals.XIP_CTRL, 0x14000000); + pub const XIP_CTRL = @as(*volatile types.peripherals.XIP_CTRL, @ptrFromInt(0x14000000)); /// DW_apb_ssi has the following features: /// * APB interface – Allows for easy integration into a DesignWare Synthesizable Components for AMBA 2 implementation. /// * APB3 and APB4 protocol support. @@ -94,27 +94,27 @@ pub const devices = struct { /// - Interrupt polarity – active high interrupt lines. /// - Serial clock polarity – low serial-clock polarity directly after reset. /// - Serial clock phase – capture on first edge of serial-clock directly after reset. - pub const XIP_SSI = @ptrFromInt(*volatile types.peripherals.XIP_SSI, 0x18000000); - pub const SYSINFO = @ptrFromInt(*volatile types.peripherals.SYSINFO, 0x40000000); + pub const XIP_SSI = @as(*volatile types.peripherals.XIP_SSI, @ptrFromInt(0x18000000)); + pub const SYSINFO = @as(*volatile types.peripherals.SYSINFO, @ptrFromInt(0x40000000)); /// Register block for various chip control signals - pub const SYSCFG = @ptrFromInt(*volatile types.peripherals.SYSCFG, 0x40004000); - pub const CLOCKS = @ptrFromInt(*volatile types.peripherals.CLOCKS, 0x40008000); - pub const RESETS = @ptrFromInt(*volatile types.peripherals.RESETS, 0x4000c000); - pub const PSM = @ptrFromInt(*volatile types.peripherals.PSM, 0x40010000); - pub const IO_BANK0 = @ptrFromInt(*volatile types.peripherals.IO_BANK0, 0x40014000); - pub const IO_QSPI = @ptrFromInt(*volatile types.peripherals.IO_QSPI, 0x40018000); - pub const PADS_BANK0 = @ptrFromInt(*volatile types.peripherals.PADS_BANK0, 0x4001c000); - pub const PADS_QSPI = @ptrFromInt(*volatile types.peripherals.PADS_QSPI, 0x40020000); + pub const SYSCFG = @as(*volatile types.peripherals.SYSCFG, @ptrFromInt(0x40004000)); + pub const CLOCKS = @as(*volatile types.peripherals.CLOCKS, @ptrFromInt(0x40008000)); + pub const RESETS = @as(*volatile types.peripherals.RESETS, @ptrFromInt(0x4000c000)); + pub const PSM = @as(*volatile types.peripherals.PSM, @ptrFromInt(0x40010000)); + pub const IO_BANK0 = @as(*volatile types.peripherals.IO_BANK0, @ptrFromInt(0x40014000)); + pub const IO_QSPI = @as(*volatile types.peripherals.IO_QSPI, @ptrFromInt(0x40018000)); + pub const PADS_BANK0 = @as(*volatile types.peripherals.PADS_BANK0, @ptrFromInt(0x4001c000)); + pub const PADS_QSPI = @as(*volatile types.peripherals.PADS_QSPI, @ptrFromInt(0x40020000)); /// Controls the crystal oscillator - pub const XOSC = @ptrFromInt(*volatile types.peripherals.XOSC, 0x40024000); - pub const PLL_SYS = @ptrFromInt(*volatile types.peripherals.PLL_SYS, 0x40028000); - pub const PLL_USB = @ptrFromInt(*volatile types.peripherals.PLL_SYS, 0x4002c000); + pub const XOSC = @as(*volatile types.peripherals.XOSC, @ptrFromInt(0x40024000)); + pub const PLL_SYS = @as(*volatile types.peripherals.PLL_SYS, @ptrFromInt(0x40028000)); + pub const PLL_USB = @as(*volatile types.peripherals.PLL_SYS, @ptrFromInt(0x4002c000)); /// Register block for busfabric control signals and performance counters - pub const BUSCTRL = @ptrFromInt(*volatile types.peripherals.BUSCTRL, 0x40030000); - pub const UART0 = @ptrFromInt(*volatile types.peripherals.UART0, 0x40034000); - pub const UART1 = @ptrFromInt(*volatile types.peripherals.UART0, 0x40038000); - pub const SPI0 = @ptrFromInt(*volatile types.peripherals.SPI0, 0x4003c000); - pub const SPI1 = @ptrFromInt(*volatile types.peripherals.SPI0, 0x40040000); + pub const BUSCTRL = @as(*volatile types.peripherals.BUSCTRL, @ptrFromInt(0x40030000)); + pub const UART0 = @as(*volatile types.peripherals.UART0, @ptrFromInt(0x40034000)); + pub const UART1 = @as(*volatile types.peripherals.UART0, @ptrFromInt(0x40038000)); + pub const SPI0 = @as(*volatile types.peripherals.SPI0, @ptrFromInt(0x4003c000)); + pub const SPI1 = @as(*volatile types.peripherals.SPI0, @ptrFromInt(0x40040000)); /// DW_apb_i2c address block /// List of configuration constants for the Synopsys I2C hardware (you may see references to these in I2C register header; these are *fixed* values, set at hardware design time): /// IC_ULTRA_FAST_MODE ................ 0x0 @@ -185,7 +185,7 @@ pub const devices = struct { /// IC_STOP_DET_IF_MASTER_ACTIVE ...... 0x0 /// IC_DEFAULT_UFM_SPKLEN ............. 0x1 /// IC_TX_BUFFER_DEPTH ................ 16 - pub const I2C0 = @ptrFromInt(*volatile types.peripherals.I2C0, 0x40044000); + pub const I2C0 = @as(*volatile types.peripherals.I2C0, @ptrFromInt(0x40044000)); /// DW_apb_i2c address block /// List of configuration constants for the Synopsys I2C hardware (you may see references to these in I2C register header; these are *fixed* values, set at hardware design time): /// IC_ULTRA_FAST_MODE ................ 0x0 @@ -256,11 +256,11 @@ pub const devices = struct { /// IC_STOP_DET_IF_MASTER_ACTIVE ...... 0x0 /// IC_DEFAULT_UFM_SPKLEN ............. 0x1 /// IC_TX_BUFFER_DEPTH ................ 16 - pub const I2C1 = @ptrFromInt(*volatile types.peripherals.I2C0, 0x40048000); + pub const I2C1 = @as(*volatile types.peripherals.I2C0, @ptrFromInt(0x40048000)); /// Control and data interface to SAR ADC - pub const ADC = @ptrFromInt(*volatile types.peripherals.ADC, 0x4004c000); + pub const ADC = @as(*volatile types.peripherals.ADC, @ptrFromInt(0x4004c000)); /// Simple PWM - pub const PWM = @ptrFromInt(*volatile types.peripherals.PWM, 0x40050000); + pub const PWM = @as(*volatile types.peripherals.PWM, @ptrFromInt(0x40050000)); /// Controls time and alarms /// time is a 64 bit value indicating the time in usec since power-on /// timeh is the top 32 bits of time & timel is the bottom 32 bits @@ -271,35 +271,35 @@ pub const devices = struct { /// An alarm can be cancelled before it has finished by clearing the alarm_enable /// When an alarm fires, the corresponding alarm_irq is set and alarm_running is cleared /// To clear the interrupt write a 1 to the corresponding alarm_irq - pub const TIMER = @ptrFromInt(*volatile types.peripherals.TIMER, 0x40054000); - pub const WATCHDOG = @ptrFromInt(*volatile types.peripherals.WATCHDOG, 0x40058000); + pub const TIMER = @as(*volatile types.peripherals.TIMER, @ptrFromInt(0x40054000)); + pub const WATCHDOG = @as(*volatile types.peripherals.WATCHDOG, @ptrFromInt(0x40058000)); /// Register block to control RTC - pub const RTC = @ptrFromInt(*volatile types.peripherals.RTC, 0x4005c000); - pub const ROSC = @ptrFromInt(*volatile types.peripherals.ROSC, 0x40060000); + pub const RTC = @as(*volatile types.peripherals.RTC, @ptrFromInt(0x4005c000)); + pub const ROSC = @as(*volatile types.peripherals.ROSC, @ptrFromInt(0x40060000)); /// control and status for on-chip voltage regulator and chip level reset subsystem - pub const VREG_AND_CHIP_RESET = @ptrFromInt(*volatile types.peripherals.VREG_AND_CHIP_RESET, 0x40064000); + pub const VREG_AND_CHIP_RESET = @as(*volatile types.peripherals.VREG_AND_CHIP_RESET, @ptrFromInt(0x40064000)); /// Testbench manager. Allows the programmer to know what platform their software is running on. - pub const TBMAN = @ptrFromInt(*volatile types.peripherals.TBMAN, 0x4006c000); + pub const TBMAN = @as(*volatile types.peripherals.TBMAN, @ptrFromInt(0x4006c000)); /// DMA with separate read and write masters - pub const DMA = @ptrFromInt(*volatile types.peripherals.DMA, 0x50000000); + pub const DMA = @as(*volatile types.peripherals.DMA, @ptrFromInt(0x50000000)); /// DPRAM layout for USB device. - pub const USBCTRL_DPRAM = @ptrFromInt(*volatile types.peripherals.USBCTRL_DPRAM, 0x50100000); + pub const USBCTRL_DPRAM = @as(*volatile types.peripherals.USBCTRL_DPRAM, @ptrFromInt(0x50100000)); /// USB FS/LS controller device registers - pub const USBCTRL_REGS = @ptrFromInt(*volatile types.peripherals.USBCTRL_REGS, 0x50110000); + pub const USBCTRL_REGS = @as(*volatile types.peripherals.USBCTRL_REGS, @ptrFromInt(0x50110000)); /// Programmable IO block - pub const PIO0 = @ptrFromInt(*volatile types.peripherals.PIO0, 0x50200000); + pub const PIO0 = @as(*volatile types.peripherals.PIO0, @ptrFromInt(0x50200000)); /// Programmable IO block - pub const PIO1 = @ptrFromInt(*volatile types.peripherals.PIO0, 0x50300000); + pub const PIO1 = @as(*volatile types.peripherals.PIO0, @ptrFromInt(0x50300000)); /// Single-cycle IO block /// Provides core-local and inter-core hardware for the two processors, with single-cycle access. - pub const SIO = @ptrFromInt(*volatile types.peripherals.SIO, 0xd0000000); - pub const PPB = @ptrFromInt(*volatile types.peripherals.PPB, 0xe0000000); + pub const SIO = @as(*volatile types.peripherals.SIO, @ptrFromInt(0xd0000000)); + pub const PPB = @as(*volatile types.peripherals.PPB, @ptrFromInt(0xe0000000)); /// System Tick Timer - pub const SysTick = @ptrFromInt(*volatile types.peripherals.SysTick, 0xe000e010); + pub const SysTick = @as(*volatile types.peripherals.SysTick, @ptrFromInt(0xe000e010)); /// System Control Space - pub const NVIC = @ptrFromInt(*volatile types.peripherals.NVIC, 0xe000e100); + pub const NVIC = @as(*volatile types.peripherals.NVIC, @ptrFromInt(0xe000e100)); /// System Control Space - pub const SCB = @ptrFromInt(*volatile types.peripherals.SCB, 0xe000ed00); + pub const SCB = @as(*volatile types.peripherals.SCB, @ptrFromInt(0xe000ed00)); }; }; }; diff --git a/src/hal/adc.zig b/src/hal/adc.zig index 973db63..e025a63 100644 --- a/src/hal/adc.zig +++ b/src/hal/adc.zig @@ -17,7 +17,7 @@ pub const Error = error{ /// temp_sensor is not valid because you can refer to it by name. pub fn input(n: u2) Input { - return @enumFromInt(Input, n); + return @as(Input, @enumFromInt(n)); } /// Enable the ADC controller. @@ -50,7 +50,7 @@ pub fn apply(config: Config) void { .ERR_STICKY = 0, .AINSEL = 0, .RROBIN = if (config.round_robin) |rr| - @bitCast(u5, rr) + @as(u5, @bitCast(rr)) else 0, @@ -63,8 +63,8 @@ pub fn apply(config: Config) void { if (config.sample_frequency) |sample_frequency| { const cycles = (48_000_000 * 256) / @as(u64, sample_frequency); ADC.DIV.write(.{ - .FRAC = @truncate(u8, cycles), - .INT = @intCast(u16, (cycles >> 8) - 1), + .FRAC = @as(u8, @truncate(cycles)), + .INT = @as(u16, @intCast((cycles >> 8) - 1)), .padding = 0, }); @@ -85,7 +85,7 @@ pub fn select_input(in: Input) void { /// 4 is the temperature sensor. pub fn get_selected_input() Input { const cs = ADC.SC.read(); - return @enumFromInt(Input, cs.AINSEL); + return @as(Input, @enumFromInt(cs.AINSEL)); } pub const Input = enum(u3) { @@ -126,7 +126,7 @@ pub fn set_temp_sensor_enabled(enable: bool) void { /// T must be floating point. pub fn temp_sensor_result_to_celcius(comptime T: type, comptime vref: T, result: u12) T { // TODO: consider fixed-point - const raw = @floatFromInt(T, result); + const raw = @as(T, @floatFromInt(result)); const voltage: T = vref * raw / 0x0fff; return (27.0 - ((voltage - 0.706) / 0.001721)); } @@ -144,7 +144,7 @@ pub const InputMask = packed struct(u5) { /// 0 will disable round-robin mode but `disableRoundRobin()` is provided so /// the user may be explicit. pub fn round_robin_set(enabled_inputs: InputMask) void { - ADC.CS.modify(.{ .RROBIN = @bitCast(u5, enabled_inputs) }); + ADC.CS.modify(.{ .RROBIN = @as(u5, @bitCast(enabled_inputs)) }); } /// Disable round-robin sample mode. diff --git a/src/hal/clocks.zig b/src/hal/clocks.zig index cb5350f..9a027aa 100644 --- a/src/hal/clocks.zig +++ b/src/hal/clocks.zig @@ -83,7 +83,7 @@ pub const Generator = enum(u32) { assert(24 == @sizeOf([2]GeneratorRegs)); } - const generators = @ptrCast(*volatile [@typeInfo(Generator).Enum.fields.len]GeneratorRegs, CLOCKS); + const generators = @as(*volatile [@typeInfo(Generator).Enum.fields.len]GeneratorRegs, @ptrCast(CLOCKS)); fn get_regs(generator: Generator) *volatile GeneratorRegs { return &generators[@intFromEnum(generator)]; @@ -617,7 +617,7 @@ pub const Configuration = struct { // source frequency has to be faster because dividing will always reduce. assert(input.freq >= output_freq); - const div = @intCast(u32, (@intCast(u64, input.freq) << 8) / output_freq); + const div = @as(u32, @intCast((@as(u64, @intCast(input.freq)) << 8) / output_freq)); // check divisor if (div > generator.get_div()) diff --git a/src/hal/dma.zig b/src/hal/dma.zig index 35b285a..469966f 100644 --- a/src/hal/dma.zig +++ b/src/hal/dma.zig @@ -19,7 +19,7 @@ pub const Dreq = enum(u6) { pub fn channel(n: u4) Channel { assert(n < num_channels); - return @enumFromInt(Channel, n); + return @as(Channel, @enumFromInt(n)); } pub fn claim_unused_channel() ?Channel { @@ -76,7 +76,7 @@ pub const Channel = enum(u4) { }; fn get_regs(chan: Channel) *volatile Regs { - const regs = @ptrCast(*volatile [12]Regs, &DMA.CH0_READ_ADDR); + const regs = @as(*volatile [12]Regs, @ptrCast(&DMA.CH0_READ_ADDR)); return ®s[@intFromEnum(chan)]; } diff --git a/src/hal/flash.zig b/src/hal/flash.zig index cb86c72..7ce3206 100644 --- a/src/hal/flash.zig +++ b/src/hal/flash.zig @@ -23,7 +23,7 @@ pub const boot2 = struct { /// Copy the 2nd stage bootloader into memory pub fn flash_init() linksection(".time_critical") void { if (copyout_valid) return; - const bootloader = @ptrFromInt([*]u32, XIP_BASE); + const bootloader = @as([*]u32, @ptrFromInt(XIP_BASE)); var i: usize = 0; while (i < BOOT2_SIZE_BYTES) : (i += 1) { copyout[i] = bootloader[i]; diff --git a/src/hal/gpio.zig b/src/hal/gpio.zig index 3b9e5ae..dffe3d1 100644 --- a/src/hal/gpio.zig +++ b/src/hal/gpio.zig @@ -72,11 +72,11 @@ pub fn num(n: u5) Pin { if (n > 29) @panic("the RP2040 only has GPIO 0-29"); - return @enumFromInt(Pin, n); + return @as(Pin, @enumFromInt(n)); } pub fn mask(m: u32) Mask { - return @enumFromInt(Mask, m); + return @as(Mask, @enumFromInt(m)); } pub const Mask = enum(u30) { @@ -85,7 +85,7 @@ pub const Mask = enum(u30) { pub fn set_function(self: Mask, function: Function) void { const raw_mask = @intFromEnum(self); for (0..@bitSizeOf(Mask)) |i| { - const bit = @intCast(u5, i); + const bit = @as(u5, @intCast(i)); if (0 != raw_mask & (@as(u32, 1) << bit)) num(bit).set_function(function); } @@ -102,7 +102,7 @@ pub const Mask = enum(u30) { pub fn set_pull(self: Mask, pull: ?Pull) void { const raw_mask = @intFromEnum(self); for (0..@bitSizeOf(Mask)) |i| { - const bit = @intCast(u5, i); + const bit = @as(u5, @intCast(i)); if (0 != raw_mask & (@as(u32, 1) << bit)) num(bit).set_pull(pull); } @@ -154,12 +154,12 @@ pub const Pin = enum(u5) { pub const PadsReg = @TypeOf(PADS_BANK0.GPIO0); fn get_regs(gpio: Pin) *volatile Regs { - const regs = @ptrCast(*volatile [30]Regs, &IO_BANK0.GPIO0_STATUS); + const regs = @as(*volatile [30]Regs, @ptrCast(&IO_BANK0.GPIO0_STATUS)); return ®s[@intFromEnum(gpio)]; } fn get_pads_reg(gpio: Pin) *volatile PadsReg { - const regs = @ptrCast(*volatile [30]PadsReg, &PADS_BANK0.GPIO0); + const regs = @as(*volatile [30]PadsReg, @ptrCast(&PADS_BANK0.GPIO0)); return ®s[@intFromEnum(gpio)]; } diff --git a/src/hal/hw.zig b/src/hal/hw.zig index 28a1981..8227027 100644 --- a/src/hal/hw.zig +++ b/src/hal/hw.zig @@ -20,27 +20,27 @@ const set_bits = @as(u32, 0x2) << 12; const clear_bits = @as(u32, 0x3) << 12; pub fn clear_alias_raw(ptr: anytype) *volatile u32 { - return @ptrFromInt(*volatile u32, @intFromPtr(ptr) | clear_bits); + return @as(*volatile u32, @ptrFromInt(@intFromPtr(ptr) | clear_bits)); } pub fn set_alias_raw(ptr: anytype) *volatile u32 { - return @ptrFromInt(*volatile u32, @intFromPtr(ptr) | set_bits); + return @as(*volatile u32, @ptrFromInt(@intFromPtr(ptr) | set_bits)); } pub fn xor_alias_raw(ptr: anytype) *volatile u32 { - return @ptrFromInt(*volatile u32, @intFromPtr(ptr) | xor_bits); + return @as(*volatile u32, @ptrFromInt(@intFromPtr(ptr) | xor_bits)); } pub fn clear_alias(ptr: anytype) @TypeOf(ptr) { - return @ptrFromInt(@TypeOf(ptr), @intFromPtr(ptr) | clear_bits); + return @as(@TypeOf(ptr), @ptrFromInt(@intFromPtr(ptr) | clear_bits)); } pub fn set_alias(ptr: anytype) @TypeOf(ptr) { - return @ptrFromInt(@TypeOf(ptr), @intFromPtr(ptr) | set_bits); + return @as(@TypeOf(ptr), @ptrFromInt(@intFromPtr(ptr) | set_bits)); } pub fn xor_alias(ptr: anytype) @TypeOf(ptr) { - return @ptrFromInt(@TypeOf(ptr), @intFromPtr(ptr) | xor_bits); + return @as(@TypeOf(ptr), @ptrFromInt(@intFromPtr(ptr) | xor_bits)); } pub inline fn tight_loop_contents() void { diff --git a/src/hal/i2c.zig b/src/hal/i2c.zig index cc7467e..b000e35 100644 --- a/src/hal/i2c.zig +++ b/src/hal/i2c.zig @@ -20,14 +20,14 @@ pub const Config = struct { }; pub fn num(n: u1) I2C { - return @enumFromInt(I2C, n); + return @as(I2C, @enumFromInt(n)); } pub const Address = enum(u7) { _, pub fn new(addr: u7) Address { - var a = @enumFromInt(Address, addr); + var a = @as(Address, @enumFromInt(addr)); std.debug.assert(!a.is_reserved()); return a; } @@ -167,10 +167,10 @@ pub const I2C = enum(u1) { // Always use "fast" mode (<= 400 kHz, works fine for standard mode too) regs.IC_CON.modify(.{ .SPEED = .{ .value = .FAST } }); - regs.IC_FS_SCL_HCNT.write(.{ .IC_FS_SCL_HCNT = @intCast(u16, hcnt), .padding = 0 }); - regs.IC_FS_SCL_LCNT.write(.{ .IC_FS_SCL_LCNT = @intCast(u16, lcnt), .padding = 0 }); - regs.IC_FS_SPKLEN.write(.{ .IC_FS_SPKLEN = if (lcnt < 16) 1 else @intCast(u8, lcnt / 16), .padding = 0 }); - regs.IC_SDA_HOLD.modify(.{ .IC_SDA_TX_HOLD = @intCast(u16, sda_tx_hold_count) }); + regs.IC_FS_SCL_HCNT.write(.{ .IC_FS_SCL_HCNT = @as(u16, @intCast(hcnt)), .padding = 0 }); + regs.IC_FS_SCL_LCNT.write(.{ .IC_FS_SCL_LCNT = @as(u16, @intCast(lcnt)), .padding = 0 }); + regs.IC_FS_SPKLEN.write(.{ .IC_FS_SPKLEN = if (lcnt < 16) 1 else @as(u8, @intCast(lcnt / 16)), .padding = 0 }); + regs.IC_SDA_HOLD.modify(.{ .IC_SDA_TX_HOLD = @as(u16, @intCast(sda_tx_hold_count)) }); i2c.enable(); @@ -343,7 +343,7 @@ pub const I2C = enum(u1) { } const abort_reason = regs.IC_TX_ABRT_SOURCE.read(); - if (@bitCast(u32, abort_reason) != 0) { + if (@as(u32, @bitCast(abort_reason)) != 0) { // Note clearing the abort flag also clears the reason, and // this instance of flag is clear-on-read! Note also the // IC_CLR_TX_ABRT register always reads as 0. diff --git a/src/hal/multicore.zig b/src/hal/multicore.zig index bc2b4de..1ca3ebb 100644 --- a/src/hal/multicore.zig +++ b/src/hal/multicore.zig @@ -70,7 +70,7 @@ pub fn launch_core1_with_stack(entrypoint: *const fn () void, stack: []u32) void fn wrapper(_: u32, _: u32, _: u32, _: u32, entry: u32, stack_base: [*]u32) callconv(.C) void { // TODO: protect stack using MPU _ = stack_base; - @ptrFromInt(*const fn () void, entry)(); + @as(*const fn () void, @ptrFromInt(entry))(); } }.wrapper; diff --git a/src/hal/pins.zig b/src/hal/pins.zig index 5de0810..3ca3238 100644 --- a/src/hal/pins.zig +++ b/src/hal/pins.zig @@ -362,13 +362,13 @@ pub fn Pins(comptime config: GlobalConfiguration) type { } else if (pin_config.function.is_adc()) { pin_field.name = pin_config.name orelse @tagName(pin_config.function); pin_field.type = adc.Input; - pin_field.default_value = @ptrCast(?*const anyopaque, switch (pin_config.function) { + pin_field.default_value = @as(?*const anyopaque, @ptrCast(switch (pin_config.function) { .ADC0 => &adc.Input.ain0, .ADC1 => &adc.Input.ain1, .ADC2 => &adc.Input.ain2, .ADC3 => &adc.Input.ain3, else => unreachable, - }); + })); } else { continue; } @@ -536,7 +536,7 @@ pub const GlobalConfiguration = struct { var ret: Pins(config) = undefined; inline for (@typeInfo(Pins(config)).Struct.fields) |field| { if (field.default_value) |default_value| { - @field(ret, field.name) = @ptrCast(*const field.field_type, default_value).*; + @field(ret, field.name) = @as(*const field.field_type, @ptrCast(default_value)).*; } else { @field(ret, field.name) = .{}; } diff --git a/src/hal/pio.zig b/src/hal/pio.zig index 6b7ca1f..9472728 100644 --- a/src/hal/pio.zig +++ b/src/hal/pio.zig @@ -74,10 +74,10 @@ pub const ClkDivOptions = struct { frac: u8 = 0, pub fn from_float(div: f32) ClkDivOptions { - const fixed = @intFromFloat(u24, div * 256); + const fixed = @as(u24, @intFromFloat(div * 256)); return ClkDivOptions{ - .int = @truncate(u16, fixed >> 8), - .frac = @truncate(u8, fixed), + .int = @as(u16, @truncate(fixed >> 8)), + .frac = @as(u8, @truncate(fixed)), }; } }; @@ -154,7 +154,7 @@ pub const Pio = enum(u1) { pub fn get_instruction_memory(self: Pio) *volatile [32]u32 { const regs = self.get_regs(); - return @ptrCast(*volatile [32]u32, ®s.INSTR_MEM0); + return @as(*volatile [32]u32, @ptrCast(®s.INSTR_MEM0)); } pub fn gpio_init(self: Pio, pin: gpio.Pin) void { @@ -184,7 +184,7 @@ pub const Pio = enum(u1) { else error.NoSpace else for (0..(32 - program.instructions.len)) |i| { - const offset = @intCast(u5, i); + const offset = @as(u5, @intCast(i)); if (self.can_add_program_at_offset(program, offset)) break offset; } else error.NoSpace; @@ -218,23 +218,23 @@ pub const Pio = enum(u1) { const claimed_mask = claimed_state_machines[@intFromEnum(self)]; return for (0..4) |i| { - const sm_mask = (@as(u4, 1) << @intCast(u2, i)); + const sm_mask = (@as(u4, 1) << @as(u2, @intCast(i))); if (0 == (claimed_mask & sm_mask)) { claimed_state_machines[@intFromEnum(self)] |= sm_mask; - break @enumFromInt(StateMachine, i); + break @as(StateMachine, @enumFromInt(i)); } } else error.NoSpace; } pub fn get_sm_regs(self: Pio, sm: StateMachine) *volatile StateMachine.Regs { const pio_regs = self.get_regs(); - const sm_regs = @ptrCast(*volatile [4]StateMachine.Regs, &pio_regs.SM0_CLKDIV); + const sm_regs = @as(*volatile [4]StateMachine.Regs, @ptrCast(&pio_regs.SM0_CLKDIV)); return &sm_regs[@intFromEnum(sm)]; } fn get_irq_regs(self: Pio, irq: Irq) *volatile Irq.Regs { const pio_regs = self.get_regs(); - const irq_regs = @ptrCast(*volatile [2]Irq.Regs, &pio_regs.IRQ0_INTE); + const irq_regs = @as(*volatile [2]Irq.Regs, @ptrCast(&pio_regs.IRQ0_INTE)); return &irq_regs[@intFromEnum(irq)]; } @@ -313,7 +313,7 @@ pub const Pio = enum(u1) { pub fn sm_get_tx_fifo(self: Pio, sm: StateMachine) *volatile u32 { const regs = self.get_regs(); - const fifos = @ptrCast(*volatile [4]u32, ®s.TXF0); + const fifos = @as(*volatile [4]u32, @ptrCast(®s.TXF0)); return &fifos[@intFromEnum(sm)]; } @@ -386,7 +386,7 @@ pub const Pio = enum(u1) { const regs = self.get_regs(); const levels = regs.FLEVEL.raw; - return @truncate(u4, levels >> (@as(u5, 4) * num) + offset); + return @as(u4, @truncate(levels >> (@as(u5, 4) * num) + offset)); } fn interrupt_bit_pos( @@ -469,7 +469,7 @@ pub const Pio = enum(u1) { pub fn sm_exec(self: Pio, sm: StateMachine, instruction: Instruction) void { const sm_regs = self.get_sm_regs(sm); - sm_regs.instr.raw = @bitCast(u16, instruction); + sm_regs.instr.raw = @as(u16, @bitCast(instruction)); } pub fn sm_load_and_start_program( @@ -498,7 +498,7 @@ pub const Pio = enum(u1) { .wrap = if (program.wrap) |wrap| wrap else - offset + @intCast(u5, program.instructions.len), + offset + @as(u5, @intCast(program.instructions.len)), .wrap_target = if (program.wrap_target) |wrap_target| wrap_target diff --git a/src/hal/pio/assembler.zig b/src/hal/pio/assembler.zig index 1c38816..6c6be6f 100644 --- a/src/hal/pio/assembler.zig +++ b/src/hal/pio/assembler.zig @@ -22,7 +22,7 @@ pub const Program = struct { wrap: ?u5, pub fn get_mask(program: Program) u32 { - return (@as(u32, 1) << @intCast(u5, program.instructions.len)) - 1; + return (@as(u32, 1) << @as(u5, @intCast(program.instructions.len))) - 1; } }; diff --git a/src/hal/pio/assembler/Expression.zig b/src/hal/pio/assembler/Expression.zig index 794b5c3..789fb78 100644 --- a/src/hal/pio/assembler/Expression.zig +++ b/src/hal/pio/assembler/Expression.zig @@ -123,7 +123,7 @@ fn trim_outer_parenthesis(str: []const u8) TrimResult { return TrimResult{ .str = str[start..end], - .index = @intCast(u32, start), + .index = @as(u32, @intCast(start)), }; } @@ -142,9 +142,9 @@ fn recursive_tokenize( var parenthesis_found = false; var depth: u32 = 0; - var i = @intCast(i32, expr_str.len - 1); + var i = @as(i32, @intCast(expr_str.len - 1)); outer: while (i >= 0) : (i -= 1) { - const idx = @intCast(u32, i); + const idx = @as(u32, @intCast(i)); // TODO: how about if the expression is fully enveloped in parenthesis? switch (expr_str[idx]) { ')' => { @@ -176,7 +176,7 @@ fn recursive_tokenize( const is_negative = (i == 0) or is_negative: { var j = i - 1; while (j >= 0) : (j -= 1) { - const jdx = @intCast(u32, j); + const jdx = @as(u32, @intCast(j)); switch (expr_str[jdx]) { ' ', '\t' => continue, '+', '-', '*', '/' => continue :outer, @@ -229,7 +229,7 @@ fn recursive_tokenize( } else { // if we hit this path, then the full string has been scanned, and no operators const trimmed = std.mem.trim(u8, expr_str, " \t"); - const value_index = expr_index + @intCast(u32, std.mem.indexOf(u8, expr_str, trimmed).?); + const value_index = expr_index + @as(u32, @intCast(std.mem.indexOf(u8, expr_str, trimmed).?)); try ops.append(.{ .op = .value, .index = value_index, @@ -242,7 +242,7 @@ fn recursive_tokenize( } if (depth != 0) { - diags.* = Diagnostics.init(expr_index + @intCast(u32, i), "mismatched parenthesis", .{}); + diags.* = Diagnostics.init(expr_index + @as(u32, @intCast(i)), "mismatched parenthesis", .{}); return error.MismatchedParenthesis; } } @@ -349,7 +349,7 @@ fn recursive_evaluate( } break :blk .{ - .value = @bitCast(i128, @bitReverse(@bitCast(u128, values[0].num)) >> (128 - 32)), + .value = @as(i128, @bitCast(@bitReverse(@as(u128, @bitCast(values[0].num))) >> (128 - 32))), .index = values[0].index, .consumed = .{ .ops = 2, diff --git a/src/hal/pio/assembler/encoder.zig b/src/hal/pio/assembler/encoder.zig index 502b374..0d13c1b 100644 --- a/src/hal/pio/assembler/encoder.zig +++ b/src/hal/pio/assembler/encoder.zig @@ -87,13 +87,13 @@ pub fn Encoder(comptime options: Options) type { std.mem.copy(u8, &define_name, define.name); tmp.append(.{ .name = &define_name, - .value = @intCast(i64, define.value), + .value = @as(i64, @intCast(define.value)), }) catch unreachable; } break :blk tmp.slice(); }, - .instructions = @ptrCast([]const u16, bounded.instructions.slice()), + .instructions = @as([]const u16, @ptrCast(bounded.instructions.slice())), .origin = bounded.origin, .side_set = bounded.side_set, .wrap_target = bounded.wrap_target, @@ -152,7 +152,7 @@ pub fn Encoder(comptime options: Options) type { diags: *?Diagnostics, ) !T { return switch (value) { - .integer => |int| @intCast(T, int), + .integer => |int| @as(T, @intCast(int)), .string => |str| outer: for (define_lists) |defines| { for (defines) |define| { if (std.mem.eql(u8, str, define.name)) { @@ -166,7 +166,7 @@ pub fn Encoder(comptime options: Options) type { break :outer error.TooBig; } - break :outer @intCast(T, define.value); + break :outer @as(T, @intCast(define.value)); } } } else { @@ -189,7 +189,7 @@ pub fn Encoder(comptime options: Options) type { ); } - return @intCast(T, result); + return @as(T, @intCast(result)); }, }; } @@ -482,19 +482,19 @@ pub fn Encoder(comptime options: Options) type { switch (token.data) { .instruction => |instr| try self.encode_instruction(program, instr, token.index, diags), .word => |word| try program.instructions.append( - @bitCast(Instruction, try self.evaluate(u16, program.*, word, token.index, diags)), + @as(Instruction, @bitCast(try self.evaluate(u16, program.*, word, token.index, diags))), ), // already processed .label, .wrap_target, .wrap => {}, .program => { - self.index = @intCast(u32, i); + self.index = @as(u32, @intCast(i)); break; }, else => unreachable, // invalid } } else if (self.tokens.len > 0) - self.index = @intCast(u32, self.tokens.len); + self.index = @as(u32, @intCast(self.tokens.len)); } fn encode_program(self: *Self, diags: *?Diagnostics) !?BoundedProgram { diff --git a/src/hal/pio/assembler/tokenizer.zig b/src/hal/pio/assembler/tokenizer.zig index 29c596d..2aef428 100644 --- a/src/hal/pio/assembler/tokenizer.zig +++ b/src/hal/pio/assembler/tokenizer.zig @@ -199,7 +199,7 @@ pub const Tokenizer = struct { fn consume_peek(self: *Tokenizer, result: PeekResult) void { assert(self.index <= result.start); - self.index = result.start + @intCast(u32, result.str.len); + self.index = result.start + @as(u32, @intCast(result.str.len)); } /// gets next arg without consuming the stream @@ -616,7 +616,7 @@ pub const Tokenizer = struct { const bit_count = if (bit_count_tmp == 32) @as(u5, 0) else - @intCast(u5, bit_count_tmp); + @as(u5, @intCast(bit_count_tmp)); return Token.Instruction.Payload{ .in = .{ @@ -635,7 +635,7 @@ pub const Tokenizer = struct { const bit_count = if (bit_count_tmp == 32) @as(u5, 0) else - @intCast(u5, bit_count_tmp); + @as(u5, @intCast(bit_count_tmp)); return Token.Instruction.Payload{ .out = .{ @@ -1644,7 +1644,7 @@ test "tokenize.instr.in" { try expect_instr_in(.{ .source = @field(Token.Instruction.In.Source, source), - .bit_count = @intCast(u5, bit_count), + .bit_count = @as(u5, @intCast(bit_count)), }, tokens.get(0)); } } @@ -1667,7 +1667,7 @@ test "tokenize.instr.out" { try expect_instr_out(.{ .destination = @field(Token.Instruction.Out.Destination, destination), - .bit_count = @intCast(u5, bit_count), + .bit_count = @as(u5, @intCast(bit_count)), }, tokens.get(0)); } } diff --git a/src/hal/pwm.zig b/src/hal/pwm.zig index 6e5fe1d..82c3756 100644 --- a/src/hal/pwm.zig +++ b/src/hal/pwm.zig @@ -10,7 +10,7 @@ fn get_regs(comptime slice: u32) *volatile Regs { @import("std").debug.assert(slice < 8); const PwmType = microzig.chip.types.peripherals.PWM; const reg_diff = comptime @offsetOf(PwmType, "CH1_CSR") - @offsetOf(PwmType, "CH0_CSR"); - return @ptrFromInt(*volatile Regs, @intFromPtr(PWM) + reg_diff * slice); + return @as(*volatile Regs, @ptrFromInt(@intFromPtr(PWM) + reg_diff * slice)); } pub fn Pwm(comptime slice_num: u32, comptime chan: Channel) type { diff --git a/src/hal/random.zig b/src/hal/random.zig index 3a292a6..b7f102f 100644 --- a/src/hal/random.zig +++ b/src/hal/random.zig @@ -76,10 +76,10 @@ pub const Ascon = struct { var i: usize = 0; while (i < buffer.len) : (i += 1) { // We poll RANDOMBIT eight times per cycle to build a random byte - var r: u8 = @intCast(u8, peripherals.ROSC.RANDOMBIT.read().RANDOMBIT); + var r: u8 = @as(u8, @intCast(peripherals.ROSC.RANDOMBIT.read().RANDOMBIT)); var j: usize = 0; while (j < 7) : (j += 1) { - r = (r << 1) | @intCast(u8, peripherals.ROSC.RANDOMBIT.read().RANDOMBIT); + r = (r << 1) | @as(u8, @intCast(peripherals.ROSC.RANDOMBIT.read().RANDOMBIT)); } buffer[i] = r; } diff --git a/src/hal/resets.zig b/src/hal/resets.zig index a35ad79..3bffa6f 100644 --- a/src/hal/resets.zig +++ b/src/hal/resets.zig @@ -36,7 +36,7 @@ pub const Mask = packed struct(u32) { }; pub fn reset(mask: Mask) void { - const raw_mask = @bitCast(u32, mask); + const raw_mask = @as(u32, @bitCast(mask)); RESETS.RESET.raw = raw_mask; RESETS.RESET.raw = 0; @@ -45,22 +45,22 @@ pub fn reset(mask: Mask) void { } pub inline fn reset_block(mask: Mask) void { - hw.set_alias_raw(&RESETS.RESET).* = @bitCast(u32, mask); + hw.set_alias_raw(&RESETS.RESET).* = @as(u32, @bitCast(mask)); } pub inline fn unreset_block(mask: Mask) void { - hw.clear_alias_raw(&RESETS.RESET).* = @bitCast(u32, mask); + hw.clear_alias_raw(&RESETS.RESET).* = @as(u32, @bitCast(mask)); } pub fn unreset_block_wait(mask: Mask) void { - const raw_mask = @bitCast(u32, mask); + const raw_mask = @as(u32, @bitCast(mask)); hw.clear_alias_raw(&RESETS.RESET).* = raw_mask; // have to bitcast after a read() instead of `RESETS.RESET_DONE.raw` due to // some optimization bug. While loops will not be optimzed away if the // condition has side effects like dereferencing a volatile pointer. // It seems that volatile is not propagating correctly. - while (@bitCast(u32, RESETS.RESET_DONE.read()) & raw_mask != raw_mask) {} + while (@as(u32, @bitCast(RESETS.RESET_DONE.read())) & raw_mask != raw_mask) {} } pub const masks = struct { diff --git a/src/hal/rom.zig b/src/hal/rom.zig index 7b25387..a398074 100644 --- a/src/hal/rom.zig +++ b/src/hal/rom.zig @@ -88,7 +88,7 @@ pub const signatures = struct { /// /// A 32 bit address pointing into bootrom pub fn rom_table_code(c1: u8, c2: u8) u32 { - return @intCast(u32, c1) | (@intCast(u32, c2) << 8); + return @as(u32, @intCast(c1)) | (@as(u32, @intCast(c2)) << 8); } /// Convert a 16 bit pointer stored at the given rom address into a pointer @@ -100,8 +100,8 @@ pub fn rom_table_code(c1: u8, c2: u8) u32 { /// /// The converted pointer pub inline fn rom_hword_as_ptr(rom_addr: u32) *anyopaque { - const ptr_to_ptr = @ptrFromInt(*u16, rom_addr); - return @ptrFromInt(*anyopaque, @intCast(usize, ptr_to_ptr.*)); + const ptr_to_ptr = @as(*u16, @ptrFromInt(rom_addr)); + return @as(*anyopaque, @ptrFromInt(@as(usize, @intCast(ptr_to_ptr.*)))); } /// Lookup a bootrom function by code (inline) @@ -113,8 +113,8 @@ pub inline fn rom_hword_as_ptr(rom_addr: u32) *anyopaque { /// /// A anyopaque pointer to the function; must be cast by the caller pub inline fn _rom_func_lookup(code: Code) *anyopaque { - const rom_table_lookup = @ptrCast(*signatures.rom_table_lookup, rom_hword_as_ptr(0x18)); - const func_table = @ptrCast(*u16, @alignCast(2, rom_hword_as_ptr(0x14))); + const rom_table_lookup = @as(*signatures.rom_table_lookup, @ptrCast(rom_hword_as_ptr(0x18))); + const func_table = @as(*u16, @ptrCast(@alignCast(rom_hword_as_ptr(0x14)))); return rom_table_lookup(func_table, @intFromEnum(code)); } @@ -140,7 +140,7 @@ pub fn popcount32(value: u32) u32 { var f: ?*signatures.popcount32 = null; }; - if (S.f == null) S.f = @ptrCast(*signatures.popcount32, _rom_func_lookup(Code.popcount32)); + if (S.f == null) S.f = @as(*signatures.popcount32, @ptrCast(_rom_func_lookup(Code.popcount32))); return S.f.?(value); } @@ -150,7 +150,7 @@ pub fn reverse32(value: u32) u32 { var f: ?*signatures.reverse32 = null; }; - if (S.f == null) S.f = @ptrCast(*signatures.reverse32, _rom_func_lookup(Code.reverse32)); + if (S.f == null) S.f = @as(*signatures.reverse32, @ptrCast(_rom_func_lookup(Code.reverse32))); return S.f.?(value); } @@ -160,7 +160,7 @@ pub fn clz32(value: u32) u32 { var f: ?*signatures.clz32 = null; }; - if (S.f == null) S.f = @ptrCast(*signatures.clz32, _rom_func_lookup(Code.clz32)); + if (S.f == null) S.f = @as(*signatures.clz32, @ptrCast(_rom_func_lookup(Code.clz32))); return S.f.?(value); } @@ -170,7 +170,7 @@ pub fn ctz32(value: u32) u32 { var f: ?*signatures.ctz32 = null; }; - if (S.f == null) S.f = @ptrCast(*signatures.ctz32, _rom_func_lookup(Code.ctz32)); + if (S.f == null) S.f = @as(*signatures.ctz32, @ptrCast(_rom_func_lookup(Code.ctz32))); return S.f.?(value); } @@ -184,7 +184,7 @@ pub fn memset(dest: []u8, c: u8) []u8 { var f: ?*signatures.memset = null; }; - if (S.f == null) S.f = @ptrCast(*signatures.memset, _rom_func_lookup(Code.memset)); + if (S.f == null) S.f = @as(*signatures.memset, @ptrCast(_rom_func_lookup(Code.memset))); return S.f.?(dest.ptr, c, dest.len)[0..dest.len]; } @@ -196,7 +196,7 @@ pub fn memcpy(dest: []u8, src: []const u8) []u8 { const n = if (dest.len <= src.len) dest.len else src.len; - if (S.f == null) S.f = @ptrCast(*signatures.memcpy, _rom_func_lookup(Code.memcpy)); + if (S.f == null) S.f = @as(*signatures.memcpy, @ptrCast(_rom_func_lookup(Code.memcpy))); return S.f.?(dest.ptr, src.ptr, n)[0..n]; } @@ -206,9 +206,9 @@ pub fn memcpy(dest: []u8, src: []const u8) []u8 { /// Restore all QSPI pad controls to their default state, and connect the SSI to the QSPI pads pub inline fn connect_internal_flash() *signatures.connect_internal_flash { - return @ptrCast( + return @as( *signatures.connect_internal_flash, - _rom_func_lookup(Code.connect_internal_flash), + @ptrCast(_rom_func_lookup(Code.connect_internal_flash)), ); } @@ -218,9 +218,9 @@ pub inline fn connect_internal_flash() *signatures.connect_internal_flash { /// SSI to XIP mode (e.g. by a call to _flash_flush_cache). This function configures /// the SSI with a fixed SCK clock divisor of /6. pub inline fn flash_exit_xip() *signatures.flash_exit_xip { - return @ptrCast( + return @as( *signatures.flash_exit_xip, - _rom_func_lookup(Code.flash_exit_xip), + @ptrCast(_rom_func_lookup(Code.flash_exit_xip)), ); } @@ -230,9 +230,9 @@ pub inline fn flash_exit_xip() *signatures.flash_exit_xip { /// possible, for much higher erase speed. addr must be aligned to a 4096-byte sector, /// and count must be a multiple of 4096 bytes. pub inline fn flash_range_erase() *signatures.flash_range_erase { - return @ptrCast( + return @as( *signatures.flash_range_erase, - _rom_func_lookup(Code.flash_range_erase), + @ptrCast(_rom_func_lookup(Code.flash_range_erase)), ); } @@ -240,18 +240,18 @@ pub inline fn flash_range_erase() *signatures.flash_range_erase { /// start of flash) and count bytes in size. addr must be aligned to a 256-byte /// boundary, and the length of data must be a multiple of 256. pub inline fn flash_range_program() *signatures.flash_range_program { - return @ptrCast( + return @as( *signatures.flash_range_program, - _rom_func_lookup(Code.flash_range_program), + @ptrCast(_rom_func_lookup(Code.flash_range_program)), ); } /// Flush and enable the XIP cache. Also clears the IO forcing on QSPI CSn, so that /// the SSI can drive the flash chip select as normal. pub inline fn flash_flush_cache() *signatures.flash_flush_cache { - return @ptrCast( + return @as( *signatures.flash_flush_cache, - _rom_func_lookup(Code.flash_flush_cache), + @ptrCast(_rom_func_lookup(Code.flash_flush_cache)), ); } @@ -262,8 +262,8 @@ pub inline fn flash_flush_cache() *signatures.flash_flush_cache { /// visible to the debug host, without having to know exactly what kind of flash /// device is connected. pub inline fn flash_enter_cmd_xip() *signatures.flash_enter_cmd_xip { - return @ptrCast( + return @as( *signatures.flash_enter_cmd_xip, - _rom_func_lookup(Code.flash_enter_cmd_xip), + @ptrCast(_rom_func_lookup(Code.flash_enter_cmd_xip)), ); } diff --git a/src/hal/spi.zig b/src/hal/spi.zig index 251b61b..b9b8891 100644 --- a/src/hal/spi.zig +++ b/src/hal/spi.zig @@ -22,7 +22,7 @@ pub const Config = struct { }; pub fn num(n: u1) SPI { - return @enumFromInt(SPI, n); + return @as(SPI, @enumFromInt(n)); } pub const SPI = enum(u1) { @@ -161,8 +161,8 @@ pub const SPI = enum(u1) { while (postdiv > 1) : (postdiv -= 1) { if (freq_in / (prescale * (postdiv - 1)) > baudrate) break; } - spi_regs.SSPCPSR.modify(.{ .CPSDVSR = @intCast(u8, prescale) }); - spi_regs.SSPCR0.modify(.{ .SCR = @intCast(u8, postdiv - 1) }); + spi_regs.SSPCPSR.modify(.{ .CPSDVSR = @as(u8, @intCast(prescale)) }); + spi_regs.SSPCR0.modify(.{ .SCR = @as(u8, @intCast(postdiv - 1)) }); // Return the frequency we were able to achieve return freq_in / (prescale * postdiv); diff --git a/src/hal/time.zig b/src/hal/time.zig index 95691cc..6bf6932 100644 --- a/src/hal/time.zig +++ b/src/hal/time.zig @@ -7,7 +7,7 @@ pub const Absolute = enum(u64) { _, pub fn from_us(us: u64) Absolute { - return @enumFromInt(Absolute, us); + return @as(Absolute, @enumFromInt(us)); } pub fn to_us(time: Absolute) u64 { @@ -36,7 +36,7 @@ pub const Duration = enum(u64) { _, pub fn from_us(us: u64) Duration { - return @enumFromInt(Duration, us); + return @as(Duration, @enumFromInt(us)); } pub fn from_ms(ms: u64) Duration { @@ -67,14 +67,14 @@ pub fn get_time_since_boot() Absolute { var low_word = TIMER.TIMERAWL; const next_high_word = TIMER.TIMERAWH; if (next_high_word == high_word) - break @enumFromInt(Absolute, @intCast(u64, high_word) << 32 | low_word); + break @as(Absolute, @enumFromInt(@as(u64, @intCast(high_word)) << 32 | low_word)); high_word = next_high_word; } else unreachable; } pub fn make_timeout_us(timeout_us: u64) Absolute { - return @enumFromInt(Absolute, get_time_since_boot().to_us() + timeout_us); + return @as(Absolute, @enumFromInt(get_time_since_boot().to_us() + timeout_us)); } pub fn sleep_ms(time_ms: u32) void { diff --git a/src/hal/uart.zig b/src/hal/uart.zig index 678d979..5e4162c 100644 --- a/src/hal/uart.zig +++ b/src/hal/uart.zig @@ -43,7 +43,7 @@ pub const Config = struct { }; pub fn num(n: u1) UART { - return @enumFromInt(UART, n); + return @as(UART, @enumFromInt(n)); } pub const UART = enum(u1) { @@ -118,7 +118,7 @@ pub const UART = enum(u1) { pub fn tx_fifo(uart: UART) *volatile u32 { const regs = uart.get_regs(); - return @ptrCast(*volatile u32, ®s.UARTDR); + return @as(*volatile u32, @ptrCast(®s.UARTDR)); } pub fn dreq_tx(uart: UART) dma.Dreq { @@ -180,7 +180,7 @@ pub const UART = enum(u1) { assert(baud_rate > 0); const uart_regs = uart.get_regs(); const baud_rate_div = (8 * peri_freq / baud_rate); - var baud_ibrd = @intCast(u16, baud_rate_div >> 7); + var baud_ibrd = @as(u16, @intCast(baud_rate_div >> 7)); const baud_fbrd: u6 = if (baud_ibrd == 0) baud_fbrd: { baud_ibrd = 1; @@ -188,7 +188,7 @@ pub const UART = enum(u1) { } else if (baud_ibrd >= 65535) baud_fbrd: { baud_ibrd = 65535; break :baud_fbrd 0; - } else @intCast(u6, ((@truncate(u7, baud_rate_div)) + 1) / 2); + } else @as(u6, @intCast(((@as(u7, @truncate(baud_rate_div))) + 1) / 2)); uart_regs.UARTIBRD.write(.{ .BAUD_DIVINT = baud_ibrd, .padding = 0 }); uart_regs.UARTFBRD.write(.{ .BAUD_DIVFRAC = baud_fbrd, .padding = 0 }); diff --git a/src/hal/usb.zig b/src/hal/usb.zig index 60a9f69..3177047 100644 --- a/src/hal/usb.zig +++ b/src/hal/usb.zig @@ -42,7 +42,7 @@ pub const utf8ToUtf16Le = usb.utf8Toutf16Le; pub var EP0_OUT_CFG: usb.EndpointConfiguration = .{ .descriptor = &usb.EndpointDescriptor{ - .length = @intCast(u8, @sizeOf(usb.EndpointDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.EndpointDescriptor))), .descriptor_type = usb.DescType.Endpoint, .endpoint_address = usb.EP0_OUT_ADDR, .attributes = @intFromEnum(usb.TransferType.Control), @@ -57,7 +57,7 @@ pub var EP0_OUT_CFG: usb.EndpointConfiguration = .{ pub var EP0_IN_CFG: usb.EndpointConfiguration = .{ .descriptor = &usb.EndpointDescriptor{ - .length = @intCast(u8, @sizeOf(usb.EndpointDescriptor)), + .length = @as(u8, @intCast(@sizeOf(usb.EndpointDescriptor))), .descriptor_type = usb.DescType.Endpoint, .endpoint_address = usb.EP0_IN_ADDR, .attributes = @intFromEnum(usb.TransferType.Control), @@ -89,26 +89,26 @@ pub const buffers = struct { /// Mapping to the different data buffers in DPSRAM pub var B: usb.Buffers = .{ - .ep0_buffer0 = @ptrFromInt([*]u8, USB_EP0_BUFFER0), - .ep0_buffer1 = @ptrFromInt([*]u8, USB_EP0_BUFFER1), + .ep0_buffer0 = @as([*]u8, @ptrFromInt(USB_EP0_BUFFER0)), + .ep0_buffer1 = @as([*]u8, @ptrFromInt(USB_EP0_BUFFER1)), // We will initialize this comptime in a loop .rest = .{ - @ptrFromInt([*]u8, USB_BUFFERS + (0 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (1 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (2 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (3 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (4 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (5 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (6 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (7 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (8 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (9 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (10 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (11 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (12 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (13 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (14 * BUFFER_SIZE)), - @ptrFromInt([*]u8, USB_BUFFERS + (15 * BUFFER_SIZE)), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (0 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (1 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (2 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (3 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (4 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (5 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (6 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (7 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (8 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (9 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (10 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (11 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (12 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (13 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (14 * BUFFER_SIZE))), + @as([*]u8, @ptrFromInt(USB_BUFFERS + (15 * BUFFER_SIZE))), }, }; }; @@ -278,7 +278,7 @@ pub const F = struct { // The offset _should_ fit in a u16, but if we've gotten something // wrong in the past few lines, a common symptom will be integer // overflow producing a Very Large Number, - const dpram_offset = @intCast(u16, buf_base - dpram_base); + const dpram_offset = @as(u16, @intCast(buf_base - dpram_base)); // Configure the endpoint! modify_endpoint_control(epci, .{ @@ -287,7 +287,7 @@ pub const F = struct { // buffer is done, thx. .INTERRUPT_PER_BUFF = 1, // Select bulk vs control (or interrupt as soon as implemented). - .ENDPOINT_TYPE = .{ .raw = @intCast(u2, ep.descriptor.attributes) }, + .ENDPOINT_TYPE = .{ .raw = @as(u2, @intCast(ep.descriptor.attributes)) }, // And, designate our buffer by its offset. .BUFFER_ADDRESS = dpram_offset, }); @@ -331,7 +331,7 @@ pub const F = struct { modify_buffer_control(ep.buffer_control_index, .{ .PID_0 = np, // DATA0/1, depending .FULL_0 = 1, // We have put data in - .LENGTH_0 = @intCast(u10, buffer.len), // There are this many bytes + .LENGTH_0 = @as(u10, @intCast(buffer.len)), // There are this many bytes }); // Nop for some clock cycles @@ -367,7 +367,7 @@ pub const F = struct { .PID_0 = np, // DATA0/1 depending .FULL_0 = 0, // Buffer is NOT full, we want the computer to fill it .AVAILABLE_0 = 1, // It is, however, available to be filled - .LENGTH_0 = @intCast(u10, len), // Up tho this many bytes + .LENGTH_0 = @as(u10, @intCast(len)), // Up tho this many bytes }); // Flip the DATA0/1 PID for the next receive @@ -588,14 +588,14 @@ pub fn next(self: *usb.EPBIter) ?usb.EPB { var lowbit_index: u5 = 0; while ((self.bufbits >> lowbit_index) & 0x01 == 0) : (lowbit_index += 1) {} // Remove their bit from our set. - const lowbit = @intCast(u32, 1) << lowbit_index; + const lowbit = @as(u32, @intCast(1)) << lowbit_index; self.last_bit = lowbit; self.bufbits ^= lowbit; // Here we exploit knowledge of the ordering of buffer control // registers in the peripheral. Each endpoint has a pair of // registers, so we can determine the endpoint number by: - const epnum = @intCast(u8, lowbit_index >> 1); + const epnum = @as(u8, @intCast(lowbit_index >> 1)); // Of the pair, the IN endpoint comes first, followed by OUT, so // we can get the direction by: const dir = if (lowbit_index & 1 == 0) usb.Dir.In else usb.Dir.Out; @@ -637,7 +637,7 @@ pub fn next(self: *usb.EPBIter) ?usb.EPB { // Get the actual length of the data, which may be less // than the buffer size. - const len = @intCast(usize, bc & 0x3ff); + const len = @as(usize, @intCast(bc & 0x3ff)); // Copy the data from SRAM return usb.EPB{