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
utils.cpp@20:a1029437adca, 2018-06-03 (annotated)
- Committer:
- Helmut64
- Date:
- Sun Jun 03 19:25:37 2018 +0000
- Revision:
- 20:a1029437adca
- Parent:
- 17:98f2528e8399
When USB is active we keep a Timer running to avoid deepsleep.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 17:98f2528e8399 | 1 | /* |
Helmut64 | 17:98f2528e8399 | 2 | * Copyright (c) 2018 Helmut Tschemernjak |
Helmut64 | 17:98f2528e8399 | 3 | * 30826 Garbsen (Hannover) Germany |
Helmut64 | 17:98f2528e8399 | 4 | */ |
Helmut64 | 17:98f2528e8399 | 5 | #include "main.h" |
Helmut64 | 17:98f2528e8399 | 6 | |
Helmut64 | 17:98f2528e8399 | 7 | time_t cvt_date(char const *date, char const *time); |
Helmut64 | 17:98f2528e8399 | 8 | |
Helmut64 | 17:98f2528e8399 | 9 | BufferedSerial *ser; |
Helmut64 | 17:98f2528e8399 | 10 | #ifdef FEATURE_USBSERIAL |
Helmut64 | 17:98f2528e8399 | 11 | USBSerialBuffered *usb; |
Helmut64 | 17:98f2528e8399 | 12 | #endif |
Helmut64 | 17:98f2528e8399 | 13 | bool _useDprintf; |
Helmut64 | 17:98f2528e8399 | 14 | |
Helmut64 | 20:a1029437adca | 15 | /* |
Helmut64 | 20:a1029437adca | 16 | * keep a timer running to avoid USB hangup in sleep, |
Helmut64 | 20:a1029437adca | 17 | * in newer mbed versions sleep calls deepsleep which may |
Helmut64 | 20:a1029437adca | 18 | * hangup the USBSerial. |
Helmut64 | 20:a1029437adca | 19 | * For this reason we keep a timer pending which avoids deepsleep. |
Helmut64 | 20:a1029437adca | 20 | */ |
Helmut64 | 20:a1029437adca | 21 | static Timeout busyTimer; |
Helmut64 | 20:a1029437adca | 22 | |
Helmut64 | 20:a1029437adca | 23 | void busyTimerFunc(void) |
Helmut64 | 20:a1029437adca | 24 | { |
Helmut64 | 20:a1029437adca | 25 | busyTimer.attach(callback(&busyTimerFunc), 300); |
Helmut64 | 20:a1029437adca | 26 | } |
Helmut64 | 20:a1029437adca | 27 | |
Helmut64 | 17:98f2528e8399 | 28 | void InitSerial(int timeout, DigitalOut *led) |
Helmut64 | 17:98f2528e8399 | 29 | { |
Helmut64 | 17:98f2528e8399 | 30 | _useDprintf = true; |
Helmut64 | 17:98f2528e8399 | 31 | bool uartActive; |
Helmut64 | 17:98f2528e8399 | 32 | { |
Helmut64 | 17:98f2528e8399 | 33 | { |
Helmut64 | 17:98f2528e8399 | 34 | // need to turn rx low to avoid floating signal |
Helmut64 | 17:98f2528e8399 | 35 | DigitalOut rx(USBRX); |
Helmut64 | 17:98f2528e8399 | 36 | rx = 0; |
Helmut64 | 17:98f2528e8399 | 37 | } |
Helmut64 | 17:98f2528e8399 | 38 | DigitalIn uartRX(USBRX); |
Helmut64 | 17:98f2528e8399 | 39 | uartActive = uartRX.read(); |
Helmut64 | 17:98f2528e8399 | 40 | } |
Helmut64 | 17:98f2528e8399 | 41 | #ifdef FEATURE_USBSERIAL |
Helmut64 | 17:98f2528e8399 | 42 | if (!uartActive) { |
Helmut64 | 17:98f2528e8399 | 43 | usb = new USBSerialBuffered(); |
Helmut64 | 17:98f2528e8399 | 44 | Timer t; |
Helmut64 | 17:98f2528e8399 | 45 | t.start(); |
Helmut64 | 17:98f2528e8399 | 46 | while(!usb->connected()) { |
Helmut64 | 17:98f2528e8399 | 47 | if (led) |
Helmut64 | 17:98f2528e8399 | 48 | *led = !*led; |
Helmut64 | 17:98f2528e8399 | 49 | wait_ms(100); |
Helmut64 | 17:98f2528e8399 | 50 | if (timeout) { |
Helmut64 | 17:98f2528e8399 | 51 | if (t.read_ms() >= timeout) |
Helmut64 | 17:98f2528e8399 | 52 | return; |
Helmut64 | 17:98f2528e8399 | 53 | } |
Helmut64 | 17:98f2528e8399 | 54 | } |
Helmut64 | 20:a1029437adca | 55 | busyTimerFunc(); |
Helmut64 | 17:98f2528e8399 | 56 | return; |
Helmut64 | 17:98f2528e8399 | 57 | } else { |
Helmut64 | 17:98f2528e8399 | 58 | #else |
Helmut64 | 17:98f2528e8399 | 59 | { |
Helmut64 | 17:98f2528e8399 | 60 | #endif |
Helmut64 | 17:98f2528e8399 | 61 | ser = new BufferedSerial(USBTX, USBRX); |
Helmut64 | 17:98f2528e8399 | 62 | ser->baud(230400); |
Helmut64 | 17:98f2528e8399 | 63 | ser->format(8); |
Helmut64 | 17:98f2528e8399 | 64 | } |
Helmut64 | 17:98f2528e8399 | 65 | time_t t = cvt_date(__DATE__, __TIME__); |
Helmut64 | 17:98f2528e8399 | 66 | if (t > time(NULL)) { |
Helmut64 | 17:98f2528e8399 | 67 | set_time(t); |
Helmut64 | 17:98f2528e8399 | 68 | } |
Helmut64 | 17:98f2528e8399 | 69 | |
Helmut64 | 17:98f2528e8399 | 70 | } |
Helmut64 | 17:98f2528e8399 | 71 | |
Helmut64 | 17:98f2528e8399 | 72 | void printTimeStamp() |
Helmut64 | 17:98f2528e8399 | 73 | { |
Helmut64 | 17:98f2528e8399 | 74 | static LowPowerTimer *timer; |
Helmut64 | 17:98f2528e8399 | 75 | if (!timer) { |
Helmut64 | 17:98f2528e8399 | 76 | timer = new LowPowerTimer(); |
Helmut64 | 17:98f2528e8399 | 77 | timer->start(); |
Helmut64 | 17:98f2528e8399 | 78 | } |
Helmut64 | 17:98f2528e8399 | 79 | time_t seconds = time(NULL); |
Helmut64 | 17:98f2528e8399 | 80 | struct tm *tm = localtime(&seconds); |
Helmut64 | 17:98f2528e8399 | 81 | int usecs = timer->read_us(); |
Helmut64 | 17:98f2528e8399 | 82 | if (usecs < 0) { |
Helmut64 | 17:98f2528e8399 | 83 | usecs = 0; |
Helmut64 | 17:98f2528e8399 | 84 | timer->stop(); |
Helmut64 | 17:98f2528e8399 | 85 | timer->reset(); |
Helmut64 | 17:98f2528e8399 | 86 | timer->start(); |
Helmut64 | 17:98f2528e8399 | 87 | } |
Helmut64 | 17:98f2528e8399 | 88 | int msecs = usecs % 1000000; |
Helmut64 | 17:98f2528e8399 | 89 | |
Helmut64 | 17:98f2528e8399 | 90 | rprintf("%02d:%02d:%02d.%06d ", tm->tm_hour, tm->tm_min, tm->tm_sec, msecs); |
Helmut64 | 17:98f2528e8399 | 91 | } |
Helmut64 | 17:98f2528e8399 | 92 | |
Helmut64 | 17:98f2528e8399 | 93 | void dprintf(const char *format, ...) |
Helmut64 | 17:98f2528e8399 | 94 | { |
Helmut64 | 17:98f2528e8399 | 95 | std::va_list arg; |
Helmut64 | 17:98f2528e8399 | 96 | |
Helmut64 | 17:98f2528e8399 | 97 | va_start(arg, format); |
Helmut64 | 17:98f2528e8399 | 98 | VAprintf(true, true, _useDprintf, format, arg); |
Helmut64 | 17:98f2528e8399 | 99 | va_end(arg); |
Helmut64 | 17:98f2528e8399 | 100 | } |
Helmut64 | 17:98f2528e8399 | 101 | |
Helmut64 | 17:98f2528e8399 | 102 | void rprintf(const char *format, ...) |
Helmut64 | 17:98f2528e8399 | 103 | { |
Helmut64 | 17:98f2528e8399 | 104 | std::va_list arg; |
Helmut64 | 17:98f2528e8399 | 105 | |
Helmut64 | 17:98f2528e8399 | 106 | va_start(arg, format); |
Helmut64 | 17:98f2528e8399 | 107 | VAprintf(false, false, _useDprintf, format, arg); |
Helmut64 | 17:98f2528e8399 | 108 | va_end(arg); |
Helmut64 | 17:98f2528e8399 | 109 | } |
Helmut64 | 17:98f2528e8399 | 110 | |
Helmut64 | 17:98f2528e8399 | 111 | void VAprintf(bool timstamp, bool newline, bool printEnabled, const char *format, va_list arg) |
Helmut64 | 17:98f2528e8399 | 112 | { |
Helmut64 | 17:98f2528e8399 | 113 | if (!printEnabled) |
Helmut64 | 17:98f2528e8399 | 114 | return; |
Helmut64 | 17:98f2528e8399 | 115 | |
Helmut64 | 17:98f2528e8399 | 116 | if (timstamp) |
Helmut64 | 17:98f2528e8399 | 117 | printTimeStamp(); |
Helmut64 | 17:98f2528e8399 | 118 | #ifdef FEATURE_USBSERIAL |
Helmut64 | 17:98f2528e8399 | 119 | if (usb) { |
Helmut64 | 17:98f2528e8399 | 120 | usb->vprintf_irqsafe(format, arg); |
Helmut64 | 17:98f2528e8399 | 121 | if (newline) |
Helmut64 | 17:98f2528e8399 | 122 | usb->printf_irqsafe("\r\n"); |
Helmut64 | 17:98f2528e8399 | 123 | #else |
Helmut64 | 17:98f2528e8399 | 124 | if (0) { |
Helmut64 | 17:98f2528e8399 | 125 | #endif |
Helmut64 | 17:98f2528e8399 | 126 | } else if (ser) { |
Helmut64 | 17:98f2528e8399 | 127 | // serial jas |
Helmut64 | 17:98f2528e8399 | 128 | int r = 0; |
Helmut64 | 17:98f2528e8399 | 129 | r = vsnprintf(NULL, 0, format, arg); |
Helmut64 | 17:98f2528e8399 | 130 | if (r < 82) { |
Helmut64 | 17:98f2528e8399 | 131 | char buffer[82+1]; |
Helmut64 | 17:98f2528e8399 | 132 | |
Helmut64 | 17:98f2528e8399 | 133 | vsnprintf(buffer, sizeof(buffer), format, arg); |
Helmut64 | 17:98f2528e8399 | 134 | r = ser->write(buffer, r); |
Helmut64 | 17:98f2528e8399 | 135 | } else { |
Helmut64 | 17:98f2528e8399 | 136 | char *buffer = new char[r+1]; |
Helmut64 | 17:98f2528e8399 | 137 | if (buffer) { |
Helmut64 | 17:98f2528e8399 | 138 | vsnprintf(buffer, r+1, format, arg); |
Helmut64 | 17:98f2528e8399 | 139 | r = ser->write(buffer, r); |
Helmut64 | 17:98f2528e8399 | 140 | delete[] buffer; |
Helmut64 | 17:98f2528e8399 | 141 | } else { |
Helmut64 | 17:98f2528e8399 | 142 | error("%s %d cannot alloc memory (%d bytes)!\r\n", __FILE__, __LINE__, r+1); |
Helmut64 | 17:98f2528e8399 | 143 | r = 0; |
Helmut64 | 17:98f2528e8399 | 144 | } |
Helmut64 | 17:98f2528e8399 | 145 | } |
Helmut64 | 17:98f2528e8399 | 146 | if (newline) |
Helmut64 | 17:98f2528e8399 | 147 | ser->write("\r\n", 2); |
Helmut64 | 17:98f2528e8399 | 148 | } |
Helmut64 | 17:98f2528e8399 | 149 | } |
Helmut64 | 17:98f2528e8399 | 150 | |
Helmut64 | 17:98f2528e8399 | 151 | |
Helmut64 | 17:98f2528e8399 | 152 | void dump(const char *title, const void *data, int len, bool dwords) |
Helmut64 | 17:98f2528e8399 | 153 | { |
Helmut64 | 17:98f2528e8399 | 154 | dprintf("dump(\"%s\", 0x%x, %d bytes)", title, data, len); |
Helmut64 | 17:98f2528e8399 | 155 | |
Helmut64 | 17:98f2528e8399 | 156 | int i, j, cnt; |
Helmut64 | 17:98f2528e8399 | 157 | unsigned char *u; |
Helmut64 | 17:98f2528e8399 | 158 | const int width = 16; |
Helmut64 | 17:98f2528e8399 | 159 | const int seppos = 7; |
Helmut64 | 17:98f2528e8399 | 160 | |
Helmut64 | 17:98f2528e8399 | 161 | cnt = 0; |
Helmut64 | 17:98f2528e8399 | 162 | u = (unsigned char *)data; |
Helmut64 | 17:98f2528e8399 | 163 | while (len > 0) { |
Helmut64 | 17:98f2528e8399 | 164 | rprintf("%08x: ", (unsigned int)data + cnt); |
Helmut64 | 17:98f2528e8399 | 165 | if (dwords) { |
Helmut64 | 17:98f2528e8399 | 166 | unsigned int *ip = ( unsigned int *)u; |
Helmut64 | 17:98f2528e8399 | 167 | rprintf(" 0x%08x\r\n", *ip); |
Helmut64 | 17:98f2528e8399 | 168 | u+= 4; |
Helmut64 | 17:98f2528e8399 | 169 | len -= 4; |
Helmut64 | 17:98f2528e8399 | 170 | cnt += 4; |
Helmut64 | 17:98f2528e8399 | 171 | continue; |
Helmut64 | 17:98f2528e8399 | 172 | } |
Helmut64 | 17:98f2528e8399 | 173 | cnt += width; |
Helmut64 | 17:98f2528e8399 | 174 | j = len < width ? len : width; |
Helmut64 | 17:98f2528e8399 | 175 | for (i = 0; i < j; i++) { |
Helmut64 | 17:98f2528e8399 | 176 | rprintf("%2.2x ", *(u + i)); |
Helmut64 | 17:98f2528e8399 | 177 | if (i == seppos) |
Helmut64 | 17:98f2528e8399 | 178 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 179 | } |
Helmut64 | 17:98f2528e8399 | 180 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 181 | if (j < width) { |
Helmut64 | 17:98f2528e8399 | 182 | i = width - j; |
Helmut64 | 17:98f2528e8399 | 183 | if (i > seppos + 1) |
Helmut64 | 17:98f2528e8399 | 184 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 185 | while (i--) { |
Helmut64 | 17:98f2528e8399 | 186 | rprintf("%s", " "); |
Helmut64 | 17:98f2528e8399 | 187 | } |
Helmut64 | 17:98f2528e8399 | 188 | } |
Helmut64 | 17:98f2528e8399 | 189 | for (i = 0; i < j; i++) { |
Helmut64 | 17:98f2528e8399 | 190 | int c = *(u + i); |
Helmut64 | 17:98f2528e8399 | 191 | if (c >= ' ' && c <= '~') |
Helmut64 | 17:98f2528e8399 | 192 | rprintf("%c", c); |
Helmut64 | 17:98f2528e8399 | 193 | else |
Helmut64 | 17:98f2528e8399 | 194 | rprintf("."); |
Helmut64 | 17:98f2528e8399 | 195 | if (i == seppos) |
Helmut64 | 17:98f2528e8399 | 196 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 197 | } |
Helmut64 | 17:98f2528e8399 | 198 | len -= width; |
Helmut64 | 17:98f2528e8399 | 199 | u += width; |
Helmut64 | 17:98f2528e8399 | 200 | rprintf("\r\n"); |
Helmut64 | 17:98f2528e8399 | 201 | } |
Helmut64 | 17:98f2528e8399 | 202 | rprintf("--\r\n"); |
Helmut64 | 17:98f2528e8399 | 203 | } |
Helmut64 | 17:98f2528e8399 | 204 | |
Helmut64 | 17:98f2528e8399 | 205 | /* |
Helmut64 | 17:98f2528e8399 | 206 | * Convert compile time to system time |
Helmut64 | 17:98f2528e8399 | 207 | */ |
Helmut64 | 17:98f2528e8399 | 208 | time_t |
Helmut64 | 17:98f2528e8399 | 209 | cvt_date(char const *date, char const *time) |
Helmut64 | 17:98f2528e8399 | 210 | { |
Helmut64 | 17:98f2528e8399 | 211 | char s_month[5]; |
Helmut64 | 17:98f2528e8399 | 212 | int year; |
Helmut64 | 17:98f2528e8399 | 213 | struct tm t; |
Helmut64 | 17:98f2528e8399 | 214 | static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
Helmut64 | 17:98f2528e8399 | 215 | sscanf(date, "%s %d %d", s_month, &t.tm_mday, &year); |
Helmut64 | 17:98f2528e8399 | 216 | sscanf(time, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec); |
Helmut64 | 17:98f2528e8399 | 217 | // Find where is s_month in month_names. Deduce month value. |
Helmut64 | 17:98f2528e8399 | 218 | t.tm_mon = (strstr(month_names, s_month) - month_names) / 3; |
Helmut64 | 17:98f2528e8399 | 219 | t.tm_year = year - 1900; |
Helmut64 | 17:98f2528e8399 | 220 | return (int)mktime(&t); |
Helmut64 | 17:98f2528e8399 | 221 | } |