清理了部分无用代码

更新了部分逻辑
This commit is contained in:
CSSC-WORK\murmur 2023-07-28 16:29:57 +08:00
parent 1891a2b388
commit 5816baef54
13 changed files with 312 additions and 182 deletions

View File

@ -17,7 +17,7 @@ CONFIG_RT_USING_HOOK=y
CONFIG_RT_HOOK_USING_FUNC_PTR=y
CONFIG_RT_USING_IDLE_HOOK=y
CONFIG_RT_IDLE_HOOK_LIST_SIZE=4
CONFIG_IDLE_THREAD_STACK_SIZE=256
CONFIG_IDLE_THREAD_STACK_SIZE=512
CONFIG_RT_USING_TIMER_SOFT=y
CONFIG_RT_TIMER_THREAD_PRIO=4
CONFIG_RT_TIMER_THREAD_STACK_SIZE=512
@ -340,7 +340,7 @@ CONFIG_ULOG_LINE_BUF_SIZE=256
#
# log format
#
CONFIG_ULOG_OUTPUT_FLOAT=y
# CONFIG_ULOG_OUTPUT_FLOAT is not set
CONFIG_ULOG_USING_COLOR=y
CONFIG_ULOG_OUTPUT_TIME=y
CONFIG_ULOG_TIME_USING_TIMESTAMP=y

View File

@ -1,3 +1,2 @@
- TT接收数据
- 完成开窗功能
--解决关闭TT时异常重启的问题
--配置文件更新,配置上传下载

View File

