feat: add STM8 mesh relay firmware
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* @file uart1.c
|
||||
* @brief UART1 第一阶段占位实现。
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include "uart1.h"
|
||||
#include "stm8s003_registers.h"
|
||||
|
||||
/** UART1 接收环形缓冲区容量,必须使用 2 的幂便于快速回绕。 */
|
||||
#define UART1_RX_BUFFER_SIZE 64u
|
||||
|
||||
/** UART1 接收环形缓存,保存尚未交给协议层处理的原始字节。 */
|
||||
static volatile uint8_t rx_buffer[UART1_RX_BUFFER_SIZE];
|
||||
/** 环形缓存写入位置,由 UART 接收任务推进。 */
|
||||
static volatile uint8_t rx_write_index;
|
||||
/** 环形缓存读取位置,由上层协议任务推进。 */
|
||||
static volatile uint8_t rx_read_index;
|
||||
|
||||
/** 初始化 UART1;后续接入 JDY-28M 的 9600 baud 配置。 */
|
||||
void Uart1_Init(void)
|
||||
{
|
||||
/* 先关闭 UART,再配置 9600 baud@16 MHz:BRR=0x0683。 */
|
||||
REG8(UART1_CR2_ADDR) = 0x00u;
|
||||
REG8(UART1_BRR2_ADDR) = 0x03u;
|
||||
REG8(UART1_BRR1_ADDR) = 0x68u;
|
||||
REG8(UART1_CR1_ADDR) = 0x00u;
|
||||
REG8(UART1_CR3_ADDR) = 0x00u;
|
||||
/* RE=1、TE=1、接收中断暂不打开,环形缓冲区接入后再开启。 */
|
||||
REG8(UART1_CR2_ADDR) = 0x0Cu;
|
||||
rx_write_index = 0u;
|
||||
rx_read_index = 0u;
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询 UART1 接收状态并保存字节。
|
||||
*
|
||||
* 本阶段先采用轮询方式,避免在尚未建立中断向量表前引入中断链接问题。
|
||||
* 后续如果业务帧速率提高,可将同一环形缓冲区写入逻辑迁移到 UART RX 中断。
|
||||
*/
|
||||
void Uart1_Task(void)
|
||||
{
|
||||
uint8_t next_index;
|
||||
|
||||
while ((REG8(UART1_SR_ADDR) & 0x20u) != 0u)
|
||||
{
|
||||
next_index = (uint8_t)((rx_write_index + 1u) & (UART1_RX_BUFFER_SIZE - 1u));
|
||||
|
||||
/* 缓冲区满时丢弃当前字节,避免覆盖尚未处理的完整帧。 */
|
||||
if (next_index == rx_read_index)
|
||||
{
|
||||
(void)REG8(UART1_DR_ADDR);
|
||||
continue;
|
||||
}
|
||||
|
||||
rx_buffer[rx_write_index] = REG8(UART1_DR_ADDR);
|
||||
rx_write_index = next_index;
|
||||
}
|
||||
}
|
||||
|
||||
/** 发送一个字节;当前阶段不连接真实 UART 寄存器。 */
|
||||
void Uart1_WriteByte(uint8_t value)
|
||||
{
|
||||
while ((REG8(UART1_SR_ADDR) & 0x80u) == 0u)
|
||||
{
|
||||
}
|
||||
REG8(UART1_DR_ADDR) = value;
|
||||
}
|
||||
|
||||
/** 从接收环形缓冲区读取一个字节;当前阶段始终表示无数据。 */
|
||||
uint8_t Uart1_ReadByte(uint8_t *value)
|
||||
{
|
||||
if ((value == (uint8_t *)0) || (rx_read_index == rx_write_index))
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
|
||||
*value = rx_buffer[rx_read_index];
|
||||
rx_read_index = (uint8_t)((rx_read_index + 1u) & (UART1_RX_BUFFER_SIZE - 1u));
|
||||
return 1u;
|
||||
}
|
||||
|
||||
/** 返回接收缓冲区当前字节数,供协议层判断是否需要继续读取。 */
|
||||
uint8_t Uart1_Available(void)
|
||||
{
|
||||
return (uint8_t)((rx_write_index - rx_read_index) & (UART1_RX_BUFFER_SIZE - 1u));
|
||||
}
|
||||
|
||||
/** UART1 接收中断入口占位。 */
|
||||
void Uart1_InterruptHandler(void)
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user