Embedded System Design 2 - Project
main.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file main.c
3  * @brief The main file for Project 2 from Embedded System Design 2 - Lab.
4  * @version 5.2
5  * @author Brecht Van Eeckhoudt
6  *
7  * ******************************************************************************
8  *
9  * @section Versions
10  *
11  * Please check https://github.com/Fescron/Project-LabEmbeddedDesign2/tree/master/software to find the latest version!
12  *
13  * @li v1.0: Started from https://github.com/Fescron/Project-LabEmbeddedDesign1
14  * and added code for the DS18B20 temperature sensor and the selfmade
15  * link breakage sensor. Reformatted some of these imported methods.
16  * @li v1.1: Removed unused files, add cable-checking method.
17  * @li v1.2: Moved initRTCcomp method to "util.c".
18  * @li v1.3: Stopped using deprecated function "GPIO_IntConfig".
19  * @li v1.4: Started using get/set method for the static variable "ADXL_triggered".
20  * @li v1.5: Reworked the code a lot (stopped disabling cmuClock_GPIO, ...).
21  * @li v1.6: Moved all documentation above source files to this file.
22  * @li v1.7: Updated documentation, started using a state-machine.
23  * @li v1.8: Moved checkCable method to "other.c" and started using readVBAT method.
24  * @li v1.9: Cleaned up documentation and TODO's.
25  * @li v2.0: Updated code with new DEFINE checks.
26  * @li v2.1: Updated code with new ADC functionality.
27  * @li v2.2: Started using working temperature sensor code.
28  * @li v2.3: Started using working cable checking code and added "SEND" `mcu_state`.
29  * @li v2.4: Started using a struct to keep the measurements in, added line to disable the RN2483.
30  * @li v2.5: Started using custom enum types for the accelerometer settings.
31  * @li v2.6: Added ADXL testing functionality.
32  * @li v2.7: Removed USTIMER logic from this file.
33  * @li v2.8: Added functionality to detect a *storm*.
34  * @li v2.9: Started adding LoRaWAN functionality, added more wake-up/sleep functionality.
35  * @li v3.0: Started using updated LoRaWAN send methods.
36  * @li v3.1: Added functionality to enable/disable the LED blinking when measuring/sending data.
37  * @li v3.2: Fixed button wake-up when sleeping for WAKE_UP_PERIOD_S/2.
38  * @li v3.3: Moved `data.index` reset to LoRaWAN sending functionality and updated documentation.
39  * @li v3.4: Moved `data.index` reset back to `main.c`.
40  * @li v3.5: Added functionality to go back to the remaining sleep time on an accelerometer wake-up.
41  * @li v3.6: Updated code to handle underlying changes.
42  * @li v4.0: Updated documentation and version number.
43  * @li v4.1: Added definitions to easily change the accelerometer's configuration settings.
44  * @li v5.0: Updated sleep logic when waking up using the accelerometer.
45  * @li v5.1: Added extra ISR handlers.
46  * @li v5.2: Removed `static` before a local variable (not necessary).
47  *
48  * ******************************************************************************
49  *
50  * @todo
51  * **Optimalisations:**@n
52  * - Change LoRaWAN Spreading Factor and send power, and check the effect on the power usage.
53  * - Add some time to the `while` increment escape counters?
54  * - Check power usage effect of disabling `PM_SENS_EXT`.
55  * - Check difference between absolute/referenced mode for the accelerometer.
56  * - Change *mode* to release.
57  *
58  * @todo
59  * **Future improvements:**@n
60  * - Use *IAR toolchain* instead of *GCC*, see section **Optimizing code** in `documentation.h`
61  * - Sleep for some time in *while* loops instead of just incrementing the *escape-counter*.
62  * - First separate `ULFRCO` definition in `delay.c
63  * - Try to shorten delays (for example: 40 ms power-up time for the RN2483...)
64  * - Fix `sleepLoRaWAN` and `wakeLoRaWAN` functionality (also see `lora_wrappers.c`).
65  *
66  * ******************************************************************************
67  *
68  * @section License
69  *
70  * **Copyright (C) 2019 - Brecht Van Eeckhoudt**
71  *
72  * This program is free software: you can redistribute it and/or modify
73  * it under the terms of the **GNU General Public License** as published by
74  * the Free Software Foundation, either **version 3** of the License, or
75  * (at your option) any later version.
76  *
77  * This program is distributed in the hope that it will be useful,
78  * but WITHOUT ANY WARRANTY; without even the implied warranty of
79  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80  * GNU General Public License for more details.
81  *
82  * *A copy of the GNU General Public License can be found in the `LICENSE`
83  * file along with this source code.*
84  *
85  * @n
86  *
87  * Some methods also use code obtained from examples from [Silicon Labs' GitHub](https://github.com/SiliconLabs/peripheral_examples).
88  * These sections are licensed under the Silabs License Agreement. See the file
89  * "Silabs_License_Agreement.txt" for details. Before using this software for
90  * any purpose, you must agree to the terms of that agreement.
91  *
92  * ******************************************************************************
93  *
94  * @attention
95  * See the file `documentation.h` for a lot of useful documentation about this project!
96  *
97  * ******************************************************************************
98  *
99  * @section Errors
100  *
101  * If in this project something unexpected occurs, an `error` method gets called.
102  * What happens in this method can be selected in `util.h` with the definition
103  * `ERROR_FORWARDING`. If it's value is `0` the MCU displays (if `dbprint` is enabled)
104  * a UART message and gets put in a `while(true)` to flash the LED. If it's value is
105  * `1` then certain values (all values except 30 - 55 since these are errors in the
106  * LoRaWAN functionality itself) get forwarded to the cloud using LoRaWAN functionality
107  * and the MCU resumes it's code.
108  *
109  * When calling an `error` method, the following things were kept in mind:
110  * - **Values 0 - 9:** Reserved for reset and other critical functionality.
111  * - **1:** Send once after a reset (`firstBoot == true` in `main.c`)
112  * - **9:** Used in `sendTest(data);` method (`lora_wrappers.c`)
113  * - **Values 10 - ... :** Available to use for anything else.
114  *
115  * Below is a list of values which correspond to certain functionality:
116  * - **10:** Problem in the case of the state machine (`main.c`)
117  * - **11 - 13:** `adc.c`
118  * - **14 - 17:** `delay.c`
119  * - **18 - 19:** `interrupt.c`
120  * - **20 - 27:** `ADXL362.c`
121  * - **28 - 29:** `DS18B20.c`
122  * - **30 - 50:** `lora_wrappers.c`
123  * - **51 - 55:** `leuart.c`
124  *
125  * ******************************************************************************
126  *
127  * @section CHANNELS LoRaWAN sensor channels
128  *
129  * - `LPP_VBAT_CHANNEL 0x10 // 16`
130  * - `LPP_TEMPERATURE_CHANNEL_INT 0x11 // 17`
131  * - `LPP_TEMPERATURE_CHANNEL_EXT 0x12 // 18`
132  * - `LPP_STORM_CHANNEL 0x13 // 19`
133  * - `LPP_CABLE_BROKEN_CHANNEL 0x14 // 20`
134  * - `LPP_STATUS_CHANNEL 0x15 // 21`
135  *
136  ******************************************************************************/
137 
138 
139 #include <stdint.h> /* (u)intXX_t */
140 #include <stdbool.h> /* "bool", "true", "false" */
141 #include "em_device.h" /* Include necessary MCU-specific header file */
142 #include "em_chip.h" /* Chip Initialization */
143 #include "em_cmu.h" /* Clock management unit */
144 #include "em_gpio.h" /* General Purpose IO */
145 #include "em_rtc.h" /* Real Time Counter (RTC) */
146 
147 #include "pin_mapping.h" /* PORT and PIN definitions */
148 #include "debug_dbprint.h" /* Enable or disable printing to UART for debugging */
149 #include "delay.h" /* Delay functionality */
150 #include "util.h" /* Utility functionality */
151 #include "interrupt.h" /* GPIO wake-up initialization and interrupt handlers */
152 #include "ADXL362.h" /* Functions related to the accelerometer */
153 #include "DS18B20.h" /* Functions related to the temperature sensor */
154 #include "adc.h" /* Internal voltage and temperature reading functionality */
155 #include "cable.h" /* Cable checking functionality */
156 #include "lora_wrappers.h" /* LoRaWAN functionality */
157 #include "datatypes.h" /* Definitions of the custom data-types */
158 
159 
160 /* Local definitions */
161 /** Time between each wake-up in seconds
162  * @li max 500 seconds when using LFXO delay
163  * @li 3600 seconds (one hour) works fine when using ULFRCO delay */
164 #define WAKE_UP_PERIOD_S 180 // On buoy: 1800 /* 600 = every 10 minutes */
165 
166 /** Amount of PIN interrupt wakeups (before a RTC wake-up) to be considered as a *storm* */
167 #define STORM_INTERRUPTS 8
168 
169 /** The threshold value [g] for the accelerometer to detect and send an interrupt to wake-up the MCU */
170 #define ADXL_THRESHOLD 7
171 
172 /** The *g* range to configure the accelerometer with */
173 #define ADXL_RANGE ADXL_RANGE_8G
174 
175 /** The ODR setting to configure the accelerometer with */
176 #define ADXL_ODR ADXL_ODR_12_5_HZ
177 
178 /** Public definition to select if the LED is turned on while measuring or sending data
179  * @li `1` - Enable the LED when while measuring or sending data.
180  * @li `0` - Don't enable the LED while measuring or sending data. */
181 #define LED_ENABLED 0
182 
183 
184 /* Local variables */
185 /** Keep the state of the state machine */
186 static MCU_State_t MCUstate; /* Static so it's always kept in memory (@data segment, space provided during compile time) */
187 
188 /** Keep the measurement data */
190 
191 
192 /**************************************************************************//**
193  * @brief
194  * Method to check if any interrupts are triggered by buttons.
195  *
196  * @return
197  * @li `true` - A button interrupt was triggered.
198  * @li `false` - No button interrupt was triggered.
199  *****************************************************************************/
201 {
202  bool value = false;
203 
204  if (BTN_getTriggered(0))
205  {
206 
207 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
208  dbprintln_color("PB0 pushed!", 4);
209 #endif /* DEBUG_DBPRINT */
210 
211  value = true;
212 
213  BTN_setTriggered(0, false); /* Clear variable */
214  }
215 
216 
217  if (BTN_getTriggered(1))
218  {
219 
220 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
221  dbprintln_color("PB1 pushed!", 4);
222 #endif /* DEBUG_DBPRINT */
223 
224  value = true;
225 
226  BTN_setTriggered(1, false); /* Clear variable */
227  }
228 
229  return (value);
230 }
231 
232 
233 /**************************************************************************//**
234  * @brief
235  * Main function.
236  *****************************************************************************/
237 int main (void)
238 {
239  /* Keep a value if a storm has been detected */
240  bool stormDetected = false;
241 
242  /* Value used to send one test LoRaWAN message after booting */
243  bool firstBoot = true;
244 
245  /* Value to keep the remaining sleep time when waking up using the accelerometer */
246  uint32_t remainingSleeptime = 0;
247 
248  /* Set the index to put the measurements in */
249  data.index = 0;
250 
251  while (1)
252  {
253  switch (MCUstate)
254  {
255  case INIT:
256  {
257  CHIP_Init(); /* Initialize chip */
258 
259 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
260 #if CUSTOM_BOARD == 1 /* Custom Happy Gecko pinout */
261  dbprint_INIT(DBG_UART, DBG_UART_LOC, false, false);
262  dbwarn("CUSTOM board pinout selected");
263 #else /* Regular Happy Gecko pinout */
264  dbprint_INIT(USART1, 4, true, false); /* VCOM */
265  dbwarn("REGULAR board pinout selected");
266 #endif /* Board pinout selection */
267 #endif /* DEBUG_DBPRINT */
268 
269  led(true); /* Enable (and initialize) LED */
270 
271  delay(4000); /* 4 second delay to notice initialization */
272  led(false);
273  delay(100);
274  led(true);
275 
276  initGPIOwakeup(); /* Initialize GPIO wake-up */
277 
278  initADC(BATTERY_VOLTAGE); /* Initialize ADC to read battery voltage */
279 
280  /* Initialize pin and disable power to RN2483 */
281  GPIO_PinModeSet(PM_RN2483_PORT, PM_RN2483_PIN, gpioModePushPull, 0);
282 
283  /* Initialize pin and disable external sensor power on DRAMCO shield */
284  GPIO_PinModeSet(PM_SENS_EXT_PORT, PM_SENS_EXT_PIN, gpioModePushPull, 0); // TODO: check power usage effect?
285 
286  /* Initialize accelerometer */
287  if (true)
288  {
289  /* Go through all of the accelerometer ODR settings to see the influence they have on power usage. */
290  if (false)
291  {
292  initADXL(); /* Initialize the accelerometer */
293 
294  led(false);
295  testADXL();
296  led(true);
297  }
298 
299  initADXL(); /* Initialize the accelerometer */
300 
301  ADXL_configRange(ADXL_RANGE); /* Set the measurement range */
302 
303  ADXL_configODR(ADXL_ODR); /* Configure ODR */
304 
305  if (false) ADXL_readValues(); /* Read and display values forever */
306 
307  ADXL_configActivity(ADXL_THRESHOLD); /* Configure (referenced) activity threshold mode on INT1 [g] */
308 
309  ADXL_enableMeasure(true); /* Enable measurements */
310 
311  delay(300);
312 
313  ADXL_ackInterrupt(); /* ADXL gives interrupt, capture this and acknowledge it by reading from it's status register */
314 
315  ADXL_enableSPI(false); /* Disable SPI after the initializations */
316 
317  ADXL_clearCounter(); /* Clear the trigger counter */
318  }
319 
320 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
321  dbprintln("");
322 #endif /* DEBUG_DBPRINT */
323 
324  led(false); /* Disable LED */
325 
326  MCUstate = MEASURE;
327  } break;
328 
329  case MEASURE:
330  {
331 
332 #if LED_ENABLED == 1 /* LED_ENABLED */
333  led(true); /* Enable LED */
334 #endif /* LED_ENABLED */
335 
336  /* Measure and store the external temperature */
337  data.extTemp[data.index] = readTempDS18B20();
338 
339  /* Measure and store the battery voltage */
340  data.voltage[data.index] = readADC(BATTERY_VOLTAGE);
341 
342  /* Measure and store the internal temperature */
344 
345 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
346  dbinfoInt("Measurement ", data.index + 1, "");
347  dbinfoInt("Temperature: ", data.extTemp[data.index], "");
348  dbinfoInt("Battery voltage: ", data.voltage[data.index], "");
349  dbinfoInt("Internal temperature: ", data.intTemp[data.index], "");
350 #endif /* DEBUG_DBPRINT */
351 
352  /* Increase the index to put the next measurements in */
353  data.index++;
354 
355  /* Check if the cable is intact and send LoRaWAN data if necessary */
356  if (checkCable(data)) data.index = 0; /* If measurements have been send, reset the index */
357 
358  if (firstBoot && (data.index > 0))
359  {
360 
361 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
362  dbwarn("Sending first measurements ...");
363 #endif /* DEBUG_DBPRINT */
364 
365  initLoRaWAN(); /* Initialize LoRaWAN functionality */
366 
367  sendStatus(1); /* Send the status data */
368 
369  sendMeasurements(data); /* Send the measurements */
370 
371  disableLoRaWAN(); /* Disable RN2483 */
372 
373  data.index = 0; /* Reset the index to put the measurements in (needs to be here for the correct data to be affected) */
374 
375  firstBoot = false;
376  }
377 
378 #if LED_ENABLED == 1 /* LED_ENABLED */
379  led(false); /* Disable LED */
380 #endif /* LED_ENABLED */
381 
382  /* Decide if 6 measurements are taken or not and react accordingly */
383  if (data.index == 6) MCUstate = SEND;
384  else MCUstate = SLEEP;
385  } break;
386 
387  case SEND:
388  {
389 
390 #if LED_ENABLED == 1 /* LED_ENABLED */
391  led(true); /* Enable LED */
392 #endif /* LED_ENABLED */
393 
394 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
395  dbwarnInt("Sending ", data.index, " measurements ...");
396 #endif /* DEBUG_DBPRINT */
397 
398  initLoRaWAN(); /* Initialize LoRaWAN functionality */
399 
400  sendMeasurements(data); /* Send the measurements */
401 
402  disableLoRaWAN(); /* Disable RN2483 */
403 
404  data.index = 0; /* Reset the index to put the measurements in (needs to be here for the correct data to be affected) */
405 
406 #if LED_ENABLED == 1 /* LED_ENABLED */
407  led(false); /* Disable LED */
408 #endif /* LED_ENABLED */
409 
410  MCUstate = SLEEP;
411  } break;
412 
413  case SEND_STORM:
414  {
415 
416 #if LED_ENABLED == 1 /* LED_ENABLED */
417  led(true); /* Enable LED */
418 #endif /* LED_ENABLED */
419 
420 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
421  dbwarnInt("STORM DETECTED! Sending ", data.index, " measurement(s) ...");
422 #endif /* DEBUG_DBPRINT */
423 
424  initLoRaWAN(); /* Initialize LoRaWAN functionality */
425 
426  sendStormDetected(true); /* Send a message to indicate that a storm has been detected */
427 
428  if (data.index > 0)
429  {
430  sendMeasurements(data); /* Send the already gathered measurements */
431 
432  data.index = 0; /* Reset the index to put the measurements in (needs to be here for the correct data to be affected) */
433  }
434 
435  disableLoRaWAN(); /* Disable RN2483 */
436 
437  stormDetected = true; /* Indicate that a storm has been detected */
438 
439 #if LED_ENABLED == 1 /* LED_ENABLED */
440  led(false); /* Disable LED */
441 #endif /* LED_ENABLED */
442 
444  } break;
445 
446  case SLEEP:
447  {
448  sleep(WAKE_UP_PERIOD_S); /* Go to sleep for xx seconds */
449 
450  MCUstate = WAKEUP;
451  } break;
452 
453  case SLEEP_HALFTIME:
454  {
455  sleep(WAKE_UP_PERIOD_S/2); /* Go to sleep for xx seconds */
456 
457  MCUstate = WAKEUP;
458  } break;
459 
460  case WAKEUP:
461  {
462 
463 #if LED_ENABLED == 1 /* LED_ENABLED */
464  led(true); /* Enable LED */
465 #endif /* LED_ENABLED */
466 
467  /* Check if we woke up using buttons */
468  if (checkBTNinterrupts())
469  {
470  ADXL_clearCounter(); /* Clear the trigger counter */
471  remainingSleeptime = 0; /* Reset passed sleeping time */
472 
473  MCUstate = MEASURE; /* Take measurements on "case WAKEUP" exit */
474  }
475 
476  /* Check if we woke up using the RTC sleep functionality and act accordingly */
477  if (RTC_checkWakeup())
478  {
479  RTC_clearWakeup(); /* Clear variable */
480 
481  ADXL_clearCounter(); /* Clear the trigger counter because we woke up "normally" */
482  remainingSleeptime = 0; /* Reset passed sleeping time since it's an RTC wakeup */
483 
484  MCUstate = MEASURE; /* Take measurements on "case WAKEUP" exit */
485  }
486 
487  /* Check if we woke up using the accelerometer */
488  if (ADXL_getTriggered())
489  {
490  /* Check if we detected a storm */
492  {
493  RTC_Enable(false); /* Disable the counter */
494 
495  MCUstate = SEND_STORM; /* Storm detected, send a message on "case WAKEUP" exit */
496  }
497  else
498  {
499 
500 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
501  dbprintln_color("INT-PD7 triggered!", 4);
502 #endif /* DEBUG_DBPRINT */
503 
504  ADXL_enableSPI(true); /* Enable SPI functionality */
505  ADXL_ackInterrupt(); /* Acknowledge ADXL interrupt by reading the status register */
506  ADXL_enableSPI(false); /* Disable SPI functionality */
507 
508  /* Check if a storm was detected before going to sleep for WAKE_UP_PERIOD_S/2 */
509  if (stormDetected)
510  {
511  stormDetected = false; /* Reset variable */
512  MCUstate = MEASURE; /* Take measurements on "case WAKEUP" exit */
513  }
514  else
515  {
516  remainingSleeptime += RTC_getPassedSleeptime(); /* Add the time spend sleeping to the variable */
517 
518 #if LED_ENABLED == 1 /* LED_ENABLED */
519  led(false); /* Disable LED */
520 #endif /* LED_ENABLED */
521 
522  /* Go back to sleep for the remaining time until another measurement (we only take measurements on RTC/button wake-up) */
523  sleep(WAKE_UP_PERIOD_S - remainingSleeptime);
524 
525  MCUstate = WAKEUP; /* Go back to this case if we wake-up */
526  }
527  }
528  }
529 
530 #if LED_ENABLED == 1 /* LED_ENABLED */
531  led(false); /* Disable LED */
532 #endif /* LED_ENABLED */
533 
534  } break;
535 
536  default:
537  {
538  error(10);
539  } break;
540  }
541  }
542 }
543 
544 
545 /**************************************************************************//**
546  * @brief
547  * NMI interrupt service routine.
548  *
549  * @note
550  * The *weak* definition for this method is located in `startup_gcc_efm32hg.h`.
551  *****************************************************************************/
552 void NMI_Handler(void)
553 {
554  while(1);
555 }
556 
557 
558 /**************************************************************************//**
559  * @brief
560  * HardFault interrupt service routine.
561  *
562  * @note
563  * The *weak* definition for this method is located in `startup_gcc_efm32hg.h`.
564  *****************************************************************************/
566 {
567  while(1);
568 }
569 
570 
571 /**************************************************************************//**
572  * @brief
573  * SVC interrupt service routine.
574  *
575  * @note
576  * The *weak* definition for this method is located in `startup_gcc_efm32hg.h`.
577  *****************************************************************************/
578 void SVC_Handler(void)
579 {
580  while(1);
581 }
582 
583 
584 /**************************************************************************//**
585  * @brief
586  * PendSV interrupt service routine.
587  *
588  * @note
589  * The *weak* definition for this method is located in `startup_gcc_efm32hg.h`.
590  *****************************************************************************/
591 void PendSV_Handler(void)
592 {
593  while(1);
594 }
595 
bool checkBTNinterrupts(void)
Method to check if any interrupts are triggered by buttons.
Definition: main.c:200
Definition: datatypes.h:60
void initADC(ADC_Measurement_t peripheral)
Method to initialize the ADC to later check the battery voltage or internal temperature.
Definition: adc.c:86
#define DBG_UART
Definition: pin_mapping.h:95
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
Definition: dbprint.c:395
void dbinfoInt(char *message1, int32_t value, char *message2)
Print an info value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:567
void SVC_Handler(void)
SVC interrupt service routine.
Definition: main.c:578
bool checkCable(MeasurementData_t data)
Method to check if the wire is broken.
Definition: cable.c:92
void ADXL_readValues(void)
Read and display "g" values forever with a 100ms interval.
Definition: ADXL362.c:515
All code for the ADXL362 accelerometer.
bool ADXL_getTriggered(void)
Getter for the ADXL_triggered variable.
Definition: ADXL362.c:216
Definitions of the custom data-types used.
void ADXL_configODR(ADXL_ODR_t givenODR)
Configure the Output Data Rate (ODR).
Definition: ADXL362.c:411
MeasurementData_t data
Definition: main.c:189
#define DBG_UART_LOC
Definition: pin_mapping.h:96
void dbwarn(char *message)
Print a warning string (char array) in yellow to USARTx and go to the next line.
Definition: dbprint.c:521
void ADXL_clearCounter(void)
Method to set the ADXL_triggercounter variable back to zero.
Definition: ADXL362.c:188
#define PM_RN2483_PIN
Definition: pin_mapping.h:134
#define PM_SENS_EXT_PIN
Definition: pin_mapping.h:136
void dbwarnInt(char *message1, int32_t value, char *message2)
Print a warning value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:598
void BTN_setTriggered(uint8_t number, bool value)
Setter for the PB0_triggered and PB1_triggered variable.
Definition: interrupt.c:174
void sendStormDetected(bool stormDetected)
Send a packet to the cloud using LoRaWAN to indicate that a storm has been detected.
uint16_t ADXL_getCounter(void)
Getter for the ADXL_triggercounter variable.
Definition: ADXL362.c:178
void ADXL_enableMeasure(bool enabled)
Enable or disable measurement mode.
Definition: ADXL362.c:310
ADC functionality for reading the (battery) voltage and internal temperature.
bool BTN_getTriggered(uint8_t number)
Getter for the PB0_triggered and PB1_triggered variables.
Definition: interrupt.c:145
void testADXL(void)
This method goes through all of the ODR settings to see the influence they have on power usage...
Definition: ADXL362.c:571
void initADXL(void)
Initialize the accelerometer.
Definition: ADXL362.c:132
int32_t intTemp[6]
Definition: datatypes.h:73
void ADXL_enableSPI(bool enabled)
Enable or disable the SPI pins and USART0/1 clock and peripheral to the accelerometer.
Definition: ADXL362.c:245
#define PM_RN2483_PORT
Definition: pin_mapping.h:133
Cable checking functionality.
void PendSV_Handler(void)
PendSV interrupt service routine.
Definition: main.c:591
void error(uint8_t number)
Error method.
Definition: util.c:131
void sendMeasurements(MeasurementData_t data)
Send measured battery voltages and internal and external temperatures to the cloud using LoRaWAN...
void ADXL_ackInterrupt(void)
Acknowledge the interrupt from the accelerometer.
Definition: ADXL362.c:230
uint32_t RTC_getPassedSleeptime(void)
Method to get the time spend sleeping (in seconds) in the case of GPIO wake-up.
Definition: delay.c:428
Utility functionality.
#define ADXL_ODR
Definition: main.c:176
void disableLoRaWAN(void)
Disable LoRaWAN functionality.
void initGPIOwakeup(void)
Initialize GPIO wake-up functionality.
Definition: interrupt.c:91
The pin definitions for the regular and custom Happy Gecko board.
#define ADXL_RANGE
Definition: main.c:173
Delay functionality.
void initLoRaWAN(void)
Initialize LoRaWAN functionality.
Definition: lora_wrappers.c:92
Interrupt functionality.
void dbprint_INIT(USART_TypeDef *pointer, uint8_t location, bool vcom, bool interrupts)
Initialize USARTx.
Definition: dbprint.c:148
void sendStatus(uint8_t status)
Send a packet to indicate a status.
void RTC_clearWakeup(void)
Method to clear RTC_sleep_wakeup.
Definition: delay.c:414
LoRa wrapper methods.
Enable or disable printing to UART with dbprint.
int32_t extTemp[6]
Definition: datatypes.h:74
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
void ADXL_configRange(ADXL_Range_t givenRange)
Configure the measurement range and store the selected one in a global variable for later (internal) ...
Definition: ADXL362.c:358
void ADXL_configActivity(uint8_t gThreshold)
Configure the accelerometer to work in (referenced) activity threshold mode.
Definition: ADXL362.c:468
#define WAKE_UP_PERIOD_S
Definition: main.c:164
static MCU_State_t MCUstate
Definition: main.c:186
#define STORM_INTERRUPTS
Definition: main.c:167
#define PM_SENS_EXT_PORT
Definition: pin_mapping.h:135
#define ADXL_THRESHOLD
Definition: main.c:170
enum mcu_states MCU_State_t
int32_t readADC(ADC_Measurement_t peripheral)
Method to read the battery voltage or internal temperature.
Definition: adc.c:164
void HardFault_Handler(void)
HardFault interrupt service routine.
Definition: main.c:565
Definition: datatypes.h:58
int32_t voltage[6]
Definition: datatypes.h:72
void NMI_Handler(void)
NMI interrupt service routine.
Definition: main.c:552
bool RTC_checkWakeup(void)
Method to check if the wakeup was caused by the RTC.
Definition: delay.c:404
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:480
void sleep(uint32_t sSleep)
Sleep for a certain amount of seconds in EM2/3.
Definition: delay.c:276
int main(void)
Main function.
Definition: main.c:237
void led(bool enabled)
Enable or disable the LED.
Definition: util.c:103
int32_t readTempDS18B20(void)
Get a temperature value from the DS18B20.
Definition: DS18B20.c:113
All code for the DS18B20 temperature sensor.