Timers and Interrupt
Timers and Interrupt
Timers and Interrupt
4. Timers
What is timer?
Timer is used to set time for a job
Time remain? How long is a tick?
3
Timing functions
• Periodically interrupt CPU to perform tasks
- Sample sensor readings (temperature, pressure, etc.)
- Generate control signals
6
System Timer (SysTick)
Systick timer is a standard hardware component built into ARM Cortex-M.
7
Diagram of SysTick
SysTick reload value register
(SysTick_LOAD)
8
SysTick interrupt
9
Calculating Reload Value
10
Implementing SysTick
Systick Registers address and Config function are defined in core_cm4.h and <device>.h
<device>.h
core_cm4.h
11
Implementing SysTick
Implementing
Delay(msex) Function
12
Basic Fucntion Timer
13
Basic Timer
Basic timer (TIM6/TIM7)
• 16-bit auto-reload upcounter APB1 clock
• 16-bit programmable
prescaler TIMx_CRy
• Synchronization circuit to trigger TIMx_ARR
the DAC
• Interrupt generation on the
update event: counter overflow
• Can operate in 2 modes: TIMx_PSC TIMx_CNT
- Repetitive mode
- One shot mode
14
Basic Timer Clock Source
can be R/W on the fly
AHB APB1 Prescaler Timer Prescaler
Counter
Clock (/ 1,2,4,8,16) x1 or x2 (/ 1 to 65535 )
RCC_CFGR TIMx_PSC Enable by
bit [10:8] bit [15:0] TIMx_CR1
bit [0]
Example:
Note:
Given HCLK= 32 MHz For STM32L4xx, there are two cases:
1. If the APB prescaler equals 1, the
APB1 prescaler = 1/8 and Timer prescaler = 1/4 timer clock frequencies are set to the
same frequency as that of the APB
We have ftimer = 32/8/4 x 2 = 2 MHz and domain.
2. Otherwise, they are set to twice (×2)
Teach count = 0.5 µs the frequency of the APB domain.
15
Basic Timer Operation
0xFFFF
ARR
CNT
Need to enable
timer interrupt UIF
(TIMx_DIER bit 0)
CEN
16
Basic Timer Operation
ARR
CNT
UIF
CEN
17
Basic Timer Interrupt
Occur when counter overflow
• UIF is set to 1 by hardware, need to be cleared by software in the interrupt function
• If UIE = 1, the processor will be interrupted and call the interrupt function
Interrupt function
• Interrupt function name is predefined in startup_<device>.s
TIM6_DAC_IRQHandler and TIM7_IRQHandler
• Template of the interrupt function
void TIM6_DAC_IRQHandler (void)
{
/* clear the interrupt flag */
TIM6->SR = 0;
/* Your code */
}
18
Basic Timer Control Registers
Basic timer registers are defined in <device.h>
• TIMx_CR1: enable counter, select operation mode
• TIMx_CR2: select trigger output source
• TIMx_DIER: enable/disable Timer interrupt
• TIMx_SR: status of interrupt flag -> clear this register in interrupt
function
• TIMx_CNT: counter value
• TIMx_PSC: prescaler value
• TIMx_ARR: auro reload value
19
Basic Timer Initialization
21
How CPU processes the Interrupt
22
http://learn.mikroe.com/ebooks/piccprogramming/chapter/pic16f887-basic-features/
Interrupt Service Routine (ISR)
23
Interrupt Signal Flow
Each potential interrupt source has a separate enable bit
• Set for devices from which interrupts are to be accepted
• Clear to prevent the peripheral from interrupting the CPU
26
Cortex-M NVIC
27
Cortex-M CPU and peripheral
exceptions
28
Interrupt Vector table
• Declare in startup_<device>.s
30
Interrupt priority levels
31
Interrupt Priority Registers
The NVIC_IPRx registers provide an 8-bit priority field for each interrupt.
33
Accessing the NVIC registers
using CMSIS
34
Accessing the NVIC registers
using CMSIS
Enable/disable interrupts
Example:
EXTI0_IRQn = 6 ; //External interrupt EXTI0 is IRQ #6
TIM2_IRQn = 28 ; //Timer TIM2 interrupt is IRQ #28
Usage:
NVIC_EnableIRQ(EXTI0_IRQn); //enable external interrupt EXTI0
NVIC_DisableIRQ(TIM2_IRQn); //disable interrupt from timer TIM2
35
Accessing the NVIC registers
using CMSIS
Interrupts pending
NVIC interrupt pending flag for each IRQ
• NVIC sets pending flag when it detects IRQn request
- IRQn status changes to “pending”
- IRQn status changes to “active” when its interrupt handler is entered
• NVIC clears pending flag when handler exited
- IRQn status changes to “inactive”
CMSIS functions to set/clear/get IRQn pending status
• Write 1 to NVIC_ICPRx to clear IRQn from pending state
NVIC_ClearPendingIRQ(IRQn);
• Write 1 to NVIC_ISPRx to force IRQn into pending state
NVIC_SetPendingIRQ(IRQn); //simulates IRQn request
• Read 1 from ISPR if IRQn in pending state
NVIC_GetPendingIRQ(IRQn); 36
Accessing the NVIC registers
using CMSIS
Set interrupts priorites
• NVIC selects highest-priority pending IRQ to send to CPU
- Lower priority# = higher priority (default value = 0)
+ If equal priorities, lower IRQ# selected
- Higher priority IRQ can interrupt lower priority one
- Lower priority IRQ not sent to CPU until higher priority IRQ service
completed
• Set IRQn priority via CMSIS function: NVIC_SetPriority(IRQn, priority);
Ex:
//set ext. intr. EXTI0 priority = 1
NVIC_SetPriority(EXTI0_IRQn, 1);
• Get IRQn priority via CMSIS function: NVIC_GetPriority(IRQn_Type IRQn)
37
Example