Docs
Gizwits Docs
中文
热搜词
APP
SDK
Android
ios
WIFI
Porting using STM32CubeMX
Edit

Overview

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:

Porting tutorial using STM32CubeMX

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.

Porting tutorial using STM32CubeMX

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.

Porting to other STM32 platforms

1. Create the STM32CubeMX project file

Select the MCU you are using on STM32CubeMX (for example, here we choose STM32L496ZGTx), as follows:

Porting tutorial using STM32CubeMX

Click Project - Setting to set the project location and the corresponding parameters as follows:

Porting tutorial using STM32CubeMX

Note: Here chooses Keil 5 as the compiler.

2. Import the Gizwits configuration template

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.

Porting tutorial using STM32CubeMX

Select the STM32F103C8.ioc configuration file metioned in the Overview section as shown below:

Porting tutorial using STM32CubeMX

After clicking OK, you can see the configuration file generated by Gizwits has been imported to the STM32L496ZGT6 project.

Porting tutorial using STM32CubeMX

Porting tutorial using STM32CubeMX

3. Special processing (only for the STM32 L series low-power platforms, skip this step for other platforms)

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:

Porting tutorial using STM32CubeMX

Note: Manually set pin PG8/PG7 to LPUART_RX/LPUART_TX type.

Porting tutorial using STM32CubeMX

4. Generate code

Click Project - Generate Code or click the shortcut icon to generate the code.

Porting tutorial using STM32CubeMX

5. Compile and configure the project

Click Rebuild to compile the project.

Porting tutorial using STM32CubeMX

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.

Porting tutorial using STM32CubeMX

Porting tutorial using STM32CubeMX

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.

Porting tutorial using STM32CubeMX

Click Download icon to initiate a flash download. If the following messages appear, it indicates the successful completion of the flash download:

Porting tutorial using STM32CubeMX

6. Port code

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:

Porting tutorial using STM32CubeMX

After porting (i.e. copying Gizwits, Hal, Utils to STM32L496ZGT from STM32F103) as follows:

Porting tutorial using STM32CubeMX

Next we add the following directories to the Keil 5 project STM32L496ZGT:

Porting tutorial using STM32CubeMX

Similarly, set up the corresponding paths for compilation:

Porting tutorial using STM32CubeMX

7. Modify the code

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:

  • First incorporate references to the relevant header files:
1
2
3
4
5
6
7
/* USER CODE BEGIN Includes */

#include "hal_key.h"
#include "gizwits_product.h"
#include "common.h"

/* USER CODE END Includes */
  • Next is the relevant global variable:
1
2
3
4
5
6
7
8
/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/
#define GPIO_KEY_NUM 2 ///< Defines the total number of key member
keyTypedef_t singleKey[GPIO_KEY_NUM]; ///< Defines a single key member array pointer
keysTypedef_t keys;

/* USER CODE END PV */
  • The following is the callback function definitions for the long press and short press on the respective keys:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* USER CODE BEGIN 0 */

/**
* key1 short press handle
* @param none
* @return none
*/
void key1ShortPress(void)
{
GIZWITS_LOG("KEY1 PRESS ,Production Mode\n");
gizwitsSetMode(WIFI_PRODUCTION_TEST);
}

/**
* key1 long press handle
* @param none
* @return none
*/
void key1LongPress(void)
{
GIZWITS_LOG("KEY1 PRESS LONG ,Wifi Reset\n");
gizwitsSetMode(WIFI_RESET_MODE);

}

/**
* key2 short press handle
* @param none
* @return none
*/
void key2ShortPress(void)
{
GIZWITS_LOG("KEY2 PRESS ,Soft AP mode\n");
#if !MODULE_TYPE
gizwitsSetMode(WIFI_SOFTAP_MODE);
#endif
}

/**
* key2 long press handle
* @param none
* @return none
*/
void key2LongPress(void)
{
//AirLink mode
GIZWITS_LOG("KEY2 PRESS LONG ,AirLink mode\n");
#if !MODULE_TYPE
gizwitsSetMode(WIFI_AIRLINK_MODE);
#endif
}

/**
* Key init function
* @param none
* @return none
*/
void keyInit(void)
{
singleKey[0] = keyInitOne(NULL, KEY1_GPIO_Port, KEY1_Pin, key1ShortPress, key1LongPress);
singleKey[1] = keyInitOne(NULL, KEY2_GPIO_Port, KEY2_Pin, key2ShortPress, key2LongPress);
keys.singleKey = (keyTypedef_t *)&singleKey;
keyParaInit(&keys);
}

/* USER CODE END 0 */
  • Next is the initialization of the relevant drivers and the Gizwits protocol:
1
2
3
4
5
6
7
8
9
10
11
/* USER CODE BEGIN 2 */

timerInit();
uartInit();

userInit();
gizwitsInit();
keyInit();
GIZWITS_LOG("MCU Init Success \n");

/* USER CODE END 2 */
  • Finally, the function calls in the loop structure of the main function are as follows:
1
2
3
4
5
6
7
/* USER CODE BEGIN 3 */

userHandle();
gizwitsHandle((dataPoint_t *)¤tDataPoint);
}

/* USER CODE END 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.

Porting tutorial using STM32CubeMX

Porting tutorial using STM32CubeMX

7. Compile and download

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.

Advanced development

Application development

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.

Driver development

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.

Porting tutorial using STM32CubeMX

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.

Porting tutorial using STM32CubeMX

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.

Porting tutorial using STM32CubeMX

Finally click the source code generation button to update the IO driver in the source code (and will not affect the previously added code).

Porting tutorial using STM32CubeMX

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

Porting tutorial using STM32CubeMX

to

1
if(HAL_GPIO_ReadPin((GPIO_TypeDef\*)keyS->singleKey[i].keyPort,keyS->singleKey[i].keyGpio))

After compiling and download, test the key.

Support

For individual developers

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

For organizations

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:

Porting tutorial using STM32CubeMX