IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
interrupt.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file interrupt.c
3  * @brief Interrupt functionality.
4  * @version 4.0
5  * @author Brecht Van Eeckhoudt
6  *
7  * ******************************************************************************
8  *
9  * @section Versions
10  *
11  * @li v1.0: Started from https://github.com/Fescron/Project-LabEmbeddedDesign1
12  * @li v1.1: Added dbprintln(""); above dbinfo statements in IRQ handlers to fix
13  * overwriting of text.
14  * @li v1.2: Disabled the RTC counter if GPIO handlers are called, only added necessary includes
15  * in header file, moved the others to the source file, updated documentation.
16  * @li v1.3: Started using set method for the static variable `ADXL_triggered`, added GPIO
17  * wakeup initialization method here, renamed file.
18  * @li v1.4: Stopped disabling the GPIO clock.
19  * @li v1.5: Started using getters/setters to indicate an interrupt to `main.c`.
20  * @li v1.6: Moved IRQ handler of RTC to this `delay.c`.
21  * @li v1.7: Updated clear pending interrupt logic.
22  * @li v1.8: Updated code with new DEFINE checks.
23  * @li v1.9: Removed error calls for "unknown" pins and added flag check for custom Happy Gecko board pinout.
24  * @li v2.0: Stopped disabling the RTC counter on pin interrupts.
25  * @li v2.1: Disabled the RTC counter on a button push because it confused delays called in LoRaWAN code.
26  * @li v2.2: Changed error numbering.
27  * @li v3.0: Updated version number.
28  * @li v3.1: Removed `static` before the local variables (not necessary).
29  * @li v4.0: Changed functionality for new board type.
30  *
31  * ******************************************************************************
32  *
33  * @note
34  * Other interrupt handlers can be found in `delay.c` and `adc.c`.
35  *
36  * ******************************************************************************
37  *
38  * @section License
39  *
40  * **Copyright (C) 2020 - Brecht Van Eeckhoudt**
41  *
42  * This program is free software: you can redistribute it and/or modify
43  * it under the terms of the **GNU General Public License** as published by
44  * the Free Software Foundation, either **version 3** of the License, or
45  * (at your option) any later version.
46  *
47  * This program is distributed in the hope that it will be useful,
48  * but WITHOUT ANY WARRANTY; without even the implied warranty of
49  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50  * GNU General Public License for more details.
51  *
52  * *A copy of the GNU General Public License can be found in the `LICENSE`
53  * file along with this source code.*
54  *
55  * @n
56  *
57  * Some methods use code obtained from examples from [Silicon Labs' GitHub](https://github.com/SiliconLabs/peripheral_examples).
58  * These sections are licensed under the Silabs License Agreement. See the file
59  * "Silabs_License_Agreement.txt" for details. Before using this software for
60  * any purpose, you must agree to the terms of that agreement.
61  *
62  ******************************************************************************/
63 
64 
65 #include <stdint.h> /* (u)intXX_t */
66 #include <stdbool.h> /* "bool", "true", "false" */
67 #include "em_device.h" /* Include necessary MCU-specific header file */
68 #include "em_cmu.h" /* Clock management unit */
69 #include "em_gpio.h" /* General Purpose IO */
70 #include "em_rtc.h" /* Real Time Counter (RTC) */
71 
72 #include "interrupt.h" /* Corresponding header file */
73 #include "pin_mapping.h" /* PORT and PIN definitions */
74 #include "debug_dbprint.h" /* Enable or disable printing to UART */
75 #include "util.h" /* Utility functionality */
76 
77 
78 /* Local variables */
79 volatile bool BTN_triggered = false; /* Volatile because it's modified by an interrupt service routine */
80 volatile bool PULSE0_triggered = false;
81 volatile bool PULSE1_triggered = false;
82 
83 
84 /**************************************************************************//**
85  * @brief
86  * Initialize GPIO wake-up functionality.
87  *
88  * @details
89  * Initialize falling-edge interrupts.
90  *****************************************************************************/
91 void initGPIOwakeup (void)
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 inputs (external pull-up-resistors), the last argument enables the filter */
98  GPIO_PinModeSet(BTN_PORT, BTN_PIN, gpioModeInput, 1);
99  GPIO_PinModeSet(PULSE0_PORT, PULSE0_PIN, gpioModeInput, 1);
100  GPIO_PinModeSet(PULSE1_PORT, PULSE1_PIN, gpioModeInput, 1);
101 
102  /* Clear all odd pin interrupt flags (just in case)
103  * NVIC_ClearPendingIRQ(GPIO_ODD_IRQn); would also work but is less "readable" */
104  GPIO_IntClear(0xAAAA);
105 
106  /* Clear all even pin interrupt flags (just in case)
107  * NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn); would also work but is less "readable" */
108  GPIO_IntClear(0x5555);
109 
110  /* All pending interrupts can be cleared with GPIO_IntClear(0xFFFF); */
111 
112  /* Enable IRQ for even numbered GPIO pins */
113  NVIC_EnableIRQ(GPIO_EVEN_IRQn);
114 
115  /* Enable IRQ for odd numbered GPIO pins */
116  NVIC_EnableIRQ(GPIO_ODD_IRQn);
117 
118  /* Enable falling-edge interrupts */
119  GPIO_ExtIntConfig(BTN_PORT, BTN_PIN, BTN_PIN, false, true, true);
120  GPIO_ExtIntConfig(PULSE0_PORT, PULSE0_PIN, PULSE0_PIN, false, true, true);
121  GPIO_ExtIntConfig(PULSE1_PORT, PULSE1_PIN, PULSE1_PIN, false, true, true);
122 
123 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
124  dbinfo("GPIO wake-up initialized");
125 #endif /* DEBUG_DBPRINT */
126 
127 }
128 
129 
130 /**************************************************************************//**
131  * @brief
132  * Getter for the `BTN_triggered` variable.
133  *
134  * @return
135  * The value of `BTN_triggered`.
136  *****************************************************************************/
137 bool BTN_getTriggered (void)
138 {
139  return (BTN_triggered);
140 }
141 
142 
143 /**************************************************************************//**
144  * @brief
145  * Setter for the `BTN_triggered` variable.
146  *
147  * @param[in] value
148  * The boolean value to set to the selected variable.
149  *****************************************************************************/
150 void BTN_setTriggered (bool value)
151 {
152  BTN_triggered = value;
153 }
154 
155 
156 /**************************************************************************//**
157  * @brief
158  * Getter for the `PULSE0/1_triggered` variable.
159  *
160  * @param[in] number
161  * @li `0` - PULSE0 selected.
162  * @li `1` - PULSE1 selected.
163  *
164  * @return
165  * The value of `PULSE0/1_triggered`.
166  *****************************************************************************/
168 {
169  if (number == 0) return (PULSE0_triggered);
170  else if (number == 1) return (PULSE1_triggered);
171 
172  return (false);
173 }
174 
175 
176 /**************************************************************************//**
177  * @brief
178  * Setter for the `PULSE0/1_triggered` variable.
179  *
180  * @param[in] number
181  * @li `0` - PULSE0 selected.
182  * @li `1` - PULSE1 selected.
183  *
184  * @param[in] value
185  * The boolean value to set to the selected variable.
186  *****************************************************************************/
187 void PULSE_setTriggered (uint8_t number, bool value)
188 {
189  if (number == 0) PULSE0_triggered = value;
190  else if (number == 1) PULSE1_triggered = value;
191 }
192 
193 
194 /**************************************************************************//**
195  * @brief
196  * GPIO Even IRQ for even-numbered pins.
197  *
198  * @details
199  * The RTC is also disabled on a button press (*manual wake-up*)
200  *
201  * @note
202  * The *weak* definition for this method is located in `system_efm32hg.h`.
203  *****************************************************************************/
204 void GPIO_EVEN_IRQHandler (void) // TODO
205 {
206  /* Read interrupt flags */
207  uint32_t flags = GPIO_IntGet();
208 
209  /* Check if BTN is pushed */
210  if (flags == 0x0004)
211  {
212  /* Disable the counter (manual wakeup) */
213  RTC_Enable(false);
214 
215  BTN_triggered = true;
216  }
217 
218  /* Check if P2 (PULSE1) is triggered */
219  if (flags == 0x4000) PULSE1_triggered = true;
220 
221  /* Clear all even pin interrupt flags */
222  GPIO_IntClear(0x5555);
223 }
224 
225 
226 /**************************************************************************//**
227  * @brief
228  * GPIO Odd IRQ for odd-numbered pins.
229  *
230  * @note
231  * The *weak* definition for this method is located in `system_efm32hg.h`.
232  *****************************************************************************/
233 void GPIO_ODD_IRQHandler (void) // TODO
234 {
235  /* Read interrupt flags */
236  uint32_t flags = GPIO_IntGet();
237 
238  /* Check if P1 (PULSE0) is triggered */
239  if (flags == 0x2000) PULSE0_triggered = true;
240 
241  /* Clear all odd pin interrupt flags */
242  GPIO_IntClear(0xAAAA);
243 }
volatile bool PULSE0_triggered
Definition: interrupt.c:80
void PULSE_setTriggered(uint8_t number, bool value)
Setter for the PULSE0/1_triggered variable.
Definition: interrupt.c:187
volatile bool BTN_triggered
Definition: interrupt.c:79
void GPIO_ODD_IRQHandler(void)
GPIO Odd IRQ for odd-numbered pins.
Definition: interrupt.c:233
bool PULSE_getTriggered(uint8_t number)
Getter for the PULSE0/1_triggered variable.
Definition: interrupt.c:167
#define BTN_PIN
Definition: pin_mapping.h:62
#define PULSE1_PIN
Definition: pin_mapping.h:112
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:505
void initGPIOwakeup(void)
Initialize GPIO wake-up functionality.
Definition: interrupt.c:91
#define PULSE0_PORT
Definition: pin_mapping.h:109
void BTN_setTriggered(bool value)
Setter for the BTN_triggered variable.
Definition: interrupt.c:150
#define PULSE1_PORT
Definition: pin_mapping.h:111
void GPIO_EVEN_IRQHandler(void)
GPIO Even IRQ for even-numbered pins.
Definition: interrupt.c:204
Utility functionality.
The pin definitions for the custom board.
Interrupt functionality.
Enable or disable printing to UART with dbprint.
#define BTN_PORT
Definition: pin_mapping.h:61
volatile bool PULSE1_triggered
Definition: interrupt.c:81
bool BTN_getTriggered(void)
Getter for the BTN_triggered variable.
Definition: interrupt.c:137
#define PULSE0_PIN
Definition: pin_mapping.h:110
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
unsigned int uint32_t
Typedef defining 32 bit unsigned int type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:78