STS_E2/Air_Quality_LORAWAN_WINEXT_.../User/main.c

287 lines
6.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
******************************************************************************
* @file : main.c
* @author : zsq
* @version : V1.0
* @date : 2019-06-27
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2019 YUNHORN(Shenzhen YunHorn Technology Co., Ltd
* All rights reserved.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "bsp_usart.h"
#include "bsp_tim.h"
#include "usart_dma.h"
#include "sht3x.h"
#include "system.h"
#include "user_sensor.h"
#include "user_mqtt.h"
#include "fifo.h"
#ifdef USE_LORA
#include "user_lora.h"
#endif
#ifdef USE_WIFI
#include "user_wifi.h"
#endif
/** @addtogroup STM32F0xx_StdPeriph_Templates
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//传感器数据定义
SensorDataTypeDef sensorData;
extern volatile uint8_t WifiConfigState;
extern __IO uint8_t WORK_STATE;
extern __IO uint8_t TIMER_COUNTER;
extern __IO uint8_t LORA_STATE;
uint8_t NH3_Buffer[NH3_BUF_LEN]; //NH3接收Buffer
uint8_t CH2O_Buffer[CH2O_BUF_LEN]; //CH2O接收Buffer
uint8_t PM25_Buffer[PM25_BUF_LEN]; //PM25接收Buffer
uint8_t H2S_Buffer[H2S_BUF_LEN]; //H2S接收Buffer
uint8_t CO2_Buffer[CO2_BUF_LEN]; //CO2接收Buffer
uint8_t WIFI_Buffer[WIFI_BUF_LEN]; //WIFI接收Buffer
uint8_t loraNode_Buffer[LoraNode_BUF_LEN]; //LORA接收Buffer
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/* 配置系统时钟
*/
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(ENABLE);
FLASH_SetLatency(FLASH_Latency_1);
RCC_PREDIV1Config(RCC_PREDIV1_Div1);
RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_3);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLKConfig(RCC_HCLK_Div1);
RCC_USARTCLKConfig(RCC_USART1CLK_PCLK);
RCC_USARTCLKConfig(RCC_USART2CLK_PCLK);
RCC_USARTCLKConfig(RCC_USART3CLK_PCLK);
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
RCC_USBCLKConfig(RCC_USBCLK_PLLCLK);
}
//设置系统滴答定时器
SysTickConfig();
//获取系统时钟配置
// RCC_ClocksTypeDef get_rcc_clock;
// RCC_GetClocksFreq(&get_rcc_clock);
//串口时钟配置
USART_RCC_Configuration();
//初始化GPIO
GPIO_Configuration();
//初始化串口配置
USART_Configuration();
//配置中断向量
NVIC_Configuration();
#ifdef USE_WIFI
EXTI_Button_Config();
RingBuff_Init();//初始化环形缓冲区
#endif
//LORA模块复位
DelayMs(100);
GPIO_ResetBits(LORA_RST_PORT,LORA_RST_PIN);
DelayMs(100);
GPIO_SetBits(LORA_RST_PORT,LORA_RST_PIN);
//串口DMA配置
USARTx_DMA_CONFIG();
//定时器配置
TIMx_Configuration();
//延时等待传感器上电初始化
Delay(3000);
//传感器模式设置
SensorSet();
//开启独立看门狗
IWDG_Config(IWDG_Prescaler_256,4095);
#ifdef DEBUG
printf("DEBUG Test\r\n");
#endif
/* Infinite loop */
while(1)
{
#ifdef USE_WIFI
//配置WIFI
Check_WifiConfigState();
#endif
if(WORK_STATE == ENABLE)
{
switch(TIMER_COUNTER)
{
case CONFIG:
NetConfig();
break;
case STATE:
CheckState();
break;
case SEND:
DataSend();
break;
}
//
WORK_STATE = DISABLE;
}
//喂狗
IWDG_Feed();
}
}
void IWDG_Config(uint8_t prv ,uint16_t rlv)
{
// 使能 预分频寄存器PR和重装载寄存器RLR可写
IWDG_WriteAccessCmd( IWDG_WriteAccess_Enable );
// 设置预分频器值
IWDG_SetPrescaler( prv );
// 设置重装载寄存器值
IWDG_SetReload( rlv );
// 把重装载寄存器的值放到计数器中
IWDG_ReloadCounter();
// 使能 IWDG
IWDG_Enable();
}
// 喂狗
void IWDG_Feed(void)
{
// 把重装载寄存器的值放到计数器中喂狗防止IWDG复位
// 当计数器的值减到0的时候会产生系统复位
IWDG_ReloadCounter();
}
void DataSend(void)
{
//获取传感器数据
GetSensorData(&sensorData);
//解析传感器数据
AnalysisSensorData(&sensorData,NH3_Buffer,H2S_Buffer
,CH2O_Buffer,PM25_Buffer,CO2_Buffer);
//发送传感器数据
SensorDataSend(&sensorData);
}
void CheckState(void)
{
#ifdef USE_WIFI
CheckState_Wifi();
#endif
#ifdef USE_LORA
CheckState_Lora();
#endif
}
void NetConfig(void)
{
#ifdef USE_WIFI
NetConfig_Wifi();
#endif
#ifdef USE_LORA
if(LORA_STATE == LORA_JOINED)
{
NetConfig_Lora();
}
#endif
}
#ifdef DEBUG
//输出重定向 DEBUG_USART
int fputc(int ch, FILE *f)
{
USART_SendData(DEBUG_USART,(uint8_t)ch);
while((USART_GetFlagStatus(DEBUG_USART,USART_FLAG_TXE) == RESET))
{
}
return ch;
}
#endif
#ifdef USE_LORA
//输出重定向 LORA_USART
int fputc(int ch, FILE *f)
{
USART_SendData(LORA_USART,(uint8_t)ch);
while((USART_GetFlagStatus(LORA_USART,USART_FLAG_TXE) == RESET))
{
}
return ch;
}
#endif
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/