Files
433_STM32/Driver_RF433/Src/rf433_hal.c
zhongxuanzhen 1c6ff020e9 3.24_433_TX版本:项目去UI化,删除OLED/菜单/按键,TX已验证,RX待验证
删除内容:
- 删除OLED显示相关代码(u8g2库、I2C接口)
- 删除按键输入相关代码(key.c)
- 删除菜单系统(MultMenu整个目录)
- 删除USB CDC功能(USB_DEVICE目录)
- 删除旧的E32演示代码(e32_demo.c/h)
保留内容:
- Driver_RF433核心驱动(已封装完整)
- rf433_tx_app.c(TX应用层,独立无UI依赖)
- rf433_rx_app.c(RX应用层,独立无UI依赖)
- GPIO LED指示功能(LED_TX/LED_RX)
2026-03-24 19:39:43 +08:00

510 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
******************************************************************************
* @file rf433_hal.c
* @brief RF433硬件抽象层实现
* @note 基于原e32_hal.c重构保持与原版设备兼容
******************************************************************************
*/
#include "rf433_hal.h"
/* 与单片机平台相关 */
#include "main.h"
#include "gpio.h"
#include "usart.h"
/* ============================================================================
* 私有变量
* ============================================================================ */
static bool rf433_hal_initialized = false;
/* FIFO缓冲区 */
static uint8_t rf433_fifo_buffer[1024];
static volatile uint32_t rf433_fifo_head = 0;
static volatile uint32_t rf433_fifo_tail = 0;
static volatile uint32_t rf433_fifo_count = 0;
#define RF433_FIFO_SIZE 1024
/* ============================================================================
* 私有FIFO函数
* ============================================================================ */
/**
* @brief FIFO写入数据
*/
static rf433_hal_error_t rf433_fifo_write(const uint8_t *data, uint16_t length)
{
uint16_t i;
if (data == NULL || length == 0)
{
return RF433_HAL_ERROR_INVALID_PARAM;
}
for (i = 0; i < length; i++)
{
if (rf433_fifo_count >= RF433_FIFO_SIZE)
{
return RF433_HAL_ERROR;
}
rf433_fifo_buffer[rf433_fifo_head] = data[i];
rf433_fifo_head = (rf433_fifo_head + 1) % RF433_FIFO_SIZE;
rf433_fifo_count++;
}
return RF433_HAL_OK;
}
/**
* @brief FIFO读取数据
*/
static rf433_hal_error_t rf433_fifo_read(uint8_t *data, uint16_t length, uint16_t *actual_length)
{
uint16_t i;
if (data == NULL || length == 0 || actual_length == NULL)
{
return RF433_HAL_ERROR_INVALID_PARAM;
}
if (rf433_fifo_count == 0)
{
*actual_length = 0;
return RF433_HAL_ERROR;
}
*actual_length = 0;
for (i = 0; i < length && rf433_fifo_count > 0; i++)
{
data[i] = rf433_fifo_buffer[rf433_fifo_tail];
rf433_fifo_tail = (rf433_fifo_tail + 1) % RF433_FIFO_SIZE;
rf433_fifo_count--;
(*actual_length)++;
}
return RF433_HAL_OK;
}
/**
* @brief 获取FIFO数据长度
*/
static rf433_hal_error_t rf433_fifo_get_length(uint16_t *length)
{
if (length == NULL)
{
return RF433_HAL_ERROR_INVALID_PARAM;
}
*length = rf433_fifo_count;
return RF433_HAL_OK;
}
/**
* @brief 清空FIFO
*/
static rf433_hal_error_t rf433_fifo_clear(void)
{
rf433_fifo_head = 0;
rf433_fifo_tail = 0;
rf433_fifo_count = 0;
return RF433_HAL_OK;
}
/* UART接收相关变量 */
uint8_t rf433_uart_rx_tmp;
static volatile uint32_t rf433_uart_rx_timeout = 0;
static volatile bool rf433_uart_rx_done = false;
/* ============================================================================
* 公共函数实现
* ============================================================================ */
/**
* @brief 初始化硬件抽象层
*/
rf433_hal_error_t rf433_hal_init(void)
{
/* 检查是否已初始化 */
if (rf433_hal_initialized)
{
return RF433_HAL_ERROR;
}
/* 初始化FIFO */
rf433_fifo_head = 0;
rf433_fifo_tail = 0;
rf433_fifo_count = 0;
rf433_hal_initialized = true;
return RF433_HAL_OK;
}
/**
* @brief 反初始化硬件抽象层
*/
rf433_hal_error_t rf433_hal_deinit(void)
{
/* 检查是否已初始化 */
if (!rf433_hal_initialized)
{
return RF433_HAL_ERROR;
}
rf433_hal_initialized = false;
return RF433_HAL_OK;
}
/**
* @brief UART发送数据基于原e32_hal_uart_tx
*/
rf433_hal_error_t rf433_hal_uart_tx(uint8_t *buffer, uint16_t length)
{
HAL_StatusTypeDef ret;
/* 参数检查 */
if (buffer == NULL || length == 0)
{
return RF433_HAL_ERROR_INVALID_PARAM;
}
/* 发送数据 */
ret = HAL_UART_Transmit(&huart1, buffer, length, 0xFFFF);
if (ret != HAL_OK)
{
return RF433_HAL_ERROR_TIMEOUT;
}
return RF433_HAL_OK;
}
/**
* @brief UART接收回调由中断调用
*/
void rf433_hal_uart_rx_callback(uint8_t *data, uint16_t length)
{
/* 写入FIFO */
rf433_fifo_write(data, length);
/* 设置超时 */
rf433_uart_rx_timeout = 10;
}
/**
* @brief 等待AUX引脚变为空闲基于原e32_hal_aux_wait
*/
rf433_hal_error_t rf433_hal_aux_wait(void)
{
#if RF433_USE_GPIO_AUX
uint32_t start_time;
/* 等待AUX引脚变为高电平 */
if (HAL_GPIO_ReadPin(AUX_GPIO_Port, AUX_Pin) == GPIO_PIN_RESET)
{
/* 等到模块的AUX信号由忙变为空闲 */
start_time = HAL_GetTick();
while (HAL_GPIO_ReadPin(AUX_GPIO_Port, AUX_Pin) == GPIO_PIN_RESET)
{
/* 检查超时 */
if ((HAL_GetTick() - start_time) >= RF433_AUX_TIMEOUT)
{
return RF433_HAL_ERROR_TIMEOUT;
}
}
/* 此时是检测到AUX信号有上升沿需要再等待1~2ms */
HAL_Delay(2);
}
#else
/* 不使用AUX引脚使用延时等待 */
HAL_Delay(30);
#endif
return RF433_HAL_OK;
}
/**
* @brief 设置工作模式基于原e32_hal_work_mode
*/
rf433_hal_error_t rf433_hal_set_work_mode(rf433_work_mode_t mode)
{
rf433_hal_error_t ret;
#if RF433_USE_GPIO_AUX
/* 等待模块空闲 */
ret = rf433_hal_aux_wait();
if (ret != RF433_HAL_OK)
{
return ret;
}
#endif
/* 设置M0和M1引脚 */
switch (mode)
{
/* 模式0一般模式 (M0=0 M1=0) */
case RF433_WORK_MODE_TRANSPARENT:
HAL_GPIO_WritePin(M0_GPIO_Port, M0_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(M1_GPIO_Port, M1_Pin, GPIO_PIN_RESET);
break;
/* 模式1唤醒模式 (M0=1 M1=0) */
case RF433_WORK_MODE_WAKE_ON_RADIO_MASTER:
HAL_GPIO_WritePin(M0_GPIO_Port, M0_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(M1_GPIO_Port, M1_Pin, GPIO_PIN_RESET);
break;
/* 模式2省电模式 (M0=0 M1=1) */
case RF433_WORK_MODE_WAKE_ON_RADIO_SLAVE:
HAL_GPIO_WritePin(M0_GPIO_Port, M0_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(M1_GPIO_Port, M1_Pin, GPIO_PIN_SET);
break;
/* 模式3配置模式 (M0=1 M1=1) */
case RF433_WORK_MODE_CONFIG_AND_SLEEP:
HAL_GPIO_WritePin(M0_GPIO_Port, M0_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(M1_GPIO_Port, M1_Pin, GPIO_PIN_SET);
break;
default:
return RF433_HAL_ERROR_INVALID_PARAM;
}
#if RF433_USE_GPIO_AUX
/* 切换模式后模块的AUX信号不会立即变为低电平需要等待一段时间再进行检测 */
HAL_Delay(5);
ret = rf433_hal_aux_wait();
if (ret != RF433_HAL_OK)
{
return ret;
}
#else
/* 这里保留一定延时,确保模式切换完成 */
HAL_Delay(50);
#endif
return RF433_HAL_OK;
}
/**
* @brief 复位模块基于原e32_hal_reset
*/
rf433_hal_error_t rf433_hal_reset(void)
{
/* 拉低复位引脚 */
HAL_GPIO_WritePin(RESET_GPIO_Port, RESET_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
/* 拉高复位引脚 */
HAL_GPIO_WritePin(RESET_GPIO_Port, RESET_Pin, GPIO_PIN_SET);
#if RF433_USE_GPIO_AUX
/* 注意硬件复位时可能会出现AUX引脚输出低电平需要等待一段时间再开启AUX检测 */
HAL_Delay(10);
return rf433_hal_aux_wait();
#else
/* E32-V2 (V8.2) 版本 */
HAL_Delay(30);
return RF433_HAL_OK;
#endif
}
/**
* @brief 写入FIFO
*/
rf433_hal_error_t rf433_hal_fifo_write(const uint8_t *data, uint16_t length)
{
return rf433_fifo_write(data, length);
}
/**
* @brief 读取FIFO
*/
rf433_hal_error_t rf433_hal_fifo_read(uint8_t *data, uint16_t length, uint16_t *actual_length)
{
return rf433_fifo_read(data, length, actual_length);
}
/**
* @brief 获取FIFO数据长度
*/
rf433_hal_error_t rf433_hal_fifo_get_length(uint16_t *length)
{
return rf433_fifo_get_length(length);
}
/**
* @brief 清空FIFO
*/
rf433_hal_error_t rf433_hal_fifo_clear(void)
{
return rf433_fifo_clear();
}
/**
* @brief 1ms定时器回调用于超时检测
*/
void rf433_hal_1ms_callback(void)
{
/* UART接收超时检测 */
if (rf433_uart_rx_timeout > 0)
{
rf433_uart_rx_timeout--;
if (rf433_uart_rx_timeout == 0)
{
rf433_uart_rx_done = true;
}
}
}
/* ============================================================================
* UART中断回调需要在stm32f1xx_it.c中调用
* ============================================================================ */
/**
* @brief UART接收完成回调
* @note 此函数需要在HAL_UART_RxCpltCallback中调用
*/
void rf433_hal_uart_rxcplt_callback(void)
{
uint8_t data;
/* 读取接收到的数据 */
data = huart1.Instance->DR;
/* 写入FIFO */
rf433_fifo_write(&data, 1);
/* 设置超时 */
rf433_uart_rx_timeout = 10;
/* 重新启动接收 */
HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
}
/* ============================================================================
* 兼容函数(用于保持与原代码的兼容性)
* ============================================================================ */
/**
* @brief UART接收超时1ms回调兼容原uart1_rx_timeout_1ms_callback
* @note 此函数需要在1ms定时器中断中调用
*/
void uart1_rx_timeout_1ms_callback(void)
{
/* 调用RF433模块的1ms回调 */
rf433_hal_1ms_callback();
}
/**
* @brief UART等待响应阻塞模式兼容原uart1_wait_response_blocked
* @param buffer 数据缓冲区
* @param length 数据长度输出
* @note 与main.h中的声明保持一致返回void
*/
void uart1_wait_response_blocked(uint8_t *buffer, uint16_t *length)
{
uint32_t start_time;
uint16_t fifo_length;
rf433_hal_error_t ret;
/* 参数检查 */
if (buffer == NULL || length == NULL)
{
return;
}
/* 等待数据 */
start_time = HAL_GetTick();
while (true)
{
ret = rf433_hal_fifo_get_length(&fifo_length);
if (ret != RF433_HAL_OK)
{
return;
}
if (fifo_length > 0)
{
break;
}
/* 检查超时 */
if ((HAL_GetTick() - start_time) >= 1000)
{
return;
}
HAL_Delay(1);
}
/* 读取数据 */
ret = rf433_hal_fifo_read(buffer, 255, length);
if (ret != RF433_HAL_OK)
{
return;
}
}
/**
* @brief UART检查接收完成兼容原uart1_check_rx_done
* @param buffer 数据缓冲区
* @param length 数据长度输出uint32_t*与main.h中的声明保持一致
* @return true 接收到数据
* false 无数据
*/
bool uart1_check_rx_done(uint8_t *buffer, uint32_t *length)
{
rf433_hal_error_t ret;
uint16_t fifo_length;
/* 参数检查 */
if (buffer == NULL || length == NULL)
{
return false;
}
/* 检查超时标志 */
if (!rf433_uart_rx_done)
{
return false;
}
/* 获取FIFO长度 */
ret = rf433_hal_fifo_get_length(&fifo_length);
if (ret != RF433_HAL_OK)
{
return false;
}
/* 检查是否有数据 */
if (fifo_length == 0)
{
return false;
}
/* 读取数据 */
ret = rf433_hal_fifo_read(buffer, 255, (uint16_t*)length);
if (ret != RF433_HAL_OK)
{
return false;
}
/* 清除超时标志 */
rf433_uart_rx_done = false;
return true;
}
/**
* @brief 检查接收完成标志用于RF433应用层
* @return true 接收完成
* false 接收未完成
*/
bool rf433_hal_check_rx_done(void)
{
return rf433_uart_rx_done;
}