En DM00367435
En DM00367435
En DM00367435
User manual
Getting started with X-CUBE-AWS
Amazon web services IoT software expansion for STM32Cube
Introduction
This user manual describes the content of the STM32 AWS (Amazon web services) IoT
(Internet of Things) software expansion package for STM32Cube.
The Amazon web services Internet of Things service enables secure, bidirectional
communication between IoT devices and the cloud over MQTT, HTTP and WebSockets.
The STM32 AWS IoT software expansion package (X-CUBE-AWS) for STM32Cube
provides application examples that connect and subscribe to the AWS IoT service via MQTT
in order to receive information and to publish data.
X-CUBE-AWS is available for B-L475E-IOT01, 32F413HDISCOVERY and
32F769IDISCOVERY boards.
The X-CUBE-AWS features are as follows:
Ready to run firmware example using WiFi and Ethernet connectivity to support quick
evaluation and development of IoT cloud applications
Interface to configure the board to connect to the AWS IoT
WiFi connection
AWS IoT connection, subscribe and publish tasks
Specific features on the B-L475E-IOT01 board:
Measurement of humidity, temperature, 3-axis magnetic data, 3D acceleration, 3D
gyroscope data, atmospheric pressure and proximity
AWS private key protection with firewall
Remote firmware update
Contents
1 Acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 Package description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
4.2 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
4.3 Folder structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.4 WiFi components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.5 Reset push-button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.6 User push-button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.7 User LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.8 Real time clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.9 mbedTLS configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.10 Application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.10.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.10.2 LED behavior of the three platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.10.3 Interacting with the boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.10.4 WiFi and AWS security credential update . . . . . . . . . . . . . . . . . . . . . . . 19
9 Memory footprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
11 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
List of tables
List of figures
1 Acronyms
Table 1 presents the definition of acronyms that are relevant for a better understanding of
this document.
)URP7KLQJWR5HPRWH8VHUDWDQ\ORFDWLRQ
66/7/66HFXULW\ 6WRUDJH
7&3,36WDFN
(WKHUQHW
+DUGZDUH :L)L
"Action": "iot:Subscribe",
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
}
]
}
To set up the hardware and software environment, one of the three supported boards must
be plugged into a personal computer via a USB cable. This connection with the PC allows
the user to:
Flash the board
Store the WiFi and the AWS security credentials
Interact with the board via a UART console
Debug
The B-L475E-IOT01 or 32F413HDISCOVERY boards must be connected to a WiFi access
point. The 32F769IDISCOVERY board must be connected to an Ethernet interface (see
Figure 2).
86%FRQQHFWLRQ
7KHVHERDUGVFRQQHFW :L)L
WRD:L)LDFFHVVSRLQW DFFHVVSRLQW
,R7GHYLFHRQHRIWKHEHORZERDUGV
%/(,27
)+',6&29(5<
7KLVERDUGPXVWEHSOXJJHG
),',6&29(5<
WRDQ(WKHUQHWLQWHUIDFH
06Y9
4 Package description
This section details the X-CUBE-AWS package content and how to use it.
4.1 Description
The X-CUBE-AWS package provides an AWS stack middleware for the STM32
microcontrollers. The package is split into the following components:
AWS SDK for connecting to AWS IoT from a device using embedded C
mbedTLS
LwIP
FreeRTOS
WiFi drivers
Ethernet driver for the 32F769IDISCOVERY board
Sensor drivers for the B-L475E-IOT01 board
STM32L4 Series, STM32F4 Series and STM32F7 Series HAL
AWS application examples
The software is provided as a zip archive containing source-code.
The following Integrated development environments are supported:
IAR Embedded Workbench for ARM (EWARM).
IAR version 7.80.4 or higher must be used
Keil Microcontroller Development Kit (MDK-ARM)
System Workbench for STM32. Version 1.14.0 or higher must be used
Note that the Keil and the System Workbench projects present the following limitations:
They cannot directly create an image file (which may be installed through RFU). They
would require a utility which would translate a hex image file into an IAR simple-code
image file. Although not excessively complex, it is not included in the present version of
the package.
The firewall build is not ported to those compilation suites yet.
4.2 Architecture
This section describes the software components of the X-CUBE-AWS package.
The X-CUBE-AWS software is an expansion for the STM32Cube, its main features and
characteristics are:
It is fully compliant with the STM32Cube architecture
It expands the STM32Cube in order to enable the development of applications
accessing and using the AWS IoT
It is based on the STM32CubeHAL, which is the hardware abstraction layer for the
STM32 microcontrollers
The software layers used by the application software to access and use the AWS IoT are
the following:
STM32Cube HAL layer: the HAL driver layer provides a generic multi instance simple
set of APIs (application programming interfaces) to interact with the upper layers
(application, libraries and stacks).
It is composed of generic and extension APIs. It is directly built around a generic
architecture and allows the layers that are built upon, such as the middleware layer, to
implement their functionalities without dependencies on the specific hardware
configuration for a given microcontroller unit (MCU).
This structure improves the library code reusability and guarantees an easy portability
onto other devices.
Board support package (BSP) layer: The software package needs to support the
peripherals on the STM32 boards apart from the MCU. This software is included in the
board support package (BSP). This is a limited set of APIs which provides a
programming interface for certain board specific peripherals such as the LED and the
user button.
AWS middleware: MQTT client.
mbedTLS: the AWS middleware uses a TLS connection which is ensured by the
mbedTLS library.
The TCP/IP connection can be handled either by the WiFi module (when a WiFi
connection is being used) or by the LwIP middleware (when an Ethernet connection is
being used). In the X-CUBE-AWS package, only the 32F769IDISCOVERY board can
connect via Ethernet.
FreeRTOS is a real-time operating system used by LwIP.
Figure 3 outlines the X-CUBE-AWS software architecture.
%RDUG6XSSRUW +DUGZDUH$EVWUDFWLRQ
3DFNDJH%63 /D\HU+$/ &06,6
'ULYHUV
+DUGZDUHFRPSRQHQWV
'HYHORSPHQWERDUGV
06Y9
;<=LVDQDEVWUDFWLRQWRWKHYHUVLRQXVHG
%63GULYHUVIRUWKH%/(,27ERDUG
&RQWDLQVWKHERDUGFRPSRQHQWGULYHUVIRUH[DPSOHWKH
VHQVRUVIRUWKH%/(,27ERDUG
%63GULYHUVIRUWKH670)+',6&2DQG
670),',6&2ERDUGV
$:6,R7GHYLFH6'.IRUHPEHGGHG&0477FOLHQW
)UHH57265HDO7LPH2SHQ6\VWHPXVHGZLWK/Z,3
/Z,3LQFKDUJHRI7&3,3FRQQHFWLRQZLWK(WKHUQHW
PEHG7/6
$:6,R7DSSOLFDWLRQIRUWKH%/(,27ERDUG
,'(SURMHFWIROGHUVWRROFKDLQVXSSRUW
&RQWDLQV:L)LVRXUFHILOHVIRU,2RSHUDWLRQVDQGIRU:L)L
PRGXOHDEVWUDFWLRQ
&RQWDLQVDSSOLFDWLRQVRXUFHILOHVWKDWDUHERDUG
LQGHSHQGHQW
$:6,R7DSSOLFDWLRQVIRUWKH670)+',6&2DQG
670),'LVFRYHU\ERDUGV
&38/RDGFDOFXODWLRQXWLOLW\
8WLOLWLHVIRUWKH670),',6&2/&'GLVSOD\
8WLOLW\WRIODVKDQHZ,QYHQWHNILUPZDUH$SSOLFDEOHWRWKH
%/(,27ERDUG
7HUD7HUPLQLWLDOLVDWLRQVFULSWSDUDPHWHUV
#else
#include MBEDTLS_CONFIG_FILE
#endif
Some macros are redefined to minimize the RAM footprint, sometimes at cost of ROM
memory. See below:
#define MBEDTLS_AES_ROM_TABLES
#define MBEDTLS_MPI_WINDOW_SIZE 1 /**< Maximum windows size used.
*/
#define MBEDTLS_MPI_MAX_SIZE 256 /**< Maximum number of bytes
for usable MPIs. *///
#define MBEDTLS_ECP_MAX_BITS 521 /**< 521 Maximum bit size of
groups */
#define MBEDTLS_ECP_WINDOW_SIZE 2 /**< Maximum window size used
*/
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Disable fixed-point speed-
up */
#define MBEDTLS_SSL_MAX_CONTENT_LEN 4096 /**< Maximum fragment length in
bytes, determines the size of each of the two internal I/O buffers */
The configuration file specifies which ciphers to integrate.
The serial port is to be configured with: COM port number, 115200 baud rate, 8-bit data,
parity none, 1 stop bit and no flow control, as highlighted in Figure 6
Once the UART terminal and the serial port are set up, press the board reset button (black).
Follow the indications on the UART terminal to upload WiFi and AWS data. Those data
remain in Flash and are reused the next time the board boots.
In Figure 8, the user copies his root CA that he has previously saved and pastes it into the
console.
Figure 9 shows the acknowledgment from the device after read: --->. This corresponds to
what is saved in the Flash memory.
This section describes how to use the application example provided at Projects\B-L475E-
IOT01\Applications\Cloud\AWS\.
Active low.
The Inventek module after
power-up or reset raises the
CMD/DATA READY pin to
ISM43362_RST PE8 Output GPIO, open drain mode signal that the first Data
Phase has started. The
CMD/DATA READY pin is
mapped to the
ISM43362_DRDY_EXTI1
STM32 MCU pin.
Enable Inventek micro boot
ISM43362_BOOT0 PB12 Output GPIO, push pull mode
loader.
Seen from the Inventek
module, the wakeup pin is
an external interrupt pin that
ISM43362_WAKEUP PB13 Output GPIO, push pull mode on the rising edge causes
the module to exit stop
mode. It is an edge trigged
input.
The STM32 host shall set
this output at low to initiate a
ISM43362_SPI3_CSN PE0 Output GPIO, push pull mode
communication with the WiFi
module.
The Inventek module sets
Input GPIO, interrupt mode when
ISM43362_DRDY_EXTI1 PE1 this pin high when ready to
rising
communicate.
INTERNAL_SPI3_SCK PC10
SPI interface to read and
Mode SPI3 Alternate Function,
INTERNAL_SPI3_MISO PC11 write data to the Inventek
push pull
WIFI module.
INTERNAL_SPI3_MOSI PC12
On top of sensor value publications, the LED and user button work as stated in
Section 4.10.2 on page 17.
Each illegal access to these protected areas is detected by the firewall hardware unit which
generates a reset, kicking off any intrusion. The firewall hardware unit can protect three
distinct configurable areas (more commonly called segments):
Code segment located in the Flash or in the SRAM memories
Non-volatile data segment located in the Flash memory
Volatile data segment located in the SRAM memory
Each of these segments may be accessed by the CPU when the firewall is open (allowed
access types depend on the segment in which the access is performed). While the firewall is
closed, there is no possible access to these protected segments. Each access triggers a
reset. A specific sequence called call gate needs to be executed to open the firewall.
The call gate sequence is the single entry point to open the firewall and unlock the
accesses to the protected code and data areas (protected code execution as well). Once
the critical code has been executed, coming back from the call gate closes the firewall.
address range register. The main program has then to write a single register to really
activate the firewall protection.
FIREWALL_PK_PARSE_KEY_FUNC: parses and checks a private key file in PEM
string format and creates the associated context. It calls the mbedTLS library function
mbedtls_pk_parse_key(pk,key,keylen,pwd,pwdlen).
FIREWALL_SIGN_FUNC: computes a signature using the private key context.
FIREWALL_CTX_FREE_FUNC: frees the dedicated memory allocation to the private
key context.
FIREWALL_CANDO_FUNC: queries the key context to check if a specific cypher is
supported or not.
FIREWALL_HEAP_STAT_FUNC: gets the maximum heap and stack level used by the
firewall library, useful for sizing memory needs. The memory requirements strongly
depend on mbedTLS configuration.
FIREWALL_FLASH_KEY_FUNC: the private key is provided as a PEM string. This
string is flashed to a firewall protected NVM area. The flashing operation has to write
and read this protected NVM area.
The firewall-protected software uses a dedicated runtime stack and a dedicated heap. Both
of these data segments are protected by the firewall. When entering / leaving the firewall-
protected software, the stack pointer is switched to / from the protected stack (see assembly
file stackswitch.c, Thumb code is assumed).
Heap stack and heap monitoring can be disabled / enabled thanks to the HEAP_DEBUG
preprocessor directive defined in the project options. Having defined HEAP_DEBUG
increases a bit the HEAP size because it also implements leak detection (see file heap.c).
Flashing the NVM memory depends on the HAL_GetTick function for timeout detection.
Under firewall, interruptions are masked, it is not possible to have a valid tick. Therefore, the
GetTick function is locally stubbed, and the possible timeouts cannot be detected.
uint32_t HAL_GetTick(void)
{
return 0;
}
Extract from the firewall_wrapper.c file showing how the function parsing the key has to be
implemented to pass the firewall call gate:
int mbedtls_firewall_pk_parse_key( mbedtls_pk_context *pk,
const unsigned char *key, size_t keylen,
const unsigned char *pwd, size_t pwdlen )
{
int ret;
__disable_irq();
ret =
(*FireWallCallGatePtr)(FIREWALL_PK_PARSE_KEY_FUNC,pk,key,keylen,pwd,pwdlen
);
__enable_irq();
return ret;
}
The development strategy is to minimize the changes in the third part middleware such as
mbedTLS. The mbedTLS middleware relies on a specific structure named
mbedtls_pk_context.
This structure is an interface to the public key cryptosystem and provides primary
functionalities:
Encrypt
Decrypt
Sign
Others
The structure is split in two parts:
pk_info contains a list of function pointers to the primary functionalities. This structure
depends on the type of the selected public Key algorithm.
pk_ctx contains the real key information, which must be protected.
Extract from the pk.h file, which is the public key abstraction layer interface:
typedef struct
{
const mbedtls_pk_info_t * pk_info; /**< key informations */
void * pk_ctx; /**< key context */
} mbedtls_pk_context;
The modification for firewall consists in having the pk_ctx structure filled and allocated by
code running under the firewall. For this purpose, the function mbedtl_pk_parse_key has to
be run under firewall while parsing the private key (see file network_st_wrapper.c).
This function returns the pk_info and the pk_ctx data, allocated in the firewall-protected
memory area.
The pk_info structure must point to the firewall wrapper entry points. In case of TLS
protocol, only a few functions are needed. The pk_info structure is replaced by a locally
managed structure initialized as below; the xxx_func are not used. The functions
sign_wrap() and can_do() are redirected to their counterpart to reach the firewall (see file
mbedtls_patch.c for more details).
In network_st_wrapper.c the pk_info structure is overwritten by the mbedtls_firewall_info.
Extract from the mbedtls_patch.c file:
mbedtls_pk_info_t mbedtls_firewall_info = {
MBEDTLS_PK_NONE,
"FIREWALL",
xxx_get_bitlen,
firewall_can_do,
xxx_verify_wrap,
firewall_sign_wrap,
xxx_decrypt_wrap,
xxx_encrypt_wrap,
xxx_check_pair_wrap,
xxx_alloc_wrap,
firewall_free_wrap,
xxx_debug,
};
The linker file (.icf) forces the placement of the section firewall_section at the specific
address corresponding to the one used in FireWallLib project.
In the stm32l475xx_flash.icf file, we find the below settings:
Define exported symbol __firewall_ROM_start = 0x08066D00;
This configuration sets the FIREWALL_MBEDLIB preprocessor define and the firewall
binary is added to the main project (as highlighted by Figure 10). Using this configuration,
the user does not need to modify the code.
1. The blue boxes represent the action states which are part of the firmware version management dialog.
5.7.2 Design
This sections describes the code that has been developed for the RFU feature.
Note: The IAR implementation is explained in this section as reference. The firmware information
and user settings sections are implemented for Keil and System Workbench as well.
However those 2 latter tools cannot export the IAR simple code file format that is supported
for the RFU feature.
For Keil and System Workbench, an additional PC utility development is needed to produce
the IAR simple-code file format that is used.
Flash update
Each data segment decoded from the firmware file is translated to the alternate Flash bank.
For instance, the program entry that should normally be programmed at 0x08000000 is
actually written at 0x08080000. After the bank switch and CPU reset, it is seen at
0x08000000 by the CPU.
FLASH_update() programs arbitrarily-aligned buffers into the Flash memory: the impacted
pages are copied to the RAM before the page is erased, and rewritten.
When the source buffer is already page-aligned and fully overwrites the target pages,
FLASH_unlock_erase() and FLASH_write_at() are rather directly used in order to prevent
two useless memory copies.
Note: The written data are compared to the source buffer to detect any defective Flash cell.
Note: While programming through ST-LINK or the IDE, please remind that the first bank is being
flashed. If another firmware is already programmed in the second bank, the bootloader
boots there as soon as the BFB2 option byte flag is set and a plausible stack pointer
initializer is found at 0x08080000.
See application note STM32 microcontroller system memory boot mode (AN2606)
available at www.st.com for more details.
Booting to the first bank can be obtained by resetting the BFB2 option byte flag, or by
erasing the 0x08080000 page in the STM32 ST-LINK utility.
Figure 14 highlights where to revert the BFB2 option flag.
This section describes how to use the application example provided at Projects\
STM32F413H-DISCO\Applications\Cloud\AWS\.
This section describes how to use the application example provided at Projects\
STM32F469I-DISCO\Applications\Cloud\AWS\.
Alternatively to the AWS IoT cloud, the application can run with an own Mosquitto server.
The AWS MQTT client and mbedTLS are compatible with Mosquitto and openssl.
To use an MQTT server, the following is needed:
To generate keys and certificates
A Linux host, which can be reached by the device over TCP/IP (or another OS which
can run Mosquitto and openssl).
In the example application, the mbedTLS configuration is tailored to the AWS server TLS
configuration. This sets some constraints on the TLS credentials that Mosquitto and the
device are able to use.
The openssl utility (available on multiple platforms, including Linux and Cygwin - the below
commands work with openssl 1.0.2) allows the user to create the proper keys and
certificates:
1. Create a root certification authority and use it to generate the certificates for the
Mosquitto server and for the device:
openssl req -new -x509 -days 1000 -extensions v3_ca -keyout ca.key -out
ca.crt
This produces the files: ca.key, ca.crt
2. Create private key for Mosquitto server, certificate request from server key and
certificate for the server
a) Create a private key for the Mosquitto server:
openssl ecparam -name secp384r1 -out server.key -genkey
This produces the file: server.key
b) Create a certificate request from the server key:
openssl req -out server.csr -key server.key -new
This produces the file: server.csr
Note: It is important that the Common Name (CN) that is set in the certificate request matches the
hostname of the host where Mosquitto runs. Otherwise, the server certificate verification
fails and the AWS MQTT client does not connect. For instance, if Mosquitto runs at
myiothost.st.com, the Common Name must be set to myiothost.st.com, or *.st.com.
Alternatively, if the name of the server cannot be resolved through DNS by the device, the
host verification feature must be disabled by setting mqttInitParams.isSSLHostnameVerify =
false before initializing the MQTT client by aws_iot_mqtt_init().
c) Create the certificate for the server, signed by the root certification authority:
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -
out server.crt -days 1000
This produces the files: server.crt, ca.srl
3. Create private key for the device, certificate request from device key and certificate for
the device
a) Create a private key for the device:
openssl genrsa -out client.key 2048
This produces the file: client.key
b) Create a certificate request from the device key:
openssl req -out client.csr -key client.key -new
This produces the file: client.csr
c) Create the certificate for the device, signed by the root certification authority:
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -
out client.crt -days 1000
This produces the files: client.crt, ca.srl
Then, on server side, the server credentials must be passed to Mosquitto through the
configuration file mosquitto.conf (absolute paths are recommended):
listener 8883
cafile /home/john/ca.crt
certfile /home/john/server.crt
keyfile /home/john/server.key
require_certificate true
The server is launched on the internet host (for example a Linux virtual host):
mosquitto -v -c mosquitto.conf
Finally, on device side:
Set the security credentials through the terminal as in AWS case, but use the files
ca.crt, client.crt and client.key.
Set the server address to the address of the host where Mosquitto runs. The device
name does not matter.
Run the application.
9 Memory footprint
The values in Figure 5 have been measured for the following configuration with the EWARM
IDE 7.80.4:
Optimization: optimized for size level 3
Debug option: on
Board: B-L475E-IOT01
Firewall not used
Q: Why do I get this pop up when I open the project with IAR?
Figure 15. Pop-up when the IAR IDE version is not compatible with
the one used for X-CUBE-AWS
A: It is very likely that the IAR IDE version is older than the one used to develop the package
(see Section 4.1: Description), hence the compatibility is not ensured. In this case, the IAR
IDE version needs to be updated.
Q: My device does not connect to the WiFi access point. How shall I proceed?
A: Make sure that another device can connect to the WiFi access point. If it can, enter the
WiFi credentials by pressing the user button (blue) up to five seconds after board reset.
Q: The proximity sensor always reports 8190 even if I place an obstacle close to it
A: Make sure that the liner (which is a very thin film placed on the proximity sensor) has
been removed. Its color is orange and it is not very visible.
11 Revision history
STMicroelectronics NV and its subsidiaries (ST) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to STs terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.