cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l476 disc uart DMA not working

Jagath
Associate II

#include "main.h"

#include "uartDma.h"

UART_HandleTypeDef huart2;

DMA_HandleTypeDef hdma_usart2_tx;

void uartStart(void)

{

MX_DMA_Init();

 MX_USART2_UART_Init();

uint8_t pData[] = "This is Polling\r\n";

uint8_t pdata[] = "This is Dma\r\n";

HAL_UART_Transmit(&huart2,pData,sizeof(pData),100);

HAL_UART_Transmit_DMA(&huart2,pdata,sizeof(pdata));

}

/**

 * @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 */

}

/**

* @brief UART MSP Initialization

* This function configures the hardware resources used in this example

* @param huart: UART handle pointer

* @retval None

*/

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(huart->Instance==USART2)

 {

 /* USER CODE BEGIN USART2_MspInit 0 */

 /* USER CODE END USART2_MspInit 0 */

  /* Peripheral clock enable */

  __HAL_RCC_USART2_CLK_ENABLE();

  

  __HAL_RCC_GPIOD_CLK_ENABLE();

  /**USART2 GPIO Configuration   

  PD5   ------> USART2_TX

  PD6   ------> USART2_RX 

  */

  GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /* USART2 DMA Init */

  /* USART2_TX Init */

  hdma_usart2_tx.Instance = DMA1_Channel7;

  hdma_usart2_tx.Init.Request = DMA_REQUEST_2;

  hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;

  hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;

  hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;

  hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

  hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

  hdma_usart2_tx.Init.Mode = DMA_NORMAL;

  hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;

  if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)

  {

   Error_Handler();

  }

  __HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx);

  /* USART2 interrupt Init */

  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(USART2_IRQn);

 /* USER CODE BEGIN USART2_MspInit 1 */

 /* USER CODE END USART2_MspInit 1 */

 }

}

/** 

 * Enable DMA controller clock

 */

static void MX_DMA_Init(void) 

{

 /* DMA controller clock enable */

 __HAL_RCC_DMA1_CLK_ENABLE();

 /* DMA interrupt init */

 /* DMA1_Channel7_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);

}

/**

 * @brief This function handles DMA1 channel7 global interrupt.

 */

void DMA1_Channel7_IRQHandler(void)

{

 /* USER CODE BEGIN DMA1_Channel7_IRQn 0 */

 /* USER CODE END DMA1_Channel7_IRQn 0 */

 HAL_DMA_IRQHandler(&hdma_usart2_tx);

 /* USER CODE BEGIN DMA1_Channel7_IRQn 1 */

 /* USER CODE END DMA1_Channel7_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 */

 /* USER CODE END USART2_IRQn 1 */

}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

 /* Prevent unused argument(s) compilation warning */

uint8_t pData[] = "Dma Complete\r\n";

HAL_UART_Transmit(huart,pData,sizeof(pData),1000);

}

/////////////////////////////

output is

This is Polling

This iùÿDma Complete

i just copied the cubeMx generated code to single file . i am not getting DMA output

is there any order for calling functions for getting DMA

0 REPLIES 0