BLE transmit and receive success

This commit is contained in:
2025-06-26 01:41:18 +08:00
parent 331d7dedb7
commit 59f65128b0
29 changed files with 8421 additions and 7601 deletions

View File

@@ -57,7 +57,7 @@ void GPDMA1_Channel4_IRQHandler(void);
void GPDMA1_Channel5_IRQHandler(void);
void TIM2_IRQHandler(void);
void TIM3_IRQHandler(void);
void TIM5_IRQHandler(void);
void TIM6_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
void UART4_IRQHandler(void);

View File

@@ -115,7 +115,7 @@
/*#define TX_TIMER_THREAD_STACK_SIZE 1024*/
/*#define TX_TIMER_THREAD_PRIORITY 0*/
/*#define TX_MINIMUM_STACK 200*/
#define TX_MINIMUM_STACK 400
/* Determine if timer expirations (application timers, timeouts, and tx_thread_sleep) calls
should be processed within the a system timer thread or directly in the timer ISR.

View File

@@ -33,10 +33,10 @@
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define BLE_RX_THREAD_STACK_SIZE 1024
#define BLE_RX_THREAD_STACK_SIZE 2048
#define BLE_RX_THREAD_PRIORITY 10
#define BLE_TX_THREAD_STACK_SIZE 1024
#define BLE_TX_THREAD_STACK_SIZE 2048
#define BLE_TX_THREAD_PRIORITY 10
/* USER CODE END PD */
@@ -58,8 +58,7 @@ UCHAR ble_rx_stack[BLE_RX_THREAD_STACK_SIZE];
TX_THREAD ble_tx_thread;
UCHAR ble_tx_stack[BLE_TX_THREAD_STACK_SIZE];
#define BLE_TX_QUEUE_DEPTH 4
UCHAR ble_tx_queue_buffer[BLE_TX_QUEUE_DEPTH * sizeof(BLE_Message)];
@@ -82,42 +81,13 @@ UCHAR ble_tx_queue_buffer[BLE_TX_QUEUE_DEPTH * sizeof(BLE_Message)];
*/
UINT App_ThreadX_Init(VOID *memory_ptr)
{
// UINT ret = TX_SUCCESS;
/* USER CODE BEGIN App_ThreadX_MEM_POOL */
UINT status;
//初始化BLE接收任务
// 初始化 BLE 接收任务
status = tx_thread_create(&ble_rx_thread, "BLE RX Thread",
ble_rx_task_entry, 0,
ble_rx_stack, BLE_RX_THREAD_STACK_SIZE,
BLE_RX_THREAD_PRIORITY, BLE_RX_THREAD_PRIORITY,
TX_NO_TIME_SLICE, TX_AUTO_START);
if (status != TX_SUCCESS) {
return status;
}
// 创建 BLE TX 线程
status = tx_thread_create(&ble_tx_thread, "BLE TX Thread",
ble_tx_task_entry, 0,
ble_tx_stack, BLE_TX_THREAD_STACK_SIZE,
BLE_TX_THREAD_PRIORITY, BLE_TX_THREAD_PRIORITY,
TX_NO_TIME_SLICE, TX_AUTO_START);
if (status != TX_SUCCESS) return status;
// ✅ 创建 BLE TX 消息队列
status = tx_queue_create(&ble_tx_queue, "BLE TX Queue",
sizeof(BLE_Message)/sizeof(ULONG),
ble_tx_queue_buffer,
sizeof(ble_tx_queue_buffer));
if (status != TX_SUCCESS) return status;
/* USER CODE END App_ThreadX_MEM_POOL */
/* USER CODE BEGIN App_ThreadX_Init */
/* USER CODE END App_ThreadX_Init */
HCBle_SendData("进入 App_ThreadX_Init\r\n");
return TX_SUCCESS;
return TX_SUCCESS;
}
/**
* @brief Function that implements the kernel's initialization.
* @param None

View File

@@ -34,6 +34,7 @@
/** Configure pins
PH0-OSC_IN(PH0) ------> RCC_OSC_IN
PH1-OSC_OUT(PH1) ------> RCC_OSC_OUT
PA13(JTMS/SWDIO) ------> DEBUG_JTMS-SWDIO
PA14(JTCK/SWCLK) ------> DEBUG_JTCK-SWCLK
*/

