TT12-MCU/applications/_main.c

240 lines
5.8 KiB
C
Raw Normal View History

/*
* 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>
#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;
static void initEvent(void)
{
/* 事 件 控 制 块 */
rt_err_t result = rt_event_init(&sw_check, "SHcheck", RT_IPC_FLAG_PRIO);
result = rt_event_init(&hw_check, "HWcheck", RT_IPC_FLAG_PRIO) | result;
if (result != RT_EOK)
{
LOG_E("init event failed.\n");
}
}
2023-06-02 06:37:58 +00:00
INIT_COMPONENT_EXPORT(initEvent);
void upSWflag(void)
{
rt_event_send(&sw_check, FILE_IS_OK);
}
void upTTflag(void)
{
rt_event_send(&sw_check, TT_IS_OK);
}
/**
*
*/
static void updatecfg()
{
maxTTWaitTime = get_cfg("maxTTWaitTime");
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
}
2023-06-02 06:37:58 +00:00
//INIT_COMPONENT_EXPORT(t3);
extern void ttinfoInit(void);
extern void startTTinfo(void);
/**
* TT状态1TT连续5个周期为激活状态且信号强度不低于5
*/
void checkTT()
{
repGetTT(); //持续更新
}
2023-07-26 08:26:53 +00:00
static void upSendFile_thread_entry(void)
{
//等待事件
extern struct rt_event hw_check; //硬件条件
static rt_uint8_t d[10][200] = { };
static rt_uint8_t s[10] = { };
while (1)
{
if (rt_event_recv(&hw_check, ALL_READY, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, RT_NULL) == RT_EOK)
{
//get file to be send
char *f = "2023_06_16_10_28_00_123.bin";
// char *f="1023_05_19_15_29_59_254.txt";
// pack file
rt_uint8_t len = 0;
len = pack_File(f, 0, d, s);
rt_kprintf("len is %d\n", len);
if (len)
{
for (size_t var = 0; var < len; var++)
{
sendMsg(d[var], s[var]);
}
tcpClose();
LOG_I("Done.");
}
}
}
}
//MSH_CMD_EXPORT(getFileData_thread_entry,tet);
2023-07-26 08:26:53 +00:00
static void upSendFile(void)
{
/* 创建 serial 线程 */
2023-07-26 08:26:53 +00:00
rt_thread_t thread = rt_thread_create("upSendFile", upSendFile_thread_entry, RT_NULL, 1024 * 12, 27, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
2023-07-26 08:26:53 +00:00
LOG_E("thread 'upSendFile' create failure.");
return;
}
}
2023-07-26 08:26:53 +00:00
static void chkAndSendFile_thread_entry()
{
static rt_err_t rst = RT_ERROR;
updatecfg();
static rt_uint8_t d[10][200] = { };
2023-06-16 08:20:18 +00:00
static rt_uint8_t s[10] = { };
while (1)
{
// int e;
LOG_I("等待软件就绪");
rst = rt_event_recv(&sw_check, FILE_IS_OK | TIMER_IS_OUT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, RT_NULL);
if (rst == RT_EOK) //软件条件满足
{
//检查硬件条件
LOG_I("软件就绪开始等待TT");
// continue;
checkTT();
//打包数据
}
for (size_t var = 0; var < maxTTRetryCnt; var++) //轮询尝试
{
LOG_I("第%d次尝试。", var + 1);
// rst = rt_event_recv(&sw_check, TT_IS_OK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, maxTTWaitTime*60*1000,
// RT_NULL);
rst = rt_sem_take(TTReady, rt_tick_from_millisecond(maxTTWaitTime * 60 * 1000));
if (rst == RT_EOK) //硬件条件满足
{
2023-06-16 08:20:18 +00:00
LOG_I("TT准备就绪");
rt_event_send(&hw_check, ALL_READY);
break;
}
else if (rst == -RT_ETIMEOUT)
; //超时则重试
{
LOG_W("第%d次尝试中TT准备失败。", var + 1);
// pwTT_thread_entry("0");
rt_thread_mdelay(1000);
checkTT();
// continue;
}
}
//continue;
2023-07-26 08:26:53 +00:00
//发送数据
//可能有多个待发文件,每个文件打包后为一个二维数组
2023-07-26 08:26:53 +00:00
//如果先打包后发送,需要三维数组,和大内存占用
//故改为“打包-发送”轮询操作
// upSendFile();
LOG_I("----------------------------");
}
}
2023-07-26 08:26:53 +00:00
void chkAndSendFile()
{
/* 创建 serial 线程 */
2023-07-26 08:26:53 +00:00
rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 5, 24, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
2023-07-26 08:26:53 +00:00
LOG_E("thread 'chk&send' create failure.");
return;
}
}
2023-06-19 07:56:45 +00:00
//INIT_COMPONENT_EXPORT(smsg);
//tcp连接保活
//实际场景不存在中间断掉的可能
void iniCnt()
{
rt_hw_stm32_eth_init(); //激活网口
rt_thread_mdelay(5000);
while (1 && !isTCPok()) //判断TCP连接是否正常异常自动重连
{
// while (!isTCPok())
{
LOG_W("TT server is not ready.");
tcpInit();
LOG_D("s=%d",isTCPok());
// rt_thread_mdelay(5000);
}
{
LOG_D("TT server is ready.");
tcpRecMQ();
recTT();
rt_thread_mdelay(1000);
}
}
}
void deiniCnt()
{
rt_hw_stm32_eth_deinit(); //qu激活网口
}
#define FUNC_DEMO
#ifdef FUNC_DEMO //测试时导出命令到控制台
2023-07-26 08:26:53 +00:00
MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile);
MSH_CMD_EXPORT(upSendFile, upSendFile);
MSH_CMD_EXPORT(iniCnt,tcp连接);
MSH_CMD_EXPORT(deiniCnt,tcp连接);
#endif