~ruther/uni-mam-arm

ref: 415b8c65ff282fadc6bb5629c301448a8259dc5c uni-mam-arm/arm07/src/exti.c -rw-r--r-- 1.9 KiB
415b8c65 — Rutherther feat(arm07): add exti both edges interrupt function 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdint.h>
#include <stdlib.h>

#include <stm32f4xx.h>
#include "exti.h"
#include "registers.h"

void exti_init(exti_t* exti, uint8_t line, EXTI_TypeDef *exti_periph, SYSCFG_TypeDef *syscfg) {
  exti->exti = exti_periph;
  exti->syscfg = syscfg;
  exti->line = line;
}

void exti_external_interrupt(exti_t *exti, uint8_t gpio) {
  uint8_t index = exti->line >> 2;
  uint8_t pos = (exti->line & 0x3) * 4;
  volatile uint32_t *exticr = exti->syscfg->EXTICR;
  reg_write_bits_pos(exticr + index, gpio, pos, 0x0F);
}

void exti_rising_interrupt(exti_t *exti) {
  reg_write_bits_pos(&exti->exti->RTSR, 1, exti->line, 1);
  reg_write_bits_pos(&exti->exti->FTSR, 0, exti->line, 1);
}

void exti_both_edges_interrupt(exti_t *exti) {
  reg_write_bits_pos(&exti->exti->RTSR, 1, exti->line, 1);
  reg_write_bits_pos(&exti->exti->FTSR, 1, exti->line, 1);
}

void exti_falling_interrupt(exti_t *exti) {
  reg_write_bits_pos(&exti->exti->FTSR, 1, exti->line, 1);
  reg_write_bits_pos(&exti->exti->RTSR, 0, exti->line, 1);
}

void exti_disable_interrupt(exti_t *exti) {
  reg_write_bits_pos(&exti->exti->IMR, 0, exti->line, 1);
}

IRQn_Type exti_irq_idx(uint8_t line) {
  switch (line) {
  case 0:
    return EXTI0_IRQn;
  case 1:
    return EXTI1_IRQn;
  case 2:
    return EXTI2_IRQn;
  case 3:
    return EXTI3_IRQn;
  case 4:
    return EXTI4_IRQn;
  case 5:
  case 6:
  case 7:
  case 8:
  case 9:
    return EXTI9_5_IRQn;
  case 10:
  case 11:
  case 12:
  case 13:
  case 14:
  case 15:
    return EXTI15_10_IRQn;
  default:
    return -1;
  }
}

void exti_enable_interrupt(exti_t *exti) {
  reg_write_bits_pos(&exti->exti->IMR, 1, exti->line, 1);

  IRQn_Type idx = exti_irq_idx(exti->line);
  NVIC_SetPriority(idx, 2);
  NVIC_EnableIRQ(idx);
}

uint32_t exti_is_interrupt(exti_t *exti) {
  return exti->exti->PR & (EXTI_PR_PR0 << exti->line);
}

void exti_clear_interrupt(exti_t *exti) {
  exti->exti->PR |= (EXTI_PR_PR0 << exti->line);
}
Do not follow this link