diff --git a/esp32/lib/dirtviz/include/dirtviz.hpp b/esp32/lib/dirtviz/include/dirtviz.hpp index 7fffdf3d..796edb43 100644 --- a/esp32/lib/dirtviz/include/dirtviz.hpp +++ b/esp32/lib/dirtviz/include/dirtviz.hpp @@ -1,25 +1,24 @@ /** * @brief Library used to interface with the Dirtviz API - * + * * Assumes that WiFi interface is connected to the network. - * + * * @author John Madden * @date 2023-11-29 -*/ - -#include -#include -#include + */ #include #include +#include +#include +#include + /** * @brief HTTP interface for Dirtviz API -*/ -class Dirtviz -{ -private: + */ +class Dirtviz { + private: /** URL of API */ char *url = nullptr; @@ -29,73 +28,72 @@ class Dirtviz /** Buffer for the HTTP response */ char *response = nullptr; - -public: + public: /** * @brief Default constructor - * + * * Allows for setting of URL on initialization. - * + * * @param url API URL * @param port API port number - */ + */ Dirtviz(const char *url, const uint16_t &port); /** * @brief Frees all memory - */ + */ ~Dirtviz(); /** * @brief Setter for @p url - * + * * @param new_url New API URL - */ + */ void SetUrl(const char *new_url); /** * @brief Getter for @p url - * + * * @returns Pointer to @p url - */ + */ const char *GetUrl(void) const; /** * @brief Setter for @p port - * + * * @param new_port New port number - */ + */ void SetPort(const uint16_t &new_port); /** * @brief Getter for @p port - * + * * @returns value of @p url - */ + */ uint16_t GetPort(void) const; /** * @brief Send serialized measurement to the API - * + * * The entire response is stored in a dynamically allocated buffer. Use * Dirtviz::GetResponse to get the binary response data. - * + * * @param meas Pointer to serialized measurement data * @param meas_len Number of bytes in @p meas - * + * * @return HTTP response code, -1 indicates an error in parsing - */ + */ int SendMeasurement(const uint8_t *meas, size_t meas_len); /** * @brief Get binary data from response - * + * * A returned length of 0 indicates an error and the pointer @p data has not * been modified. - * + * * @param data Pointer to response binary data - * + * * @return Length of @p data - */ + */ size_t GetResponse(const uint8_t *data) const; }; \ No newline at end of file diff --git a/esp32/lib/dirtviz/src/dirtviz.cpp b/esp32/lib/dirtviz/src/dirtviz.cpp index 077bb107..c27b8cc4 100644 --- a/esp32/lib/dirtviz/src/dirtviz.cpp +++ b/esp32/lib/dirtviz/src/dirtviz.cpp @@ -1,64 +1,55 @@ /** * @see dirtviz.hpp - * + * * @author John Madden * @date 2023-11-29 -*/ + */ -#include #include "dirtviz.hpp" -Dirtviz::Dirtviz(const char *url, const uint16_t &port) : url(nullptr), response(nullptr) -{ +#include + +Dirtviz::Dirtviz(const char *url, const uint16_t &port) + : url(nullptr), response(nullptr) { // set parameters this->SetUrl(url); this->SetPort(port); } -Dirtviz::~Dirtviz() -{ +Dirtviz::~Dirtviz() { // free memory free(this->url); free(this->response); } -void Dirtviz::SetUrl(const char *new_url) -{ +void Dirtviz::SetUrl(const char *new_url) { // get length of new url string, add 1 for null char size_t url_len = strlen(new_url); ++url_len; // allocate memory - char * temp_url = (char *) realloc(this->url, url_len); + char *temp_url = (char *)realloc(this->url, url_len); if (temp_url != nullptr) { this->url = temp_url; - strcpy(this->url, new_url); // strcpy is safe here because we just allocated enough space + strcpy( + this->url, + new_url); // strcpy is safe here because we just allocated enough space } else { - // Handle allocation failure (e.g., set an error flag, use a default URL, etc.) - + // Handle allocation failure (e.g., set an error flag, use a default URL, + // etc.) } } -const char *Dirtviz::GetUrl(void) const -{ - return this->url; -} +const char *Dirtviz::GetUrl(void) const { return this->url; } -void Dirtviz::SetPort(const uint16_t &new_port) -{ - this->port = new_port; -} +void Dirtviz::SetPort(const uint16_t &new_port) { this->port = new_port; } -uint16_t Dirtviz::GetPort(void) const -{ - return this->port; -} +uint16_t Dirtviz::GetPort(void) const { return this->port; } -int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) -{ +int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) { // WiFi client for connection with API - //WiFiClientSecure client; + // WiFiClientSecure client; WiFiClient client; // buffer for making post requests @@ -67,10 +58,9 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) Serial.print(":"); Serial.print(this->port); // try connection return negative length if error - //client.setInsecure(); - //client.setCACert(rootCACertificate); - if (!client.connect(this->url, this->port)) - { + // client.setInsecure(); + // client.setCACert(rootCACertificate); + if (!client.connect(this->url, this->port)) { Serial.println("Connection failure"); return -1; } @@ -94,14 +84,12 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) // newline indicating end of headers client.println(); // send data - for (int idx = 0; idx < meas_len; ++idx) - { + for (int idx = 0; idx < meas_len; ++idx) { client.write(meas[idx]); } - // read response - + // get length of response int resp_len = client.available(); @@ -110,7 +98,7 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) this->response = nullptr; // allocate memory - this->response = (char *) realloc(this->response, resp_len + 1); + this->response = (char *)realloc(this->response, resp_len + 1); // copy into buffer if (this->response != nullptr) { @@ -119,7 +107,7 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) while (client.available() && bytesRead < resp_len) { this->response[bytesRead++] = client.read(); } - this->response[bytesRead] = '\0'; // Null-terminate the response + this->response[bytesRead] = '\0'; // Null-terminate the response } else { Serial.println("Null pointer failure"); return -1; @@ -129,38 +117,33 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) // disconnect after message is sent client.stop(); - // find status code int status_code; - if (sscanf(this->response, "%*s %d", &status_code) != 1) - { + if (sscanf(this->response, "%*s %d", &status_code) != 1) { Serial.println("Unable to parse status code"); return -1; } - return status_code; + return status_code; } -size_t Dirtviz::GetResponse(const uint8_t *data) const -{ +size_t Dirtviz::GetResponse(const uint8_t *data) const { // find response length from header // get pointer to start of line const char *length_start = strstr(this->response, "Content-Length:"); - if (length_start == nullptr) - { + if (length_start == nullptr) { return 0; } // parse the length size_t data_len; - if (sscanf(length_start, "%*s %u", &data_len)) - { + if (sscanf(length_start, "%*s %u", &data_len)) { return 0; } // read binary data, look for double CRLF - data = (const uint8_t *) strstr(this->response, "\r\n\r\n"); + data = (const uint8_t *)strstr(this->response, "\r\n\r\n"); data += 4; // return the length of data diff --git a/esp32/src/examples/example_dirtviz.cpp b/esp32/src/examples/example_dirtviz.cpp index 072c389b..441b35de 100644 --- a/esp32/src/examples/example_dirtviz.cpp +++ b/esp32/src/examples/example_dirtviz.cpp @@ -1,6 +1,6 @@ /** * @brief Example of using dirtviz library - * + * * @author John Madden * @date 2023-11-30 */ @@ -23,15 +23,16 @@ const size_t data_len = 21; /** * @brief Initialization code run on startup - * - * -*/ -void setup() -{ + * + * + */ +void setup() { // Start serial interface Serial.begin(115200); // Wait for serial connection - while (!Serial) { delay(100); } + while (!Serial) { + delay(100); + } Serial.print("SSID: "); Serial.println(ssid); @@ -43,8 +44,7 @@ void setup() WiFi.begin(ssid, pass); // Wait for WiFi to connect - while (WiFi.status() != WL_CONNECTED) - { + while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } @@ -54,15 +54,14 @@ void setup() Serial.println(WiFi.localIP()); } -void loop() -{ +void loop() { int resp_code; const uint8_t *resp_data; size_t resp_data_len; // Send example measurement - resp_code = api.SendMeasurement((const uint8_t*) data, data_len); + resp_code = api.SendMeasurement((const uint8_t *)data, data_len); Serial.print("Response Code: "); Serial.println(resp_code); diff --git a/esp32/src/examples/example_ntp.cpp b/esp32/src/examples/example_ntp.cpp index ff457ec4..611aa877 100644 --- a/esp32/src/examples/example_ntp.cpp +++ b/esp32/src/examples/example_ntp.cpp @@ -1,13 +1,13 @@ /** * @brief Example of using ESP32 as an NTP client - * + * * @author Varun Sreedharan * @date 2024-09-03 */ #include -#include #include +#include /** Baud rate for serial interface */ #define SERIAL_BAUD 115200 @@ -21,47 +21,49 @@ NTPClient timeClient(ntpUDP); String currentTime; void setup() { - // Start serial interface - Serial.begin(115200); - // Wait for serial connection - while (!Serial) { delay(100); } + // Start serial interface + Serial.begin(115200); + // Wait for serial connection + while (!Serial) { + delay(100); + } - Serial.print("SSID: "); - Serial.println(ssid); - Serial.print("Password: "); - Serial.println(pass); - Serial.print("Connecting"); + Serial.print("SSID: "); + Serial.println(ssid); + Serial.print("Password: "); + Serial.println(pass); + Serial.print("Connecting"); - // Connect to WiFi network - WiFi.begin(ssid, pass); + // Connect to WiFi network + WiFi.begin(ssid, pass); - // Wait for WiFi to connect - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(""); - Serial.println("Connected!"); - Serial.println(WiFi.localIP()); + // Wait for WiFi to connect + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("Connected!"); + Serial.println(WiFi.localIP()); - timeClient.begin(); - // Set time offset in seconds - // GMT = 0 - // California: PST (GMT -7 hours) = -25200 - // Georgia: EST (GMT -4 hours) = -14400 - // Illinois: CDT (GMT -5 hours) = -18000 - timeClient.setTimeOffset(-28800); + timeClient.begin(); + // Set time offset in seconds + // GMT = 0 + // California: PST (GMT -7 hours) = -25200 + // Georgia: EST (GMT -4 hours) = -14400 + // Illinois: CDT (GMT -5 hours) = -18000 + timeClient.setTimeOffset(-28800); } void loop() { - // Prints time in HH:MM:SS format - while (!timeClient.update()) { - timeClient.forceUpdate(); - } - currentTime = timeClient.getFormattedTime(); - Serial.print("The time is: "); - Serial.println(currentTime); + // Prints time in HH:MM:SS format + while (!timeClient.update()) { + timeClient.forceUpdate(); + } + currentTime = timeClient.getFormattedTime(); + Serial.print("The time is: "); + Serial.println(currentTime); - // Wait 1 second - delay(1000); + // Wait 1 second + delay(1000); } diff --git a/esp32/src/soil_power_sensor.cpp b/esp32/src/soil_power_sensor.cpp index 1dd8cb9e..71388e09 100644 --- a/esp32/src/soil_power_sensor.cpp +++ b/esp32/src/soil_power_sensor.cpp @@ -1,6 +1,6 @@ /** * @brief Main file for the Soil Power Sensor firmware - * + * * @author John Madden * @date 2023-11-28 */ @@ -23,15 +23,16 @@ const size_t data_len = 12; /** * @brief Initialization code run on startup - * - * -*/ -void setup() -{ + * + * + */ +void setup() { // Start serial interface Serial.begin(115200); // Wait for serial connection - while (!Serial) { delay(100); } + while (!Serial) { + delay(100); + } Serial.print("SSID: "); Serial.println(ssid); @@ -43,8 +44,7 @@ void setup() WiFi.begin(ssid, pass); // Wait for WiFi to connect - while (WiFi.status() != WL_CONNECTED) - { + while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } @@ -54,16 +54,15 @@ void setup() Serial.println(WiFi.localIP()); } -void loop() -{ +void loop() { // Buffer to store response static uint8_t resp[256]; static size_t resp_len; // Send example measurement - resp_len = api.SendMeasurement((const uint8_t*) data, data_len); + resp_len = api.SendMeasurement((const uint8_t*)data, data_len); // Print response Serial.print("Response:"); - Serial.println((char*) resp); + Serial.println((char*)resp); } diff --git a/proto/c/include/transcoder.h b/proto/c/include/transcoder.h index 3c2feca6..da8a1b1c 100644 --- a/proto/c/include/transcoder.h +++ b/proto/c/include/transcoder.h @@ -1,18 +1,18 @@ /** * @file transcoder.h - * + * * @brief Library for encoding/decoding protobuf messages - * + * * Encoding functions are provided for each measurement type. A decoding * function is provided for a Response message. If more functionality is * required, use the protobuf generated located in soil_power_sensor.pb.h. - * + * * @see transcoder.c * @see test_transcoder.c - * + * * @author John Madden * @date 2024-01-04 -*/ + */ #ifndef __TRANSCODER_H__ #define __TRANSCODER_H__ @@ -27,12 +27,12 @@ extern "C" { /** * @brief Encodes a power measurement - * + * * The timestamp is not able to encode timezones and is references from UTC+0. * The serialized data is stored in @p buffer with the number of bytes written * being returned by the function. A return value of -1 indicates an error in * encoding. - * + * * @param ts Unix epochs * @param logger_id Logger Id * @param cell_id Cell Id @@ -40,19 +40,18 @@ extern "C" { * @param current Current measured in uA * @param buffer Buffer to store serialized measurement * @return Number of bytes in @p buffer -*/ -size_t EncodePowerMeasurement(uint32_t ts, uint32_t logger_id, - uint32_t cell_id, double voltage, - double current, uint8_t *buffer); + */ +size_t EncodePowerMeasurement(uint32_t ts, uint32_t logger_id, uint32_t cell_id, + double voltage, double current, uint8_t *buffer); /** * @brief Encodes a Teros12 measurement - * + * * The timestamp is not able to encode timezones and is references from UTC+0. * The serialized data is stored in @p buffer with the number of bytes written * being returned by the function. A return value of -1 indicates an error in * encoding. - * + * * @param ts Timestamp * @param logger_id Logger Id * @param cell_id Cell Id @@ -62,7 +61,7 @@ size_t EncodePowerMeasurement(uint32_t ts, uint32_t logger_id, * @param ec Electrical conductivity * @param buffer Buffer to store serialized measurement * @return Number of bytes in @p buffer -*/ + */ size_t EncodeTeros12Measurement(uint32_t ts, uint32_t logger_id, uint32_t cell_id, double vwc_raw, double vwc_adj, double temp, uint32_t ec, @@ -70,15 +69,15 @@ size_t EncodeTeros12Measurement(uint32_t ts, uint32_t logger_id, /** * @brief Encodes a Phytos31 measurement - * + * * Currently only the voltage measurement is used. Leaf wetness will * be implemented once more is known about the sensor. - * + * * The timestamp is not able to encode timezones and is references from UTC+0. * The serialized data is stored in @p buffer with the number of bytes written * being returned by the function. A return value of -1 indicates an error in * encoding. - * + * * @param ts Timestamp * @param logger_id Logger Id * @param cell_id Cell Id @@ -93,16 +92,16 @@ size_t EncodePhytos31Measurement(uint32_t ts, uint32_t logger_id, /** * @brief Decodes a response message - * + * * Take bytes in @p data withy length @p len and decodes into a response type. * Neither @p data or @p len are modified. A return value of -1 indicates an * error in decoding. See soil_power_sensor.proto for a full list of response * types. - * + * * @param data Protobuf serialized data * @param len Number of bytes in @p data * @return Response type -*/ + */ Response_ResponseType DecodeResponse(const uint8_t *data, const size_t len); #ifdef __cplusplus diff --git a/proto/c/src/transcoder.c b/proto/c/src/transcoder.c index 8a98dd0d..318bf0db 100644 --- a/proto/c/src/transcoder.c +++ b/proto/c/src/transcoder.c @@ -1,35 +1,32 @@ /** * @file transcoder.c - * + * * @see transcoder.h - * + * * @author John Madden * @date 2023-01-04 -*/ + */ #include "transcoder.h" -#include "pb_encode.h" #include "pb_decode.h" - +#include "pb_encode.h" /** * @brief Encodes a measurement - * + * * Serializes a generic measurement using protobuf and stores result in buffer. * The resulting number of bytes is returned with -1 indicating there was an * error. - * + * * @param meas Measurement * @param buffer Buffer to store serialized measurement * @return Length of buffer, -1 indicates there was an error -*/ + */ size_t EncodeMeasurement(Measurement *meas, uint8_t *buffer); -size_t EncodePowerMeasurement(uint32_t ts, uint32_t logger_id, - uint32_t cell_id, double voltage, - double current, uint8_t *buffer) -{ +size_t EncodePowerMeasurement(uint32_t ts, uint32_t logger_id, uint32_t cell_id, + double voltage, double current, uint8_t *buffer) { Measurement meas = Measurement_init_zero; meas.has_meta = true; @@ -41,16 +38,14 @@ size_t EncodePowerMeasurement(uint32_t ts, uint32_t logger_id, meas.which_measurement = Measurement_power_tag; meas.measurement.power.voltage = voltage; meas.measurement.power.current = current; - + return EncodeMeasurement(&meas, buffer); } - size_t EncodeTeros12Measurement(uint32_t ts, uint32_t logger_id, uint32_t cell_id, double vwc_raw, double vwc_adj, double temp, uint32_t ec, - uint8_t *buffer) -{ + uint8_t *buffer) { Measurement meas = Measurement_init_zero; meas.has_meta = true; @@ -70,8 +65,7 @@ size_t EncodeTeros12Measurement(uint32_t ts, uint32_t logger_id, size_t EncodePhytos31Measurement(uint32_t ts, uint32_t logger_id, uint32_t cell_id, double voltage, - double leaf_wetness, uint8_t *buffer) -{ + double leaf_wetness, uint8_t *buffer) { Measurement meas = Measurement_init_zero; meas.has_meta = true; @@ -87,16 +81,14 @@ size_t EncodePhytos31Measurement(uint32_t ts, uint32_t logger_id, return EncodeMeasurement(&meas, buffer); } -Response_ResponseType DecodeResponse(const uint8_t *data, const size_t len) -{ +Response_ResponseType DecodeResponse(const uint8_t *data, const size_t len) { Response resp; // create input buffer pb_istream_t istream = pb_istream_from_buffer(data, len); // decode data and check status bool status = pb_decode(&istream, Response_fields, &resp); - if (!status) - { + if (!status) { return -1; } @@ -104,14 +96,12 @@ Response_ResponseType DecodeResponse(const uint8_t *data, const size_t len) return resp.resp; } -size_t EncodeMeasurement(Measurement *meas, uint8_t *buffer) -{ +size_t EncodeMeasurement(Measurement *meas, uint8_t *buffer) { // create output stream pb_ostream_t ostream = pb_ostream_from_buffer(buffer, 256); // encode message and check rc bool status = pb_encode(&ostream, Measurement_fields, meas); - if (!status) - { + if (!status) { return -1; } diff --git a/stm32/Src/examples/calibrate_adc.c b/stm32/Src/examples/calibrate_adc.c index 37e18362..f649de72 100644 --- a/stm32/Src/examples/calibrate_adc.c +++ b/stm32/Src/examples/calibrate_adc.c @@ -1,41 +1,40 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2023 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "adc.h" +#include "app_lorawan.h" #include "dma.h" +#include "gpio.h" #include "i2c.h" -#include "app_lorawan.h" #include "usart.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include #include - -#include "sys_app.h" #include -#include #include "ads.h" -#include "sdi12.h" #include "rtc.h" +#include "sdi12.h" #include "stm32_timer.h" +#include "sys_app.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -66,22 +65,22 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ - + /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* USER CODE BEGIN Init */ @@ -101,7 +100,8 @@ int main(void) MX_I2C2_Init(); /*Initialize timer and RTC*/ - /*Have to be initilized in example files because LoRaWan cannot be initialized like in main*/ + /*Have to be initilized in example files because LoRaWan cannot be initialized + * like in main*/ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); UTIL_TIMER_Init(); @@ -111,16 +111,12 @@ int main(void) // Print the compilation time at startup char info_str[100]; int info_len; - info_len = sprintf( - info_str, - "Soil Power Sensor Wio-E5 firmware, compiled on %s %s\n", - __DATE__,__TIME__ - ); - HAL_UART_Transmit(&huart1, (const uint8_t *) info_str, info_len, 1000); - + info_len = sprintf(info_str, + "Soil Power Sensor Wio-E5 firmware, compiled on %s %s\n", + __DATE__, __TIME__); + HAL_UART_Transmit(&huart1, (const uint8_t *)info_str, info_len, 1000); /* USER CODE BEGIN 2 */ - char output[20]; char output2[20]; @@ -133,51 +129,51 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ - while (1) - { - - /* USER CODE END WHILE */ + while (1) { + /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - HAL_UART_Receive(&huart1, (uint8_t *) controller_input, 2, 1000); + HAL_UART_Receive(&huart1, (uint8_t *)controller_input, 2, 1000); - if (controller_input[0] == '0'){ + if (controller_input[0] == '0') { voltage_reading = ADC_readVoltage(); reading_len = sprintf(output, "Voltage: %f\r\n", voltage_reading); - HAL_UART_Transmit(&huart1, (const uint8_t *) output, reading_len, HAL_MAX_DELAY); + HAL_UART_Transmit(&huart1, (const uint8_t *)output, reading_len, + HAL_MAX_DELAY); current_reading = ADC_readCurrent(); reading_len = sprintf(output2, "Current: %f\r\n", current_reading); - HAL_UART_Transmit(&huart1, (const uint8_t *) output2, reading_len, HAL_MAX_DELAY); + HAL_UART_Transmit(&huart1, (const uint8_t *)output2, reading_len, + HAL_MAX_DELAY); controller_input[0] = 'n'; } HAL_Delay(100); - /* USER CODE END 3 */ + /* USER CODE END 3 */ } } /** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure LSE Drive Capability - */ + */ HAL_PWR_EnableBkUpAccess(); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); /** Configure the main internal regulator output voltage - */ + */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI; + */ + RCC_OscInitStruct.OscillatorType = + RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; @@ -189,67 +185,61 @@ void SystemClock_Config(void) RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK - |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 - |RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | + RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV5; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); } - - /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ char error[30]; int error_len = sprintf(error, "Error! HAL Status: %d\n", rc); - HAL_UART_Transmit(&huart1, (const uint8_t *) error, error_len, 1000); + HAL_UART_Transmit(&huart1, (const uint8_t *)error, error_len, 1000); /* User can add his own implementation to report the HAL error return state */ __disable_irq(); - while (1) - { + while (1) { } /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/stm32/Src/examples/example_adc.c b/stm32/Src/examples/example_adc.c index 07c2c705..060afe48 100644 --- a/stm32/Src/examples/example_adc.c +++ b/stm32/Src/examples/example_adc.c @@ -1,40 +1,39 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2023 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "adc.h" +#include "app_lorawan.h" #include "dma.h" +#include "gpio.h" #include "i2c.h" -#include "app_lorawan.h" #include "usart.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include #include - -#include "sys_app.h" #include -#include #include "ads.h" -#include "sdi12.h" #include "rtc.h" +#include "sdi12.h" +#include "sys_app.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -65,22 +64,22 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ - + /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* USER CODE BEGIN Init */ @@ -100,7 +99,8 @@ int main(void) MX_I2C2_Init(); /*Initialize timer and RTC*/ - /*Have to be initilized in example files because LoRaWan cannot be initialized like in main*/ + /*Have to be initilized in example files because LoRaWan cannot be initialized + * like in main*/ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); UTIL_TIMER_Init(); @@ -110,13 +110,10 @@ int main(void) // Print the compilation time at startup char info_str[100]; int info_len; - info_len = sprintf( - info_str, - "Soil Power Sensor Wio-E5 firmware, compiled on %s %s\n", - __DATE__,__TIME__ - ); - HAL_UART_Transmit(&huart1, (const uint8_t *) info_str, info_len, 1000); - + info_len = sprintf(info_str, + "Soil Power Sensor Wio-E5 firmware, compiled on %s %s\n", + __DATE__, __TIME__); + HAL_UART_Transmit(&huart1, (const uint8_t *)info_str, info_len, 1000); /* USER CODE BEGIN 2 */ ADC_init(); @@ -133,111 +130,106 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ - while (1) - { - - /* USER CODE END WHILE */ + while (1) { + /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ voltage_reading = ADC_readVoltage(); reading_len = sprintf(output, "Voltage: %f\r\n", voltage_reading); - HAL_UART_Transmit(&huart1, (const uint8_t *) output, reading_len, HAL_MAX_DELAY); + HAL_UART_Transmit(&huart1, (const uint8_t *)output, reading_len, + HAL_MAX_DELAY); current_reading = ADC_readCurrent(); reading_len = sprintf(output2, "Current: %f\r\n", current_reading); - HAL_UART_Transmit(&huart1, (const uint8_t *) output2, reading_len, HAL_MAX_DELAY); + HAL_UART_Transmit(&huart1, (const uint8_t *)output2, reading_len, + HAL_MAX_DELAY); - HAL_Delay(1000); + HAL_Delay(1000); } /* USER CODE END 3 */ } /** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure LSE Drive Capability - */ + */ HAL_PWR_EnableBkUpAccess(); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); /** Configure the main internal regulator output voltage - */ + */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI; + */ + RCC_OscInitStruct.OscillatorType = + RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK - |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 - |RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | + RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); } - /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ char error[30]; int error_len = sprintf(error, "Error! HAL Status: %d\n", rc); - HAL_UART_Transmit(&huart1, (const uint8_t *) error, error_len, 1000); + HAL_UART_Transmit(&huart1, (const uint8_t *)error, error_len, 1000); /* User can add his own implementation to report the HAL error return state */ __disable_irq(); - while (1) - { + while (1) { } /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/stm32/Src/examples/example_battery.c b/stm32/Src/examples/example_battery.c index 2d3aaf56..da1335da 100644 --- a/stm32/Src/examples/example_battery.c +++ b/stm32/Src/examples/example_battery.c @@ -1,24 +1,26 @@ /** * @file test_battery.c * @brief Prints out battery voltage levels - * - * In an infinite loop the battery voltage level is retrieved then outputted over serial. The user should check if the voltage levels are expected. When connected to USB the voltage should be ~5V. The battery voltage level should be checked and compared to a multimeter measurement. - * + * + * In an infinite loop the battery voltage level is retrieved then outputted + * over serial. The user should check if the voltage levels are expected. When + * connected to USB the voltage should be ~5V. The battery voltage level should + * be checked and compared to a multimeter measurement. + * * @see battery.h - * + * * @author John Madden * @date 2023-11-17 -*/ - -#include "main.h" -#include "adc.h" -#include "dma.h" -#include "usart.h" -#include "gpio.h" + */ #include +#include "adc.h" #include "battery.h" +#include "dma.h" +#include "gpio.h" +#include "main.h" +#include "usart.h" /** Delay between print statements */ #ifndef DELAY @@ -26,22 +28,22 @@ #endif void SystemClock_Config(void); - + /** Global variable for all return codes */ HAL_StatusTypeDef rc; /** - * @brief Entry point for battery test - * @retval int - */ -int main(void) -{ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + * @brief Entry point for battery test + * @retval int + */ +int main(void) { + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); - + /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); @@ -55,69 +57,65 @@ int main(void) char info_str[128]; int info_len; info_len = sprintf( - info_str, - "Soil Power Sensor Wio-E5 firmware, test: %s, compiled on %s %s\n", - __FILE__, __DATE__, __TIME__ - ); - HAL_UART_Transmit(&huart1, (const uint8_t *) info_str, info_len, 1000); + info_str, + "Soil Power Sensor Wio-E5 firmware, test: %s, compiled on %s %s\n", + __FILE__, __DATE__, __TIME__); + HAL_UART_Transmit(&huart1, (const uint8_t *)info_str, info_len, 1000); // Infinite loop - while (1) - { + while (1) { // Print voltage level char buf[32]; int buf_len = sprintf(buf, "Battery Voltage: %d mV\n", battery_voltage()); - HAL_UART_Transmit(&huart1, (const uint8_t *) buf, buf_len, 1000); + HAL_UART_Transmit(&huart1, (const uint8_t *)buf, buf_len, 1000); // Sleep - //HAL_Delay(DELAY); + // HAL_Delay(DELAY); - for (int i=0; i < 1000000; i++) { + for (int i = 0; i < 1000000; i++) { __NOP(); } } } /** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage - */ + */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI; + */ + RCC_OscInitStruct.OscillatorType = + RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK - |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 - |RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | + RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) - { + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); @@ -128,40 +126,37 @@ void SystemClock_Config(void) /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); - /* USER CODE BEGIN Error_Handler_Debug */ char error[30]; int error_len = sprintf(error, "Error! HAL Status: %d\n", rc); - HAL_UART_Transmit(&huart1, (const uint8_t *) error, error_len, 1000); + HAL_UART_Transmit(&huart1, (const uint8_t *)error, error_len, 1000); /* User can add his own implementation to report the HAL error return state */ __disable_irq(); - while (1) - { + while (1) { } /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/stm32/Src/examples/example_phytos.c b/stm32/Src/examples/example_phytos.c index 6ea97325..3944ccad 100644 --- a/stm32/Src/examples/example_phytos.c +++ b/stm32/Src/examples/example_phytos.c @@ -1,41 +1,40 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2023 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "adc.h" +#include "app_lorawan.h" #include "dma.h" +#include "gpio.h" #include "i2c.h" -#include "app_lorawan.h" #include "usart.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include #include - -#include "sys_app.h" #include -#include #include "ads.h" -#include "sdi12.h" -#include "rtc.h" #include "phytos31.h" +#include "rtc.h" +#include "sdi12.h" +#include "sys_app.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -66,22 +65,22 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ - + /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* USER CODE BEGIN Init */ @@ -101,24 +100,21 @@ int main(void) MX_I2C2_Init(); /*Initialize timer and RTC*/ - /*Have to be initilized in example files because LoRaWan cannot be initialized like in main*/ + /*Have to be initilized in example files because LoRaWan cannot be initialized + * like in main*/ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); UTIL_TIMER_Init(); - - //TIMER_IF_Init(); + // TIMER_IF_Init(); /* USER CODE BEGIN 2 */ // Print the compilation time at startup char info_str[100]; int info_len; - info_len = sprintf( - info_str, - "Soil Power Sensor Wio-E5 firmware, compiled on %s %s\n", - __DATE__,__TIME__ - ); - HAL_UART_Transmit(&huart1, (const uint8_t *) info_str, info_len, 1000); - + info_len = sprintf(info_str, + "Soil Power Sensor Wio-E5 firmware, compiled on %s %s\n", + __DATE__, __TIME__); + HAL_UART_Transmit(&huart1, (const uint8_t *)info_str, info_len, 1000); /* USER CODE BEGIN 2 */ Phytos31Init(); @@ -137,22 +133,22 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ - while (1) - { - - /* USER CODE END WHILE */ + while (1) { + /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - measurment = Phytos31GetMeasurment(); - reading_len = sprintf(output, "Phytos Raw: %f\r\n", measurment.phytos31_raw); - HAL_UART_Transmit(&huart1, (const uint8_t *) output, reading_len, HAL_MAX_DELAY); + reading_len = + sprintf(output, "Phytos Raw: %f\r\n", measurment.phytos31_raw); + HAL_UART_Transmit(&huart1, (const uint8_t *)output, reading_len, + HAL_MAX_DELAY); // for (int i = 0; i < 10000; i++){ // asm("nop"); // } - HAL_Delay(1000); //I guess HAL_Delay is broken somehow, don't understand why + HAL_Delay( + 1000); // I guess HAL_Delay is broken somehow, don't understand why // for (int i = 0; i < 1000000; i++){ // asm("nop"); // } @@ -161,91 +157,86 @@ int main(void) } /** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure LSE Drive Capability - */ + */ HAL_PWR_EnableBkUpAccess(); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); /** Configure the main internal regulator output voltage - */ + */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI; + */ + RCC_OscInitStruct.OscillatorType = + RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK - |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 - |RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | + RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); } - /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ char error[30]; int error_len = sprintf(error, "Error! HAL Status: %d\n", rc); - HAL_UART_Transmit(&huart1, (const uint8_t *) error, error_len, 1000); + HAL_UART_Transmit(&huart1, (const uint8_t *)error, error_len, 1000); /* User can add his own implementation to report the HAL error return state */ __disable_irq(); - while (1) - { + while (1) { } /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/stm32/Src/examples/example_sdi12.c b/stm32/Src/examples/example_sdi12.c index 7e003f1f..38aa90f9 100644 --- a/stm32/Src/examples/example_sdi12.c +++ b/stm32/Src/examples/example_sdi12.c @@ -2,26 +2,29 @@ * Copyright 2024 jLab * @file test_battery.c * @brief Prints out battery voltage levels - * - * In an infinite loop the battery voltage level is retrieved then outputted over serial. The user should check if the voltage levels are expected. When connected to USB the voltage should be ~5V. The battery voltage level should be checked and compared to a multimeter measurement. - * + * + * In an infinite loop the battery voltage level is retrieved then outputted + * over serial. The user should check if the voltage levels are expected. When + * connected to USB the voltage should be ~5V. The battery voltage level should + * be checked and compared to a multimeter measurement. + * * @see battery.h - * + * * @author John Madden * @date 2023-11-17 -*/ + */ #include -#include "main.h" #include "app_lorawan.h" -#include "usart.h" #include "gpio.h" #include "lptim.h" +#include "main.h" +#include "rtc.h" #include "sdi12.h" #include "stm32_timer.h" -#include "rtc.h" #include "sys_app.h" +#include "usart.h" /** Delay between print statements */ #ifndef DELAY @@ -34,11 +37,11 @@ void SystemClock_Config(void); HAL_StatusTypeDef rc; /** - * @brief Entry point for battery test - * @retval int - */ + * @brief Entry point for battery test + * @retval int + */ int main(void) { - /* Reset of all peripherals, + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); @@ -71,10 +74,10 @@ int main(void) { char info_str[128]; int info_len; info_len = snprintf( - info_str, sizeof(info_str), - "Soil Power Sensor Wio-E5 firmware, test: %s, compiled on %s %s\n", - __FILE__, __DATE__, __TIME__); - HAL_UART_Transmit(&huart1, (const uint8_t *) info_str, info_len, 1000); + info_str, sizeof(info_str), + "Soil Power Sensor Wio-E5 firmware, test: %s, compiled on %s %s\n", + __FILE__, __DATE__, __TIME__); + HAL_UART_Transmit(&huart1, (const uint8_t *)info_str, info_len, 1000); char success[] = "HAL_OK\n"; char failure[] = "HAL_FAIL\n"; char buffer[20]; @@ -87,12 +90,11 @@ int main(void) { char buf[32]; int buf_len = snprintf(buf, sizeof(buf), "0M!"); - - if (SDI12GetMeasurment(addr, &measurment_info, buffer, 3000) == HAL_OK) { - HAL_UART_Transmit(&huart1, (const uint8_t *) success, 7, 100); + if (SDI12GetMeasurment(addr, &measurment_info, buffer, 3000) == HAL_OK) { + HAL_UART_Transmit(&huart1, (const uint8_t *)success, 7, 100); HAL_UART_Transmit(&huart1, buffer, 18, 100); } else { - HAL_UART_Transmit(&huart1, (const uint8_t *) failure, 10, 100); + HAL_UART_Transmit(&huart1, (const uint8_t *)failure, 10, 100); } // Sleep @@ -104,26 +106,26 @@ int main(void) { } /** - * @brief System Clock Configuration - * @retval None - */ + * @brief System Clock Configuration + * @retval None + */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure LSE Drive Capability - */ + */ HAL_PWR_EnableBkUpAccess(); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); /** Configure the main internal regulator output voltage - */ + */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE| - RCC_OSCILLATORTYPE_MSI; + */ + RCC_OscInitStruct.OscillatorType = + RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; @@ -140,10 +142,10 @@ void SystemClock_Config(void) { } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK - |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 - |RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | + RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV5; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; @@ -160,17 +162,17 @@ void SystemClock_Config(void) { /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ void Error_Handler(void) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); - /* USER CODE BEGIN Error_Handler_Debug */ char error[30]; - int error_len = snprintf(error, sizeof(error), "Error! HAL Status: %d\n", rc); - HAL_UART_Transmit(&huart1, (const uint8_t *) error, error_len, 1000); + int error_len = + snprintf(error, sizeof(error), "Error! HAL Status: %d\n", rc); + HAL_UART_Transmit(&huart1, (const uint8_t *)error, error_len, 1000); /* User can add his own implementation to report the HAL error return state */ __disable_irq(); @@ -179,18 +181,19 @@ void Error_Handler(void) { /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/stm32/lib/ads/include/ads.h b/stm32/lib/ads/include/ads.h index 2ad6e7e4..1ccf2475 100644 --- a/stm32/lib/ads/include/ads.h +++ b/stm32/lib/ads/include/ads.h @@ -18,13 +18,12 @@ extern "C" { #include +#include "../../../../../.platformio/packages/framework-stm32cubewl/Utilities/misc/stm32_systime.h" +#include "../../proto/c/include/transcoder.h" #include "../Inc/i2c.h" +#include "../Inc/timestamp.h" #include "../Inc/usart.h" -#include "../../../../../.platformio/packages/framework-stm32cubewl/Utilities/misc/stm32_systime.h" - #include "../Inc/user_config.h" -#include "../Inc/timestamp.h" -#include "../../proto/c/include/transcoder.h" #define ADS12_WRITE 0x80 #define ADS12_READ 0x81 @@ -39,15 +38,14 @@ extern "C" { #define CURRENT_SLOPE 0 #define CURRENT_B 0 - - /** ****************************************************************************** * @brief This function starts up the ADS1219 -* -* This function is a wrapper for the STM32 HAl I2C library. The ADS1219 uses -* I2C the I2C communication protocol. This function configures then ADS1219 for -* single read mode. Note: the ADS1219 requires a minimum of 500us when it is powered on. +* +* This function is a wrapper for the STM32 HAl I2C library. The +*ADS1219 uses I2C the I2C communication protocol. This function configures then +*ADS1219 for single read mode. Note: the ADS1219 requires a minimum of 500us +*when it is powered on. * * @param void * @return HAL_StatusTypeDef @@ -57,8 +55,9 @@ HAL_StatusTypeDef ADC_init(void); /** ****************************************************************************** -* @brief This function reconfigures the ADS1219 based on the parameter reg_data -* +* @brief This function reconfigures the ADS1219 based on the parameter +*reg_data +* * * @param reg_data * @return HAL_StatusTypeDef @@ -69,10 +68,11 @@ HAL_StatusTypeDef ADC_configure(uint8_t reg_data); /** ****************************************************************************** * @brief This function reads the current ADC voltage value. -* -* This function is a wrapper for the STM32 HAl I2C library. The ADS1219 uses -* I2C the I2C communication protocol. This version simply chops the noisy bits. -* +* +* This function is a wrapper for the STM32 HAl I2C library. The +*ADS1219 uses I2C the I2C communication protocol. This version simply chops the +*noisy bits. +* * @param void * @return double, current ADC reading in microvolts ****************************************************************************** @@ -82,10 +82,11 @@ double ADC_readVoltage(void); /** ****************************************************************************** * @brief This function reads the current ADC ampere value. -* -* This function is a wrapper for the STM32 HAl I2C library. The ADS1219 uses -* I2C the I2C communication protocol. This version simply chops the noisy bits. -* +* +* This function is a wrapper for the STM32 HAl I2C library. The +*ADS1219 uses I2C the I2C communication protocol. This version simply chops the +*noisy bits. +* * @param void * @return double, current ADC reading in microamps ****************************************************************************** @@ -95,7 +96,7 @@ double ADC_readCurrent(void); /** ****************************************************************************** * @brief This function probes the ADS12 to see if it is responsive. -* +* * @param void * @return HAL_StatusTypeDef *******************************************f*********************************** @@ -105,7 +106,7 @@ HAL_StatusTypeDef ADC_probe(void); /** ****************************************************************************** * @brief This function encodes the ADS1219 power measurments into protobuf -* +* * @param *data * @return size_t *******************************************f*********************************** diff --git a/stm32/lib/ads/src/ads.c b/stm32/lib/ads/src/ads.c index d779b7b7..f947e4d2 100644 --- a/stm32/lib/ads/src/ads.c +++ b/stm32/lib/ads/src/ads.c @@ -1,14 +1,15 @@ /** - ****************************************************************************** - * @file ads.c - * @author Stephen Taylor - * @brief Soil Power Sensor ADS12 library - * This file provides a function to read from the onboard ADC (ADS1219). - * @date 11/27/2023 - * - ****************************************************************************** - * Copyright [2023] - **/ + ****************************************************************************** + * @file ads.c + * @author Stephen Taylor + * @brief Soil Power Sensor ADS12 library + * This file provides a function to read from the onboard ADC + *(ADS1219). + * @date 11/27/2023 + * + ****************************************************************************** + * Copyright [2023] + **/ /* Includes ------------------------------------------------------------------*/ #include "../include/ads.h" @@ -17,14 +18,14 @@ /** * @brief GPIO port for adc data ready line - * + * * @see data_ready_pin */ const GPIO_TypeDef* data_ready_port = GPIOC; /** * @brief GPIO pin for adc data ready line - * + * */ const uint16_t data_ready_pin = GPIO_PIN_0; @@ -65,8 +66,8 @@ HAL_StatusTypeDef ADC_init(void) { // Set control register, leaving everything at default except for the VREF, // which will be set to external reference mode - ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, - register_data, 2, HAL_MAX_DELAY); + ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, register_data, 2, + HAL_MAX_DELAY); if (ret != HAL_OK) { return ret; } @@ -92,8 +93,8 @@ HAL_StatusTypeDef ADC_configure(uint8_t reg_data) { // Set control register, leaving everything at default except for the VREF, // which will be set to external reference mode - ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, - register_data, 2, HAL_MAX_DELAY); + ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, register_data, 2, + HAL_MAX_DELAY); if (ret != HAL_OK) { return ret; } @@ -102,7 +103,7 @@ HAL_StatusTypeDef ADC_configure(uint8_t reg_data) { // Send a start code ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, &code, 1, HAL_MAX_DELAY); return ret; - } +} double ADC_readVoltage(void) { uint8_t code; @@ -117,7 +118,8 @@ double ADC_readVoltage(void) { } // Wait for the DRDY pin on the ADS12 to go low, this means data is ready - while (HAL_GPIO_ReadPin(data_ready_port, data_ready_pin)) {} + while (HAL_GPIO_ReadPin(data_ready_port, data_ready_pin)) { + } code = ADS12_READ_DATA_CODE; ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, &code, 1, HAL_MAX_DELAY); if (ret != HAL_OK) { @@ -129,12 +131,12 @@ double ADC_readVoltage(void) { } // Combine the 3 bytes into a 24-bit value - int32_t temp = ((int32_t)rx_data[0] << 16) | ((int32_t)rx_data[1] << 8) - | ((int32_t)rx_data[2]); + int32_t temp = ((int32_t)rx_data[0] << 16) | ((int32_t)rx_data[1] << 8) | + ((int32_t)rx_data[2]); // Check if the sign bit (24th bit) is set if (temp & 0x800000) { - // Extend the sign to 32 bits - temp |= 0xFF000000; + // Extend the sign to 32 bits + temp |= 0xFF000000; } reading = (double)temp; @@ -163,7 +165,8 @@ double ADC_readCurrent(void) { return -1; } // Wait for the DRDY pin on the ADS12 to go low, this means data is ready - while (HAL_GPIO_ReadPin(data_ready_port, data_ready_pin)) {} + while (HAL_GPIO_ReadPin(data_ready_port, data_ready_pin)) { + } code = ADS12_READ_DATA_CODE; ret = HAL_I2C_Master_Transmit(&hi2c2, ADS12_WRITE, &code, 1, HAL_MAX_DELAY); if (ret != HAL_OK) { @@ -174,8 +177,8 @@ double ADC_readCurrent(void) { return -1; } - uint64_t temp = ((uint64_t)rx_data[0] << 16) | ((uint64_t)rx_data[1] << 8) - | ((uint64_t)rx_data[2]); + uint64_t temp = ((uint64_t)rx_data[0] << 16) | ((uint64_t)rx_data[1] << 8) | + ((uint64_t)rx_data[2]); reading = (double)temp; // Uncomment these lines if you wish to see the raw and shifted values @@ -199,7 +202,7 @@ HAL_StatusTypeDef probeADS12(void) { return ret; } -size_t ADC_measure(uint8_t *data) { +size_t ADC_measure(uint8_t* data) { // get timestamp SysTime_t ts = SysTimeGet(); diff --git a/stm32/lib/battery/include/battery.h b/stm32/lib/battery/include/battery.h index 959025c3..35da1819 100644 --- a/stm32/lib/battery/include/battery.h +++ b/stm32/lib/battery/include/battery.h @@ -1,12 +1,14 @@ /** * @file battery.h * @brief Provides declarations for battery monitoring interface. - * - * Assumes the HAL layer is already initialized. The function, @ref battery_init calibrates the ADC and starts continuous conversion to a variable. The most recent battery voltage level is retrieved with @ref battery_voltage. - * + * + * Assumes the HAL layer is already initialized. The function, @ref battery_init + * calibrates the ADC and starts continuous conversion to a variable. The most + * recent battery voltage level is retrieved with @ref battery_voltage. + * * @author John Madden * @date 2023-11-16 -*/ + */ #ifndef __BATTERY_H__ #define __BATTERY_H__ @@ -17,20 +19,20 @@ extern "C" { /** * @brief Initializes the battery monitoring module. - * + * * Starts continuous sampling on ADC. - * + * * @return Return code -*/ + */ HAL_StatusTypeDef battery_init(void); /** * @brief Gets the most recent battery voltage level - * + * * The battery voltage level is in mV. - * + * * @return Battery voltage -*/ + */ unsigned int battery_voltage(void); #ifdef __cplusplus diff --git a/stm32/lib/battery/src/battery.c b/stm32/lib/battery/src/battery.c index c344df83..30d4709e 100644 --- a/stm32/lib/battery/src/battery.c +++ b/stm32/lib/battery/src/battery.c @@ -1,15 +1,15 @@ /** - * @file battery.c - * + * @file battery.c + * * @author John Madden * @date 2023-11-16 -*/ - -#include "main.h" -#include "adc.h" + */ #include "battery.h" +#include "adc.h" +#include "main.h" + /** Voltage reference in mV */ #define VOLTAGE_REF 3300 @@ -24,23 +24,21 @@ /** Private variable that stores battery voltage */ volatile uint32_t adc_reading = 0; -HAL_StatusTypeDef battery_init(void) -{ +HAL_StatusTypeDef battery_init(void) { // Calibrate and start conversion process rc = HAL_ADCEx_Calibration_Start(&hadc); if (rc != HAL_OK) Error_Handler(); // Start continuous measurements - rc = HAL_ADC_Start_DMA(&hadc, (uint32_t *) &adc_reading, 1); + rc = HAL_ADC_Start_DMA(&hadc, (uint32_t *)&adc_reading, 1); if (rc != HAL_OK) Error_Handler(); return rc; } -unsigned int battery_voltage(void) -{ +unsigned int battery_voltage(void) { // Adjust to real voltage value - unsigned int voltage = (unsigned int) adc_reading << 1; + unsigned int voltage = (unsigned int)adc_reading << 1; voltage *= VOLTAGE_REF; voltage /= FULL_SCALE; diff --git a/stm32/lib/fram/include/fifo.h b/stm32/lib/fram/include/fifo.h index 8ecb2bec..098f0220 100644 --- a/stm32/lib/fram/include/fifo.h +++ b/stm32/lib/fram/include/fifo.h @@ -4,20 +4,20 @@ * @file fifo.h * @author Stephen Taylor * @brief Circular buffer as FIFO data structure for buffering measurements - * + * * A circular buffer is implemented on part of the memory space of the fram * chip. The address space used can be modified with FRAM_BUFFER_START and * FRAM_BUFFER_END depending on user needs. - * + * * Measurements are stored with a single uint8_t of their length followed by * the serialized protobuf message. On reads, the length is first read, then * the length number of bytes are read into RAM. Ensure the read buffer used * is of sufficient size. - * + * * The buffer's implementation does not allow for overwriting of data. Once * the buffer is full, indicated by FRAM_BUFFER_FULL, data needs to be removed * by getting the next measurement or clearing the buffer entirely. - * + * * @date 11/17/2023 */ @@ -31,16 +31,13 @@ extern "C" { #include #include "Inc/i2c.h" - #include "include/fram.h" - #ifndef FRAM_BUFFER_START /** Starting address of buffer */ #define FRAM_BUFFER_START 0 #endif /* FRAM_BUFFER_START */ - #ifndef FRAM_BUFFER_END /** Ending address of buffer */ #define FRAM_BUFFER_END 1792 @@ -55,7 +52,7 @@ static const uint16_t kFramBufferSize = FRAM_BUFFER_END - FRAM_BUFFER_START; /** * @brief Puts a measurement into the circular buffer - * + * * @param data An array of data bytes. * @param num_bytes The number of bytes to be written. * @return See FramStatus @@ -64,7 +61,7 @@ FramStatus FramPut(const uint8_t *data, uint16_t num_bytes); /** * @brief Reads a measurement from the queue - * + * * * @param data Array to be read into * @param len Length of data @@ -74,14 +71,14 @@ FramStatus FramGet(uint8_t *data, uint8_t *len); /** * @brief Get the current number of measurements stored in the buffer - * + * * @return Number of measurements */ uint16_t FramBufferLen(void); /** * @brief Clears the buffer - * + * * Read and write addresses are set to their default values allowing for the * buffer to be overwritten. */ diff --git a/stm32/lib/fram/include/fram.h b/stm32/lib/fram/include/fram.h index 0847eee9..a40aa1ab 100644 --- a/stm32/lib/fram/include/fram.h +++ b/stm32/lib/fram/include/fram.h @@ -4,21 +4,21 @@ * @file fram.h * @author Stephen Taylor * @brief This file contains all the function prototypes for the fram.c file - * + * * The FM24CL16B memory architecture is structured around 8 segments of 256 * bytes each. The library can be used with other memory chips with the same * memory layout. The defines FRAM_PAGES and FRAM_SEG_SIZE can used to change * the configuration from the compiler. - * + * * Internally addressing is converted from matrix-style (pages and segments) to * a flat memory address space starting at 0x0. The max available memory - * address is stored in fram_max_addr that can be used for higher level + * address is stored in fram_max_addr that can be used for higher level * implementations. - * + * * Currently user configuration template is provided by this library but will * moved in a future version. - * - * @date 11/17/2023 + * + * @date 11/17/2023 */ #ifndef LIB_FRAM_INCLUDE_FRAM_H_ @@ -28,8 +28,8 @@ extern "C" { #endif -#include #include +#include #include "Inc/i2c.h" @@ -77,7 +77,7 @@ static const uint16_t fram_max_addr = (FRAM_SEG_SIZE * FRAM_PAGES) - 1; /** * @brief Writes bytes to an address * - * + * * * @param addr Address of write * @param data An array of data bytes. @@ -88,7 +88,7 @@ FramStatus FramWrite(uint16_t addr, const uint8_t *data, uint8_t len); /** * @brief This function reads a dynamic number of bytes to FRAM. - * + * * @param addr Address of read * @param data Array to be read into * @param len Number of sequential bytes to read @@ -99,14 +99,14 @@ FramStatus FramRead(uint16_t addr, uint8_t len, uint8_t *data); /** * @brief This function stores user configurable settings to non-volatile * memory. - * + * * Specifically cell ID, logger ID, LoRaWAN gateway EUI, LoRaWAN application * EUI and end device EUI. As well as the logging and upload intervals. - * + * * @param configuration an instance of the typedef struct user_configurations. * Containing all the user defined settings to be stored in non-volatile * memory. - * + * * @return HAL_StatusTypeDef, status of the I2C function */ HAL_StatusTypeDef ConfigureSettings(configuration c); @@ -114,7 +114,7 @@ HAL_StatusTypeDef ConfigureSettings(configuration c); /** * @brief This function reads the user configurable settings from non-volatile * memory. - * + * * @return configuration, an instance of the typedef struct * user_configurations. Containing all the user defined settings to be stored * in non-volatile memory. diff --git a/stm32/lib/fram/src/fifo.c b/stm32/lib/fram/src/fifo.c index 04d620b3..3b9b627a 100644 --- a/stm32/lib/fram/src/fifo.c +++ b/stm32/lib/fram/src/fifo.c @@ -4,7 +4,7 @@ * @file fifo.c * @author Stephen Taylor * @date 11/17/2023 - * + * * @see fifo.h **/ @@ -18,9 +18,9 @@ static uint16_t buffer_len = 0; /** * @brief Updates circular buffer address based on number of bytes - * - * @param addr - * @param num_bytes + * + * @param addr + * @param num_bytes */ static inline void update_addr(uint16_t *addr, const uint16_t num_bytes) { *addr = (*addr + num_bytes) % kFramBufferSize; @@ -28,11 +28,11 @@ static inline void update_addr(uint16_t *addr, const uint16_t num_bytes) { /** * @brief Get the remaining space in the buffer - * + * * Calculates the difference between the write and read address in a single * forward direction in the circular buffer. If they are equal then nothing has * been written. - * + * * @return Remaining space in bytes */ static uint16_t get_remaining_space(void) { @@ -57,7 +57,7 @@ static uint16_t get_remaining_space(void) { FramStatus FramPut(const uint8_t *data, const uint16_t num_bytes) { // check remaining space - if (num_bytes+1 > get_remaining_space()) { + if (num_bytes + 1 > get_remaining_space()) { return FRAM_BUFFER_FULL; } @@ -110,9 +110,7 @@ FramStatus FramGet(uint8_t *data, uint8_t *len) { return FRAM_OK; } -uint16_t FramBufferLen(void) { - return buffer_len; -} +uint16_t FramBufferLen(void) { return buffer_len; } FramStatus FramBufferClear(void) { // Set read and write addresses to their default values diff --git a/stm32/lib/fram/src/fram.c b/stm32/lib/fram/src/fram.c index ee3f4681..ed54d840 100644 --- a/stm32/lib/fram/src/fram.c +++ b/stm32/lib/fram/src/fram.c @@ -3,8 +3,9 @@ /** * @file fram.c * @author Stephen Taylor - * @brief This file contains all the function definitions for the fram library. - * + * @brief This file contains all the function definitions for the fram + * library. + * * Copyright 2023 jLab, UCSC MIT License */ #include "include/fram.h" @@ -43,12 +44,12 @@ FramStatus FramWrite(uint16_t addr, const uint8_t *data, uint8_t len) { // Write byte array to memory // NOTE write is performed a single byte at a time due to address // configuration of the chip. - for (uint8_t *d = data; d < data+len; d++) { + for (uint8_t *d = data; d < data + len; d++) { FramAddress addr_mat = FramConvertAddrMem(addr); // check for out of memory // pages are zero indexed - if (addr_mat.page > FRAM_PAGES-1) { + if (addr_mat.page > FRAM_PAGES - 1) { return FRAM_OUT_OF_RANGE; } @@ -77,7 +78,7 @@ FramStatus FramRead(uint16_t addr, uint8_t len, uint8_t *data) { // Write byte array to memory // NOTE write is performed a single byte at a time due to address // configuration of the chip. - for (uint8_t *d = data; d < data+len; d++) { + for (uint8_t *d = data; d < data + len; d++) { FramAddress addr_mat = FramConvertAddrMem(addr); // check for out of memory diff --git a/stm32/lib/phytos31/include/phytos31.h b/stm32/lib/phytos31/include/phytos31.h index c5770648..1d03e296 100644 --- a/stm32/lib/phytos31/include/phytos31.h +++ b/stm32/lib/phytos31/include/phytos31.h @@ -6,8 +6,8 @@ * @author Stephen Taylor * @brief This file contains all the function prototypes for * the phytos31.c file. - * - * This library is designed to read measurements from a PHYTOS-31 + * + * This library is designed to read measurements from a PHYTOS-31 * sensor from METER. * https://metergroup.com/products/phytos-31/ * @date 4/18/2024 @@ -23,16 +23,17 @@ extern "C" { #include #include + #include "stm32/lib/ads/ads.h" typedef struct { - double phytos31_raw; - double phytos31_calibrated; + double phytos31_raw; + double phytos31_calibrated; } phytos_measurments; /** ****************************************************************************** -* @brief Wrapper function for the ADC initilization. +* @brief Wrapper function for the ADC initilization. * * @param void * @return HAL_StatusTypeDef @@ -53,15 +54,15 @@ phytos_measurments Phytos31GetMeasurment(void); /** * @brief Read PHYTOS31 sensor and serialize measurement - * + * * The voltage output of the PHYTOS31 is measured. A calibration is applied * to convert voltage into a leaf wetness measurement. - * + * * Current voltage and leaf wetness are the same value, until a calibration * is obtained. - * + * * @note Implemented for the sensors library - * + * * @see SensorsPrototypeMeasure */ size_t Phytos31_measure(uint8_t *data); diff --git a/stm32/lib/phytos31/src/phytos31.c b/stm32/lib/phytos31/src/phytos31.c index 0420b7e6..5ef85e57 100644 --- a/stm32/lib/phytos31/src/phytos31.c +++ b/stm32/lib/phytos31/src/phytos31.c @@ -4,24 +4,23 @@ ****************************************************************************** * @file sdi12.c * @author Stephen Taylor - * - * @brief This library is designed to read measurements from a PHYTOS-31 + * + * @brief This library is designed to read measurements from a PHYTOS-31 * sensor from METER. * https://metergroup.com/products/phytos-31/ * @date 4/18/2024 ****************************************************************************** */ +#include "stm32/lib/phytos31/include/phytos31.h" + #include #include #include -#include "stm32/lib/phytos31/include/phytos31.h" #include "proto/c/include/transcoder.h" -HAL_StatusTypeDef Phytos31Init() { - return ADC_init(); -} +HAL_StatusTypeDef Phytos31Init() { return ADC_init(); } phytos_measurments Phytos31GetMeasurment() { phytos_measurments measurments; @@ -40,7 +39,7 @@ size_t Phytos31_measure(uint8_t *data) { // encode measurement size_t data_len = EncodePhytos31Measurement(ts.Seconds, LOGGER_ID, CELL_ID, - adc_voltage_float, 0.0, data); + adc_voltage_float, 0.0, data); // return number of bytes in serialized measurement return data_len; diff --git a/stm32/lib/sdi12/include/sdi12.h b/stm32/lib/sdi12/include/sdi12.h index 70a0076a..d57f1fa6 100644 --- a/stm32/lib/sdi12/include/sdi12.h +++ b/stm32/lib/sdi12/include/sdi12.h @@ -21,11 +21,11 @@ extern "C" { #include #include -#include "usart.h" #include "gpio.h" #include "lptim.h" -#include "user_config.h" #include "tim.h" +#include "usart.h" +#include "user_config.h" /** Status codes for the Fram library*/ typedef enum { @@ -35,7 +35,6 @@ typedef enum { SDI12_PARSING_ERROR = -3, } SDI12Status; - /* The returned values from a SDI12 get measurment command*/ typedef struct { char Address; @@ -84,22 +83,23 @@ SDI12Status SDI12SendCommand(const char *command, uint8_t size); ****************************************************************************** */ SDI12Status SDI12ReadData(char *buffer, uint16_t bufferSize, -uint16_t timeoutMillis); + uint16_t timeoutMillis); /** ****************************************************************************** * @brief This is a function to read a measurment from a particular sensor. * * @param char const addr, the device address -* @param SDI12_Measure_TypeDef, a custom struct to store the measurment information returned from start measurment +* @param SDI12_Measure_TypeDef, a custom struct to store the measurment +*information returned from start measurment * @param char* the measurment data returned * @param uint16_t timeoutMillis time out in milliseconds * @return SDI12Status ****************************************************************************** */ SDI12Status SDI12GetMeasurment(uint8_t addr, -SDI12_Measure_TypeDef *measurment_info, -char *measurment_data, uint16_t timeoutMillis); + SDI12_Measure_TypeDef *measurment_info, + char *measurment_data, uint16_t timeoutMillis); #ifdef __cplusplus } diff --git a/stm32/lib/sdi12/src/sdi12.c b/stm32/lib/sdi12/src/sdi12.c index a51b2ca7..79b60d30 100644 --- a/stm32/lib/sdi12/src/sdi12.c +++ b/stm32/lib/sdi12/src/sdi12.c @@ -6,18 +6,18 @@ * @brief This file provides a library to communicate between a STM32 MC * and a TEROS-12 via SDI-12. * https://www.sdi-12.org/ - * + * * @date 4/1/2024 * ****************************************************************************** **/ +#include "sdi12.h" + #include #include #include -#include "sdi12.h" - static const uint16_t REQUEST_MEASURMENT_RESPONSE_SIZE = 7; static const uint16_t SERVICE_REQUEST_SIZE = 3; static const uint16_t MEASURMENT_DATA_SIZE = 18; @@ -25,42 +25,41 @@ static const uint16_t SEND_COMMAND_TIMEOUT = 1000; /* Helper function to parse the sensor's response to a get measurment command*/ SDI12Status ParseMeasurementResponse(const char *responseBuffer, char addr, -SDI12_Measure_TypeDef *measurement_info); + SDI12_Measure_TypeDef *measurement_info); /* Helper function to parse a sensor's service request */ SDI12Status ParseServiceRequest(const char *requestBuffer, char addr); void SDI12WakeSensors(void) { - HAL_LIN_SendBreak(&huart2); // Send a break - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET); // Send the marking - // HAL_Delay(20); // Need an extra 10ms to account for the fact - // that HAL_LIN_SendBreak is nonblocking - for (int i = 0; i <= 40000; i++) { - // Figure out a way to do this delay with the low-power timer - asm("nop"); - } + HAL_LIN_SendBreak(&huart2); // Send a break + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET); // Send the marking + // HAL_Delay(20); // Need an extra 10ms to account for the fact + // that HAL_LIN_SendBreak is nonblocking + for (int i = 0; i <= 40000; i++) { + // Figure out a way to do this delay with the low-power timer + asm("nop"); + } } SDI12Status SDI12SendCommand(const char *command, uint8_t size) { - HAL_StatusTypeDef ret; - SDI12WakeSensors(); - ret = HAL_UART_Transmit(&huart2, (const uint8_t *) command, - size, SEND_COMMAND_TIMEOUT); - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Set to RX mode - if (ret == HAL_OK) { - return SDI12_OK; - } else { - return SDI12_ERROR; - } + HAL_StatusTypeDef ret; + SDI12WakeSensors(); + ret = HAL_UART_Transmit(&huart2, (const uint8_t *)command, size, + SEND_COMMAND_TIMEOUT); + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Set to RX mode + if (ret == HAL_OK) { + return SDI12_OK; + } else { + return SDI12_ERROR; + } } -SDI12Status SDI12ReadData(char *buffer, - uint16_t bufferSize, uint16_t timeoutMillis) { +SDI12Status SDI12ReadData(char *buffer, uint16_t bufferSize, + uint16_t timeoutMillis) { HAL_StatusTypeDef ret; HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Set to RX mode __HAL_UART_FLUSH_DRREGISTER(&huart2); - ret = HAL_UART_Receive(&huart2, (uint8_t *) buffer, - bufferSize, HAL_MAX_DELAY); + ret = HAL_UART_Receive(&huart2, (uint8_t *)buffer, bufferSize, HAL_MAX_DELAY); if (ret == HAL_OK) { return SDI12_OK; } else if (ret == HAL_TIMEOUT) { @@ -70,10 +69,10 @@ SDI12Status SDI12ReadData(char *buffer, } } -SDI12Status ParseMeasurementResponse(const char *responseBuffer, - char addr, SDI12_Measure_TypeDef *measurement_info) { +SDI12Status ParseMeasurementResponse(const char *responseBuffer, char addr, + SDI12_Measure_TypeDef *measurement_info) { sscanf(responseBuffer, "%1c%3hu%1hhu", &(measurement_info->Address), - &(measurement_info->Time), &(measurement_info->NumValues)); + &(measurement_info->Time), &(measurement_info->NumValues)); // Parse the response and populate the structure // Check if the response address matches the expected address if (measurement_info->Address == addr) { @@ -97,8 +96,8 @@ SDI12Status ParseServiceRequest(const char *requestBuffer, char addr) { } SDI12Status SDI12GetMeasurment(uint8_t addr, - SDI12_Measure_TypeDef *measurment_info, - char *measurment_data, uint16_t timeoutMillis) { + SDI12_Measure_TypeDef *measurment_info, + char *measurment_data, uint16_t timeoutMillis) { // Command to request measurement ("0!\r\n" for example) char reqMeas[30]; // Command to send the data diff --git a/stm32/lib/sensors/include/sensors.h b/stm32/lib/sensors/include/sensors.h index df7804d5..ad2c9f97 100644 --- a/stm32/lib/sensors/include/sensors.h +++ b/stm32/lib/sensors/include/sensors.h @@ -3,40 +3,40 @@ * @author John Madden (jtmadden@ucsc.edu) * @brief Queries sensors and adds measurements to the upload queue * @date 2024-04-01 - * + * * Provides an interface for querying sensors and adding the measurements to the * transmit buffer. Functions to query sensors are registered and called on a * interval that is global to all sensors. - * + * * The library expects all initialization code for registered sensors to be * called before SensorsStart. The timer utility library (UTIL_TIMER_Init) and * sequencer utility (UTIL_SEQ_Init) must be initialized as well. - * + * * Currently there is no way to modify or remove sensors after adding the * callback function. This is based on the assumption sensors are not going * to be dynamically added or removed during firmware runtime. Also new sensors * will require updates to the firmware binaries. - * + * * MEASUREMENT_PERIOD defines the amount of time between measurements. This * value should be an order of magnitude greater than the upload frequency that * is defined by APP_TX_DUTY_CYCLE. - * + * * @copyright - * + * * MIT License - * + * * Copyright (c) 2024 jLab in Smart Sensing at UCSC - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -49,13 +49,12 @@ #ifndef LIB_SENSORS_INCLUDE_SENSORS_H_ #define LIB_SENSORS_INCLUDE_SENSORS_H_ +#include "fifo.h" +#include "lora_app.h" #include "stm32_seq.h" #include "stm32_timer.h" -#include "lora_app.h" #include "sys_app.h" -#include "fifo.h" - #ifdef __cplusplus extern "C" { #endif @@ -72,36 +71,36 @@ extern "C" { /** * @brief Function prototype for measure functions - * + * * @param data Location to store data - * + * * @return Number of bytes in data */ typedef size_t (*SensorsPrototypeMeasure)(uint8_t* data); /** * @brief Registers the measurement task with the sequencer - * + * */ void SensorsInit(void); /** * @brief Starts timer to take measurements and add to upload queue - * + * */ void SensorsStart(void); /** * @brief Stops measurement timer - * + * */ void SensorsStop(void); /** * @brief Adds sensor call to measurement cycle - * + * * @param cb Callback to the measurement function - * + * * @return Index of callback in internal array, -1 indicates an error */ int SensorsAdd(SensorsPrototypeMeasure cb); diff --git a/stm32/lib/sensors/src/sensors.c b/stm32/lib/sensors/src/sensors.c index 6eade084..0d7c7231 100644 --- a/stm32/lib/sensors/src/sensors.c +++ b/stm32/lib/sensors/src/sensors.c @@ -3,23 +3,23 @@ * @author John Madden (jmadden173@pm.me) * @brief See sensors.h * @date 2024-04-01 - * + * * @copyright - * + * * MIT License - * + * * Copyright (c) 2024 jLab in Smart Sensing at UCSC - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -46,7 +46,7 @@ static const uint32_t measure_period = MEASUREMENT_PERIOD; /** * @brief Measures sensors and adds to tx buffer - * + * * Loops through all added callbacks and calls the functions. The resulting * serialized data is added to the buffer. */ @@ -54,7 +54,7 @@ void SensorsMeasure(void); /** * @brief Runs the SensorsMeasure task - * + * * The task priority is set to 0 to trigger the measurement of sensors. */ void SensorsRun(void); diff --git a/stm32/test/main_helper.c b/stm32/test/main_helper.c index 0acdb593..509090b3 100644 --- a/stm32/test/main_helper.c +++ b/stm32/test/main_helper.c @@ -1,26 +1,26 @@ #include "main_helper.h" /** - * @brief System Clock Configuration - * @retval None - */ + * @brief System Clock Configuration + * @retval None + */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure LSE Drive Capability - */ + */ HAL_PWR_EnableBkUpAccess(); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); /** Configure the main internal regulator output voltage - */ + */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE| - RCC_OSCILLATORTYPE_MSI; + */ + RCC_OscInitStruct.OscillatorType = + RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; @@ -31,10 +31,10 @@ void SystemClock_Config(void) { } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK - |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 - |RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | + RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; @@ -52,9 +52,9 @@ void SystemClock_Config(void) { /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ // char error[30]; @@ -63,22 +63,24 @@ void Error_Handler(void) { /* User can add his own implementation to report the HAL error return state */ __disable_irq(); - while (1) {} + while (1) { + } /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/stm32/test/main_helper.h b/stm32/test/main_helper.h index 87ecde0b..f85351ca 100644 --- a/stm32/test/main_helper.h +++ b/stm32/test/main_helper.h @@ -4,12 +4,12 @@ * @brief Helper functions for main * @version 0.1 * @date 2024-09-24 - * + * * Includes the SystemClock configuration and error handling. This file is * supplemental to "main.h". - * + * * @copyright Copyright (c) 2024 - * + * */ #ifndef TEST_MAIN_HELPER_H_ diff --git a/stm32/test/test_ads/test_ads.c b/stm32/test/test_ads/test_ads.c index cb9258db..094a4467 100644 --- a/stm32/test/test_ads/test_ads.c +++ b/stm32/test/test_ads/test_ads.c @@ -6,16 +6,14 @@ #include #include +#include "ads.h" +#include "fifo.h" +#include "gpio.h" +#include "i2c.h" #include "main.h" #include "main_helper.h" -#include "i2c.h" -#include "usart.h" -#include "gpio.h" #include "sys_app.h" - -#include "fifo.h" -#include "ads.h" - +#include "usart.h" void setUp(void) {} @@ -42,16 +40,15 @@ void test_ADC_readCurrent(void) { TEST_ASSERT_NOT_EQUAL(curr, -1); } - /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { /* MCU Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* USER CODE BEGIN Init */ @@ -73,7 +70,7 @@ int main(void) // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } UNITY_BEGIN(); diff --git a/stm32/test/test_battery/test_battery.c b/stm32/test/test_battery/test_battery.c index 6250db76..e516c8e0 100644 --- a/stm32/test/test_battery/test_battery.c +++ b/stm32/test/test_battery/test_battery.c @@ -1,55 +1,54 @@ /** * @file test_battery.c * @brief unit test for battery.c - * + * * @author Ahmed Falah * @date 2024-03-18 -*/ - -#include "main.h" -#include "main_helper.h" -#include "usart.h" -#include "gpio.h" + */ #include - #include #include "battery.h" +#include "gpio.h" +#include "main.h" +#include "main_helper.h" +#include "usart.h" void SystemClock_Config(void); - /** * @brief Setup code that runs at the start of every test -*/ + */ void setUp(void) {} /** * @brief Tear down code that runs at the end of every test -*/ + */ void tearDown(void) {} // Test case for battery_init void test_battery_init_success(void) { - HAL_StatusTypeDef ret = battery_init(); - // Assert successful initialization - TEST_ASSERT_EQUAL(HAL_OK, ret); - /* We can add conditions based on the return status of the battery_init()*/ + HAL_StatusTypeDef ret = battery_init(); + // Assert successful initialization + TEST_ASSERT_EQUAL(HAL_OK, ret); + /* We can add conditions based on the return status of the battery_init()*/ } // Test case for battery_voltage valid reading void test_battery_voltage_stability(void) { // Threshold for voltage change const int MAX_VOLTAGE_CHANGE = 100; - // Calling battery_voltage() function multiple times with delay between readings + // Calling battery_voltage() function multiple times with delay between + // readings unsigned int voltage1 = battery_voltage(); - HAL_Delay(1000); // Delay 1sec + HAL_Delay(1000); // Delay 1sec unsigned int voltage2 = battery_voltage(); // Assert that the change in voltage is within the threshold - TEST_ASSERT_INT_WITHIN_MESSAGE(-MAX_VOLTAGE_CHANGE, MAX_VOLTAGE_CHANGE, voltage2 - voltage1, - "Battery voltage changed excessively between readings."); + TEST_ASSERT_INT_WITHIN_MESSAGE( + -MAX_VOLTAGE_CHANGE, MAX_VOLTAGE_CHANGE, voltage2 - voltage1, + "Battery voltage changed excessively between readings."); } // void test_battery_voltage_usb_power(void) { @@ -67,22 +66,23 @@ void test_battery_voltage_stability(void) { // average_voltage /= 5; // // Expected voltage range for USB power // const int EXPECTED_USB_VOLTAGE = (VOLTAGE_REF * 1000) / FULL_SCALE; // mV -// const int USB_VOLTAGE_TOLERANCE = 200; +// const int USB_VOLTAGE_TOLERANCE = 200; // // Assert that the average voltage is within the expected range -// TEST_ASSERT_INT_WITHIN_MESSAGE(EXPECTED_USB_VOLTAGE - USB_VOLTAGE_TOLERANCE, -// EXPECTED_USB_VOLTAGE + USB_VOLTAGE_TOLERANCE, -// average_voltage, -// "Battery voltage under USB power is outside expected range."); +// TEST_ASSERT_INT_WITHIN_MESSAGE(EXPECTED_USB_VOLTAGE - +// USB_VOLTAGE_TOLERANCE, +// EXPECTED_USB_VOLTAGE + +// USB_VOLTAGE_TOLERANCE, average_voltage, +// "Battery voltage under USB power is outside +// expected range."); // } - /** - * @brief Entry point for protobuf test - * @retval int - */ -int main(void) -{ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + * @brief Entry point for protobuf test + * @retval int + */ +int main(void) { + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* Configure the system clock */ @@ -91,10 +91,10 @@ int main(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); - + // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } // Unit testing @@ -102,6 +102,6 @@ int main(void) // Run all the tests RUN_TEST(test_battery_init_success); RUN_TEST(test_battery_voltage_stability); - //RUN_TEST(test_battery_voltage_usb_power); + // RUN_TEST(test_battery_voltage_usb_power); UNITY_END(); } diff --git a/stm32/test/test_fifo/test_fifo.c b/stm32/test/test_fifo/test_fifo.c index e3a23d6a..935927c1 100644 --- a/stm32/test/test_fifo/test_fifo.c +++ b/stm32/test/test_fifo/test_fifo.c @@ -7,17 +7,14 @@ #include #include +#include "fifo.h" +#include "gpio.h" +#include "i2c.h" #include "main.h" #include "main_helper.h" -#include "i2c.h" #include "usart.h" -#include "gpio.h" -#include "fifo.h" - -void setUp(void) { - FramBufferClear(); -} +void setUp(void) { FramBufferClear(); } void tearDown(void) {} @@ -62,7 +59,7 @@ void test_FramPut_Sequential_BufferFull(void) { // starting values uint8_t data[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; - const int niters = kFramBufferSize / (sizeof(data)+1); + const int niters = kFramBufferSize / (sizeof(data) + 1); // write 100 times, therefore 1100 bytes (data + len) for (int i = 0; i < niters; i++) { @@ -141,7 +138,7 @@ void test_FramGet_Sequential_BufferFull(void) { // starting values uint8_t data[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; - const int niters = (kFramBufferSize / (sizeof(data)+1)); + const int niters = (kFramBufferSize / (sizeof(data) + 1)); // write 100 times, therefore 1100 bytes (data + len) for (int i = 0; i < niters; i++) { @@ -245,13 +242,13 @@ void test_FramBuffer_Wraparound(void) { } /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { /* MCU Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); @@ -273,7 +270,7 @@ int main(void) { // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } UNITY_BEGIN(); diff --git a/stm32/test/test_fram/test_fram.c b/stm32/test/test_fram/test_fram.c index 0c1c6d36..dd911377 100644 --- a/stm32/test/test_fram/test_fram.c +++ b/stm32/test/test_fram/test_fram.c @@ -8,18 +8,18 @@ #include #include +#include "fram.h" +#include "gpio.h" +#include "i2c.h" #include "main.h" #include "main_helper.h" -#include "i2c.h" #include "usart.h" -#include "gpio.h" -#include "fram.h" void setUp(void) {} /** * @brief Tear down code that runs at the end of every test -*/ + */ void tearDown(void) {} void test_i2c(void) { @@ -29,8 +29,8 @@ void test_i2c(void) { HAL_StatusTypeDef status; // test raw i2c write - status = HAL_I2C_Mem_Write(&hi2c2, 0xA0, 0x00, I2C_MEMADD_SIZE_8BIT, &test, - 1, 50); + status = + HAL_I2C_Mem_Write(&hi2c2, 0xA0, 0x00, I2C_MEMADD_SIZE_8BIT, &test, 1, 50); TEST_ASSERT_EQUAL(HAL_OK, status); // test raw i2c read @@ -70,7 +70,7 @@ void test_FramWrite_ZeroLength(void) { void test_FramWrite_MultiplePages(void) { uint8_t data[] = {1, 2, 3, 4, 5}; // right below the segment size - uint16_t addr = FRAM_SEG_SIZE-1; + uint16_t addr = FRAM_SEG_SIZE - 1; FramStatus status = FramWrite(addr, data, sizeof(data)); @@ -119,7 +119,7 @@ void test_FramRead_OutOfRange(void) { void test_FramRead_MultiplePages(void) { // right below the segment size - uint16_t addr = FRAM_SEG_SIZE-1; + uint16_t addr = FRAM_SEG_SIZE - 1; uint8_t write_data[] = {1, 2, 3, 4, 5}; FramWrite(addr, write_data, sizeof(write_data)); @@ -132,9 +132,9 @@ void test_FramRead_MultiplePages(void) { } /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { HAL_Init(); @@ -146,7 +146,7 @@ int main(void) { // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } UNITY_BEGIN(); diff --git a/stm32/test/test_main/test_main.c b/stm32/test/test_main/test_main.c index e16efb05..71d09ead 100644 --- a/stm32/test/test_main/test_main.c +++ b/stm32/test/test_main/test_main.c @@ -6,35 +6,30 @@ #include #include +#include "fram.h" +#include "gpio.h" +#include "i2c.h" #include "main.h" #include "main_helper.h" -#include "i2c.h" #include "usart.h" -#include "gpio.h" -#include "fram.h" -void setUp(void) { - -} +void setUp(void) {} /** * @brief Tear down code that runs at the end of every test -*/ + */ void tearDown(void) {} -void test_GetTick(void){ +void test_GetTick(void) { uint32_t ticks = HAL_GetTick(); - TEST_ASSERT_NOT_EQUAL(ticks, 0); // if zero, it means timer was never started + TEST_ASSERT_NOT_EQUAL(ticks, 0); // if zero, it means timer was never started } - - /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { HAL_Init(); /* USER CODE BEGIN Init */ @@ -61,7 +56,7 @@ int main(void) // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } UNITY_BEGIN(); diff --git a/stm32/test/test_proto/test_proto.c b/stm32/test/test_proto/test_proto.c index 6e91b566..fbe32334 100644 --- a/stm32/test/test_proto/test_proto.c +++ b/stm32/test/test_proto/test_proto.c @@ -1,29 +1,24 @@ /** * @file test_proto.c * @brief Tests ability to encode and decode protobuf messages - * + * * Encoding/decoding of each message type is tested to ensure functionality of * nanopb and correctly matched tests with generate protobuf source files. - * + * * @author John Madden * @date 2023-11-27 -*/ - -#include "main.h" -#include "main_helper.h" -#include "usart.h" -#include "gpio.h" + */ #include - #include -#include "pb_encode.h" +#include "gpio.h" +#include "main.h" +#include "main_helper.h" #include "pb_decode.h" - +#include "pb_encode.h" #include "soil_power_sensor.pb.h" - - +#include "usart.h" #ifndef UNIX_EPOCHS /** Unix epochs encoded in timestamp. Default corresponds to Mon Nov 27 2023 @@ -36,9 +31,7 @@ #define FLOAT_DELTA 1e-4 #endif /* FLOAT_DELTA */ - void SystemClock_Config(void); - /** Buffer for encoded data */ uint8_t buffer[256]; @@ -55,28 +48,26 @@ bool status; /** * @brief Default MeasurementMetadata - * + * * Uses as submessage when testing parent messages. Defaults to zeros with * ts_default - * + * * @see ts_default -*/ + */ MeasurementMetadata meta_default; /** * @brief Setup code that runs at the start of every test - * + * * Creates required variables that are shared between all tests. The output * buffer is initialized based on length of the buffer. Input buffer is defined, * but not initialized since it depends on the message length. Default values * are defined for Timestamp and MeasurementMetadata. -*/ -void setUp(void) -{ + */ +void setUp(void) { // create output buffer ostream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - // Set default measurement metadata meta_default.ts = UNIX_EPOCHS; meta_default.cell_id = 0; @@ -85,11 +76,10 @@ void setUp(void) /** * @brief Tear down code that runs at the end of every test -*/ + */ void tearDown(void) {} -void test_meta(void) -{ +void test_meta(void) { MeasurementMetadata meta_encode = MeasurementMetadata_init_zero; meta_encode.ts = UNIX_EPOCHS; @@ -102,7 +92,6 @@ void test_meta(void) TEST_ASSERT_TRUE(status); TEST_ASSERT_GREATER_THAN(0, msg_len); - MeasurementMetadata meta_decode; istream = pb_istream_from_buffer(buffer, msg_len); @@ -114,11 +103,9 @@ void test_meta(void) TEST_ASSERT_EQUAL(1, meta_decode.logger_id); TEST_ASSERT_EQUAL(UNIX_EPOCHS, meta_decode.ts); - } -void test_power(void) -{ +void test_power(void) { Measurement power_encode = Measurement_init_zero; power_encode.which_measurement = Measurement_power_tag; power_encode.has_meta = true; @@ -132,7 +119,6 @@ void test_power(void) TEST_ASSERT_TRUE(status); TEST_ASSERT_GREATER_THAN(0, msg_len); - Measurement power_decode; istream = pb_istream_from_buffer(buffer, msg_len); @@ -154,8 +140,7 @@ void test_power(void) power_decode.measurement.power.current); } -void test_teros12(void) -{ +void test_teros12(void) { Measurement teros12_encode = Measurement_init_zero; teros12_encode.which_measurement = Measurement_teros12_tag; teros12_encode.has_meta = true; @@ -171,7 +156,6 @@ void test_teros12(void) TEST_ASSERT_TRUE(status); TEST_ASSERT_GREATER_THAN(0, msg_len); - Measurement teros12_decode; istream = pb_istream_from_buffer(buffer, msg_len); @@ -196,16 +180,14 @@ void test_teros12(void) teros12_decode.measurement.teros12.vwc_raw); } -void test_response(void) -{ +void test_response(void) { Response response_encode = Response_init_zero; response_encode.resp = Response_ResponseType_SUCCESS; status = pb_encode(&ostream, Response_fields, &response_encode); msg_len = ostream.bytes_written; - - TEST_ASSERT_TRUE(status); + TEST_ASSERT_TRUE(status); Response response_decode; @@ -217,14 +199,13 @@ void test_response(void) TEST_ASSERT_EQUAL(Response_ResponseType_SUCCESS, response_decode.resp); } - /** - * @brief Entry point for protobuf test - * @retval int - */ -int main(void) -{ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + * @brief Entry point for protobuf test + * @retval int + */ +int main(void) { + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* Configure the system clock */ @@ -233,10 +214,10 @@ int main(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); - + // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } // Unit testing diff --git a/stm32/test/test_sdi12/test_sdi12.c b/stm32/test/test_sdi12/test_sdi12.c index 6c8b4626..94f8e165 100644 --- a/stm32/test/test_sdi12/test_sdi12.c +++ b/stm32/test/test_sdi12/test_sdi12.c @@ -6,19 +6,18 @@ #include #include +#include "gpio.h" +#include "i2c.h" #include "main.h" #include "main_helper.h" -#include "i2c.h" -#include "usart.h" -#include "gpio.h" #include "sdi12.h" +#include "usart.h" /** * @brief Generated from CubeMX -*/ + */ void SystemClock_Config(void); - void setUp(void) {} void tearDown(void) {} @@ -34,24 +33,22 @@ void test_SDI12_SendCommand_success(void) { TEST_ASSERT_EQUAL(SDI12_OK, status); } -void test_SDI12_Read_success(void) { - TEST_IGNORE(); -} +void test_SDI12_Read_success(void) { TEST_IGNORE(); } void test_SDI12_GetMeasurment_success(void) { SDI12Status status; uint8_t addr = '0'; SDI12_Measure_TypeDef measurment_info; char buffer[20]; - status = SDI12GetMeasurment(addr, &measurment_info, buffer, 1000); + status = SDI12GetMeasurment(addr, &measurment_info, buffer, 1000); TEST_ASSERT_EQUAL(SDI12_OK, status); } /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { /* MCU Configuration--------------------------------------------------------*/ @@ -77,7 +74,7 @@ int main(void) { // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } UNITY_BEGIN(); diff --git a/stm32/test/test_template/test_template.c b/stm32/test/test_template/test_template.c index 97c27752..0753691a 100644 --- a/stm32/test/test_template/test_template.c +++ b/stm32/test/test_template/test_template.c @@ -1,46 +1,40 @@ /** * @file test_template.c * @brief Template file for tests - * + * * @author John Madden * @date 2024-01-05 -*/ + */ +#include +#include + +#include "gpio.h" #include "main.h" #include "main_helper.h" #include "usart.h" -#include "gpio.h" - -#include - -#include void SystemClock_Config(void); - /** * @brief Setup code that runs at the start of every test -*/ + */ void setUp(void) {} /** * @brief Tear down code that runs at the end of every test -*/ + */ void tearDown(void) {} - -void test_true(void) -{ - TEST_ASSERT_TRUE(1); -} +void test_true(void) { TEST_ASSERT_TRUE(1); } /** - * @brief Entry point for protobuf test - * @retval int - */ -int main(void) -{ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + * @brief Entry point for protobuf test + * @retval int + */ +int main(void) { + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* Configure the system clock */ @@ -52,7 +46,7 @@ int main(void) // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } // Unit testing diff --git a/stm32/test/test_transcoder/test_transcoder.c b/stm32/test/test_transcoder/test_transcoder.c index 75350806..b2cd141e 100644 --- a/stm32/test/test_transcoder/test_transcoder.c +++ b/stm32/test/test_transcoder/test_transcoder.c @@ -1,135 +1,126 @@ /** * @file test_transcoder.c - * + * * @brief Tests encoding/decoding against binary data obtained from python - * + * * Encoding of a Measurement message is compared against serialized data from * python using the same input parameters. A byte wise comparison is used for * these tests. - * + * * Decoding a Response message takes binary data from python and compares the * decoded ResponseType to the expected. Both a SUCCESS and ERROR are checked to * cover the default case where "no" data is passed into the function. - * + * * The parameters used for Measurement and Response messages are located in * generate_bytes.py and can be used to generate C code to copy into this test * file. - * + * * @see generate_bytes.py - * + * * @author John Madden * @date 2024-01-05 -*/ - -#include "main.h" -#include "main_helper.h" -#include "usart.h" -#include "gpio.h" + */ #include - #include +#include "gpio.h" +#include "main.h" +#include "main_helper.h" #include "transcoder.h" - +#include "usart.h" /** * @brief Generated from CubeMX -*/ + */ void SystemClock_Config(void); - /** * @brief Setup code that runs at the start of every test -*/ + */ void setUp(void) {} /** * @brief Tear down code that runs at the end of every test -*/ + */ void tearDown(void) {} -void TestEncodePower(void) -{ +void TestEncodePower(void) { uint8_t buffer[256]; size_t buffer_len; buffer_len = EncodePowerMeasurement(1436079600, 7, 4, 37.13, 185.29, buffer); - - uint8_t data[] = {0xa, 0xa, 0x8, 0x4, 0x10, 0x7, 0x18, 0xf0, 0xab, 0xe3, 0xac, - 0x5, 0x12, 0x12, 0x11, 0x71, 0x3d, 0xa, 0xd7, 0xa3, 0x90, - 0x42, 0x40, 0x19,0xe1, 0x7a, 0x14, 0xae, 0x47, 0x29, 0x67, - 0x40}; + + uint8_t data[] = {0xa, 0xa, 0x8, 0x4, 0x10, 0x7, 0x18, 0xf0, + 0xab, 0xe3, 0xac, 0x5, 0x12, 0x12, 0x11, 0x71, + 0x3d, 0xa, 0xd7, 0xa3, 0x90, 0x42, 0x40, 0x19, + 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x29, 0x67, 0x40}; size_t data_len = 32; TEST_ASSERT_EQUAL_HEX8_ARRAY(data, buffer, buffer_len); TEST_ASSERT_EQUAL_INT(data_len, buffer_len); } -void TestEncodeTeros12(void) -{ +void TestEncodeTeros12(void) { uint8_t buffer[256]; size_t buffer_len; buffer_len = EncodeTeros12Measurement(1436079600, 7, 4, 2124.62, 0.43, 24.8, 123, buffer); - uint8_t data[] = {0xa, 0xa, 0x8, 0x4, 0x10, 0x7, 0x18, 0xf0, 0xab, 0xe3, - 0xac, 0x5, 0x1a, 0x1d, 0x11, 0xa, 0xd7, 0xa3, 0x70, 0x3d, - 0x99, 0xa0, 0x40, 0x19, 0x85, 0xeb, 0x51, 0xb8, 0x1e, - 0x85, 0xdb, 0x3f, 0x21, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, - 0xcc, 0x38, 0x40, 0x28, 0x7b}; + uint8_t data[] = {0xa, 0xa, 0x8, 0x4, 0x10, 0x7, 0x18, 0xf0, 0xab, + 0xe3, 0xac, 0x5, 0x1a, 0x1d, 0x11, 0xa, 0xd7, 0xa3, + 0x70, 0x3d, 0x99, 0xa0, 0x40, 0x19, 0x85, 0xeb, 0x51, + 0xb8, 0x1e, 0x85, 0xdb, 0x3f, 0x21, 0xcd, 0xcc, 0xcc, + 0xcc, 0xcc, 0xcc, 0x38, 0x40, 0x28, 0x7b}; size_t data_len = 43; TEST_ASSERT_EQUAL_HEX8_ARRAY(data, buffer, buffer_len); TEST_ASSERT_EQUAL_INT(data_len, buffer_len); } -void TestEncodePhytos31(void) -{ +void TestEncodePhytos31(void) { uint8_t buffer[256]; size_t buffer_len; - buffer_len = EncodePhytos31Measurement(1436079600, 7, 4, 1425.12, 1962.2, - buffer); + buffer_len = + EncodePhytos31Measurement(1436079600, 7, 4, 1425.12, 1962.2, buffer); - uint8_t data[] = {0xa, 0xa, 0x8, 0x4, 0x10, 0x7, 0x18, 0xf0, 0xab, 0xe3, 0xac, - 0x5, 0x22, 0x12, 0x9, 0x14, 0xae, 0x47, 0xe1, 0x7a, 0x44, - 0x96, 0x40, 0x11, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xa8, 0x9e, - 0x40}; + uint8_t data[] = {0xa, 0xa, 0x8, 0x4, 0x10, 0x7, 0x18, 0xf0, + 0xab, 0xe3, 0xac, 0x5, 0x22, 0x12, 0x9, 0x14, + 0xae, 0x47, 0xe1, 0x7a, 0x44, 0x96, 0x40, 0x11, + 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xa8, 0x9e, 0x40}; size_t data_len = 32; TEST_ASSERT_EQUAL_HEX8_ARRAY(data, buffer, buffer_len); TEST_ASSERT_EQUAL_INT(data_len, buffer_len); } -void TestDecodeResponseSuccess(void) -{ +void TestDecodeResponseSuccess(void) { uint8_t data[] = {}; size_t data_len = 0; Response_ResponseType resp_type = DecodeResponse(data, data_len); - + TEST_ASSERT_EQUAL(Response_ResponseType_SUCCESS, resp_type); } -void TestDecodeResponseError(void) -{ +void TestDecodeResponseError(void) { uint8_t data[] = {0x8, 0x1}; size_t data_len = 2; Response_ResponseType resp_type = DecodeResponse(data, data_len); - + TEST_ASSERT_EQUAL(Response_ResponseType_ERROR, resp_type); } /** - * @brief Entry point for protobuf test - * @retval int - */ -int main(void) -{ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + * @brief Entry point for protobuf test + * @retval int + */ +int main(void) { + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ HAL_Init(); /* Configure the system clock */ @@ -141,7 +132,7 @@ int main(void) // wait for UART for (int i = 0; i < 1000000; i++) { - __NOP(); + __NOP(); } // Unit testing diff --git a/stm32/test/unity_config.c b/stm32/test/unity_config.c index dabb906d..a2e880b8 100644 --- a/stm32/test/unity_config.c +++ b/stm32/test/unity_config.c @@ -1,8 +1,7 @@ #include "unity_config.h" -void UnityOutputChar(char c) -{ - HAL_StatusTypeDef status; - status = HAL_UART_Transmit(&huart1, (const uint8_t *) &c, 1, UART_TIMEOUT); - if (status != HAL_OK) Error_Handler(); +void UnityOutputChar(char c) { + HAL_StatusTypeDef status; + status = HAL_UART_Transmit(&huart1, (const uint8_t *)&c, 1, UART_TIMEOUT); + if (status != HAL_OK) Error_Handler(); } \ No newline at end of file diff --git a/stm32/test/unity_config.h b/stm32/test/unity_config.h index afc386d1..a2050ab8 100644 --- a/stm32/test/unity_config.h +++ b/stm32/test/unity_config.h @@ -1,11 +1,11 @@ /** * @brief Unity test framework configuration for STM32 HAL libraries - * - * - * + * + * + * * @author John Madden * @date 2023-11-27 -*/ + */ #ifndef UNITY_CONFIG_H #define UNITY_CONFIG_H @@ -29,15 +29,14 @@ #define UART_TIMEOUT 1000 #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif /** * @brief Transmits single charater to USART1 - * + * * The return code is checked. Requires the USART library to be initialized. - * + * * @param c Character to transmit */ void UnityOutputChar(char c);