Lora support for the STM B_L072Z_LRWAN1 board out of the box. Also supports HopeRF RFM95, Murata CMWX1ZZABZ and Semtech SX1276MB1MAS/SX1276MB1LAS modules.

Dependencies:   BufferedSerial SX1276GenericLib mbed USBDeviceHT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers utils.cpp Source File

utils.cpp

00001 /*
00002  * Copyright (c) 2018 Helmut Tschemernjak
00003  * 30826 Garbsen (Hannover) Germany
00004  */
00005  #include "main.h"
00006  
00007 time_t cvt_date(char const *date, char const *time);
00008 
00009 BufferedSerial *ser;
00010 #ifdef FEATURE_USBSERIAL
00011 USBSerialBuffered *usb;
00012 #endif
00013 bool _useDprintf;
00014 
00015 /*
00016  * keep a timer running to avoid USB hangup in sleep,
00017  * in newer mbed versions sleep calls deepsleep which may 
00018  * hangup the USBSerial.
00019  * For this reason we keep a timer pending which avoids deepsleep.
00020  */
00021 static Timeout busyTimer;
00022 
00023 void busyTimerFunc(void)
00024 {
00025     busyTimer.attach(callback(&busyTimerFunc), 300);
00026 }
00027 
00028 void InitSerial(int timeout, DigitalOut *led)
00029 {
00030     _useDprintf = true;
00031     bool uartActive;
00032     {
00033         {
00034             // need to turn rx low to avoid floating signal
00035             DigitalOut rx(USBRX);
00036             rx = 0;
00037         }
00038         DigitalIn uartRX(USBRX);
00039         uartActive = uartRX.read();
00040     }
00041 #ifdef FEATURE_USBSERIAL
00042     if (!uartActive) {
00043         usb = new USBSerialBuffered();
00044         Timer t;
00045         t.start();
00046         while(!usb->connected()) {
00047             if (led)
00048                 *led = !*led;
00049             wait_ms(100);
00050             if (timeout) {
00051                 if (t.read_ms() >= timeout)
00052                     return;
00053             }
00054         }
00055         busyTimerFunc();
00056         return;
00057     } else {
00058 #else
00059     {
00060 #endif
00061         ser = new BufferedSerial(USBTX, USBRX);
00062         ser->baud(230400);
00063         ser->format(8);
00064     }
00065     time_t t = cvt_date(__DATE__, __TIME__);
00066     if (t > time(NULL)) {
00067         set_time(t);
00068     }
00069 
00070 }
00071 
00072 void printTimeStamp()
00073 {
00074     static LowPowerTimer *timer;
00075     if (!timer) {
00076         timer = new LowPowerTimer();
00077         timer->start();
00078     }
00079     time_t seconds = time(NULL);
00080     struct tm *tm = localtime(&seconds);
00081     int usecs = timer->read_us();
00082     if (usecs < 0) {
00083         usecs = 0;
00084         timer->stop();
00085         timer->reset();
00086         timer->start();
00087     }
00088     int msecs = usecs % 1000000;
00089     
00090     rprintf("%02d:%02d:%02d.%06d ", tm->tm_hour, tm->tm_min, tm->tm_sec, msecs);
00091 }
00092 
00093 void dprintf(const char *format, ...)
00094 {
00095     std::va_list arg;
00096 
00097     va_start(arg, format);
00098     VAprintf(true, true, _useDprintf, format, arg);
00099     va_end(arg);
00100 }
00101 
00102 void rprintf(const char *format, ...)
00103 {
00104     std::va_list arg;
00105 
00106     va_start(arg, format);
00107     VAprintf(false, false, _useDprintf, format, arg);
00108     va_end(arg);   
00109 }
00110 
00111 void VAprintf(bool timstamp, bool newline, bool printEnabled, const char *format, va_list arg)
00112 {
00113      if (!printEnabled)
00114         return;
00115 
00116     if (timstamp)
00117         printTimeStamp();
00118 #ifdef FEATURE_USBSERIAL
00119     if (usb) {
00120         usb->vprintf_irqsafe(format, arg);
00121         if (newline)
00122             usb->printf_irqsafe("\r\n");
00123 #else
00124     if (0) {
00125 #endif
00126     } else if (ser) {
00127         // serial jas 
00128         int r = 0;
00129         r = vsnprintf(NULL, 0, format, arg);
00130         if (r < 82) {
00131             char buffer[82+1];
00132 
00133             vsnprintf(buffer, sizeof(buffer), format, arg);
00134             r = ser->write(buffer, r);
00135         } else {
00136             char *buffer = new char[r+1];
00137             if (buffer) {
00138                 vsnprintf(buffer, r+1, format, arg);
00139                 r = ser->write(buffer, r);
00140                 delete[] buffer;
00141             } else {
00142                 error("%s %d cannot alloc memory (%d bytes)!\r\n", __FILE__, __LINE__, r+1);
00143                 r = 0;
00144             }
00145         }
00146         if (newline)
00147             ser->write("\r\n", 2);
00148     }
00149 }
00150 
00151 
00152 void dump(const char *title, const void *data, int len, bool dwords)
00153 {
00154     dprintf("dump(\"%s\", 0x%x, %d bytes)", title, data, len);
00155 
00156     int i, j, cnt;
00157     unsigned char *u;
00158     const int width = 16;
00159     const int seppos = 7;
00160 
00161     cnt = 0;
00162     u = (unsigned char *)data;
00163     while (len > 0) {
00164         rprintf("%08x: ", (unsigned int)data + cnt);
00165         if (dwords) {
00166             unsigned int *ip = ( unsigned int *)u;
00167             rprintf(" 0x%08x\r\n", *ip);
00168             u+= 4;
00169             len -= 4;
00170             cnt += 4;
00171             continue;
00172         }
00173         cnt += width;
00174         j = len < width ? len : width;
00175         for (i = 0; i < j; i++) {
00176             rprintf("%2.2x ", *(u + i));
00177             if (i == seppos)
00178                 rprintf(" ");
00179         }
00180         rprintf(" ");
00181         if (j < width) {
00182             i = width - j;
00183             if (i > seppos + 1)
00184                 rprintf(" ");
00185             while (i--) {
00186                 rprintf("%s", "   ");
00187             }
00188         }
00189         for (i = 0; i < j; i++) {
00190             int c = *(u + i);
00191             if (c >= ' ' && c <= '~')
00192                 rprintf("%c", c);
00193             else
00194                 rprintf(".");
00195             if (i == seppos)
00196                 rprintf(" ");
00197         }
00198         len -= width;
00199         u += width;
00200         rprintf("\r\n");
00201     }
00202     rprintf("--\r\n");
00203 }
00204 
00205 /*
00206  * Convert compile time to system time
00207  */
00208 time_t
00209 cvt_date(char const *date, char const *time)
00210 {
00211     char s_month[5];
00212     int year;
00213     struct tm t;
00214     static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
00215     sscanf(date, "%s %d %d", s_month, &t.tm_mday, &year);
00216     sscanf(time, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec);
00217     // Find where is s_month in month_names. Deduce month value.
00218     t.tm_mon = (strstr(month_names, s_month) - month_names) / 3;
00219     t.tm_year = year - 1900;
00220     return (int)mktime(&t);
00221 }