Embedded System Design 2 - Project
cable.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file cable.c
3  * @brief Cable checking functionality.
4  * @version 2.1
5  * @author
6  * Matthias Alleman@n
7  * Modified by Brecht Van Eeckhoudt
8  *
9  * ******************************************************************************
10  *
11  * @section Versions
12  *
13  * @li v1.0: Move `checkCable` from `main.c` to this file and add battery voltage measurement logic.
14  * @li v1.1: Updated code with new DEFINE checks.
15  * @li v1.2: Moved ADC functionality to `adc.c`.
16  * @li v1.3: Changed filename to `cable.c`
17  * @li v1.4: Fixed cable checking method.
18  * @li v1.5: Moved more functionality to this file.
19  * @li v2.0: Added functionality to also send taken measurements when a cable break is
20  * detected and updated version number.
21  * @li v2.1: Updated documentation.
22  *
23  * ******************************************************************************
24  *
25  * @todo
26  * **Future improvements:**@n
27  * - Use two parallel lines who normally aren't connected and use an interrupt
28  * to detect if they broke and get connected through the water.
29  *
30  * ******************************************************************************
31  *
32  * @section License
33  *
34  * **Copyright (C) 2019 - 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_device.h" /* Include necessary MCU-specific header file */
62 #include "em_cmu.h" /* Clock management unit */
63 #include "em_gpio.h" /* General Purpose IO */
64 
65 #include "cable.h" /* Corresponding header file */
66 #include "pin_mapping.h" /* PORT and PIN definitions */
67 #include "debug_dbprint.h" /* Enable or disable printing to UART for debugging */
68 #include "lora_wrappers.h" /* LoRaWAN functionality */
69 #include "datatypes.h" /* Definitions of the custom data-types */
70 
71 
72 /* Local variable */
73 /** Keep the amount of times a message has been send to indicate the cable is broken (only send 4 messages in total) */
75 
76 
77 /* Local prototype */
78 static bool checkCable_internal (void);
79 
80 
81 /**************************************************************************//**
82  * @brief
83  * Method to check if the wire is broken.
84  *
85  * @param[in] data
86  * The struct which contains the measurements to send using LoRaWAN.
87  *
88  * @return
89  * @li `true` - Measurements have been send so the index has to be set back to zero.
90  * @li `false` - No LoRaWAN message has been send.
91  *****************************************************************************/
93 {
94  /* Check if the cable is broken */
95  if (!checkCable_internal())
96  {
97  /* Only send a message 4 times */
98  if (cableBrokenSendTimes < 4)
99  {
100 
101 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
102  dbcrit("Cable broken! Sending the data ...");
103 #endif /* DEBUG_DBPRINT */
104 
105  initLoRaWAN(); /* Initialize LoRaWAN functionality */
106 
107  sendCableBroken(true); /* Send the LoRaWAN message */
108  sendMeasurements(data); /* Send the measurements */
109 
110  disableLoRaWAN(); /* Disable RN2483 */
111 
112  cableBrokenSendTimes++; /* Increment the counter */
113 
114  return (true);
115  }
116  else
117  {
118 
119 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
120  dbcrit("Cable broken but no longer sending the data");
121 #endif /* DEBUG_DBPRINT */
122 
123  return (false);
124  }
125  }
126  else
127  {
128 
129 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
130  dbinfo("Cable still intact");
131 #endif /* DEBUG_DBPRINT */
132 
133  return (false);
134  }
135 }
136 
137 
138 /**************************************************************************//**
139  * @brief
140  * Method to check if the wire is broken.
141  *
142  * @details
143  * This method enables the necessary GPIO clocks, sets the mode of the pins,
144  * checks the connection between them and also disables them at the end.
145  *
146  * @note
147  * This is a static method because it's only internally used in this file
148  * and called by other methods if necessary.
149  *
150  * @return
151  * @li `true` - The connection is still okay.
152  * @li `false` - The connection is broken!
153  *****************************************************************************/
154 static bool checkCable_internal (void)
155 {
156  /* Value to eventually return */
157  bool check = false;
158 
159  /* Enable necessary clocks (just in case) */
160  CMU_ClockEnable(cmuClock_HFPER, true); /* GPIO is a High Frequency Peripheral */
161  CMU_ClockEnable(cmuClock_GPIO, true);
162 
163  /* Change mode of first pin */
164  GPIO_PinModeSet(BREAK1_PORT, BREAK1_PIN, gpioModeInput, 0);
165 
166  /* Change mode of second pin and also set it low with the last argument */
167  GPIO_PinModeSet(BREAK2_PORT, BREAK2_PIN, gpioModePushPull, 0);
168 
169  /* Check the connection */
170  if (!GPIO_PinInGet(BREAK1_PORT,BREAK1_PIN)) check = true;
171 
172  /* Disable the pins */
173  GPIO_PinModeSet(BREAK1_PORT, BREAK1_PIN, gpioModeDisabled, 0);
174  GPIO_PinModeSet(BREAK2_PORT, BREAK2_PIN, gpioModeDisabled, 0);
175 
176  return (check);
177 }
static bool checkCable_internal(void)
Method to check if the wire is broken.
Definition: cable.c:154
#define BREAK1_PORT
Definition: pin_mapping.h:109
Definitions of the custom data-types used.
MeasurementData_t data
Definition: main.c:189
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
Cable checking functionality.
void sendMeasurements(MeasurementData_t data)
Send measured battery voltages and internal and external temperatures to the cloud using LoRaWAN...
void sendCableBroken(bool cableBroken)
Send a packet to the cloud using LoRaWAN to indicate that the cable is broken.
void disableLoRaWAN(void)
Disable LoRaWAN functionality.
The pin definitions for the regular and custom Happy Gecko board.
void initLoRaWAN(void)
Initialize LoRaWAN functionality.
Definition: lora_wrappers.c:92
#define BREAK2_PIN
Definition: pin_mapping.h:112
LoRa wrapper methods.
Enable or disable printing to UART with dbprint.
uint8_t cableBrokenSendTimes
Definition: cable.c:74
#define BREAK2_PORT
Definition: pin_mapping.h:111
bool checkCable(MeasurementData_t data)
Method to check if the wire is broken.
Definition: cable.c:92
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
#define BREAK1_PIN
Definition: pin_mapping.h:110