255 lines
10 KiB
C
255 lines
10 KiB
C
#include "userapp.h"
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
|
||
/********************************************************************************
|
||
* @file gateapp.c
|
||
* @author 晏诚科技 Mr.Wang
|
||
* @version V1.0.0
|
||
* @date 11-Dec-2018
|
||
* @brief 针对SmartGate应用程序,对所有驱动进行封装
|
||
******************************************************************************
|
||
* @attention
|
||
* 所有对除USER层的封装都放在gateapp中
|
||
*******************************************************************************/
|
||
|
||
|
||
/*********应用层外部调用文件*******************************************************/
|
||
#include "usart.h" //printf
|
||
#include "ec20module.h"
|
||
#include "ec20net.h"
|
||
#include "ec20tcp.h"
|
||
#include "ec20http.h" //RunResult Send_Post( POSTP_s *psHttpP, char* postBody )
|
||
#include "key.h"
|
||
#include "sysport.h"
|
||
#include "malloc.h"
|
||
#include "cjson.h"
|
||
#include "rtc.h"
|
||
#include "beep.h"
|
||
#include "led.h"
|
||
#include "logflash.h"
|
||
#include "syslib.h"
|
||
#include "user_oled.h"
|
||
#include "user_tcp.h"
|
||
#include "user_http.h"
|
||
#include "usercmd.h"
|
||
#include "logflash.h"
|
||
#include "io.h"
|
||
#include "pvd.h"
|
||
#include "oled.h"
|
||
#include "stm32Temp.h"
|
||
#include "user_key.h"
|
||
/**********************************************************************************/
|
||
|
||
/**********************************************************************************
|
||
*内部使用的常变量
|
||
**********************************************************************************/
|
||
|
||
|
||
/**********************************************************************************
|
||
*供外部使用的常变量
|
||
***********************************************************************************/
|
||
Application gateApp ; //gateApp存放应用参数
|
||
/*应用层缓冲队列*/
|
||
EventGroupHandle_t netEventHandler ; //事件标志组(标志位反馈网络状态)
|
||
EventGroupHandle_t osSafeEventHandler ; //事件标志组(标志位OS每个任务的运行情况)
|
||
SemaphoreHandle_t ec20MutexSemaphore; //互斥信号量句柄(EC20资源抢占及释放)
|
||
char lastOccupyEc20[30] = {0} ; //全局数组 记录上一次占用EC20资源的__FILE__, __LINE__
|
||
char lastReleseEc20[30] = {0} ; //全局数组 记录上一次释放EC20资源的__FILE__, __LINE__
|
||
|
||
|
||
/**************************************************************************************************
|
||
* 名 称: void Board_Init(void)
|
||
* 功能说明: 驱动及功能块初始化
|
||
**************************************************************************************************/
|
||
void Board_Init(void)
|
||
{
|
||
SysTick_Init() ; //系统滴答定时器初始化 1ms中断一次 中断内部进行了喂狗操作
|
||
LogFlash_Init() ; //初始化本地FLASH总存储的LOG
|
||
MyMenInit(SRAMIN) ; //动态内存分配初始化
|
||
RTC_Init(INT_RANK_15) ; //实时时钟初始化
|
||
PrintfDeviceInfo() ; //UART_DEBUG输出硬件信息
|
||
Rtc_RegHookCallback(RTC_IT_ALR, Rtc_Alr_Callback) ; //注册RTC闹钟中断回调函数
|
||
UserKeyInit() ; //初始化KEY按键
|
||
Led_Init() ; //初始化LED
|
||
Beep_Init() ; //初始化蜂鸣器
|
||
T_Adc_Init() ; //初始化ADC用于MCU内部温度检测
|
||
OLED_Init() ; //OLED初始化
|
||
RS232Init(115200) ; //RS232初始化
|
||
RS485Init(115200) ; //RS485初始化
|
||
//Pvd_Init( EXTI_Trigger_Rising_Falling, INT_RANK_14) ;
|
||
PWR_PVD_Init() ;
|
||
}
|
||
|
||
|
||
/**************************************************************************************************
|
||
* 名 称: void PrintfDeviceInfo(void)
|
||
* 功能说明: DEBUG口输出信息
|
||
**************************************************************************************************/
|
||
void PrintfDeviceInfo(void)
|
||
{
|
||
printf("\r\n**************************************************************" ) ;
|
||
printf("\r\n版权归属 : 晏诚科技 2017/8-2027/8 .\
|
||
\r\n技术支持 : Mr. Wang 13635513618 .") ;
|
||
SetBootVersion(&gateApp, (char*)uIapFlash.sIapFlash.BootVers) ;
|
||
printf("\r\nBoot版本 : %*.*s", 0, VERSION_LEN, gateApp.bootVers) ;
|
||
Query_AppVersion((char*)gateApp.appVers) ;
|
||
SetAppVersion(&gateApp, (char*)gateApp.appVers) ;
|
||
printf("\r\nAPP 版本 : %s", gateApp.appVers) ;
|
||
printf("\r\nRunAppNum: APP%d", uIapFlash.sIapFlash.RunAppNum-0x30 ) ;
|
||
GetDeviceMacAddress((uint8_t*)gateApp.macId, STRMACID) ;
|
||
printf("\r\n设备编号 : %.*s", MAC_BYTES_LEN, gateApp.macId) ;
|
||
DeviceRstReason((uint8_t*)gateApp.rstReason, VERSION_LEN) ;
|
||
printf("\r\n重启原因 : %.*s", VERSION_LEN, gateApp.rstReason) ;
|
||
printf("\r\n**************************************************************" ) ;
|
||
char buf[128] = {0} ;
|
||
snprintf(buf, 128, "重启原因:%s.", gateApp.rstReason) ;
|
||
WriteLogToFlash(buf) ;
|
||
}
|
||
|
||
/**************************************************************************************************
|
||
* 名 称: void DeviceRstReason(uint8_t *reason, uint8_t maxLen)
|
||
* 功能说明: 判断硬件重启原因
|
||
**************************************************************************************************/
|
||
void DeviceRstReason(uint8_t *reason, uint8_t maxLen)
|
||
{
|
||
if( SET == RCC_GetFlagStatus( RCC_FLAG_PORRST) )
|
||
{
|
||
strncpy((char*)reason, "重上电启动", maxLen) ;
|
||
}
|
||
if( SET == RCC_GetFlagStatus(RCC_FLAG_SFTRST) )
|
||
{
|
||
strncpy((char*)reason, "软复位启动", maxLen) ;
|
||
}
|
||
if( SET == RCC_GetFlagStatus(RCC_FLAG_IWDGRST) )
|
||
{
|
||
strncpy((char*)reason, "独立看门狗启动", maxLen) ;
|
||
}
|
||
if( SET == RCC_GetFlagStatus(RCC_FLAG_WWDGRST) )
|
||
{
|
||
strncpy((char*)reason, "窗口看门狗启动", maxLen) ;
|
||
}
|
||
if( (RESET == RCC_GetFlagStatus(RCC_FLAG_SFTRST)) &&
|
||
(RESET == RCC_GetFlagStatus(RCC_FLAG_IWDGRST)) &&
|
||
(RESET == RCC_GetFlagStatus(RCC_FLAG_WWDGRST)) &&
|
||
(SET == RCC_GetFlagStatus(RCC_FLAG_PINRST))
|
||
)
|
||
{
|
||
strncpy((char*)reason, "硬复位启动", maxLen) ;
|
||
}
|
||
RCC_ClearFlag() ;
|
||
}
|
||
|
||
/****************************************************************************
|
||
* 名 称:void OccpyEc20(TickType_t timeout, char* file, int line)
|
||
* 功 能:获取EC20资源, 如果超过timeout没有获取到资源则写入LOG然后重启系统
|
||
* 注 意:一直挂起延时为:portMAX_DELAY
|
||
****************************************************************************/
|
||
void OccpyEc20(TickType_t timeout, char* file, int line)
|
||
{
|
||
BaseType_t result = pdFAIL ;
|
||
result = xSemaphoreTake(ec20MutexSemaphore, timeout) ;
|
||
if( result == pdPASS )
|
||
{
|
||
memset(lastOccupyEc20, 0, 30);
|
||
snprintf( lastOccupyEc20, 30, "%s %d 抢占EC20!", file, line) ;
|
||
AppLogPrintf(lastOccupyEc20) ;
|
||
//UARTx_SendData(UART_DEBUG, lastOccupyEc20, strlen(lastOccupyEc20)) ;
|
||
}
|
||
else
|
||
{
|
||
char *buf = portMalloc(192);
|
||
snprintf( buf, 192, "上次:%s, 上次:%s, 当前%s %d 抢占EC20失败,系统软重启!", lastOccupyEc20, lastReleseEc20, file, line ) ;
|
||
WriteLogToFlash(buf) ;
|
||
AppLogPrintf(buf) ;
|
||
portFree(buf) ;
|
||
SystemSoftReset() ; //系统重启
|
||
}
|
||
}
|
||
|
||
/****************************************************************************
|
||
* 名 称:void TcpUpFlashLog(void)
|
||
* 功 能:通过TCP上传本地FLASH存储的LOG
|
||
****************************************************************************/
|
||
void TcpUpFlashLog(void)
|
||
{
|
||
uint32_t readAddr = LOGFLASH_START_ADDRESS ;
|
||
uint8_t* logReadBuffer = portMalloc(FLASH_PAGE_SIZE/8) ;
|
||
uint16_t n = uLogFlash.sLogFlash.writeOffset/(FLASH_PAGE_SIZE/8) ;
|
||
while(n--)
|
||
{
|
||
Read_Flash_Byte(readAddr, logReadBuffer, (FLASH_PAGE_SIZE/8)) ;
|
||
TcpWritedata( TCPLOGFRAME, "本地log:%s", (char*)logReadBuffer);
|
||
readAddr = readAddr+(FLASH_PAGE_SIZE/8) ;
|
||
}
|
||
memset(logReadBuffer, 0, (FLASH_PAGE_SIZE/8)) ;
|
||
Read_Flash_Byte(readAddr, logReadBuffer, uLogFlash.sLogFlash.writeOffset%(FLASH_PAGE_SIZE/8) ) ;
|
||
TcpWritedata( TCPLOGFRAME, "本地log:%s", (char*)logReadBuffer) ;
|
||
portFree(logReadBuffer) ;
|
||
Erase_LogFlash() ;
|
||
}
|
||
|
||
/**************************************************************************************************
|
||
* 名 称: void RefreshOledTime(void)
|
||
* 功能说明: 刷新显示屏第3行、4行显示时间“2018-03-20 17:51”
|
||
**************************************************************************************************/
|
||
void RefreshOledTime(void)
|
||
{
|
||
static Calendar_u uLastTime ;
|
||
if( currentMenuIndex == 0 ) //HTTP网络状态标志位为STATE_OK,表示已经获取到正确时间 (GETHTTPSTATE == STATE_OK) &&
|
||
{
|
||
if( memcmp(uLastTime.bytes, uCalendar.bytes, 16) == 0 ) /*秒钟之前的数据完全相同,则只改变时间点*/
|
||
{
|
||
DisplayPointBlink() ;
|
||
}
|
||
else /*年月日时分有发生改变*/
|
||
{
|
||
DisplayTime() ;
|
||
strncpy((char*)uLastTime.bytes, (const char*)uCalendar.bytes, 20) ;
|
||
}
|
||
}
|
||
}
|
||
|
||
/**************************************************************************************************
|
||
* 名 称: void Rtc_Alr_Callback(void)
|
||
* 功能说明: RTC闹钟中断回调函数,中断内先重设闹钟时间为第二天,同时重启设备
|
||
**************************************************************************************************/
|
||
void Rtc_Alr_Callback(void)
|
||
{
|
||
Calendar_u uAlaCal ;
|
||
memcpy(uAlaCal.bytes, uCalendar.bytes, CALENDAR_LEN) ;
|
||
uAlaCal.sCalendar.w_date[1] += 1 ; //天数+1天
|
||
memcpy((u8*)uAlaCal.sCalendar.hour, "01", 2) ; //凌晨1点闹钟时、分取决于此刻的时、分值//
|
||
//memcpy((u8*)uAlaCal.sCalendar.min, "42", 2) ; //凌晨30分//uAlaCal.sCalendar.sec[0] += 2 ;
|
||
RTC_Alarm_Set(&uAlaCal) ;
|
||
WriteLogToFlash((char*)uCalendar.bytes) ; //将新日期写入FLASH中
|
||
WriteLogToFlash("Rtc_Alr_Callback!") ;
|
||
SysLog("Rtc_Alr_Callback!") ;
|
||
SystemSoftReset() ;
|
||
}
|
||
|
||
/**************************************************************************************************
|
||
* 名 称: void InitApplictationState(Application *appPointer)
|
||
* 功能说明: 对Application类型数据进行初始化
|
||
* 入口参数: @param *appPointer Application结构指针
|
||
*************************************************************************************************/
|
||
void InitApplictationState(Application *appPointer)
|
||
{
|
||
memset(appPointer->appVers, 0, VERSION_LEN+1) ;
|
||
memset(appPointer->bootVers, 0, VERSION_LEN+1) ;
|
||
memset(appPointer->macId, 0, MAC_BYTES_LEN+1) ;
|
||
}
|
||
|
||
|
||
void SetAppVersion(Application *appPointer, char *version)
|
||
{
|
||
strncpy((char*)appPointer->appVers, version, VERSION_LEN) ;
|
||
*(appPointer->appVers+VERSION_LEN+1) = 0 ;
|
||
}
|
||
|
||
void SetBootVersion(Application *appPointer, char *version)
|
||
{
|
||
strncpy((char*)appPointer->bootVers, version, VERSION_LEN) ;
|
||
*(appPointer->bootVers+VERSION_LEN+1) = 0 ;
|
||
}
|