Ex Excepti tion
- ns
s and Interru rrupts ts
Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Kasetsart Unive versity
01204322 Embedded System
Revised 2020-01-14
Ex Excepti tion ons s and Interru rrupts ts 01204322 Embedded - - PowerPoint PPT Presentation
Ex Excepti tion ons s and Interru rrupts ts 01204322 Embedded System Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Kasetsart Unive versity Revised 2020-01-14 Ou Outline Polling vs. interrupt
01204322 Embedded System
Revised 2020-01-14
2
3
4
event to happen
program suspends normal execution and executes code that handles the event
http://clipart-library.com/clipart/1162315.htm
https://lastminuteengineers.com/handling
5
forever: wait until switch is pressed toggle LED wait until switch is released when switch pressed is detected: toggle LED forever: do nothing (or sleep)
6
event or service
possible
7
normal program flow à ISR normal program flow à interrupt saves program state restores program state
8
http://clipart-library.com/clipart/618555.htm
9
vector table stores ISR addresses
10
Priority: IRQ2 > IRQ1 (IRQ = Interrupt ReQuest)
ST’s training slides on Moving from 8 to 32 bits hands-on workshop
11
save state, and start executing the ISR
instructions that count as a ‘response’
Taken from lecture slides by Prof. Chung-Ta King
12
shared data structures
etc.)
13
Source: ST’s UM1956: STM32 Nucleo-32 Boards
14
0xFFFFFFFF 0x00000000 0x20000000
Code SRAM
0x40000000
On-chip flash, storing application code On-chip SRAM, for heap, stack, and code Peripherals
0x60000000
E.g., timers, GPIO External Device External RAM
0xA0000000 0xE0000000
System
0.5 GB 0.5 GB 0.5 GB 1 GB 1 GB 0.5 GB
E.g., SD card Off-chip memory for data NVIC, system timer, system control block, vendor-specific memory (e.g., boot loader) 4GB address space
Based on lecture slides and video clips by Dr. Yifeng Zhu
15
0xFFFFFFFF 0x00000000 0x20000000
Code SRAM
0x40000000
Peripherals
0x60000000
External Device External RAM
0xA0000000 0xE0000000
System
0.5 GB 0.5 GB 0.5 GB 1 GB 1 GB 0.5 GB 0x3FFFFFFF 0x20000000 0x2000FFFF
Actual RAM for STM32L432KC (64KB) Stack Global Variables Heap
Based on lecture slides and video clips by Dr. Yifeng Zhu
16
0xFFFFFFFF 0x00000000 0x20000000
Code SRAM
0x40000000
Peripherals
0x60000000
External Device External RAM
0xA0000000 0xE0000000
System
0.5 GB 0.5 GB 0.5 GB 1 GB 1 GB 0.5 GB 0x1FFFFFFF 0x00000000 0x08000000
Reserved Reserved Actual flash for STM32L432KC (256KB)
0x0803FFFF Interrupt Vector Table
Text Section RO Data Section RW Data Section
Initial Stack Pointer Interrupt Vector Table Initial Stack Pointer
default mapping
Based on lecture slides and video clips by Dr. Yifeng Zhu
17
from the interrupt vector table
Based on lecture slides and video clips by Dr. Yifeng Zhu Interrupt Position (8 bits) Memory Address of ISR (32 bits) 1 Address of ISR for interrupt 1 2 Address of ISR for interrupt 2 3 Address of ISR for interrupt 3 : :
Interrupt Vector Table
18
: : : :
Taken from RM0394: Reference Manual for STM32L41xx/2xx/3xx/4xx/5xx/6xx
Address for nth interrupt = 64 + 4n Stack :
Reserved for initial MSP
Reset_Handler()
Code executed after reset :
SysTick_Handler()
Code executed every system tick
WWDG_IRQHandler()
ISR for WWDG interrupt
19
20
EXTI3
Based on lecture slides and video clips by Dr. Yifeng Zhu
21
22
Suppose: * DMA1_Channel2’s priority = 3 * EXTI3’s priority = 5 (The smaller the number, the higher the priority)
Based on lecture slides and video clips by Dr. Yifeng Zhu
23
cycles
Based on lecture slides and video clips by Dr. Yifeng Zhu
25
26
27
with 32.768 kHz LSE enabled for clock trimming
28
Pin Assignment Label PA2 USART2_TX USART2_TX PA15 USART2_RX USART2_RX PB0 GPIO_Input SW PB3 GPIO_Output LD3
29
30
31
32
/* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_UART_Transmit(&huart2, "Hello\n", 6, 0xffff); HAL_Delay(1000); } /* USER CODE END 3 */
33
34
/* Private user code -----------------------------------------*/ /* USER CODE BEGIN 0 */ int __io_putchar(int ch) { HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, 0xffff); return ch; } /* USER CODE END 0 */
35
/* Infinite loop */ /* USER CODE BEGIN WHILE */ int x = 0; while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ printf("x = %d\n", x); HAL_Delay(1000); x++; } /* USER CODE END 3 */
36
main loop
/* USER CODE BEGIN 0 */ : int IsSwitchPressed() { return HAL_GPIO_ReadPin(SW_GPIO_Port, SW_Pin) == 0; } /* USER CODE END 0 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ int x = 0; while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ while (!IsSwitchPressed()) ; x++; printf("x = %d\n", x); while (IsSwitchPressed()) ; } /* USER CODE END 3 */
37
amount of time (e.g., 10 ms)
38
periodic interrupts for sampling switch’s signal levels
connectivity to external I/O pins
(for 80MHz system clock)
39
40
/* USER CODE BEGIN PV */ volatile int g_sw_pressed; /* USER CODE END PV */ : /* USER CODE BEGIN 0 */ : int IsSwitchPressed() { return g_sw_pressed; } /* USER CODE END 0 */
41
/* USER CODE BEGIN 4 */ void CheckSW() { static uint16_t count = 0; int raw_sw_pressed = !HAL_GPIO_ReadPin(SW_GPIO_Port, SW_Pin); if (g_sw_pressed == raw_sw_pressed) { // switch returned to previous state; reset counter count = 10; } else { if (--count == 0) { // switch state has stabilized g_sw_pressed = raw_sw_pressed; } } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance == htim6.Instance) { CheckSW(); } } /* USER CODE END 4 */
This function is called by TIM6_DAC_IRQHandler(), which is the actual ISR
42
/* USER CODE BEGIN 2 */ HAL_TIM_Base_Start_IT(&htim6); /* USER CODE END 2 */
43
44
/* Private typedef -------------------------------------------------*/ /* USER CODE BEGIN PTD */ typedef void (*SwitchChangedCallbackFn)(int state); /* USER CODE END PTD */ : /* USER CODE BEGIN PV */ volatile int g_sw_pressed; SwitchChangedCallbackFn g_sw_cb = NULL; /* USER CODE END PV */ : /* USER CODE BEGIN 0 */ void SetSwitchChangedCallback(SwitchChangedCallbackFn fn) { g_sw_cb = fn; } /* USER CODE END 0 */
45
/* USER CODE BEGIN 4 */ void CheckSW() { static uint16_t count = 0; int raw_sw_pressed = !HAL_GPIO_ReadPin(SW_GPIO_Port, SW_Pin); if (g_sw_pressed == raw_sw_pressed) { // switch returned to previous state; reset counter count = 10; } else { if (--count == 0) { // switch state has stabilized g_sw_pressed = raw_sw_pressed; if (g_sw_cb) // invoke the callback, if registered (*g_sw_cb)(g_sw_pressed); } } } : /* USER CODE END 4 */
46
/* USER CODE BEGIN 0 */ : void MySwitchCb(int state) { printf("Switch state is %d\n", state); } /* USER CODE END 0 */ /* USER CODE BEGIN WHILE */ SetSwitchChangedCallback(MySwitchCb); while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */
47