langref: reword some packed struct text and example

This commit is contained in:
Andrew Kelley
2025-04-11 14:16:57 -07:00
parent a8621731ec
commit bd38c417fc
2 changed files with 34 additions and 30 deletions

View File

@@ -1,16 +1,19 @@
pub const GPIORegister = packed struct(u8) {
pub const GpioRegister = packed struct(u8) {
GPIO0: bool,
GPIO1: bool,
GPIO2: bool,
GPIO3: bool,
_reserved: u4 = 0,
reserved: u4 = 0,
};
/// Write a new state to the memory-mapped IO.
pub fn writeToGPIO(new_states: GPIORegister) void {
const gpio_register_address = 0x0123;
const raw_ptr: *align(1) volatile GPIORegister = @ptrFromInt(gpio_register_address);
raw_ptr.* = new_states;
const gpio: *volatile GpioRegister = @ptrFromInt(0x0123);
pub fn writeToGpio(new_states: GpioRegister) void {
// Example of what not to do:
// BAD! gpio.GPIO0 = true; BAD!
// Instead, do this:
gpio.* = new_states;
}
// syntax