This document describes how to use STM32CubeMX to port the automatically generated source code for STM32 platform to other MCU platforms.
STM32CubeMX can be downloaded from STM website and installed after registration, this document will not elaborate on it.
We will take the STM32F103 source code project generated in Gizwits Developer Center as an example, and port it to the STM32L496ZGT6 platform using STM32Cube (the way of porting to other STM32 platforms is similar). The development kit NUCLEO-L496ZG is used here:
Note: NUCLEO-L496ZG is a new product of the ST Nucleo series development kit. The L4 series combines the advantages of low power consumption and high performance. It integrates ST-LINK/V2-1 and uses STM32L496ZGT6 as the main control board, featuring ultra-low power consumption, a rich peripheral set and etc.
We can generate the source code of STM32F103 platform through Gizwits MCU Code Auto-Generator (For details, see Guide to Gizwits MCU Code Auto-Generator). The following is the generated source code directory structure.
File/folder name | Description |
---|---|
Driver | It contains all the library files |
Gizwits | Gizwits protocol processing files |
Hal | Peripheral driver files |
MDK-ARM | MDK project file |
Inc | Header files for STM32 source code |
Src | C files for STM32 source code |
Utils | Utility library files |
.mxproject | STM32Cube configuration file |
STM32F103C8.ioc | STM32CubeMX project file (take STM32F103C8 platform as an example) |
Note: The directories that need to be ported are: Gizwits, Hal, and Utils.
Select the MCU you are using on STM32CubeMX (for example, here we choose STM32L496ZGTx), as follows:
Click Project - Setting to set the project location and the corresponding parameters as follows:
Note: Here chooses Keil 5 as the compiler.
Since the source code automatically generated by Gizwits contains the STM32CubeMX project configuration file for the corresponding platform, where the relevant driver parameters are already configured. To facilitate developers to port the STM32 source code to other platforms, you can use the project configuration file import function of STM32CubeMX to import the configuration parameters of STM32F103 platform for STM32L496ZGT6 platform, as follows:
Choose File > Import Project.
Select the STM32F103C8.ioc configuration file metioned in the Overview section as shown below:
After clicking OK, you can see the configuration file generated by Gizwits has been imported to the STM32L496ZGT6 project.
Since the USB serial port of the NUCLEO-L496ZG development kit is supposed to be connected to the low power serial port (LPUART1) on the MCU by default, instead of the UART1 on F103 platform, the UART1 in the configuration file should be changed to LPUART1, as shown below:
Note: Manually set pin PG8/PG7 to LPUART_RX/LPUART_TX type.
Click Project - Generate Code or click the shortcut icon to generate the code.
Click Rebuild to compile the project.
Choose the STM32L496ZGT development kit. If it raises errors, you can click the icon to modify on the Debug tab of Options for Target.
Note: ST-LINK is selected in the figure.
Click Settings-Flash Download, check the Reset and Run. After the program is downloaded, it will automatically run, no need to press reset or power on again.
Click Download icon to initiate a flash download. If the following messages appear, it indicates the successful completion of the flash download:
At this time, only the STM32L496ZGT related driver code is used in the project. To use the Gizwits IoT Cloud access capability, you need to port the relevant protocol source code (i.e. the three directories in the automatically generated STM32F103 project mentioned above: Gizwits, Hal, Utils).
The STM32L496ZGT project directory before porting is as follows:
After porting (i.e. copying Gizwits, Hal, Utils to STM32L496ZGT from STM32F103) as follows:
Next we add the following directories to the Keil 5 project STM32L496ZGT:
Similarly, set up the corresponding paths for compilation:
First, in order to adapt to the corresponding platform, replace with the header files of the corresponding platform in gizwits_product.h / hal_key.h. For example:
Replace the header file #include “stm32f1xx_hal.h” of the original F103C8 platform with the #include “stm32l4xx_it.h” of the L496ZGT platform.
Note: The header files for the corresponding platform can be found in Drivers\STM32F1xx_HAL_Driver\Inc in the root directory.
Next add the code related to the Gizwits protocol processing in main.c:
1 | /* USER CODE BEGIN Includes */ |
1 | /* USER CODE BEGIN PV */ |
1 | /* USER CODE BEGIN 0 */ |
1 | /* USER CODE BEGIN 2 */ |
1 | /* USER CODE BEGIN 3 */ |
The following are special modifications for the L496ZGT platform. Skip this step for other non-low-power STM32 platforms.
Since NUCLEO-L496ZG uses LPUART1 instead of UART1 on the F103 platform, it is required to replace the two “huart1” in Gizwits/gizwits_product.c to the automatically generated “hlpuart1” in main.c.
After the completion of the relevant porting, compile and flash to the NUCLEO-L496ZG development kit. According to the CubeMX project configuration file, the serial port baud rate is 115200.
Furthermore, connect the log output pin to debug source code.
After completing the porting of the STM32 related platform, you can use the various tools provided by Gizwits to develop your own projects. For details, please refer to the related chapters of Guide to porting Common Platform Edition of MCU SDK in Gizwits Document Center.
When the hardware configuration needs to be changed, STM32CubeMX can be used to perform relevant hardware driver configuration development, thereby minimizing the repeated development of the source code and improving the development efficiency. Here, the key IO configuration is taken as an example:
Open the STM32CubeMX project file of the corresponding platform in the root directory.
On the pin configuration (Pinout) tab, update the pin IO configuration. For example, change the original KEY1 (PB10) to PE3 and the type to GPIO_Input.
Go to the configuration option (Configuration) tab, and keep the new configured pin name consistent with the original name (so you do not need to change the macro definition in the source code), then select OK.
Finally click the source code generation button to update the IO driver in the source code (and will not affect the previously added code).
Since the NUCLEO-L496ZG hardware wiring is different from that in F103 (pull-down), it needs to update the code of some key drivers. In Hal/hal_key.c, change
to
1 | if(HAL_GPIO_ReadPin((GPIO_TypeDef\*)keyS->singleKey[i].keyPort,keyS->singleKey[i].keyGpio)) |
After compiling and download, test the key.
Gokit is free, but only a limited number are available for individual developers. Register in our forum or follow our official WeChat to request for provision.
Developer Forum: http://club.gizwits.com/forum.php
Documentation Center: http://docs.gizwits.com/en-us/overview/overview.html
Gizwits has a lot of support programs for organizations, you can contact Gizwits to get Gokit and timely technical support.
Website: http://en.gizwits.com/about-us
WeChat Official Account QR code of Gizwits: