Embedded System Design 2 - Project
util.c File Reference

Utility functionality. More...

#include <stdint.h>
#include <stdbool.h>
#include "em_cmu.h"
#include "em_gpio.h"
#include "util.h"
#include "pin_mapping.h"
#include "debug_dbprint.h"
#include "delay.h"

Go to the source code of this file.

Functions

static void initLED (void)
 Initialize the LED. More...
 
void led (bool enabled)
 Enable or disable the LED. More...
 
void error (uint8_t number)
 Error method. More...
 

Variables

uint8_t errorNumber = 0
 
bool LED_initialized = false
 

Detailed Description

Utility functionality.

Version
3.1
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Started with the code from https://github.com/Fescron/Project-LabEmbeddedDesign1/tree/master/code/SLSTK3400A_ADXL362
  • v1.1: Changed PinModeSet DOUT value to 0 in initLED.
  • v1.2: Removed unnecessary GPIO_PinOutClear line in initLED.
  • v1.3: Moved initRTCcomp method from main.c to here, added delay functionality which goes into EM1 or EM2.
  • v1.4: Moved delay functionality to specific header and source files.
  • v2.0: Changed Error() to error(), added a global variable to keep the error number and initialize the pin of the LED automatically.
  • v2.1: Changed initLED to be a static (~hidden) method and also made the global variables static.
  • v2.2: Added peripheral clock enable/disable functionality for energy saving purposes, only added necessary includes in header file, moved the others to the source file, updated documentation, replaced SysTick delay with RTCC delay, changed error delay length.
  • v2.3: Changed name of static variable, simplified some logic.
  • v2.4: Stopped disabling the GPIO clock.
  • v2.5: Moved documentation.
  • v2.6: Updated code with new DEFINE checks.
  • v2.7: Added functionality to send error values using LoRaWAN.
  • v2.8: Added the ability to enable/disable error forwarding to the cloud using a public definition and changed UART error color.
  • v3.0: Updated version number.
  • v3.1: Removed static before the local variables (not necessary).

Todo:
Future improvements:
  • Only send a maximum amount of errors to the could using LoRaWAN according to a defined value.
    • Reset the counter in the MEASURE/SEND state?
  • Go back to INIT state on an error call?
    • GOTO is supported in C but is dangerous to use (nested loops, ...)
    • Check if the clock functionality doesn't break when this is implemented ...

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 util.c.

Function Documentation

◆ error()

void error ( uint8_t  number)

Error method.

ERROR_FORWARDING == 0
The method flashes the LED, displays a UART message and holds the MCU forever in a loop until it gets reset. The error value gets stored in a global variable.

ERROR_FORWARDING == 1
The method sends the error value to the cloud using LoRaWAN if the error number doesn't correspond to LoRaWAN-related functionality (numbers 30 - 55).

Parameters
[in]numberThe number to indicate where in the code the error was thrown.

Definition at line 131 of file util.c.

132 {
133  /* Initialize LED if not already the case */
134  if (!LED_initialized) initLED();
135 
136  /* Save the given number in the global variable */
137  errorNumber = number;
138 
139 #if ERROR_FORWARDING == 0 /* ERROR_FORWARDING */
140 
141 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
142  dbprint_color(">>> Error (", 5);
143  dbprintInt(number);
144  dbprintln_color(")! Please reset MCU. <<<", 5);
145 #endif /* DEBUG_DBPRINT */
146 
147  while (1)
148  {
149  delay(100);
150  GPIO_PinOutToggle(LED_PORT, LED_PIN); /* Toggle LED */
151  }
152 
153 #else /* ERROR_FORWARDING */
154 
155  /* Check if the error number isn't called in LoRaWAN functionality */
156  if ((number < 30) || (number > 55))
157  {
158 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
159  dbprint_color(">>> Error (", 5);
160  dbprintInt(number);
161  dbprintln_color(")! Sending the message to the cloud. <<<", 5);
162 #endif /* DEBUG_DBPRINT */
163 
164  initLoRaWAN(); /* Initialize LoRaWAN functionality */
165 
166  sendStatus(number); /* Send the status value */
167 
168  disableLoRaWAN(); /* Disable RN2483 */
169  }
170  else
171  {
172 
173 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
174  dbprint_color(">>> Error in LoRaWAN functionality (", 5);
175  dbprintInt(number);
176  dbprintln_color(")! <<<", 5);
177 #endif /* DEBUG_DBPRINT */
178 
179  }
180 
181 #endif /* ERROR_FORWARDING */
182 
183 }
void disableLoRaWAN(void)
Disable LoRaWAN functionality.
void initLoRaWAN(void)
Initialize LoRaWAN functionality.
Definition: lora_wrappers.c:92
#define LED_PORT
Definition: pin_mapping.h:85
void sendStatus(uint8_t status)
Send a packet to indicate a status.
bool LED_initialized
Definition: util.c:85
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
#define LED_PIN
Definition: pin_mapping.h:86
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:738
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
uint8_t errorNumber
Definition: util.c:84
static void initLED(void)
Initialize the LED.
Definition: util.c:194
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ initLED()

static void initLED ( void  )
static

Initialize the LED.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.

Definition at line 194 of file util.c.

195 {
196  /* Enable necessary clocks (just in case) */
197  CMU_ClockEnable(cmuClock_HFPER, true); /* GPIO is a High Frequency Peripheral */
198  CMU_ClockEnable(cmuClock_GPIO, true);
199 
200  /* In the case of gpioModePushPull, the last argument directly sets the pin state */
201  GPIO_PinModeSet(LED_PORT, LED_PIN, gpioModePushPull, 0);
202 
203 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
204  dbinfo("LED pin initialized");
205 #endif /* DEBUG_DBPRINT */
206 
207  LED_initialized = true;
208 }
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
#define LED_PORT
Definition: pin_mapping.h:85
bool LED_initialized
Definition: util.c:85
#define LED_PIN
Definition: pin_mapping.h:86

◆ led()

void led ( bool  enabled)

Enable or disable the LED.

This method also initializes the pin-mode if necessary.

Parameters
[in]enabled
  • true - Enable LED.
  • false - Disable LED.

Definition at line 103 of file util.c.

104 {
105  /* Initialize LED if not already the case */
106  if (!LED_initialized) initLED();
107 
108  /* Set the selected state */
109  if (enabled) GPIO_PinOutSet(LED_PORT, LED_PIN);
110  else GPIO_PinOutClear(LED_PORT, LED_PIN);
111 }
#define LED_PORT
Definition: pin_mapping.h:85
bool LED_initialized
Definition: util.c:85
#define LED_PIN
Definition: pin_mapping.h:86
static void initLED(void)
Initialize the LED.
Definition: util.c:194

Variable Documentation

◆ errorNumber

uint8_t errorNumber = 0

Definition at line 84 of file util.c.

◆ LED_initialized

bool LED_initialized = false

Definition at line 85 of file util.c.