Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

STM32 Buttons Exercise

In this exercise you’ll get familiar with:

  • controlling GPIO pins from a Rust HAL
  • modifying a Board Support Package (BSP) to configure additional hardware

Outline

As discussed in the previous exercise, the nucleo-u5a5zj-bsp BSP configures the three GPIO pins that control the three LEDs present on the NUCLEO-U5A5ZJ-Q board.

In this exercise we’re going to extend the BSP to support support the “User” button. The NonSecureBoard structure should have a new field button and the data type for that field should have an API fn is_pressed(&self) -> bool which reports whether the button is pressed.

Once we’ve added that API, we will modify the standalone-button.rs file to do something interesting when the button is pressed. Exactly what is up to you!

Tasks

  1. Study the Led type in the nucleo-u5a5zj-bsp BSP
  2. Add a new Button type to the BSP
  3. Give the Button type a fn is_pressed(&self) -> bool method
  4. Add a field of type Button to the NonSecureBoard structure
  5. Review the STM32 NUCLEO documentation to check which GPIO pin the “User” button (B1) is connected to and whether the pin goes high or low when pressed
  6. Initialise the value of type Button and add it to the NonSecureBoard initialisation

Step by Step

The Button Type

You should end up with something like:

Solution
/// Represents a Button on the board
pub struct Button {
    inner: Input,
}

impl Button {
    /// Is the button pressed?
    pub fn is_pressed(&self) -> bool {
        self.inner.is_high()
    }
}

Import Input as required.

The GPIO goes “high” when pressed.

Reading the User Manual

Solution

Section 7.6 explains that User button (B1):

  • is on pin PC13
  • requires a pull-down input
  • goes high when pressed

Observe the warning about never setting this pin to an output!

Modifying the NonSecureBoard

Once you added a field, like user_button: Button, you’ll need to create a value of type Button.

Solution
let user_button = Button {
    inner: gpio.change_to_input(pins.port_c.pin13, Pull::Down),
};

Import Pull as required.