2024-12-04 10:31:57 +08:00
|
|
|
#include "bsp_bt.h"
|
2024-12-04 18:55:59 +08:00
|
|
|
#include "bsp_ml307.h"
|
|
|
|
#include "bsp_flash.h"
|
|
|
|
#include "rtdef.h"
|
2024-12-04 10:31:57 +08:00
|
|
|
|
|
|
|
|
2024-12-06 16:16:12 +08:00
|
|
|
#define LOG_TAG "bsp_bt"
|
|
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
|
|
#include <ulog.h>
|
|
|
|
|
|
|
|
#define BT_UART "uart5"
|
|
|
|
|
|
|
|
rt_timer_t bt_timer;
|
|
|
|
struct rt_semaphore bt_rx_sem;
|
|
|
|
static rt_device_t rt_bt_device;
|
|
|
|
|
|
|
|
#define BT_THREAD_TIMESLICE (5)
|
|
|
|
#define BT_THREAD_PRIORITY (10)
|
|
|
|
#define BT_THREAD_STACK_SIZE (2048)
|
|
|
|
|
|
|
|
|
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
|
|
|
static rt_uint8_t bt_thread_stack[BT_THREAD_STACK_SIZE] = {0};
|
|
|
|
static struct rt_thread bt_thread = {0};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
rt_size_t recv_bufsz;
|
|
|
|
char recv_line_buf[128];
|
|
|
|
rt_size_t recv_line_len;
|
|
|
|
} bt_device;
|
|
|
|
|
|
|
|
bt_device bt = {
|
|
|
|
.recv_bufsz = 64,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-04 18:55:59 +08:00
|
|
|
valve_data_t valve_t[MAX_VALVE_NUM];
|
2024-12-04 10:31:57 +08:00
|
|
|
|
2024-12-04 18:55:59 +08:00
|
|
|
int BSP_BT_Init(void)
|
|
|
|
{
|
|
|
|
rt_uint8_t num = Flash_Get_Valve_Num();
|
|
|
|
rt_uint8_t mac_buf[FLASH_VALVE_MAC_ADDR_LEN] = {0};
|
|
|
|
if (num != 0)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < MAX_VALVE_NUM; i++)
|
|
|
|
{
|
2024-12-05 16:57:19 +08:00
|
|
|
valve_t[i].valve_id = (i + 1);//1-8
|
2024-12-04 18:55:59 +08:00
|
|
|
if (Flash_Get_Mac_Addr(valve_t[i].valve_mac, i) == RT_EOK)
|
|
|
|
{
|
|
|
|
rt_memcpy(valve_t[i].valve_mac, mac_buf, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < MAX_VALVE_NUM; i++)
|
|
|
|
{
|
|
|
|
rt_memset(&valve_t[i], 0, sizeof(valve_data_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
INIT_PREV_EXPORT(BSP_BT_Init);
|
2024-12-06 16:16:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static rt_err_t bt_getchar(char *ch, rt_int32_t timeout)
|
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
|
|
|
while (rt_device_read(rt_bt_device, 0, ch, 1) == 0)
|
|
|
|
{
|
|
|
|
result = rt_sem_take(&bt_rx_sem, rt_tick_from_millisecond(timeout));
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bt_recv_readline(bt_device *dev)
|
|
|
|
{
|
|
|
|
rt_size_t read_len = 0;
|
|
|
|
char ch = 0, last_ch = 0;
|
|
|
|
rt_bool_t is_full = RT_FALSE;
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
char bt_data[128] = {0};
|
|
|
|
rt_memset(dev->recv_line_buf, 0x00, dev->recv_bufsz);
|
|
|
|
dev->recv_line_len = 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
result = bt_getchar(&ch, RT_WAITING_FOREVER);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_D("get sem bt_rx_sem error");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (read_len < dev->recv_bufsz)
|
|
|
|
{
|
|
|
|
bt_data[read_len++] = ch;
|
|
|
|
dev->recv_line_len = read_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_full = RT_TRUE;
|
|
|
|
}
|
|
|
|
if (ch == '\n' && last_ch == '\r')
|
|
|
|
{
|
|
|
|
bt_data[dev->recv_line_len - 1] = '\0';
|
|
|
|
rt_memcpy(dev->recv_line_buf, bt_data, read_len);
|
|
|
|
rt_memset(bt_data, 0, dev->recv_line_len);
|
|
|
|
if (is_full)
|
|
|
|
{
|
|
|
|
LOG_E("read line failed. The line data length is out of buffer size(%d)!", dev->recv_bufsz);
|
|
|
|
rt_memset(dev->recv_line_buf, 0x00, dev->recv_bufsz);
|
|
|
|
dev->recv_line_len = 0;
|
|
|
|
return -RT_EFULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last_ch = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return read_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Bt_Thread_Entry(void *parameter)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (bt_recv_readline(&bt) > 0)
|
|
|
|
{
|
|
|
|
LOG_D("bt recv data: %s", bt.recv_line_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 接收数据回调函数 */
|
|
|
|
static rt_err_t Bt_Rcv_Cb(rt_device_t dev, rt_size_t size)
|
|
|
|
{
|
|
|
|
rt_sem_release(&bt_rx_sem);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BSP_Bt_Init(void)
|
|
|
|
{
|
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
|
|
|
|
|
/* 查找系统中的串口设备 */
|
|
|
|
rt_bt_device = rt_device_find(BT_UART);
|
|
|
|
if (!rt_bt_device)
|
|
|
|
{
|
|
|
|
LOG_E("find %s failed!\n", BT_UART);
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
/* 以中断接收模式打开串口设备 */
|
|
|
|
if (rt_device_open(rt_bt_device, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("rt_device_open failed!\n");
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
rt_sem_init(&bt_rx_sem, "bt_rx_sem", 0, RT_IPC_FLAG_PRIO); /* 初始化信号量 */
|
|
|
|
/* 设置接收回调函数 */
|
|
|
|
if (rt_device_set_rx_indicate(rt_bt_device, Bt_Rcv_Cb) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("rt_device_set_rx_indicate failed!\n");
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
/* 静态初始化线程 1*/
|
|
|
|
ret = rt_thread_init(&bt_thread, // 该线程用于数据解析
|
|
|
|
"bt_thread",
|
|
|
|
Bt_Thread_Entry,
|
|
|
|
RT_NULL,
|
|
|
|
&bt_thread_stack[0],
|
|
|
|
sizeof(bt_thread_stack),
|
|
|
|
BT_THREAD_PRIORITY,
|
|
|
|
BT_THREAD_TIMESLICE);
|
|
|
|
/* 创建成功则启动线程 */
|
|
|
|
rt_thread_startup(&bt_thread);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(BSP_Bt_Init);
|