Download, build, flash and run the FreeRTOS OTA demo on the Renesas RX65N - FreeRTOS

Download, build, flash and run the FreeRTOS OTA demo on the Renesas RX65N

Important

This reference integration is hosted on the Amazon-FreeRTOS repository which is deprecated. We recommend that you start here when you create a new project. If you already have an existing FreeRTOS project based on the now deprecated Amazon-FreeRTOS repository, see the Amazon-FreeRTOS Github Repository Migration Guide.

This chapter shows you how download, build, flash and run the FreeRTOS OTA demo applications on the Renesas RX65N.

Set up your operating environment

The procedures in this section use the following environments:

  • IDE: e2 studio 7.8.0, e2 studio 2020-07

  • Toolchains: CCRX Compiler v3.0.1

  • Target devices: RSKRX65N-2MB

  • Debuggers: E2, E2 Lite emulator

  • Software: Renesas Flash Programmer, Renesas Secure Flash Programmer.exe, Tera Term

To set up your hardware
  1. Connect the E2 Lite emulator and USB serial port to your RX65N board and PC.

  2. Connect the power source to the RX65N.

Set up your AWS resources

  1. To run the FreeRTOS demos, you must have an AWS account with an IAM user that has permission to access AWS IoT services. If you haven't already, follow the steps in Setting up your AWS account and permissions.

  2. To set up for OTA updates, follow the steps in OTA update prerequisites. In particular, follow the steps in Prerequisites for OTA updates using MQTT.

  3. Open the AWS IoT console.

  4. In the left navigation pane, choose Manage, then choose Things to create a thing.

    A thing is a representation of a device or logical entity in AWS IoT. It can be a physical device or sensor (for example, a light bulb or a switch on a wall). It can also be a logical entity like an instance of an application or physical entity that doesn't connect to AWS IoT, but is related to devices that do (for example, a car that has engine sensors or a control panel). AWS IoT provides a thing registry that helps you manage your things.

    1. Choose Create, then choose Create a single thing.

    2. Enter a Name for your thing, then choose Next.

    3. Choose Create certificate.

    4. Download the three files that are created and then choose Activate.

    5. Choose Attach a policy.

      console screen showing files to be downloaded
    6. Select the policy that you created in Device policy.

      Each device that receives an OTA update using MQTT must be registered as a thing in AWS IoT and the thing must have an attached policy like the one listed. You can find more information about the items in the "Action" and "Resource" objects at AWS IoT Core Policy Actions and AWS IoT Core Action Resources.

      Notes
      • The iot:Connect permissions allow your device to connect to AWS IoT over MQTT.

      • The iot:Subscribe and iot:Publish permissions on the topics of AWS IoT jobs (.../jobs/*) allow the connected device to receive job notifications and job documents, and to publish the completion state of a job execution.

      • The iot:Subscribe and iot:Publish permissions on the topics of AWS IoT OTA streams (.../streams/*) allow the connected device to fetch OTA update data from AWS IoT. These permissions are required to perform firmware updates over MQTT.

      • The iot:Receive permissions allow AWS IoT Core to publish messages on those topics to the connected device. This permission is checked on every delivery of an MQTT message. You can use this permission to revoke access to clients that are currently subscribed to a topic.

  5. To create a code-signing profile and register a code-signing certificate on AWS.

    1. To create the keys and certification, see section 7.3 "Generating ECDSA-SHA256 Key Pairs with OpenSSL" in Renesas MCU Firmware Update Design Policy.

    2. Open the AWS IoT console. In the left navigation pane, select Manage, then Jobs. Select Create a job then Create OTA update Job.

    3. Under Select devices to update choose Select then choose the thing you created previously. Select Next.

    4. On the Create a FreeRTOS OTA update job page, do the following:

      1. For Select the protocol for firmware image transfer, choose MQTT.

      2. For Select and sign your firmware image, choose Sign a new firmware image for me.

      3. For Code signing profile, choose Create.

      4. In the Create a code signing profile window, enter a Profile name. For the Device hardware platform select Windows Simulator. For the Code signing certificate choose Import.

      5. Browse to select the certificate (secp256r1.crt), the certificate private key (secp256r1.key), and the certificate chain (ca.crt).

      6. Enter a Pathname of code signing certificate on device. Then choose Create.

  6. To grant access to code signing for AWS IoT, follow the steps in Grant access to code signing for AWS IoT.

If don't have Tera Term installed on your PC, you can download it from https://ttssh2.osdn.jp/index.html.en and set it up as shown here. Make sure that you plug in the USB Serial port from your device to your PC.

Tera Term serial port setup window

Import, configure the header file and build aws_demos and boot_loader

To begin, you select the latest version of the FreeRTOS package, and this will be downloaded from GitHub and imported automatically into the project. This way you can focus on the configuring FreeRTOS and writing application code.

  1. Launch e2 studio.

  2. Choose File, and then choose Import….

  3. Select Renesas GitHub FreeRTOS (with IoT libraries) Project.

    e-squared studio import window
  4. Choose Check for more version… to show the download dialog box.

    e-squared studio download dialog window
  5. Select the latest package.

    e-squared studio module download dialog window
  6. Choose Agree to accept the end user license agreement.

    e-squared studio EULA dialog
  7. Wait for the download to complete.

    download progress bar
  8. Select the aws_demos and boot_loader projects, then choose Finish to import them.

    import projects window
  9. For both projects, open the project properties. In the navigation pane, choose Tool Chain Editor.

    1. Choose the Current toolchain.

    2. Choose the Current builder.

    e-squared studio properties window
  10. In the navigation pane, choose Settings. Choose the Toolchain tab, and then choose the toolchain Version.

    Toolchain integration settings for Renesas CCRX Version v3.01.00, with option to change toolchain.

    Choose the Tool Settings tab, expand Converter and then choose Output. In the main window, make sure Output hex file is selected, and then choose the Output file type.

    C/C++ Build configuration settings window showing compiler and linker options like output hex file, output file type, output directory, and file division options.
    Interface settings tree with options for Stack Analysis, Tool Chain Editor, C/C++ General, MCU, Project References, etc.
  11. In the bootloader project, open projects\renesas\rx65n-rsk\e2studio\boot_loader\src\key\code_signer_public_key.h and input the public key. For information on how to create a public key, see How to implement FreeRTOS OTA by using Amazon Web Services on RX65N and section 7.3 "Generating ECDSA-SHA256 Key Pairs with OpenSSL" in Renesas MCU Firmware Update Design Policy.

    Code editor showing a C header file with definition for CODE_SIGNER_PUBLIC_KEY and a PEM-encoded code signer public key variable.

    Then build the project to create boot_loader.mot.

  12. Open the aws_demos project.

    1. Open the AWS IoT console.

    2. In the left navigation pane, choose Settings. Make a note of your custom endpoint in the Device data endpoint text box.

    3. Choose Manage, and then choose Things. Make a note of the AWS IoT thing name of your board.

    4. In the aws_demos project, open demos/include/aws_clientcredential.h and specify the following values.

      #define clientcredentialMQTT_BROKER_ENDPOINT[] = "Your AWS IoT endpoint"; #define clientcredentialIOT_THING_NAME "The AWS IoT thing name of your board"
      Code snippet showing AWS IoT thing name and broker endpoint configuration settings.
    5. Open the tools/certificate_configuration/CertificateConfigurator.html file.

    6. Import the certificate PEM file and Private Key PEM file that you downloaded earlier.

    7. Choose Generate and save aws_clientcredential_keys.h and replace this file in the demos/include/ directory.

      Certificate Configuration Tool with fields to provide client certificate and private key PEM files from the AWS IoT Console, and a button to generate and save aws_clientcredential_keys.h file.
    8. Open the vendors/renesas/boards/rx65n-rsk/aws_demos/config_files/ota_demo_config.h file, and specify these values.

      #define otapalconfigCODE_SIGNING_CERTIFICATE [] = "your-certificate-key";

      Where your-certificate-key is the value from the file secp256r1.crt. Remember to add "\" after each line in the certification. For more information on creating the secp256r1.crt file, see How to implement FreeRTOS OTA by using Amazon Web Services on RX65N and section 7.3 "Generating ECDSA-SHA256 Key Pairs with OpenSSL" in Renesas MCU Firmware Update Design Policy.

      Source code file showing C code defining a PEM-encoded code signer certificate constant string with redacted certificate data.
  13. Task A: Install the initial version of the firmware

    1. Open the vendors/renesas/boards/board/aws_demos/config_files/aws_demo_config.h file, comment out #define CONFIG_CORE_MQTT_MUTUAL_AUTH_DEMO_ENABLED, and define either CONFIG_OTA_MQTT_UPDATE_DEMO_ENABLED or CONFIG_OTA_HTTP_UPDATE_DEMO_ENABLED.

    2. Open the demos/include/ aws_application_version.h file, and set the initial version of the firmware to 0.9.2.

      Code snippet showing version definitions for an application, including macros for major, minor, and build version numbers.
    3. Change the following settings in the Section Viewer.

      Section viewer window showing memory addresses, section names like SU, SI, registers, and interface components like network buffers, exceptions, and action buttons.
    4. Choose Build to create the aws_demos.mot file.

  14. Create the file userprog.mot with the Renesas Secure Flash Programmer. userprog.mot is a combination of aws_demos.mot and boot_loader.mot. You can flash this file to the RX65N-RSK to install the initial firmware.

    1. Download https://github.com/renesas/Amazon-FreeRTOS-Tools and open Renesas Secure Flash Programmer.exe.

    2. Choose the Initial Firm tab and then set the following parameters:

      • Private Key Path – The location of secp256r1.privatekey.

      • Boot Loader File Path– The location of boot_loader.mot (projects\renesas\rx65n-rsk\e2studio\boot_loader\HardwareDebug).

      • File Path – The location of the aws_demos.mot (projects\renesas\rx65n-rsk\e2studio\aws_demos\HardwareDebug).

      Renesas Secure Flash Programmer window with MCU, firmware verification, sequence number, AES key path, and file path fields.
    3. Create a directory named init_firmware, Generate userprog.mot, and save it to the init_firmware directory. Verify that the generate succeeded.

  15. Flash the initial firmware on the RX65N-RSK.

    1. Download the latest version of the Renesas Flash Programmer (Programming GUI) from https://www.renesas.com/tw/en/products/software-tools/tools/programmer/renesas-flash-programmer-programming-gui.html.

    2. Open the vendors\renesas\rx_mcu_boards\boards\rx65n-rsk\aws_demos\flash_project\erase_from_bank\ erase.rpj file to erase data on the bank.

    3. Choose Start to erase the bank.

      Renesas Flash Programmer window showing RX Group microcontroller project details, file path, and flash operation options like Erase, Program, and Verify with Start and OK buttons.
    4. To flash userprog.mot, choose Browse... and navigate to the init_firmware directory, select the userprog.mot file and choose Start.

      Renesas Flash Programmer window showing erase operation settings, including microcontroller RX Group, option to browse program file, Erase and Start buttons, and status details on selected blocks to erase.
  16. Version 0.9.2 (initial version) of the firmware was installed to your RX65N-RSK. The RX65N-RSK board is now listening for OTA updates. If you have opened Tera Term on your PC, you see something like the following when the initial firmware runs.

    ------------------------------------------------- RX65N secure boot program ------------------------------------------------- Checking flash ROM status. bank 0 status = 0xff [LIFECYCLE_STATE_BLANK] bank 1 status = 0xfc [LIFECYCLE_STATE_INSTALLING] bank info = 1. (start bank = 0) start installing user program. copy secure boot (part1) from bank0 to bank1...OK copy secure boot (part2) from bank0 to bank1...OK update LIFECYCLE_STATE from [LIFECYCLE_STATE_INSTALLING] to [LIFECYCLE_STATE_VALID] bank1(temporary area) block0 erase (to update LIFECYCLE_STATE)...OK bank1(temporary area) block0 write (to update LIFECYCLE_STATE)...OK swap bank... ------------------------------------------------- RX65N secure boot program ------------------------------------------------- Checking flash ROM status. bank 0 status = 0xf8 [LIFECYCLE_STATE_VALID] bank 1 status = 0xff [LIFECYCLE_STATE_BLANK] bank info = 0. (start bank = 1) integrity check scheme = sig-sha256-ecdsa bank0(execute area) on code flash integrity check...OK jump to user program 0 1 [ETHER_RECEI] Deferred Interrupt Handler Task started 1 1 [ETHER_RECEI] Network buffers: 3 lowest 3 2 1 [ETHER_RECEI] Heap: current 234192 lowest 234192 3 1 [ETHER_RECEI] Queue space: lowest 8 4 1 [IP-task] InitializeNetwork returns OK 5 1 [IP-task] xNetworkInterfaceInitialise returns 0 6 101 [ETHER_RECEI] Heap: current 234592 lowest 233392 7 2102 [ETHER_RECEI] prvEMACHandlerTask: PHY LS now 1 8 3001 [IP-task] xNetworkInterfaceInitialise returns 1 9 3092 [ETHER_RECEI] Network buffers: 2 lowest 2 10 3092 [ETHER_RECEI] Queue space: lowest 7 11 3092 [ETHER_RECEI] Heap: current 233320 lowest 233320 12 3193 [ETHER_RECEI] Heap: current 233816 lowest 233120 13 3593 [IP-task] vDHCPProcess: offer c0a80a09ip 14 3597 [ETHER_RECEI] Heap: current 233200 lowest 233000 15 3597 [IP-task] vDHCPProcess: offer c0a80a09ip 16 3597 [IP-task] IP Address: 192.168.10.9 17 3597 [IP-task] Subnet Mask: 255.255.255.0 18 3597 [IP-task] Gateway Address: 192.168.10.1 19 3597 [IP-task] DNS Server Address: 192.168.10.1 20 3600 [Tmr Svc] The network is up and running 21 3622 [Tmr Svc] Write certificate... 22 3697 [ETHER_RECEI] Heap: current 232320 lowest 230904 23 4497 [ETHER_RECEI] Heap: current 226344 lowest 225944 24 5317 [iot_thread] [INFO ][DEMO][5317] ---------STARTING DEMO--------- 25 5317 [iot_thread] [INFO ][INIT][5317] SDK successfully initialized. 26 5317 [iot_thread] [INFO ][DEMO][5317] Successfully initialized the demo. Network type for the demo: 4 27 5317 [iot_thread] [INFO ][MQTT][5317] MQTT library successfully initialized. 28 5317 [iot_thread] [INFO ][DEMO][5317] OTA demo version 0.9.2 29 5317 [iot_thread] [INFO ][DEMO][5317] Connecting to broker... 30 5317 [iot_thread] [INFO ][DEMO][5317] MQTT demo client identifier is rx65n-gr-rose (length 13). 31 5325 [ETHER_RECEI] Heap: current 206944 lowest 206504 32 5325 [ETHER_RECEI] Heap: current 206440 lowest 206440 33 5325 [ETHER_RECEI] Heap: current 206240 lowest 206240 38 5334 [ETHER_RECEI] Heap: current 190288 lowest 190288 39 5334 [ETHER_RECEI] Heap: current 190088 lowest 190088 40 5361 [ETHER_RECEI] Heap: current 158512 lowest 158168 41 5363 [ETHER_RECEI] Heap: current 158032 lowest 158032 42 5364 [ETHER_RECEI] Network buffers: 1 lowest 1 43 5364 [ETHER_RECEI] Heap: current 156856 lowest 156856 44 5364 [ETHER_RECEI] Heap: current 156656 lowest 156656 46 5374 [ETHER_RECEI] Heap: current 153016 lowest 152040 47 5492 [ETHER_RECEI] Heap: current 141464 lowest 139016 48 5751 [ETHER_RECEI] Heap: current 140160 lowest 138680 49 5917 [ETHER_RECEI] Heap: current 138280 lowest 138168 59 7361 [iot_thread] [INFO ][MQTT][7361] Establishing new MQTT connection. 62 7428 [iot_thread] [INFO ][MQTT][7428] (MQTT connection 81cfc8, CONNECT operation 81d0e8) Wait complete with result SUCCESS. 63 7428 [iot_thread] [INFO ][MQTT][7428] New MQTT connection 4e8c established. 64 7430 [iot_thread] [OTA_AgentInit_internal] OTA Task is Ready. 65 7430 [OTA Agent T] [prvOTAAgentTask] Called handler. Current State [Ready] Event [Start] New state [RequestingJob] 66 7431 [OTA Agent T] [INFO ][MQTT][7431] (MQTT connection 81cfc8) SUBSCRIBE operation scheduled. 67 7431 [OTA Agent T] [INFO ][MQTT][7431] (MQTT connection 81cfc8, SUBSCRIBE operation 818c48) Waiting for operation completion. 68 7436 [ETHER_RECEI] Heap: current 128248 lowest 127992 69 7480 [OTA Agent T] [INFO ][MQTT][7480] (MQTT connection 81cfc8, SUBSCRIBE operation 818c48) Wait complete with result SUCCESS. 70 7480 [OTA Agent T] [prvSubscribeToJobNotificationTopics] OK: $aws/things/rx65n-gr-rose/jobs/$next/get/accepted 71 7481 [OTA Agent T] [INFO ][MQTT][7481] (MQTT connection 81cfc8) SUBSCRIBE operation scheduled. 72 7481 [OTA Agent T] [INFO ][MQTT][7481] (MQTT connection 81cfc8, SUBSCRIBE operation 818c48) Waiting for operation completion. 73 7530 [OTA Agent T] [INFO ][MQTT][7530] (MQTT connection 81cfc8, SUBSCRIBE operation 818c48) Wait complete with result SUCCESS. 74 7530 [OTA Agent T] [prvSubscribeToJobNotificationTopics] OK: $aws/things/rx65n-gr-rose/jobs/notify-next 75 7530 [OTA Agent T] [prvRequestJob_Mqtt] Request #0 76 7532 [OTA Agent T] [INFO ][MQTT][7532] (MQTT connection 81cfc8) MQTT PUBLISH operation queued. 77 7532 [OTA Agent T] [INFO ][MQTT][7532] (MQTT connection 81cfc8, PUBLISH operation 818b80) Waiting for operation completion. 78 7552 [OTA Agent T] [INFO ][MQTT][7552] (MQTT connection 81cfc8, PUBLISH operation 818b80) Wait complete with result SUCCESS. 79 7552 [OTA Agent T] [prvOTAAgentTask] Called handler. Current State [RequestingJob] Event [RequestJobDocument] New state [WaitingForJob] 80 7552 [OTA Agent T] [prvParseJSONbyModel] Extracted parameter [ clientToken: 0:rx65n-gr-rose ] 81 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: execution 82 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: jobId 83 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: jobDocument 84 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: afr_ota 85 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: protocols 86 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: files 87 7552 [OTA Agent T] [prvParseJSONbyModel] parameter not present: filepath 99 7651 [ETHER_RECEI] Heap: current 129720 lowest 127304 100 8430 [iot_thread] [INFO ][DEMO][8430] State: Ready Received: 1 Queued: 0 Processed: 0 Dropped: 0 101 9430 [iot_thread] [INFO ][DEMO][9430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0 102 10430 [iot_thread] [INFO ][DEMO][10430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0 103 11430 [iot_thread] [INFO ][DEMO][11430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0 104 12430 [iot_thread] [INFO ][DEMO][12430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0 105 13430 [iot_thread] [INFO ][DEMO][13430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0 106 14430 [iot_thread] [INFO ][DEMO][14430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0 107 15430 [iot_thread] [INFO ][DEMO][15430] State: WaitingForJob Received: 1 Queued: 0 Processed: 0 Dropped: 0
  17. Task B: Update the version of your firmware

    1. Open the demos/include/aws_application_version.hfile and increment the APP_VERSION_BUILD token value to 0.9.3.

    2. Rebuild the project.

  18. Create the userprog.rsu file with the Renesas Secure Flash Programmer to update the version of your firmware.

    1. Open the Amazon-FreeRTOS-Tools\Renesas Secure Flash Programmer.exe file.

    2. Choose the Update Firm tab and set the following parameters:

      • File Path – The location of the aws_demos.mot file (projects\renesas\rx65n-rsk\e2studio\aws_demos\HardwareDebug).

    3. Create a directory named update _firmware. Generate userprog.rsu and save it to the update_firmware directory. Verify that the generate succeeded.

      Renesas Secure Flash Programmer window with MCU selection, firmware verification type, sequence number, AES MAC key field, and file path input for generating secure firmware.
  19. Upload the firmware update, userproj.rsu, into an Amazon S3 bucket as described in Create an Amazon S3 bucket to store your update.

    Amazon S3 bucket management interface with folders, uploads, versions, and permissions options
  20. Create a job to update firmware on the RX65N-RSK.

    AWS IoT Jobs is a service that notifies one or more connected devices of a pending Job. A job can be used to manage a fleet of devices, update firmware and security certificates on devices, or perform administrative tasks such as restarting devices and performing diagnostics.

    1. Sign in to the AWS IoT console. In the navigation pane, choose Manage, and choose Jobs.

    2. Choose Create a job, then choose Create OTA Update job. Select a thing, then choose Next.

    3. Create a FreeRTOS OTA update job as follows:

      • Choose MQTT.

      • Select the code signing profile you created in the previous section.

      • Select the firmware image that you uploaded to an Amazon S3 bucket.

      • For Pathname of firmware image on device, enter test.

      • Choose the IAM role that you created in the previous section.

    4. Choose Next.

      Firmware image sign and OTA update settings with options to sign new firmware, select previously signed firmware, use custom signed firmware, specify code signing profile, firmware image file, path on device, and IAM role for OTA update job.
    5. Enter an ID and then choose Create.

  21. Reopen Tera Term to verify that the firmware was updated successfully to OTA demo version 0.9.3.

    Command line output showing initialization and connection of a thread to a broker.
  22. On the AWS IoT console, verify that the job status is Succeeded.

    AFR OTA-demo test job overview showing 1 resource succeeded.