generated from Template/H563ZI-HAL-CMake-Template
tx task success,rx dont
This commit is contained in:
149
fun/HCBle.c
149
fun/HCBle.c
@@ -1,6 +1,8 @@
|
||||
#include "HCBle.h"
|
||||
|
||||
/**
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
<EFBFBD><EFBFBD>ʾ: <20><>Ȼ BLE <20>Լ<EFBFBD> GPS<50><53><EFBFBD><EFBFBD>DMA+ UART<52><54><EFBFBD>գ<EFBFBD><D5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ
|
||||
BLE --- DMA + IDLE <20>ж<EFBFBD> ѭ<><D1AD> DMA + <20><><EFBFBD><EFBFBD><EFBFBD>жϴ<D0B6><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UART IDLE
|
||||
GPS --- DMA +<2B>̶<EFBFBD><CCB6><EFBFBD><EFBFBD><EFBFBD> + TC<54>ж<EFBFBD> <20><><EFBFBD>ù̶<C3B9><CCB6><EFBFBD><EFBFBD><EFBFBD> DMA DMA<4D><41><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
||||
@@ -34,11 +36,17 @@ MotorCommand cmd;
|
||||
LocationData current_location = {0};
|
||||
float imu_angle = 0.0f;
|
||||
|
||||
TX_EVENT_FLAGS_GROUP ble_event_flags;
|
||||
#define BLE_EVENT_DATA_READY 0x01
|
||||
void HCBle_InitEventFlags(void)
|
||||
{ tx_event_flags_create(&ble_event_flags, "BLE Events");}
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC>DMA<4D><41><EFBFBD>պ<EFBFBD><D5BA><EFBFBD>
|
||||
void HCBle_InitDMAReception(void)
|
||||
{
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
|
||||
HAL_UARTEx_ReceiveToIdle_IT(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
|
||||
// HAL_UART_Receive_DMA(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
|
||||
// HAL_UARTEx_ReceiveToIdle_DMA(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
|
||||
// ֻ<><D6BB>Ҫֱ<D2AA>Ӵ<EFBFBD><D3B4><EFBFBD> <20><><EFBFBD>ڿ<EFBFBD><DABF>н<EFBFBD><D0BD><EFBFBD>
|
||||
// HAL_UARTEx_RxEventCallback --- <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>
|
||||
}
|
||||
@@ -67,86 +75,90 @@ void HCBle_SendData(char *p,...)
|
||||
// <20><><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>һ<EFBFBD><D2BB>DMA
|
||||
|
||||
|
||||
|
||||
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
||||
{
|
||||
if (huart->Instance == UART4)
|
||||
{
|
||||
|
||||
uint16_t data_len = UART_DMA_RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart->hdmarx);
|
||||
for (uint16_t i = 0; i < data_len; i++)
|
||||
for (uint16_t i = 0; i < Size; i++)
|
||||
{
|
||||
ble_rx_ring.buffer[ble_rx_ring.head] = uart_dma_rx_buf[i];
|
||||
ble_rx_ring.head = (ble_rx_ring.head + 1) % RING_BUFFER_SIZE;
|
||||
uint8_t ch = uart_dma_rx_buf[i];
|
||||
uint16_t next_head = (ble_rx_ring.head + 1) % RING_BUFFER_SIZE;
|
||||
|
||||
if (next_head != ble_rx_ring.tail)
|
||||
{
|
||||
ble_rx_ring.buffer[ble_rx_ring.head] = ch;
|
||||
ble_rx_ring.head = next_head;
|
||||
}
|
||||
}
|
||||
// memset(uart_dma_rx_buf, '\0', data_len);
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> uart_rx_buf[0..Size-1] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>գ<EFBFBD>HAL<41><4C><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ˣ<EFBFBD>
|
||||
|
||||
tx_event_flags_set(&ble_event_flags, BLE_EVENT_DATA_READY, TX_OR);
|
||||
HAL_UARTEx_ReceiveToIdle_IT(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD> Json<6F><6E><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD> <20>Լ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>ֻ<EFBFBD>App
|
||||
|
||||
|
||||
// Json<6F><6E><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD> <20><><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>Thread x <20><><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
|
||||
void HCBle_ExtractAndParseFrame(void)
|
||||
{
|
||||
static char json_buf[128];
|
||||
static int idx = 0;
|
||||
static int in_frame = 0;
|
||||
void HCBle_ExtractAndParseFrame(void) {
|
||||
static char json_buf[128];
|
||||
static int idx = 0;
|
||||
static int parsing = 0;
|
||||
|
||||
while (ble_rx_ring.tail != ble_rx_ring.head)
|
||||
{
|
||||
char c = ble_rx_ring.buffer[ble_rx_ring.tail];
|
||||
ble_rx_ring.tail = (ble_rx_ring.tail + 1) % RING_BUFFER_SIZE;
|
||||
while (ble_rx_ring.tail != ble_rx_ring.head) {
|
||||
char c = ble_rx_ring.buffer[ble_rx_ring.tail];
|
||||
ble_rx_ring.tail = (ble_rx_ring.tail + 1) % RING_BUFFER_SIZE;
|
||||
|
||||
// ֡<><D6A1>ʼ<EFBFBD><CABC>#
|
||||
if (c == '#') {
|
||||
idx = 0;
|
||||
in_frame = 1;
|
||||
json_buf[idx++] = c;
|
||||
}
|
||||
else if (in_frame) {
|
||||
if (idx < sizeof(json_buf) - 1) {
|
||||
json_buf[idx++] = c;
|
||||
if (!parsing) {
|
||||
if (c == '#') {
|
||||
idx = 0;
|
||||
parsing = 1;
|
||||
memset(json_buf, 0, sizeof(json_buf));
|
||||
json_buf[idx++] = c;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// ֡<><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n
|
||||
if (c == '\n') {
|
||||
json_buf[idx] = '\0';
|
||||
|
||||
// ? JSON<4F><4E><EFBFBD><EFBFBD>
|
||||
if (strstr(json_buf, "leftSpeed") && strstr(json_buf, "rightSpeed")) {
|
||||
sscanf(json_buf, "#{\"leftSpeed\":%d,\"rightSpeed\":%d}", &cmd.LeftSpeed, &cmd.RightSpeed);
|
||||
tx_event_flags_set(&system_events,EVENT_BLE_COMMAND_RECEIVED,TX_OR);
|
||||
//SetMotorSpeed(cmd.LeftSpeed, cmd.RightSpeed); <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
HCBle_SendData("<EFBFBD><EFBFBD>=%d, <20><>=%d\r\n", cmd.LeftSpeed, cmd.RightSpeed);
|
||||
}
|
||||
|
||||
in_frame = 0;
|
||||
idx = 0;
|
||||
}
|
||||
} else {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
in_frame = 0;
|
||||
idx = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (idx < sizeof(json_buf) - 1) {
|
||||
json_buf[idx++] = c;
|
||||
if (c == '\n') {
|
||||
json_buf[idx] = '\0';
|
||||
int left, right;
|
||||
if (sscanf(json_buf, "#{\"leftSpeed\":%d,\"rightSpeed\":%d}\n", &left, &right) == 2) {
|
||||
HCBle_SendData("? <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: <20><>=%d, <20><>=%d\r\n", left, right);
|
||||
cmd.LeftSpeed = left;
|
||||
cmd.RightSpeed = right;
|
||||
} else {
|
||||
HCBle_SendData("? JSON<4F><4E>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>: %s\r\n", json_buf);
|
||||
}
|
||||
parsing = 0;
|
||||
idx = 0;
|
||||
}
|
||||
} else {
|
||||
parsing = 0;
|
||||
idx = 0;
|
||||
memset(json_buf, 0, sizeof(json_buf));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// BLE<4C><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void ble_rx_task_entry(ULONG thread_input)
|
||||
{
|
||||
HCBle_InitDMAReception();
|
||||
HCBle_SendData("BLE RX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\r\n");
|
||||
while(1)
|
||||
|
||||
ULONG actual_flags;
|
||||
|
||||
while (1)
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
HCBle_ExtractAndParseFrame(); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>JSON<4F><4E><EFBFBD><EFBFBD>
|
||||
tx_thread_sleep(10); //ÿ10ms<6D><73>ѯ1<D1AF><31>
|
||||
}
|
||||
tx_event_flags_get(&ble_event_flags, BLE_EVENT_DATA_READY, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER);
|
||||
|
||||
if (actual_flags & BLE_EVENT_DATA_READY)
|
||||
{
|
||||
HCBle_ExtractAndParseFrame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -155,25 +167,10 @@ void ble_rx_task_entry(ULONG thread_input)
|
||||
// current_location.lat, current_location.lon, current_location.angle);
|
||||
void ble_tx_task_entry(ULONG thread_input) {
|
||||
BLE_Message msg;
|
||||
HCBle_SendData("BLE TX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\r\n");
|
||||
while(1) {
|
||||
HCBle_SendData("fuck");
|
||||
if (tx_queue_receive(&ble_tx_queue, &msg, TX_WAIT_FOREVER) == TX_SUCCESS) {
|
||||
switch (msg.msg_type)
|
||||
{
|
||||
case 1: // <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>λ<EFBFBD>ϱ<EFBFBD>
|
||||
// HCBle_SendData("fuck");
|
||||
HCBle_SendData("#{\"lat\":%.6f,\"lon\":%.6f,\"angle\":%.2f}\n",23.123456, 113.654321, 95.0);
|
||||
break;
|
||||
|
||||
case 2: // ACK <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
|
||||
HCBle_SendData("%s", msg.data);
|
||||
break;
|
||||
|
||||
default:
|
||||
HCBle_SendData("δ֪<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
tx_thread_sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user