2024-08-22 16:24:54 +08:00
|
|
|
|
/*------------------------------------------includes--------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "rtthread.h"
|
|
|
|
|
#include "drv_uart.h"
|
2024-08-23 14:07:32 +08:00
|
|
|
|
#include "bsp_rtc.h"
|
2024-09-05 10:01:45 +08:00
|
|
|
|
#include "drv_gpio.h"
|
|
|
|
|
#include "dev_pin.h"
|
|
|
|
|
#include "user_rtt.h"
|
2024-09-18 14:03:55 +08:00
|
|
|
|
#include "bsp_flash.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include "bsp_io_dev.h"
|
2024-08-22 16:24:54 +08:00
|
|
|
|
|
|
|
|
|
#define LOG_TAG "main"
|
|
|
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
|
|
|
#include <ulog.h>
|
|
|
|
|
|
|
|
|
|
/*--------------------------------------------variables-----------------------------------------*/
|
2024-09-18 14:03:55 +08:00
|
|
|
|
#define ALARM_THREAD_PRIORITY 20
|
|
|
|
|
#define ALARM_THREAD_TIMESLICE 50
|
|
|
|
|
|
2024-09-20 11:14:25 +08:00
|
|
|
|
#define RT_ALARM_THREAD_STACK_SIZE (768)
|
2024-09-18 14:03:55 +08:00
|
|
|
|
|
|
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
|
|
|
|
static rt_uint8_t alarm_thread_stack[RT_ALARM_THREAD_STACK_SIZE];
|
|
|
|
|
static struct rt_thread alarm_thread;
|
|
|
|
|
|
|
|
|
|
/* Global Variable */
|
|
|
|
|
static rt_uint32_t event_flags[kMaxEventcnt] = {0};
|
|
|
|
|
|
|
|
|
|
/* Alarm Event */
|
|
|
|
|
struct rt_event alarm_event;
|
|
|
|
|
|
|
|
|
|
static rt_bool_t is_event_initialized = RT_FALSE; //是否完成初始化
|
|
|
|
|
|
|
|
|
|
EventIndex event_index;
|
2024-08-22 16:24:54 +08:00
|
|
|
|
/*-------------------------------------------functions------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
2024-09-14 16:50:58 +08:00
|
|
|
|
LOG_I("SystemCoreClock = %d MHZ\n", SystemCoreClock / 1000000);
|
2024-08-22 16:24:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 14:03:55 +08:00
|
|
|
|
// 系统复位
|
2024-08-22 16:24:54 +08:00
|
|
|
|
void System_Reset(void)
|
|
|
|
|
{
|
2024-09-18 14:03:55 +08:00
|
|
|
|
NVIC_SystemReset();
|
2024-08-22 16:24:54 +08:00
|
|
|
|
}
|
2024-08-23 11:16:27 +08:00
|
|
|
|
MSH_CMD_EXPORT(System_Reset, "System_Reset");
|
2024-09-18 14:03:55 +08:00
|
|
|
|
|
|
|
|
|
static void Set_Event(AlarmEvent event_type)
|
|
|
|
|
{
|
|
|
|
|
if (event_type < kMaxEventcnt)
|
|
|
|
|
{
|
|
|
|
|
event_flags[event_type] = (1 << event_type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Alarm_Init_event(void)
|
|
|
|
|
{
|
|
|
|
|
if (!is_event_initialized)
|
|
|
|
|
{
|
|
|
|
|
rt_event_init(&alarm_event, "alarm_event", RT_IPC_FLAG_FIFO);
|
|
|
|
|
is_event_initialized = RT_TRUE;
|
|
|
|
|
|
|
|
|
|
for (AlarmEvent event = kPowerOnEvent; event < kMaxEventcnt; event++)
|
|
|
|
|
{
|
|
|
|
|
Set_Event(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rt_uint32_t Get_Sys_Event_Flag(AlarmEvent event_type)
|
|
|
|
|
{
|
|
|
|
|
if (event_type < kMaxEventcnt)
|
|
|
|
|
{
|
|
|
|
|
return event_flags[event_type];
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static rt_bool_t Is_Event_Init_Func(void)
|
|
|
|
|
{
|
|
|
|
|
return is_event_initialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Send_Laser_Alarm_Event(AlarmEvent event_type)
|
|
|
|
|
{
|
|
|
|
|
if (Is_Event_Init_Func())
|
|
|
|
|
{
|
|
|
|
|
rt_event_send(&alarm_event, Get_Sys_Event_Flag(event_type));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("Alarm event is not initialized!\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _Self_Check_Mode(void)
|
|
|
|
|
{
|
|
|
|
|
LED_STOP(r);
|
|
|
|
|
LED_STOP(g);
|
|
|
|
|
LED_STOP(y);
|
|
|
|
|
|
|
|
|
|
LED_CTRL(r, "500,1000", 6);
|
|
|
|
|
LED_START(r);
|
|
|
|
|
rt_thread_mdelay(500);
|
|
|
|
|
|
|
|
|
|
LED_CTRL(g, "500,1000", 6);
|
|
|
|
|
LED_START(g);
|
|
|
|
|
rt_thread_mdelay(500);
|
|
|
|
|
|
|
|
|
|
LED_CTRL(y, "500,1000", 6);
|
|
|
|
|
LED_START(y);
|
|
|
|
|
rt_thread_mdelay(500 + 4500);
|
|
|
|
|
|
|
|
|
|
// 第6s开始蜂鸣器叫 3 次
|
|
|
|
|
BUZZER_CYCLE_1000MS(3);
|
2024-09-19 11:46:33 +08:00
|
|
|
|
// AudioPlayControl_Ctrl(&audio_play_control, kBuzzerVoice, -1);
|
2024-09-18 14:03:55 +08:00
|
|
|
|
// Send_Voice_Event(kBuzzerVoice);
|
|
|
|
|
rt_thread_mdelay(3000);
|
|
|
|
|
|
|
|
|
|
//第9s关闭电磁阀
|
|
|
|
|
LOG_I("自检电磁阀动作\n");
|
|
|
|
|
|
|
|
|
|
EMV_ACT;
|
|
|
|
|
LOG_I("自检风机动作\n");
|
|
|
|
|
FJ_ON;
|
|
|
|
|
rt_thread_mdelay(3000);
|
|
|
|
|
FJ_OFF;
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
rt_thread_mdelay(20);
|
|
|
|
|
}
|
|
|
|
|
static void Alarm_Thread_Recv_Event(void *param)
|
|
|
|
|
{
|
|
|
|
|
rt_err_t result;
|
|
|
|
|
rt_uint32_t received_event;
|
|
|
|
|
|
|
|
|
|
if (std_rcc_get_reset_flag(RCC_RESET_FLAG_PMU) != RESET)
|
|
|
|
|
{
|
|
|
|
|
std_rcc_clear_reset_flags();
|
|
|
|
|
Send_Laser_Alarm_Event(kPowerOnEvent);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Send_Laser_Alarm_Event(kPreheatingEvent);
|
|
|
|
|
}
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
result = rt_event_recv(&alarm_event,
|
|
|
|
|
Get_Sys_Event_Flag(kPowerOnEvent) | //上电
|
|
|
|
|
Get_Sys_Event_Flag(kPowerDownEvent) | //掉电
|
|
|
|
|
Get_Sys_Event_Flag(kPreheatingEvent) | //预热
|
|
|
|
|
Get_Sys_Event_Flag(kNormalDetectionEvents) | //正常检测
|
|
|
|
|
Get_Sys_Event_Flag(kAlarmEvent) | //报警
|
|
|
|
|
Get_Sys_Event_Flag(kAlarmRcyEvent) | //报警恢复
|
|
|
|
|
Get_Sys_Event_Flag(kSelfInspectionEvents) | //自检
|
|
|
|
|
Get_Sys_Event_Flag(kFaultEvent) | //故障模式
|
|
|
|
|
Get_Sys_Event_Flag(kFaultRcyEvent), //故障恢复
|
|
|
|
|
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
|
|
|
|
|
RT_WAITING_FOREVER, &received_event);
|
|
|
|
|
if (result == RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
if (received_event & Get_Sys_Event_Flag(kPowerOnEvent)) //上电
|
|
|
|
|
{
|
|
|
|
|
LOG_D("上电模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.current_event = kPowerOnEvent;
|
|
|
|
|
|
|
|
|
|
Flash_Write_Record(kRecordPowerOn);
|
|
|
|
|
Send_Laser_Alarm_Event(kPreheatingEvent);
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kPreheatingEvent)) //预热
|
|
|
|
|
{
|
|
|
|
|
LOG_D("预热模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kPreheatingEvent;
|
|
|
|
|
|
|
|
|
|
GREEN_LED_PREAT;
|
|
|
|
|
rt_thread_mdelay(WARM_UP_TIMER_TICKS);
|
|
|
|
|
LOG_D("预热完成\n");
|
|
|
|
|
BUZZER_CYCLE_1000MS(1);//蜂鸣器叫一下
|
|
|
|
|
Send_Laser_Alarm_Event(kNormalDetectionEvents);
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kNormalDetectionEvents)) //正常检测
|
|
|
|
|
{
|
|
|
|
|
LOG_D("正常检测模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kNormalDetectionEvents;
|
|
|
|
|
|
|
|
|
|
GREEN_LED_NORMAL;
|
|
|
|
|
|
|
|
|
|
if (event_index.last_event == kPreheatingEvent) //如果是从预热到正常检测模式,则等个1秒钟,让蜂鸣器叫一下再关闭
|
|
|
|
|
{
|
|
|
|
|
rt_thread_delay(1000);
|
|
|
|
|
BUZZER_STOP(buzzer);
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kAlarmEvent)) //报警
|
|
|
|
|
{
|
|
|
|
|
LOG_D("报警模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kAlarmEvent;
|
|
|
|
|
|
|
|
|
|
EMV_ACT;
|
|
|
|
|
FJ_ON;
|
|
|
|
|
|
|
|
|
|
Flash_Write_Record(kRecordAlarm); //写入flash报警信息
|
|
|
|
|
|
|
|
|
|
RED_LED_ALAEM;
|
|
|
|
|
|
|
|
|
|
// rt_thread_mdelay(8000); //等待语音播报完成,再蜂鸣器响所以这个延时时间一定要大于等于语音播放时间,不然会播放中断,播放下一条
|
|
|
|
|
|
|
|
|
|
BUZZER_CYCLE_500MS(-1);
|
|
|
|
|
// AudioPlayControl_Ctrl(&audio_play_control, kBuzzerVoice, -1);//播放蜂鸣器前要先设置参数,才能播放,否则可能会出现问题
|
|
|
|
|
// Send_Voice_Event(kBuzzerVoice); //播放蜂鸣器
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kAlarmRcyEvent))//报警恢复
|
|
|
|
|
{
|
|
|
|
|
LOG_D("报警恢复模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kAlarmRcyEvent;
|
|
|
|
|
|
|
|
|
|
Flash_Write_Record(kRecordAlarmRcy);
|
|
|
|
|
LOG_D("关闭风机\n");
|
|
|
|
|
FJ_OFF;
|
|
|
|
|
LOG_D("关闭蜂鸣器\n");
|
|
|
|
|
BUZZER_STOP(buzzer);
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
// rt_thread_mdelay(20);
|
|
|
|
|
|
|
|
|
|
// Send_Voice_Event(kAlarmRcyEventVoice);
|
|
|
|
|
// rt_thread_mdelay(8000);
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
|
|
|
|
|
Send_Laser_Alarm_Event(kNormalDetectionEvents);
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kFaultEvent)) //故障
|
|
|
|
|
{
|
|
|
|
|
LOG_D("故障模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kFaultEvent;
|
|
|
|
|
|
|
|
|
|
YELLOW_LED_FAULT;
|
|
|
|
|
Flash_Write_Record(kRecordFault);
|
|
|
|
|
BUZZER_CYCLE_20000MS(-1);
|
|
|
|
|
// Send_Voice_Event(kFaultEventVoice);
|
|
|
|
|
// rt_thread_mdelay(8000); //这里如果发生恢复,应不应该让他播放完这个语音,还是直接停止播放。
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kFaultRcyEvent)) //故障恢复
|
|
|
|
|
{
|
|
|
|
|
LOG_D("故障恢复模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kFaultRcyEvent;
|
|
|
|
|
|
|
|
|
|
Flash_Write_Record(kRecordFaultRcy);
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
// rt_thread_mdelay(20);
|
|
|
|
|
// Send_Voice_Event(kFaultRcyEventVoice);
|
|
|
|
|
// rt_thread_mdelay(8000);
|
|
|
|
|
// Send_Voice_Event(kVoiceStop);
|
|
|
|
|
BUZZER_STOP(buzzer);
|
|
|
|
|
Send_Laser_Alarm_Event(kNormalDetectionEvents);
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kPowerDownEvent))//掉电
|
|
|
|
|
{
|
|
|
|
|
LOG_D("掉电模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kPowerDownEvent;
|
|
|
|
|
|
|
|
|
|
// 写入掉电记录
|
|
|
|
|
Flash_Write_Record(kRecordPowerDown);
|
|
|
|
|
LOG_E("Enter AnalogWatchdog Interrupt");
|
|
|
|
|
|
|
|
|
|
for (rt_uint16_t i = 0; i < 65535; i++)
|
|
|
|
|
{
|
|
|
|
|
rt_hw_us_delay(999);
|
|
|
|
|
LOG_D("i = %d", i);
|
|
|
|
|
}
|
|
|
|
|
rt_thread_mdelay(1000);
|
|
|
|
|
// 还是没断电的话就重启
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kSelfInspectionEvents)) // <20>Լ<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
LOG_D("自检模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kSelfInspectionEvents;
|
|
|
|
|
|
|
|
|
|
_Self_Check_Mode();
|
|
|
|
|
Send_Laser_Alarm_Event(event_index.last_event);
|
|
|
|
|
}
|
|
|
|
|
else if (received_event & Get_Sys_Event_Flag(kSensorFailureEvent)) // ʧЧ
|
|
|
|
|
{
|
|
|
|
|
LOG_D("传感器失效模式\n");
|
|
|
|
|
|
|
|
|
|
event_index.last_event = event_index.current_event;
|
|
|
|
|
event_index.current_event = kSensorFailureEvent;
|
|
|
|
|
|
|
|
|
|
Flash_Write_Record(kRecordSensorFailure);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Alarm_Handle(void)
|
|
|
|
|
{
|
|
|
|
|
Alarm_Init_event();
|
|
|
|
|
|
|
|
|
|
rt_thread_init(&alarm_thread,
|
|
|
|
|
"alarm_thread",
|
|
|
|
|
Alarm_Thread_Recv_Event,
|
|
|
|
|
RT_NULL,
|
|
|
|
|
&alarm_thread_stack[0],
|
|
|
|
|
sizeof(alarm_thread_stack),
|
|
|
|
|
ALARM_THREAD_PRIORITY, ALARM_THREAD_TIMESLICE);
|
|
|
|
|
|
|
|
|
|
rt_thread_startup(&alarm_thread);
|
|
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
|
}
|
|
|
|
|
INIT_APP_EXPORT(Alarm_Handle);
|