cancel
Showing results for 
Search instead for 
Did you mean: 

DMA USART callback function

ilyaz yilmaz
Associate III

Hello,

Stm32 F401RE Hal Library 2016 Works.

Stm32 F401RE Hal Library 2017(last version) DOESNT Works.

For Test purpose only I use USART DMA Send function. Main.C is like that...

Also This StMicroelectrincs youtube vide work 2016 HAL Library but it doesnt work 2017 Hal Library. Video is very simple actually. I test it maybe 10 times. but 2017 Hal Library doesnt work.

https://www.youtube.com/watch?v=BxFYmBYBmaU

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

/* USER CODE BEGIN 0 */

uint8_t data_tx[5] = {0x41 , 0x42 ,0x43 ,0x44 , 0x45};

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

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_USART2_UART_Init();

 MX_USART1_UART_Init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */

    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);

    HAL_UART_Transmit_DMA(&huart1, data_tx, 5);

    HAL_Delay(500);

 }

 /* USER CODE END 3 */

}

/* USER CODE BEGIN 4 */

uint64_t cnt1;

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

   cnt1++;

}

/* USER CODE END 4 */

I Will also add interrupt file

extern DMA_HandleTypeDef hdma_usart1_rx;

extern DMA_HandleTypeDef hdma_usart1_tx;

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/

/*          Cortex-M4 Processor Interruption and Exception Handlers         */

/******************************************************************************/

/**

 * @brief This function handles Non maskable interrupt.

 */

void NMI_Handler(void)

{

 /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

 /* USER CODE END NonMaskableInt_IRQn 0 */

 /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

 /* USER CODE END NonMaskableInt_IRQn 1 */

}

/**

 * @brief This function handles Hard fault interrupt.

 */

void HardFault_Handler(void)

{

 /* USER CODE BEGIN HardFault_IRQn 0 */

 /* USER CODE END HardFault_IRQn 0 */

 while (1)

 {

   /* USER CODE BEGIN W1_HardFault_IRQn 0 */

   /* USER CODE END W1_HardFault_IRQn 0 */

 }

}

/**

 * @brief This function handles Memory management fault.

 */

void MemManage_Handler(void)

{

 /* USER CODE BEGIN MemoryManagement_IRQn 0 */

 /* USER CODE END MemoryManagement_IRQn 0 */

 while (1)

 {

   /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */

   /* USER CODE END W1_MemoryManagement_IRQn 0 */

 }

}

/**

 * @brief This function handles Pre-fetch fault, memory access fault.

 */

void BusFault_Handler(void)

{

 /* USER CODE BEGIN BusFault_IRQn 0 */

 /* USER CODE END BusFault_IRQn 0 */

 while (1)

 {

   /* USER CODE BEGIN W1_BusFault_IRQn 0 */

   /* USER CODE END W1_BusFault_IRQn 0 */

 }

}

/**

 * @brief This function handles Undefined instruction or illegal state.

 */

void UsageFault_Handler(void)

{

 /* USER CODE BEGIN UsageFault_IRQn 0 */

 /* USER CODE END UsageFault_IRQn 0 */

 while (1)

 {

   /* USER CODE BEGIN W1_UsageFault_IRQn 0 */

   /* USER CODE END W1_UsageFault_IRQn 0 */

 }

}

/**

 * @brief This function handles System service call via SWI instruction.

 */

void SVC_Handler(void)

{

 /* USER CODE BEGIN SVCall_IRQn 0 */

 /* USER CODE END SVCall_IRQn 0 */

 /* USER CODE BEGIN SVCall_IRQn 1 */

 /* USER CODE END SVCall_IRQn 1 */

}

/**

 * @brief This function handles Debug monitor.

 */

void DebugMon_Handler(void)

{

 /* USER CODE BEGIN DebugMonitor_IRQn 0 */

 /* USER CODE END DebugMonitor_IRQn 0 */

 /* USER CODE BEGIN DebugMonitor_IRQn 1 */

 /* USER CODE END DebugMonitor_IRQn 1 */

}

/**

 * @brief This function handles Pendable request for system service.

 */

void PendSV_Handler(void)

{

 /* USER CODE BEGIN PendSV_IRQn 0 */

 /* USER CODE END PendSV_IRQn 0 */

 /* USER CODE BEGIN PendSV_IRQn 1 */

 /* USER CODE END PendSV_IRQn 1 */

}

/**

 * @brief This function handles System tick timer.

 */

void SysTick_Handler(void)

{

 /* USER CODE BEGIN SysTick_IRQn 0 */

 /* USER CODE END SysTick_IRQn 0 */

 HAL_IncTick();

 /* USER CODE BEGIN SysTick_IRQn 1 */

 /* USER CODE END SysTick_IRQn 1 */

}

