Giter Site home page Giter Site logo

serj-bashlayev / stm32_profiler Goto Github PK

View Code? Open in Web Editor NEW
33.0 2.0 4.0 456 KB

Simple and fast code profiler for STM32 ARM Cortex-M CPU family

License: MIT License

C 97.35% HTML 1.78% Batchfile 0.01% Assembly 0.86%
stm32 profiler example keil data-watchpoint-and-trace cycle-counter arm

stm32_profiler's Introduction

Code profiling for STM32

Features

Simple and fast code profiler for STM32 ARM Cortex-M CPU family.
Allow measuring time intervals and printing it in table view to SWO debug port.
Use Data Watchpoint and Trace (DWT) cycle counter (DWT_CYCCNT).

Example data print

Profiling "Start" sequence:                                
--Event-----------------------|--timestamp--|---delta_t----
GLCD_Init                     :       41 us | +       41 us
OLED_Init                     :     5288 us | +     5247 us
u8g_SetFont                   :     5292 us | +        4 us
HAL_Delay(10)                 : 10004967 us | +  9999675 us

Minimum measure time = 0 µs
Maximum measure time = 59,6 sec at core clock 72 MHz
Time accuracy ±1 µs.

Easy to use. Easy to port to another ARM architecture and programming language.

Technical background:
SWO is a dedicated pin of ARM's Cortex-M debug interface. While it can be used to output various information in real time by the CPU, it's main usage is terminal I/O in real time with very low intrusion. Most programs can perform debug outputs without losing their real time behavior.

Cycle Count Register (DWT_CYCCNT): 32-bit, incrementing (up) cycle counter. When enabled, this counter counts the number of core cycles. Counting is suspended when the core is halted in Debug state. CYCCNT wraps to 0 on overflow.

How to use

start profiling session
PROFILING_START("*session name*");

Insert timestamp command as many times as necessary.[1]
PROFILING_EVENT("*event name*");

close profiling session and read it times on Serial wire viewer (SWV)
PROFILING_STOP();

Example code

int main(void)
{
  SysTick_Config(SystemCoreClock / 1000);
  
  PROFILING_START("MAIN startup timing");

  Init_IO();
  PROFILING_EVENT("IO_Init()");

  Init_TIM6();
  PROFILING_EVENT("TIM6_Init()");

  PROFILING_STOP();

  while (1)
  {
    PROFILING_START("MAIN loop timing");

    GPIO_WriteBit(GPIOE, GPIO_Pin_9, (((Tick % 1000) > 500) ? Bit_SET : Bit_RESET));
    PROFILING_EVENT("GPIO_WriteBit(...)");

    // Wait for update Tick
    delay_tick = Tick;
    while (delay_tick == Tick);
    PROFILING_EVENT("Wait for update Tick");

    // Delay 1000 ms
    delay_tick = Tick + 1000;
    while (delay_tick > Tick);
    PROFILING_EVENT("DELAY 1 s");

    // Stop profiling and print
    PROFILING_STOP();
  }
}

Debug (printf) Viewer example

Profiling "MAIN startup timing" sequence: 
--Event-----------------------|--timestamp--|----delta_t---
IO_Init()                     :        9 µs | +        9 µs
TIM6_Init()                   :       18 µs | +        8 µs

Profiling "MAIN loop timing" sequence: 
--Event-----------------------|--timestamp--|----delta_t---
GPIO_WriteBit(...)            :        1 µs | +        1 µs
Wait for update Tick          :      797 µs | +      795 µs
DELAY 1 s                     :  1000797 µs | +   999999 µs

Keil µVision Debug (printf) Viewer

For more information, how to use the Keil µVision Debug (printf) Viewer see http://www.keil.com/support/man/docs/ulink2/ulink2_trace_itm_viewer.htm
You can also use ST-LINK - Printf via SWO viewer feature or other debugging software with SWO Viewer support.


note 1 The maximum number of events is defined in MAX_EVENT_COUNT (profiling.h)

stm32_profiler's People

Contributors

s-t-a-n avatar serj-bashlayev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

stm32_profiler's Issues

"DWT->LAR" not recognized when compiling for STM32F103

The compiler shows error in the function

void PROFILING_START(const char *profile_name)
{
  prof_name = profile_name;
  event_count = 0;

  CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  DWT->LAR = 0xC5ACCE55; // <<<<<<<< 'DWT_Type {aka struct <anonymous>}' has no member named 'LAR'
  DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // enable counter
  //DWT->CYCCNT  = time_start = 0;
  time_start = DWT->CYCCNT;
}

Looking at the core headers, "LAR" field is in "ITM_Type" struct, not in "DWT_Type".
The issue occurs within Platformio IDE using stm32cube framework.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.