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