Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: lr1110 sx12xx_hal
main.cpp@1:416ed16806fc, 2021-02-09 (annotated)
- Committer:
- Wayne Roberts
- Date:
- Tue Feb 09 10:47:25 2021 -0800
- Revision:
- 1:416ed16806fc
add source file
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Wayne Roberts |
1:416ed16806fc | 1 | #include "radio.h" |
| Wayne Roberts |
1:416ed16806fc | 2 | |
| Wayne Roberts |
1:416ed16806fc | 3 | #define TX_DBM 20 |
| Wayne Roberts |
1:416ed16806fc | 4 | #define BW_KHZ 500 |
| Wayne Roberts |
1:416ed16806fc | 5 | #define SPREADING_FACTOR 11 |
| Wayne Roberts |
1:416ed16806fc | 6 | #define CF_HZ 919000000 |
| Wayne Roberts |
1:416ed16806fc | 7 | |
| Wayne Roberts |
1:416ed16806fc | 8 | #define HEADER_LENGTH 10 /* for chipEUI and extra reserved */ |
| Wayne Roberts |
1:416ed16806fc | 9 | #define WIFI_MAX_RESULTS 20 |
| Wayne Roberts |
1:416ed16806fc | 10 | #define LORA_RX_TIME_MS 20000 /* how long for cloud to reply with resolved location? */ |
| Wayne Roberts |
1:416ed16806fc | 11 | |
| Wayne Roberts |
1:416ed16806fc | 12 | /**********************************************************************/ |
| Wayne Roberts |
1:416ed16806fc | 13 | bool wifiResultFormatBasic; /* false, TODO get basic results functional */ |
| Wayne Roberts |
1:416ed16806fc | 14 | EventQueue queue(4 * EVENTS_EVENT_SIZE); |
| Wayne Roberts |
1:416ed16806fc | 15 | |
| Wayne Roberts |
1:416ed16806fc | 16 | unsigned packet_len; |
| Wayne Roberts |
1:416ed16806fc | 17 | uint8_t chip_eui[8]; |
| Wayne Roberts |
1:416ed16806fc | 18 | |
| Wayne Roberts |
1:416ed16806fc | 19 | InterruptIn ub(USER_BUTTON); /* released = hi, pressed = lo */ |
| Wayne Roberts |
1:416ed16806fc | 20 | void button_released(void); |
| Wayne Roberts |
1:416ed16806fc | 21 | int wifi_scan_id, long_press_id; |
| Wayne Roberts |
1:416ed16806fc | 22 | bool long_pressed; |
| Wayne Roberts |
1:416ed16806fc | 23 | |
| Wayne Roberts |
1:416ed16806fc | 24 | uint64_t wifi_start_at, wifi_scan_dur; |
| Wayne Roberts |
1:416ed16806fc | 25 | |
| Wayne Roberts |
1:416ed16806fc | 26 | static void cfg_lora() |
| Wayne Roberts |
1:416ed16806fc | 27 | { |
| Wayne Roberts |
1:416ed16806fc | 28 | /* after wifi scan, lora is gone */ |
| Wayne Roberts |
1:416ed16806fc | 29 | Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); |
| Wayne Roberts |
1:416ed16806fc | 30 | Radio::SetChannel(CF_HZ); |
| Wayne Roberts |
1:416ed16806fc | 31 | |
| Wayne Roberts |
1:416ed16806fc | 32 | Radio::set_tx_dbm(TX_DBM); |
| Wayne Roberts |
1:416ed16806fc | 33 | |
| Wayne Roberts |
1:416ed16806fc | 34 | // preambleLen, fixLen, crcOn, invIQ |
| Wayne Roberts |
1:416ed16806fc | 35 | Radio::LoRaPacketConfig(8, false, true, false); |
| Wayne Roberts |
1:416ed16806fc | 36 | } |
| Wayne Roberts |
1:416ed16806fc | 37 | |
| Wayne Roberts |
1:416ed16806fc | 38 | void wifi_scan() |
| Wayne Roberts |
1:416ed16806fc | 39 | { |
| Wayne Roberts |
1:416ed16806fc | 40 | uint8_t wifiScan_buf[9]; |
| Wayne Roberts |
1:416ed16806fc | 41 | |
| Wayne Roberts |
1:416ed16806fc | 42 | { /* wifi scan defaults, see LR1110 user manual section 10.2 */ |
| Wayne Roberts |
1:416ed16806fc | 43 | unsigned chanmask = 0x0421; // ch1, ch6, ch11 |
| Wayne Roberts |
1:416ed16806fc | 44 | unsigned timeout = 105; // in milliseconds, 100 wifi TUs (beacon interval) |
| Wayne Roberts |
1:416ed16806fc | 45 | |
| Wayne Roberts |
1:416ed16806fc | 46 | wifiScan_buf[0] = 0x01; // wifi type |
| Wayne Roberts |
1:416ed16806fc | 47 | wifiScan_buf[2] = chanmask; // chanmask-lo |
| Wayne Roberts |
1:416ed16806fc | 48 | chanmask >>= 8; |
| Wayne Roberts |
1:416ed16806fc | 49 | wifiScan_buf[1] = chanmask; // chanmask-hi |
| Wayne Roberts |
1:416ed16806fc | 50 | wifiScan_buf[3] = 0x02; // acqMode |
| Wayne Roberts |
1:416ed16806fc | 51 | wifiScan_buf[4] = WIFI_MAX_RESULTS; // NbMaxRes |
| Wayne Roberts |
1:416ed16806fc | 52 | wifiScan_buf[5] = 0x10; // NbScanPerChan |
| Wayne Roberts |
1:416ed16806fc | 53 | wifiScan_buf[7] = timeout; // Timeout-lo |
| Wayne Roberts |
1:416ed16806fc | 54 | timeout >>= 8; |
| Wayne Roberts |
1:416ed16806fc | 55 | wifiScan_buf[6] = timeout; // Timeout-hi |
| Wayne Roberts |
1:416ed16806fc | 56 | wifiScan_buf[8] = 0x00; // AbortOnTimeout |
| Wayne Roberts |
1:416ed16806fc | 57 | } |
| Wayne Roberts |
1:416ed16806fc | 58 | |
| Wayne Roberts |
1:416ed16806fc | 59 | Radio::radio.xfer(OPCODE_WIFI_SCAN, 9, 0, wifiScan_buf); |
| Wayne Roberts |
1:416ed16806fc | 60 | wifi_start_at = Kernel::get_ms_count(); |
| Wayne Roberts |
1:416ed16806fc | 61 | printf("wifiScan...\r\n"); |
| Wayne Roberts |
1:416ed16806fc | 62 | } |
| Wayne Roberts |
1:416ed16806fc | 63 | |
| Wayne Roberts |
1:416ed16806fc | 64 | void long_press() |
| Wayne Roberts |
1:416ed16806fc | 65 | { |
| Wayne Roberts |
1:416ed16806fc | 66 | long_pressed = true; |
| Wayne Roberts |
1:416ed16806fc | 67 | wifi_scan(); |
| Wayne Roberts |
1:416ed16806fc | 68 | } |
| Wayne Roberts |
1:416ed16806fc | 69 | |
| Wayne Roberts |
1:416ed16806fc | 70 | void button_pressed() |
| Wayne Roberts |
1:416ed16806fc | 71 | { |
| Wayne Roberts |
1:416ed16806fc | 72 | ub.rise(button_released); |
| Wayne Roberts |
1:416ed16806fc | 73 | queue.cancel(wifi_scan_id); |
| Wayne Roberts |
1:416ed16806fc | 74 | long_pressed = false; |
| Wayne Roberts |
1:416ed16806fc | 75 | long_press_id = queue.call_in(500, long_press); |
| Wayne Roberts |
1:416ed16806fc | 76 | } |
| Wayne Roberts |
1:416ed16806fc | 77 | |
| Wayne Roberts |
1:416ed16806fc | 78 | void button_released() |
| Wayne Roberts |
1:416ed16806fc | 79 | { |
| Wayne Roberts |
1:416ed16806fc | 80 | ub.fall(button_pressed); |
| Wayne Roberts |
1:416ed16806fc | 81 | if (!long_pressed) { |
| Wayne Roberts |
1:416ed16806fc | 82 | wifi_scan_id = queue.call_in(20, wifi_scan); |
| Wayne Roberts |
1:416ed16806fc | 83 | queue.cancel(long_press_id); |
| Wayne Roberts |
1:416ed16806fc | 84 | } |
| Wayne Roberts |
1:416ed16806fc | 85 | } |
| Wayne Roberts |
1:416ed16806fc | 86 | |
| Wayne Roberts |
1:416ed16806fc | 87 | void txDoneCB() |
| Wayne Roberts |
1:416ed16806fc | 88 | { |
| Wayne Roberts |
1:416ed16806fc | 89 | Radio::Rx(0); |
| Wayne Roberts |
1:416ed16806fc | 90 | queue.call_in(LORA_RX_TIME_MS, Radio::Standby); |
| Wayne Roberts |
1:416ed16806fc | 91 | } |
| Wayne Roberts |
1:416ed16806fc | 92 | |
| Wayne Roberts |
1:416ed16806fc | 93 | void rxDoneCB(uint8_t size, float rssi, float snr) |
| Wayne Roberts |
1:416ed16806fc | 94 | { |
| Wayne Roberts |
1:416ed16806fc | 95 | unsigned i; |
| Wayne Roberts |
1:416ed16806fc | 96 | printf("%.1fdBm snr:%.1fdB\t", rssi, snr); |
| Wayne Roberts |
1:416ed16806fc | 97 | |
| Wayne Roberts |
1:416ed16806fc | 98 | /* |
| Wayne Roberts |
1:416ed16806fc | 99 | for (i = 0; i < size; i++) { |
| Wayne Roberts |
1:416ed16806fc | 100 | printf("%02x ", Radio::radio.rx_buf[i]); |
| Wayne Roberts |
1:416ed16806fc | 101 | } |
| Wayne Roberts |
1:416ed16806fc | 102 | printf("\r\n");*/ |
| Wayne Roberts |
1:416ed16806fc | 103 | |
| Wayne Roberts |
1:416ed16806fc | 104 | if (memcmp(Radio::radio.rx_buf, chip_eui, 8) == 0) { |
| Wayne Roberts |
1:416ed16806fc | 105 | /* print resolved coordinates from cloud */ |
| Wayne Roberts |
1:416ed16806fc | 106 | printf(">> %s\r\n", Radio::radio.rx_buf + HEADER_LENGTH); |
| Wayne Roberts |
1:416ed16806fc | 107 | } |
| Wayne Roberts |
1:416ed16806fc | 108 | } |
| Wayne Roberts |
1:416ed16806fc | 109 | |
| Wayne Roberts |
1:416ed16806fc | 110 | struct wifidr { |
| Wayne Roberts |
1:416ed16806fc | 111 | const char *txt; |
| Wayne Roberts |
1:416ed16806fc | 112 | float Mbps; |
| Wayne Roberts |
1:416ed16806fc | 113 | }; |
| Wayne Roberts |
1:416ed16806fc | 114 | |
| Wayne Roberts |
1:416ed16806fc | 115 | const struct wifidr wifiDatarates[] = { |
| Wayne Roberts |
1:416ed16806fc | 116 | /* 0 */ { NULL, 0}, |
| Wayne Roberts |
1:416ed16806fc | 117 | /* 1 */ { "DBPSK", 1}, |
| Wayne Roberts |
1:416ed16806fc | 118 | /* 2 */ { "DQPSK", 2}, |
| Wayne Roberts |
1:416ed16806fc | 119 | /* 3 */ { "BPSK", 6}, |
| Wayne Roberts |
1:416ed16806fc | 120 | /* 4 */ { "BPSK", 9}, |
| Wayne Roberts |
1:416ed16806fc | 121 | /* 5 */ { "QPSK", 12}, |
| Wayne Roberts |
1:416ed16806fc | 122 | /* 6 */ { "QPSK", 18}, |
| Wayne Roberts |
1:416ed16806fc | 123 | /* 7 */ { "16-QAM", 24}, |
| Wayne Roberts |
1:416ed16806fc | 124 | /* 8 */ { "16-QAM", 36}, |
| Wayne Roberts |
1:416ed16806fc | 125 | /* 9 */ { "(9)", 0}, |
| Wayne Roberts |
1:416ed16806fc | 126 | /* 10 */ { "(10)", 0}, |
| Wayne Roberts |
1:416ed16806fc | 127 | /* 11 */ { "BPSK", 6.5}, |
| Wayne Roberts |
1:416ed16806fc | 128 | /* 12 */ { "QPSK", 13}, |
| Wayne Roberts |
1:416ed16806fc | 129 | /* 13 */ { "QPSK", 19.5}, |
| Wayne Roberts |
1:416ed16806fc | 130 | /* 14 */ { "16-QAM", 26}, |
| Wayne Roberts |
1:416ed16806fc | 131 | /* 15 */ { "16-QAM", 39}, |
| Wayne Roberts |
1:416ed16806fc | 132 | /* 16 */ { "(16)", 0}, |
| Wayne Roberts |
1:416ed16806fc | 133 | /* 17 */ { "(17)", 0}, |
| Wayne Roberts |
1:416ed16806fc | 134 | /* 18 */ { "(18)", 0}, |
| Wayne Roberts |
1:416ed16806fc | 135 | /* 19 */ { "BPSK", 7.2}, |
| Wayne Roberts |
1:416ed16806fc | 136 | /* 20 */ { "QPSK", 14.4}, |
| Wayne Roberts |
1:416ed16806fc | 137 | /* 21 */ { "QPSK", 21.7}, |
| Wayne Roberts |
1:416ed16806fc | 138 | /* 22 */ { "16-QAM", 28.9}, |
| Wayne Roberts |
1:416ed16806fc | 139 | /* 23 */ { "16-QAM", 43.3}, |
| Wayne Roberts |
1:416ed16806fc | 140 | }; |
| Wayne Roberts |
1:416ed16806fc | 141 | |
| Wayne Roberts |
1:416ed16806fc | 142 | void print_wifi_result(const uint8_t *result) |
| Wayne Roberts |
1:416ed16806fc | 143 | { |
| Wayne Roberts |
1:416ed16806fc | 144 | char out[96]; |
| Wayne Roberts |
1:416ed16806fc | 145 | char str[24]; |
| Wayne Roberts |
1:416ed16806fc | 146 | unsigned n, macStart; |
| Wayne Roberts |
1:416ed16806fc | 147 | wifiType_t wt; |
| Wayne Roberts |
1:416ed16806fc | 148 | wifiChanInfo_t ci; |
| Wayne Roberts |
1:416ed16806fc | 149 | wt.octet = result[0]; |
| Wayne Roberts |
1:416ed16806fc | 150 | ci.octet = result[1]; |
| Wayne Roberts |
1:416ed16806fc | 151 | out[0] = 0; |
| Wayne Roberts |
1:416ed16806fc | 152 | strcat(out, "802.11"); |
| Wayne Roberts |
1:416ed16806fc | 153 | switch (wt.bits.signal) { |
| Wayne Roberts |
1:416ed16806fc | 154 | case 1: strcat(out, "b"); break; |
| Wayne Roberts |
1:416ed16806fc | 155 | case 2: strcat(out, "g"); break; |
| Wayne Roberts |
1:416ed16806fc | 156 | case 3: strcat(out, "n"); break; |
| Wayne Roberts |
1:416ed16806fc | 157 | } |
| Wayne Roberts |
1:416ed16806fc | 158 | sprintf(str, " %s %.1fMbps", wifiDatarates[wt.bits.datarate].txt, wifiDatarates[wt.bits.datarate].Mbps); |
| Wayne Roberts |
1:416ed16806fc | 159 | strcat(out, str); |
| Wayne Roberts |
1:416ed16806fc | 160 | strcat(out, " "); |
| Wayne Roberts |
1:416ed16806fc | 161 | |
| Wayne Roberts |
1:416ed16806fc | 162 | sprintf(str, "ch%u ", ci.bits.channelID); |
| Wayne Roberts |
1:416ed16806fc | 163 | strcat(out, str); |
| Wayne Roberts |
1:416ed16806fc | 164 | switch (ci.bits.channelID) { |
| Wayne Roberts |
1:416ed16806fc | 165 | // table 10-5 |
| Wayne Roberts |
1:416ed16806fc | 166 | } |
| Wayne Roberts |
1:416ed16806fc | 167 | strcat(out, " "); |
| Wayne Roberts |
1:416ed16806fc | 168 | sprintf(str, "mv:%u ", ci.bits.macValidationID); |
| Wayne Roberts |
1:416ed16806fc | 169 | strcat(out, str); |
| Wayne Roberts |
1:416ed16806fc | 170 | switch (ci.bits.macValidationID) { |
| Wayne Roberts |
1:416ed16806fc | 171 | case 1: strcat(out, "gateway"); break; |
| Wayne Roberts |
1:416ed16806fc | 172 | case 2: strcat(out, "phone"); break; |
| Wayne Roberts |
1:416ed16806fc | 173 | case 3: strcat(out, "?"); break; |
| Wayne Roberts |
1:416ed16806fc | 174 | // table 10.8 |
| Wayne Roberts |
1:416ed16806fc | 175 | } |
| Wayne Roberts |
1:416ed16806fc | 176 | |
| Wayne Roberts |
1:416ed16806fc | 177 | strcat(out, " "); |
| Wayne Roberts |
1:416ed16806fc | 178 | |
| Wayne Roberts |
1:416ed16806fc | 179 | if (wifiResultFormatBasic) { |
| Wayne Roberts |
1:416ed16806fc | 180 | macStart = 3; |
| Wayne Roberts |
1:416ed16806fc | 181 | } else { |
| Wayne Roberts |
1:416ed16806fc | 182 | macStart = 4; |
| Wayne Roberts |
1:416ed16806fc | 183 | } |
| Wayne Roberts |
1:416ed16806fc | 184 | for (n = 0; n < 6; n++) { |
| Wayne Roberts |
1:416ed16806fc | 185 | sprintf(str, "%02x", result[n+macStart]); |
| Wayne Roberts |
1:416ed16806fc | 186 | strcat(out, str); |
| Wayne Roberts |
1:416ed16806fc | 187 | if (n < 5) |
| Wayne Roberts |
1:416ed16806fc | 188 | strcat(out, ":"); |
| Wayne Roberts |
1:416ed16806fc | 189 | } |
| Wayne Roberts |
1:416ed16806fc | 190 | |
| Wayne Roberts |
1:416ed16806fc | 191 | sprintf(str, " rssi:%d ", (int8_t)result[2]); |
| Wayne Roberts |
1:416ed16806fc | 192 | strcat(out, str); |
| Wayne Roberts |
1:416ed16806fc | 193 | |
| Wayne Roberts |
1:416ed16806fc | 194 | if (!wifiResultFormatBasic) { |
| Wayne Roberts |
1:416ed16806fc | 195 | sprintf(str, "frameCtrl:%02x ", result[3]); |
| Wayne Roberts |
1:416ed16806fc | 196 | strcat(out, str); |
| Wayne Roberts |
1:416ed16806fc | 197 | } |
| Wayne Roberts |
1:416ed16806fc | 198 | printf("%s\r\n", out); |
| Wayne Roberts |
1:416ed16806fc | 199 | } |
| Wayne Roberts |
1:416ed16806fc | 200 | |
| Wayne Roberts |
1:416ed16806fc | 201 | static void service() |
| Wayne Roberts |
1:416ed16806fc | 202 | { |
| Wayne Roberts |
1:416ed16806fc | 203 | irq_t irq; |
| Wayne Roberts |
1:416ed16806fc | 204 | irq.dword = Radio::radio.service(); |
| Wayne Roberts |
1:416ed16806fc | 205 | if (irq.bits.WifiDone) { |
| Wayne Roberts |
1:416ed16806fc | 206 | unsigned n; |
| Wayne Roberts |
1:416ed16806fc | 207 | stat_t stat; |
| Wayne Roberts |
1:416ed16806fc | 208 | uint8_t nbResults; |
| Wayne Roberts |
1:416ed16806fc | 209 | stat.word = Radio::radio.xfer(OPCODE_GET_WIFI_NB_RESULTS, 0, 0, NULL); |
| Wayne Roberts |
1:416ed16806fc | 210 | stat.word = Radio::radio.xfer(0x0000, 0, 1, &nbResults); |
| Wayne Roberts |
1:416ed16806fc | 211 | if (stat.bits.cmdStatus != CMD_DAT) { |
| Wayne Roberts |
1:416ed16806fc | 212 | printf("get-nbResult-fail\r\n"); |
| Wayne Roberts |
1:416ed16806fc | 213 | return; |
| Wayne Roberts |
1:416ed16806fc | 214 | } |
| Wayne Roberts |
1:416ed16806fc | 215 | packet_len = HEADER_LENGTH; |
| Wayne Roberts |
1:416ed16806fc | 216 | printf("%ums nbResults:%u\r\n", (unsigned)wifi_scan_dur, nbResults); |
| Wayne Roberts |
1:416ed16806fc | 217 | for (n = 0; n < nbResults; n++) { |
| Wayne Roberts |
1:416ed16806fc | 218 | uint8_t buf[3]; |
| Wayne Roberts |
1:416ed16806fc | 219 | uint8_t resultBuf[22]; |
| Wayne Roberts |
1:416ed16806fc | 220 | buf[0] = n; |
| Wayne Roberts |
1:416ed16806fc | 221 | buf[1] = 1; // number of results in this read |
| Wayne Roberts |
1:416ed16806fc | 222 | buf[2] = wifiResultFormatBasic ? 4 : 1; |
| Wayne Roberts |
1:416ed16806fc | 223 | stat.word = Radio::radio.xfer(OPCODE_WIFI_READ_RESULTS, 3, 0, buf); |
| Wayne Roberts |
1:416ed16806fc | 224 | // basic = 9byte length |
| Wayne Roberts |
1:416ed16806fc | 225 | // full = 22byte length |
| Wayne Roberts |
1:416ed16806fc | 226 | stat.word = Radio::radio.xfer(0x0000, 0, wifiResultFormatBasic ? 9 : 22, resultBuf); |
| Wayne Roberts |
1:416ed16806fc | 227 | |
| Wayne Roberts |
1:416ed16806fc | 228 | if (stat.bits.cmdStatus == CMD_DAT) { |
| Wayne Roberts |
1:416ed16806fc | 229 | unsigned n, macStart; |
| Wayne Roberts |
1:416ed16806fc | 230 | wifiChanInfo_t ci; |
| Wayne Roberts |
1:416ed16806fc | 231 | ci.octet = resultBuf[1]; |
| Wayne Roberts |
1:416ed16806fc | 232 | if (ci.bits.macValidationID == 1) { // gateway (AP) |
| Wayne Roberts |
1:416ed16806fc | 233 | macStart = wifiResultFormatBasic ? 3 : 4; |
| Wayne Roberts |
1:416ed16806fc | 234 | for (n = 0; n < 6; n++) { |
| Wayne Roberts |
1:416ed16806fc | 235 | Radio::radio.tx_buf[packet_len++] = resultBuf[n+macStart]; |
| Wayne Roberts |
1:416ed16806fc | 236 | printf("%02x", resultBuf[n+macStart]); |
| Wayne Roberts |
1:416ed16806fc | 237 | if (n < 5) |
| Wayne Roberts |
1:416ed16806fc | 238 | printf(":"); |
| Wayne Roberts |
1:416ed16806fc | 239 | } |
| Wayne Roberts |
1:416ed16806fc | 240 | printf(" rssi:%d\r\n", (int8_t)resultBuf[2]); |
| Wayne Roberts |
1:416ed16806fc | 241 | Radio::radio.tx_buf[packet_len++] = resultBuf[2]; |
| Wayne Roberts |
1:416ed16806fc | 242 | } |
| Wayne Roberts |
1:416ed16806fc | 243 | } else |
| Wayne Roberts |
1:416ed16806fc | 244 | printf("readResult:%s\r\n", Radio::radio.cmdStatus_toString(stat.bits.cmdStatus)); |
| Wayne Roberts |
1:416ed16806fc | 245 | } |
| Wayne Roberts |
1:416ed16806fc | 246 | |
| Wayne Roberts |
1:416ed16806fc | 247 | if (!long_pressed) { |
| Wayne Roberts |
1:416ed16806fc | 248 | unsigned n; |
| Wayne Roberts |
1:416ed16806fc | 249 | cfg_lora(); |
| Wayne Roberts |
1:416ed16806fc | 250 | for (n = 0; n < 8; n++) |
| Wayne Roberts |
1:416ed16806fc | 251 | Radio::radio.tx_buf[n] = chip_eui[n]; |
| Wayne Roberts |
1:416ed16806fc | 252 | |
| Wayne Roberts |
1:416ed16806fc | 253 | Radio::radio.tx_buf[n++] = 0; // rfu |
| Wayne Roberts |
1:416ed16806fc | 254 | Radio::radio.tx_buf[n++] = 0; // rfu |
| Wayne Roberts |
1:416ed16806fc | 255 | printf("pktLen:%u\r\n", packet_len); |
| Wayne Roberts |
1:416ed16806fc | 256 | Radio::Send(packet_len, 0, 0, 0); /* begin transmission */ |
| Wayne Roberts |
1:416ed16806fc | 257 | } |
| Wayne Roberts |
1:416ed16806fc | 258 | } // ..if (irq.bits.WifiDone) |
| Wayne Roberts |
1:416ed16806fc | 259 | |
| Wayne Roberts |
1:416ed16806fc | 260 | if (irq.bits.TxDone) { |
| Wayne Roberts |
1:416ed16806fc | 261 | printf("main-TxDone\r\n"); |
| Wayne Roberts |
1:416ed16806fc | 262 | } |
| Wayne Roberts |
1:416ed16806fc | 263 | } |
| Wayne Roberts |
1:416ed16806fc | 264 | |
| Wayne Roberts |
1:416ed16806fc | 265 | void radio_irq_callback() |
| Wayne Roberts |
1:416ed16806fc | 266 | { |
| Wayne Roberts |
1:416ed16806fc | 267 | wifi_scan_dur = Kernel::get_ms_count() - wifi_start_at; |
| Wayne Roberts |
1:416ed16806fc | 268 | queue.call(service); |
| Wayne Roberts |
1:416ed16806fc | 269 | } |
| Wayne Roberts |
1:416ed16806fc | 270 | |
| Wayne Roberts |
1:416ed16806fc | 271 | const RadioEvents_t rev = { |
| Wayne Roberts |
1:416ed16806fc | 272 | /* DioPin_top_half */ radio_irq_callback, |
| Wayne Roberts |
1:416ed16806fc | 273 | /* TxDone_topHalf */ NULL, |
| Wayne Roberts |
1:416ed16806fc | 274 | /* TxDone_botHalf */ txDoneCB, |
| Wayne Roberts |
1:416ed16806fc | 275 | /* TxTimeout */ NULL, |
| Wayne Roberts |
1:416ed16806fc | 276 | /* RxDone */ rxDoneCB, |
| Wayne Roberts |
1:416ed16806fc | 277 | /* RxTimeout */ NULL, |
| Wayne Roberts |
1:416ed16806fc | 278 | /* RxError */ NULL, |
| Wayne Roberts |
1:416ed16806fc | 279 | /* FhssChangeChannel */NULL, |
| Wayne Roberts |
1:416ed16806fc | 280 | /* CadDone */ NULL |
| Wayne Roberts |
1:416ed16806fc | 281 | }; |
| Wayne Roberts |
1:416ed16806fc | 282 | |
| Wayne Roberts |
1:416ed16806fc | 283 | int main() |
| Wayne Roberts |
1:416ed16806fc | 284 | { |
| Wayne Roberts |
1:416ed16806fc | 285 | Radio::Init(&rev); |
| Wayne Roberts |
1:416ed16806fc | 286 | |
| Wayne Roberts |
1:416ed16806fc | 287 | Radio::Standby(); |
| Wayne Roberts |
1:416ed16806fc | 288 | cfg_lora(); |
| Wayne Roberts |
1:416ed16806fc | 289 | |
| Wayne Roberts |
1:416ed16806fc | 290 | { |
| Wayne Roberts |
1:416ed16806fc | 291 | uint8_t buf[9]; |
| Wayne Roberts |
1:416ed16806fc | 292 | stat_t stat; |
| Wayne Roberts |
1:416ed16806fc | 293 | stat.word = Radio::radio.xfer(OPCODE_GET_VERSION, 0, 0, NULL); |
| Wayne Roberts |
1:416ed16806fc | 294 | stat.word = Radio::radio.xfer(0x0000, 0, 4, buf); |
| Wayne Roberts |
1:416ed16806fc | 295 | if (stat.bits.cmdStatus == CMD_DAT) { |
| Wayne Roberts |
1:416ed16806fc | 296 | printf("LR1110 chip:%02x use:%02x fw-v%u.%u\r\n", |
| Wayne Roberts |
1:416ed16806fc | 297 | buf[0], /* silicon rev */ |
| Wayne Roberts |
1:416ed16806fc | 298 | buf[1], /* use case */ |
| Wayne Roberts |
1:416ed16806fc | 299 | buf[2], /* firmware major */ |
| Wayne Roberts |
1:416ed16806fc | 300 | buf[3] /* firmware minor */ |
| Wayne Roberts |
1:416ed16806fc | 301 | ); |
| Wayne Roberts |
1:416ed16806fc | 302 | } |
| Wayne Roberts |
1:416ed16806fc | 303 | |
| Wayne Roberts |
1:416ed16806fc | 304 | stat.word = Radio::radio.xfer(OPCODE_GET_DEVEUI, 0, 0, NULL); |
| Wayne Roberts |
1:416ed16806fc | 305 | stat.word = Radio::radio.xfer(0x0000, 0, 9, buf); |
| Wayne Roberts |
1:416ed16806fc | 306 | memcpy(chip_eui, buf+1, 8); |
| Wayne Roberts |
1:416ed16806fc | 307 | for (unsigned i = 0; i < 9; i++) |
| Wayne Roberts |
1:416ed16806fc | 308 | printf("%02x ", buf[i]); |
| Wayne Roberts |
1:416ed16806fc | 309 | printf("\r\n"); |
| Wayne Roberts |
1:416ed16806fc | 310 | } |
| Wayne Roberts |
1:416ed16806fc | 311 | |
| Wayne Roberts |
1:416ed16806fc | 312 | #ifdef AUTO_TX |
| Wayne Roberts |
1:416ed16806fc | 313 | queue.call_in(500, tx_test); |
| Wayne Roberts |
1:416ed16806fc | 314 | #endif /* AUTO_TX */ |
| Wayne Roberts |
1:416ed16806fc | 315 | |
| Wayne Roberts |
1:416ed16806fc | 316 | if (ub.read()) |
| Wayne Roberts |
1:416ed16806fc | 317 | ub.fall(button_pressed); |
| Wayne Roberts |
1:416ed16806fc | 318 | else |
| Wayne Roberts |
1:416ed16806fc | 319 | ub.rise(button_released); |
| Wayne Roberts |
1:416ed16806fc | 320 | |
| Wayne Roberts |
1:416ed16806fc | 321 | queue.dispatch(); |
| Wayne Roberts |
1:416ed16806fc | 322 | } |
| Wayne Roberts |
1:416ed16806fc | 323 |