Skip to main content
Graduate II
August 12, 2024
Solved

hdma_tim1_ch1.XferCpltCallback = TransferCompleteCallback; IS NOT TRIGGERIING

  • August 12, 2024
  • 10 replies
  • 3517 views

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 

 

 

    This topic has been closed for replies.
    Best answer by MM..1

    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.

    10 replies

    Graduate II
    August 18, 2024

    Any help ?

    @Sarra.S 

     

     

    Graduate II
    August 18, 2024

    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;

     

    Graduate II
    August 19, 2024

    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 ?

    Graduate II
    August 19, 2024

    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)

    Graduate II
    August 19, 2024

    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 

     

    MM..1Answer
    Graduate II
    August 20, 2024

    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.

    Visitor II
    January 3, 2025

    It seems he's asking about DMA transfer complete and you keep responding with PulseFinished Callback.  Those are not entirely the same thing to my knowledge.  I'm having a similar issue on STM32G491RE and no one seems to have an answer.

    ST Employee
    August 20, 2024

    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. 

     

    Graduate II
    August 20, 2024

    @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.

     

     

    Graduate II
    August 21, 2024

    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

     

     

     

     

     

     

     

     

    Graduate II
    August 21, 2024

    You still have not understood what both MM and I have already explained to you. I'm sorry, but I just can't explain it any more clearly than I already have. 

    Graduate II
    August 21, 2024

    Oh okay i got and corrected it as follows 

    void PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
    { if (htim->hdma[TIM_DMA_ID_CC1] != NULL && htim->hdma[TIM_DMA_ID_CC1] == &hdma_tim1_ch1)
    {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
    }
    }

    htim1.PWM_PulseFinishedCallback = PWM_PulseFinishedCallback;

     

    I get Triggered as expected .

    But I have two problems as follows

    The DMA array size is 20 elements.

    Total Transfer Time=506 µs×20=10.12 ms (as per pwm SIGNAL )

    See the picture

    k.png

    why some red marked area is it continously  (blue is the transfer complete callbak led toggeling signal ) ?

    Also see the picture below 

    k1.png

    Here the blue line is the DMA complete trigger there take an 8 usecon ow to reduce it from the pwm 

     

    Graduate II
    August 21, 2024

    Please use the code highlight block for showing code, it's "</>"  on the posting toolbar. And ask new questions in a new thread, since  your issue has been solved.