-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
1976 lines (1769 loc) · 76.8 KB
/
command.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: command.c
* Author: Peter Thornton
* Purpose: top-level handlers for uplink commands
* Created on: 14 October 2020
*
*/
#include "xc.h"
#include "autoimg.h"
#include "command.h"
#include "sd_card.h" // includes FError as a global mailbox for SD errors
#include "he100.h"
#include "hex_lut.h"
#include "sgp4.h"
#include "parse_tle.h"
#include "datetime.h"
#include "sfm.h"
#include "pdt.h"
#include "rtc.h"
#include "rtc_user.h"
#include "telemetry.h"
#include "arducam.h"
#include "arducam_user.h"
#include "spi.h"
#include "eps_bat.h"
#include "clock.h"
#include "adc.h"
#include "init.h"
#include "imtq.h"
#include "ants.h"
#include "position_attitude.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define B_SIZE 120 // buffer size for reading files
#define B_SIZE2 (B_SIZE*2)+1 // buffer to hold the ascii hex equivalent
// common arrays for downlink data and he100 response
char downlink_data[260];
char downlink_data2[260];
unsigned char he100_response[8];
// declare external global variable for initialization data, defined in Ramsat_flight_main.c
extern init_data_type init_data;
// declare external global variable for position and attitude data, defined in Ramsat_flight_main.c
extern position_attitude_type posatt;
// declare external global variable for the TLE data, defined (for now) in RamSat_flight_main.c
extern tle_t tle;
// declare external global variables for telemetry control, defined in RamSat_flight_main.c
extern telem_control_type telem_lev0; // Level 0 control data
extern telem_control_type telem_lev1; // Level 1 control data
extern telem_control_type telem_lev2; // Level 2 control data
// declare external global variable for watchdog timer interrupt
extern volatile int minute_elapsed;
// A No-Op command to verify 2-way radio connection
void CmdNoOp(void)
{
sprintf(downlink_data,"RamSat: No-Op command acknowledged.");
he100_transmit_packet(he100_response, downlink_data);
}
// Return the number of files on SD card
int CmdFileCount(void)
{
int err = 0;
unsigned nfiles;
MEDIA * sd_dat; // pointer to SD card data structure
// attempt to mount SD card
sd_dat = SD_mount();
if (!sd_dat)
{
sprintf(downlink_data,"RamSat: CmdFileCount->SD_mount Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
return FError;
}
// get the number of files on SD card
err = CountDIR(&nfiles);
if (!err)
{
// form data portion of downlink packet, pass to transmit routine
sprintf(downlink_data,"RamSat: Number of files = %u",nfiles);
he100_transmit_packet(he100_response, downlink_data);
}
else
{
// special message if error getting file count
sprintf(downlink_data,"RamSat: CmdFileCount->CountDIR Error: %d",err);
he100_transmit_packet(he100_response, downlink_data);
}
// unmount the SD card and free memory
SD_umount();
return err;
}
// List all files on SD card
int CmdFileList(void)
{
int err = 0;
MEDIA * sd_dat; // pointer to SD card data structure
MFILE * fp1; // pointer to file data structure
int i;
unsigned nfiles;
char name[9]; // file name
char ext[4]; // file extension
name[8]=0; // null termination
ext[3]=0; // null termination
// attempt to mount SD card
sd_dat = SD_mount();
if (!sd_dat)
{
sprintf(downlink_data,"RamSat: CmdFileList->SD_mount Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
return FError;
}
// allocate a MFILE structure on the heap
fp1 = (MFILE *) malloc( sizeof( MFILE));
if ( fp1 == NULL) // report an error
{
FError = FE_MALLOC_FAILED;
sprintf(downlink_data,"RamSat: CmdFileList->malloc Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
return FError;
}
// get the number of files on SD card
err = CountDIR(&nfiles);
if (!err)
{
// send a first packet that reports number of files
sprintf(downlink_data,"RamSat: Number of files = %u",nfiles);
he100_transmit_packet(he100_response, downlink_data);
// loop through files, transmit filename, size, and creation date
for (i=0 ; i<nfiles ; i++)
{
err = flistM(fp1, i);
if (!err)
{
// extract file info
memcpy(name,fp1->name,8);
memcpy(ext,fp1->name+8,3);
int year = (fp1->date>>9 & 0x7f) + 1980;
int mon = fp1->date>>5 & 0x0f;
int day = fp1->date & 0x1f;
int hrs = fp1->time>>11 & 0x1f;
int min = fp1->time>>5 & 0x3f;
int sec = (fp1->time & 0x1f) *2;
sprintf(downlink_data,"RamSat: File# %d: Name: %s.%s Size: %ld Date: %d-%d-%d Time: %d:%d:%d",
i,name,ext,fp1->size,year,mon,day,hrs,min,sec);
he100_transmit_packet(he100_response, downlink_data);
}
else
{
// special message if error getting file details
sprintf(downlink_data,"RamSat: CmdFileList->File# %d: Error: %d",i,err);
he100_transmit_packet(he100_response, downlink_data);
}
}
}
else
{
// special message if error getting file count
sprintf(downlink_data,"RamSat: CmdFileList->CountDIR Error: %d",err);
he100_transmit_packet(he100_response, downlink_data);
}
// unmount the SD card, free memory, and return
SD_umount();
free(fp1);
return err;
}
// Downlink the contents of a named file in a series of numbered packets
int CmdFileDump(char *paramstr)
{
int err = 0;
MEDIA * sd_dat; // pointer to SD card data structure
MFILE * fp1; // pointer to file data structure
int i;
char fname[13];
fname[12]=0;
unsigned char file_data[B_SIZE]; // the latest chunk of data read from file
unsigned char hex_data[B_SIZE2]; // the two-byte hex equivalent of the file data
unsigned bytes_read; // the number of bytes in most recent read
int npackets; // number of packets that will be sent
int packet_num; // current packet
int n_param; // number of parameters passed to command
int n_minutes_elapsed; // a counter for minutes elapsed during downlink
n_minutes_elapsed = 0;
// read parameter string, return if too few parameters
n_param = sscanf(paramstr,"%s",fname);
if (n_param != 1)
{
sprintf(downlink_data,"RamSat: CmdFileDump->wrong n_param: %d",n_param);
he100_transmit_packet(he100_response, downlink_data);
return 1;
}
// attempt to mount SD card
sd_dat = SD_mount();
if (!sd_dat)
{
sprintf(downlink_data,"RamSat: CmdFileDump->SD_mount Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
return FError;
}
// open specified file for reading
fp1 = fopenM(fname, "r");
if (!fp1)
{
sprintf(downlink_data,"RamSat: CmdFileDump->fopenM Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
SD_umount();
return FError;
}
// first calculate the expected number of packets, adding one for a short packet
npackets = fp1->size/B_SIZE;
if (fp1->size%B_SIZE) npackets++;
// send a message with filename, size, and number of packets to expect
sprintf(downlink_data,"RamSat: %s is open to read: Size=%ld: npackets=%d",fname,fp1->size,npackets);
he100_transmit_packet(he100_response, downlink_data);
// loop until the file is empty
packet_num = 0;
do
{
// check the watchdog timer interrupt, reset watchdog if needed
// required here because the image dump can take longer than the normal
// watchdog period (4 minutes)
if (minute_elapsed)
{
eps_reset_watchdog();
minute_elapsed = 0;
n_minutes_elapsed++;
}
// read a chunk of data from the file
bytes_read = freadM(file_data,B_SIZE,fp1);
// if any bytes were read, form a packet and send
if (bytes_read)
{
// loop through the bytes and convert to hex equivalent
for (i=0 ; i<bytes_read ; i++)
{
memcpy(&hex_data[i*2],&hex_lut[file_data[i]*2],2);
}
// null terminate the hex_data and form packet with header and data
hex_data[bytes_read*2]=0;
sprintf(downlink_data,"RS: %4d %s",packet_num,hex_data);
he100_transmit_packet(he100_response, downlink_data);
// 100 msec delay to prevent overrunning transmit buffer
TMR1 = 0;
while (TMR1 < 100*TMR1MSEC);
packet_num++;
}
} while (bytes_read == B_SIZE);
// check the packet number and write a final line to report completion
if (packet_num == npackets)
{
sprintf(downlink_data,"RamSat: %s dump complete, correct packet count: %d (%d minutes)",fname, packet_num, n_minutes_elapsed);
he100_transmit_packet(he100_response, downlink_data);
err = 0;
}
else
{
sprintf(downlink_data,"RamSat: %s dump complete, incorrect packet count: %d",fname, packet_num);
he100_transmit_packet(he100_response, downlink_data);
err = 1;
}
// unmount the SD card, free memory, and return
SD_umount();
free (fp1->buffer);
free(fp1);
return err;
}
// Downlink the contents of a named file in a series of numbered packets
// user specifies the first and last packet numbers in a range to downlink
int CmdFileDumpRange(char *paramstr)
{
int err = 0;
MEDIA * sd_dat; // pointer to SD card data structure
MFILE * fp1; // pointer to file data structure
int i;
char fname[13];
fname[12]=0;
int first_packet, last_packet; // user-specified range of packets (base-zero)
unsigned char file_data[B_SIZE]; // the latest chunk of data read from file
unsigned char hex_data[B_SIZE2]; // the two-byte hex equivalent of the file data
unsigned bytes_read; // the number of bytes in most recent read
int npackets; // number of packets that will be sent
int packet_num; // current packet
int n_param; // number of parameters passed to command
int n_minutes_elapsed; // a counter for minutes elapsed during downlink
n_minutes_elapsed = 0;
// read parameter string, return if too few parameters
n_param = sscanf(paramstr,"%s %d %d",fname, &first_packet, &last_packet);
if (n_param != 3)
{
sprintf(downlink_data,"RamSat: CmdFileDumpRange->wrong n_param: %d",n_param);
he100_transmit_packet(he100_response, downlink_data);
return 1;
}
// attempt to mount SD card
sd_dat = SD_mount();
if (!sd_dat)
{
sprintf(downlink_data,"RamSat: CmdFileDumpRange->SD_mount Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
return FError;
}
// open specified file for reading
fp1 = fopenM(fname, "r");
if (!fp1)
{
sprintf(downlink_data,"RamSat: CmdFileDumpRange->fopenM Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
SD_umount();
return FError;
}
// first calculate the expected number of packets, adding one for a short packet
npackets = fp1->size/B_SIZE;
if (fp1->size%B_SIZE) npackets++;
// send a message with filename, size, and number of packets to expect
sprintf(downlink_data,"RamSatDR: %s is open to read: Size=%ld: npackets=%d first_packet=%d last_packet=%d",
fname,fp1->size,npackets,first_packet,last_packet);
he100_transmit_packet(he100_response, downlink_data);
// loop until the file is empty
packet_num = 0;
do
{
// check the watchdog timer interrupt, reset watchdog if needed
// required here because the image dump can take longer than the normal
// watchdog period (4 minutes)
if (minute_elapsed)
{
eps_reset_watchdog();
minute_elapsed = 0;
n_minutes_elapsed++;
}
// read a chunk of data from the file
bytes_read = freadM(file_data,B_SIZE,fp1);
// if any bytes were read, form a packet and send
if (bytes_read && (packet_num >= first_packet) && (packet_num <= last_packet))
{
// loop through the bytes and convert to hex equivalent
for (i=0 ; i<bytes_read ; i++)
{
memcpy(&hex_data[i*2],&hex_lut[file_data[i]*2],2);
}
// null terminate the hex_data and form packet with header and data
hex_data[bytes_read*2]=0;
sprintf(downlink_data,"RS: %4d %s",packet_num,hex_data);
he100_transmit_packet(he100_response, downlink_data);
// 100 msec delay to prevent overrunning transmit buffer
TMR1 = 0;
while (TMR1 < 100*TMR1MSEC);
//for (k=0 ; k<10 ; k++)
//{
// TMR1=0;
// while(TMR1 < 1000*TMR1MSEC);
//}
}
if (packet_num == last_packet) break;
packet_num++;
} while (bytes_read == B_SIZE);
// check the packet number and write a final line to report completion
if (packet_num == last_packet)
{
sprintf(downlink_data,"RamSat: %s dump complete, correct last_packet: %d (%d minutes)",fname, packet_num, n_minutes_elapsed);
he100_transmit_packet(he100_response, downlink_data);
err = 0;
}
else
{
sprintf(downlink_data,"RamSat: %s dump complete, incorrect last_packet: %d",fname, packet_num);
he100_transmit_packet(he100_response, downlink_data);
err = 1;
}
// unmount the SD card, free memory, and return
SD_umount();
free (fp1->buffer);
free(fp1);
return err;
}
// Downlink a single numbered packet from a named file
int CmdFileDumpOnePacket(char *paramstr)
{
int err = 0;
MEDIA * sd_dat; // pointer to SD card data structure
MFILE * fp1; // pointer to file data structure
int i;
char fname[13];
fname[12]=0;
unsigned char file_data[B_SIZE]; // the latest chunk of data read from file
unsigned char hex_data[B_SIZE2]; // the two-byte hex equivalent of the file data
unsigned bytes_read; // the number of bytes in most recent read
int npackets; // number of packets that will be sent
int packet_num; // current packet
int req_pacnum; // the index for single packet requested to downlink
int n_param; // number of parameters passed to command
// read parameter string, return if too few parameters
n_param = sscanf(paramstr,"%s %d",fname, &req_pacnum);
if (n_param != 2)
{
sprintf(downlink_data,"RamSat: CmdFileDumpOnePacket->wrong n_param: %d",n_param);
he100_transmit_packet(he100_response, downlink_data);
return 1;
}
// attempt to mount SD card
sd_dat = SD_mount();
if (!sd_dat)
{
sprintf(downlink_data,"RamSat: CmdFileDumpOnePacket->SD_mount Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
return FError;
}
// open specified file for reading
fp1 = fopenM(fname, "r");
if (!fp1)
{
sprintf(downlink_data,"RamSat: CmdFileDumpOnePacket->fopenM Error: %d",FError);
he100_transmit_packet(he100_response, downlink_data);
SD_umount();
return FError;
}
// first calculate the expected number of packets, adding one for a short packet
npackets = fp1->size/B_SIZE;
if (fp1->size%B_SIZE) npackets++;
// send a message with filename, size, and number of packets to expect
sprintf(downlink_data,"RamSatSP: %s is open to read packet: Size=%ld: npackets=%u",fname,fp1->size,npackets);
he100_transmit_packet(he100_response, downlink_data);
packet_num = 0;
// loop until the file is empty
do
{
// read a chunk of data from the file
bytes_read = freadM(file_data,B_SIZE,fp1);
// if any bytes were read, form a packet and send
if (bytes_read && packet_num == req_pacnum)
{
// loop through the bytes and convert to hex equivalent
for (i=0 ; i<bytes_read ; i++)
{
memcpy(&hex_data[i*2],&hex_lut[file_data[i]*2],2);
}
// null terminate the hex_data
hex_data[bytes_read*2]=0;
sprintf(downlink_data,"RS: %4d %s", packet_num, hex_data);
he100_transmit_packet(he100_response, downlink_data);
break;
}
packet_num++;
} while (bytes_read == B_SIZE);
// check the packet number and write a final line to report completion
if (packet_num == req_pacnum)
{
sprintf(downlink_data,"RamSat: %s dump complete, packet %d found",fname, req_pacnum);
he100_transmit_packet(he100_response, downlink_data);
err = 0;
}
else
{
sprintf(downlink_data,"RamSat: %s dump complete, packet %d NOT found %d",fname, req_pacnum, packet_num);
he100_transmit_packet(he100_response, downlink_data);
err = 0;
}
// unmount the SD card, free memory, and return
SD_umount();
free (fp1->buffer);
free(fp1);
return err;
}
// Uplink a new Two-Line Element (TLE) from ground station, update current TLE
int CmdNewTLE(char *paramstr, int param_nbytes, int *good_tle)
{
int err = 0;
// check for the right amount of data in parameter
if (param_nbytes != 138)
{
// clear flag and downlink error message
*good_tle = 0;
sprintf(downlink_data,"RamSat: NewTLE ERROR - Received %d bytes, expecting 138", param_nbytes);
he100_transmit_packet(he100_response, downlink_data);
err = 1;
}
else
{
// good size, split out the two lines
char line1[70]; // TLE Line 1 (first 69 characters)
char line2[70]; // TLE Line 2 (second 69 characters)
memcpy(line1,paramstr,69);
memcpy(line2,paramstr+69,69);
// line1 and line2 need to be null-terminated strings
line1[69]=0;
line2[69]=0;
// call function to parse elements from TLE
err = parse_elements(line1, line2, &tle);
if (err)
{
// clear flag and downlink error message
*good_tle = 0;
sprintf(downlink_data,"RamSat: NewTLE ERROR - parse_elements() err = %d",err);
he100_transmit_packet(he100_response, downlink_data);
}
else
{
// TLE parsed without error
// set flag and downlink success message
*good_tle = 1;
sprintf(downlink_data,"RamSat: NewTLE successful, TLE updated");
he100_transmit_packet(he100_response, downlink_data);
}
}
return err;
}
// return the current date and time from RTC
int CmdGetDateTime(void)
{
char datetime[128];
int err = 0;
err = get_isodatetime(datetime);
sprintf(downlink_data,"RamSat: ISO DateTime = %s",datetime);
he100_transmit_packet(he100_response, downlink_data);
return err;
}
// set the date and time on RTC
int CmdSetDateTime(char* paramstr, int param_nbytes)
{
int err = 0;
int rtc_flags;
int nbytes;
unsigned char rtc_data[8];
unsigned char firstbyte;
char numstr[3];
int num1, num10;
// null-terminated string to hold two-digit int
numstr[2]=0;
// check for the right amount of data in parameter
if (param_nbytes != 24)
{
// clear flag and downlink error message
sprintf(downlink_data,"RamSat: Set Date/Time Error - Received %d bytes, expecting 24.",param_nbytes);
he100_transmit_packet(he100_response, downlink_data);
err = 1;
}
else if (paramstr[10] != 'T' || paramstr[22] != 'Z')
{
// basic format check fails
sprintf(downlink_data,"RamSat: Set Date/Time Error - incorrect format");
he100_transmit_packet(he100_response, downlink_data);
err = 1;
}
else
{
// good size and format, split out the elements needed to set RTC
// Note that the rtc_data values are decimal-coded binary, which is
// why there are funny-looking translations between the input date/time
// values and the values sent to RTC.
firstbyte = 0x00;
nbytes = 8;
// hundredths of seconds (00-99)
num10=paramstr[20]-48;
num1 =paramstr[21]-48;
rtc_data[0]=num10*16+num1;
// seconds (0-59)
num10=paramstr[17]-48;
num1 =paramstr[18]-48;
rtc_data[1]=num10*16+num1;
// minutes (0-59)
num10=paramstr[14]-48;
num1 =paramstr[15]-48;
rtc_data[2]=num10*16+num1;
// hour (00-23)
num10=paramstr[11]-48;
num1 =paramstr[12]-48;
rtc_data[3]=num10*16+num1;
// day of week (1-7)
num10=0;
num1 =paramstr[23]-48;
rtc_data[4]=num10*16+num1;
// day of month (1-31)
num10=paramstr[8]-48;
num1 =paramstr[9]-48;
rtc_data[5]=num10*16+num1;
// month (1-12)
num10=paramstr[5]-48;
num1 =paramstr[6]-48;
rtc_data[6]=num10*16+num1;
// year (00-99) e.g. "20" for 2020
num10=paramstr[2]-48;
num1 =paramstr[3]-48;
rtc_data[7]=num10*16+num1;
// Make sure the HALT, STOP, and OF bits are clear before setting RTC
// read flags first
err = rtc_read_flags(&rtc_flags);
if (err)
{
// problem reading flags from RTC
sprintf(downlink_data,"RamSat: Set Date/Time Error - RTC flag read error");
he100_transmit_packet(he100_response, downlink_data);
}
else
{
// clear any flags that are set
err = rtc_clear_flags(rtc_flags);
if (err)
{
// problem clearing RTC flags
sprintf(downlink_data,"RamSat: Set Date/Time Error - RTC flag clearing error");
he100_transmit_packet(he100_response, downlink_data);
}
else
{
// write the RTC data to device, acknowledge wia downlink
rtc_write_nbytes(nbytes, firstbyte, rtc_data);
sprintf(downlink_data,"RamSat: Set Date/Time successful.");
he100_transmit_packet(he100_response, downlink_data);
err = CmdGetDateTime();
}
}
}
return err;
}
// Erase specified 64KB sector on Serial Flash Memory
int CmdEraseSector(char* paramstr)
{
int err = 0;
int sector;
sector = atoi(paramstr);
// make sure this is in a valid range, and prevent erasing sector 0
if (sector == 0 || sector > 127)
{
err = 1;
}
else
{
sfm_erase_64k(sector);
}
return err;
}
// write one 256-byte page within one sector on SFM
int CmdWritePage(char* paramstr)
{
int err = 0;
int sector, page, fill_value;
int n_param;
char data[256];
int i;
// read three parameters from parameter string
n_param = sscanf(paramstr,"%d %d %d",§or, &page, &fill_value);
// error checking
if (n_param != 3 || sector < 1 || sector > 127 || page < 0 || page > 255
|| fill_value < 0 || fill_value > 255)
{
err = 1;
}
else
{
sprintf(downlink_data,"RamSat: test write page fill value = %d",fill_value);
he100_transmit_packet(he100_response, downlink_data);
// fill the test array
for (i=0 ; i<254 ; i++)
{
data[i]=fill_value;
}
// set a null terminator in the last place
data[254]=0;
// write the page
sfm_write_page(sector, page, data, 256);
}
return err;
}
// read one 256-byte page within one sector on SFM, downlink as a string
int CmdDownlinkPage(char* paramstr)
{
int err = 0;
int sector, page;
int n_param;
int data[256];
int i;
// read two parameters from parameter string
n_param = sscanf(paramstr,"%d %d",§or, &page);
// error checking
if (n_param != 2 || sector < 0 || sector > 127 || page < 0 || page > 255)
{
err = 1;
}
else
{
// read the page into data
sfm_read_page(sector, page, data);
// copy from data (int array) to downlink_data (char array)
for (i=0 ; i<254 ; i++)
{
downlink_data[i] = data[i] & 0x00ff;
}
// force null-termination in last place, for safety
downlink_data[254]=0;
// downlink the page as a packet payload string
he100_transmit_packet(he100_response, downlink_data);
}
return err;
}
// downlink telemetry control data for requested telemetry level (0, 1, or 2))
int CmdGetTelemControl(char* paramstr)
{
int err = 0;
int telem_level;
telem_control_type* p;
// read one parameters from parameter string
telem_level = atoi(paramstr);
// error checking on available telemetry levels
if (telem_level < 0 || telem_level > 2)
{
err = 1;
}
else
{
// select which telemetry level control data to report
switch (telem_level)
{
case 0: // Level 0 telemetry control
p = &telem_lev0;
break;
case 1: // Level 1 telemetry control
p = &telem_lev1;
break;
case 2: // Level 2 telemetry control
p = &telem_lev2;
break;
}
// using the assigned pointer, downlink requested telemetry control data
sprintf(downlink_data,"RamSat: Reporting telemetry control data for Level %d",telem_level);
he100_transmit_packet(he100_response, downlink_data);
sprintf(downlink_data,"Lev%d: First timestamp = %s, last timestamp = %s",
telem_level, p->first_timestamp, p->last_timestamp);
he100_transmit_packet(he100_response, downlink_data);
sprintf(downlink_data,"Lev%d: First sector = %d, num_sectors = %d, page_count = %d, record_count = %ld",
telem_level, p->first_sector, p->num_sectors, p->page_count, p->record_count);
he100_transmit_packet(he100_response, downlink_data);
sprintf(downlink_data,"Lev%d: record_period = %d, rec_per_page = %d, page_per_block = %d",
telem_level, p->record_period, p->rec_per_page, p->page_per_block);
he100_transmit_packet(he100_response, downlink_data);
if (p->pagedata[0] != 0)
{
sprintf(downlink_data,"Lev%d: Unwritten page data follows...", telem_level);
he100_transmit_packet(he100_response, downlink_data);
sprintf(downlink_data,"%s",p->pagedata);
he100_transmit_packet(he100_response, downlink_data);
}
else
{
sprintf(downlink_data,"Lev%d: No unwritten page data", telem_level);
he100_transmit_packet(he100_response, downlink_data);
}
}
return err;
}
// downlink telemetry data by sector and page range
int CmdGetTelemData(char* paramstr)
{
int err = 0;
int n_param;
int sector, page;
int start_page, stop_page;
int data[256];
int i;
n_param = sscanf(paramstr,"%d %d %d",§or, &start_page, &stop_page);
// error checking
if (n_param != 3 || sector > 127 || start_page > 255 || stop_page > 255)
{
err = 1;
}
else
{
sprintf(downlink_data,"RamSat: Sending telemetry data for sector %d, start_page = %d, stop_page = %d",
sector, start_page, stop_page);
he100_transmit_packet(he100_response, downlink_data);
for (page=start_page ; page<= stop_page ; page++)
{
// read the page into data
sfm_read_page(sector, page, data);
// copy from data (int array) to downlink_data (char array)
for (i=0 ; i<254 ; i++)
{
downlink_data[i] = data[i] & 0x00ff;
}
// force null-termination in last place, for safety
downlink_data[254]=0;
// downlink the page as a packet payload string
he100_transmit_packet(he100_response, downlink_data);
}
}
return err;
}
// Return the current telemetry for one system, specified by the index value in paramstr
int CmdCurrentTelemetry(char* paramstr)
{
int err = 0;
int n_param;
int index = 0;
unsigned char bat_status, eps_status;
int pdm_initial, pdm_expected, pdm_actual;
int ischarging, bat_nbr, bat_nar, bat_nmr;
float batv, bati, bat_mbt, bat_dbt;
float eps_bcr1v, eps_bcr2v, eps_bcr3v, eps_bcroutv;
float eps_bcr1ia, eps_bcr1ib, eps_bcr2ia, eps_bcr2ib, eps_bcr3ia, eps_bcr3ib;
float eps_bati, eps_bus12i, eps_bus5i, eps_bus33i, eps_eps5i, eps_eps33i, eps_mbt;
imtq_resp_common imtq_common; // iMTQ response from every command
imtq_resp_mtm imtq_calib_mtm; // iMTQ calibrated magnetometer data
// get the index parameter
n_param = sscanf(paramstr, "%d", &index);
// return current telemetry depending on index
switch(index)
{
case 1: // battery telemetry
// response header
sprintf(downlink_data,"RamSat: CmdCurrentTelemetry->Retrieving battery telemetry, index %d", index);
he100_transmit_packet(he100_response, downlink_data);
// a delay to let the transmitter complete before getting telemetry
TMR1 = 0;
while (TMR1 < 1000*TMR1MSEC);
// gather telemetry
bat_status = bat_get_status();
batv = bat_get_batv();
bati = bat_get_bati();
ischarging = bat_get_batischarging();
bat_mbt = bat_get_mbt();
bat_dbt = bat_get_dbt();
bat_nbr = bat_get_nbr();
bat_nar = bat_get_nar();
bat_nmr = bat_get_nmr();
// format and send response
sprintf(downlink_data,"Bat Telem: 0x%02x %.2f %.2f %d %.2f %.2f %d %d %d",
bat_status, batv, bati, ischarging, bat_mbt, bat_dbt, bat_nbr, bat_nar, bat_nmr);
he100_transmit_packet(he100_response, downlink_data);
break;
case 2: // EPS telemetry
// response header
sprintf(downlink_data,"RamSat: CmdCurrentTelemetry->Retrieving EPS telemetry, index %d", index);
he100_transmit_packet(he100_response, downlink_data);
// a delay to let the transmitter complete before getting telemetry
TMR1 = 0;
while (TMR1 < 1000*TMR1MSEC);
// gather telemetry
eps_status = eps_get_status();
eps_bcr1v = eps_get_bcr1v();
eps_bcr2v = eps_get_bcr2v();
eps_bcr3v = eps_get_bcr3v();
eps_bcroutv = eps_get_bcroutv();
eps_bcr1ia = eps_get_bcr1ia();
eps_bcr1ib = eps_get_bcr1ib();
eps_bcr2ia = eps_get_bcr2ia();
eps_bcr2ib = eps_get_bcr2ib();
eps_bcr3ia = eps_get_bcr3ia();
eps_bcr3ib = eps_get_bcr3ib();
eps_bati = eps_get_bati();
eps_bus12i = eps_get_bus12i();
eps_bus5i = eps_get_bus5i();
eps_bus33i = eps_get_bus33i();
eps_eps5i = eps_get_eps5i();
eps_eps33i = eps_get_eps33i();
eps_mbt = eps_get_mbt();
// format and send response
sprintf(downlink_data,"EPS Telem: 0x%02x %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f",
eps_status, eps_bcr1v, eps_bcr2v, eps_bcr3v, eps_bcroutv,
eps_bcr1ia, eps_bcr1ib, eps_bcr2ia, eps_bcr2ib, eps_bcr3ia, eps_bcr3ib,
eps_bati, eps_bus12i, eps_bus5i, eps_bus33i, eps_eps5i, eps_eps33i, eps_mbt);
he100_transmit_packet(he100_response, downlink_data);
break;
case 3: // sun sensor telemetry
// response header
sprintf(downlink_data,"RamSat: CmdCurrentTelemetry->Retrieving sun sensor telemetry, index %d", index);
he100_transmit_packet(he100_response, downlink_data);
// a delay to let the transmitter complete before getting telemetry
TMR1 = 0;
while (TMR1 < 1000*TMR1MSEC);
// gather telemetry
adc_scan_all();
// format and send response
sprintf(downlink_data,"SS Telem: +X=(%d, %d) -X=(%d, %d) +Y=(%d, %d) -Y=(%d, %d)",
ADC1BUF4, ADC1BUF0, ADC1BUF6, ADC1BUF2, ADC1BUF3, ADC1BUF7, ADC1BUF5, ADC1BUF1);
he100_transmit_packet(he100_response, downlink_data);
break;
case 4: // IMTQ magnetometer telemetry
// response header
sprintf(downlink_data,"RamSat: CmdCurrentTelemetry->Retrieving IMTQ magnetometer telemetry, index %d", index);
he100_transmit_packet(he100_response, downlink_data);
// a delay to let the transmitter complete before getting telemetry
TMR1 = 0;
while (TMR1 < 1000*TMR1MSEC);
// gather telemetry: MTM data in frame coordinates
// start the MTM measurement
imtq_start_mtm(&imtq_common);
// delay for MTM integration
TMR1 = 0;
while (TMR1 <= 82 * TMR1MSEC);
// get the calibrated MTM data
imtq_get_calib_mtm(&imtq_common, &imtq_calib_mtm);
// format and send response
sprintf(downlink_data,"MTM Telem: B_fx=%ld, B_fy=%ld, B_fz=%ld",
imtq_calib_mtm.x, imtq_calib_mtm.y, imtq_calib_mtm.z);
he100_transmit_packet(he100_response, downlink_data);
break;
case 5: // Startup telemetry
// response header
sprintf(downlink_data,"RamSat: CmdCurrentTelemetry->Retrieving startup telemetry, index %d", index);
he100_transmit_packet(he100_response, downlink_data);
// telemetry is already in init_data structure from startup sequence
sprintf(downlink_data,"Init Telem: %ld %ld %ld %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x %d %d %d %d %d %d %d %d %.2f %s %s",
init_data.u2br_actual, init_data.i2c1br, init_data.i2c2br,