-
Notifications
You must be signed in to change notification settings - Fork 232
/
at.c
753 lines (684 loc) · 15.1 KB
/
at.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// -*- Mode: C; c-basic-offset: 8; -*-
//
// Copyright (c) 2011 Michael Smith, All Rights Reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// o Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// o Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//
///
/// @file at.c
///
/// A simple AT command parser.
///
#include "radio.h"
#include "tdm.h"
#include "flash_layout.h"
#include "at.h"
#include "board.h"
#ifdef INCLUDE_AES
#include "AES/aes.h"
#endif
// canary data for ram wrap. It is in at.c as the compiler
// assigns addresses in alphabetial order and we want this at a low
// address
__pdata uint8_t pdata_canary = 0x41;
// AT command buffer
__xdata char at_cmd[AT_CMD_MAXLEN + 1];
__pdata uint8_t at_cmd_len;
// mode flags
bool at_mode_active; ///< if true, incoming bytes are for AT command
bool at_cmd_ready; ///< if true, at_cmd / at_cmd_len contain valid data
// test bits
__pdata uint8_t at_testmode; ///< test modes enabled (AT_TEST_*)
// command handlers
static void at_ok(void);
static void at_error(void);
static void at_i(void);
static void at_s(void);
static void at_ampersand(void);
static void at_p(void);
static void at_plus(void);
static void at_m(void);
#pragma save
#pragma nooverlay
void
at_input(register uint8_t c)
{
// AT mode is active and waiting for a command
switch (c) {
// CR - submits command for processing
case '\r':
putchar('\n');
at_cmd[at_cmd_len] = 0;
at_cmd_ready = true;
break;
// backspace - delete a character
// delete - delete a character
case '\b':
case '\x7f':
if (at_cmd_len > 0) {
putchar('\b');
putchar(' ');
putchar('\b');
at_cmd_len--;
}
break;
// character - add to buffer if valid
default:
if (at_cmd_len < AT_CMD_MAXLEN) {
if (isprint(c)) {
c = toupper(c);
at_cmd[at_cmd_len++] = c;
putchar(c);
}
break;
}
// If the AT command buffer overflows we abandon
// AT mode and return to passthrough mode; this is
// to minimise the risk of locking up on reception
// of an accidental escape sequence.
at_mode_active = 0;
at_cmd_len = 0;
break;
}
}
#pragma restore
// +++ detector state machine
//
// states:
//
// wait_for_idle: -> wait_for_plus1, count = 0 after 1s
//
// wait_for_plus: -> wait_for_idle if char != +
// -> wait_for_plus, count++ if count < 3
// wait_for_enable: -> enabled after 1s
// -> wait_for_idle if any char
//
#define ATP_WAIT_FOR_IDLE 0
#define ATP_WAIT_FOR_PLUS1 1
#define ATP_WAIT_FOR_PLUS2 2
#define ATP_WAIT_FOR_PLUS3 3
#define ATP_WAIT_FOR_ENABLE 4
#define ATP_COUNT_1S 100 // 100 ticks of the 100Hz timer
static __pdata uint8_t at_plus_state;
static __pdata uint8_t at_plus_counter = ATP_COUNT_1S;
#pragma save
#pragma nooverlay
void
at_plus_detector(register uint8_t c)
{
// If we get a character that's not '+', unconditionally
// reset the state machine to wait-for-idle; this will
// restart the 1S timer.
//
if (c != (uint8_t)'+')
at_plus_state = ATP_WAIT_FOR_IDLE;
// We got a plus; handle it based on our current state.
//
switch (at_plus_state) {
case ATP_WAIT_FOR_PLUS1:
case ATP_WAIT_FOR_PLUS2:
at_plus_state++;
break;
case ATP_WAIT_FOR_PLUS3:
at_plus_state = ATP_WAIT_FOR_ENABLE;
at_plus_counter = ATP_COUNT_1S;
break;
default:
at_plus_state = ATP_WAIT_FOR_IDLE;
// FALLTHROUGH
case ATP_WAIT_FOR_IDLE:
case ATP_WAIT_FOR_ENABLE:
at_plus_counter = ATP_COUNT_1S;
break;
}
}
#pragma restore
#pragma save
#pragma nooverlay
void
at_timer(void)
{
// if the counter is running
if (at_plus_counter > 0) {
// if it reaches zero, the timeout has expired
if (--at_plus_counter == 0) {
// make the relevant state change
switch (at_plus_state) {
case ATP_WAIT_FOR_IDLE:
at_plus_state = ATP_WAIT_FOR_PLUS1;
break;
case ATP_WAIT_FOR_ENABLE:
at_mode_active = true;
at_plus_state = ATP_WAIT_FOR_IDLE;
// stuff an empty 'AT' command to get the OK prompt
at_cmd[0] = 'A';
at_cmd[1] = 'T';
at_cmd[2] = '\0';
at_cmd_len = 2;
at_cmd_ready = true;
break;
default:
// should never happen, but otherwise harmless
break;
}
}
}
}
#pragma restore
void
at_command(void)
{
// require a command with the AT prefix
if (at_cmd_ready) {
if ((at_cmd_len >= 2) && (at_cmd[0] == 'R') && (at_cmd[1] == 'T')) {
// remote AT command - send it to the tdm
// system to send to the remote radio
tdm_remote_at();
at_cmd_len = 0;
at_cmd_ready = false;
return;
}
if ((at_cmd_len >= 2) && (at_cmd[0] == 'A') && (at_cmd[1] == 'T')) {
// look at the next byte to determine what to do
switch (at_cmd[2]) {
case '\0': // no command -> OK
at_ok();
break;
case '&':
at_ampersand();
break;
case '+':
at_plus();
break;
case 'I':
at_i();
break;
case 'M':
at_m();
break;
case 'P':
at_p();
break;
case 'O': // O -> go online (exit command mode)
at_plus_counter = ATP_COUNT_1S;
at_mode_active = 0;
break;
case 'S':
at_s();
break;
case 'Z':
// generate a software reset
RSTSRC |= (1 << 4);
for (;;)
;
default:
at_error();
}
}
// unlock the command buffer
at_cmd_len = 0;
at_cmd_ready = false;
}
}
static void
at_ok(void)
{
printf("%s\n", "OK");
}
static void
at_error(void)
{
printf("%s\n", "ERROR");
}
__xdata uint8_t idx;
__xdata uint32_t at_num;
/*
parse a number at idx putting the result in at_num
*/
static void
at_parse_number() __reentrant
{
register uint8_t c;
at_num = 0;
for (;;) {
c = at_cmd[idx];
if (!isdigit(c))
break;
at_num = (at_num * 10) + (c - '0');
idx++;
}
}
static void print_ID_vals(char param, uint8_t end,
const char *__code (*name_param)(__data enum ParamID param),
param_t (*get_param)(__data enum ParamID param)
)
{
register enum ParamID id;
// convenient way of showing all parameters
for (id = 0; id < end; id++) {
printf("%c%u:%s=%lu\n",
param,
(unsigned)id,
name_param(id),
(unsigned long)get_param(id));
}
}
static void
at_i(void)
{
switch (at_cmd[3]) {
case '\0':
case '0':
printf("%s\n", g_banner_string);
return;
case '1':
printf("%s\n", g_version_string);
return;
case '2':
printf("%u\n", BOARD_ID);
break;
case '3':
printf("%u\n", g_board_frequency);
break;
case '4':
printf("%u\n", g_board_bl_version);
return;
case '5':
print_ID_vals('S', PARAM_MAX, param_name, param_get);
return;
case '6':
tdm_report_timing();
return;
case '7':
tdm_show_rssi();
return;
default:
at_error();
return;
}
}
static void
at_s(void)
{
__pdata uint8_t sreg;
// get the register number first
idx = 3;
at_parse_number();
sreg = at_num;
// validate the selected sreg
if (sreg >= PARAM_MAX) {
at_error();
return;
}
switch (at_cmd[idx]) {
case '?':
at_num = param_get(sreg);
printf("%lu\n", at_num);
return;
case '=':
if (sreg > 0) {
idx++;
at_parse_number();
if (param_set(sreg, at_num)) {
at_ok();
return;
}
}
break;
}
at_error();
}
static void
at_ampersand(void)
{
switch (at_cmd[3]) {
case 'F':
param_default();
at_ok();
break;
case 'W':
param_save();
at_ok();
break;
case 'U':
if (!strcmp(at_cmd + 4, "PDATE")) {
// Erase Flash signature forcing it into reprogram mode next reset
FLKEY = 0xa5;
FLKEY = 0xf1;
PSCTL = 0x03; // set PSWE and PSEE
*(uint8_t __xdata *)FLASH_SIGNATURE_BYTES = 0xff; // do the page erase
PSCTL = 0x00; // disable PSWE/PSEE
// Reset the device using sofware reset
RSTSRC |= 0x10;
for (;;)
;
}
at_error();
break;
case 'P':
tdm_change_phase();
break;
case 'T':
// enable test modes
if (!strcmp(at_cmd + 4, "")) {
// disable all tests
at_testmode = 0;
} else if (!strcmp(at_cmd + 4, "=RSSI")) {
// display RSSI stats
at_testmode ^= AT_TEST_RSSI;
} else if (!strcmp(at_cmd + 4, "=TDM")) {
// display TDM debug
at_testmode ^= AT_TEST_TDM;
} else {
at_error();
}
break;
#ifdef INCLUDE_AES
case 'E':
switch (at_cmd[4]) {
case '?':
print_encryption_key();
return;
case '=':
if (param_set_encryption_key((__xdata unsigned char *)&at_cmd[5])) {
at_ok();
return;
}
break;
}
#endif // INCLUDE_AES
default:
at_error();
break;
}
}
static void
at_p (void)
{
#if PIN_MAX > 0
__pdata uint8_t pinId;
if(at_cmd[3] == 'P')
{
for (pinId = 0; pinId < PIN_MAX; pinId++)
{
printf("Pin:%u ", pinId);
if (pins_user_get_io(pinId))
printf("Output ");
else
printf("Input ");
printf("Val: %u\n",pins_user_get_value(pinId));
}
return;
}
else if(at_cmd[4] != '=' || !isdigit(at_cmd[5]))
{
at_error();
return;
}
pinId = at_cmd[5] - '0';
switch (at_cmd[3]) {
// Set pin to output, turn mirroring off pulling pin to ground
case 'O':
pins_user_set_io(pinId, PIN_OUTPUT);
break;
// Need to figure out how to set pins to Input/Output
case 'I':
pins_user_set_io(pinId, PIN_INPUT);
break;
case 'R':
if(pins_user_get_io(pinId) == PIN_INPUT)
printf("val:%u\n", pins_user_get_adc(pinId));
else
at_error();
return;
break;
case 'C':
if(!isdigit(at_cmd[7]) || !pins_user_set_value(pinId, (at_cmd[7]-'0')?1:0))
{
at_error();
return;
}
break;
default:
at_error();
return;
}
at_ok();
#else
at_error();
#endif
}
static void
at_plus(void)
{
__pdata uint8_t creg;
// get the register number first
idx = 4;
at_parse_number();
creg = at_num;
switch (at_cmd[3])
{
#if defined BOARD_rfd900a || defined BOARD_rfd900p
case 'P': // AT+P=x set power level pwm to x immediately
if (at_cmd[4] != '=')
{
break;
}
idx = 5;
at_parse_number();
PCA0CPH0 = at_num & 0xFF;
radio_set_diversity(DIVERSITY_DISABLED);
at_ok();
return;
case 'C': // AT+Cx=y write calibration value
switch (at_cmd[idx])
{
case '?':
at_num = calibration_get(creg);
printf("%lu\n",at_num);
return;
case '=':
idx++;
at_parse_number();
if (calibration_set(creg, at_num&0xFF))
{
at_ok();
} else {
at_error();
}
return;
}
break;
case 'F': // AT+Fx? get calibration value
switch (at_cmd[idx])
{
case '?':
at_num = calibration_force_get(creg);
printf("%lu\n",at_num);
return;
}
break;
case 'L': // AT+L lock bootloader area if all calibrations written
if (calibration_lock())
{
at_ok();
} else {
at_error();
}
return;
#endif //BOARD_rfd900a / BOARD_rfd900p
#ifdef RFD900_DIVERSITY
case 'A':
if (at_cmd[4] != '=')
{
break;
}
idx = 5;
at_parse_number();
if (at_num == 1) {
radio_set_diversity(DIVERSITY_ANT1);
}
else {
radio_set_diversity(DIVERSITY_ANT2);
}
at_ok();
return;
#endif // RFD900_DIVERSITY
}
at_error();
}
static void
at_m(void)
{
#ifdef BOARD_mro900
//mRo Options
__pdata uint8_t mRo_num = 0;
__pdata uint8_t osc_val = 0;
printf(" mRo command received \n");
if (at_cmd[4] == '=')
{
idx = 5;
at_parse_number();
mRo_num = at_num;
// printf("Received value: %u \n", mRo_num);
}
switch (at_cmd[3])
{
case 'H': //ATMH , HELP
printf("mRo AT commands help \n");
// printf("ATMO=X ->Set oscillator capacitance. \n");
// printf("ATMO? ->Get Cal stored. \n");
// printf("ATMOS ->Store Cal in flash.\n");
// printf("ATMOF ->Force get Cal \n");
// printf("ATMOL ->Lock Bootloader with calibration \n");
printf("ATMD=X -> sets antenna diversity behavior \n");
printf(" 0 = diversity disabled, using antenna 1 \n");
printf(" 1 = diversity enabled \n");
printf(" 2 = diversity disabled, using antenna 2 \n");
// printf("ATML=X -> sets LNA \n");
// printf(" 0 = LNA disabled \n");
// printf(" 1 = LNA enabled \n");
printf("ATMR ->Reboot \n");
break;
case 'C': //ATMC
radio_set_output_clock_freq();
break;
case 'O': //ATMO
switch (at_cmd[4])
{
case '=': //ATMO=??
radio_set_oscillator_capacitance(mRo_num);
printf("OSC Value set to 0x%x | %d \n", mRo_num, mRo_num);
return;
case '?': //ATMO?
osc_val = calibration_get();
printf(" OSC value 0x%x | %d\n", osc_val, osc_val);
if ( osc_val == 0xFF || osc_val == 0) {
printf(" Default value 0x%x | %d\n", EZRADIOPRO_OSC_CAP_VALUE, EZRADIOPRO_OSC_CAP_VALUE);
}
return;
case 'S': //ATMOS??
// printf(" Capacitance value 0x%x | %d\n", radio_get_oscillator_capacitance(), radio_get_oscillator_capacitance());
if (calibration_set(radio_get_oscillator_capacitance()))
{
at_ok();
} else {
printf("Calibration seems to be locked! \n");
at_error();
}
return;
case 'F': //ATMOF get calibration value
osc_val = calibration_force_get();
printf("%d\n", osc_val);
return;
case 'L': // ATMOL lock bootloader area if calibration written
if (calibration_lock())
{
at_ok();
} else {
at_error();
}
return;
default:
at_error();
return;
}
break;
case 'D': //ATMD=?
if (at_cmd[4] != '=' )
{
at_error();
return;
}
switch (mRo_num)
{
case 0:
radio_set_diversity(DIVERSITY_DISABLED); //Diversity Disabled
printf("mRo Diversity Disabled (Using Ant1) \n");
return;
case 1:
radio_set_diversity(DIVERSITY_ENABLED); //Diversity Enabled
printf("mRo Diversity Enabled \n");
return;
case 2:
radio_set_diversity(DIVERSITY_ANT2); //Diversity
printf("mRo Diversity Disabled (Using Ant2) \n");
return;
default:
at_error();
return;
}
break;
case 'L': //ATML=?
if (at_cmd[4] != '=')
{
at_error();
return;
}
switch (mRo_num)
{
case 0:
LNA_ENABLE = LNA_OFF;
printf("mRo LNA disabled \n");
return;
case 1:
LNA_ENABLE = LNA_ON;
printf("mRo LNA enabled \n");
return;
default:
at_error();
return;
}
case 'R': //Software reset.
printf("Rebooting \n");
RSTSRC |= 0x10;
while (1)
{}
break;
default:
at_error();
return;
}
#endif
}