Hello everyone. This is my new article about Rust for embedded systems. I decided to start writing articles in English to attract a large audience (forgive me the Russian audience). This article is about creating a stopwatch using Rust and the embedded-graphics library. Today I’ve made for you an example of creating a digital and analog stopwatch. For this example, you need stm32f429i-disco (development kit) and ssd1306 (SPI). This article is a spin-off of the stopwatch from the second article on Rust for Embedded Systems. I will briefly describe the work of our stopwatch:

  1. You need to connect ssd1306 to the stm32f429i-disco board. You should follow the following pinout. We’ll use SPI4.
    1. Pin SCK (ssd1306) — PE2
    2. Pin MISO (ssd1306) — PE5
    3. Pin MOSI (ssd1306) — PE6
    4. Pin CS (ssd1306) — PE4
    5. Pin DC (ssd1306) — PE3
  2. There are two LEDs LD3 (PG13) and LD4 (PG14) on this development board. The LD3 LED is on when the stopwatch is ready for use. The LD 3 LED blinks when the stopwatch is running. The red LED is on when the stopwatch is stopped.
  3. My example is based on the example I took from here.
  4. When the stopwatch is stopped, you could clear the LCD or continue to work. To clear and restore the screen to its original state, press the user button (the blue button) and hold it for two seconds. To continue the stopwatch, briefly press the blue button.
  5. You could watch the video below to understand how this works in more detail.

First of all, we will describe this Cargo.toml file. The Cargo.toml file for each package is called its manifest. This manifest file consists of the following sections.

First, we need to set up the internal clock (RCC). I decided to run this example at a core frequency of 180 MHz.

This project uses two interrupt handlers. This is an interrupt handler for the user button EXTI0 and an interrupt handler for the timer TIM2. Interrupts differ from exceptions in a variety of ways but their operation and use are largely similar and they are also handled by the same interrupt controller. Whereas exceptions are defined by the Cortex-M architecture, interrupts are always vendor (and often even chip) specific implementations, both in naming and functionality. Interrupt handlers look like plain functions (except for the lack of arguments) similar to exception handlers. However, they can not be called directly by other parts of the firmware due to the special calling conventions. It is, however, possible to generate interrupt requests in software to trigger a diversion to the interrupt handler.

By the way, we need to initialize the SPI4 for the screen. We can do this as follows.

We also need to set a global state for our variables. We can do this as follows.

For synchronization, we will use mutexes. These constructs ensure exclusive access to a variable, such as our counter. A thread can attempt to lock (or acquire) the mutex, and either succeed immediately, or blocks waiting for the lock to be acquired, or returns an error that the mutex could not be locked. While that thread holds the lock, it is granted access to the protected data. When the thread is done, it unlocks (or releases) the mutex, allowing another thread to lock it. Using a mutex with interrupt handlers can be tricky: it is not normally acceptable for the interrupt handler to block, and it would be especially disastrous for it to block waiting for the main thread to release a lock since we would then deadlock (the main thread will never release the lock because execution stays in the interrupt handler). Deadlocking is not considered unsafe: it is possible to even in safe Rust.

Embedded-graphics is a 2D graphics library that is focused on memory-constrained embedded devices. It contains built-in items that make it easy to draw 2D graphics primitives. To support many different kinds of display embedded-graphics doesn’t include any drivers directly but provides an API that can be implemented by external crates. This example shows some more advanced usage of Embedded Graphics. It draws a round clock face with a second hand. A digital clock is drawn at the top of the clock.

The source code of the project.

You can download the project sources as follows:
git clone https://github.com/mcuby/rust-stm32f429i-disco.git

Отправить ответ

avatar

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.

  Subscribe  
Уведомлять о