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

Go to the source code of this file.

Functions

_DECL_EXTERNALLY void handle_debouncing (void)
 RIT job for handling debouncing. Defined & used in button_irq.c.
 
BUTTON_Error BUTTON_Init (u8 options)
 Initializes the BUTTON peripherals.
 
void BUTTON_Deinit (void)
 Deinitializes the buttons, by removing all jobs, disabling the interrupts, and removing the debouncing job from the RIT.
 

Variables

_USED_EXTERNALLY bool debouncer_on = false
 Flag to indicate if the debouncer is currently active. Used in button_irq.c.
 

Function Documentation

◆ BUTTON_Deinit()

void BUTTON_Deinit ( void  )

Deinitializes the buttons, by removing all jobs, disabling the interrupts, and removing the debouncing job from the RIT.

Definition at line 43 of file buttons.c.

44{
45 // Disabling EINT[0..2] in PINSEL4. In the table, these PINS behave
46 // like EINTs when set to 01.
47 LPC_PINCON->PINSEL4 &= ~(0b010101 << 20);
48 LPC_SC->EXTMODE = 0x0;
49 LPC_SC->EXTPOLAR = 0x0;
50
51 NVIC_DisableIRQ(EINT0_IRQn);
52 NVIC_DisableIRQ(EINT1_IRQn);
53 NVIC_DisableIRQ(EINT2_IRQn);
54
55 if (debouncer_on)
56 {
57 debouncer_on = false;
59 }
60}
_DECL_EXTERNALLY void handle_debouncing(void)
RIT job for handling debouncing. Defined & used in button_irq.c.
Definition buttons_irq.c:29
_USED_EXTERNALLY bool debouncer_on
Flag to indicate if the debouncer is currently active. Used in button_irq.c.
Definition buttons.c:8
RIT_Error RIT_RemoveJob(RIT_Job job)
Removes a job from the RIT interrupt handler job queue.
Definition rit_job.c:117

◆ BUTTON_Init()

BUTTON_Error BUTTON_Init ( u8  options)

Initializes the BUTTON peripherals.

Parameters
optionsButton options from the BUTTON_Config enum
Returns
BUTTON_Error The error.

Definition at line 15 of file buttons.c.

16{
17 // Enabling EINT[0..2] in PINSEL4. In the table, these PINS behave
18 // like EINTs when set to 01.
19 LPC_PINCON->PINSEL4 |= (0b010101 << 20);
20
21 // EITs mapped to PINs 2.[10..12]. Need to set them to 0, i.e. input.
22 LPC_GPIO2->FIODIR &= ~(0b111 << 10);
23
24 // Setting how EINTs are raised using EXTMODE in SC. Setting 1 to the
25 // bottom 3 bits to set interrupt edge sensitive. Since we're not modifying
26 // EXTPOLAR in SC, ints are raised on FALLING EDGE.
27 LPC_SC->EXTMODE = 0x7;
28 LPC_SC->EXTPOLAR = 0x0;
29
30 if (options & BTN_DEBOUNCE_WITH_RIT)
31 {
32 if (!RIT_IsEnabled())
33 return BTN_ERR_RIT_UNINIT;
34
35 debouncer_on = true;
38 }
39
40 return BTN_ERR_OK;
41}
@ BTN_DEBOUNCE_WITH_RIT
Definition button_types.h:6
@ BTN_ERR_RIT_UNINIT
During initialization, DEBOUNCE_WITH_RIT was requested but the RIT was not enabled.
@ BTN_ERR_OK
No error.
RIT_Error RIT_EnableJob(RIT_Job job)
Include this job in the RIT handler queue.
Definition rit_job.c:65
RIT_Error RIT_AddJob(RIT_Job job, u8 multiplier_factor)
Definition rit_job.c:104
bool RIT_IsEnabled(void)
Returns whether the RIT is counting or not.
Definition rit.c:60
#define RIT_NO_DIVIDER
Tells the RIT that the specified job needs to be executed at exactly the interval of interrupts chose...
Definition rit_types.h:8

◆ handle_debouncing()

_DECL_EXTERNALLY void handle_debouncing ( void  )

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

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}
@ 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.
_PRIVATE u8 eint2_down
Definition buttons_irq.c:23
_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
BUTTON_Function handler
Definition buttons_irq.c:12
#define SET_BIT(reg, bit)
Definition types.h:26

Variable Documentation

◆ debouncer_on

_USED_EXTERNALLY bool debouncer_on = false

Flag to indicate if the debouncer is currently active. Used in button_irq.c.

Definition at line 8 of file buttons.c.