LED roulette

Alright, let’s start by building the following application:

LED roulette - 图1

I’m going to give you a high level API to implement this app but don’t worry we’ll do low levelstuff later on. The main goal of this chapter is to get familiar with the flashing and debuggingprocess.

Throughout this text we’ll be using the starter code that’s in the discovery repository. Make sureyou always have the latest version of the master branch because this website tracks that branch.

The starter code is in the src directory of that repository. Inside that directory there are moredirectories named after each chapter of this book. Most of those directories are starter Cargoprojects.

Now, jump into the src/05-led-roulette directory. Check the src/main.rs file:

  1. {{#include src/main.rs}}

Microcontroller programs are different from standard programs in two aspects: #![no_std] and#![no_main].

The no_std attribute says that this program won’t use the std crate, which assumes an underlyingOS; the program will instead use the core crate, a subset of std that can run on bare metalsystems (i.e., systems without OS abstractions like files and sockets).

The no_main attribute says that this program won’t use the standard main interface, which istailored for command line applications that receive arguments. Instead of the standard main we’lluse the entry attribute from the cortex-m-rt crate to define a custom entry point. In thisprogram we have named the entry point “main”, but any other name could have been used. The entrypoint function must have signature fn() -> !; this type indicates that the function can’t return— this means that the program never terminates.

If you are a careful observer, you’ll also notice there is a .cargo directory in the Cargo projectas well. This directory contains a Cargo configuration file (.cargo/config) that tweaks thelinking process to tailor the memory layout of the program to the requirements of the target device.This modified linking process is a requirement of the cortex-m-rt crate.

Alright, let’s start by building this program.