STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Loading...
Searching...
No Matches
exti.c
Go to the documentation of this file.
1
5#include "exti.h"
6
7#include <stddef.h>
8
13
15#define EXTI_LINE_UNUSED (19U)
16
23static DriverStatus_e extiValidateLine(uint32_t line)
24{
26
27 if ((line > EXTI_LINE_MAX) || (line == EXTI_LINE_UNUSED))
28 {
30 }
31
32 return status;
33}
34
42static DriverStatus_e extiPortSelector(const GpioRegisters_t *port, uint32_t *selector)
43{
45
46 if (port == GPIOA)
47 {
48 *selector = SYSCFG_EXTICR_PA;
49 }
50 else if (port == GPIOB)
51 {
52 *selector = SYSCFG_EXTICR_PB;
53 }
54 else if (port == GPIOC)
55 {
56 *selector = SYSCFG_EXTICR_PC;
57 }
58 else if (port == GPIOD)
59 {
60 *selector = SYSCFG_EXTICR_PD;
61 }
62 else if (port == GPIOE)
63 {
64 *selector = SYSCFG_EXTICR_PE;
65 }
66 else if (port == GPIOF)
67 {
68 *selector = SYSCFG_EXTICR_PF;
69 }
70 else if (port == GPIOG)
71 {
72 *selector = SYSCFG_EXTICR_PG;
73 }
74 else if (port == GPIOH)
75 {
76 *selector = SYSCFG_EXTICR_PH;
77 }
78 else
79 {
81 }
82
83 return status;
84}
85
87{
88 uint32_t selector = 0U;
90
91 if (line > EXTI_GPIO_LINE_MAX)
92 {
94 }
95 else
96 {
97 status = extiPortSelector(port, &selector);
98 }
99
100 /* GPIOH has only pins 0 and 1. */
101 if ((status == DRIVER_STATUS_OK) && (selector == SYSCFG_EXTICR_PH) && (line > 1U))
102 {
104 }
105
106 if (status == DRIVER_STATUS_OK)
107 {
108 uint32_t index = line / 4U;
109 uint32_t shift = (line % 4U) * 4U;
110
111 syscfg->EXTICR[index] =
112 (syscfg->EXTICR[index] & ~((uint32_t)0xFU << shift)) | (selector << shift);
113 }
114
115 return status;
116}
117
119{
120 DriverStatus_e status = extiValidateLine(line);
121
122 if ((status == DRIVER_STATUS_OK) && (edge != EXTI_EDGE_RISING) && (edge != EXTI_EDGE_FALLING)
123 && (edge != EXTI_EDGE_BOTH))
124 {
126 }
127
128 if (status == DRIVER_STATUS_OK)
129 {
130 uint32_t bit = (uint32_t)1U << line;
131
132 if (edge == EXTI_EDGE_FALLING)
133 {
134 exti->RTSR &= ~bit;
135 }
136 else
137 {
138 exti->RTSR |= bit;
139 }
140
141 if (edge == EXTI_EDGE_RISING)
142 {
143 exti->FTSR &= ~bit;
144 }
145 else
146 {
147 exti->FTSR |= bit;
148 }
149 }
150
151 return status;
152}
153
155{
156 DriverStatus_e status = extiValidateLine(line);
157
158 if (status == DRIVER_STATUS_OK)
159 {
160 exti->IMR |= (uint32_t)1U << line;
161 }
162
163 return status;
164}
165
167{
168 DriverStatus_e status = extiValidateLine(line);
169
170 if (status == DRIVER_STATUS_OK)
171 {
172 exti->IMR &= ~((uint32_t)1U << line);
173 }
174
175 return status;
176}
177
179{
180 DriverStatus_e status = extiValidateLine(line);
181
182 if (status == DRIVER_STATUS_OK)
183 {
184 /* Only a 0-to-1 write triggers, so setting a bit that is already set is harmless. */
185 exti->SWIER |= (uint32_t)1U << line;
186 }
187
188 return status;
189}
190
191DriverStatus_e extiIsPending(const ExtiRegisters_t *exti, uint32_t line, bool *pending)
192{
193 DriverStatus_e status = extiValidateLine(line);
194
195 if ((status == DRIVER_STATUS_OK) && (pending == NULL))
196 {
198 }
199
200 if (status == DRIVER_STATUS_OK)
201 {
202 *pending = ((exti->PR & ((uint32_t)1U << line)) != 0U);
203 }
204
205 return status;
206}
207
209{
210 DriverStatus_e status = extiValidateLine(line);
211
212 if (status == DRIVER_STATUS_OK)
213 {
214 /* A store, not |=: PR is write-1-to-clear, and reading it back would
215 * write 1 to, and so clear, every other pending line. */
216 exti->PR = (uint32_t)1U << line;
217 }
218
219 return status;
220}
221
EXTI driver: route a GPIO pin to an EXTI line, choose its edges, and manage its interrupt and pending...
DriverStatus_e
Outcome of a driver call: a category for control flow, not a diagnosis.
@ DRIVER_STATUS_OK
Succeeded.
@ DRIVER_STATUS_ERR_INVALID_PARAM
A parameter is outside its valid domain.
#define EXTI_LINE_MAX
Highest EXTI line on the F446.
Definition exti.h:38
DRIVER_MUST_CHECK DriverStatus_e extiIsPending(const ExtiRegisters_t *exti, uint32_t line, bool *pending)
Report whether line's pending bit (PR) is set.
Definition exti.c:191
ExtiEdge_e
Which edges of a line's input set its pending bit.
Definition exti.h:43
DRIVER_MUST_CHECK DriverStatus_e extiTriggerSoftware(ExtiRegisters_t *exti, uint32_t line)
Raise line's interrupt from software (SWIER).
Definition exti.c:178
DRIVER_MUST_CHECK DriverStatus_e extiSelectPort(SyscfgRegisters_t *syscfg, uint32_t line, const GpioRegisters_t *port)
Route GPIO pin line of port to EXTI line line.
Definition exti.c:86
DRIVER_MUST_CHECK DriverStatus_e extiDisableInterrupt(ExtiRegisters_t *exti, uint32_t line)
Mask line's interrupt (IMR).
Definition exti.c:166
DRIVER_MUST_CHECK DriverStatus_e extiClearPending(ExtiRegisters_t *exti, uint32_t line)
Clear line's pending bit, and its SWIER bit with it.
Definition exti.c:208
DRIVER_MUST_CHECK DriverStatus_e extiEnableInterrupt(ExtiRegisters_t *exti, uint32_t line)
Unmask line's interrupt (IMR).
Definition exti.c:154
static DriverStatus_e extiPortSelector(const GpioRegisters_t *port, uint32_t *selector)
Map a GPIO port to its EXTICR selector.
Definition exti.c:42
static DriverStatus_e extiValidateLine(uint32_t line)
Accept a line that exists: 0 to EXTI_LINE_MAX, except 19.
Definition exti.c:23
#define EXTI_GPIO_LINE_MAX
Highest line fed by a GPIO pin.
Definition exti.h:39
DRIVER_MUST_CHECK DriverStatus_e extiConfigure(ExtiRegisters_t *exti, uint32_t line, ExtiEdge_e edge)
Set which edges trigger line; the other edge is cleared.
Definition exti.c:118
#define EXTI_LINE_UNUSED
The line with no source on the F446 (RM0390 section 10.2.5).
Definition exti.c:15
@ EXTI_EDGE_RISING
Rising edge only (RTSR).
Definition exti.h:44
@ EXTI_EDGE_BOTH
Both edges.
Definition exti.h:46
@ EXTI_EDGE_FALLING
Falling edge only (FTSR).
Definition exti.h:45
#define GPIOG
Port G.
Definition gpio_reg.h:54
#define GPIOC
Port C.
Definition gpio_reg.h:50
#define GPIOF
Port F.
Definition gpio_reg.h:53
#define GPIOB
Port B.
Definition gpio_reg.h:49
#define GPIOD
Port D.
Definition gpio_reg.h:51
#define GPIOA
Port A.
Definition gpio_reg.h:48
#define GPIOH
Port H.
Definition gpio_reg.h:55
#define GPIOE
Port E.
Definition gpio_reg.h:52
#define SYSCFG_EXTICR_PE
GPIOE.
Definition syscfg_reg.h:42
#define SYSCFG_EXTICR_PD
GPIOD.
Definition syscfg_reg.h:41
#define SYSCFG_EXTICR_PH
GPIOH.
Definition syscfg_reg.h:45
#define SYSCFG_EXTICR_PG
GPIOG.
Definition syscfg_reg.h:44
#define SYSCFG_EXTICR_PF
GPIOF.
Definition syscfg_reg.h:43
#define SYSCFG_EXTICR_PB
GPIOB.
Definition syscfg_reg.h:39
#define SYSCFG_EXTICR_PC
GPIOC.
Definition syscfg_reg.h:40
#define SYSCFG_EXTICR_PA
GPIOA.
Definition syscfg_reg.h:38
EXTI register block.
Definition exti_reg.h:23
volatile uint32_t SWIER
0x10: software interrupt trigger
Definition exti_reg.h:28
volatile uint32_t PR
0x14: pending, <span class="regaccess">rc_w1</span>
Definition exti_reg.h:29
volatile uint32_t FTSR
0x0C: falling-edge trigger
Definition exti_reg.h:27
volatile uint32_t IMR
0x00: interrupt mask (1 = enabled)
Definition exti_reg.h:24
volatile uint32_t RTSR
0x08: rising-edge trigger
Definition exti_reg.h:26
GPIO port register block.
Definition gpio_reg.h:33
SYSCFG register block.
Definition syscfg_reg.h:23
volatile uint32_t EXTICR[4]
0x08-0x14: EXTI line routing, 4 lines per register
Definition syscfg_reg.h:26