diff --git a/global.h b/global.h index 184504f..07fd195 100644 --- a/global.h +++ b/global.h @@ -19,6 +19,9 @@ */ #pragma once +/** enable debugging functionalities */ +#define DEBUG true + /** get the length of an array */ #define LENGTH(x) (sizeof(x) / sizeof((x)[0])) /** concatenate 2 arguments (used to defer concatenation) */ diff --git a/main.c b/main.c index f17cba6..399f2ab 100644 --- a/main.c +++ b/main.c @@ -27,13 +27,15 @@ #include // mathematical utilities /* STM32 (including CM3) libraries */ -#include // real-time control clock library -#include // general purpose input output library -#include // vector table definition #include // Cortex M3 utilities +#include // vector table definition #include // interrupt utilities +#include // general purpose input output library +#include // real-time control clock library #include // external interrupt utilities #include // real time clock utilities +#include // independent watchdog utilities +#include // debug utilities /* own libraries */ #include "global.h" // board definitions @@ -143,6 +145,14 @@ void main(void) { rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock +#if DEBUG + DBGMCU_CR |= DBGMCU_CR_IWDG_STOP; // stop independent watchdog counter when code is halted + DBGMCU_CR |= DBGMCU_CR_WWDG_STOP; // stop window watchdog counter when code is halted + DBGMCU_CR |= DBGMCU_CR_STANDBY; // allow debug also in standby mode (keep digital part and clock powered) + DBGMCU_CR |= DBGMCU_CR_STOP; // allow debug also in stop mode (keep clock powered) + DBGMCU_CR |= DBGMCU_CR_SLEEP; // allow debug also in sleep mode (keep clock powered) +#endif + // setup board board_setup(); @@ -189,14 +199,24 @@ void main(void) radio_esp8266_setup(); printf("OK\n"); +#if !(DEBUG) + //setup watchdog to reset in case we get stuck (i.e. when an error occurred) + #define WATCHDOG_PERIOD 10000 /**< watchdog period in ms */ + printf("setup watchdog (%.2fs): ",WATCHDOG_PERIOD/1000.0); + iwdg_set_period_ms(WATCHDOG_PERIOD); // set independent watchdog period + iwdg_start(); // start independent watchdog + printf("OK\n"); +#endif + // send HTTP POST request printf("making HTTP request: "); - radio_esp8266_tcp_open("192.168.42.2",8086); // open connection + radio_esp8266_tcp_open("192.168.42.3",8086); // open connection while (!radio_esp8266_activity) { // wait until response has been received __WFI(); // wait until something happens } if (!radio_esp8266_success) { // could not open port - return; // not ideal error handling + printf("TCP connection failed\n"); + while (true); // unhandled error. wait for debugging or watchdog } char http_line[256+1] = {0}; int http_length = snprintf(http_line, LENGTH(http_line), "POST /write?db=test HTTP/1.1\r\n"); @@ -263,6 +283,7 @@ void main(void) char c = ' '; // to store received character bool char_flag = false; // a new character has been received while (true) { // infinite loop + iwdg_reset(); // kick the dog while (usart_received) { // data received over UART action = true; // action has been performed led_toggle(); // toggle LED