Lab 01-03: Quectel BG-96 Cellular Module from STM32 Host MCU Using STM32Cube IDE

Lab Overview

This lab is quite similar to the previous lab. However we will use the IoT Discovery Kit node which is having the onboard secure element. i.e. to remove the constraint mentioned in the previous lab.

This require to use the native tooling from ST. i.e. STM32Cube IDE for the FreeRTOS Cellular HAL Source Code Compilation. Also this lab, shows how to collect the onboard sensor data from the STM32 discovery kit and send to AWS IoT core.

Why Native Tooling from ST for secure element ?

The secure element has a secure bootloader & SBSFU and requires some additional post-processing during the firmware source code compilation. Currently the firmware compilation which has the secure element specific post-processing steps are integrated with the STM32Cube IDE.X-CUBE-AWS-2.0 is pre-integrated with the FreeRTOS Kernel version 202002 and has the middleware for ST-SAFE secure element i.e. security co-processor. However FreeRTOS Cellular HAL is based on FreeRTOS Kernel Version 202007, in this lab we will provide the steps to do a manual migration of the source of FreeRTOS cellular HAL to compile with the STM32Cube IDE

Migration of FreeRTOS CMake Project to STM32Cube Project complying with X-CUBE-AWS-2.0 Packaging Format

Pre-requisites

1.Install the STM32Cube Ecosystem Tools (Cube IDE, Cube Programmer etc.,). Refer to this lab for more details if you are not familiar with the STM32 IoT Discovery kit with secure element here

2.X-CUBE-AWS V2.0 SDK DOWNLOAD FROM HERE

3.Extract the x-cube-aws v2.0 SDK to a folder. ie. C:\STM32CubeExpansion_Cloud_AWS_V2.0.0

Caveat

  • In the migration steps specified below, the FreeRTOS kernel, Cellular HAL code etc., is fully migrated to the latest version, however some of the demo codes aren’t migrated to keep the migration procedure simple and to meet the immediate need of being able to test the FreeRTOS cellular HAL with the latest version of the IoT Discovery Kit node hardware with the on-board secure element.

There are are couple of conflicts, i) the “HAL_UART_MspInit” & “HAL_UART_MspDeInit” is defined in two files. ie. comm_if_st.c & stm32l4xx_hal_msp.c. This need to be manually resolved. The fix is to disable the duplicate definition from one of the files. ii) The iot_secure_sockets.c file which has changes done by ST specific to the WiFi MCU, need to be overwritten by the iot_secure_sockets.c for cellular. During this testing, this was manually replaced, hence making WiFi/Cellular mutually-exclusive.

3rd party libraries like mBedTLS etc., aren’t upgraded per the latest FreeRTOS package used in cellular HAL. The original version which is available in x-cube-aws-2.0 is retained as-is (This has a dependency with the ST-SAFE middleware)

STM32L4+ Discovery boards works in both the modes. i.e using the on-board secure element generating the device certificates (or) using an external device certificates (Generated by AWS IoT Core).

Steps for Migration

  1. Adaptation of the FreeRTOS folder structure to suit the X-Cube-AWS-2.0 packaging format. This involves, removing the folders pertaining to other silicon partners from the projects & the vendor folder. The current FreeRTOS packaging is a monolithic format, where the multi-vendor/partner vendor specific projects & source codes resides in a common folder

  2. Replace the FreeRTOS Kernel in the x-cube-aws-2.0 to the version that is used by FreeRTOS Cellular HAL. i.e upgrade from 202002 to 202007

  3. Copy the Cellular HAL specific folders to x-cube-aws-2.0 source code structure.

  4. Update the demo source to ensure the cellular manager is able to initialise.(Impact to the existing Network Manager)

  5. There is a subtle difference in the way the vendor specific folder for ST is organised in the X-Cube-AWS-2.0 and the FreeRTOS AWS Reference Integration Packaging (i.e. FREERTOS_BASEDIR/vendors/st/boards/stm32l475_discovery). This set of files requires special treament. Exploded directory approach used by x-cube-aws-2.0

Let’s Start the Migration

  1. Clone the FreeRTOS Cellular HAL source to a directory, where the source code shall be prepared/adapted for ease of copying to the destination
