-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Summary
In 32-bit ARM, peripheral control is achieved through control registers that are mapped into the processor's address space. These control registers must be read/written in 32-bit, 4-byte aligned chunks. Most control registers have multiple fields that can each be modified independently through a read, modify, write cycle.
Desired Features
- Ability to specify each register as big-endian or little-endian. Most ARM processors have peripherals mapped via little-endian words, but I have encountered a processor that had some peripherals mapped as little-endian and other as big-endian. (Sounds like a lazy EE to me...)
- Some way to guarantee that control registers are read/written one word (32-bits) at a time, with proper alignment.
- Ability to read/write multiple fields of a control register at one time. This is usually an efficiency issue as multiple read, modify, write cycles don't usually cause problems. There are a few control registers where multiple fields must be modified during the same write, though.
- Ability to specify that a control register is read-only, write-only, or read-write. Most peripherals have control registers that restrict if reads or writes are performed on them.
Optional Features
Bit-banding support. ARM Cortex-M processors have memory and control registers mapped to two different address ranges. The normal address range is accessed like normal memory. Reading a 32-bit, word-aligned location will load 32-bits into a register; writing a 32-bit, word-aligned location will write 32-bits from a register. The bit-banding address range maps each bit from the normal address range to a 32-bit, word-aligned location. The bit-banded word reads or writes a single bit from the normal address range as either 0x00000000 or 0x00000001. This is useful to avoid a read, modify, write when setting a 1-bit field in a control register.
Summary
Bit fields in Zig seem to be a well thought out feature, but they have some limitations that make it so that they cannot be used for memory-mapped IO on 32-bit ARM processors. All cases where bit fields cannot be used can be done with []u32 and bit masking, though this can be error prone.
Generally, Zig does a good job providing safe(ish) features to write low-level code, but bit fields cannot currently be used for memory mapped IO on 32-bit ARM.