Guest User

Untitled

a guest
Mar 13th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <gpio.h>
  2. #include <stdint.h>
  3.  
  4. #define EXTI_INTENR REGISTER(0x40010400)
  5. #define EXTI_RTENR REGISTER(0x40010408)
  6. #define EXTI_FTENR REGISTER(0x4001040C)
  7. #define EXTI_INTFR REGISTER(0x40010410)
  8. #define AFIO_EXTICR (*(volatile uint32_t *)(0x40010008))
  9.  
  10. #define P_PIN 3
  11.  
  12. void configure_pd3_interrupt() {
  13.   // Map EXTI line 3 to Port D (4 bits per line)
  14.   AFIO_EXTICR &= ~(0xF << (4 * 3));
  15.   AFIO_EXTICR |= (0x03 << (4 * 3));  // 0x03 for Port D
  16.  
  17.   // enable interrupt for gpio pin
  18.   EXTI_INTENR |= PIN(3);
  19.   EXTI_RTENR |= PIN(3);
  20.   EXTI_FTENR |= PIN(3);
  21.  
  22.   EXTI_INTFR &= ~PIN(3);
  23.   REGISTER(0xE000E100) |= PIN(20);  // Enable EXTI7_0 interrupt in PFIC (IRQ 20)
  24. }
  25.  
  26. int main(void) {
  27.   enable_APB2_peripheral(AFIOEN);  // enable alternative function
  28.   enable_gpio_port(PORT_D);        // enable port D
  29.  
  30.   gpio_set_mode(PORT_D, 3, GPIO_MODE_INPUT_FLOATING);
  31.   gpio_set_mode(PORT_D, 2, GPIO_MODE_OUTPUT_PP_50MHZ);
  32.  
  33.   configure_pd3_interrupt();
  34.  
  35.   while (1) {
  36.     uint32_t flag_register_value = EXTI_INTFR;
  37.     if (flag_register_value & PIN(3)) {
  38.       EXTI_INTFR &= ~PIN(3);
  39.       gpio_write(PORT_D, 2, 1);
  40.     }
  41.   }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment