-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmdTRX.c
802 lines (683 loc) · 23 KB
/
cmdTRX.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
/* SUCHAI
* NANOSATELLITE FLIGHT SOFTWARE
*
* Copyright 2013, Carlos Gonzalez Cortes, carlgonz@ug.uchile.cl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cmdTRX.h"
#include "csp.h"
#include "csp_port.h"
static int trx_tm_send(uint16_t *data, int len);
/* Auxiliary variables */
nanocom_conf_t TRX_CONFIG; /*Stores TRX configuration*/
static uint16_t com_timeout = 5000; //average time is 3000ms
cmdFunction trxFunction[TRX_NCMD];
int trx_sysReq[TRX_NCMD];
void trx_onResetCmdTRX(void){
printf(" trx_onResetCmdTRX\n");
int i;
for(i=0; i<TRX_NCMD; i++) trx_sysReq[i] = CMD_SYSREQ_MIN;
trxFunction[(unsigned char)trx_id_isAlive] = trx_isAlive;
trxFunction[(unsigned char)trx_id_get_operation_mode] = trx_get_operation_mode;
trxFunction[(unsigned char)trx_id_set_operation_mode] = trx_set_operation_mode;
trxFunction[(unsigned char)trx_id_get_count_tm] = trx_get_count_tm;
trxFunction[(unsigned char)trx_id_set_count_tm] = trx_set_count_tm;
trxFunction[(unsigned char)trx_id_get_count_tc] = trx_get_count_tc;
trxFunction[(unsigned char)trx_id_set_count_tc] = trx_set_count_tc;
trxFunction[(unsigned char)trx_id_get_day_last_tc] = trx_get_day_last_tc;
trxFunction[(unsigned char)trx_id_set_day_last_tc] = trx_set_day_last_tc;
trxFunction[(unsigned char)trx_id_get_tx_baud] = trx_get_tx_baud;
trxFunction[(unsigned char)trx_id_set_tx_baud] = trx_set_tx_baud;
trxFunction[(unsigned char)trx_id_get_rx_baud] = trx_get_rx_baud;
trxFunction[(unsigned char)trx_id_set_rx_baud] = trx_set_rx_baud;
trxFunction[(unsigned char)trx_id_get_beacon_level] = trx_get_beacon_level;
trxFunction[(unsigned char)trx_id_set_beacon_level] = trx_set_beacon_level;
trxFunction[(unsigned char)trx_id_get_beacon_period] = trx_get_beacon_period;
trxFunction[(unsigned char)trx_id_set_beacon_period] = trx_set_beacon_period;
trxFunction[(unsigned char)trx_id_set_conf] = trx_set_conf;
trxFunction[(unsigned char)trx_id_read_conf] = trx_read_conf;
trxFunction[(unsigned char)trx_id_ping] = trx_ping;
trxFunction[(unsigned char)trx_id_getstatus] = trx_getstatus;
trxFunction[(unsigned char)trx_id_set_beacon] = trx_set_beacon;
trxFunction[(unsigned char)trx_id_initialize] = trx_initialize;
//Power budget restriction
// Ej. trx_sysReq[(unsigned char)trx_id_tm_trxstatus] = CMD_SYSREQ_MIN+2;
}
/* Status repository commands */
/**
* Check if the TRX is working normally
* @param param not used
* @return 1 Ok, 0 Fail
*/
int trx_isAlive(void *param){
printf("trx_isAlive .. \r\n");
#if (SCH_TRX_ONBOARD == 0)
return 0;
#endif
int arg = NODE_COM;
return trx_ping(&arg);
//return trx_getstatus(NULL);
//return trx_read_conf(NULL);
}
/**
* Returns TRX current operation mode
* @param param Not used
* @return 1(Normal), 0(ERROR)
*/
int trx_get_operation_mode(void *param)
{
// TODO: Implement operation mode report (normal, silent, etc)
// Probably don't need to store values in EEPROM
return 1;
}
/**
* Set TRX mode
* @param param (0)RESET, (1)SYSRESET, (2)SILENT, (3)ONLYBEACON, (4)NOBEACON, (5)NOMINAL
* @return 1 - OK; 0 - Fail
*/
int trx_set_operation_mode(void *param)
{
int mode = *(int *)param;
//TODO: Implement
return mode;
}
/**
* Get current TM count (from status repository)
* @param param Not used
* @return TM count
*/
int trx_get_count_tm(void *param){
MemEEPROM_Vars mem_eeprom_var = mem_trx_count_tm;
int res = mem_getVar(mem_eeprom_var);
return res;
}
/**
* Set current TM count to status repository
* @param param (int *) current TM count
* @return 1(Ok)
*/
int trx_set_count_tm(void *param){
MemEEPROM_Vars mem_eeprom_var = mem_trx_count_tm;
int value = *((int*)param);
mem_setVar(mem_eeprom_var, value);
return 1;
}
/**
* Get current TC count
* @param param Not used
* @return TC count
*/
int trx_get_count_tc(void *param){
MemEEPROM_Vars mem_eeprom_var = mem_trx_count_tc;
int res = mem_getVar(mem_eeprom_var);
return res;
}
/**
* Set current TC count to status repository
* @param param (int *) TC count
* @return 1 (Ok)
*/
int trx_set_count_tc(void *param){
MemEEPROM_Vars mem_eeprom_var = mem_trx_count_tc;
int value = *((int*)param);
mem_setVar(mem_eeprom_var, value);
return 1;
}
/**
* Get the day of the last received TC from status repo.
* @param param Not used
* @return Day of the last TC
*/
int trx_get_day_last_tc(void *param){
MemEEPROM_Vars mem_eeprom_var = mem_trx_day_last_tc;
int res = mem_getVar(mem_eeprom_var);
return res;
}
/**
* Set the day of the last received TC to status repo.
* @param param (int *) Day of las TC
* @return 1 (Ok)
*/
int trx_set_day_last_tc(void *param){
MemEEPROM_Vars mem_eeprom_var = mem_trx_day_last_tc;
int value = *((int*)param);
mem_setVar(mem_eeprom_var, value);
return 1;
}
/**
* Sets baudrate for telemetry
*
* @param param RX Baurade 12=1200bps, 24=2400bps, 48=4800bps [48 default]
* @return 1 - OK; 0 - Fail
*/
int trx_set_tx_baud(void *param)
{
int result;
int baud = *((int *)param);
if(!((baud==12) || (baud==24) || (baud==48)))
return 0;
result = trx_read_conf(NULL);
if(!result)
return 0;
TRX_CONFIG.tx_baud = (uint8_t)baud;
// Save changes to status repo
MemEEPROM_Vars mem_eeprom_var = mem_trx_tx_baud;
mem_setVar(mem_eeprom_var, baud);
// Save changes to device
result = trx_set_conf(NULL);
return result;
}
/**
* Read tx baudrate from device config
* @param param Not used
* @return tx baudrate
*/
int trx_get_tx_baud(void *param)
{
//printf("trx_get_tx_baud ..\r\n");
int result2;
result2 = com_get_conf(&TRX_CONFIG, NODE_COM, com_timeout);
// int result = trx_read_conf(NULL);
// if(!result)
// return 0;
int result = (int)TRX_CONFIG.tx_baud;
return result;
}
/**
* Sets baudrate for telecomand reception
*
* @param param RX Baurade 12=1200bps, 24=2400bps, 48=4800bps [48 default]
* @return 1 - OK; 0 - Fail
*/
int trx_set_rx_baud(void *param)
{
int result;
int baud = *((int *)param);
if(!((baud==12) || (baud==24) || (baud==48)))
return 0;
result = trx_read_conf(NULL);
if(!result)
return 0;
TRX_CONFIG.rx_baud = (uint8_t)baud;
// Save changes to status repo
MemEEPROM_Vars mem_eeprom_var = mem_trx_rx_baud;
mem_setVar(mem_eeprom_var, baud);
// Save changes to device
result = trx_set_conf(NULL);
return result;
}
/**
* Get rx baudrate from device config
* @param param Not used
* @return rx baudrate
*/
int trx_get_rx_baud(void *param)
{
int result2;
result2 = com_get_conf(&TRX_CONFIG, NODE_COM, com_timeout);
// int result = trx_read_conf(NULL);
// if(!result)
// return 0;
int result = (int)TRX_CONFIG.rx_baud;
return result;
}
/**
* Sets beacon battery level. If battery is below this level, trx doesn't send
* beacons
* @param param *uint16_t Beacon battery level
* @return 1 Ok, 0 Fail
*/
int trx_set_beacon_level(void* param)
{
printf("trx_set_beacon_level ..\n");
uint16_t beacon_level = *((uint16_t *)param);
int result = trx_read_conf(NULL);
if(!result)
return 0;
// Save changes to status repo
MemEEPROM_Vars mem_eeprom_var = mem_trx_beacon_bat_lvl;
mem_setVar(mem_eeprom_var, (int)beacon_level);
// Save changes to device
TRX_CONFIG.morse_bat_level = beacon_level;
return trx_set_conf(NULL);
}
/**
* Get beacon level from device config
* @param param Not used
* @return beacon level
*/
int trx_get_beacon_level(void *param)
{
int result2;
result2 = com_get_conf(&TRX_CONFIG, NODE_COM, com_timeout);
// int result = trx_read_conf(NULL);
// if(!result)
// return 0;
int result = (int)TRX_CONFIG.morse_bat_level;
return result;
}
/**
* Sets beacon period in seconds.
* @param param *uint16_t Beacon period in seconds
* @return 1 Ok, 0 Fail
*/
int trx_set_beacon_period(void* param)
{
uint16_t beacon_period = *((uint16_t *)param);
int result = trx_read_conf(NULL);
if(!result)
return 0;
// Save changes to status repo
MemEEPROM_Vars mem_eeprom_var = mem_trx_beacon_period;
mem_setVar(mem_eeprom_var, (int)beacon_period);
// Save changes to device
TRX_CONFIG.morse_inter_delay = beacon_period;
return trx_set_conf(NULL);
}
/**
* Get beacon period from device configuration
* @param param Not used
* @return beacon period
*/
int trx_get_beacon_period(void *param)
{
int result;
result = com_get_conf(&TRX_CONFIG, NODE_COM, com_timeout);
// result = trx_read_conf(NULL);
// if(!result)
// return 0;
result = (int)TRX_CONFIG.morse_inter_delay;
return result;
}
/* TRX commands */
/**
* Upload current configuration into TRX
* @param param Not used
* @return 0 Fail, 1 OK
*/
int trx_set_conf(void *param)
{
#if SCH_CMDTRX_VERBOSE >= 1
printf("Uploading TRX configuration\n");
com_print_conf(&TRX_CONFIG);
#endif
return com_set_conf(&TRX_CONFIG, NODE_COM, com_timeout);
}
/**
* Get TRX current configuration and save to local variable
*
* @param param Not used
* @return 1 - OK, 0 - Fail
*/
int trx_read_conf(void *param)
{
int result;
result = com_get_conf(&TRX_CONFIG, NODE_COM, com_timeout);
#if (SCH_CMDTRX_VERBOSE>=1)
printf("Current TRX configuration\n");
com_print_conf(&TRX_CONFIG);
#endif
return result;
}
/**
* Set the standart beacon: 00SUCHAI00
* @sa tcm_send_beacon()
*
* @param param (0)SUCHAI beacon. (1)Test beacon 1. (2)Test beacon 2.
* @return 1 - OK
*/
int trx_set_beacon(void *param)
{
static char stdbeacon[COM_MORSE_LEN];
memset(stdbeacon, '\0', COM_MORSE_LEN);
switch (*(int *)param)
{
case 0:
strcpy(stdbeacon, "00SUCHAI00");
break;
case 1:
strcpy(stdbeacon, "0123456789");
break;
case 2:
strcpy(stdbeacon, "0000000000");
break;
default:
strcpy(stdbeacon, "");
break;
}
#if SCH_CMDTRX_VERBOSE > 2
printf("Setting beacon: %s\n", stdbeacon);
#endif
memcpy(TRX_CONFIG.morse_text, stdbeacon, COM_MORSE_LEN);
int result = trx_set_conf(NULL);
return result;
}
/**
* Send a frame for testing (ping) to desired node
*
* @param param int Node to transmit
* @return 1 - OK
*/
int trx_ping(void *param)
{
int result;
int node = *((int *)param);
#if SCH_CMDTRX_VERBOSE
printf("Sending test frame to node %d...\n", node);
#endif
result = csp_ping(node, com_timeout, 10, CSP_O_NONE);
#if SCH_CMDTRX_VERBOSE
printf("Ping to %d of size %d, took %d ms\n", node, 10, result);
#endif
result = result > 0 ? 1:0;
return result;
}
/**
* Read and show TRX status. Debug only
*
* @param param Not used
* @return 1 - OK, 0 - Fail
*/
int trx_getstatus(void *param)
{
nanocom_data_t status;
int result;
result = com_get_status(&status, NODE_COM, com_timeout);
#if SCH_CMDTRX_VERBOSE
com_printf_status(&status);
#endif
return result;
}
/**
* Initializes TRX main parameters keeping modifications
*
* @param param Is deployed?
* @return 1 - OK; 0 - Fail
*/
int trx_initialize(void *param)
{
int deployed;
if(param)
deployed = *((int *)param);
else
deployed = 0;
// Reading settings from status repo
MemEEPROM_Vars mem_eeprom_var;
mem_eeprom_var = mem_trx_beacon_period;
uint16_t morse_inter_delay = (uint16_t)mem_getVar(mem_eeprom_var);
mem_eeprom_var = mem_trx_beacon_bat_lvl;
uint16_t morse_bat_level = (uint16_t)mem_getVar(mem_eeprom_var);
mem_eeprom_var = mem_trx_rx_baud;
uint8_t rx_baud = (uint8_t)mem_getVar(mem_eeprom_var);
mem_eeprom_var = mem_trx_tx_baud;
uint8_t tx_baud = (uint8_t)mem_getVar(mem_eeprom_var);
// Generating trx settings
TRX_CONFIG.do_random = 1;
TRX_CONFIG.do_rs = 1;
TRX_CONFIG.do_viterbi = 0;
TRX_CONFIG.hk_interval = 5;
TRX_CONFIG.morse_bat_level = morse_bat_level;
TRX_CONFIG.morse_cycle = 1;
TRX_CONFIG.morse_en_rf_err = 1;
TRX_CONFIG.morse_en_rssi = 0;
TRX_CONFIG.morse_en_rx_count = 0;
TRX_CONFIG.morse_en_temp_a = 0;
TRX_CONFIG.morse_en_temp_b = 0;
TRX_CONFIG.morse_en_tx_count = 0;
TRX_CONFIG.morse_en_voltage = 1;
TRX_CONFIG.morse_enable = 1;
TRX_CONFIG.morse_inter_delay = morse_inter_delay;
TRX_CONFIG.morse_mode = (uint8_t)SCH_TRX_BEACON_MODE;
TRX_CONFIG.morse_pospone = (uint16_t)SCH_TRX_BEACON_POSPONE_NOM;
// TRX_CONFIG.morse_text = "00SUCHAI00"; //Use command
TRX_CONFIG.morse_wpm = (uint8_t)SCH_TRX_BEACON_WPM;
TRX_CONFIG.preamble_length = 150;
TRX_CONFIG.rx_baud = rx_baud;
TRX_CONFIG.tx_baud = tx_baud;
TRX_CONFIG.tx_max_temp = 60;
if(deployed==0)
{
printf(" * trx_initialize, first time on => TRX_CONFIG.morse_pospone = %d \r\n", SCH_TRX_BEACON_POSPONE_PRE);
TRX_CONFIG.morse_pospone = SCH_TRX_BEACON_POSPONE_PRE;
}
/* Save configuration to TRX */
int beacon = 0; // Set suchai beacon
int result = trx_set_beacon((void *)(&beacon)); //Also call trx_set_conf
return result;
}
/**
* Organize temletry data in frames and load this frames in TRX. Each telemetry
* frame has the following format.
* @code
* |--- Control fields ---||---- Data fields ---|
* __________________________________________________________
* | Type (2) | Frame# (2)|| TM-ID(2) | DATA(LEN) ||
* |__________|___________||__________||INT16|___...___|INT16||
* @endcode
*
*
* @param data Pointer to data that will be append
* @param len Lenght of the data array
* @param mode Mode of append CMD_ADDFRAME_START -> Start new frame
* CMD_ADDFRAME_CONT -> Inter frame
* CMD_ADDFRAME_STOP -> Stop frame
* CMD_ADDFRAME_FIN -> Finish current session
* CMD_ADDFRAME_ADD -> Add new data
* CMD_ADDFRAME_SINGL -> Create a single frame
*
* @return 0, TX Fail. 1, TX Success. 2, No TX operation performe
*/
static int single_frame = 1;
int trx_tm_addtoframe(int *data, int len, int mode)
{
static uint16_t tmframe[TRX_TMFRAMELEN16];
static int int16_counter = 0;
static int frame_counter = 0;
static int tm_type = CMD_STOP;
int send_stat = 2;
while(mode != CMD_ADDFRAME_EXIT)
{
switch (mode)
{
/* A single frame that contains max TRX_TMFRAMELEN bytes of data */
case CMD_ADDFRAME_SINGL:
/* A new frame being configured */
int16_counter = 0;
/* Append control field */
tmframe[int16_counter++] = CMD_TMFRAME_TSINGL; /* Type (1) */
tmframe[int16_counter++] = (uint16_t)(frame_counter); /* Frame# (2) */
/* Adding data to the current frame. Max TRX_TMFRAMELEN bytes */
while((len > 0) && (int16_counter < TRX_TMFRAMELEN16))
{
tmframe[int16_counter++] = (uint16_t)(*data); /* Data */
data++;
len--;
}
/* Loading frame to TRX and exiting */
for(;int16_counter<TRX_TMFRAMELEN16;int16_counter++)
{
tmframe[int16_counter] = (uint16_t)CMD_STOP;
}
/* Load and transmit TM */
trx_tm_send(tmframe, TRX_TMFRAMELEN16);
frame_counter++;
int16_counter = 0;
mode = CMD_ADDFRAME_EXIT;
break;
/* A new start frame for more than TRX_TMFRAMELEN bytes, so we need
* to load several frames cotaining all the telemetry */
case CMD_ADDFRAME_START:
/* A new frame being configured */
/* Load all remaining frames before */
if(int16_counter > 0)
{
for(;int16_counter<TRX_TMFRAMELEN16;int16_counter++)
{
tmframe[int16_counter] = (uint16_t)CMD_STOP;
}
/* Load and transmit TM */
trx_tm_send(tmframe, TRX_TMFRAMELEN16);
}
int16_counter = 0;
/* Append control field */
tmframe[int16_counter++] = CMD_TMFRAME_TSTART; /* Type (2) */
tmframe[int16_counter++] = (uint16_t)(frame_counter); /* Frame# (2) */
frame_counter++;
/* Adding current tm type */
if(len > 0)
{
tm_type = *data;
tmframe[int16_counter++] = (uint16_t)(tm_type); /* DataL */
/* Update the remaining data counter */
data++;
len--;
}
mode = CMD_ADDFRAME_ADD;
break;
case CMD_ADDFRAME_CONT:
/* A new frame being configured */
int16_counter = 0;
single_frame = 0;
/* Append control field */
tmframe[int16_counter++] = CMD_TMFRAME_TCONT; /* Type (2) */
tmframe[int16_counter++] = (char)(frame_counter); /* Frame# (2) */
/* Add tm type */
/* For TM formate please see:
* https://docs.google.com/spreadsheets/d/1LKhd_xAB94L9oMWbPKR8nDvijBbtvDN1tCDrdkQpOPM/edit?usp=sharing
*/
//tmframe[int16_counter++] = (uint16_t)(tm_type); /* DataL */ // solo se envia en el START
/* Add data if needed */
frame_counter++;
mode = CMD_ADDFRAME_ADD;
break;
/* The last frame with the current telemetry */
case CMD_ADDFRAME_STOP:
/* Load all remaining frames before */
if(int16_counter > 0)
{
for(;int16_counter<TRX_TMFRAMELEN16;int16_counter++)
{
tmframe[int16_counter] = (uint16_t)CMD_STOP;
}
/* Load and transmit TM */
trx_tm_send(tmframe, TRX_TMFRAMELEN16);
}
/* A new frame stop being configured */
int16_counter = 0;
/* Append control field */
tmframe[int16_counter++] = CMD_TMFRAME_TSTOP; /* Type (2) */
tmframe[int16_counter++] = (uint16_t)(frame_counter); /* Frame# (2) */
/* Add data to this frame */
frame_counter++;
mode = CMD_ADDFRAME_ADD;
break;
/* Load to TRX all reamaining frames and reset the control field
* counters to start a new session. No new data will be append */
case CMD_ADDFRAME_FIN:
/* Load to trx the remaining frames */
if(int16_counter > 0)
{
for(;int16_counter<TRX_TMFRAMELEN16;int16_counter++)
{
tmframe[int16_counter] = (uint16_t)CMD_STOP;
}
/* Load and transmit TM */
if(single_frame == 1){
tmframe[0] = CMD_TMFRAME_TSINGL;
}
else{
tmframe[0] = CMD_TMFRAME_TSTOP;
}
trx_tm_send(tmframe, TRX_TMFRAMELEN16);
}
single_frame = 1;
int16_counter = 0;
frame_counter = 0;
tm_type = CMD_STOP;
mode = CMD_ADDFRAME_EXIT;
break;
/* Append all the requiered data to the current frame or adds more
* CONT frame if needed */
case CMD_ADDFRAME_ADD:
if(len > 0)
{
/* Current frame is not initilized */
if(int16_counter == 0 )
{
mode = CMD_ADDFRAME_EXIT;
}
/* Current frame is not full */
else if(int16_counter < TRX_TMFRAMELEN16)
{
/* Adding data to the current frame*/
tmframe[int16_counter++] = (uint16_t)(*data); /* DataL */
/* Update the remaining data counter */
data++;
len--;
mode = CMD_ADDFRAME_ADD;
}
else /* Current frame is full */
{
/* This frame is ready to be loaded into TRX buffer */
/* First fill tmframe's unused fields */
for(;int16_counter<TRX_TMFRAMELEN16;int16_counter++)
{
tmframe[int16_counter] = (uint16_t)CMD_STOP;
}
/* Load and transmit TM */
trx_tm_send(tmframe, TRX_TMFRAMELEN16);
/* Keep adding data in a new frame */
int16_counter = 0;
mode = CMD_ADDFRAME_CONT;
}
}
else /* No more data to load */
{
/* Exit */
mode = CMD_ADDFRAME_EXIT;
}
break;
/* Invalid state */
default:
mode = CMD_ADDFRAME_EXIT;
break;
}
}
//return frame_counter;
return send_stat;
}
/**
* Auxiliary function. Send a frame by TRX
*
* @param data Pointer to data buffer (**int16 buffer**)
* @param len Lenght of **int16** buffer to transmit
* @return 1 Ok, 0 fail.
*/
static int trx_tm_send(uint16_t *data, int len)
{
#if(SCH_CMDTRX_VERBOSE >= 1)
//printf("Sending TM frame...\n");
int i; unsigned int h;
for(i=0;i<len;i++){
h = data[i];
printf("0x%04X,", h);
}
printf("\r\n");
#endif
int result;
result = csp_transaction(CSP_PRIO_NORM, SCH_TRX_NODE_GND, SCH_TRX_PORT_TM,
com_timeout, (void *)data, len*2, NULL, 0);
return result;
}