standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Committer:
ftagius
Date:
Wed Jul 29 12:14:42 2015 +0000
Revision:
32:a2472bbe7c92
Parent:
31:2c813f321db7
cleanup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ftagius 29:0ea07cc7124b 1 #include "mbed.h"
ftagius 29:0ea07cc7124b 2 #include "sx1276-hal.h"
ftagius 29:0ea07cc7124b 3 #include "main.h"
ftagius 29:0ea07cc7124b 4 #include "debug.h"
ftagius 31:2c813f321db7 5 //radfta #include "vt100.h"
ftagius 29:0ea07cc7124b 6 #include "serial_api.h"
ftagius 29:0ea07cc7124b 7
ftagius 29:0ea07cc7124b 8 void console_chat()
ftagius 29:0ea07cc7124b 9 {
ftagius 29:0ea07cc7124b 10 //char pcbuf[64
ftagius 29:0ea07cc7124b 11 if (ackRcvd == false)
ftagius 29:0ea07cc7124b 12 {
ftagius 29:0ea07cc7124b 13 check_rx_chat();
ftagius 29:0ea07cc7124b 14 return;
ftagius 29:0ea07cc7124b 15 }
ftagius 29:0ea07cc7124b 16 else
ftagius 29:0ea07cc7124b 17 {
ftagius 29:0ea07cc7124b 18 printf("\r\n");
ftagius 29:0ea07cc7124b 19 printf("-chat-> ");
ftagius 29:0ea07cc7124b 20 fflush(stdout);
ftagius 29:0ea07cc7124b 21 }
ftagius 29:0ea07cc7124b 22
ftagius 29:0ea07cc7124b 23 int i, len = get_kbd_str(pcbuf, PCBUF_SIZE);
ftagius 29:0ea07cc7124b 24 if (len < 0) {
ftagius 29:0ea07cc7124b 25 // ctrl.cls();
ftagius 29:0ea07cc7124b 26 printf("Console chat ended\r\n");
ftagius 29:0ea07cc7124b 27 print_help();
ftagius 29:0ea07cc7124b 28 printf("> ");
ftagius 29:0ea07cc7124b 29 app = APP_CONSOLE;
ftagius 29:0ea07cc7124b 30 return;
ftagius 29:0ea07cc7124b 31 }
ftagius 29:0ea07cc7124b 32 else
ftagius 29:0ea07cc7124b 33 {
ftagius 29:0ea07cc7124b 34
ftagius 29:0ea07cc7124b 35
ftagius 29:0ea07cc7124b 36 if (len > 0)
ftagius 29:0ea07cc7124b 37 {
ftagius 29:0ea07cc7124b 38 for (i = 0; i < len; i++)
ftagius 29:0ea07cc7124b 39 BufferTx[i] = pcbuf[i];
ftagius 29:0ea07cc7124b 40 BufferTx[len] = '\0';
ftagius 29:0ea07cc7124b 41
ftagius 29:0ea07cc7124b 42 // debug( "\r\nchat send %s, len is %d, state is %d \r\n", BufferTx, len, State );
ftagius 29:0ea07cc7124b 43 // wait_ms(20);
ftagius 29:0ea07cc7124b 44
ftagius 29:0ea07cc7124b 45
ftagius 29:0ea07cc7124b 46 //State = LOWPOWER;
ftagius 29:0ea07cc7124b 47 //Radio.Sleep( );
ftagius 29:0ea07cc7124b 48 ackRcvd = false;
ftagius 29:0ea07cc7124b 49 txLen = len;
ftagius 29:0ea07cc7124b 50 Radio.Send( BufferTx, txLen );
ftagius 29:0ea07cc7124b 51 }
ftagius 29:0ea07cc7124b 52
ftagius 29:0ea07cc7124b 53
ftagius 29:0ea07cc7124b 54
ftagius 29:0ea07cc7124b 55 }
ftagius 29:0ea07cc7124b 56 }
ftagius 29:0ea07cc7124b 57
ftagius 29:0ea07cc7124b 58 void print_help()
ftagius 29:0ea07cc7124b 59 {
ftagius 31:2c813f321db7 60 //radfta ctrl.cls();
ftagius 29:0ea07cc7124b 61 printf(" Radio Utilities \r\n");
ftagius 29:0ea07cc7124b 62 printf("-----------------------------------------\r\n");
ftagius 29:0ea07cc7124b 63 print_status();
ftagius 29:0ea07cc7124b 64 printf("M set mode to Master\r\n");
ftagius 29:0ea07cc7124b 65 printf("S set mode to Slave\r\n");
ftagius 29:0ea07cc7124b 66 //printf("B set mode to Bi-directional\r\n");
ftagius 29:0ea07cc7124b 67 printf("P Start ping pong application\r\n");
ftagius 29:0ea07cc7124b 68 printf("C Start console chat application\r\n");
ftagius 29:0ea07cc7124b 69 printf("H Start hello exchange\r\n");
ftagius 29:0ea07cc7124b 70 printf("G Start GPS exchange\r\n");
ftagius 29:0ea07cc7124b 71
ftagius 29:0ea07cc7124b 72
ftagius 29:0ea07cc7124b 73 //printf("i radio_init\r\n");
ftagius 29:0ea07cc7124b 74 //printf("h hw_reset\r\n");
ftagius 29:0ea07cc7124b 75 //printf("tx[%%d] transmit\r\n");
ftagius 29:0ea07cc7124b 76 //printf("rx receive\r\n");
ftagius 29:0ea07cc7124b 77 //printf("C toggle crcOn\r\n");
ftagius 29:0ea07cc7124b 78 //printf("R read all registers\r\n");
ftagius 29:0ea07cc7124b 79 printf("op[%%d] get/set output power. op14 -> 14dBm...\r\n");
ftagius 29:0ea07cc7124b 80 //printf("d[0-5] change DIO pin assignment\r\n");
ftagius 29:0ea07cc7124b 81 printf("frf[%%f} get/set operating frequency (MHz). frf915 -> 915MHz, fr413 ->413MHz....\r\n");
ftagius 29:0ea07cc7124b 82 //printf("pl[%%d] LORA get/set RegPayloadLength\r\n");
ftagius 29:0ea07cc7124b 83 printf("cr[1234] LORA set coding rate. cr1 -> 4/5, cr2 -> 4/6, cr3 -> 4/7 cr4 ->4/8 \r\n");
ftagius 29:0ea07cc7124b 84 printf("bw[%%d] LORA get/set bandwidth. bw0 -> 125KHz, bw1 -> 250KHz, bw2 -> 500KHz \r\n");
ftagius 29:0ea07cc7124b 85 printf("sf[%%d] LORA get/set spreading factor. sf7-sf12\r\n");
ftagius 29:0ea07cc7124b 86 //printf("T LORA toggle TxContinuousMode\r\n");
ftagius 29:0ea07cc7124b 87 //printf("hp[%%d] LORA get/set hop period\r\n");
ftagius 29:0ea07cc7124b 88 //printf("hm LORA toggle explicit/explicit header mode\r\n");
ftagius 29:0ea07cc7124b 89
ftagius 29:0ea07cc7124b 90 printf("? Display help\r\n");
ftagius 29:0ea07cc7124b 91 printf(". Display configuration\r\n");
ftagius 29:0ea07cc7124b 92 }
ftagius 29:0ea07cc7124b 93
ftagius 29:0ea07cc7124b 94 void console()
ftagius 29:0ea07cc7124b 95 {
ftagius 29:0ea07cc7124b 96 int len;
ftagius 29:0ea07cc7124b 97 // char pcbuf[64];
ftagius 29:0ea07cc7124b 98
ftagius 29:0ea07cc7124b 99
ftagius 29:0ea07cc7124b 100 len = get_kbd_str(pcbuf, PCBUF_SIZE);
ftagius 29:0ea07cc7124b 101
ftagius 29:0ea07cc7124b 102 if (len < 0) {
ftagius 29:0ea07cc7124b 103 // ctrl.cls();
ftagius 29:0ea07cc7124b 104 Radio.Sleep( );
ftagius 29:0ea07cc7124b 105 debug("Console configuration ended\r\n");
ftagius 29:0ea07cc7124b 106 app = APP_CONSOLE;
ftagius 29:0ea07cc7124b 107 print_help();
ftagius 29:0ea07cc7124b 108 printf("> ");
ftagius 29:0ea07cc7124b 109 fflush(stdout);
ftagius 29:0ea07cc7124b 110 return;
ftagius 29:0ea07cc7124b 111 }
ftagius 29:0ea07cc7124b 112
ftagius 29:0ea07cc7124b 113 printf("\r\n");
ftagius 29:0ea07cc7124b 114 if (len == 1) {
ftagius 29:0ea07cc7124b 115 switch (pcbuf[0]) {
ftagius 29:0ea07cc7124b 116 case '?':
ftagius 29:0ea07cc7124b 117 print_help();
ftagius 29:0ea07cc7124b 118 break;
ftagius 29:0ea07cc7124b 119 case '.':
ftagius 29:0ea07cc7124b 120 print_status();
ftagius 29:0ea07cc7124b 121 break;
ftagius 29:0ea07cc7124b 122 case 'M':
ftagius 29:0ea07cc7124b 123 AlwaysMaster=true;
ftagius 29:0ea07cc7124b 124 isMaster = true;
ftagius 29:0ea07cc7124b 125 AlwaysSlave=false;
ftagius 29:0ea07cc7124b 126 break;
ftagius 29:0ea07cc7124b 127 case 'S':
ftagius 29:0ea07cc7124b 128 AlwaysSlave=true;
ftagius 29:0ea07cc7124b 129 isMaster = false;
ftagius 29:0ea07cc7124b 130 AlwaysMaster=false;
ftagius 29:0ea07cc7124b 131 break;
ftagius 29:0ea07cc7124b 132 case 'B':
ftagius 29:0ea07cc7124b 133 AlwaysSlave=false;
ftagius 29:0ea07cc7124b 134 isMaster = true;
ftagius 29:0ea07cc7124b 135 AlwaysMaster=false;
ftagius 29:0ea07cc7124b 136 break;
ftagius 29:0ea07cc7124b 137 case 'G':
ftagius 31:2c813f321db7 138 // radfta ctrl.cls();
ftagius 29:0ea07cc7124b 139 debug("Starting GPS exchange\r\n");
ftagius 29:0ea07cc7124b 140 gpsEnabled = true;
ftagius 29:0ea07cc7124b 141 fflush(stdout);
ftagius 29:0ea07cc7124b 142 app = APP_HELLO;
ftagius 31:2c813f321db7 143 // radfta gpsd.l_latitude = gpsd.l_longitude = gpsd.r_latitude = gpsd.r_longitude = 0;
ftagius 29:0ea07cc7124b 144 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 145 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 146 break;
ftagius 29:0ea07cc7124b 147 case 'P':
ftagius 31:2c813f321db7 148 // radfta ctrl.cls();
ftagius 29:0ea07cc7124b 149 debug("Starting ping pong app\r\n");
ftagius 29:0ea07cc7124b 150 fflush(stdout);
ftagius 29:0ea07cc7124b 151 app = APP_PING;
ftagius 31:2c813f321db7 152 // radfta gpsd.l_latitude = gpsd.l_longitude = gpsd.r_latitude = gpsd.r_longitude = 0;
ftagius 29:0ea07cc7124b 153 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 154 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 155 break;
ftagius 29:0ea07cc7124b 156 case 'H':
ftagius 31:2c813f321db7 157 // radfta ctrl.cls();
ftagius 29:0ea07cc7124b 158 debug("Starting hello exchange\r\n");
ftagius 29:0ea07cc7124b 159 gpsEnabled = false;
ftagius 29:0ea07cc7124b 160 fflush(stdout);
ftagius 29:0ea07cc7124b 161 app = APP_HELLO;
ftagius 29:0ea07cc7124b 162 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 163 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 164 break;
ftagius 29:0ea07cc7124b 165 case 'C':
ftagius 31:2c813f321db7 166 // radfta ctrl.cls();
ftagius 29:0ea07cc7124b 167 printf("Starting chat app\r\n");
ftagius 29:0ea07cc7124b 168 //printf("-chat-> ");
ftagius 29:0ea07cc7124b 169 fflush(stdout);
ftagius 29:0ea07cc7124b 170 ackRcvd = true;
ftagius 29:0ea07cc7124b 171 app = APP_CHAT;
ftagius 29:0ea07cc7124b 172 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 173 //radf Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 174 break;
ftagius 29:0ea07cc7124b 175 default:
ftagius 32:a2472bbe7c92 176 debug("Unsupported command received\r\n");
ftagius 29:0ea07cc7124b 177 break;
ftagius 29:0ea07cc7124b 178 }
ftagius 29:0ea07cc7124b 179 } else if (pcbuf[0] == 'f' && pcbuf[1] == 'r' && pcbuf[2] == 'f') {
ftagius 29:0ea07cc7124b 180 if (pcbuf[3] >= '0' && pcbuf[3] <= '9') {
ftagius 29:0ea07cc7124b 181 float MHz;
ftagius 29:0ea07cc7124b 182 sscanf(pcbuf+3, "%f", &MHz);
ftagius 29:0ea07cc7124b 183 MHz *= 1000000;
ftagius 29:0ea07cc7124b 184 // printf("MHz:%f\r\n", MHz);
ftagius 29:0ea07cc7124b 185 Frequency = (MHz);
ftagius 29:0ea07cc7124b 186 }
ftagius 29:0ea07cc7124b 187 Radio.SetChannel( Frequency );
ftagius 29:0ea07cc7124b 188 configRxTx();
ftagius 29:0ea07cc7124b 189 printf("Channel: %.1f MHz\r\n", Radio.GetChannel());
ftagius 29:0ea07cc7124b 190
ftagius 29:0ea07cc7124b 191 } else if (pcbuf[0] == 'o' && pcbuf[1] == 'p') {
ftagius 29:0ea07cc7124b 192 if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
ftagius 29:0ea07cc7124b 193 sscanf(pcbuf+2, "%d", &TxPower);
ftagius 29:0ea07cc7124b 194 //printf("setting tx power to %d\r\n", TxPower);
ftagius 29:0ea07cc7124b 195 configRxTx();
ftagius 29:0ea07cc7124b 196
ftagius 29:0ea07cc7124b 197 }
ftagius 29:0ea07cc7124b 198 print_power(); printf("\r\n");
ftagius 29:0ea07cc7124b 199 //printf("OutputPower: %d\r\n", ( uint8_t )( ( uint16_t )( Radio.Read(REG_PACONFIG) - 1 ) & 0x0F ));
ftagius 29:0ea07cc7124b 200
ftagius 29:0ea07cc7124b 201 } else if (pcbuf[0] == 'b' && pcbuf[1] == 'w') {
ftagius 29:0ea07cc7124b 202
ftagius 29:0ea07cc7124b 203 if (pcbuf[2] >= '0' && pcbuf[2] <= '2') {
ftagius 29:0ea07cc7124b 204 Radio.Standby();
ftagius 29:0ea07cc7124b 205 sscanf(&pcbuf[2], "%d", &Bandwidth);
ftagius 29:0ea07cc7124b 206 switch (Bandwidth) {
ftagius 29:0ea07cc7124b 207 case 0:
ftagius 29:0ea07cc7124b 208 printf("setting bandwidth to 125KHz\r\n");
ftagius 29:0ea07cc7124b 209 break;
ftagius 29:0ea07cc7124b 210 case 1:
ftagius 29:0ea07cc7124b 211 printf("setting bandwidth to 250KHz\r\n");
ftagius 29:0ea07cc7124b 212 break;
ftagius 29:0ea07cc7124b 213 case 2:
ftagius 29:0ea07cc7124b 214 printf("setting bandwidth to 500KHz\r\n");
ftagius 29:0ea07cc7124b 215 break;
ftagius 29:0ea07cc7124b 216 }
ftagius 29:0ea07cc7124b 217 configRxTx();
ftagius 29:0ea07cc7124b 218 }
ftagius 29:0ea07cc7124b 219 print_bandwidth(); printf("\r\n");
ftagius 29:0ea07cc7124b 220
ftagius 29:0ea07cc7124b 221 } else if (pcbuf[0] == 's' && pcbuf[1] == 'f' ) {
ftagius 29:0ea07cc7124b 222 if (pcbuf[2] >= '0' && pcbuf[2] <= '9') {
ftagius 29:0ea07cc7124b 223 sscanf(pcbuf+2, "%d", &SpreadingFactor);
ftagius 29:0ea07cc7124b 224 configRxTx();
ftagius 29:0ea07cc7124b 225 }
ftagius 29:0ea07cc7124b 226 printf("Spreading Factor: %d\r\n",SpreadingFactor);
ftagius 29:0ea07cc7124b 227
ftagius 29:0ea07cc7124b 228 } else if (pcbuf[0] == 'c' && pcbuf[1] == 'r' ) {
ftagius 29:0ea07cc7124b 229 if (pcbuf[2] >= '0' && pcbuf[2] <= '9')
ftagius 29:0ea07cc7124b 230 {
ftagius 29:0ea07cc7124b 231 sscanf(pcbuf+2, "%d", &CodingRate);
ftagius 29:0ea07cc7124b 232 configRxTx();
ftagius 29:0ea07cc7124b 233 }
ftagius 29:0ea07cc7124b 234
ftagius 29:0ea07cc7124b 235 print_cr();printf("\r\n");
ftagius 29:0ea07cc7124b 236
ftagius 32:a2472bbe7c92 237 }
ftagius 32:a2472bbe7c92 238 #if 0
ftagius 32:a2472bbe7c92 239 else
ftagius 29:0ea07cc7124b 240 debug("Unsupported command\r\n");
ftagius 32:a2472bbe7c92 241 #endif
ftagius 29:0ea07cc7124b 242
ftagius 29:0ea07cc7124b 243 if (app == APP_CONSOLE)
ftagius 29:0ea07cc7124b 244 {
ftagius 29:0ea07cc7124b 245 printf("> ");
ftagius 29:0ea07cc7124b 246 fflush(stdout);
ftagius 29:0ea07cc7124b 247 }
ftagius 29:0ea07cc7124b 248
ftagius 29:0ea07cc7124b 249 }
ftagius 29:0ea07cc7124b 250
ftagius 29:0ea07cc7124b 251 void check_rx_chat()
ftagius 29:0ea07cc7124b 252 {
ftagius 29:0ea07cc7124b 253 //wait(1);
ftagius 29:0ea07cc7124b 254 switch( State )
ftagius 29:0ea07cc7124b 255 {
ftagius 29:0ea07cc7124b 256 case RX:
ftagius 29:0ea07cc7124b 257
ftagius 29:0ea07cc7124b 258 if( BufferSize > 0 )
ftagius 29:0ea07cc7124b 259 {
ftagius 29:0ea07cc7124b 260 if( strncmp( ( const char* )BufferRx, "ACK", 3 ) != 0 )
ftagius 29:0ea07cc7124b 261 {
ftagius 29:0ea07cc7124b 262 // received msg is not an ACK, so ACK it
ftagius 29:0ea07cc7124b 263 //red = !red;
ftagius 29:0ea07cc7124b 264 printf("\b\b\b\b\b\b\b\bchat received msg: %s \r\n", BufferRx );
ftagius 29:0ea07cc7124b 265 printf("-chat-> ");
ftagius 29:0ea07cc7124b 266 fflush(stdout);
ftagius 29:0ea07cc7124b 267 memcpy( BufferTx, "ACK", 3 );
ftagius 29:0ea07cc7124b 268 Radio.Send( BufferTx, 3 );
ftagius 29:0ea07cc7124b 269 }
ftagius 29:0ea07cc7124b 270 else
ftagius 29:0ea07cc7124b 271 {
ftagius 29:0ea07cc7124b 272 ackRcvd = true;
ftagius 29:0ea07cc7124b 273 // printf("got an ack\r\n");
ftagius 29:0ea07cc7124b 274 }
ftagius 29:0ea07cc7124b 275
ftagius 29:0ea07cc7124b 276 }
ftagius 29:0ea07cc7124b 277 Radio.Rx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 278 State = LOWPOWER;
ftagius 29:0ea07cc7124b 279 break;
ftagius 29:0ea07cc7124b 280 case TX:
ftagius 29:0ea07cc7124b 281 // red = !red;
ftagius 29:0ea07cc7124b 282 // debug("tx in chat mode\r\n");
ftagius 29:0ea07cc7124b 283 ackRcvd = false;
ftagius 29:0ea07cc7124b 284 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 285 State = LOWPOWER;
ftagius 29:0ea07cc7124b 286 break;
ftagius 29:0ea07cc7124b 287 case RX_TIMEOUT:
ftagius 29:0ea07cc7124b 288 static int retryCount=1;
ftagius 29:0ea07cc7124b 289 if (ackRcvd == false)
ftagius 29:0ea07cc7124b 290 {
ftagius 29:0ea07cc7124b 291 if (retryCount <= 4)
ftagius 29:0ea07cc7124b 292 {
ftagius 29:0ea07cc7124b 293 Radio.Send( BufferTx, txLen );
ftagius 29:0ea07cc7124b 294 ++retryCount;
ftagius 29:0ea07cc7124b 295 }
ftagius 29:0ea07cc7124b 296 else
ftagius 29:0ea07cc7124b 297 {
ftagius 29:0ea07cc7124b 298 retryCount = 1;
ftagius 29:0ea07cc7124b 299 printf("\r\nLast message (%s) was not acked", BufferTx);
ftagius 29:0ea07cc7124b 300 fflush(stdout);
ftagius 29:0ea07cc7124b 301 ackRcvd = true;
ftagius 29:0ea07cc7124b 302 }
ftagius 29:0ea07cc7124b 303 }
ftagius 29:0ea07cc7124b 304 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 305
ftagius 29:0ea07cc7124b 306 State = LOWPOWER;
ftagius 29:0ea07cc7124b 307 break;
ftagius 29:0ea07cc7124b 308 case RX_ERROR:
ftagius 29:0ea07cc7124b 309 debug("rx error in chat mode\r\n");
ftagius 29:0ea07cc7124b 310 State = LOWPOWER;
ftagius 29:0ea07cc7124b 311 break;
ftagius 29:0ea07cc7124b 312 case TX_TIMEOUT:
ftagius 29:0ea07cc7124b 313 //debug("tx timeout in chat mode\r\n");
ftagius 29:0ea07cc7124b 314 Radio.Rx( RX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 315 State = LOWPOWER;
ftagius 29:0ea07cc7124b 316 break;
ftagius 29:0ea07cc7124b 317 case LOWPOWER:
ftagius 29:0ea07cc7124b 318 wait_ms (5);
ftagius 29:0ea07cc7124b 319 break;
ftagius 29:0ea07cc7124b 320 default:
ftagius 29:0ea07cc7124b 321 // debug("state is set to low power\r\n");
ftagius 29:0ea07cc7124b 322 State = LOWPOWER;
ftagius 29:0ea07cc7124b 323 break;
ftagius 29:0ea07cc7124b 324 }
ftagius 29:0ea07cc7124b 325
ftagius 29:0ea07cc7124b 326 }