mkdir /c/src
cd /c/src
git clone https://github.com/chinglee-iot/amazon-freertos.git --recurse-submodules -b feature/cellular
  1. Navigate to the $FreeRTOS_BASEDIR/vendors folder. Remove all the folders except for the these four folders [ aws, pc, quectel, vendor,st ]

  2. Navigate to the $FreeRTOS_BASEDIR/projects folder. Remove all the folders except for the these three folders [ pc , st, vendor ]

  3. Now, let’s copy these to the X-CUBE-AWS-2.0 Cube Expansion Folder. If the x-cube-aws-2.0 source code is extracted to the default directory, it will be extracted to C:\STM32CubeExpansion_Cloud_AWS2.0.0 a.k.a. X-CUBE-AWS-BASEDIR henceforth.

    • Copy the folders (except for vendors/st) from $FreeRTOS_BASEDIR/vendors to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/vendors
    • Copy the $FreeRTOS_BASEDIR\freertos_kernel to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/freertos_kernel
  4. Copy the cellular HAL specific demo files (cherry pick the specific files/folders mentioned here)

    • Copy the directory $FreeRTOS_BASEDIR/demos/cellular_manager to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/demos
    • Copy the directory $FreeRTOS_BASEDIR/demos/include to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/demos/include
    • Copy the files from $FreeRTOS_BASEDIR/demos/network_manager to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/demos/network_manager
    • Copy the files from $FreeRTOS_BASEDIR/libraries/abstractions/cellular to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/libraries/abstractions/cellular
    • Copy the files from $FreeRTOS_BASEDIR/libraries/c_sdk/standard/common/include/types/iot_network_types.h X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/libraries/c_sdk/standard/common/include/types/iot_network_types.h
    • Copy the files from $FreeRTOS_BASEDIR/demos/demo_runner/iot_demo_freertos.c to $X-CUBE-AWS-BASEDIR/Middlewares/Third_Party/amazon-freertos/demos/demo_runner/iot_demo_freertos.c
  • Copy the files from $FreeRTOS_BASEDIR/vendors/st/boards/stm32l475_discovery/ports/comm_if to $X-CUBE-AWS-BASEDIR/Projects/B-L4S5I-IOT01A/Applications/Cloud/aws_demos/Src/ports. i.e. the files (comm_if_st.c, iot_fifo.c iot_fifo.h)

  • Copy the below set of files from $FreeRTOS_BASEDIR/vendors/st/boards/stm32l475_discovery/aws_demos/config_files to $X-CUBE-AWS-BASEDIR/Projects/B-L4S5I-IOT01A/Applications/Cloud/aws_demos/config_files [ aws_demo_config.h, aws_cellular_config.h, FreeRTOSConfig.h, FreeRTOSIPConfig.h aws_iot_network_config.h, aws_secure_sockets_config.h ]

  • Copy the file iot_secure_sockets.c from $FREERTOS_BASEDIR/libraries/abstractions/secure_sockets/cellular/iot_secure_sockets.c to $X-CUBE-AWS-BASEDIR/Projects//B-L4S5I-IOT01A/Applications/Cloud/aws_demos/Src/ports

  • In the file $X-CUBE-AWS-BASEDIR/Projects//B-L4S5I-IOT01A/Applications/Cloud/aws_demos/Src/main.c, vApplicationDaemonTaskStartupHook() comment the prvWiFiConnect() & other WiFi specific initialisations. If this step isn’t done, even though Cellular Manager is enabled, the actual user data shall use the WiFi connectivity.

  1. Resolve the duplicate definition of the 2 functions HAL_UART_MspInit & HAL_UART_MspDeInit in the file $X-CUBE-AWS-BASEDIR/Projects/B-L4S5I-IOT01A/Applications/Cloud/aws_demos/Src/stm32l4xx_hal_msp.c

  2. Update the .project & .cproject. To perform this step, download the attachment from the below link

Use the reference files provided along with the x-cube-aws-sensor-demo.zip. This set of files has the on-board sensor code integrated and has the updates for the .project & .cproject required for FreeRTOS cellular HAL. Copy the files available under x-cube-aws-sensordemo/sensor-demo/src/STM32CubeExpansion_Cloud_AWS_V2.0.0 to X-CUBE-AWS-BASEDIR.

  1. Once the files from the above attachment as replaced on to the X-CUBE-AWS-BASEDIR folder, this completes the Source Adaptation for x-cube-aws.

  2. Open the STM32Cube IDE and Select Import existing Projects and navigate to the below $X-CUBE-AWS-BASEDIR/Projects/B-L4S5I-IOT01A/Applications/Cloud/aws_demos/STM32CubeIDE

  3. In the STM32Cube IDE, From the Projects->Clean & Build All. If the migration procedure is successful, you should now be able to compile the source code successfully.

Remember to update the cellular HAL specific user configuration eg. APN name etc., from the earlier labs.

AWS IoT Core Device Provisioning ( ST Root CA Vs AWS Root CA )
  1. To control the behaviour of using the onboard secure element Vs externally generated private keys/certificates, depends on the information available in the file aws_clientcredential_keys.h.

  2. If the values for keyCLIENT_CERTIFICATE_PEM , keyCLIENT_PRIVATE_KEY_PEM are "” then the device certificates from the secure element shall be used.

  3. To use externally generated device certificates (i.e. from AWS IoT core) populate the values for keyCLIENT_CERTIFICATE_PEM , keyCLIENT_PRIVATE_KEY_PEM, per the standard AWS IoT core device provisioning process.

Connecting the BG-96 Shield Board to the STM32 IoT Discovery board

Please refer to the procedure from the previous lab.The only difference is the board used for this lab, should have on-board secure element.

Steps for Configuration & Source Code Compilation for the Standard FreeRTOS MQTT Demo Application

IoT Device Certificate & Endpoint Configuration

If you have completed the steps from the previous lab, then there is no need to repeat the device certificate creation setup. The configuration is stored in the aws_clientcredential.h & aws_clientcredential_keys.h file. It’s adequate to do this setup only once.

Download the Application to the target

To download the firmware to the STM32 target, use the STM32Cube Programmer. link here

The start address should be set to 0x08000000 for programming

Download the file SBSFU_B-L4S5I-IOT01_aws_demos.bin located in the C:\STM32CubeExpansion_Cloud_AWS_V2.0.0\Projects\B-L4S5I-IOT01A\Applications\Cloud\aws_demos\STM32CubeIDE\PostBuild folder

Verify the results in the AWS IoT Core Management/Test Console

If the cellular connectivity initialisation is sucessful, then you should now be able to view the messages, sensor data on to the AWS IoT Code Test Client.