"完成了433多数据同时接收的时候的冲突问题,解决了网络未连接会卡死整个程序的问题“
This commit is contained in:
@ -125,10 +125,10 @@ void RF433_SendPacket(uint8_t type, const uint8_t *payload, uint8_t len)
|
||||
uint16_t frame_idx = 0;
|
||||
uint8_t checksum = 0;
|
||||
|
||||
frame[frame_idx++] = PROTO_START_BYTE; // AA
|
||||
frame[frame_idx++] = type; // TYPE
|
||||
frame[frame_idx++] = (uint8_t)(len + 1); // LEN (ID + Payload)
|
||||
frame[frame_idx++] = MY_DEVICE_ID; // ID
|
||||
frame[frame_idx++] = PROTO_START_BYTE; // AA
|
||||
frame[frame_idx++] = type; // TYPE
|
||||
frame[frame_idx++] = (uint8_t)(len + 1 + 1); // LEN (ID + Payload + SUM)
|
||||
frame[frame_idx++] = MY_DEVICE_ID; // ID
|
||||
|
||||
if (len > 0 && payload != NULL) {
|
||||
memcpy(&frame[frame_idx], payload, len);
|
||||
|
||||
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user