From c85c13f959683be63bb74286aea1ede77810bbf4 Mon Sep 17 00:00:00 2001 From: chenfu <2412777093@qq.com> Date: Fri, 23 Jun 2023 17:05:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=AD=A6=E6=8A=A5=E5=A3=B0=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=B0DaemonTask?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 ++ bsp/bsp_init.c | 4 ++-- bsp/bsp_legacy_support/bsp_buzzer.c | 28 ---------------------------- bsp/bsp_legacy_support/bsp_buzzer.h | 10 ---------- bsp/pwm/bsp_pwm.c | 10 +++++++--- modules/daemon/daemon.c | 11 ++++++++++- modules/daemon/daemon.h | 23 +++++++++++++++++++++-- 7 files changed, 42 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index c9c229f..8b71b87 100644 --- a/Makefile +++ b/Makefile @@ -153,6 +153,7 @@ modules/can_comm/can_comm.c \ modules/message_center/message_center.c \ modules/daemon/daemon.c \ modules/vofa/vofa.c \ +modules/alarm/buzzer.c \ application/gimbal/gimbal.c \ application/chassis/chassis.c \ application/shoot/shoot.c \ @@ -268,6 +269,7 @@ C_INCLUDES = \ -Imodules/message_center \ -Imodules/daemon \ -Imodules/vofa \ +-Imodules/alarm \ -Imodules diff --git a/bsp/bsp_init.c b/bsp/bsp_init.c index d85f809..1166f09 100644 --- a/bsp/bsp_init.c +++ b/bsp/bsp_init.c @@ -2,7 +2,7 @@ #include "bsp_log.h" #include "bsp_dwt.h" #include "bsp_usb.h" -#include "bsp_buzzer.h" +#include "buzzer.h" #include "bsp_led.h" #include "bsp_temperature.h" @@ -16,5 +16,5 @@ void BSPInit() // legacy support,待删除,将在实现了led/tempctrl/buzzer的module之后移动到app层进行XXXRegister() LEDInit(); IMUTempInit(); - BuzzerInit(); + buzzer_init(); } \ No newline at end of file diff --git a/bsp/bsp_legacy_support/bsp_buzzer.c b/bsp/bsp_legacy_support/bsp_buzzer.c index 625d53f..e69de29 100644 --- a/bsp/bsp_legacy_support/bsp_buzzer.c +++ b/bsp/bsp_legacy_support/bsp_buzzer.c @@ -1,28 +0,0 @@ -#include "bsp_buzzer.h" -#include "main.h" - -#warning this is a legacy support file, please use the new version - -extern TIM_HandleTypeDef htim4; -static uint8_t tmp_warning_level = 0; - -void BuzzerInit() -{ - HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); -} - -void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level) -{ - if (level > tmp_warning_level) - { - tmp_warning_level = level; - __HAL_TIM_PRESCALER(&htim4, psc); - __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwm); - } -} - -void BuzzerOff(void) -{ - __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0); - tmp_warning_level = 0; -} diff --git a/bsp/bsp_legacy_support/bsp_buzzer.h b/bsp/bsp_legacy_support/bsp_buzzer.h index cc09a68..e69de29 100644 --- a/bsp/bsp_legacy_support/bsp_buzzer.h +++ b/bsp/bsp_legacy_support/bsp_buzzer.h @@ -1,10 +0,0 @@ -#ifndef BSP_BUZZER_H -#define BSP_BUZZER_H - -#include - -void BuzzerInit(); -extern void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level); -extern void BuzzerOff(void); - -#endif diff --git a/bsp/pwm/bsp_pwm.c b/bsp/pwm/bsp_pwm.c index 410992b..9e56ef8 100644 --- a/bsp/pwm/bsp_pwm.c +++ b/bsp/pwm/bsp_pwm.c @@ -15,7 +15,7 @@ void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) { for (uint8_t i = 0; i < idx; i++) { // 来自同一个定时器的中断且通道相同 - if (pwm_instance[i]->htim == htim && htim->Channel == pwm_instance[i]->channel) + if (pwm_instance[i]->htim == htim && htim->Channel == (1<<(pwm_instance[i]->channel/4))) { if (pwm_instance[i]->callback) // 如果有回调函数 pwm_instance[i]->callback(pwm_instance[i]); @@ -39,7 +39,7 @@ PWMInstance *PWMRegister(PWM_Init_Config_s *config) pwm->callback = config->callback; pwm->id = config->id; // 启动PWM - HAL_TIM_PWM_Start(pwm->htim, pwm->channel); + HAL_TIM_PWM_Start_IT(pwm->htim, pwm->channel); __HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); // 设置占空比 pwm_instance[idx++] = pwm; @@ -65,7 +65,11 @@ void PWMSetPulse(PWMInstance *pwm, uint32_t pulse) pwm->pulse = pulse; __HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); } - +void PWMSetPeriod(PWMInstance *pwm, uint32_t period) +{ + pwm->period = period; + __HAL_TIM_PRESCALER(pwm->htim, pwm->period); +} /* 只是对HAL的函数进行了形式上的封装 */ void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size) { diff --git a/modules/daemon/daemon.c b/modules/daemon/daemon.c index 85cf752..00a4ec2 100644 --- a/modules/daemon/daemon.c +++ b/modules/daemon/daemon.c @@ -2,6 +2,7 @@ #include "bsp_dwt.h" // 后续通过定时器来计时? #include "stdlib.h" #include "memory.h" +#include "buzzer.h" // 用于保存所有的daemon instance static DaemonInstance *daemon_instances[DAEMON_MX_CNT] = {NULL}; @@ -15,7 +16,9 @@ DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config) instance->owner_id = config->owner_id; instance->reload_count = config->reload_count == 0 ? 100 : config->reload_count; instance->callback = config->callback; - + instance->alarm_state = config->alarm_state; + instance->alarm_level = config->alarm_level; + instance->temp_count = config->reload_count; daemon_instances[idx++] = instance; return instance; } @@ -36,6 +39,7 @@ void DaemonTask() DaemonInstance *dins; // 提高可读性同时降低访存开销 for (size_t i = 0; i < idx; ++i) { + dins = daemon_instances[i]; if (dins->temp_count > 0) // 如果计数器还有值,说明上一次喂狗后还没有超时,则计数器减一 dins->temp_count--; @@ -43,6 +47,11 @@ void DaemonTask() { dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用特定module的offline callback // @todo 为蜂鸣器/led等增加离线报警的功能,非常关键! + if(dins->alarm_state == ALARM_ON) + { + BuzzerPlay(dins->alarm_level); + } + } } } diff --git a/modules/daemon/daemon.h b/modules/daemon/daemon.h index e729c96..d99fdff 100644 --- a/modules/daemon/daemon.h +++ b/modules/daemon/daemon.h @@ -2,18 +2,34 @@ #define MONITOR_H #include "stdint.h" +#include "string.h" #define DAEMON_MX_CNT 64 /* 模块离线处理函数指针 */ typedef void (*offline_callback)(void *); - +typedef enum +{ + ALARM_OFF = 0, + ALARM_ON = 1, +}alarm_state_e; +typedef enum +{ + ALARM_LEVEL_LOW = 0, + ALARM_LEVEL_BELOW_MEDIUM = 1, + ALARM_LEVEL_MEDIUM = 2, + ALARM_LEVEL_ABOVE_MEDIUM = 3, + ALARM_LEVEL_HIGH = 4, + ALARM_OFFLINE = 5, +}alarm_level_e; /* daemon结构体定义 */ typedef struct daemon_ins { uint16_t reload_count; // 重载值 offline_callback callback; // 异常处理函数,当模块发生异常时会被调用 - + alarm_state_e alarm_state; // 蜂鸣器状态 + alarm_level_e alarm_level; //警报级别 + uint16_t temp_count; // 当前值,减为零说明模块离线或异常 void *owner_id; // daemon实例的地址,初始化的时候填入 } DaemonInstance; @@ -23,6 +39,9 @@ typedef struct { uint16_t reload_count; // 实际上这是app唯一需要设置的值? offline_callback callback; // 异常处理函数,当模块发生异常时会被调用 + alarm_state_e alarm_state; // 蜂鸣器状态 + alarm_level_e alarm_level; //警报级别 + void *owner_id; // id取拥有daemon的实例的地址,如DJIMotorInstance*,cast成void*类型 } Daemon_Init_Config_s;