cancel
Showing results for 
Search instead for 
Did you mean: 

Why UART1 block UART2

PCong.1
Associate II

Hello ST Community,

I use two UARTs at the same time, 1 to communicate with a sensor and the other to communicate with the BlueNGR-M2SP BLE module.

The UART2 is configured with DMA IDLE because the length of the frame I receive varies and the UART1 has a classic configuration, but when I receive data from the sensor (UART1), it stops the transmission and reception of the UART2

here is my code

K_LD7.c

#include "main.h"
#include "k_ld7.h"
#include "dmx_rx.h"
#include "dmx_tx.h"
#include "DMX.h"
#include "LED_animation.h"
#include "stdio.h"
#include "stdlib.h"
#include <malloc.h>
 
extern uint8_t flag;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_usart1_rx;
 
uint16_t distance;
int16_t angle;
 
int angleBLE;
 
//Initialization commands of sensor.
uint8_t INIT[12] = {0x49, 0x4E, 0x49, 0x54, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Init command for the first communication and fix the Bauderate
uint8_t TDAT[12] = {0x47, 0x4E, 0x46, 0x44, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00}; // TDAT to detect target and get the rang and the angle
uint8_t GBYE[8] = {0x49, 0x4E, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00}; // command to disconnect the sensor
 
int compute_pow(int nb, int pow)
{
    int res = 1;
 
    if (pow == 0)
        return 1;
    for (int i = 0; i < pow; i++)
        res *= nb;
    return res;
}
 
void sensor_init(void){
	HAL_UART_Transmit_IT(&huart1, INIT, sizeof(INIT));
	HAL_Delay(75);
}
 
int sensor_data(void){
 
	LEDStripe_AllOff();
	if (flag == 1) {
		HAL_UART_Transmit_IT(&huart1, TDAT, sizeof(TDAT));
		HAL_Delay(125);
		flag = 0;
	}
	distance =*((uint16_t *)&k_ld7_resp[8]);
	angle = ((*(int16_t*)&k_ld7_resp[12])) / 100;
	intensity = 255 - distance;
 
	int distanceBle = distance;
	int angleBle = angle;
 
	int tmp = distanceBle;
	int tmp2 = angleBle;
	int length = 0;
	int length2 = 0;
 
	while(tmp != 0){
		length += 1;
		tmp /= 10;
	}
 
	while(tmp2 != 0){
		length2 += 1;
		tmp2 /= 10;
	}
 
	char *str = malloc(length + length2 + 4);
	int i = 0;
	for (; i < length; i++) {
		int digit = distanceBle / compute_pow(10, length - i - 1);
		str[i] = digit + '0';
		distanceBle -= digit * compute_pow(10, length - i - 1);
	}
	str[i] = ' ';
	for (; i < length + length2 + 1; i++) {
		int digit = angleBle / compute_pow(10, length + length2 - i - 1);
		str[i] = digit + '0';
		angleBle -= digit * compute_pow(10, length + length2 - i - 1);
	}
 
	str[length + length2 + 1] = '\r';
	str[length + length2 + 2] = '\n';
	str[length + length2 + 3] = '\0';
 
 
	/**************************Centre*****************************************/
	if(angle <= 5 && angle >= -5) {
		/*********Top*************/
		LEDStripe_SetIntensity(intensity_val[4], 1);
		LEDStripe_SetIntensity(intensity_val[5], 2);
		LEDStripe_SetIntensity(intensity_val[6], 3);
		LEDStripe_SetIntensity(intensity_val[7], 4);
		LEDStripe_SetIntensity(intensity_val[8], 5);
		LEDStripe_SetIntensity(intensity_val[9], 6);
		LEDStripe_SetIntensity(intensity_val[8], 7);
		LEDStripe_SetIntensity(intensity_val[7], 8);
		LEDStripe_SetIntensity(intensity_val[6], 9);
		LEDStripe_SetIntensity(intensity_val[5], 10);
		LEDStripe_SetIntensity(intensity_val[4], 11);
		LEDStripe_SetIntensity(intensity_val[3], 12);
 
 
-
-
-
 
 
		/*********Bottom*************/
		LEDStripe_SetIntensity(intensity_val[9], 19);
		LEDStripe_SetIntensity(intensity_val[9], 20);
		LEDStripe_SetIntensity(intensity_val[8], 21);
		LEDStripe_SetIntensity(intensity_val[7], 22);
		LEDStripe_SetIntensity(intensity_val[6], 23);
		LEDStripe_SetIntensity(intensity_val[5], 24);
		LEDStripe_SetIntensity(intensity_val[4], 25);
		LEDStripe_SetIntensity(intensity_val[3], 26);
		LEDStripe_SetIntensity(intensity_val[2], 27);
		LEDStripe_SetIntensity(intensity_val[1], 28);
		LEDStripe_SetIntensity(intensity_val[0] / distance, 29);
		LEDStripe_SetIntensity(intensity_val[0] / distance, 30);
		DMX_TransmitDataTxWait();
			flag = 1;
		}
 
	else {
			LEDStripe_SetIntensityRange(0, 1, 36);
			DMX_TransmitDataTxWait();
			flag = 1;
		}
	flag = 1;
 
	HAL_UART_Transmit_IT(&huart2, (uint8_t*)str, sizeof((uint8_t*)str));
	HAL_Delay(100);
	return distance && angle;
}

main.c for the UART config

/*Sensor Private variable*/
char k_ld7_resp[RxBuff_SIZE];
char MainBuf_K_LD7[Main_buf_SIZE];
 
uint8_t flag = 0;
uint8_t BLE_Byte;
uint8_t rxByte;
uint8_t j;
 
extern uint16_t distance;
extern int16_t angle;
 
uint16_t mode;
uint8_t sensor_BLE[5];
 
/*BLE private variable*/
uint16_t BLE_recv[BLE_RXBuff_SIZE];
char MainBuff_BLE[BLE_MainBuff_SIZE];
 
 
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
	if (huart->Instance == USART1) {
		memcpy(MainBuf_K_LD7, k_ld7_resp, Size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, (uint8_t*)k_ld7_resp, RxBuff_SIZE);
		 __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
		 flag = 1;
	}
}
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	HAL_UART_Receive_IT(&huart2, &BLE_Byte, sizeof(BLE_Byte));
	if(huart->Instance == USART2){
		BLE_recv[j++] = BLE_Byte;
		if (j > 1)
			j = 0;
	}
}
 
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); //Enable the UART1 interface
  HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t*)k_ld7_resp, RxBuff_SIZE);//Reception of the UART1 Direct Access Memory frame
  __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);//Disable the UART1 interface
 
 
