启用外部中断 ARM Cortex-M0+ (STM32G070)
Enable external interrupts ARM Cortex-M0+ (STM32G070)
我想对 STM32 微控制器的编程有一个基本的了解。我正在尝试通过设置所有适当的寄存器(无外部 libraries/definitions),使用按钮的外部中断来切换 LED 的状态。我似乎无法让中断例程工作,控制权从未传递给处理程序。
这是我用来将 PC-13(根据 Nucleo-G070RB 板连接了一个按钮)设置为外部中断的方法:
RCC_APBENR2 |= 0b1 << 0; // Enable SYSCFGEN
RCC_IOPENR |= 0b1 << 2; // Enable PORTC
EXTI_RTSR1 |= 0b1 << 13; // Enable interrupt on rising edge at EXTI line 13
EXTI_EXTICR4 |= 0x2 << 8; // Set PC-13 as GPIO pin for interrupt
EXTI_IMR1 |= 0x1 << 13; // Unmask EXTI line 13
NVIC_ISER |= 0b1 << 7; // Enable interrupts for EXTI4_15
我不确定我错过了什么。
完整代码如下:
#include <stdint.h>
#include <stdio.h>
#define RCC_IOPENR *((uint32_t volatile *) 0x40021034)
#define RCC_APBENR2 *((uint32_t volatile *) 0x40021040)
#define GPIOA_MODER *((uint32_t volatile *) 0x50000000)
#define GPIOA_IDR *((uint32_t volatile *) 0x50000010)
#define GPIOA_ODR *((uint32_t volatile *) 0x50000014)
#define GPIOC_MODER *((uint32_t volatile *) 0x50000800)
#define GPIOC_IDR *((uint32_t volatile *) 0x50000810)
#define GPIOC_ODR *((uint32_t volatile *) 0x50000814)
#define NVIC_ISER *((uint32_t volatile *) 0xE000E100)
#define EXTI_RTSR1 *((uint32_t volatile *) 0x40021800)
#define EXTI_EXTICR1 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 0))
#define EXTI_EXTICR2 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 1))
#define EXTI_EXTICR3 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 2))
#define EXTI_EXTICR4 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 3))
#define EXTI_IMR1 *((uint32_t volatile *) (0x40021800 + 0x080))
#define EXTI_RPR1 *((uint32_t volatile *) (0x40021800 + 0x00C))
void button_init();
extern void initialise_monitor_handles();
uint8_t volatile g_button_pressed = 0;
int main(void) {
//asm volatile ("cpsie i");
initialise_monitor_handles();
//printf("Hello World!\n");
RCC_IOPENR |= 0b1 << 0; // Enable PORTA
GPIOA_MODER &= ~(0b11 << (2*5)); // Clear MODE bits for 5th pin (PA-5)
GPIOA_MODER |= 0b1 << (2*5); // Set MODE bits for 5th pin (PA-5) to 01 (set output)
GPIOC_MODER &= ~(0b11 << (2*13)); // Clear MODE bits for 13th pin (PC-13) (set input)
button_init();
while(1) {
if (g_button_pressed) {
GPIOA_ODR ^= 0b1 << 5;
printf("Button pressed\n");
g_button_pressed = 0;
}
}
}
void button_init() {
RCC_APBENR2 |= 0b1 << 0; // Enable SYSCFGEN
RCC_IOPENR |= 0b1 << 2; // Enable PORTC
EXTI_RTSR1 |= 0b1 << 13; // Enable interrupt on rising edge at EXTI line 13
EXTI_EXTICR4 |= 0x2 << 8; // Set PC-13 as GPIO pin for interrupt
EXTI_IMR1 |= 0x1 << 13; // Unmask EXTI line 13
NVIC_ISER |= 0b1 << 7; // Enable interrupts for EXTI4_15
}
void EXTI4_15_IRQHandler(void) {
g_button_pressed = 1;
printf("Button pressed\n");
EXTI_RPR1 |= 0xFFFF << 0;
}
我将 STM32CubeIDE 与默认编译器一起使用,并使用 OpenOCD 通过半主机进行打印。
所以,我的问题是,我是漏掉了一步还是做错了什么?
所以,我想出了我的愚蠢错误是什么。我太专注于试图了解我在设置外部中断时哪里出错了,我没有真正检查我是否正确设置了按钮。
我尝试将 PC13 设置为输入:
GPIOC_MODER &= ~(0b11 << (2*13)); // Clear MODE bits for 13th pin (PC-13) (set input)
在我调用我的 button_init()
函数之前,它通过以下方式为 GPIOC 启用时钟:
RCC_IOPENR |= 0b1 << 2; // Enable PORTC
由于没有启用GPIOC,PC13也没有设置为输入,所以没有什么可检测的。
这是固定代码:(我还根据 P__J__
的建议将其更新为使用 CMSIS stm32 headers)
#define STM32G070xx
#include "stm32g0xx.h"
uint8_t volatile g_button_pressed = 0;
void button_init();
//extern void initialise_monitor_handles();
int main(void) {
// initialise_monitor_handles();
//printf("Hello World!\n");
RCC->IOPENR |= 0b1 << 0; // Enable PORTA
GPIOA->MODER &= ~(0b11 << (2*5)); // Clear MODE bits for 5th pin (PA-5)
GPIOA->MODER |= 0b1 << (2*5); // Set MODE bits for 5th pin (PA-5) to 01 (set output)
button_init();
while(1) {
if (g_button_pressed) {
GPIOA->ODR ^= 0b1 << 5;
// printf("Button pressed! Yay!\n");
g_button_pressed = 0;
}
}
}
void button_init() {
// RCC->APBENR2 |= (uint32_t) (0b1 << 0); // Enable SYSCFGEN
RCC->IOPENR |= (uint32_t) (0b1 << 2); // Enable PORTC
GPIOC->MODER &= ~(0b11 << (2*13)); // Clear MODE bits for 13th pin (PC-13) (set input)
EXTI->RTSR1 |= (uint32_t) (0b1 << 13); // Enable interrupt on rising edge at EXTI line 13
EXTI->EXTICR[3] |= (uint32_t) (0x2 << 8); // Set PC-13 as GPIO pin for interrupt
EXTI->IMR1 |= (uint32_t) (0x1 << 13); // Unmask EXTI line 13
NVIC->ISER[0] |= (uint32_t) (0b1 << 7); // Enable interrupts for EXTI4_15
}
void EXTI4_15_IRQHandler(void) {
g_button_pressed = 1;
// printf("Button pressed\n");
EXTI->RPR1 = 0b1 << 13;
}
我想对 STM32 微控制器的编程有一个基本的了解。我正在尝试通过设置所有适当的寄存器(无外部 libraries/definitions),使用按钮的外部中断来切换 LED 的状态。我似乎无法让中断例程工作,控制权从未传递给处理程序。
这是我用来将 PC-13(根据 Nucleo-G070RB 板连接了一个按钮)设置为外部中断的方法:
RCC_APBENR2 |= 0b1 << 0; // Enable SYSCFGEN
RCC_IOPENR |= 0b1 << 2; // Enable PORTC
EXTI_RTSR1 |= 0b1 << 13; // Enable interrupt on rising edge at EXTI line 13
EXTI_EXTICR4 |= 0x2 << 8; // Set PC-13 as GPIO pin for interrupt
EXTI_IMR1 |= 0x1 << 13; // Unmask EXTI line 13
NVIC_ISER |= 0b1 << 7; // Enable interrupts for EXTI4_15
我不确定我错过了什么。
完整代码如下:
#include <stdint.h>
#include <stdio.h>
#define RCC_IOPENR *((uint32_t volatile *) 0x40021034)
#define RCC_APBENR2 *((uint32_t volatile *) 0x40021040)
#define GPIOA_MODER *((uint32_t volatile *) 0x50000000)
#define GPIOA_IDR *((uint32_t volatile *) 0x50000010)
#define GPIOA_ODR *((uint32_t volatile *) 0x50000014)
#define GPIOC_MODER *((uint32_t volatile *) 0x50000800)
#define GPIOC_IDR *((uint32_t volatile *) 0x50000810)
#define GPIOC_ODR *((uint32_t volatile *) 0x50000814)
#define NVIC_ISER *((uint32_t volatile *) 0xE000E100)
#define EXTI_RTSR1 *((uint32_t volatile *) 0x40021800)
#define EXTI_EXTICR1 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 0))
#define EXTI_EXTICR2 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 1))
#define EXTI_EXTICR3 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 2))
#define EXTI_EXTICR4 *((uint32_t volatile *) (0x40021800 + 0x060 + 0x4 * 3))
#define EXTI_IMR1 *((uint32_t volatile *) (0x40021800 + 0x080))
#define EXTI_RPR1 *((uint32_t volatile *) (0x40021800 + 0x00C))
void button_init();
extern void initialise_monitor_handles();
uint8_t volatile g_button_pressed = 0;
int main(void) {
//asm volatile ("cpsie i");
initialise_monitor_handles();
//printf("Hello World!\n");
RCC_IOPENR |= 0b1 << 0; // Enable PORTA
GPIOA_MODER &= ~(0b11 << (2*5)); // Clear MODE bits for 5th pin (PA-5)
GPIOA_MODER |= 0b1 << (2*5); // Set MODE bits for 5th pin (PA-5) to 01 (set output)
GPIOC_MODER &= ~(0b11 << (2*13)); // Clear MODE bits for 13th pin (PC-13) (set input)
button_init();
while(1) {
if (g_button_pressed) {
GPIOA_ODR ^= 0b1 << 5;
printf("Button pressed\n");
g_button_pressed = 0;
}
}
}
void button_init() {
RCC_APBENR2 |= 0b1 << 0; // Enable SYSCFGEN
RCC_IOPENR |= 0b1 << 2; // Enable PORTC
EXTI_RTSR1 |= 0b1 << 13; // Enable interrupt on rising edge at EXTI line 13
EXTI_EXTICR4 |= 0x2 << 8; // Set PC-13 as GPIO pin for interrupt
EXTI_IMR1 |= 0x1 << 13; // Unmask EXTI line 13
NVIC_ISER |= 0b1 << 7; // Enable interrupts for EXTI4_15
}
void EXTI4_15_IRQHandler(void) {
g_button_pressed = 1;
printf("Button pressed\n");
EXTI_RPR1 |= 0xFFFF << 0;
}
我将 STM32CubeIDE 与默认编译器一起使用,并使用 OpenOCD 通过半主机进行打印。
所以,我的问题是,我是漏掉了一步还是做错了什么?
所以,我想出了我的愚蠢错误是什么。我太专注于试图了解我在设置外部中断时哪里出错了,我没有真正检查我是否正确设置了按钮。
我尝试将 PC13 设置为输入:
GPIOC_MODER &= ~(0b11 << (2*13)); // Clear MODE bits for 13th pin (PC-13) (set input)
在我调用我的 button_init()
函数之前,它通过以下方式为 GPIOC 启用时钟:
RCC_IOPENR |= 0b1 << 2; // Enable PORTC
由于没有启用GPIOC,PC13也没有设置为输入,所以没有什么可检测的。
这是固定代码:(我还根据 P__J__
的建议将其更新为使用 CMSIS stm32 headers)
#define STM32G070xx
#include "stm32g0xx.h"
uint8_t volatile g_button_pressed = 0;
void button_init();
//extern void initialise_monitor_handles();
int main(void) {
// initialise_monitor_handles();
//printf("Hello World!\n");
RCC->IOPENR |= 0b1 << 0; // Enable PORTA
GPIOA->MODER &= ~(0b11 << (2*5)); // Clear MODE bits for 5th pin (PA-5)
GPIOA->MODER |= 0b1 << (2*5); // Set MODE bits for 5th pin (PA-5) to 01 (set output)
button_init();
while(1) {
if (g_button_pressed) {
GPIOA->ODR ^= 0b1 << 5;
// printf("Button pressed! Yay!\n");
g_button_pressed = 0;
}
}
}
void button_init() {
// RCC->APBENR2 |= (uint32_t) (0b1 << 0); // Enable SYSCFGEN
RCC->IOPENR |= (uint32_t) (0b1 << 2); // Enable PORTC
GPIOC->MODER &= ~(0b11 << (2*13)); // Clear MODE bits for 13th pin (PC-13) (set input)
EXTI->RTSR1 |= (uint32_t) (0b1 << 13); // Enable interrupt on rising edge at EXTI line 13
EXTI->EXTICR[3] |= (uint32_t) (0x2 << 8); // Set PC-13 as GPIO pin for interrupt
EXTI->IMR1 |= (uint32_t) (0x1 << 13); // Unmask EXTI line 13
NVIC->ISER[0] |= (uint32_t) (0b1 << 7); // Enable interrupts for EXTI4_15
}
void EXTI4_15_IRQHandler(void) {
g_button_pressed = 1;
// printf("Button pressed\n");
EXTI->RPR1 = 0b1 << 13;
}