cancel
Showing results for 
Search instead for 
Did you mean: 

LCD Suggestion to optimize CPU time.

thln47
Associate III
Posted on April 12, 2018 at 15:52

Suggestion to optimize CPU time.

Hello,

I'm working on STM32L433 device and I use the LCD peripheral embedded.

The CPU executes my main loop every 50ms, like the description below.

And I refresh the LCD display at each end of the main loop.

My LCD_Update_Display function call HAL_LCD_UpdateDisplayRequest routine in stm32l4xx_hal_lcd.c file.

Depending on which define (UPDATE_LCD_BEFORE or UPDATE_LCD_AFTER) I declare, the result shows that the HAL_LCD_UpdateDisplayRequest wait for LCD_FLAG_UDD flag.

But during this wait, no optimization is possible.

    #define LOOP_TIME_MS        50

    ...

    uint32_t t, ta=0;

    

    /* Infinite loop */

    while (1) {

        t = HAL_GetTick();                                     // get the time in ms

        PRINT('t_activity = %d%%\r\n', (unsigned int)ta);     // print the CPU usage in % on the debug console.

        

        Do_something();        

        

#ifdef UPDATE_LCD_BEFORE

        LCD_Update_Display();

#endif        

        ta = HAL_GetTick();

        ta = (100*(ta - t)) / LOOP_TIME_MS;

        t += LOOP_TIME_MS;

#ifdef UPDATE_LCD_AFTER

        LCD_Update_Display();

#endif        

        while (HAL_GetTick() < t) {

           Enter_in_sleep_mode();

        }

    }

Below is the result with UPDATE_LCD_BEFORE define. The variation of these values show that the main loop and LCD wait loop are not synchronized. Furthermore, the LCD wait loop is important compared to the main loop. It's taking about 46ms.

t_activity = 8%

t_activity = 92%

t_activity = 84%

t_activity = 76%

t_activity = 68%

t_activity = 60%

t_activity = 52%

t_activity = 44%

t_activity = 36%

t_activity = 28%

t_activity = 18%

t_activity = 10%

Below is the result with UPDATE_LCD_AFTER define. the CPU activity doesn't include the LCD wait loop.

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

t_activity = 4%

These results shows that the LCD wait loop should be optimized. But no callback is coded inside the loop :(.

So my suggestion is that the HAL_LCD_UpdateDisplayRequest function includes a weak callback function like this:

  ...

  /*!< Wait Until the LCD display is done */

  while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDD) == RESET)

  {         

    if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)

    {

      hlcd->ErrorCode = HAL_LCD_ERROR_UDD;

      

      /* Process Unlocked */

      __HAL_UNLOCK(hlcd);

      return HAL_TIMEOUT;

    }

    /* THLN 12/04/2018 */

    HAL_WaitForLCDUpdate();    

  }

  ...

And the callback function like this:

__weak void HAL_WaitForLCDUpdate(void) {

}

With this code, the user (me) can write his own HAL_WaitForLCDUpdate function.

void HAL_WaitForLCDUpdate(void) {

    Enter_in_sleep_mode_or_do_some_stuff();

}

Device description :

STM32L433 LCD

fCPU=8000000 Hz

STM32 DEV_ID=435, REV_ID=1001

        LQFP48

        FLASH   = 256 KBytes

        BOOTLOADER version : 10

Use library version : 10801008
1 REPLY 1
Posted on April 12, 2018 at 17:03

This isn't safe, the value of HAL_GetTick wraps, you could wait 49 days

        t += LOOP_TIME_MS;

#ifdef UPDATE_LCD_AFTER

        LCD_Update_Display();

#endif         

       while (HAL_GetTick() < t) {

           Enter_in_sleep_mode();

        }

This would be a better method

#ifdef UPDATE_LCD_AFTER

        LCD_Update_Display();

#endif     

        while ((HAL_GetTick() - t) < LOOP_TIME_MS) {

           Enter_in_sleep_mode();

        }
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..