@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2023-06-01 murmur the first version
* TT相关的初始化
*/
#include <rtthread.h>
@ -18,15 +19,80 @@
//#include <cfg.h>
#include <usrcfg.h>
#include <ttTR.h>
static struct rt_event sw_check; //软件条件
static struct rt_event hw_check; //硬件条件
extern rt_sem_t TTReady;
#define ALL_READY 1
static int maxTTWaitTime = 4;
static int maxTTRetryCnt = 3;
rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady);
rt_sem_t cfgUpdate = RT_NULL;
rt_sem_t shuntDownTT = RT_NULL;
void TTisReady(void)
{
rt_sem_release(TTReady);
}
SYS_CFG scfg={
.sendInterval =60,
.maxTTWaitTime = 4,
.maxTTRetryCnt = 3,
.minTTPeriCnt=5,
.minTTsinal=5,
.timeout=5
};
/**
*
*/
static void updatecfg(void)
{
//因为不知原因,采用事件集独立更新配置出错,无精力深查
//独立响应单个参数更新事件,程序上更复杂也没特别必要
//现采用事件通知、统一全部重新加载
while(1)
{
if(rt_sem_take(cfgUpdate, RT_WAITING_FOREVER) == RT_EOK)
{
scfg.maxTTWaitTime = get_cfg("maxTTWaitTime");
scfg.maxTTRetryCnt = get_cfg("maxTTRetryCnt");
scfg.minTTPeriCnt = get_cfg("minTTPeriCnt");
scfg.minTTsinal = get_cfg("minTTsinal");
scfg.timeout = get_cfg("timeout");
}
LOG_D("cfg updated.");
}
}
void sysSemInit()
{
TTReady = rt_sem_create("TTisReady", 0, RT_IPC_FLAG_PRIO);
cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO);
shuntDownTT = rt_sem_create("shuntDNTT", 0, RT_IPC_FLAG_PRIO);
rt_sem_release(cfgUpdate); //上电更新值
updatecfg();
// LOG_D("sysSemInit DONE.");
}
static void initEvent(void)
{
/* 事 件 控 制 块 */
@ -49,19 +115,17 @@ void upTTflag(void)
{
rt_event_send(&sw_check, TT_IS_OK);
}
/**
*
*/
static void updatecfg()
void d_upSWflag()
{
maxTTWaitTime = get_cfg("maxTTWaitTime");
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
// extern struct rt_event sw_check;//软件条件
// rt_event_send(&sw_check, FILE_IS_OK);
// void upSWflag(void);
upSWflag();
}
//INIT_COMPONENT_EXPORT(t3);
extern void ttinfoInit(void);
extern void startTTinfo(void);
//extern void ttinfoInit(void);
//extern void startTTinfo(void);
/**
* TT状态1TT连续5个周期为激活状态且信号强度不低于5
*/
@ -199,13 +263,81 @@ void chkAndSendFile()
}
//INIT_COMPONENT_EXPORT(smsg);
static rt_timer_t tmrToPNTT=RT_NULL;
static int isInWindow=0;//winow 关闭时复位
/**
* TT
*/
void stopTM()
{
if (!tmrToPNTT) {
return;
}
rt_timer_stop(tmrToPNTT);
rt_timer_stop(tmrToPNTT);
}
/**
* TT
*/
void resetTM()
{
// LOG_D("try to reset");
if (tmrToPNTT == RT_NULL) {
return;
}
if (isInWindow) {
stopTM();
return;
}
rt_tick_t t= 10*1000;//rt_tick_from_millisecond(scfg.timeout*60*1000);
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_SET_TIME,(void*) &t);
rt_timer_stop(tmrToPNTT);
rt_timer_start(tmrToPNTT);
// LOG_D("timer reseted.");
}
void d_getRemain()
{
LOG_D("=%d=",rt_tick_from_millisecond(10*60*1000));
rt_tick_t *arg1,*arg2;
arg1=rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_TIME, (void*)arg1);
arg2=rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_REMAIN_TIME, (void*)arg2);
LOG_D("tm=%d-%d",&arg1,&arg2);
}
static int toPN=0;
/* 定时器超时函数 */
static void timeoutFunc(void *parameter)
{
// rt_kprintf("periodic timer is timeout %d\n", cnt);
// rt_timer_stop(timer1);
LOG_W("3s to shunt down TT");
rt_thread_mdelay(1000);
LOG_W("2s to shunt down TT");
rt_thread_mdelay(1000);
LOG_W("1s to shunt down TT");
rt_thread_mdelay(1000);
deInitTT();//Function[rt_mutex_take] shall not be used in ISR
// rt_sem_release(shuntDownTT);
// toPN =1;
}
//RT_TICK_PER_SECOND
//tcp连接保活
//实际场景不存在中间断掉的可能
void initTT_thread_entry()
{
pwTT_thread_entry("1");
while (1)
{
if (!tmrToPNTT) {
tmrToPNTT = rt_timer_create("TTtimeout", timeoutFunc,
RT_NULL, rt_tick_from_millisecond(scfg.timeout*60*1000),
RT_TIMER_FLAG_ONE_SHOT);//RT_TIMER_FLAG_SOFT_TIMER
// rt_timer_start(tmrToPNTT);
}
if (!isTTon()) {
break;
}
@ -224,42 +356,98 @@ void initTT_thread_entry()
// rt_thread_mdelay(1000);
if (isTCPok())
{
LOG_D("TT server is ready.");
LOG_D("TCP is ready.");
tcpRecMQ(); //开启tcp接收线程
recTT();
}
}
rt_thread_mdelay(3000); //chk with 3 second interval
// LOG_D("rechk-%d",isTCPok());
}
}
void deInitTT_thread_entry()
{
if (rt_sem_take(shuntDownTT, RT_WAITING_FOREVER) == RT_EOK)
// if (toPN)
{
pwTT_thread_entry("0");
rt_hw_stm32_eth_deinit(); //qu激活网口
if (tmrToPNTT)
{
rt_timer_delete(tmrToPNTT); //关闭倒计时
tmrToPNTT = RT_NULL;
}
tcpClose();
LOG_W("shunt down TT DONE");
// break;
}
// rt_thread_mdelay(1000);
// }
}
//INIT_APP_EXPORT(deInitTT_thread_entry);
/**
* TT上电TT相关的网络TCP等
*/
void initTT()
{
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("initTT", initTT_thread_entry, RT_NULL, 1024 * 1.5, 24, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'initTT' create failure.");
return;
}
rt_thread_t thread = rt_thread_create("initTT", initTT_thread_entry, RT_NULL, 1024 * 1.5, 20, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'initTT' create failure.");
return;
}
/* 创建 serial 线程 */
thread = rt_thread_create("deInitTT", deInitTT_thread_entry, RT_NULL, 1024 * 2, 30, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'deInitTT' create failure.");
return;
}
}
/**
* TT上电TT相关的网络TCP等
*/
void deInitTT()
{
tcpClose();
rt_hw_stm32_eth_deinit(); //qu激活网口
// /* 创建 serial 线程 */
// rt_thread_t thread = rt_thread_create("deInitTT", deInitTT_thread_entry, RT_NULL, 1024 * 2, 30, 10);
// /* 创建成功则启动线程 */
// if (thread != RT_NULL)
// {
// rt_thread_startup(thread);
// }
// else
// {
// LOG_E("thread 'initTT' create failure.");
// return;
// }
rt_sem_release(shuntDownTT);
}
//INIT_APP_EXPORT(deInitTT);
#define FUNC_DEMO
#ifdef FUNC_DEMO //测试时导出命令到控制台
MSH_CMD_EXPORT(d_upSWflag,FILE_IS_OK);
MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile);
MSH_CMD_EXPORT(upSendFile, upSendFile);
MSH_CMD_EXPORT(initTT,tcp连接);
MSH_CMD_EXPORT(deInitTT,tcp连接);
MSH_CMD_EXPORT(initTT,TT);
MSH_CMD_EXPORT(deInitTT,TT);
MSH_CMD_EXPORT(d_getRemain,d_getRemain);
#endif

