blob: 943dcebb4eff8229965f9a1fdce56347d1d31f5e [file] [log] [blame]
Mark Horvathbc58a682021-11-16 13:53:49 +01001#########################################
2Voice Activity Detection demo application
3#########################################
4
Anton Komlev36882782024-04-10 21:00:30 +01005.. toctree::
6 :maxdepth: 1
7
8 Threat Model <threat_model>
9
Mark Horvathbc58a682021-11-16 13:53:49 +010010A demo application for the AN552 FPGA showcasing:
11
12* secure partition using MVE to speed up algorithms
13* secure peripheral usage from secure partition with interrupt handling
14* AWS cloud connectivity with OTA on the non-secure side
15
16---------------
17Brief Operation
18---------------
19
20After boot-up the application first checks whether Over-the-Air update (OTA)
21was initiated from AWS cloud. If yes, the OTA process is executed, otherwise
22the voice activity detection algorithm is started on the secure side. While the
23algorithm is running the non-secure side keep polling it for results. After a
24minute the algorithm is stopped, and the operation restarted with the OTA check
25again.
26
27If the algorithm detects voice, a short audio sample (~100 ms) is recorded, and
28the highest energy frequency component is calculated. This frequency is written
29onto the serial line and it is sent to AWS cloud. Then the algorithm is
30restarted or the OTA check is started if the timeout is up.
31
32By default the solution requires ethernet connectivity, it will not start the
33main operation until the network is up. This can be overwritten if the
34``-DVAD_AN552_NO_CONNECTIVITY=ON`` cmake flag is defined. The effect is:
35
36* No need for Ethernet connection.
37* No need for IoT thing creation in AWS cloud and source update with
38 its credentials.
39* OTA check and AWS cloud communication is not executed.
40
41---------------
42HW requirements
43---------------
44
45* AN552 Version 2.0 FPGA image on MPS3 board.
46* Ethernet connection with access to the internet. (Not needed if
47 ``-DVAD_AN552_NO_CONNECTIVITY=ON`` is added for cmake.)
48* 2 or 3 pole microphone connected into the audio connector. In case of a
49 stereo microphone only the right channel is used.
50
51------------------
52Build instructions
53------------------
54
55*********************************************************
56AWS thing creation and source update with the credentials
57*********************************************************
58
59By default it is required to create an IoT thing in AWS to run the application,
60but this can be skipped if ``-DVAD_AN552_NO_CONNECTIVITY=ON`` is added for
61cmake.
62
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +010063-----------------------------------
Mark Horvathbc58a682021-11-16 13:53:49 +010064Create an IoT thing for your device
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +010065-----------------------------------
Mark Horvathbc58a682021-11-16 13:53:49 +010066
67#. Login to your account and browse to the `AWS IoT console <https://console.aws.amazon.com/iotv2/>`__.
68#. In the left navigation pane, choose ``All devices``, and then choose ``Things``.
69#. Click on ``Create things``.
70#. Choose ``Create single thing``.
71#. At the ``Specify thing properties`` page add the name of your thing at
72 ``Thing name``. You will need to add the name later to your C code. Click
73 ``Next``.
74#. At the ``Configure device certificate`` page choose ``Auto-generate a new
75 certificate``, and click ``Next``.
76#. The thing can be created by clicking on ``Create thing`` at the
77 ``Attach policies to certificate`` page. The policy will be created at the
78 next section.
79#. Download the key files and the certificate, and make a note of the name of
80 the certificate.
81#. Activate your certificate if it is not active by default.
82
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +010083---------------
Mark Horvathbc58a682021-11-16 13:53:49 +010084Create a policy
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +010085---------------
Mark Horvathbc58a682021-11-16 13:53:49 +010086
87For the sake of simplicity in this example a very permissive Policy is created,
88for production usage a more restrictive one is recommended.
89
90#. In the navigation pane of the AWS IoT console, choose ``Security``, and then
91 choose ``Policies``.
92#. At the ``Policies`` page, choose ``Create policy``.
93#. At the ``Create a policy`` page, enter a name for the policy.
94#. At the Policy document click on JSON, and paste the following snippet into the
95 Policy document textbox, then click on ``Create``. (``Region`` and
96 ``Account ID`` must be updated.)
97
98.. code-block:: JSON
99
100 {
101 "Version": "2012-10-17",
102 "Statement": [
103 {
104 "Effect": "Allow",
105 "Action": [
106 "iot:Connect",
107 "iot:Publish",
108 "iot:Subscribe",
109 "iot:Receive"
110 ],
111 "Resource": "arn:aws:iot:<Region>:<Account ID without dashes>:*"
112 }
113 ]
114 }
115
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +0100116---------------------------------------
Mark Horvathbc58a682021-11-16 13:53:49 +0100117Attach the created policy to your thing
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +0100118---------------------------------------
Mark Horvathbc58a682021-11-16 13:53:49 +0100119
120#. In the left navigation pane of the AWS IoT console, choose ``Secure``, and
121 then choose ``Certificates``. You should see the certificate that you have
122 created earlier.
123#. Click on the three dots next to the certificate and choose
124 ``Attach policy``.
125#. In the ``Attach policies to certificate(s)`` window choose the created
126 policy and click ``Attach``.
127
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +0100128------------------------------------
Mark Horvathbc58a682021-11-16 13:53:49 +0100129Update source with thing credentials
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +0100130------------------------------------
Mark Horvathbc58a682021-11-16 13:53:49 +0100131
132Edit `examples/vad_an552/ns_side/amazon-freertos/aws_clientcredential.h` file and
133set the value of the following macros:
134
135* `clientcredentialMQTT_BROKER_ENDPOINT`, set this to the endpoint name of your
136 amazon account. To find this go to the AWS IoT console page and in the left
137 navigation pane click on ``Settings``. The Endpoint can be found under
138 ``Device data endpoint``.
139
140* `clientcredentialIOT_THING_NAME`, set this to the name of the created thing.
141
142Recreate or update examples/vad_an552/ns_side/amazon-freertos/aws_clientcredential_keys.h`
143with the downloaded certificate and keys.
144
145Recreate with the html tool from Amazon-FreeRTOS:
146
147#. Clone `Amazon-FreeRTOS <https://github.com/aws/amazon-freertos>`__.
148#. Open ``Amazon-FreeRTOS/tools/certificate_configuration/CertificateConfigurator.html``
149 in your browser.
150#. Upload the downloaded certificate and the private key.
151#. Click on ``Generate and save aws_clientcredential_keys.h``
152#. Download the file and update `examples/vad_an552/ns_side/amazon-freertos/aws_clientcredential_keys.h`
153 with it.
154
155Alternatively, the file can be updated by hand by setting the values of the
156following macros:
157
158* ``keyCLIENT_CERTIFICATE_PEM``, content of ``<your-thing-certificate-unique-string>-certificate.pem.crt``.
159* ``keyCLIENT_PRIVATE_KEY_PEM``, content of ``<your-thing-certificate-unique-string>-private.pem.key``.
160* ``keyCLIENT_PUBLIC_KEY_PEM``, content of ``<your-thing-certificate-unique-string>-public.pem.key``.
161
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +0100162------------------
Mark Horvathbc58a682021-11-16 13:53:49 +0100163Running TF-M build
Elena Uziunaite3ad0ecc2023-10-27 15:15:35 +0100164------------------
Mark Horvathbc58a682021-11-16 13:53:49 +0100165
166For building TF-M's build system is used with the following mandatory CMAKE
167flags::
168
Bence Balogh57867a92023-09-12 10:31:26 +0200169 -DTFM_PLATFORM=arm/mps3/corstone300/an552
Mark Horvathbc58a682021-11-16 13:53:49 +0100170 -DNS_EVALUATION_APP_PATH=<path-to-tf-m-extras-repo>/examples/vad_an552/ns_side
171 -DTFM_EXTRA_PARTITION_PATHS=<path-to-tf-m-extras-repo>/partitions/vad_an552_sp/
172 -DTFM_EXTRA_MANIFEST_LIST_FILES=<path-to-tf-m-extras-repo>/partitions/vad_an552_sp/extra_manifest_list.yaml
Bence Balogh3be9fdd2022-11-23 11:42:25 +0100173 -DPROJECT_CONFIG_HEADER_FILE=<path-to-tf-m-extras-repo>/examples/vad_an552/ns_side/project_config.h
Mark Horvathbc58a682021-11-16 13:53:49 +0100174 -DTFM_PARTITION_FIRMWARE_UPDATE=ON -DMCUBOOT_DATA_SHARING=ON
175 -DMCUBOOT_UPGRADE_STRATEGY=SWAP_USING_SCRATCH
176 -DMCUBOOT_IMAGE_NUMBER=1 -DMCUBOOT_SIGNATURE_KEY_LEN=2048
Bence Balogh3be9fdd2022-11-23 11:42:25 +0100177 -DCONFIG_TFM_ENABLE_MVE=ON -DCONFIG_TFM_SPM_BACKEND=IPC
178 -DPLATFORM_HAS_FIRMWARE_UPDATE_SUPPORT=ON -DTFM_PARTITION_PLATFORM=ON
179 -DTFM_PARTITION_CRYPTO=ON -DTFM_PARTITION_INTERNAL_TRUSTED_STORAGE=ON
180 -DTFM_PARTITION_PROTECTED_STORAGE=ON -DMCUBOOT_CONFIRM_IMAGE=ON
181
Mark Horvathbc58a682021-11-16 13:53:49 +0100182
183The application also can be run without MVE support, in that case the
184``-DCONFIG_TFM_ENABLE_MVE=ON`` flags should be omitted, and the
185``configENABLE_MVE`` can be set to ``0`` in the
186``ns_side/amazon-freertos/FreeRTOSConfig.h`` file.
187Our measurements showed that MVE speeds up the frequency calculation by 10
188times with release GCC build.
189
190You can check TF-M's build instructions
Elena Uziunaite676a1462023-11-14 16:58:45 +0000191:doc:`here <TF-M:building/tfm_build_instruction>`.
Mark Horvathbc58a682021-11-16 13:53:49 +0100192
193-----------------------
194Running the application
195-----------------------
196
197It is covered by the generic TF-M run instructions for AN552
Elena Uziunaite676a1462023-11-14 16:58:45 +0000198`here <https://trustedfirmware-m.readthedocs.io/en/latest/platform/arm/mps3/corstone300/README.html?highlight=an552#build-instructions-with-platform-name-arm-mps3-corstone300-an547>`_.
Mark Horvathbc58a682021-11-16 13:53:49 +0100199
200---------------------------
201Testing the voice algorithm
202---------------------------
203
204Start up the board, wait until ``==== Start listening ====`` is written on the
205serial console and start talking, or make some noise. You can check that the
206``Voice detected with most energy at X Hz`` message is written onto the serial
207console, and the same message is sent to AWS cloud.
208
209For checking the AWS messages:
210
211#. In the left navigation pane of the AWS IoT console, choose ``Test``.
212#. Define ``<Name of your thing>/vad_an552`` as the topic filter.
213#. Click on ``Subscribe``.
214#. Once a message is sent to AWS cloud you should see it on this page.
215
216.. note::
217
218 For this test it is recommended to find a quiet environment, because any
219 noise can trigger the voice activity algorithm.
220
221For testing the frequency calculation pure sine signals should be used,
222the accuracy is about +/- 100 Hz.
223
224----------------------
225Testing Amazon AWS OTA
226----------------------
227
228To run an OTA update a new image must be created with higher version number.
229This can be easily done by rebuilding the solution with the following cmake
Bence Balogh3be9fdd2022-11-23 11:42:25 +0100230flag: ``-DMCUBOOT_IMAGE_VERSION_S=2.1.0``. (The version itself can be anything, but
231must be higher than the version of the currently running image.) The
232``-DMCUBOOT_CONFIRM_IMAGE`` flag should be set to OFF in the new image build
233config, because the demo going to confirm the new image after downloading it.
Mark Horvathbc58a682021-11-16 13:53:49 +0100234
235The image signature must be extracted from the final binary, can be done by
236openssl running the following commands in the build directory:
237
238#. ``openssl dgst -sha256 -binary -out update-digest.bin tfm_s_ns_signed.bin``
239#. ``openssl pkeyutl -sign -pkeyopt digest:sha256 -pkeyopt rsa_padding_mode:pss -pkeyopt rsa_mgf1_md:sha256 -inkey <path to tfm source>/bl2/ext/mcuboot/root-RSA-2048.pem -in update-digest.bin -out update-signature.bin``
240#. ``openssl base64 -A -in update-signature.bin -out update-signature.txt``
241
242Once the signature extracted into ``update-signature.txt`` file, the OTA job
243can be created:
244
245#. `Create an Amazon S3 bucket to store your update <https://docs.aws.amazon.com/freertos/latest/userguide/dg-ota-bucket.html>`__.
246#. `Create an OTA Update service role <https://docs.aws.amazon.com/freertos/latest/userguide/create-service-role.html>`__.
247#. `Create an OTA user policy <https://docs.aws.amazon.com/freertos/latest/userguide/create-ota-user-policy.html>`__.
248#. Go to AWS IoT web interface and choose ``Manage`` and then ``Jobs``.
249#. Click the create job button and select ``Create FreeRTOS OTA update job``.
250#. Give it a name and click next.
251#. Select the device to update (the Thing you created in earlier steps).
252#. Select ``MQTT`` transport only.
253#. Select ``Use my custom signed file``.
254#. Paste the signature string from the ``update-signature.txt`` file. Make sure
255 that it is pasted as it is without any whitespace characters.
256#. Select ``SHA-256`` and ``RSA`` algorithms.
257#. For ``Path name of code signing certificate on device`` put in ``0``
258 (the path is not used).
259#. Select upload new file and select the signed update binary
260 ``tfm_s_ns_signed.bin``.
261#. Select the S3 bucket you created to upload the binary to.
Bence Balogh3be9fdd2022-11-23 11:42:25 +0100262#. For ``Path name of file on device`` put in ``combined image``.
Mark Horvathbc58a682021-11-16 13:53:49 +0100263#. As the role, select the OTA role you created.
264#. Click next.
265#. Click next, your update job is ready and running. If your board is running
266 (or the next time it will be turned on) the update will be performed.
267
268After the update happened the system resets, and the image version is written
269onto the serial console. That way the update can be verified.
270
271.. note::
272
273 The OTA process only updates the image stored in RAM, so if the MPS3 board
274 is power cycled the system will boot up with the original image. The FPGA at
275 power-on loads the application image from the SD card to RAM, and the SD
276 card content is not changed during OTA.
277
278-------------
279
Bence Balogh57867a92023-09-12 10:31:26 +0200280*Copyright (c) 2021-2023, Arm Limited. All rights reserved.*