Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 1 | ######################################### |
| 2 | Voice Activity Detection demo application |
| 3 | ######################################### |
| 4 | |
Anton Komlev | 3688278 | 2024-04-10 21:00:30 +0100 | [diff] [blame] | 5 | .. toctree:: |
| 6 | :maxdepth: 1 |
| 7 | |
| 8 | Threat Model <threat_model> |
| 9 | |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 10 | A 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 | --------------- |
| 17 | Brief Operation |
| 18 | --------------- |
| 19 | |
| 20 | After boot-up the application first checks whether Over-the-Air update (OTA) |
| 21 | was initiated from AWS cloud. If yes, the OTA process is executed, otherwise |
| 22 | the voice activity detection algorithm is started on the secure side. While the |
| 23 | algorithm is running the non-secure side keep polling it for results. After a |
| 24 | minute the algorithm is stopped, and the operation restarted with the OTA check |
| 25 | again. |
| 26 | |
| 27 | If the algorithm detects voice, a short audio sample (~100 ms) is recorded, and |
| 28 | the highest energy frequency component is calculated. This frequency is written |
| 29 | onto the serial line and it is sent to AWS cloud. Then the algorithm is |
| 30 | restarted or the OTA check is started if the timeout is up. |
| 31 | |
| 32 | By default the solution requires ethernet connectivity, it will not start the |
| 33 | main 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 | --------------- |
| 42 | HW 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 | ------------------ |
| 52 | Build instructions |
| 53 | ------------------ |
| 54 | |
| 55 | ********************************************************* |
| 56 | AWS thing creation and source update with the credentials |
| 57 | ********************************************************* |
| 58 | |
| 59 | By default it is required to create an IoT thing in AWS to run the application, |
| 60 | but this can be skipped if ``-DVAD_AN552_NO_CONNECTIVITY=ON`` is added for |
| 61 | cmake. |
| 62 | |
Elena Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 63 | ----------------------------------- |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 64 | Create an IoT thing for your device |
Elena Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 65 | ----------------------------------- |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 66 | |
| 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 Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 83 | --------------- |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 84 | Create a policy |
Elena Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 85 | --------------- |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 86 | |
| 87 | For the sake of simplicity in this example a very permissive Policy is created, |
| 88 | for 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 Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 116 | --------------------------------------- |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 117 | Attach the created policy to your thing |
Elena Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 118 | --------------------------------------- |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 119 | |
| 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 Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 128 | ------------------------------------ |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 129 | Update source with thing credentials |
Elena Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 130 | ------------------------------------ |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 131 | |
| 132 | Edit `examples/vad_an552/ns_side/amazon-freertos/aws_clientcredential.h` file and |
| 133 | set 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 | |
| 142 | Recreate or update examples/vad_an552/ns_side/amazon-freertos/aws_clientcredential_keys.h` |
| 143 | with the downloaded certificate and keys. |
| 144 | |
| 145 | Recreate 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 | |
| 155 | Alternatively, the file can be updated by hand by setting the values of the |
| 156 | following 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 Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 162 | ------------------ |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 163 | Running TF-M build |
Elena Uziunaite | 3ad0ecc | 2023-10-27 15:15:35 +0100 | [diff] [blame] | 164 | ------------------ |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 165 | |
| 166 | For building TF-M's build system is used with the following mandatory CMAKE |
| 167 | flags:: |
| 168 | |
Bence Balogh | 57867a9 | 2023-09-12 10:31:26 +0200 | [diff] [blame] | 169 | -DTFM_PLATFORM=arm/mps3/corstone300/an552 |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 170 | -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 Balogh | 3be9fdd | 2022-11-23 11:42:25 +0100 | [diff] [blame] | 173 | -DPROJECT_CONFIG_HEADER_FILE=<path-to-tf-m-extras-repo>/examples/vad_an552/ns_side/project_config.h |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 174 | -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 Balogh | 3be9fdd | 2022-11-23 11:42:25 +0100 | [diff] [blame] | 177 | -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 Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 182 | |
| 183 | The 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. |
| 187 | Our measurements showed that MVE speeds up the frequency calculation by 10 |
| 188 | times with release GCC build. |
| 189 | |
| 190 | You can check TF-M's build instructions |
Elena Uziunaite | 676a146 | 2023-11-14 16:58:45 +0000 | [diff] [blame] | 191 | :doc:`here <TF-M:building/tfm_build_instruction>`. |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 192 | |
| 193 | ----------------------- |
| 194 | Running the application |
| 195 | ----------------------- |
| 196 | |
| 197 | It is covered by the generic TF-M run instructions for AN552 |
Elena Uziunaite | 676a146 | 2023-11-14 16:58:45 +0000 | [diff] [blame] | 198 | `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 Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 199 | |
| 200 | --------------------------- |
| 201 | Testing the voice algorithm |
| 202 | --------------------------- |
| 203 | |
| 204 | Start up the board, wait until ``==== Start listening ====`` is written on the |
| 205 | serial 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 |
| 207 | console, and the same message is sent to AWS cloud. |
| 208 | |
| 209 | For 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 | |
| 221 | For testing the frequency calculation pure sine signals should be used, |
| 222 | the accuracy is about +/- 100 Hz. |
| 223 | |
| 224 | ---------------------- |
| 225 | Testing Amazon AWS OTA |
| 226 | ---------------------- |
| 227 | |
| 228 | To run an OTA update a new image must be created with higher version number. |
| 229 | This can be easily done by rebuilding the solution with the following cmake |
Bence Balogh | 3be9fdd | 2022-11-23 11:42:25 +0100 | [diff] [blame] | 230 | flag: ``-DMCUBOOT_IMAGE_VERSION_S=2.1.0``. (The version itself can be anything, but |
| 231 | must 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 |
| 233 | config, because the demo going to confirm the new image after downloading it. |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 234 | |
| 235 | The image signature must be extracted from the final binary, can be done by |
| 236 | openssl 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 | |
| 242 | Once the signature extracted into ``update-signature.txt`` file, the OTA job |
| 243 | can 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 Balogh | 3be9fdd | 2022-11-23 11:42:25 +0100 | [diff] [blame] | 262 | #. For ``Path name of file on device`` put in ``combined image``. |
Mark Horvath | bc58a68 | 2021-11-16 13:53:49 +0100 | [diff] [blame] | 263 | #. 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 | |
| 268 | After the update happened the system resets, and the image version is written |
| 269 | onto 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 Balogh | 57867a9 | 2023-09-12 10:31:26 +0200 | [diff] [blame] | 280 | *Copyright (c) 2021-2023, Arm Limited. All rights reserved.* |