blob: 0decd26ad65655d4ef6ce0251dde08d9f773dffe [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
164 -DTFM_PLATFORM=arm/mps3/an552
165 -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
168 -DCRYPTO_ENGINE_BUF_SIZE=0x8000 -DITS_MAX_ASSET_SIZE=1300
169 -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
172 -DCONFIG_TFM_ENABLE_MVE=ON
173
174The application also can be run without MVE support, in that case the
175``-DCONFIG_TFM_ENABLE_MVE=ON`` flags should be omitted, and the
176``configENABLE_MVE`` can be set to ``0`` in the
177``ns_side/amazon-freertos/FreeRTOSConfig.h`` file.
178Our measurements showed that MVE speeds up the frequency calculation by 10
179times with release GCC build.
180
181You can check TF-M's build instructions
182`here <https://tf-m-user-guide.trustedfirmware.org/docs/technical_references/instructions/tfm_build_instruction.html>`__.
183
184-----------------------
185Running the application
186-----------------------
187
188It is covered by the generic TF-M run instructions for AN552
189`here <https://tf-m-user-guide.trustedfirmware.org/platform/ext/target/arm/mps3/an552/README.html>`__.
190
191---------------------------
192Testing the voice algorithm
193---------------------------
194
195Start up the board, wait until ``==== Start listening ====`` is written on the
196serial console and start talking, or make some noise. You can check that the
197``Voice detected with most energy at X Hz`` message is written onto the serial
198console, and the same message is sent to AWS cloud.
199
200For checking the AWS messages:
201
202#. In the left navigation pane of the AWS IoT console, choose ``Test``.
203#. Define ``<Name of your thing>/vad_an552`` as the topic filter.
204#. Click on ``Subscribe``.
205#. Once a message is sent to AWS cloud you should see it on this page.
206
207.. note::
208
209 For this test it is recommended to find a quiet environment, because any
210 noise can trigger the voice activity algorithm.
211
212For testing the frequency calculation pure sine signals should be used,
213the accuracy is about +/- 100 Hz.
214
215----------------------
216Testing Amazon AWS OTA
217----------------------
218
219To run an OTA update a new image must be created with higher version number.
220This can be easily done by rebuilding the solution with the following cmake
221flag: -DMCUBOOT_IMAGE_VERSION_S=2.1.0. (The version itself can be anything, but
222must be higher than the version of the currently running image.)
223
224The image signature must be extracted from the final binary, can be done by
225openssl running the following commands in the build directory:
226
227#. ``openssl dgst -sha256 -binary -out update-digest.bin tfm_s_ns_signed.bin``
228#. ``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``
229#. ``openssl base64 -A -in update-signature.bin -out update-signature.txt``
230
231Once the signature extracted into ``update-signature.txt`` file, the OTA job
232can be created:
233
234#. `Create an Amazon S3 bucket to store your update <https://docs.aws.amazon.com/freertos/latest/userguide/dg-ota-bucket.html>`__.
235#. `Create an OTA Update service role <https://docs.aws.amazon.com/freertos/latest/userguide/create-service-role.html>`__.
236#. `Create an OTA user policy <https://docs.aws.amazon.com/freertos/latest/userguide/create-ota-user-policy.html>`__.
237#. Go to AWS IoT web interface and choose ``Manage`` and then ``Jobs``.
238#. Click the create job button and select ``Create FreeRTOS OTA update job``.
239#. Give it a name and click next.
240#. Select the device to update (the Thing you created in earlier steps).
241#. Select ``MQTT`` transport only.
242#. Select ``Use my custom signed file``.
243#. Paste the signature string from the ``update-signature.txt`` file. Make sure
244 that it is pasted as it is without any whitespace characters.
245#. Select ``SHA-256`` and ``RSA`` algorithms.
246#. For ``Path name of code signing certificate on device`` put in ``0``
247 (the path is not used).
248#. Select upload new file and select the signed update binary
249 ``tfm_s_ns_signed.bin``.
250#. Select the S3 bucket you created to upload the binary to.
251#. For ``Path name of file on device`` put in ``full image``.
252#. As the role, select the OTA role you created.
253#. Click next.
254#. Click next, your update job is ready and running. If your board is running
255 (or the next time it will be turned on) the update will be performed.
256
257After the update happened the system resets, and the image version is written
258onto the serial console. That way the update can be verified.
259
260.. note::
261
262 The OTA process only updates the image stored in RAM, so if the MPS3 board
263 is power cycled the system will boot up with the original image. The FPGA at
264 power-on loads the application image from the SD card to RAM, and the SD
265 card content is not changed during OTA.
266
267-------------
268
269*Copyright (c) 2021-2022, Arm Limited. All rights reserved.*