View File

@@ -100,15 +100,13 @@ int main(void)
MX_UART4_Init();
MX_TIM1_Init();
MX_TIM8_Init();
// MX_ThreadX_Init();
/* USER CODE BEGIN 2 */
HCBle_InitDMAReception();
HAL_Delay(200);
// HAL_Delay(200);
/* USER CODE END 2 */
// MX_ThreadX_Init();
MX_ThreadX_Init();
/* We should never get here as control is now taken by the scheduler */
@@ -117,16 +115,7 @@ int main(void)
while (1)
{
/* USER CODE END WHILE */
if(flag == 1)
{
HCBle_ExtractAndParseFrame();
flag = 0;
}
// Ŀǰ BLE<4C>Ľ<EFBFBD><C4BD>ջ<EFBFBD>û<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>Ƿ<EFBFBD><C7B7>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
HCBle_SendData("#{\"lat\":%.6f,\"lon\":%.6f,\"angle\":%.2f}\n",
23.123456, 113.654321, 95.0);
HAL_Delay(1000);
// HCBle_SendData("#{\"lat\":%.6f,\"lon\":%.6f,\"angle\":%.2f}\n",23.123456, 113.654321, 95.0);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
@@ -195,7 +184,7 @@ void SystemClock_Config(void)
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM5 interrupt took place, inside
* @note This function is called when TIM6 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
@@ -206,7 +195,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM5)
if (htim->Instance == TIM6)
{
HAL_IncTick();
}

View File

