IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
util.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file util.c
3  * @brief Utility functionality.
4  * @version 4.0
5  * @author Brecht Van Eeckhoudt
6  *
7  * ******************************************************************************
8  *
9  * @section Versions
10  *
11  * @li v1.0: Started with the code from https://github.com/Fescron/Project-LabEmbeddedDesign1/tree/master/code/SLSTK3400A_ADXL362
12  * @li v1.1: Changed PinModeSet DOUT value to 0 in initLED.
13  * @li v1.2: Removed unnecessary `GPIO_PinOutClear` line in initLED.
14  * @li v1.3: Moved initRTCcomp method from `main.c` to here, added delay functionality which goes into EM1 or EM2.
15  * @li v1.4: Moved delay functionality to specific header and source files.
16  * @li v2.0: Changed `Error()` to `error()`, added a global variable to keep the error number and initialize the pin of the LED automatically.
17  * @li v2.1: Changed initLED to be a static (~hidden) method and also made the global variables static.
18  * @li v2.2: Added peripheral clock enable/disable functionality for energy saving purposes, only added necessary includes in header file,
19  * moved the others to the source file, updated documentation, replaced SysTick delay with RTCC delay, changed error delay length.
20  * @li v2.3: Changed name of static variable, simplified some logic.
21  * @li v2.4: Stopped disabling the GPIO clock.
22  * @li v2.5: Moved documentation.
23  * @li v2.6: Updated code with new DEFINE checks.
24  * @li v2.7: Added functionality to send error values using LoRaWAN.
25  * @li v2.8: Added the ability to enable/disable error forwarding to the cloud using a public definition and changed UART error color.
26  * @li v3.0: Updated version number.
27  * @li v3.1: Removed `static` before the local variables (not necessary).
28  * @li v4.0: Changed functionality for new board type.
29  *
30  * ******************************************************************************
31  *
32  * @section License
33  *
34  * **Copyright (C) 2020 - Brecht Van Eeckhoudt**
35  *
36  * This program is free software: you can redistribute it and/or modify
37  * it under the terms of the **GNU General Public License** as published by
38  * the Free Software Foundation, either **version 3** of the License, or
39  * (at your option) any later version.
40  *
41  * This program is distributed in the hope that it will be useful,
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44  * GNU General Public License for more details.
45  *
46  * *A copy of the GNU General Public License can be found in the `LICENSE`
47  * file along with this source code.*
48  *
49  * @n
50  *
51  * Some methods use code obtained from examples from [Silicon Labs' GitHub](https://github.com/SiliconLabs/peripheral_examples).
52  * These sections are licensed under the Silabs License Agreement. See the file
53  * "Silabs_License_Agreement.txt" for details. Before using this software for
54  * any purpose, you must agree to the terms of that agreement.
55  *
56  ******************************************************************************/
57 
58 
59 #include <stdint.h> /* (u)intXX_t */
60 #include <stdbool.h> /* "bool", "true", "false" */
61 #include "em_cmu.h" /* Clock Management Unit */
62 #include "em_gpio.h" /* General Purpose IO */
63 
64 #include "util.h" /* Corresponding header file */
65 #include "pin_mapping.h" /* PORT and PIN definitions */
66 #include "debug_dbprint.h" /* Enable or disable printing to UART */
67 #include "delay.h" /* Delay functionality */
68 
69 #if ERROR_FORWARDING == 1 /* ERROR_FORWARDING */
70 //#include "lora_wrappers.h" /* LoRaWAN functionality */
71 #endif /* ERROR_FORWARDING */
72 
73 
74 /* Local variables */
76 bool GNDd_initialized = false;
77 
78 
79 /* Local prototype */
80 static void initGNDd (void);
81 
82 
83 /**************************************************************************//**
84  * @brief
85  * Enable or disable the GNDd pin.
86  *
87  * @details
88  * This method also initializes the pin-mode if necessary.
89  *
90  * @param[in] enabled
91  * @li `true` - Enable GNDd.
92  * @li `false` - Disable GNDd.
93  *****************************************************************************/
94 void gndd (bool enabled)
95 {
96  /* Initialize GNDd if not already the case */
97  if (!GNDd_initialized) initGNDd();
98 
99  /* Set the selected state */
100  if (enabled) GPIO_PinOutSet(DBG_GNDD_PORT, DBG_GNDD_PIN);
101  else GPIO_PinOutClear(DBG_GNDD_PORT, DBG_GNDD_PIN);
102 }
103 
104 
105 /**************************************************************************//**
106  * @brief
107  * Error method.
108  *
109  * @details
110  * **ERROR_FORWARDING == 0**@n
111  * The method flashes the LED, displays a UART message and holds the MCU
112  * forever in a loop until it gets reset. The error value gets stored in
113  * a global variable.
114  *
115  * **ERROR_FORWARDING == 1**@n
116  * The method sends the error value to the cloud using LoRaWAN if the error
117  * number doesn't correspond to LoRaWAN-related functionality (numbers 30 - 55).
118  *
119  * @param[in] number
120  * The number to indicate where in the code the error was thrown.
121  *****************************************************************************/
122 void error (uint8_t number)
123 {
124  /* Initialize GNDd if not already the case */
125  if (!GNDd_initialized) initGNDd();
126 
127  /* Save the given number in the global variable */
128  errorNumber = number;
129 
130 #if ERROR_FORWARDING == 0 /* ERROR_FORWARDING */
131 
132 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
133  dbprint_color(">>> Error (", 5);
134  dbprintInt(number);
135  dbprintln_color(")! Please reset MCU. <<<", 5);
136 #endif /* DEBUG_DBPRINT */
137 
138  while (1)
139  {
140  /* Nothing here */
141  }
142 
143 #else /* ERROR_FORWARDING */
144 
145  /* Check if the error number isn't called in BG96 functionality */
146  if ((number < 30) || (number > 55))
147  {
148 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
149  dbprint_color(">>> Error (", 5);
150  dbprintInt(number);
151  dbprintln_color(")! Sending the message to the cloud. <<<", 5);
152 #endif /* DEBUG_DBPRINT */
153 
154 // initLoRaWAN(); /* Initialize LoRaWAN functionality */
155 //
156 // sendStatus(number); /* Send the status value */
157 //
158 // disableLoRaWAN(); /* Disable RN2483 */
159  }
160  else
161  {
162 
163 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
164  dbprint_color(">>> Error in BG96 functionality (", 5);
165  dbprintInt(number);
166  dbprintln_color(")! <<<", 5);
167 #endif /* DEBUG_DBPRINT */
168 
169  }
170 
171 #endif /* ERROR_FORWARDING */
172 
173 }
174 
175 
176 /**************************************************************************//**
177  * @brief
178  * Initialize the initGNDd pin.
179  *
180  * @note
181  * This is a static method because it's only internally used in this file
182  * and called by other methods if necessary.
183  *****************************************************************************/
184 static void initGNDd (void)
185 {
186  /* Enable necessary clocks (just in case) */
187  CMU_ClockEnable(cmuClock_HFPER, true); /* GPIO is a High Frequency Peripheral */
188  CMU_ClockEnable(cmuClock_GPIO, true);
189 
190  /* In the case of gpioModePushPull, the last argument directly sets the pin state */
191  GPIO_PinModeSet(DBG_GNDD_PORT, DBG_GNDD_PIN, gpioModePushPull, 0);
192 
193 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
194  dbinfo("GNDd pin initialized");
195 #endif /* DEBUG_DBPRINT */
196 
197  GNDd_initialized = true;
198 }
void error(uint8_t number)
Error method.
Definition: util.c:122
bool GNDd_initialized
Definition: util.c:76
static void initGNDd(void)
Initialize the initGNDd pin.
Definition: util.c:184
#define DBG_GNDD_PORT
Definition: pin_mapping.h:95
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:505
#define DBG_GNDD_PIN
Definition: pin_mapping.h:96
Utility functionality.
The pin definitions for the custom board.
Delay functionality.
Enable or disable printing to UART with dbprint.
void gndd(bool enabled)
Enable or disable the GNDd pin.
Definition: util.c:94
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:740
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:482
uint8_t errorNumber
Definition: util.c:75
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423
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