Skip to content

Commit 9f123a9

Browse files
committed
fixed up serial + UART + I2C chapters, cleaned up diagrams
The description of serial ports and serial communication was pretty confusing: now it's much cleaner. The new chapter name `Serial Port` should help, as it unambiguously describes the thing we will actually be working with. The I2C chapter had some slightly weird stuff going on that was fixed up a bit. The handling of figures was cleaned up. A Wikimedia Commons diagram that was incorrectly copied instead of linked was fixed. Figure background was added and slightly darkened with additional CSS to work reasonably with dark-mode reading. Thanks much to Github @iampi31 for contributions and feedback that became part of this PR. closes #78, closes #79, closes #81
1 parent eb1f8a5 commit 9f123a9

File tree

23 files changed

+1229
-466
lines changed

23 files changed

+1229
-466
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ jobs:
8888

8989
- name: Check book links
9090
working-directory: mdbook
91-
run: linkchecker --ignore-url "print.html" book
91+
run: linkchecker --ignore-url "print.html" book/html
9292

9393
- name: Copy EPUB to html directory
9494
working-directory: mdbook

mdbook/book.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
[book]
22
title = "Rust Embedded MB2 Discovery Book"
3-
description = "Discover the world of microcontrollers through Rust with the BB2 micro:bit v2"
3+
description = "Discover the world of microcontrollers through Rust with the BBC micro:bit v2"
44
authors = ["Rust Embedded Resources Team"]
55
language = "en"
66

77
[output.html]
88
git-repository-url = "https://github.com/rust-embedded/discovery-mb2/"
9-
additional-js = ["epub-link.js"]
10-
# additional-css = ["custom.css"]
11-
9+
additional-js = ["src/assets/epub-link.js"]
10+
additional-css = ["src/assets/custom.css"]
1211

1312
[output.epub]
1413
# Optional settings

mdbook/src/05-meet-your-software/flash-it.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ about… but what purpose does the remaining one serve? This chip is *another* M
1616
as powerful as the NRF52833 we will be programming! This chip has three main purposes:
1717

1818
1. Enable power and reset control of our NRF52833 MCU from the USB connector.
19-
2. Provide a serial to USB bridge for our MCU (we will look into that in a later chapter).
19+
2. Provide a [serial to USB bridge] for our MCU.
2020
3. Provide an interface for programming and debugging our NRF52833 (this is the relevant purpose for
2121
now).
2222

@@ -41,3 +41,5 @@ Furthermore, you will have noticed that `cargo build` and `cargo embed` are actu
4141
flags. This is because `cargo embed` actually executes the build and then flashes the resulting
4242
binary on to the chip. This means you can leave out the `cargo build` step in the future if you want
4343
to flash your code right away.
44+
45+
[serial to USB bridge]: ../10-serial/README.md

mdbook/src/09-registers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This chapter is a technical deep-dive. You can safely [skip it] for now and come back to it later if
44
you like. That said, there's a lot of good stuff in here, so I'd recommend you dive in.
55

6-
[skip it]: ../10-serial-communication/index.html
6+
[skip it]: ../10-serial-port/index.html
77

88
-----
99

mdbook/src/10-serial-communication/README.md

Lines changed: 0 additions & 65 deletions
This file was deleted.

