cancel
Showing results for 
Search instead for 
Did you mean: 

UART NOT Transmit if there is a delay in while loop

ARehm
Associate III

Hi,

I generated the code through CUBEMAX, using USART3 with DMA and trying to send couple of bytes by call HAL_UART_Transmit_DMA in main while(1) loop. If I add the delay after a transmission even a 1ms it stop transmitting but if I remove the delay it start transmitting.

HERE is code:

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include <string.h>

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

#define MB_PER_SEC_1 1000000

#define HOST_RADIO_SHDN_Pin GPIO_PIN_5

#define HOST_RADIO_SHDN_GPIO_Port GPIOD

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart3;

DMA_HandleTypeDef hdma_usart3_rx;

DMA_HandleTypeDef hdma_usart3_tx;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_DMA_Init(void);

static void MX_USART3_UART_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

uint8_t data[] = {0x7E ,0x44 ,0x00 ,0x85 ,0x26 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x04 ,0x00, 0x31, 0x32 ,0x30, 0x00,0xFF};

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

  

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_DMA_Init();

 MX_USART3_UART_Init();

 /* USER CODE BEGIN 2 */

HAL_GPIO_WritePin(HOST_RADIO_SHDN_GPIO_Port, HOST_RADIO_SHDN_Pin, GPIO_PIN_SET);

HAL_Delay(100);

HAL_GPIO_WritePin(HOST_RADIO_SHDN_GPIO_Port, HOST_RADIO_SHDN_Pin, GPIO_PIN_RESET);

 /* USER CODE END 2 */

HAL_Delay(5000);

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while(1){

 if(HAL_UART_Transmit_DMA(&huart3, data, strlen((char*)data) - 1) != HAL_OK)

 {

 Error_Handler();

 }

 HAL_Delay(5000);

 }

 /* USER CODE END 3 */

}

/**

 * @brief System Clock Configuration

 * @retval None

 */

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 /** Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;

 RCC_OscInitStruct.MSIState = RCC_MSI_ON;

 RCC_OscInitStruct.MSICalibrationValue = 0;

 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;

 RCC_OscInitStruct.PLL.PLLM = 1;

 RCC_OscInitStruct.PLL.PLLN = 40;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;

 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 /** Initializes the CPU, AHB and APB busses clocks 

 */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)

 {

  Error_Handler();

 }

 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3;

 PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure the main internal regulator output voltage 

 */

 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

 {

  Error_Handler();

 }

}

/**

 * @brief USART3 Initialization Function

 * @param None

 * @retval None

 */

static void MX_USART3_UART_Init(void)

{

 /* USER CODE BEGIN USART3_Init 0 */

 /* USER CODE END USART3_Init 0 */

 /* USER CODE BEGIN USART3_Init 1 */

 /* USER CODE END USART3_Init 1 */

 huart3.Instance = USART3;

 huart3.Init.BaudRate = 1000000;

 huart3.Init.WordLength = UART_WORDLENGTH_8B;

 huart3.Init.StopBits = UART_STOPBITS_1;

 huart3.Init.Parity = UART_PARITY_NONE;

 huart3.Init.Mode = UART_MODE_TX_RX;

 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;//UART_HWCONTROL_RTS_CTS;

 huart3.Init.OverSampling = UART_OVERSAMPLING_16;

 huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

 huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

 if (HAL_UART_Init(&huart3) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN USART3_Init 2 */

 /* USER CODE END USART3_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_Channel2_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);

 /* DMA1_Channel3_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOC_CLK_ENABLE();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

 __HAL_RCC_GPIOD_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /*Configure GPIO pin : HOST_RADIO_SHDN_Pin */

  GPIO_InitStruct.Pin = HOST_RADIO_SHDN_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(HOST_RADIO_SHDN_GPIO_Port, &GPIO_InitStruct);

}

2 REPLIES 2

Stop execution in debugger and find out what does the processor do. Read out and check the DMA and UART registers content.

JW

S.Ma
Principal

Understand what the HAL function does: is it blocking? if not, what function tells you when the transfer is completed (instead of a crude delay)...