Skip to content

Commit bd583ed

Browse files
committed
samples: mgmt: Expand smp_svr sample documentation
Expand the contents of the smp_svr sample documentation so that it covers all steps needed to perform DFU over BLE. Signed-off-by: Carles Cufi <[email protected]>
1 parent 8a8d981 commit bd583ed

File tree

1 file changed

+215
-56
lines changed

1 file changed

+215
-56
lines changed
Lines changed: 215 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,276 @@
11
.. _smp_svr_sample:
22

33
SMP Server Sample
4-
################################
4+
#################
55

66
Overview
77
********
88

9-
An application that implements a Simple Management Protocol (SMP) server. SMP
10-
is a basic transfer encoding for use with the mcumgr management protocol. For
11-
more information about mcumgr and SMP, please see
12-
:file:`ext/lib/mgmt/mcumgr/README.md` in the Zephyr tree.
9+
This sample application implements a Simple Management Protocol (SMP) server.
10+
SMP is a basic transfer encoding for use with the MCUmgr management protocol.
11+
For more information about MCUmgr and SMP, please see :ref:`mgmt`.
1312

1413
This sample application supports the following mcumgr transports by default:
14+
1515
* Shell
1616
* Bluetooth
1717

18-
`smp_svr` enables support for the following command groups:
19-
* fs_mgmt
20-
* img_mgmt
21-
* os_mgmt
22-
* stat_mgmt
18+
``smp_svr`` enables support for the following command groups:
19+
20+
* ``fs_mgmt``
21+
* ``img_mgmt``
22+
* ``os_mgmt``
23+
* ``stat_mgmt``
2324

2425
Caveats
2526
*******
2627

27-
The Zephyr port of `smp_svr` is configured to run on an nRF52 MCU. The
28-
application should build and run for other platforms without modification, but
29-
the file system management commands will not work. To enable file system
30-
management for a different platform, adjust the `CONFIG_FS_NFFS_FLASH_DEV_NAME`
31-
setting in `prj.conf` accordingly.
28+
* The Zephyr port of `smp_svr` is configured to run on a Nordic nRF52x MCU. The
29+
application should build and run for other platforms without modification, but
30+
the file system management commands will not work. To enable file system
31+
management for a different platform, adjust the
32+
:option:`CONFIG_FS_NFFS_FLASH_DEV_NAME` setting in :file:`prj.conf` accordingly.
33+
34+
* The MCUboot bootloader is required for ``img_mgmt`` to function
35+
properly. More information about the Device Firmware Upgrade subystem and
36+
MCUboot can be found in :ref:`mcuboot`.
37+
38+
* The :file:`mcumgr` command-line tool only works with Bluetooth Low Energy (BLE)
39+
on Linux and macOS. On Windows there is no support for Device Firmware
40+
Upgrade over BLE yet.
41+
42+
Building a BLE Controller (optional)
43+
************************************
3244

