cancel
Showing results for 
Search instead for 
Did you mean: 

hdma_tim1_ch1.XferCpltCallback = TransferCompleteCallback; IS NOT TRIGGERIING

thannara123
Senior

I am transfering data array from an array to PWM register of TIM1.

int16_t sin_table[20]= { 0, 39, 77, 113, 147, 177, 202, 223, 238,247, 250, 247, 238, 223, 202, 177, 147, 113, 77, 39};

HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)sin_table, 20);

And i am trying to blink in complete transfer callback fuction as follows .

void TransferCompleteCallback(DMA_HandleTypeDef *hdma) {
{  
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
 
}

 

I am getting PWM signal as expected . but trigger the  TransferCompleteCallback).

by using break poiint the DMA Inerupt Handler triggers but not trigger the callback i said . 

 

whats the wrong am with please help me .

i am attaching the full code here 

 

aNY BODY HELP ME 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Ok seems you dont show STM32 xx ?? Ok when you build with defined USE_HAL_TIM_REGISTER_CALLBACKS == 1

#if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
          htim->OC_DelayElapsedCallback(htim);
          htim->PWM_PulseFinishedCallback(htim);
#else
          HAL_TIM_OC_DelayElapsedCallback(htim);
          HAL_TIM_PWM_PulseFinishedCallback(htim);
#endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
        }
        htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;

part of stm32f4xx_hal_driver/Src/stm32f4xx_hal_tim.c at master · STMicroelectronics/stm32f4xx_hal_driver · GitHub

hope you now see how and when register callbacks.

View solution in original post

12 REPLIES 12
thannara123
Senior

Any help ?

@Sarra.S 

 

 

MM..1
Chief III

Seems you missunderstan HAL callbacks ... read examples in folder repository firmware for your MCU...

You mark all MCUs, this isnt very helpfull. For example F4 from stm32f4xx_hal_tim.c func HAL_TIM_PWM_Start_DMA

 

switch (Channel)
  {
    case TIM_CHANNEL_1:
    {
      /* Set the DMA compare callbacks */
      htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
      htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;

 

thannara123
Senior

Hi Thanks , I didnt find any examples related DMA transfer complete  function trigger based projects .

 

Hi may i need to register the following function to trigger the calla back function 

HAL_DMA_RegisterCallback(&hdma, HAL_DMA_XFER_CPLT_CB_ID, My_DMA_TransferCompleteCallback);

 

Or any other way to do this , is there tutorials or mannual for it ?

Simply HAL prepare weak for you and yu cant choice any func, but need overide.

/**
  * @brief  PWM Pulse finished callback in non-blocking mode
  * @PAram  htim TIM handle
  * @retval None
  */
__weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(htim);

  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
   */
}

then rename in your code void TransferCompleteCallback to HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)

thannara123
Senior

Hi

 HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)  is used for one complete cycle isnt it ?

or one ARR value 

I registred the callback as follows

HAL_DMA_RegisterCallback(&hdma_tim1_ch1,HAL_DMA_XFER_CPLT_CB_ID,GKS);

 

void GKS(DMA_HandleTypeDef*hdma)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}

 

hdma->XferCpltCallback = pCallback; // this code executed while debugging but the above GKS function is not triggering 

 

why so ,is there any oher need to enable ?

 

@MM..1 

@Sarra.S 

 

Sarra.S
ST Employee

Hello @thannara123

>>i am attaching the full code here 

I cannot see the full code, could you share it? 

The registration of the callback using HAL_DMA_RegisterCallback needs to be done before starting the DMA transfer using HAL_DMA_Start. 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Ok seems you dont show STM32 xx ?? Ok when you build with defined USE_HAL_TIM_REGISTER_CALLBACKS == 1

#if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
          htim->OC_DelayElapsedCallback(htim);
          htim->PWM_PulseFinishedCallback(htim);
#else
          HAL_TIM_OC_DelayElapsedCallback(htim);
          HAL_TIM_PWM_PulseFinishedCallback(htim);
#endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
        }
        htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;

part of stm32f4xx_hal_driver/Src/stm32f4xx_hal_tim.c at master · STMicroelectronics/stm32f4xx_hal_driver · GitHub

hope you now see how and when register callbacks.

BarryWhit
Lead II

@thannara123,as MM says, your code is not doing what you think it's doing.

 

1. By default, when Cube generates code it leaves USE_HAL_TIM_REGISTER_CALLBACKS undefined

2. When this is undefined the HAL registers and calls its own, default, callback handlers for each event.

3. This handlers are generated as part of the code but are annotated as "weak" in the code.

4. Weak symbols are compiler/linker feature that allows you to override that symbol (function) with your own implementaiton. All you need to do is to implement the same function (name, and signature) in your own code (without the "weak" annotation) and then it will be used instead of the weak function/symb 

 

Alternatively, you can define USE_HAL_TIM_REGISTER_CALLBACKS, or check the box in CubeMX  GUI that does this for you. Once that is defined, you can use RegisterCallback to register your own callback, and the HAL generated code (using an ifdef USE_HAL_TIM_REGISTER_CALLBACKS) will invoke it.

 

 

- If someone's post helped resolve your issue, please thank them by clicking "Accept as Solution".
- Please post an update with details once you've solved your issue. Your experience may help others.
thannara123
Senior

Thanks for the knowlegde sharing .

 

Actually I require the callback function is whenever complete the full arrray throgh DMA , DMA will trigger the TCIF flag .SO then i require a user defind callback .so I registred a call back function as follows 

HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)(DMA_HandleTypeDef *_hdma))
{

HAL_StatusTypeDef status = HAL_OK;

/* Process locked */
__HAL_LOCK(hdma);

if(HAL_DMA_STATE_READY == hdma->State)
{
switch (CallbackID)
{
case HAL_DMA_XFER_CPLT_CB_ID:
hdma->XferCpltCallback = pCallback; // it will trigger or give 1 , upon the full transfer 
break; //

 

 

HAL_DMA_RegisterCallback(&hdma_tim1_ch1,aCallbackIDTypeDef,Usrdefnd_Complete_callback);

 

void Usrdefnd_Complete_callback(DMA_HandleTypeDef*hdma)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}

 

I tried like wise but not worked .

 

Also I tried the follwsing type of callback 

hdma.XferCpltCallback = MyDMA_TransferCompleteCallback;  //But not worked as expected 

I doubt as follows 

HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)sin_table, 20);//low
HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_1);

 

Here I am not used IT in the HAL_TIM_PWM_Start_DMA funtion , is it make any problem ?

 

anyway the cube ide enabled the DMA2 stream 1 and 2 as shown below .

k.png