View File

@ -123,8 +123,7 @@ void InitAlarm()
for (size_t var = 0; var < 4; var++) {
if (alarm[var] != RT_NULL) {
rt_alarm_start(alarm[var]);
// LOG_D("alarm %d started.",var+1);
LOG_D("alarm %d(%02d:%02d) started.",var+1,alarm[var]->wktime.tm_hour+8,alarm[var]->wktime.tm_min);
// LOG_D("alarm %d(%02d:%02d) started.",var+1,alarm[var]->wktime.tm_hour+8,alarm[var]->wktime.tm_min);
}
else {
LOG_E("Failed to start alarm %d.",var+1);

View File

@ -1,11 +1,7 @@
[config]
# V1.6
# V1.7
#发送间隔M以零点为基准时刻
sendInterval=60
# 用于保存串口数据的单文件最大值Byte
maxSizePerFile=4096
# 最长等待数据时间,超时后将已收数据存盘
MaxTimeoutToSave=20
# 最小文件大小,超过此大小则进入发送流程
minSizeToSend=4096
# 最小等待时间S超时后进入待机模式
@ -21,8 +17,11 @@ minTTPeriCnt=2
compressType=1
encrytType=1
# 开窗时间UTC+8,持续时间H。支持多组组与组之间采用【;】为分隔符
openWindowTime=9,1;15,1
openWindowTime=1,2,7,8
#超时定时器时间M超时后关闭TT
timeout=5
commMode=1
selfDesSW=0
[stats]
# 继电器开关次数影响继电器寿命10W次
swCnt=12

View File

@ -162,12 +162,20 @@ void upSend_thread_entry(void* parameter)
LOG_HEX("upSend", 27, dout, rst);
if (rst)
{
while (!isTCPok())
{
rt_thread_mdelay(1000);
}
if (sendMsg(dout, rst) == RT_EOK)
{
tcpClose();
LOG_I("send to TT Done.");
};
}
else//发送失败,实例不存在此种情况
{
LOG_E("send error, try to cache %d bytes data to file.",msg->len);
cacheDataToFile(msg->data, msg->len);
}
}
return 0;
}
@ -182,7 +190,7 @@ RT_WEAK int upSend(uint8_t *din, size_t len)
LOG_D("try to upsend to TT.");
uint8_t sta = 1;
while (0 && !isTTjh()) //判断TT状态
while (1 && !isTTjh()) //判断TT状态
{
static uint8_t trycnt = 0;
rt_thread_mdelay(4000);
@ -275,7 +283,7 @@ void setSelfDestructSWT(int setON)
{
LOG_I("FUNC = set selfdestruct");
//write to cfg file
set_cfg("SelfDesSW", setON);
set_cfg("selfDesSW", setON);
LOG_D("set SelfDesSW to %s",setON?"ON":"OFF");
}
/**
@ -286,7 +294,7 @@ int getSelfDestructSWT()
{
LOG_I("FUNC = get selfdestruct");
//load from cfg file
int flag = get_cfg("SelfDesSW");
int flag = get_cfg("selfDesSW");
if (flag < 0) {
LOG_W("get mode fault.");
}
@ -1029,7 +1037,6 @@ int isEthUP()
#define FUNC_DEMO
#ifdef FUNC_DEMO //测试时导出命令到控制台

View File

@ -45,16 +45,12 @@ int main(void)
rt_pin_mode(TR485_RE, PIN_MODE_OUTPUT);
rt_pin_write(TR485_RE, PIN_LOW);
// rt_pin_write(ETH_RESET_PIN, PIN_LOW);//关闭ETH
// rt_pin_write(TT_EN, PIN_HIGH);//关闭TT
//sysSemInit
sysSemInit();
if (isInWindowZone()) {//开机检查是否在开窗区间内是则给TT开机
pwTT_thread_entry("1");
// initTT();
initTT();
// pwTT_thread_entry("1");
}

View File

@ -7,56 +7,50 @@
//#include <cJSON.h>
#include <dfs_file.h>
#include <usrcfg.h>
rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady);
rt_sem_t cfgUpdate = RT_NULL;
//typedef struct
//
//rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady);
//rt_sem_t cfgUpdate = RT_NULL;
//
//
//
//#define _CFGALL
//#ifdef _CFGALL
//
//SYS_CFG scfg={
// .sendInterval =60,
// .maxTTWaitTime = 4,
// .maxTTRetryCnt = 3,
// .minTTPeriCnt=5,
// .minTTsinal=5,
// .timeout=5;
//};
//
//
//
///**
// * 更新全局参数
// */
//static void updatecfg(void)
//{
// int sendInterval;
// int maxTTWaitTime;
// int maxTTRetryCnt;
// int minTTPeriCnt;
// int minTTsinal;
//} SYS_CFG;
#define _CFGALL
#ifdef _CFGALL
SYS_CFG scfg={
.sendInterval =60,
.maxTTWaitTime = 4,
.maxTTRetryCnt = 3,
.minTTPeriCnt=5,
.minTTsinal=5,
};
/**
*
*/
void updatecfg(void)
{
//因为不知原因,采用事件集独立更新配置出错,无精力深查
//独立响应单个参数更新事件,程序上更复杂也没特别必要
//现采用事件通知、统一全部重新加载
while(1)
{
if(rt_sem_take(cfgUpdate, RT_WAITING_FOREVER) == RT_EOK)
{
scfg.maxTTWaitTime = get_cfg("maxTTWaitTime");
scfg.maxTTRetryCnt = get_cfg("maxTTRetryCnt");
scfg.minTTPeriCnt = get_cfg("minTTPeriCnt");
scfg.minTTsinal = get_cfg("minTTsinal");
}
LOG_D("cfg updated.");
}
}
//INIT_APP_EXPORT(updatecfg);
#endif
////因为不知原因,采用事件集独立更新配置出错,无精力深查
////独立响应单个参数更新事件,程序上更复杂也没特别必要
////现采用事件通知、统一全部重新加载
// while(1)
// {
// if(rt_sem_take(cfgUpdate, RT_WAITING_FOREVER) == RT_EOK)
// {
// scfg.maxTTWaitTime = get_cfg("maxTTWaitTime");
// scfg.maxTTRetryCnt = get_cfg("maxTTRetryCnt");
// scfg.minTTPeriCnt = get_cfg("minTTPeriCnt");
// scfg.minTTsinal = get_cfg("minTTsinal");
// scfg.timeout = get_cfg("timeout");
// }
// LOG_D("cfg updated.");
// }
//}
//
////INIT_APP_EXPORT(updatecfg);
//#endif
/**
@ -407,30 +401,6 @@ size_t isInByte(uint8_t *din, size_t len, uint8_t *s, size_t slen, uint8_t *dout
void sDemo()
{
// extern struct rt_event sw_check;//软件条件
// rt_event_send(&sw_check, FILE_IS_OK);
void upSWflag(void);
upSWflag();
}
MSH_CMD_EXPORT(sDemo,);
void TTisReady(void)
{
rt_sem_release(TTReady);
}
void sysSemInit()
{
TTReady = rt_sem_create("TTisReady", 0, RT_IPC_FLAG_PRIO);
cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO);
rt_sem_release(cfgUpdate); //上电更新值
LOG_D("sysSemInit DONE.");
}
MSH_CMD_EXPORT(sysSemInit,semInit);
//INIT_APP_EXPORT(sysInit);
INIT_APP_EXPORT(updatecfg);

View File

@ -178,6 +178,7 @@ void tcpRecMQ_thread_entry(void)
{
// LOG_D("%d Bytes received.",msg.size);
// LOG_HEX("tcpTT", 16, msg.data, msg.size);
resetTM();
int result = rt_mq_send(&TTrx_mq, &msg, sizeof(msg));
if (result == -RT_EFULL)
{
@ -189,7 +190,7 @@ void tcpRecMQ_thread_entry(void)
{
msg.size=0;//收到数据长度为0表示tcp断开
rt_mq_send(&TTrx_mq, &msg, sizeof(msg));
LOG_E("tcp error, close.");
LOG_W("tcp closed.");
tcpClose();
break;
}

View File

@ -473,36 +473,32 @@ void pwTT_thread_entry(void *parameter)
int flag = f;
if (strcmp(parameter,"NULL")==0)//无参数,状态翻转
{
// LOG_I("null");
// int f = rt_pin_read(TT_EN);
// rt_pin_write(TT_EN, !f);
flag = !f;
}
else
{
flag = !(rt_bool_t) atoi(parameter);
}
// if ((rt_bool_t)rt_pin_read(TT_EN) != flag)
//目标状态与当前状态一致才响应
// LOG_D("new sta");
rt_pin_write(TT_EN, flag);//
if (!flag) {
initTT();
}
else {
deInitTT();
}
// if (!flag) {
// initTT();
// }
// else {
// deInitTT();
// }
if (rt_pin_read(TT_EN))//检查设置后状态
{
LOG_I("set TT %s", "OFF");//高电平关断
add_val("swCnt");//更新统计值
// add_val("swCnt");//更新统计值
}
else
{
// add_val("swCnt");//更新统计值
LOG_I("set TT %s", "ON");//低电平开启
}

View File

@ -20,7 +20,7 @@
#include <usrcfg.h>
#include <board.h>
#define LOG_TAG "uart"
#define LOG_TAG "RS232"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
@ -29,8 +29,10 @@
#else
#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */
#endif
#define MAX_SIZE_TO_SAVE 1024*2
#ifndef MIN_FRAME_LEN
#define MIN_FRAME_LEN 10
#endif
/* 串口接收消息结构*/
struct rx_msg
{
@ -42,28 +44,15 @@ static rt_device_t serial;
/* 消息队列控制块 */
static struct rt_messagequeue rx_mq;
/* 定时器的控制块 */
static rt_timer_t timer1;
/* 定时器1超时函数 */
static void timeout1(void *parameter)
{
// rt_kprintf("periodic timer is timeout %d\n", cnt);
// rt_timer_stop(timer1);
LOG_W("超时未收到新数据。");
}
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
resetTM();//重设超时
struct rx_msg msg;
rt_err_t result;
static cachecnt=0;
static rt_size_t cachecnt=0;
cachecnt += size;
if (cachecnt < MIN_FRAME_LEN) {
LOG_W("not enough data, cached and waiting...");//处理半包
@ -76,14 +65,15 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
if ( result == -RT_EFULL)
{
/* 消息队列满 */
rt_kprintf("message queue full\n");
LOG_W("message queue full");
}
cachecnt = 0;
return result;
}
struct rx_msg msg;
extern struct rt_event update_cfg;
extern int time2Str(char *str);
//extern struct rt_event update_cfg;
//extern int time2Str(char *str);
static void serial_thread_entry(void *parameter)
{
@ -91,31 +81,19 @@ static void serial_thread_entry(void *parameter)
rt_err_t result;
rt_uint32_t rx_length=0;//单次收到的数据长度
static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
static currLen=0;//已收数据长度
static char f[30];
time2Str(f);
while (1)
{
//暂定逻辑
//先创建文件
//接收数据并存盘
//
rt_memset(&msg, 0, sizeof(msg));
/* 从消息队列中读取消息*/
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
/* 从串口读取数据*/
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
currLen += rx_length;
/* 启动定时器1 */
if (timer1 != RT_NULL)
rt_timer_start(timer1);
LOG_I("%d Bytes received from RS232",rx_length);
LOG_HEX("232rx:",16,rx_buffer,rx_length);//print what received.
parseRS232(rx_buffer, rx_length);
@ -175,11 +153,6 @@ static int uart_dma_sample(int argc, char *argv[])
rt_device_write(serial, 0, str, sizeof(str));
rt_pin_write(TR485_RE, PIN_LOW);
#endif
/* 创建定时器1 周期定时器 */
timer1 = rt_timer_create("rxtimer", timeout1,
RT_NULL, rt_tick_from_millisecond(5*60*1000),
RT_TIMER_FLAG_PERIODIC);
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024*5, 25+1, 10);

View File

@ -12,6 +12,8 @@
#include "cfg.h"
#define MIN_FRAME_LEN 10
#define WORK_BOARD
#define FILE_IS_OK 1
#define TIMER_IS_OUT 1<<1
@ -34,6 +36,7 @@ typedef struct
int minTTsinal;
int minActiveTime;
int maxActiveTime;
int timeout;
} SYS_CFG;
//struct rt_event sw_check;//软件条件

View File

@ -15,7 +15,7 @@
#define RT_HOOK_USING_FUNC_PTR
#define RT_USING_IDLE_HOOK
#define RT_IDLE_HOOK_LIST_SIZE 4
#define IDLE_THREAD_STACK_SIZE 256
#define IDLE_THREAD_STACK_SIZE 512
#define RT_USING_TIMER_SOFT
#define RT_TIMER_THREAD_PRIO 4
#define RT_TIMER_THREAD_STACK_SIZE 512
@ -207,7 +207,6 @@
/* log format */
#define ULOG_OUTPUT_FLOAT
#define ULOG_USING_COLOR
#define ULOG_OUTPUT_TIME
#define ULOG_TIME_USING_TIMESTAMP