Setitimer(ITIMER_VIRTUAL, &value2, & ovalue) Setitimer(ITIMER_REAL, &value, &ovalue) // (2) Printf( " process id is %d/n ", getpid()) Struct itimerval value, ovalue, value2 // (1) Printf( " Catch a signal - SIGVTALRM /n " ) Printf( " Catch a signal - SIGALRM /n " ) SetitiMer () call successfully returns 0, otherwise returns -1.īelow is a simple demonstration about SetitiMer calls, in this example, a SIGALRM is issued every other second, a SIGVTALRM signal is issued every 0.5 seconds: Setitimer () The first parameter which specifies the timer type (above three of the above) the second parameter is an instance of structure ITIMERVAL the third parameter may not work. ITIMER_PROF: Calculates at the time of user state and in-kernel states, which is sent to the SigProf signal. Itimer_virtual: - It is calculated at the time spent at the user state, it sends a SigVTALRM signal. ITIMER_REAL: Calculates the real time of the system, it sends the SIGALRM signal. Setitimer () is powerful than the ALARM, supports three types of timers:
Int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue))
#Itimer virtual exmaple registration
It can be seen that the kernel issues a sigalrm signal to the application process, performs the corresponding registration function, not the process of killing the process.
Signal(SIGALRM, sig_alarm) // Sign up to the ALARM signalĪlarm( 5) // After 5 seconds, the kernel issues a ALARM signal to the process, performs the corresponding signal registration function. TimerInitStructure.Printf( " sig is %d, sig_alarm is called\n ", sig) TimerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1 TimerInitStructure.TIM_CounterMode = TIM_CounterMode_Up TimerInitStructure.TIM_Prescaler = 40000 TIM_TimeBaseInitTypeDef timerInitStructure RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE) GPIO_WriteBit(GPIOD, GPIO_Pin_12 | GPIO_Pin_13, Bit_RESET) GpioStructure.GPIO_Speed = GPIO_Speed_50MHz GpioStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE) Note that before we can do anything with the timer we need to enable it by calling RCC_APB1PeriphClockCmd():
#Itimer virtual exmaple series
by following this tutorial for STM32F1 series devices or this tutorial for the STM32F4-Discovery board). To learn about controlling the timers using the new HAL interface, follow this tutorial instead.īefore you begin with this tutorial please create a basic project for your STM32 device (e.g. Warning: this tutorial describes the legacy StdPeriph interface. We will show it based on the STM32F4-Discovery board, however controlling the timers of other STM32 devices is very similar. This tutorial shows how control the hardware timers of an STM32 microcontroller.