static void MX_USART1_UART_Init(void)
{
 
  /* USER CODE BEGIN USART1_Init 0 */
 
  /* USER CODE END USART1_Init 0 */
 
  /* USER CODE BEGIN USART1_Init 1 */
 
  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_9B;
  huart1.Init.StopBits = UART_STOPBITS_2;
  huart1.Init.Parity = UART_PARITY_EVEN;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */
 
  /* USER CODE END USART1_Init 2 */
 
}
 
/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
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 */
 
}
 
 
 
 
/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{
 
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();
 
  /* DMA interrupt init */
  /* DMA1_Channel5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
 
}

stm32l4xx_it.c

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_usart1_rx;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
/* USER CODE BEGIN EV */
extern UART_HandleTypeDef huart3;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
/* USER CODE END EV */
 
void DMA1_Channel5_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel5_IRQn 0 */
 
  /* USER CODE END DMA1_Channel5_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart1_rx);
  /* USER CODE BEGIN DMA1_Channel5_IRQn 1 */
 
  /* USER CODE END DMA1_Channel5_IRQn 1 */
}
 
/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
  /* USER CODE END USART1_IRQn 0 */
	HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
  //HAL_UART_Receive_IT(&huart1, &rxByte, 1);
  /* USER CODE END USART1_IRQn 1 */
}
 
/**
  * @brief This function handles USART2 global interrupt.
  */
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, sizeof(BLE_Byte));
  /* USER CODE END USART2_IRQn 1 */
}

5 REPLIES 5
TDK
Guru

> __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); //Enable the UART1 interface

> HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t*)k_ld7_resp, RxBuff_SIZE);//Reception of the UART1 Direct Access Memory frame

Why are you enabling the RXNE interrupt? ReceiveToIdle isn't expecting this, as it doesn't care about that flag.

Disabling HT as well may not play well with what HAL is expecting.

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

I will try without the activation and deactivation. of the RX interrupt

gbm
Lead III

Look at your HAL_UART_RxCpltCallback() implementation - looks like an error is there. You are not checking which UART read was completed.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

i check it inside the if

if(huart->Instance == USART2){
		BLE_recv[j++] = BLE_Byte;
		if (j > 1)
			j = 0;

without:

 __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); //Enable the UART1 interface

I can't read the data, and without that:

 __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);//Disable the UART1 interface 

I can receive data but I still can't send data with the UART2.