-
Notifications
You must be signed in to change notification settings - Fork 556
micro:bit: chapter 04; 05 #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a3b07eb
rewrite ch04
hargoniX 8f2a63e
rewrite 05-led-roulette README
hargoniX 1a17737
rewrite 05-led-roulette build-it
hargoniX 9d12344
rewrite 05-led-roulette flash-it
hargoniX bf34c43
Apply suggestions from code review
hargoniX 9b0968a
Fix cargo-embed issues
hargoniX ed1012a
add link to microbit hardware website
hargoniX b14acad
explain the reset flag im Embed.toml
hargoniX 33fd0a2
Update cargo-embed version
hargoniX ade1dc3
ch05: rewrite the rest of debug-it.md
hargoniX 56181dc
Remove unsupported monitor GDB commands
hargoniX 7df0640
Split up the initial blinky example
hargoniX 1130f63
add note about armv8 targets
hargoniX 35952a4
reexpand the note on optimzations while building again
hargoniX c430061
Apply suggestions from code review
hargoniX ec58ae3
Update src/05-led-roulette/debug-it.md
hargoniX 4a61273
Try to simplify the embedded-hal explanation.
hargoniX 5698eab
Update src/05-led-roulette/light-it-up.md
hargoniX c8aec47
blinky
hargoniX b4d8d0f
Apply suggestions from code review
hargoniX ab1d3bc
implement LED roulette and add pictures
hargoniX 1421432
add indication to put the example code into src/main.rs
hargoniX 9a14384
Apply suggestions from code review
hargoniX 759a9ee
host videos on website
hargoniX bf70e0c
remove the TODO for the resource for now, we can add it later on anyways
hargoniX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,40 +2,37 @@ | |
|
|
||
| Let's get familiar with the hardware we'll be working with. | ||
|
|
||
| ## STM32F3DISCOVERY (the "F3") | ||
| ## micro:bit | ||
|
|
||
| <p align="center"> | ||
| <img title="F3" src="../assets/f3.jpg"> | ||
| <img title="micro:bit" src="../assets/microbit.jpg"> | ||
| </p> | ||
|
|
||
| We'll refer to this board as "F3" throughout this book. Here are some of the | ||
| many components on the board: | ||
| Here are some of the many components on the board: | ||
|
|
||
| - A [microcontroller]. | ||
| - A number of LEDs, including the eight aligned in a "compass" formation. | ||
| - Two buttons. | ||
| - Two USB ports. | ||
| - An [accelerometer]. | ||
| - A [magnetometer]. | ||
| - A [gyroscope]. | ||
| - A number of LEDs, most notably the LED matrix on the back | ||
| - Two user buttons as well as a reset button (the one next to the USB port). | ||
| - One USB port. | ||
| - A sensor that is both a [magnetometer] and an [accelerometer] | ||
|
|
||
| [microcontroller]: https://en.wikipedia.org/wiki/Microcontroller | ||
| [accelerometer]: https://en.wikipedia.org/wiki/Accelerometer | ||
| [magnetometer]: https://en.wikipedia.org/wiki/Magnetometer | ||
| [gyroscope]: https://en.wikipedia.org/wiki/Gyroscope | ||
|
|
||
| Of these components, the most important is the microcontroller (sometimes | ||
| shortened to "MCU" for "microcontroller unit"), which is the large black square | ||
| sitting in the center of your board. The MCU is what runs your code. You might | ||
| sometimes read about "programming a board", when in reality what we are doing | ||
| is programming the MCU that is installed on the board. | ||
| shortened to "MCU" for "microcontroller unit"), which is the bigger of the two | ||
| black squares sitting on the side of the board with the USB port. The MCU is | ||
| what runs your code. You might sometimes read about "programming a board", when | ||
| in reality what we are doing is programming the MCU that is installed on the board. | ||
|
|
||
| ## STM32F303VCT6 (the "STM32F3") | ||
| ## Nordic nRF51822 (the "nRF51") | ||
|
|
||
| Since the MCU is so important, let's take a closer look at the one sitting on our board. | ||
|
|
||
| Our MCU is surrounded by 100 tiny metal **pins**. These pins are connected to | ||
| **traces**, the little "roads" that act as the wires connecting components | ||
| Our MCU has 48 tiny metal **pins** sitting right underneath it (it's a so called [QFN48] chip). | ||
| These pins are connected to **traces**, the little "roads" that act as the wires connecting components | ||
| together on the board. The MCU can dynamically alter the electrical properties | ||
| of the pins. This works similar to a light switch altering how electrical | ||
| current flows through a circuit. By enabling or disabling electrical current to | ||
|
|
@@ -44,68 +41,57 @@ be turned on and off. | |
|
|
||
| Each manufacturer uses a different part numbering scheme, but many will allow | ||
| you to determine information about a component simply by looking at the part | ||
| number. Looking at our MCU's part number (`STM32F303VCT6`), the `ST` at the | ||
| front hints to us that this is a part manufactured by [ST Microelectronics]. | ||
| Searching through [ST's marketing materials] we can also learn the following: | ||
|
|
||
| [ST Microelectronics]: https://st.com/ | ||
| [ST's marketing materials]: https://www.st.com/en/microcontrollers-microprocessors/stm32-mainstream-mcus.html | ||
|
|
||
| - The `M32` represents that this is an Arm®-based 32-bit microcontroller. | ||
| - The `F3` represents that the MCU is from ST's "STM32F3" series. This is a | ||
| series of MCUs based on the Cortex®-M4 processor design. | ||
| - The remainder of the part number goes into more details about things like | ||
| extra features and RAM size, which at this point we're less concerned about. | ||
|
|
||
| > ### Arm? Cortex-M4? | ||
| > | ||
| > If our chip is manufactured by ST, then who is Arm? And if our chip is the | ||
| > STM32F3, what is the Cortex-M4? | ||
| > | ||
| > You might be surprised to hear that while "Arm-based" chips are quite | ||
| > popular, the company behind the "Arm" trademark ([Arm Holdings][]) doesn't | ||
| > actually manufacture chips for purchase. Instead, their primary business | ||
| > model is to just *design* parts of chips. They will then license those designs to | ||
| > manufacturers, who will in turn implement the designs (perhaps with some of | ||
| > their own tweaks) in the form of physical hardware that can then be sold. | ||
| > Arm's strategy here is different from companies like Intel, which both | ||
| > designs *and* manufactures their chips. | ||
| > | ||
| > Arm licenses a bunch of different designs. Their "Cortex-M" family of designs | ||
| > are mainly used as the core in microcontrollers. For example, the Cortex-M0 | ||
| > is designed for low cost and low power usage. The Cortex-M7 is higher cost, | ||
| > but with more features and performance. The core of our STM32F3 is based on | ||
| > the Cortex-M4, which is in the middle: more features and performance than the | ||
| > Cortex-M0, but less expensive than the Cortex-M7. | ||
| > | ||
| > Luckily, you don't need to know too much about different types of processors | ||
| > or Cortex designs for the sake of this book. However, you are hopefully now a | ||
| > bit more knowledgeable about the terminology of your device. While you are | ||
| > working specifically with an STM32F3, you might find yourself reading | ||
| > documentation and using tools for Cortex-M-based chips, as the STM32F3 is | ||
| > based on a Cortex-M design. | ||
| number. Looking at our MCU's part number (`nRF51822-QFAA-R`, you probably cannot | ||
| see it with your bare eye, but it is on the chip), the `n` at the | ||
| front hints to us that this is a part manufactured by [Nordic Semiconductor]. | ||
| Looking up the part number on their website we quickly find the [product page]. | ||
| There we learn that our chip's main marketing point is that it is a | ||
| "Bluetooth Low Energy and 2.4 GHz SoC" (SoC being short for System on a Chip), | ||
|
hargoniX marked this conversation as resolved.
Outdated
|
||
| which explains the RF in the product name since RF is short for radio frequency. | ||
| If we search through the documentation of the chip linked on the [product page] | ||
| for a bit we find the [product specification] which has chapter 10 Ordering Infomration | ||
|
hargoniX marked this conversation as resolved.
Outdated
|
||
| dedicated to explaining the weird chip naming, here we learn that: | ||
|
hargoniX marked this conversation as resolved.
Outdated
|
||
|
|
||
| [QFN48]: https://en.wikipedia.org/wiki/Flat_no-leads_package | ||
| [Nordic Semiconductor]: https://www.nordicsemi.com/ | ||
| [product page]: https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF51822 | ||
| [product specification]: https://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.3.pdf | ||
|
|
||
| - The `nRF51` is the MCU's series, indicating that there are other `nRF51` MCUs | ||
| - The `822` is the part code | ||
| - The `QF` is short for `QFN48` | ||
| - The `AA` is the variant code, indicating how much RAM and flash memory the MCU has, | ||
| in our case 256 kilobyte flash and 16 kilobyte RAM | ||
| - The `R` is the packaging code which is relevant for factories manufacturing boards | ||
| with this chip on them in larger scales | ||
|
|
||
| The product specification does of course contain a lot more useful information about | ||
| the chip, for example that it is based on an ARM® Cortex™-M0 32 bit processor. | ||
|
|
||
| ### Arm? Cortex-M0? | ||
|
|
||
| If our chip is manufactured by Nordic, then who is Arm? And if our chip is the | ||
| nRF51822, what is the Cortex-M0? | ||
|
|
||
| You might be surprised to hear that while "Arm-based" chips are quite | ||
| popular, the company behind the "Arm" trademark ([Arm Holdings][]) doesn't | ||
| actually manufacture chips for purchase. Instead, their primary business | ||
| model is to just *design* parts of chips. They will then license those designs to | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might need to update this once the nVIDIA buyout goes through. |
||
| manufacturers, who will in turn implement the designs (perhaps with some of | ||
| their own tweaks) in the form of physical hardware that can then be sold. | ||
| Arm's strategy here is different from companies like Intel, which both | ||
| designs *and* manufactures their chips. | ||
|
|
||
| Arm licenses a bunch of different designs. Their "Cortex-M" family of designs | ||
| are mainly used as the core in microcontrollers. For example, the Cortex-M0 | ||
| (the core our chip is based on) is designed for low cost and low power usage. | ||
| The Cortex-M7 is higher cost, but with more features and performance. | ||
|
|
||
| Luckily, you don't need to know too much about different types of processors | ||
| or Cortex designs for the sake of this book. However, you are hopefully now a | ||
| bit more knowledgeable about the terminology of your device. While you are | ||
| working specifically with an nRF51822, you might find yourself reading | ||
| documentation and using tools for Cortex-M-based chips, as the nRF51822 is | ||
| based on a Cortex-M design. | ||
|
|
||
| [Arm Holdings]: https://www.arm.com/ | ||
|
|
||
| ## The Serial module | ||
|
|
||
| <p align="center"> | ||
| <img title="Serial module" src="../assets/serial.jpg"> | ||
| </p> | ||
|
|
||
| If you have an older revision of the discovery board, you can use this module to | ||
| exchange data between the microcontroller in the F3 and your computer. This module | ||
| will be connected to your computer using an USB cable. I won't say more at this | ||
| point. | ||
|
|
||
| If you have a newer release of the board then you don't need this module. The | ||
| ST-LINK will double as a USB<->serial converter connected to the microcontroller USART1 at pins PC4 and PC5. | ||
|
|
||
| ## The Bluetooth module | ||
|
|
||
| <p align="center"> | ||
| <img title="The HC-05 Bluetooth module" src="../assets/bluetooth.jpg"> | ||
| </p> | ||
|
|
||
| This module has the exact same purpose as the serial module but it sends the data over Bluetooth | ||
| instead of over USB. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| [target.thumbv7em-none-eabihf] | ||
| runner = "arm-none-eabi-gdb -q" | ||
| [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| rustflags = [ | ||
| "-C", "link-arg=-Tlink.x", | ||
| ] | ||
|
|
||
| [build] | ||
| target = "thumbv6m-none-eabi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| [package] | ||
| authors = ["Jorge Aparicio <jorge@japaric.io>"] | ||
| edition = "2018" | ||
| name = "led-roulette" | ||
| version = "0.1.0" | ||
| authors = ["Henrik Böving <hargonix@gmail.com>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| aux5 = { path = "auxiliary" } | ||
| cortex-m = "0.6.0" | ||
| cortex-m-rt = "0.6.10" | ||
| panic-halt = "0.2.0" | ||
| nrf51-hal = "0.11.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [default.general] | ||
| chip = "nrf51822" | ||
|
hargoniX marked this conversation as resolved.
Outdated
|
||
|
|
||
| [default.rtt] | ||
| enabled = false | ||
|
|
||
| [default.gdb] | ||
| enabled = true | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| Alright, let's start by building the following application: | ||
|
|
||
| # TODO REPLACE THIS | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
| <p align="center"> | ||
| <img src="https://i.imgur.com/0k1r2Lc.gif"> | ||
| </p> | ||
|
|
@@ -10,15 +11,10 @@ I'm going to give you a high level API to implement this app but don't worry we' | |
| stuff later on. The main goal of this chapter is to get familiar with the *flashing* and debugging | ||
| process. | ||
|
|
||
| Throughout this text we'll be using the starter code that's in the [discovery] repository. Make sure | ||
|
hargoniX marked this conversation as resolved.
|
||
| you 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 more | ||
| directories named after each chapter of this book. Most of those directories are starter Cargo | ||
| projects. | ||
|
|
||
| [discovery]: https://github.com/rust-embedded/discovery | ||
|
|
||
| Now, jump into the `src/05-led-roulette` directory. Check the `src/main.rs` file: | ||
|
|
||
| ``` rust | ||
|
|
@@ -46,4 +42,17 @@ as well. This directory contains a Cargo configuration file (`.cargo/config`) th | |
| linking 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. | ||
|
|
||
|
hargoniX marked this conversation as resolved.
|
||
| Furthermore there is also an `Embed.toml` file | ||
|
|
||
| ```toml | ||
| {{#include Embed.toml}} | ||
|
hargoniX marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| This file tells `cargo-embed` that: | ||
|
|
||
| * we are working with a nrf51822, | ||
| * we want to disable RTT, RTT being a protocol that allows the chip to send text to a debugger. | ||
| You have in fact already seen RTT in action, it was the protocol that sent "Hello World" in chapter 3. | ||
| * we want to enable GDB, this will be required for the debugging procedure | ||
|
|
||
| Alright, let's start by building this program. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //! This build script copies the `memory.x` file from the crate root into | ||
| //! a directory where the linker can always find it at build time. | ||
| //! For many projects this is optional, as the linker always searches the | ||
| //! project root directory (wherever `Cargo.toml` is). However, if you | ||
| //! are using a workspace or have a more complicated build setup, this | ||
| //! build script becomes required. Additionally, by requesting that | ||
| //! Cargo re-run the build script whenever `memory.x` is changed, | ||
| //! a rebuild of the application with new memory settings is ensured after updating `memory.x`. | ||
|
|
||
| use std::env; | ||
| use std::fs::File; | ||
| use std::io::Write; | ||
| use std::path::PathBuf; | ||
|
|
||
| fn main() { | ||
| // Put `memory.x` in our output directory and ensure it's | ||
| // on the linker search path. | ||
| let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
| File::create(out.join("memory.x")) | ||
| .unwrap() | ||
| .write_all(include_bytes!("memory.x")) | ||
| .unwrap(); | ||
| println!("cargo:rustc-link-search={}", out.display()); | ||
|
|
||
| // By default, Cargo will re-run a build script whenever | ||
| // any file in the project changes. By specifying `memory.x` | ||
| // here, we ensure the build script is only re-run when | ||
| // `memory.x` is changed. | ||
| println!("cargo:rerun-if-changed=memory.x"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.