mdbook/src/10-serial-communication/nix-tooling.md

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Serial Port
2+
3+
The closest thing to a universal I/O standard for modern day embedded boards is the "serial port". Pretty much every microcontroller has a way to make a few of its pins act as a serial port, and pretty much every microcontroller board makes these pins easy to get to. The MB2 is no exception.
4+
5+
In this chapter, we will describe what a serial port even is. We will then show you how to set up your computer with a "virtual serial port" using USB and use that virtual port with "terminal software" to talk to a serial port on the MB2.
6+
7+
So what's this [serial port]? It's a place where two devices exchange data one bit at a time (*serially*) using one data line in each direction (*full-duplex*) plus a common ground. The serial port originated as "RS-232": see the history later in this chapter for details. However, the protocol spoken on the transmit and receive lines doesn't have an official name I'm aware of — it's just "serial" or maybe "async serial" or "UART serial".
8+
9+
To be clear: most communication channels in modern computers are serial. USB (the "Universal Serial Bus") is a serial channel; I2C (which we will talk about later) is a serial channel. This chapter and the next are *not* about the general concept of serial communication: these chapters are about a specific thing called a "serial port" that has its own implementation and history.
10+
11+
Serial port communication is *asynchronous* in the sense that none of the shared lines carries a clock signal. Instead, both parties must agree on roughly how fast data will be sent along the wire *before* the communication occurs. A peripheral called a Universal Asynchronous Receiver/Transmitter (UART) sends bits at the specified rate on its output wire, and watches for the start of bits on its input wire.
12+
13+
<p align="center">
14+
<img class="white_bg" height="100" title="Serial Protocol" src="../assets/serial-proto.svg" />
15+
</p>
16+
17+
The serial-port communications protocol works with frames, each carrying a byte of data. Each frame has one *start* bit, 5 to 9 bits of payload data (sent lsb-to-msb; modern applications rarely send a 9-bit byte; 7 or fewer bits in a frame will be left-padded to an 8-bit byte with zeros) and 1 to 2 *stop bits*. In the diagram above, an ASCII 'E' character is sent using 8 data bits and 1 stop bit.
18+
19+
The speed of the protocol is known as *baud rate* and it's quoted in bits per second (bps). (If you're thinking that this sounds wrong — it is. "Baud" is supposed to be *symbols* per second; a symbol should correspond to a frame; even if a data bit is regarded as the "symbol" they aren't sent at this rate because of the rest of the protocol. It's a convention, and doesn't have to make sense.) Historically common baud rates for UART serial are 9600bps, 19200bps, and 115200bps, but it is not uncommon in our modern world to send data at 921,600bps.
20+
21+
With the "normal" configuration of 1 start bit, 8 bits of data, 1 stop bit and a bit rate of 921.6K bps we can send and receive 92.16K bytes per second — fast enough to transmit single-channel uncompressed CD audio. At the bit rate of 115,200 bps that we'll be using, we can send and receive 11.52K bytes per second. This is fine for most purposes.
22+
23+
We'll be using a serial port (indirectly) to exchange data between the MB2 and your computer. Now you might be asking yourself: why exactly aren't we using RTT for this like we did before? RTT is a protocol that is meant to be used solely for debugging. You will not find devices that use RTT to communicate with other devices. However, serial communication is used quite often. For example, some GPS receivers send the position information they receive via serial. In addition RTT, like many debugging protocols, is slow compared to serial transfer rates.
24+
25+
<p align="center">
26+
<img class="white_bg" height="500" title="Serial" src="../assets/serial.svg" />
27+
</p>
28+
29+
Today's computers don't usually have a serial port, and even if they do the voltage they use (+5V on a modern serial port, ±12V on an ancient RS-232 port) is outside the range that the MB2 hardware will accept and may result in damaging it. *You can't directly connect your computer to the microcontroller.*
30+
31+
<a href="https://en.wikipedia.org/wiki/File:UART_to_USB_adapter.jpg">
32+
<p align="center">
33+
<img height="240" title="UART To USB Adapter" src="https://upload.wikimedia.org/wikipedia/commons/2/24/UART_to_USB_adapter.jpg" />
34+
</p>
35+
</a>
36+
37+
You *can* buy inexpensive (typically under US$5) USB←→serial converters that will support the +3.3V inputs of most modern microcontroller boards. The board shown above is a common one that I use regularly. We will be talking to the MB2 serial port through the MB2's built-in USB port. However, if you want to connect directly to a hardware serial port, on the MB2 or some other board, a serial converter is the way to go.
38+
39+
A separate USB channel on the MB2's USB port can be used to talk to the MB2's built-in USB←→serial converter. (This is the right-hand path in the figure above.) This USB←→serial conversion is implemented using the "[communications microcontroller]" of the MB2: the communications microcontroller exposes a serial interface to the microcontroller and a virtual USB serial interface to your computer. The computer presents a virtual serial interface via the USB CDC-ACM ("Communications Device Class - Abstract Control Model", ugh) device class. The MB2 microcontroller will see your computer as a device connected to its hardware serial port; your computer will see the MB2 serial port as a virtual serial device.
40+
41+
Now, let's get familiar with the USB serial port interface that your OS offers. Pick a route:
42+
43+
- [Linux/UNIX](nix-tooling.md)
44+
- [Windows](windows-tooling.md)
45+
46+
For MacOS check out the Linux documentation, although your experience may differ somewhat.
47+
48+
[serial port]: https://en.wikipedia.org/wiki/Serial_port
49+
[communications microcontroller]: ../05-meet-your-software/flash-it.md
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Linux USB←→serial tooling
2+
3+
The MB2's USB emulated serial device shows up in Linux when you connect the MB2 to a Linux USB
4+
port.
5+
6+
## Connecting the MB2 board
7+
8+
If you connect the MB2 board to your computer you should see a new TTY device appear in
9+
`/dev`.
10+
11+
``` console
12+
$ sudo dmesg -T | tail | grep -i tty
13+
[63712.446286] cdc_acm 1-1.7:1.1: ttyACM0: USB ACM device
14+
```
15+
16+
This is the USB←→serial device. On Linux, it's named `tty` (for "TeleTYpe", believe it or not). It
17+
should show up as `ttyACM0`, or maybe `ttyUSB0`. If other "ACM" devices are plugged in, the number
18+
will be higher. (On Mac OS `ls /dev/cu.usbmodem*` will show the serial device.)
19+
20+
But what exactly is `ttyACM0`? It's a file of course! Everything is a file in Unix:
21+
22+
```
23+
$ ls -l /dev/ttyACM0
24+
crw-rw----+ 1 root plugdev 166, 0 Jan 21 11:56 /dev/ttyACM0
25+
```
26+
27+
Note that you will need to be either running as `root` (not advised) or a member of the group that
28+
appears in the `ls` output (usually `plugdev` or `dialout`) to read and write this device. You can
29+
then send out data by simply writing to this file:
30+
31+
``` console
32+
$ echo 'Hello, world!' > /dev/ttyACM0
33+
```
34+
35+
You should see the orange LED on the MB2, right next to the USB port, blink for a moment, whenever
36+
you enter this command. The bit rate and other serial parameters may not be set up right for the MB2
37+
serial port, but the MB2 can tell that it is being sent serial.
38+
39+
## minicom
40+
41+
We'll use the program `minicom` to interact with the serial device using the keyboard. We will use
42+
the default settings of modern `minicom`: 115200 bps, 8 data bits, one stop bit, no parity bits, no
43+
flow control. (115200 bps happens to be a rate that will work with the MB2.)
44+
45+
``` console
46+
$ minicom -D /dev/ttyACM0
47+
```
48+
49+
This tells `minicom` to open the serial device at `/dev/ttyACM0`. A text-based user interface
50+
(TUI) will pop out.
51+
52+
<p align="center">
53+
<img title="minicom" src="../assets/minicom.png" />
54+
</p>
55+
56+
You can now send data using the keyboard! Go ahead and type something. Note that
57+
the text UI will *not* echo back what you type. If you pay attention to the yellow LED
58+
on top of the MB2 though, you will notice that it blinks whenever you type something.
59+
60+
## `minicom` commands
61+
62+
`minicom` exposes commands via keyboard shortcuts. On Linux, the shortcuts start with `Ctrl+A`. (On
63+
Mac, the shortcuts start with the `Meta` key.) Some useful commands below:
64+
65+
- `Ctrl+A` + `Z`. Minicom Command Summary
66+
- `Ctrl+A` + `C`. Clear the screen
67+
- `Ctrl+A` + `X`. Exit and reset
68+
- `Ctrl+A` + `Q`. Quit with no reset
69+
70+
> **NOTE** Mac users: In the above commands, replace `Ctrl+A` with `Meta`.

0 commit comments

Comments
 (0)