Files
433_STM32/Core/Src/relay_control.c

185 lines
6.3 KiB
C
Raw Permalink Normal View History

/**
******************************************************************************
* @file relay_control.c
* @brief
* @author Application Layer
* @version 2.0
******************************************************************************
* @attention
*
*
* 1.
* 2.
* 3.
*
*
* v2.0 -
******************************************************************************
*/
#include "relay_control.h"
#include "uart2_print.h"
#include "main.h"
/*==============================================================================
*
*============================================================================*/
/* DEBUG_RELAY: 调试日志开关置1时启用继电器控制过程日志输出 */
#define DEBUG_RELAY 1
#if DEBUG_RELAY
/* 调试日志宏,带模块前缀"[RELAY]"方便过滤日志 */
#define DEBUG_LOG(fmt, ...) UART2_Print_Printf("[RELAY] " fmt "\r\n", ##__VA_ARGS__)
#else
#define DEBUG_LOG(fmt, ...)
#endif
/*==============================================================================
*
*============================================================================*/
/**
* @brief
* @note
*
*
* - false: (OFF)
* - true: (ON)
*
*
* false
*/
static bool current_state = false;
/**
* @brief
* @note
*
*
* 使寿
*
* (RELAY_MIN_INTERVAL)
*
*
* (ms)HAL_GetTick()
*/
static uint32_t last_toggle_tick = 0;
/*==============================================================================
*
*============================================================================*/
/**
* @brief
* @note
*
* @param
* @return
*
*
* 1.
* 2. false
* 3. 0
*
*
* -
* - OFF是考虑到故障安全(fail-safe)
*/
void Relay_Init(void)
{
/*----------------------------------------------------------
*
* RL_Control_GPIO_Port和RL_Control_Pin定义在main.h或引脚配置中
*----------------------------------------------------------*/
HAL_GPIO_WritePin(RL_Control_GPIO_Port, RL_Control_Pin, GPIO_PIN_RESET);
/*----------------------------------------------------------
* OFF
*----------------------------------------------------------*/
current_state = false;
/*----------------------------------------------------------
*
*----------------------------------------------------------*/
last_toggle_tick = 0;
DEBUG_LOG("Init OK, state=OFF");
}
/**
* @brief
* @note
*
* @param state: false=(OFF)true=(ON)()
* @return
*
*
* 1. ()
* 2. ()
* 3. GPIO操作
* 4.
*
* -
* if (current_tick - last_toggle_tick < RELAY_MIN_INTERVAL)
* return; // 切换过于频繁,直接丢弃
*
* RELAY_MIN_INTERVAL毫秒
*
*
* /
* -
* -
*/
void Relay_SetState(bool state)
{
/*----------------------------------------------------------
*
*----------------------------------------------------------*/
uint32_t current_tick = HAL_GetTick();
/*----------------------------------------------------------
*
*----------------------------------------------------------*/
if (current_tick - last_toggle_tick < RELAY_MIN_INTERVAL) {
DEBUG_LOG("Toggle too fast, ignored");
return;
}
/*----------------------------------------------------------
*
*----------------------------------------------------------*/
if (current_state == state) {
DEBUG_LOG("State unchanged: %s", state ? "ON" : "OFF");
return;
}
/*----------------------------------------------------------
* GPIO引脚电平
*----------------------------------------------------------*/
HAL_GPIO_WritePin(RL_Control_GPIO_Port, RL_Control_Pin,
state ? GPIO_PIN_SET : GPIO_PIN_RESET);
/*----------------------------------------------------------
*
*----------------------------------------------------------*/
current_state = state;
last_toggle_tick = current_tick;
DEBUG_LOG("Relay -> %s", state ? "ON" : "OFF");
}
/**
* @brief
* @note
*
* @param
* @return bool: false=(OFF)true=(ON)
*
* 使
* if (Relay_GetState()) {
* // 继电器当前处于闭合状态
* }
*/
bool Relay_GetState(void)
{
return current_state;
}