/******************************************************************************/

/* STM32F4xx Peripheral Interrupt Handlers                                   */

/* Add here the Interrupt Handlers for the used peripherals.                 */

/* For the available peripheral interrupt handler names,                     */

/* please refer to the startup file (startup_stm32f4xx.s).                   */

/******************************************************************************/

/**

 * @brief This function handles DMA2 stream2 global interrupt.

 */

void DMA2_Stream2_IRQHandler(void)

{

 /* USER CODE BEGIN DMA2_Stream2_IRQn 0 */

 /* USER CODE END DMA2_Stream2_IRQn 0 */

 HAL_DMA_IRQHandler(&hdma_usart1_rx);

 /* USER CODE BEGIN DMA2_Stream2_IRQn 1 */

 /* USER CODE END DMA2_Stream2_IRQn 1 */

}

/**

 * @brief This function handles DMA2 stream7 global interrupt.

 */

void DMA2_Stream7_IRQHandler(void)

{

 /* USER CODE BEGIN DMA2_Stream7_IRQn 0 */

 /* USER CODE END DMA2_Stream7_IRQn 0 */

 HAL_DMA_IRQHandler(&hdma_usart1_tx);

 /* USER CODE BEGIN DMA2_Stream7_IRQn 1 */

 /* USER CODE END DMA2_Stream7_IRQn 1 */

}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

11 REPLIES 11

What exactly"doesn't work" means?

Read out and check/compare between working/not working the relevant USART/DMA/GPIO registers.

JW

ilyaz yilmaz
Associate III

Ohhh I forgot to write ,

Dma function send data but Callback completed function doesnt work. below function..

/* USER CODE BEGIN 4 */

uint64_t cnt1;

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

   cnt1++;

}

/* USER CODE END 4 */

And it transmit only one time second time it doesnt transmit. Because dma stay busy state.

But same code works with 2016 Hal Library.

TDK
Guru

I would use the latest HAL library instead of 3-4 year old versions.

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

2017 is the latest hal library...

it correspond to firmvare STM32Cube_FW_F4_V1.25.0

and it s the last firmware.

TDK
Guru

> 2017 is the latest hal library...

> it correspond to firmvare STM32Cube_FW_F4_V1.25.0

> and it s the last firmware.

I agree that v1.25.0 is the latest firmware, but why are you referring to it as "Hal Library 2017"? It was released February 12, 2020.

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

Ok.I will explain why I said 2017 dma hal library.

In the source code which doesnt work (stm32f4xx_hal_dma.c) There is comment like that....

******************************************************************************
 
 * @attention
 
 *
 
 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
 
 * All rights reserved.</center></h2>
 
 *
 
 * This software component is licensed by ST under BSD 3-Clause license,
 
 * the "License"; You may not use this file except in compliance with the
 
 * License. You may obtain a copy of the License at:
 
 *                       opensource.org/licenses/BSD-3-Clause
 
 *
 
 ******************************************************************************
 
 */ 

In the source code which works (stm32f4xx_hal_dma.c) There is comment like that....

 ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */ 

So I write the 2017 and 2016 years. But Firmware 1.25.0 doesnt work as expected in dma.Transmit and receive.

But older firmware I dont know exactly whic version it's working.

Piranha
Chief II

Could be related to USE_HAL_UART_REGISTER_CALLBACKS.

Anyway it's HAL - overcomplex non-working bloatware with unusable architecture. Toss it out and invest your time in learning real development:

https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx

I agree with you about Hal Libraries. They could be more reliabable and bare...

Anyway I realize that.Hal Library only call callback function in circular mode.

static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
{
  UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  /* DMA Normal mode*/
  if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
  {
 
    huart->TxXferCount = 0x00U;
 
    /* Disable the DMA transfer for transmit request by setting the DMAT bit
       in the UART CR3 register */
    CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
 
    /* Enable the UART Transmit Complete Interrupt */
    SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
	#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
		/*Call registered Tx complete callback*/
		huart->TxCpltCallback(huart);
	#else
		/*Call legacy weak Tx complete callback*/
		HAL_UART_TxCpltCallback(huart);
	#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
 
 
  }
  /* DMA Circular mode */
  else
  {
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
    /*Call registered Tx complete callback*/
    huart->TxCpltCallback(huart);
#else
    /*Call legacy weak Tx complete callback*/
    HAL_UART_TxCpltCallback(huart);
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  }
}

ilyaz yilmaz
Associate III

https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx

Also this sample uses Low layer api.

You advie to use LL insetead of HAL.