Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
buttons_irq.c File Reference
#include "buttons.h"
#include "rit.h"
#include <LPC17xx.h>
#include <stdbool.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  InterruptHandlerWrapper
 

Functions

void handle_debouncing (void)
 RIT job for managing debouncing.
 
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 it.
 
void BUTTON_DisableSource (BUTTON_Source source)
 Disables the interrupt generation of the given source, but it leaves the function binding.
 
void BUTTON_SetFunction (BUTTON_Source source, BUTTON_Function func)
 Binds a given interrupt to a functionality.
 
void BUTTON_UnsetFunction (BUTTON_Source source)
 Unbinds the previously set function from the selected interrupt source.
 
void EINT0_IRQHandler (void)
 
void EINT1_IRQHandler (void)
 
void EINT2_IRQHandler (void)
 

Variables

_PRIVATE InterruptHandlerWrapper handlers [BTN_SRC_COUNT] = {NULL}
 Array of function pointers to the interrupt handlers for each BUTTON peripheral. This is defined regardless of the debouncing option, since the handlers will be used by the debouncing code as well. Hence the array is marked _USED_EXTERNALLY.
 
_PRIVATE u8 eint0_down
 Flag to indicate if the button is currently pressed. Used for debouncing.
 
_PRIVATE u8 eint1_down
 
_PRIVATE u8 eint2_down
 
_DECL_EXTERNALLY bool debouncer_on
 Flag to indicate if the debouncer is currently active.
 

Function Documentation

◆ BUTTON_DisableSource()

void BUTTON_DisableSource ( BUTTON_Source  source)

Disables the interrupt generation of the given source, but it leaves the function binding.

Parameters
sourceTHe source to disable.

Definition at line 121 of file buttons_irq.c.

122{
123 if (!handlers[source].handler)
124 return; // No handler associated
125
126 handlers[source].source_enabled = false;
127}
_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

◆ BUTTON_EnableSource()

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 it.

Parameters
sourceThe source to enable.
int_priorityThe priority to assign to the specified source.

Definition at line 84 of file buttons_irq.c.

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}
@ 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_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.
#define IS_DEF_PRIORITY(prio)
Definition types.h:42
#define IS_BETWEEN_EQ(value, low, hi)
Definition types.h:23

◆ BUTTON_SetFunction()

void BUTTON_SetFunction ( BUTTON_Source  source,
BUTTON_Function  func 
)

Binds a given interrupt to a functionality.

Parameters
sourceButton interrupt source (enum BUTTON_Source)
handlerFunction pointer to the interrupt handler.
int_priorityThe priority to assign to the interrupts coming from the selected source.
enableWhether to enable the function binding right away.

Definition at line 129 of file buttons_irq.c.

130{
132 .handler = func,
133 .source_enabled = false,
134 };
135}
BUTTON_Function handler
Definition buttons_irq.c:12

◆ BUTTON_UnsetFunction()

void BUTTON_UnsetFunction ( BUTTON_Source  source)

Unbinds the previously set function from the selected interrupt source.

Parameters
sourceInterrupt source (enum BUTTON_Source)

Definition at line 137 of file buttons_irq.c.

138{
140 .handler = NULL,
141 .source_enabled = false,
142 };
143}

◆ EINT0_IRQHandler()

void EINT0_IRQHandler ( void  )
extern
Note
3rd button on the board (next to RESET)

Definition at line 148 of file buttons_irq.c.

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}
_DECL_EXTERNALLY bool debouncer_on
Flag to indicate if the debouncer is currently active.
Definition buttons_irq.c:26
_PRIVATE u8 eint0_down
Flag to indicate if the button is currently pressed. Used for debouncing.
Definition buttons_irq.c:23
#define CLR_BIT(reg, bit)
Definition types.h:27

◆ EINT1_IRQHandler()

void EINT1_IRQHandler ( void  )
extern
Note
Left-most button on the board

Definition at line 166 of file buttons_irq.c.

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}
_PRIVATE u8 eint1_down
Definition buttons_irq.c:23

◆ EINT2_IRQHandler()

void EINT2_IRQHandler ( void  )
extern
Note
2nd button from the left on the board

Definition at line 184 of file buttons_irq.c.

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}
_PRIVATE u8 eint2_down
Definition buttons_irq.c:23

◆ handle_debouncing()

void handle_debouncing ( void  )

RIT job for managing debouncing.

RIT job for handling debouncing. Defined & used in button_irq.c.

Definition at line 29 of file buttons_irq.c.

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}
#define SET_BIT(reg, bit)
Definition types.h:26

Variable Documentation

◆ debouncer_on

_DECL_EXTERNALLY bool debouncer_on

Flag to indicate if the debouncer is currently active.

Definition at line 26 of file buttons_irq.c.

◆ eint0_down

_PRIVATE u8 eint0_down

Flag to indicate if the button is currently pressed. Used for debouncing.

Definition at line 23 of file buttons_irq.c.

◆ eint1_down

_PRIVATE u8 eint1_down

Definition at line 23 of file buttons_irq.c.

◆ eint2_down

_PRIVATE u8 eint2_down

Definition at line 23 of file buttons_irq.c.

◆ handlers

Array of function pointers to the interrupt handlers for each BUTTON peripheral. This is defined regardless of the debouncing option, since the handlers will be used by the debouncing code as well. Hence the array is marked _USED_EXTERNALLY.

Note
This array is updated by BUTTON_SetInterruptHandler() and BUTTON_UnsetInterruptHandler().

Definition at line 20 of file buttons_irq.c.

20{NULL};