STM32CubeMX移植机智云自动生成代码STM32CubeMX配置GPIO口
STM32CubeMX配置USART外设
STM32CubeMX配置1ms定时器
STM32CubeMX配置外部按键生成代码
移植代码从机智云“stm32f103c8x”平台生成的代码中复制Gizwits、Hal、Utils文件夹到当前目录:
在工程中添加如下的目录结构:
修改代码首先在gizwits_product.h / hal_key.h 中将原来F103C8平台的头文件 #include <stm32f1xx.h>替换为G071RB平台的 #include <stm32g0xx.h> 然后修改如下代码: 修改gizwits_product.c代码:
添加app.c代码: - /* Includes ------------------------------------------------------------------*/
- #include "app.h"
- #include "sht30.h"
- #include "gizwits_protocol.h"
- #include "gizwits_product.h"
- #include "hal_key.h"
- #define GPIO_KEY_NUM 2 ///< Defines the total number of key member
- #define KEY1_Pin GPIO_PIN_11
- #define KEY1_GPIO_Port GPIOA
- #define KEY2_Pin GPIO_PIN_4
- #define KEY2_GPIO_Port GPIOA
- ///------------------- G0 TO F7 ------------------------
- static short temperature = 0, humidity = 0;
- float gizwits_temperature = 0, gizwits_humidity = 0;
- uint8_t tui_buf[10];
- #define FRM_START 0x68
- #define FRM_CMD_UPD_SENSOR 0x01//温湿度传感器
- #define FRM_CMD_UPD_RESTWIFI 0x02//复位WIFI
- #define FRM_CMD_UPD_AIRLINK 0x03//配置WIFI到路由器
- #define FRM_CMD_UPD_CONFIGEND 0x04//链接到路由器
- #define FRM_FIXLEN 14
- #define FRM_END0 0x5c
- #define FRM_END1 0x6e
- #define FRM_POS_START 0
- #define FRM_POS_CMD 1
- #define FRM_POS_LEN 2
- #define FRM_POS_DATA 3
- #define FRM_POS_CRC 11
- #define FRM_POS_END0 12
- #define FRM_POS_END1 13
- keysTypedef_t keys;
- keyTypedef_t singleKey[GPIO_KEY_NUM]; ///< Defines a single key member array pointer
- static uint8_t s_wifi_router_status = 0;
- static void update_to_ui_wifi_status(uint8_t cmd)
- {
- if(cmd==FRM_CMD_UPD_RESTWIFI||cmd==FRM_CMD_UPD_AIRLINK||cmd==FRM_CMD_UPD_CONFIGEND)
- {
- s_wifi_router_status = cmd;
- }
- }
- void ConfigWIFIfinish(void)
- {
- update_to_ui_wifi_status(FRM_CMD_UPD_CONFIGEND);
- }
- 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 ,Soft AP\n");
- gizwitsSetMode(WIFI_SOFTAP_MODE);
- }
- /**
- * key2 short press handle
- * @param none
- * @return none
- */
- void key2ShortPress(void)
- {
- GIZWITS_LOG("------>KEY2 PRESS,Air Link mode\n\r");
- #if !MODULE_TYPE
- gizwitsSetMode(WIFI_AIRLINK_MODE);
- update_to_ui_wifi_status(FRM_CMD_UPD_AIRLINK);
- #endif
- }
- /**
- * key2 long press handle
- * @param none
- * @return none
- */
- void key2LongPress(void)
- {
- //AirLink mode
- GIZWITS_LOG("KEY2 PRESS LONG ,Wifi Reset mode\n");
- #if !MODULE_TYPE
- gizwitsSetMode(WIFI_RESET_MODE);
- update_to_ui_wifi_status(FRM_CMD_UPD_RESTWIFI);
- #endif
- }
- 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);
- }
- typedef struct __frame_sensor_to_ui{
- uint8_t start ;
- uint8_t cmd;
- uint8_t len;
- uint8_t data[8];
- uint8_t crc;
- uint8_t end ;
- }_frame_sensor_to_ui;
- _frame_sensor_to_ui s_frame_sensor_to_ui;
- //eg:68 cmd len t t h h x x y y crc 16 / crc = 68 + cmd + len + t + t + h + h + x + x + y + y 16
- extern UART_HandleTypeDef huart2;
- uint8_t cal_crc(uint8_t *buf,uint8_t len)
- {
- uint8_t t_crc = 0;
- uint8_t r_crc = 0;
- //check crc
- for(int i=0; i<len ;i++)
- {
- t_crc += buf[i];
- }
- r_crc = (uint8_t)t_crc;
- return r_crc;
- }
- #define __DEBUG_UPDATE_UI_ARRAY__
- void update_to_ui(uint8_t cmd,uint8_t *data)
- {
- uint8_t t_buf[20];
- int i = 0;
- memset(t_buf,0,sizeof(t_buf));
- t_buf[FRM_POS_START]=FRM_START;
- t_buf[FRM_POS_CMD]=cmd;
- t_buf[FRM_POS_LEN]=FRM_FIXLEN;
- t_buf[FRM_POS_DATA]= data[0];
- t_buf[FRM_POS_DATA+1]= data[1];
- t_buf[FRM_POS_DATA+2]= data[2];
- t_buf[FRM_POS_DATA+3]= data[3];
- t_buf[FRM_POS_DATA+4]= s_wifi_router_status; //wifi status update
- t_buf[FRM_POS_DATA+5]= 0;
- t_buf[FRM_POS_DATA+6]= 0;
- t_buf[FRM_POS_DATA+7]= 0;
- t_buf[FRM_POS_CRC]=cal_crc(t_buf,FRM_FIXLEN-3);
- t_buf[FRM_POS_END0]=FRM_END0;
- t_buf[FRM_POS_END1]=FRM_END1;
- #ifdef __DEBUG_UPDATE_UI_ARRAY__
- GIZWITS_LOG("update_to_ui:");
- for(i=0; i<FRM_FIXLEN; i++)
- {
- GIZWITS_LOG("x ", t_buf[i]);
- }
- GIZWITS_LOG("\n\r");
- #endif
- HAL_UART_Transmit(&huart2, t_buf, FRM_FIXLEN, 0xFFFF);
- }
- ///------------------- APP ------------------------
- extern int SHT3X_GetTempAndHumi(short *temp, short *humi);
- void APP_Init(void)
- {
- s_wifi_router_status = 0;
- SHT3X_Init();
- timerInit();
- uartInit();
- userInit();
- gizwitsInit();
- keyInit();
- GIZWITS_LOG("MCU Init Success \n");
- printf("MCU HAL Success , SoftVersion = %s\r\n",SOFTWARE_VERSION);
- Pro_D2W_Ask_Module_Reboot();
- }
- void APP_Process(void)
- {
- if(SHT3X_GetTempAndHumi(&temperature, &humidity)==0)
- {
- gizwits_temperature = temperature/10.00;
- gizwits_humidity = humidity / 10.00;
- tui_buf[0] = ((temperature >> 8) & 0xff);
- tui_buf[1] = (temperature & 0xff);
- tui_buf[2] = ((humidity >> 8) & 0xff);
- tui_buf[3] = (humidity & 0xff);
- update_to_ui(FRM_CMD_UPD_SENSOR,tui_buf);
- //printf("temp=%0.1f,humi=%0.1f\r\n",gizwits_temperature,gizwits_humidity);
- }
- }
复制代码修改main.c的代码
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- unsigned int tmp, tick = 0;
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART2_UART_Init();
- MX_TIM2_Init();
- MX_USART1_UART_Init();
- MX_USART3_UART_Init();
- MX_USART4_UART_Init();
- MX_I2C1_Init();
- MX_IWDG_Init();
- /* USER CODE BEGIN 2 */
- APP_Init();
- __HAL_IWDG_START(&hiwdg);
- while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)==GPIO_PIN_RESET);//等待F7输出的按键信号稳定
- HAL_GPIO_WritePin(ESP_RST_GPIO_Port, ESP_RST_Pin, GPIO_PIN_SET);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- tmp = HAL_GetTick();
- if(tmp - tick > 1000)
- {
- tick = tmp;
- APP_Process();
- userHandle();
- __HAL_IWDG_RELOAD_COUNTER(&hiwdg);
- //printf("APP_Process\n\r");
- }
-
- gizwitsHandle((dataPoint_t *)¤tDataPoint);
- }
- /* USER CODE END 3 */
- }
复制代码
|