"Time Analysis of Embedded Software" Chapter 5 Software Time Analysis Methods - Running Time Measurement Learning
[Copy link]
This chapter mainly studies the measurement of running time.
The oldest method of timing analysis is runtime measurement using port pins.
Use an oscilloscope or logic analyzer to visualize and measure the signals at the port pins to test the total execution time.
This is actually very commonly used. If you want to measure the time taken by a piece of code, the most common method is to use this old port measurement method.
In the application of STM32, SEV is specially introduced. This SEV is much simpler than the configuration of ATMEGA8's PORTB as an example in the book. You can directly call sev() to pull the port high and low. The sev function only takes one clock cycle. Assuming the clock is 100MHz
One clock cycle is 10ns. If it is STM32F4, the main frequency is close to 180M, then the time taken is even shorter.
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
The second method:
Time measurement method without using ports and instruments
Port pins or external measurement methods are not always available. External measurement equipment may not be available at hand, so other convenient operation methods need to be used, such as hardware timers as the basis for measurement.
After configuring the timer period, the timer value is stored at the beginning of the measurement element and read out at the end of the measurement element. The difference between these two values is the total running time.
Take STM32 as an example
start_cnt=__HAL_TIM_GET_COUNTER(&TIM1_Handler); //获取定时器的值,开始计时
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
delay_ms(20);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET); /
delay_ms(20);
end_cnt=__HAL_TIM_GET_COUNTER(&TIM1_Handler); //获取结束时的定时器的计数值
The difference between end_cnt and start_cnt is the running time
|