Embedded System Design 2 - Project
interrupt.c File Reference

Interrupt functionality. More...

#include <stdint.h>
#include <stdbool.h>
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_rtc.h"
#include "interrupt.h"
#include "pin_mapping.h"
#include "debug_dbprint.h"
#include "util.h"
#include "ADXL362.h"

Go to the source code of this file.

Functions

void initGPIOwakeup (void)
 Initialize GPIO wake-up functionality. More...
 
bool BTN_getTriggered (uint8_t number)
 Getter for the PB0_triggered and PB1_triggered variables. More...
 
void BTN_setTriggered (uint8_t number, bool value)
 Setter for the PB0_triggered and PB1_triggered variable. More...
 
void GPIO_EVEN_IRQHandler (void)
 GPIO Even IRQ for pushbuttons on even-numbered pins. More...
 
void GPIO_ODD_IRQHandler (void)
 GPIO Odd IRQ for pushbuttons on odd-numbered pins. More...
 

Variables

volatile bool PB0_triggered = false
 
volatile bool PB1_triggered = false
 

Detailed Description

Interrupt functionality.

Version
3.1
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Started from https://github.com/Fescron/Project-LabEmbeddedDesign1
  • v1.1: Added dbprintln(""); above dbinfo statements in IRQ handlers to fix overwriting of text.
  • v1.2: Disabled the RTC counter if GPIO handlers are called, only added necessary includes in header file, moved the others to the source file, updated documentation.
  • v1.3: Started using set method for the static variable ADXL_triggered, added GPIO wakeup initialization method here, renamed file.
  • v1.4: Stopped disabling the GPIO clock.
  • v1.5: Started using getters/setters to indicate an interrupt to main.c.
  • v1.6: Moved IRQ handler of RTC to this delay.c.
  • v1.7: Updated clear pending interrupt logic.
  • v1.8: Updated code with new DEFINE checks.
  • v1.9: Removed error calls for "unknown" pins and added flag check for custom Happy Gecko board pinout.
  • v2.0: Stopped disabling the RTC counter on pin interrupts.
  • v2.1: Disabled the RTC counter on a button push because it confused delays called in LoRaWAN code.
  • v2.2: Changed error numbering.
  • v3.0: Updated version number.
  • v3.1: Removed static before the local variables (not necessary).

Note
Other interrupt handlers can be found in delay.c and adc.c.

License

Copyright (C) 2019 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file interrupt.c.

Function Documentation

◆ BTN_getTriggered()

bool BTN_getTriggered ( uint8_t  number)

Getter for the PB0_triggered and PB1_triggered variables.

Parameters
[in]number
  • 0 - PB0_triggered selected.
  • 1 - PB1_triggered selected.
Returns
The value of PB0_triggered or PB1_triggered.

Definition at line 145 of file interrupt.c.

146 {
147  if (number == 0) return (PB0_triggered);
148  else if (number == 1) return (PB1_triggered);
149  else
150  {
151 
152 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
153  dbcrit("Non-existing button selected!");
154 #endif /* DEBUG_DBPRINT */
155 
156  error(18);
157 
158  return (false);
159  }
160 }
volatile bool PB0_triggered
Definition: interrupt.c:79
volatile bool PB1_triggered
Definition: interrupt.c:80
void error(uint8_t number)
Error method.
Definition: util.c:131
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ BTN_setTriggered()

void BTN_setTriggered ( uint8_t  number,
bool  value 
)

Setter for the PB0_triggered and PB1_triggered variable.

Parameters
[in]number
  • 0 - PB0_triggered selected.
  • 1 - PB1_triggered selected.
[in]valueThe boolean value to set to the selected variable.

Definition at line 174 of file interrupt.c.

175 {
176  if (number == 0) PB0_triggered = value;
177  else if (number == 1) PB1_triggered = value;
178  else
179  {
180 
181 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
182  dbcrit("Non-existing button selected!");
183 #endif /* DEBUG_DBPRINT */
184 
185  error(19);
186  }
187 }
volatile bool PB0_triggered
Definition: interrupt.c:79
volatile bool PB1_triggered
Definition: interrupt.c:80
void error(uint8_t number)
Error method.
Definition: util.c:131
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ GPIO_EVEN_IRQHandler()

void GPIO_EVEN_IRQHandler ( void  )

GPIO Even IRQ for pushbuttons on even-numbered pins.

The RTC is also disabled on a button press (manual wake-up)

Note
The weak definition for this method is located in system_efm32hg.h.

Definition at line 200 of file interrupt.c.

