cancel
Showing results for 
Search instead for 
Did you mean: 

Why i cant read and send data between UART2 of a STM32L412RBT6 (PA2 and PA3) and the BlueNRG-M2SP UART(DIO8 and DIO11)

PCong.1
Associate II

I designed a PCB with an STM32L412RBT6 and a BlueNRG-M2SP, and I checked the connection and I can't read data from the bluetooth module and I can't send data from the STM32.

I made the following code:

stm32l4xx_it.c

void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
 
  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */
  HAL_UART_Receive_IT(&huart2, &BLE_Byte, 1);
  /* USER CODE END USART2_IRQn 1 */
}

main.c

/*BLE private variable*/
char BLE_recv[BLE_RXBuff_SIZE];
char MainBuff_BLE[BLE_MainBuff_SIZE];
uint8_t BLE_Byte;
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	if(huart->Instance == USART2){
		BLE_recv[j++] = BLE_Byte;
	}
}
 
static void MX_USART2_UART_Init(void)
{
 
  /* USER CODE BEGIN USART2_Init 0 */
 
  /* USER CODE END USART2_Init 0 */
 
  /* USER CODE BEGIN USART2_Init 1 */
 
  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
 
  /* USER CODE END USART2_Init 2 */
 
}

Here is a diagram of the connection between the component

0693W00000KcGmRQAV.pngI don't understand why it doesn't work

6 REPLIES 6
KnarfB
Principal III

If you initially call HAL_UART_Receive_IT and have enabled the interrupt in the .ioc view (NVIC), the code looks reasonable. You may loose chars, because you read char by char. This is an often discussed topic here and you might eventually switch to DMA or such.

But, if it doesn't work at all, for hardware bring-up I would not use interrupt mode but polling. Probably at register level, to rule out any software issues. Like "connecting" USART1 and USART2 internaly by few lines of code.

hth

KnarfB

TDK
Guru

You have no calls to transmit or receive in the posted code. You sort of have calls to receive, but they're within an IRQ which is never called by the posted code.

Ensure BlueNRG-M2SP is set up in UART mode.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for the answer, I found out why I can't receive, I forgot to call the receive function in the callback and in main.

please can someone help me with this code i want it in my project , how to communicate between uart stm32l4xx and blueNRG

thank you

Post a new thread with clear questions if you have a question. This thread was solved.
If you feel a post has answered your question, please click "Accept as Solution".

i did not understand where i have to call the transmit and recieve function in the main.c