Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
buttons_irq.c
Go to the documentation of this file.
1#include "buttons.h"
2#include "rit.h"
3
4#include <LPC17xx.h>
5#include <stdbool.h>
6#include <stdlib.h>
7
8// PRIVATE TYPES
9
15
21
24
27
30{
31 if (eint0_down)
32 {
33 eint0_down++;
34
35 // If P2.10 is low, button is pressed
36 if ((LPC_GPIO2->FIOPIN & (1 << 10)) == 0)
37 {
38 if (eint0_down == 2 && handlers[BTN_SRC_EINT0].handler)
40 }
41 else
42 {
43 eint0_down = 0;
44 NVIC_EnableIRQ(EINT0_IRQn); // Enabling the interrupt again
45 SET_BIT(LPC_PINCON->PINSEL4, 20); // Set P2.10 back to 01 (EINT0)
46 }
47 }
48
49 if (eint1_down)
50 {
51 eint1_down++;
52 if ((LPC_GPIO2->FIOPIN & (1 << 11)) == 0)
53 {
54 if (eint1_down == 2 && handlers[BTN_SRC_EINT1].handler)
56 }
57 else
58 {
59 eint1_down = 0;
60 NVIC_EnableIRQ(EINT1_IRQn); // Enabling the interrupt again
61 SET_BIT(LPC_PINCON->PINSEL4, 22); // Set P2.11 back to 01 (EINT1)
62 }
63 }
64
65 if (eint2_down)
66 {
67 eint2_down++;
68 if ((LPC_GPIO2->FIOPIN & (1 << 12)) == 0)
69 {
70 if (eint2_down == 2 && handlers[BTN_SRC_EINT2].handler)
72 }
73 else
74 {
75 eint2_down = 0;
76 NVIC_EnableIRQ(EINT2_IRQn); // Enabling the interrupt again
77 SET_BIT(LPC_PINCON->PINSEL4, 24); // Set P2.12 back to 01 (EINT2)
78 }
79 }
80}
81
82// PUBLIC FUNCTIONS
83
85{
86 if (!handlers[source].handler)
87 return BTN_ERR_NO_HANDLER_TO_ENABLE; // No handler associated
88
89 handlers[source].source_enabled = true;
90
91 switch (source)
92 {
93 case BTN_SRC_EINT0:
94 NVIC_EnableIRQ(EINT0_IRQn);
95 if (!IS_DEF_PRIORITY(int_priority) && IS_BETWEEN_EQ(int_priority, 0, 15))
96 NVIC_SetPriority(EINT0_IRQn, int_priority);
97 else
99 break;
100 case BTN_SRC_EINT1:
101 NVIC_EnableIRQ(EINT1_IRQn);
102 if (!IS_DEF_PRIORITY(int_priority) && IS_BETWEEN_EQ(int_priority, 0, 15))
103 NVIC_SetPriority(EINT1_IRQn, int_priority);
104 else
106 break;
107 case BTN_SRC_EINT2:
108 NVIC_EnableIRQ(EINT2_IRQn);
109 if (!IS_DEF_PRIORITY(int_priority) && IS_BETWEEN_EQ(int_priority, 0, 15))
110 NVIC_SetPriority(EINT2_IRQn, int_priority);
111 else
113 break;
114 default:
116 }
117
118 return BTN_ERR_OK;
119}
120
122{
123 if (!handlers[source].handler)
124 return; // No handler associated
125
126 handlers[source].source_enabled = false;
127}
128
130{
132 .handler = func,
133 .source_enabled = false,
134 };
135}
136
138{
140 .handler = NULL,
141 .source_enabled = false,
142 };
143}
144
145// INTERRUPT HANDLERS
146
148extern void EINT0_IRQHandler(void)
149{
150 if (!handlers[BTN_SRC_EINT0].source_enabled || !handlers[BTN_SRC_EINT0].handler)
151 return;
152
153 if (debouncer_on)
154 {
155 eint0_down = 1;
156 NVIC_DisableIRQ(EINT0_IRQn);
157 CLR_BIT(LPC_PINCON->PINSEL4, 20); // Set P2.10 to 00 (GPIO, previously EINT0)
158 }
159 else
161
162 LPC_SC->EXTINT &= 1; // Clear the interrupt flag
163}
164
166extern void EINT1_IRQHandler(void)
167{
168 if (!handlers[BTN_SRC_EINT1].source_enabled || !handlers[BTN_SRC_EINT1].handler)
169 return;
170
171 if (debouncer_on)
172 {
173 eint1_down = 1;
174 NVIC_DisableIRQ(EINT1_IRQn);
175 CLR_BIT(LPC_PINCON->PINSEL4, 22); // Set P2.11 to 00 (GPIO, previously EINT1)
176 }
177 else
179
180 LPC_SC->EXTINT &= 1 << 1; // Clear the interrupt flag
181}
182
184extern void EINT2_IRQHandler(void)
185{
186 if (!handlers[BTN_SRC_EINT2].source_enabled || !handlers[BTN_SRC_EINT1].handler)
187 return;
188
189 if (debouncer_on)
190 {
191 eint2_down = 1;
192 NVIC_DisableIRQ(EINT2_IRQn);
193 CLR_BIT(LPC_PINCON->PINSEL4, 24); // Set P2.12 to 00 (GPIO, previously EINT2)
194 }
195 else
197
198 LPC_SC->EXTINT &= 1 << 2; // Clear the interrupt flag
199}
BUTTON_Source
@ BTN_SRC_EINT1
EINT1: First button from the left.
@ BTN_SRC_EINT0
INT0: Third button from the left (next to RES)
@ BTN_SRC_EINT2
EINT2: Second button from the left.
@ BTN_SRC_COUNT
void(* BUTTON_Function)(void)
BUTTON_Error
@ BTN_ERR_INT_PRIO_INVALID
An invalid priority was specified (must be 0 <= prio <= 15). This is not necessarily an error,...
@ BTN_ERR_NO_HANDLER_TO_ENABLE
It was requested to enable a button interrupt, but no function was bound to it.
@ BTN_ERR_OK
No error.
void BUTTON_SetFunction(BUTTON_Source source, BUTTON_Function func)
Binds a given interrupt to a functionality.
BUTTON_Error BUTTON_EnableSource(BUTTON_Source source, u8 int_priority)
Enables the interrupt generation of the given source, provided that there's a function associated to ...
Definition buttons_irq.c:84
void EINT0_IRQHandler(void)
_DECL_EXTERNALLY bool debouncer_on
Flag to indicate if the debouncer is currently active.
Definition buttons_irq.c:26
void EINT1_IRQHandler(void)
void EINT2_IRQHandler(void)
_PRIVATE u8 eint2_down
Definition buttons_irq.c:23
void handle_debouncing(void)
RIT job for managing debouncing.
Definition buttons_irq.c:29
void BUTTON_UnsetFunction(BUTTON_Source source)
Unbinds the previously set function from the selected interrupt source.
_PRIVATE InterruptHandlerWrapper handlers[BTN_SRC_COUNT]
Array of function pointers to the interrupt handlers for each BUTTON peripheral. This is defined rega...
Definition buttons_irq.c:20
_PRIVATE u8 eint0_down
Flag to indicate if the button is currently pressed. Used for debouncing.
Definition buttons_irq.c:23
_PRIVATE u8 eint1_down
Definition buttons_irq.c:23
void BUTTON_DisableSource(BUTTON_Source source)
Disables the interrupt generation of the given source, but it leaves the function binding.
BUTTON_Function handler
Definition buttons_irq.c:12
#define CLR_BIT(reg, bit)
Definition types.h:27
#define SET_BIT(reg, bit)
Definition types.h:26
#define IS_DEF_PRIORITY(prio)
Definition types.h:42
uint8_t u8
Definition types.h:8
#define _PRIVATE
Definition types.h:37
#define IS_BETWEEN_EQ(value, low, hi)
Definition types.h:23
#define _DECL_EXTERNALLY
Definition types.h:36