@@ -25,12 +25,12 @@
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim5;
TIM_HandleTypeDef htim6;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief This function configures the TIM5 as a time base source.
* @brief This function configures the TIM6 as a time base source.
* The time source is configured to have 1ms time base with a dedicated
* Tick interrupt priority.
* @note This function is called automatically at the beginning of program after
@@ -47,15 +47,15 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
HAL_StatusTypeDef status;
/* Enable TIM5 clock */
__HAL_RCC_TIM5_CLK_ENABLE();
/* Enable TIM6 clock */
__HAL_RCC_TIM6_CLK_ENABLE();
/* Get clock configuration */
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
/* Get APB1 prescaler */
uwAPB1Prescaler = clkconfig.APB1CLKDivider;
/* Compute TIM5 clock */
/* Compute TIM6 clock */
if (uwAPB1Prescaler == RCC_HCLK_DIV1)
{
uwTimclock = HAL_RCC_GetPCLK1Freq();
@@ -65,34 +65,34 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
}
/* Compute the prescaler value to have TIM5 counter clock equal to 100KHz */
/* Compute the prescaler value to have TIM6 counter clock equal to 100KHz */
uwPrescalerValue = (uint32_t) ((uwTimclock / 100000U) - 1U);
/* Initialize TIM5 */
htim5.Instance = TIM5;
/* Initialize TIM6 */
htim6.Instance = TIM6;
/* Initialize TIMx peripheral as follow:
* Period = [(TIM5CLK/1000) - 1]. to have a (1/1000) s time base.
* Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
* Prescaler = (uwTimclock/100000 - 1) to have a 100KHz counter clock.
* ClockDivision = 0
* Counter direction = Up
*/
htim5.Init.Period = (100000U / 1000U) - 1U;
htim5.Init.Prescaler = uwPrescalerValue;
htim5.Init.ClockDivision = 0;
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = (100000U / 1000U) - 1U;
htim6.Init.Prescaler = uwPrescalerValue;
htim6.Init.ClockDivision = 0;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
status = HAL_TIM_Base_Init(&htim5);
status = HAL_TIM_Base_Init(&htim6);
if (status == HAL_OK)
{
/* Start the TIM time Base generation in interrupt mode */
status = HAL_TIM_Base_Start_IT(&htim5);
status = HAL_TIM_Base_Start_IT(&htim6);
if (status == HAL_OK)
{
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
{
/* Enable the TIM5 global Interrupt */
HAL_NVIC_SetPriority(TIM5_IRQn, TickPriority, 0U);
/* Enable the TIM6 global Interrupt */
HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority, 0U);
uwTickPrio = TickPriority;
}
else
@@ -102,8 +102,8 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
}
}
/* Enable the TIM5 global Interrupt */
HAL_NVIC_EnableIRQ(TIM5_IRQn);
/* Enable the TIM6 global Interrupt */
HAL_NVIC_EnableIRQ(TIM6_IRQn);
/* Return function status */
return status;
@@ -111,25 +111,25 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
/**
* @brief Suspend Tick increment.
* @note Disable the tick increment by disabling TIM5 update interrupt.
* @note Disable the tick increment by disabling TIM6 update interrupt.
* @param None
* @retval None
*/
void HAL_SuspendTick(void)
{
/* Disable TIM5 update Interrupt */
__HAL_TIM_DISABLE_IT(&htim5, TIM_IT_UPDATE);
/* Disable TIM6 update Interrupt */
__HAL_TIM_DISABLE_IT(&htim6, TIM_IT_UPDATE);
}
/**
* @brief Resume Tick increment.
* @note Enable the tick increment by Enabling TIM5 update interrupt.
* @note Enable the tick increment by Enabling TIM6 update interrupt.
* @param None
* @retval None
*/
void HAL_ResumeTick(void)
{
/* Enable TIM5 Update interrupt */
__HAL_TIM_ENABLE_IT(&htim5, TIM_IT_UPDATE);
/* Enable TIM6 Update interrupt */
__HAL_TIM_ENABLE_IT(&htim6, TIM_IT_UPDATE);
}

View File

@@ -68,7 +68,7 @@ extern DMA_HandleTypeDef handle_GPDMA1_Channel3;
extern UART_HandleTypeDef huart4;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;
extern TIM_HandleTypeDef htim5;
extern TIM_HandleTypeDef htim6;
/* USER CODE BEGIN EV */
@@ -243,17 +243,17 @@ void TIM3_IRQHandler(void)
}
/**
* @brief This function handles TIM5 global interrupt.
* @brief This function handles TIM6 global interrupt.
*/
void TIM5_IRQHandler(void)
void TIM6_IRQHandler(void)
{
/* USER CODE BEGIN TIM5_IRQn 0 */
/* USER CODE BEGIN TIM6_IRQn 0 */
/* USER CODE END TIM5_IRQn 0 */
HAL_TIM_IRQHandler(&htim5);
/* USER CODE BEGIN TIM5_IRQn 1 */
/* USER CODE END TIM6_IRQn 0 */
HAL_TIM_IRQHandler(&htim6);
/* USER CODE BEGIN TIM6_IRQn 1 */
/* USER CODE END TIM5_IRQn 1 */
/* USER CODE END TIM6_IRQn 1 */
}
/**
@@ -289,29 +289,13 @@ void USART3_IRQHandler(void)
*/
void UART4_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 0 */
if (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE))
{
__HAL_UART_CLEAR_IDLEFLAG(&huart4); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */
// ֹͣDMA
HAL_UART_DMAStop(&huart4);
// <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>ʽ<EFBFBD><CABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
uint16_t len = UART_DMA_RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart4.hdmarx);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ring buffer
for (uint16_t i = 0; i < len; 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;
}
flag = 1;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DMA
HAL_UART_Receive_DMA(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
}
/* USER CODE END UART4_IRQn 1 */
}
/* USER CODE BEGIN 1 */