136 lines
3.1 KiB
C
136 lines
3.1 KiB
C
/*
|
||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||
*
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*
|
||
* Change Logs:
|
||
* Date Author Notes
|
||
* 2023-06-01 murmur the first version
|
||
*/
|
||
|
||
|
||
#include <rtthread.h>
|
||
|
||
#define LOG_TAG "main_scha"
|
||
#define LOG_LVL LOG_LVL_DBG
|
||
#include <ulog.h>
|
||
#include <board.h>
|
||
#include <ttmsg/ttmsg.h>
|
||
#include <cfg.h>
|
||
#include <usrcfg.h>
|
||
struct rt_event sw_check;//软件条件
|
||
struct rt_event hw_check;//硬件条件
|
||
//#define FILE_IS_OK 1
|
||
//#define TIMER_IS_OUT 1<<1
|
||
//#define TT_IS_OK 1<<2
|
||
|
||
static int maxTTWaitTime = 4;
|
||
static int maxTTRetryCnt = 3;
|
||
|
||
static initEvent(void)
|
||
{
|
||
/* 事 件 控 制 块 */
|
||
|
||
rt_err_t result = rt_event_init(&sw_check, "SHcheck", RT_IPC_FLAG_FIFO);
|
||
if (result != RT_EOK)
|
||
{
|
||
LOG_E("init event failed.\n");
|
||
}
|
||
}
|
||
INIT_COMPONENT_EXPORT(initEvent);
|
||
|
||
/**
|
||
* 更新各参数
|
||
*/
|
||
static void updatecfg()
|
||
{
|
||
maxTTWaitTime = get_cfg("maxTTWaitTime");
|
||
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
|
||
}
|
||
extern struct rt_event update_cfg;
|
||
void t3(void)
|
||
{
|
||
int32_t e;
|
||
int rst = rt_event_recv(&update_cfg, CFGCHANGEED, RT_EVENT_FLAG_OR , RT_WAITING_FOREVER, &e);
|
||
if (rst == RT_EOK) {
|
||
// update_cfg();
|
||
LOG_I("ggxb");
|
||
}
|
||
}
|
||
|
||
//INIT_COMPONENT_EXPORT(t3);
|
||
|
||
/**
|
||
* 监控TT状态。需求条件1:TT连续5个周期为激活状态且信号强度不低于5。
|
||
*/
|
||
void checkTT()
|
||
{
|
||
static rt_thread_t thread = RT_NULL;
|
||
//上电
|
||
pwTT_thread_entry("1");
|
||
//延时30s等待系统启动、可以GET信息,避免不必要的错误
|
||
rt_thread_mdelay(1000*30);
|
||
|
||
char c[3][10]={"getTT","","3"} ;
|
||
getTT(3,c);
|
||
// getTTinfo_thread_entry()
|
||
#ifdef NEW_THREAD
|
||
static CFG cfg;
|
||
memset(&cfg, 0, sizeof(CFG));
|
||
|
||
cfg.s = 3;
|
||
cfg.cnt = maxTTWaitTime/cfg.s-2;//避免通信异常
|
||
/* 创建 serial 线程 */
|
||
thread = rt_thread_create("getTT", getTTinfo_thread_entry, (void *) &cfg, 1024 * 3, 25, 10);
|
||
/* 创建成功则启动线程 */
|
||
if (thread != RT_NULL)
|
||
{
|
||
rt_thread_startup(thread);
|
||
}
|
||
else
|
||
{
|
||
LOG_E("thread 'getTT' create failure.");
|
||
return;
|
||
}
|
||
#endif
|
||
}
|
||
|
||
void try()
|
||
{
|
||
static rt_err_t rst = RT_ERROR;
|
||
updatecfg();
|
||
while(1)
|
||
{
|
||
int e;
|
||
rst = rt_event_recv(&sw_check, FILE_IS_OK | TIMER_IS_OUT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
|
||
RT_WAITING_FOREVER, &e);
|
||
if (rst == RT_EOK)//软件条件满足
|
||
{
|
||
//检查硬件条件
|
||
checkTT();
|
||
//打包数据
|
||
|
||
}
|
||
|
||
for (size_t var = 0; var < maxTTRetryCnt; ++var)//轮询尝试
|
||
{
|
||
rst = rt_event_recv(&sw_check, TT_IS_OK, RT_EVENT_FLAG_CLEAR, rt_tick_from_millisecond(maxTTWaitTime * 1000),
|
||
&e);
|
||
if (rst == RT_EOK)//硬件条件满足
|
||
{
|
||
break;
|
||
}
|
||
elseif(rst == RT_ETIMEOUT);//超时则重试
|
||
{
|
||
pwTT_thread_entry("0");
|
||
rt_thread_mdelay(1000);
|
||
checkTT();
|
||
// continue;
|
||
}
|
||
}
|
||
|
||
//发送数据
|
||
|
||
}
|
||
}
|