Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc. http://www.blynk.cc/

Dependents:   Blynk_RBL_BLE_Nano Blynk_MicroBit Blynk_Serial Blynk_RBL_BLE_Nano

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkDebug.cpp Source File

BlynkDebug.cpp

Go to the documentation of this file.
00001 /**
00002  * @file       BlynkDebug.cpp
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Jan 2015
00007  * @brief      Debug utilities for Arduino
00008  */
00009 #include <Blynk/BlynkDebug.h>
00010 
00011 #if defined(ARDUINO) && defined(__AVR__) && defined(BLYNK_USE_AVR_WDT)
00012 
00013     #include <Arduino.h>
00014     #include <avr/wdt.h>
00015 
00016     BLYNK_CONSTRUCTOR
00017     static void BlynkSystemInit()
00018     {
00019         MCUSR = 0;
00020         wdt_disable();
00021     }
00022 
00023     void BlynkReset()
00024     {
00025         wdt_enable(WDTO_15MS);
00026         delay(50);
00027         void(*resetFunc)(void) = 0;
00028         resetFunc();
00029         for(;;) {} // To make compiler happy
00030     }
00031 
00032     size_t BlynkFreeRam()
00033     {
00034         extern int __heap_start, *__brkval;
00035         int v;
00036         return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
00037     }
00038 
00039     #define _BLYNK_USE_DEFAULT_MILLIS
00040     #define _BLYNK_USE_DEFAULT_DELAY
00041 
00042 #elif defined(ARDUINO) && defined(__AVR__)
00043 
00044     #include <Arduino.h>
00045 
00046     void BlynkReset()
00047     {
00048         void(*resetFunc)(void) = 0;
00049         resetFunc();
00050         for(;;) {}
00051     }
00052 
00053     size_t BlynkFreeRam()
00054     {
00055         extern int __heap_start, *__brkval;
00056         int v;
00057         return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
00058     }
00059 
00060     #define _BLYNK_USE_DEFAULT_MILLIS
00061     #define _BLYNK_USE_DEFAULT_DELAY
00062 
00063 #elif defined(ARDUINO) && defined(ESP8266)
00064 
00065     #include <Arduino.h>
00066 
00067     size_t BlynkFreeRam()
00068     {
00069         return ESP.getFreeHeap();
00070     }
00071 
00072     void BlynkReset()
00073     {
00074         ESP.restart();
00075         for(;;) {}
00076     }
00077 
00078     #define _BLYNK_USE_DEFAULT_MILLIS
00079     #define _BLYNK_USE_DEFAULT_DELAY
00080 
00081 #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
00082 
00083     #include <Arduino.h>
00084 
00085     size_t BlynkFreeRam()
00086     {
00087         return 0;
00088     }
00089 
00090     void BlynkReset()
00091     {
00092         NVIC_SystemReset();
00093         for(;;) {}
00094     }
00095 
00096     #define _BLYNK_USE_DEFAULT_MILLIS
00097     #define _BLYNK_USE_DEFAULT_DELAY
00098 
00099 #elif defined (ARDUINO_ARCH_ARC32)
00100 
00101     millis_time_t BlynkMillis()
00102     {
00103         // TODO: Remove workaround for Intel Curie
00104         // https://forum.arduino.cc/index.php?topic=391836.0
00105         noInterrupts();
00106         uint64_t t = millis();
00107         interrupts();
00108         return t;
00109     }
00110 
00111     #define _BLYNK_USE_DEFAULT_FREE_RAM
00112     #define _BLYNK_USE_DEFAULT_RESET
00113     #define _BLYNK_USE_DEFAULT_DELAY
00114 
00115 #elif defined(ARDUINO) && (defined(__STM32F1__) || defined(__STM32F3__))
00116 
00117     #include <Arduino.h>
00118     #include <libmaple/nvic.h>
00119 
00120     void BlynkReset()
00121     {
00122         nvic_sys_reset();
00123         for(;;) {}
00124     }
00125 
00126     #define _BLYNK_USE_DEFAULT_FREE_RAM
00127     #define _BLYNK_USE_DEFAULT_MILLIS
00128     #define _BLYNK_USE_DEFAULT_DELAY
00129 
00130 #elif defined (PARTICLE) || defined(SPARK)
00131 
00132     #include "application.h"
00133 
00134     void BlynkReset()
00135     {
00136         System.reset();
00137         for(;;) {} // To make compiler happy
00138     }
00139 
00140     #define _BLYNK_USE_DEFAULT_FREE_RAM
00141     #define _BLYNK_USE_DEFAULT_MILLIS
00142     #define _BLYNK_USE_DEFAULT_DELAY
00143 
00144 #elif defined(__MBED__)
00145 
00146     #include "mbed.h"
00147 
00148     static Timer  blynk_millis_timer;
00149     static Ticker blynk_waker;
00150 
00151     static
00152     void blynk_wake() {
00153         //pc.puts("(...)");
00154     }
00155 
00156     //BLYNK_CONSTRUCTOR
00157     void BlynkSystemInit()
00158     {
00159         blynk_waker.attach(&blynk_wake, 2.0);
00160         blynk_millis_timer.start();
00161     }
00162 
00163     void BlynkDelay(millis_time_t ms)
00164     {
00165         wait_ms(ms);
00166     }
00167 
00168     millis_time_t BlynkMillis()
00169     {
00170         return blynk_millis_timer.read_ms();
00171     }
00172 
00173     #define _BLYNK_USE_DEFAULT_FREE_RAM
00174     #define _BLYNK_USE_DEFAULT_RESET
00175 
00176 #elif defined(LINUX) && defined(RASPBERRY)
00177 
00178     #include <stdlib.h>
00179     #include <wiringPi.h>
00180 
00181     BLYNK_CONSTRUCTOR
00182     static void BlynkSystemInit()
00183     {
00184         wiringPiSetupGpio();
00185     }
00186 
00187     void BlynkReset()
00188     {
00189         exit(1);
00190         for(;;) {} // To make compiler happy
00191     }
00192 
00193     #define _BLYNK_USE_DEFAULT_FREE_RAM
00194     #define _BLYNK_USE_DEFAULT_MILLIS
00195     #define _BLYNK_USE_DEFAULT_DELAY
00196 
00197 #elif defined(LINUX)
00198 
00199     #define _POSIX_C_SOURCE 200809L
00200     #include <stdlib.h>
00201     #include <time.h>
00202     #include <unistd.h>
00203 
00204     static millis_time_t blynk_startup_time = 0;
00205 
00206     BLYNK_CONSTRUCTOR
00207     static void BlynkSystemInit()
00208     {
00209         blynk_startup_time = BlynkMillis();
00210     }
00211 
00212     void BlynkReset()
00213     {
00214         exit(1);
00215         for(;;) {} // To make compiler happy
00216     }
00217 
00218     void BlynkDelay(millis_time_t ms)
00219     {
00220         usleep(ms * 1000);
00221     }
00222 
00223     millis_time_t BlynkMillis()
00224     {
00225         struct timespec ts;
00226         clock_gettime(CLOCK_MONOTONIC, &ts );
00227         return ( ts.tv_sec * 1000 + ts.tv_nsec / 1000000L ) - blynk_startup_time;
00228     }
00229 
00230     #define _BLYNK_USE_DEFAULT_FREE_RAM
00231 
00232 #else
00233 
00234     #if defined(BLYNK_DEBUG_ALL)
00235         #warning "Need to implement board-specific utilities"
00236     #endif
00237 
00238     #define _BLYNK_USE_DEFAULT_FREE_RAM
00239     #define _BLYNK_USE_DEFAULT_RESET
00240     #define _BLYNK_USE_DEFAULT_MILLIS
00241     #define _BLYNK_USE_DEFAULT_DELAY
00242 
00243 #endif
00244 
00245 #ifdef _BLYNK_USE_DEFAULT_DELAY
00246     void BlynkDelay(millis_time_t ms)
00247     {
00248         return delay(ms);
00249     }
00250 #endif
00251 
00252 #ifdef _BLYNK_USE_DEFAULT_MILLIS
00253     millis_time_t BlynkMillis()
00254     {
00255         return millis();
00256     }
00257 #endif
00258 
00259 #ifdef _BLYNK_USE_DEFAULT_FREE_RAM
00260     size_t BlynkFreeRam()
00261     {
00262         return 0;
00263     }
00264 #endif
00265 
00266 #ifdef _BLYNK_USE_DEFAULT_RESET
00267     void BlynkReset()
00268     {
00269         for(;;) {} // To make compiler happy
00270     }
00271 #endif
00272 
00273 void BlynkFatal()
00274 {
00275     BlynkDelay(10000L);
00276     BlynkReset();
00277 }
00278