201 {
202  /* Read interrupt flags */
203  uint32_t flags = GPIO_IntGet();
204 
205  /* Check if PB1 is pushed */
206  if (flags == 0x400)
207  {
208  /* Disable the counter (manual wakeup) */
209  RTC_Enable(false);
210 
211  PB1_triggered = true;
212  }
213 
214  /* Clear all even pin interrupt flags */
215  GPIO_IntClear(0x5555);
216 }
volatile bool PB1_triggered
Definition: interrupt.c:80

◆ GPIO_ODD_IRQHandler()

void GPIO_ODD_IRQHandler ( void  )

GPIO Odd IRQ for pushbuttons on odd-numbered pins.

The RTC is also disabled on a button press (manual wakeup).

Note
The weak definition for this method is located in system_efm32hg.h.

Definition at line 229 of file interrupt.c.

230 {
231  /* Read interrupt flags */
232  uint32_t flags = GPIO_IntGet();
233 
234  /* Check if PB0 is pushed */
235  if (flags == 0x200)
236  {
237  /* Disable the counter (manual wake-up) */
238  RTC_Enable(false);
239 
240  PB0_triggered = true;
241  }
242 
243  /* Check if INT1 is triggered */
244 #if CUSTOM_BOARD == 1 /* Custom Happy Gecko pinout */
245  if (flags == 0x8) ADXL_setTriggered(true);
246 #else /* Regular Happy Gecko pinout */
247  if (flags == 0x80) ADXL_setTriggered(true);
248 #endif /* Board pinout selection */
249 
250  /* Clear all odd pin interrupt flags */
251  GPIO_IntClear(0xAAAA);
252 }
volatile bool PB0_triggered
Definition: interrupt.c:79
void ADXL_setTriggered(bool triggered)
Setter for the ADXL_triggered variable.
Definition: ADXL362.c:202

◆ initGPIOwakeup()

void initGPIOwakeup ( void  )

Initialize GPIO wake-up functionality.

Initialize buttons PB0 and PB1 on falling-edge interrupts and ADXL_INT1 on rising-edge interrupts.

Definition at line 91 of file interrupt.c.

92 {
93  /* Enable necessary clocks (just in case) */
94  CMU_ClockEnable(cmuClock_HFPER, true); /* GPIO is a High Frequency Peripheral */
95  CMU_ClockEnable(cmuClock_GPIO, true);
96 
97  /* Configure PB0 and PB1 as input with glitch filter enabled, last argument sets pull direction */
98  GPIO_PinModeSet(PB0_PORT, PB0_PIN, gpioModeInputPullFilter, 1);
99  GPIO_PinModeSet(PB1_PORT, PB1_PIN, gpioModeInputPullFilter, 1);
100 
101  /* Configure ADXL_INT1 as input, the last argument enables the filter */
102  GPIO_PinModeSet(ADXL_INT1_PORT, ADXL_INT1_PIN, gpioModeInput, 1);
103 
104  /* Clear all odd pin interrupt flags (just in case)
105  * NVIC_ClearPendingIRQ(GPIO_ODD_IRQn); would also work but is less "readable" */
106  GPIO_IntClear(0xAAAA);
107 
108  /* Clear all even pin interrupt flags (just in case)
109  * NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn); would also work but is less "readable" */
110  GPIO_IntClear(0x5555);
111 
112  /* All pending interrupts can be cleared with GPIO_IntClear(0xFFFF); */
113 
114  /* Enable IRQ for even numbered GPIO pins */
115  NVIC_EnableIRQ(GPIO_EVEN_IRQn);
116 
117  /* Enable IRQ for odd numbered GPIO pins */
118  NVIC_EnableIRQ(GPIO_ODD_IRQn);
119 
120  /* Enable falling-edge interrupts for PB pins */
121  GPIO_ExtIntConfig(PB0_PORT, PB0_PIN, PB0_PIN, false, true, true);
122  GPIO_ExtIntConfig(PB1_PORT, PB1_PIN, PB1_PIN, false, true, true);
123 
124  /* Enable rising-edge interrupts for ADXL_INT1 */
125  GPIO_ExtIntConfig(ADXL_INT1_PORT, ADXL_INT1_PIN, ADXL_INT1_PIN, true, false, true);
126 
127 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
128  dbinfo("GPIO wake-up initialized");
129 #endif /* DEBUG_DBPRINT */
130 
131 }
#define ADXL_INT1_PORT
Definition: pin_mapping.h:77
#define PB0_PIN
Definition: pin_mapping.h:90
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
#define PB1_PORT
Definition: pin_mapping.h:91
#define PB0_PORT
Definition: pin_mapping.h:89
#define PB1_PIN
Definition: pin_mapping.h:92
#define ADXL_INT1_PIN
Definition: pin_mapping.h:78

Variable Documentation

◆ PB0_triggered

volatile bool PB0_triggered = false

Definition at line 79 of file interrupt.c.

◆ PB1_triggered

volatile bool PB1_triggered = false

Definition at line 80 of file interrupt.c.