33-
In addition, the MCUboot boot loader (https://github.com/runtimeco/mcuboot) is
34-
required for img_mgmt to function properly.
45+
.. note::
46+
This section is only relevant for Linux users
47+
48+
If you want to try out Device Firmware Upgrade (DFU) over the air using
49+
Bluetooth Low Energy (BLE) and do not have a built-in or pluggable BLE radio,
50+
you can build one and use it following the instructions in
51+
:ref:`bluetooth-hci-uart-bluez`.
3552

3653
Building and Running
3754
********************
3855

39-
The below steps describe how to build and run the `smp_svr` sample app in
40-
Zephyr. Where examples are given, they assume the following setup:
41-
42-
* BSP: Nordic nRF52dk
43-
* MCU: PCA10040
56+
The below steps describe how to build and run the ``smp_svr`` sample in
57+
Zephyr. Where examples are given, they assume the sample is being built for
58+
the Nordic nRF52 Development Kit (``BOARD=nrf52_pca10040``).
4459

45-
If you would like to use a more constrained platform, such as the nRF51, you
46-
should use the `prj.conf.tiny` configuration file rather than the default
47-
`prj.conf`.
60+
If you would like to use a more constrained platform, such as the nRF51 DK, you
61+
should use the :file:`prj.conf.tiny` configuration file rather than the default
62+
:file:`prj.conf`.
4863

4964
Step 1: Build MCUboot
5065
=====================
5166

52-
Build MCUboot by following the instructions in its `docs/readme-zephyr.md`
53-
file.
67+
Build MCUboot by following the instructions in the :ref:`mcuboot`
68+
documentation page.
5469

55-
Step 2: Upload MCUboot
70+
Step 2: Flash MCUboot
5671
======================
5772

58-
Upload the resulting `zephyr.bin` file to address 0 of your board. This can be
59-
done in gdb as follows:
73+
Flash the resulting image file to address 0x0 of flash memory.
74+
This can be done in multiple ways.
75+
76+
Using make or ninja:
77+
78+
.. code-block:: console
79+
80+
make flash
81+
# or
82+
ninja flash
83+
84+
Using GDB:
85+
86+
.. code-block:: console
6087
61-
```
62-
restore <path-to-mcuboot-zephyr.bin> binary 0
63-
```
88+
restore <path-to-mcuboot-zephyr.bin> binary 0
6489
6590
Step 3: Build smp_svr
6691
=====================
6792

68-
`smp_svr` can be built for the nRF52 as follows:
93+
``smp_svr`` can be built for the nRF52 as follows:
6994

7095
.. zephyr-app-commands::
7196
:zephyr-app: samples/mgmt/mcumgr/smp_svr
7297
:board: nrf52_pca10040
7398
:build-dir: nrf52_pca10040
7499
:goals: build
75100

76-
Step 4: Create an MCUboot-compatible image
77-
==========================================
101+
.. _smp_svr_sample_sign:
78102

79-
Using MCUboot's `imgtool.py` script, convert the `zephyr.bin` file from step 3
80-
into an image file. In the below example, the MCUboot repo is located at
81-
`~/repos/mcuboot`.
103+
Step 4: Sign the image
104+
======================
105+
106+
.. note::
107+
From this section onwards you can use either a binary (``.bin``) or an
108+
Intel Hex (``.hex``) image format. This is written as ``(bin|hex)`` in this
109+
document.
110+
111+
Using MCUboot's :file:`imgtool.py` script, sign the :file:`zephyr.(bin|hex)`
112+
file you built in Step 3. In the below example, the MCUboot repo is located at
113+
:file:`~/src/mcuboot`.
82114

83115
.. code-block:: console
84116
85-
~/repos/mcuboot/scripts/imgtool.py sign \
117+
~/src/mcuboot/scripts/imgtool.py sign \
118+
--key ~/src/mcuboot/root-rsa-2048.pem \
86119
--header-size 0x200 \
87120
--align 8 \
88121
--version 1.0 \
89122
--included-header \
90-
--key ~/repos/mcuboot/root-rsa-2048.pem \
91-
<path-to-smp_svr-zephyr.bin> signed.img
123+
<path-to-zephyr.(bin|hex)> signed.(bin|hex)
124+
125+
The above command creates an image file called :file:`signed.(bin|hex)` in the
126+
current directory.
92127

93-
The above command creates an image file called `signed.img` in the current
94-
directory.
128+
Step 5: Flash the smp_svr image
129+
===============================
95130

96-
Step 5: Upload the smp_svr image
97-
================================
131+
Upload the :file:`signed.(bin|hex)` file from Step 4 to image slot-0 of your
132+
board. The location of image slot-0 varies by board, as described in
133+
:ref:`mcuboot_partitions`. For the nRF52 DK, slot-0 is located at address
134+
``0xc000``.
98135

99-
Upload the `signed.img` file from step 4 to image slot 0 of your board. The
100-
location of image slot 0 varies by BSP. For the nRF52dk, slot 0 is located at
101-
address 0xc000.
136+
Using :file:`nrfjprog` you don't need to specify the slot-0 starting address,
137+
since :file:`.hex` files already contain that information:
102138

103-
The following gdb command uploads the image to 0xc000:
104139
.. code-block:: console
105140
106-
restore <path-to-signed.img> binary 0xc000
141+
nrfjprog --program <path-to-signed.hex>
107142
108-
Step 7: Run it!
143+
Using GDB:
144+
145+
.. code-block:: console
146+
147+
restore <path-to-signed.bin> binary 0xc000
148+
149+
Step 6: Run it!
109150
===============
110151

111-
The `smp_svr` app is ready to run. Just reset your board and test the app with
112-
the mcumgr CLI tool:
152+
.. note::
153+
If you haven't installed :file:`mcumgr` yet, then do so by following the
154+
instructions in the :ref:`mcumgr_cli` section of the Management subsystem
155+
documentation.
156+
157+
.. note::
158+
The :file:`mcumgr` command-line tool requires a connection string in order
159+
to identify the remote target device. In this sample we use a BLE-based
160+
connection string, and you might need to modify it depending on the
161+
BLE controller you are using.
162+
163+
164+
The ``smp_svr`` app is ready to run. Just reset your board and test the app
165+
with the :file:`mcumgr` command-line tool's ``echo`` functionality, which will
166+
send a string to the remote target device and have it echo it back:
167+
168+
.. code-block:: console
169+
170+
sudo mcumgr --conntype ble --connstring ctlr_name=hci0,peer_name='Zephyr' echo hello
171+
hello
172+
173+
174+
Step 7: Device Firmware Upgrade
175+
===============================
176+
177+
Now that the SMP server is running on your board and you are able to communicate
178+
with it using :file:`mcumgr`, you might want to test what is commonly called
179+
"OTA DFU", or Over-The-Air Device Firmware Upgrade.
180+
181+
To do this, build a second sample (following the steps below) to verify
182+
it is sent over the air and properly flashed into slot-1, and then
183+
swapped into slot-0 by MCUboot.
184+
185+
Build a second sample
186+
---------------------
187+
188+
Perhaps the easiest sample to test with is the :file:`samples/hello_world`
189+
sample provided by Zephyr, documented in the :ref:`hello_world` section.
190+
191+
Edit :file:`samples/hello_world/prj.conf` and enable the required MCUboot
192+
Kconfig option as described in :ref:`mcuboot` by adding the following line to
193+
it:
113194

114195
.. code-block:: console
115196
116-
$ mcumgr --conntype ble --connstring peer_name=Zephyr echo hello
117-
hello
197+
CONFIG_BOOTLOADER_MCUBOOT=y
198+
199+
Then build the sample as usual (see :ref:`hello_world`).
200+
201+
Sign the second sample
202+
----------------------
203+
204+
Next you will need to sign the sample just like you did for :file:`smp_svr`,
205+
since it needs to be loaded by MCUboot.
206+
Follow the same instructions described in :ref:`smp_svr_sample_sign`,
207+
but this time you must use a :file:`.bin` image, since :file:`mcumgr` does not
208+
yet support :file:`.hex` files.
209+
210+
Upload the image over BLE
211+
-------------------------
212+
213+
Now we are ready to send or upload the image over BLE to the target remote
214+
device.
215+
216+
.. code-block:: console
217+
218+
sudo mcumgr --conntype ble --connstring ctlr_name=hci0,peer_name='Zephyr' image upload signed.bin
219+
220+
If all goes well the image will now be stored in slot-1, ready to be swapped
221+
into slot-0 and executed.
222+
223+
List the images
224+
---------------
225+
226+
We can now obtain a list of images (slot-0 and slot-1) present in the remote
227+
target device by issuing the following command:
228+
229+
.. code-block:: console
230+
231+
sudo mcumgr --conntype ble --connstring ctlr_name=hci0,peer_name='Zephyr' image list
232+
233+
This should print the status and hash values of each of the images present.
234+
235+
Test the image
236+
--------------
237+
238+
In order to instruct MCUboot to swap the images we need to test the image first,
239+
making sure it boots:
240+
241+
.. code-block:: console
242+
243+
sudo mcumgr --conntype ble --connstring ctlr_name=hci0,peer_name='Zephyr' image test <hash of slot-1 image>
244+
245+
Now MCUBoot will swap the image on the next reset.
246+
247+
Reset remotely
248+
--------------
249+
250+
We can reset the device remotely to observe (use the console output) how
251+
MCUboot swaps the images:
252+
253+
.. code-block:: console
254+
255+
sudo mcumgr --conntype ble --connstring ctlr_name=hci0,peer_name='Zephyr' reset
256+
257+
Upon reset MCUboot will swap slot-0 and slot-1.
258+
259+
The new image is the basic ``hello_world`` sample that does not contain
260+
SMP or BLE functionality, so we cannot communicate with it using
261+
:file:`mcumgr`. Instead simply reset the board manually to force MCUboot
262+
to revert (i.e. swap back the images) due to the fact that the new image has
263+
not been confirmed.
264+
265+
If you had instead built and uploaded a new image based on ``smp_svr``
266+
(or another BLE and SMP enabled sample), you could confirm the
267+
new image and make the swap permanent by using this command:
268+
269+
.. code-block:: console
270+
271+
sudo mcumgr --conntype ble --connstring ctlr_name=hci0,peer_name='Zephyr' image confirm
272+
273+
Note that if you try to send the very same image that is already flashed in
274+
slot-0 then the procedure will not complete successfully since the hash values
275+
for both slots will be identical.
276+

0 commit comments

Comments
 (0)