"完成了433多数据同时接收的时候的冲突问题,解决了网络未连接会卡死整个程序的问题“

This commit is contained in:
2026-05-08 18:49:05 +08:00
parent 6c56fe8a60
commit 6e2b13dbb3
6 changed files with 71 additions and 43 deletions

View File

@ -42,6 +42,16 @@
#define DEBUG_LOG(fmt, ...)
#endif
/*==============================================================================
* CSMA/CA 随机退避相关
*============================================================================*/
static uint32_t csma_rand(uint32_t seed)
{
// static uint32_t seed = 12345;
seed = seed * 1103515245 + 12345;
return (seed >> 16) & 0x7FFF;
}
/*==============================================================================
* 全局变量定义
*============================================================================*/
@ -235,16 +245,23 @@ static void tx_kickoff(port_id_t port_id)
__disable_irq();
if (!ring->is_sending && ring->count > 0) {
/* --- 核心修复:针对 RF433 (UART1) 执行包级 LBT 检查 --- */
if (port_id == PORT_433) {
// 如果 AUX 为低电平,说明信道忙(接收中或正在发送前一包),暂缓起步
if (ctx->csma_backoff_active) {
if ((int32_t)(HAL_GetTick() - ctx->csma_backoff_until) < 0) {
__enable_irq();
return;
}
ctx->csma_backoff_active = false;
}
if (HAL_GPIO_ReadPin(AUX_GPIO_Port, AUX_Pin) == GPIO_PIN_RESET) {
ctx->csma_backoff_active = true;
ctx->csma_backoff_until = HAL_GetTick() + (csma_rand(HAL_GetTick()) % 1000) + 1;
__enable_irq();
return;
}
}
/* 取出下一个待发送字节 */
byte = ring->buffer[ring->tail];
ring->tail = (ring->tail + 1) % UART_TX_BUFFER_SIZE;
ring->count--;
@ -297,6 +314,10 @@ void MultiUART_Init(void)
ctx->tx_count = 0;
ctx->error_count = 0;
ctx->initialized = true;
/* 初始化 CSMA/CA 随机退避状态 */
ctx->csma_backoff_active = false;
ctx->csma_backoff_until = 0;
}
DEBUG_LOG("Init OK, %d ports configured", PORT_COUNT);