Conceptual
Login

Reading and Writing Device Registers Safely

Talking to hardware means reading and writing at fixed offsets inside a mapped window, in exactly the width the device expects, with the compiler forbidden from optimising the accesses away or reordering them. The kernel crate's input and output type provides width-specific operations and checks the offset against the window's size, at compile time when the size is known and at run time through a fallible variant when it is not. That removes the C bug of an offset that walks past the end of the mapping into whatever is next in the address space. It also stops you reading a four-byte register two bytes at a time, which some devices simply refuse.

Questions this Concept answers

  • Why must register accesses not be optimised away or reordered by the compiler?