This program is guided to help establish a connection between two RFM95 900MHz LoRa radio modules using Maxim Integrated's Feather MCUs (MAX32630FTHR Mbed and the MAX32620FTHR Mbed). Once the radios are configured after powering on and if the radios are wired correctly, the two radios will self identify as either a master or a slave, and will then proceed to PING and PONG back and forth. Information about what is happening between the radios can be seen if the two boards are hooked up to a USB COM port through the included DAPLINK/MAX32625PICO modules.

Dependencies:   BufferedSerial SX1276GenericLib USBDeviceHT max32630fthr

Fork of MAX326xxFTHR_LoRa_PingPong by Devin Alexander

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