Peripherals as State Machines

The peripherals of a microcontroller can be thought of as set of state machines. For example, the configuration of a simplified GPIO pin could be represented as the following tree of states:

  • Disabled
  • Enabled
    • Configured as Output
      • Output: High
      • Output: Low
    • Configured as Input
      • Input: High Resistance
      • Input: Pulled Low
      • Input: Pulled High

If the peripheral starts in the Disabled mode, to move to the Input: High Resistance mode, we must perform the following steps:

  • Disabled
  • Enabled
  • Configured as Input
  • Input: High ResistanceIf we wanted to move from Input: High Resistance to Input: Pulled Low, we must perform the following steps:

  • Input: High Resistance

  • Input: Pulled LowSimilarly, if we want to move a GPIO pin from configured as Input: Pulled Low to Output: High, we must perform the following steps:

  • Input: Pulled Low

  • Configured as Input
  • Configured as Output
  • Output: High

Hardware Representation

Typically the states listed above are set by writing values to given registers mapped to a GPIO peripheral. Let's define an imaginary GPIO Configuration Register to illustrate this:

NameBit Number(s)ValueMeaningNotes
enable00disabledDisables the GPIO
1enabledEnables the GPIO
direction10inputSets the direction to Input
1outputSets the direction to Output
input_mode2..300hi-zSets the input as high resistance
01pull-lowInput pin is pulled low
10pull-highInput pin is pulled high
11n/aInvalid state. Do not set
output_mode40set-lowOutput pin is driven low
1set-highOutput pin is driven high
input_status5xin-val0 if input is < 1.5v, 1 if input >= 1.5v

We could expose the following structure in Rust to control this GPIO:

  1. /// GPIO interface
  2. struct GpioConfig {
  3. /// GPIO Configuration structure generated by svd2rust
  4. periph: GPIO_CONFIG,
  5. }
  6. impl GpioConfig {
  7. pub fn set_enable(&mut self, is_enabled: bool) {
  8. self.periph.modify(|_r, w| {
  9. w.enable().set_bit(is_enabled)
  10. });
  11. }
  12. pub fn set_direction(&mut self, is_output: bool) {
  13. self.periph.modify(|r, w| {
  14. w.direction().set_bit(is_output)
  15. });
  16. }
  17. pub fn set_input_mode(&mut self, variant: InputMode) {
  18. self.periph.modify(|_r, w| {
  19. w.input_mode().variant(variant)
  20. });
  21. }
  22. pub fn set_output_mode(&mut self, is_high: bool) {
  23. self.periph.modify(|_r, w| {
  24. w.output_mode.set_bit(is_high)
  25. });
  26. }
  27. pub fn get_input_status(&self) -> bool {
  28. self.periph.read().input_status().bit_is_set()
  29. }
  30. }

However, this would allow us to modify certain registers that do not make sense. For example, what happens if we set the output_mode field when our GPIO is configured as an input?

In general, use of this structure would allow us to reach states not defined by our state machine above: e.g. an output that is pulled low, or an input that is set high. For some hardware, this may not matter. On other hardware, it could cause unexpected or undefined behavior!

Although this interface is convenient to write, it doesn't enforce the design contracts set out by our hardware implementation.