-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnode.c
1210 lines (1033 loc) · 40.3 KB
/
snode.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
/// @brief Servo-node management.
////////////////////////////////////////////////////////////////////////////////
// *****************************************************************************
// ************************** System Include Files *****************************
// *****************************************************************************
// *****************************************************************************
// ************************** User Include Files *******************************
// *****************************************************************************
#include "snode.h"
#include "fmucomm.h"
#include "can.h"
#include "coretime.h"
#include "rc.h"
// *****************************************************************************
// ************************** Macros *******************************************
// *****************************************************************************
// The number of Servo-Nodes available in the module data buffer.
#define SNODE_RX_BUF_SIZE 10
// Maximum number of CAN message read on each 'Task' execution.
#define SNODE_CTRL_DATA_MSG_MAX 4
// Number of micro-seconds after which an Servo-Node is invalidated, when
// fresh CAN data is not received.
#define SNODE_CTRL_DATA_TIMEOUT_US 1000000
// *****************************************************************************
// ************************** Defines ******************************************
// *****************************************************************************
// Structure defining the contents of a Servo-node received data.
typedef struct
{
bool valid;
uint32_t rx_time_us;
CAN_RX_MSG_SNODE_SERVO_STATUS_U servo_status;
CAN_RX_MSG_SNODE_VSENSE_DATA_U vsense_data;
CAN_RX_MSG_SNODE_STATUS_U node_status;
CAN_RX_MSG_SNODE_VERSION_U node_version;
} SNODE_RX_DATA;
typedef struct
{
SNODE_ID_WRITE_STATUS status; // Status/state of configuration write operation.
uint8_t curID; // Current Node ID.
uint8_t newID; // New/Updated Node ID.
} SNODE_WRITE_ID_DATA;
typedef struct
{
SNODE_CAL_WRITE_STATUS status; // Status/state of configuration write operation.
uint8_t id; // CAN Node ID
uint8_t cfg_sel;
SNODE_CFG_VAL cfg_val;
} SNODE_WRITE_CAL_DATA;
typedef struct
{
SNODE_CAL_READ_STATUS status; // Status/state of configuration write operation.
uint8_t id; // CAN Node ID
uint8_t cfg_sel;
SNODE_CFG_VAL cfg_val;
} SNODE_READ_CAL_DATA;
// *****************************************************************************
// ************************** Definitions **************************************
// *****************************************************************************
// Module data is managed with one level of pointer indirection to easy sorting
// of CAN node data. The ID Buffer is indexed with a Servo-node ID to access
// its data. All valid pointers within the ID buffer reference an element of
// the Data Buffer. A NULL pointer in the ID Buffer identifies CAN Node data
// which is invalid/not-received.
//
// The Data Buffer is defined with a size smaller than the ID Buffer. This
// is because to manage memory use since fewer number of Servo-nodes are
// required to be supported at any one time.
//
static SNODE_RX_DATA snode_rx_data_buf[ SNODE_RX_BUF_SIZE ];
static SNODE_RX_DATA* snode_rx_data_id_p[ 128 ] = { 0 };
static SNODE_WRITE_ID_DATA snode_write_id_data =
{
.status = SNODE_ID_WRITE_SUCCESS,
};
static SNODE_WRITE_CAL_DATA snode_write_cal_data =
{
.status = SNODE_CAL_WRITE_SUCCESS,
};
static SNODE_READ_CAL_DATA snode_read_cal_data =
{
.status = SNODE_CAL_READ_SUCCESS,
};
// Intrusive Control Mode (ICM) status. Value is initialized to 'false' to
// perform normal operation on reset.
//
static bool snode_icm = false;
// *****************************************************************************
// ************************** Function Prototypes ******************************
// *****************************************************************************
static void SNodeCtrlDataTask( void );
static void SNodeCtrlCmdTask( void );
static void SNodeCalWriteTask( void );
static void SNodeIDWriteTask( void );
static void SNodeCalReadTask( void );
static void SNodeCANRx( void );
static void SNodeCANTimeout( void );
static uint8_t SNodeCtrlDataBuild( FMUCOMM_CTRL_SURFACE_DATA_PL* eth_ctrl_data_msg );
static void SNodeBufSetup( uint8_t node_id );
// *****************************************************************************
// ************************** Global Functions *********************************
// *****************************************************************************
void SNodeTask( void )
{
// Host <-> FMU <-> Servo-Node communication tasks.
SNodeCtrlDataTask();
// Servo-Node is not being intrusively controlled ?
if( snode_icm == false )
{
// Perform normal operation of constructing and sending the
// controller commands.
SNodeCtrlCmdTask();
}
// Web <-> FMU <-> Servo-Node communication tasks.
SNodeIDWriteTask();
SNodeCalWriteTask();
SNodeCalReadTask();
}
void SNodeICMSet( bool icm )
{
snode_icm = icm;
}
void SNodeCalWriteSet( uint8_t node_id, SNODE_CFG_VAL* cfg_val_p )
{
snode_write_cal_data.status = SNODE_CAL_WRITE_IN_PROG;
snode_write_cal_data.id = node_id;
snode_write_cal_data.cfg_sel = 1;
snode_write_cal_data.cfg_val = *cfg_val_p;
}
void SNodeCalReadSet( uint8_t node_id )
{
snode_read_cal_data.status = SNODE_CAL_READ_IN_PROG;
snode_read_cal_data.id = node_id;
snode_read_cal_data.cfg_sel = 1;
}
void SNodeIDWriteSet( uint8_t node_id_cur, uint8_t node_id_new )
{
snode_write_id_data.status = SNODE_ID_WRITE_IN_PROG;
snode_write_id_data.curID = node_id_cur;
snode_write_id_data.newID = node_id_new;
}
SNODE_ID_WRITE_STATUS SNodeIDWriteStatusGet( void )
{
return snode_write_id_data.status;
}
// Get the results of the Servo-Node calibration writing process.
SNODE_CAL_WRITE_STATUS SNodeCalWriteStatusGet( void )
{
return snode_write_cal_data.status;
}
// Get the results of the Servo-Node calibration reading process.
SNODE_CAL_READ_STATUS SNodeCalReadStatusGet( void )
{
return snode_read_cal_data.status;
}
void SNodeCalReadStrGet( char* str_in )
{
uint8_t coeff_idx;
char* init_str = "";
char* del_str = ",";
char elem_str[20];
// Initialize the input string.
strcpy(str_in, init_str);
// Append string with PWM coefficients.
for( coeff_idx = 0;
coeff_idx < 6;
coeff_idx++ )
{
if( snode_read_cal_data.status == SNODE_CAL_READ_SUCCESS )
{
itoa(elem_str, snode_read_cal_data.cfg_val.pwm_coeff[ coeff_idx ], 10);
strcat(str_in, elem_str);
}
// Add comma delimiter.
strcat(str_in, del_str);
}
// Append string with VSENSE1 coefficients.
for( coeff_idx = 0;
coeff_idx < 6;
coeff_idx++ )
{
if( snode_read_cal_data.status == SNODE_CAL_READ_SUCCESS )
{
itoa(elem_str, snode_read_cal_data.cfg_val.vsense1_coeff[ coeff_idx ], 10);
strcat(str_in, elem_str);
}
// Add comma delimiter.
strcat(str_in, del_str);
}
// Append string with VSENSE2 coefficients.
for( coeff_idx = 0;
coeff_idx < 6;
coeff_idx++ )
{
if( snode_read_cal_data.status == SNODE_CAL_READ_SUCCESS )
{
itoa(elem_str, snode_read_cal_data.cfg_val.vsense2_coeff[ coeff_idx ], 10);
strcat(str_in, elem_str);
}
// Add comma delimiter.
strcat(str_in, del_str);
}
}
void SNodeRxDataStrGet( char* str_in )
{
uint8_t surface_idx = 0;
uint8_t id_idx;
char* del_str = ",";
char elem_str[20];
// Determine the number of Servo-Nodes on the network.
for( id_idx = 0;
id_idx < 128;
id_idx++ )
{
// Module data for Servo-Node ID is valid ?
if( snode_rx_data_id_p[ id_idx ] != NULL )
{
surface_idx++;
}
}
// Add number of CAN Servo-Nodes as first element.
utoa(elem_str, (uint32_t) surface_idx, 10);
strcpy(str_in, elem_str);
// Add comma delimiter.
strcat(str_in, del_str);
// Loop through the Servo-nodes on the network, populating their data.
for( id_idx = 0;
id_idx < 128;
id_idx++ )
{
// Module data for Servo-Node ID is valid ?
if( snode_rx_data_id_p[ id_idx ] != NULL )
{
// Add 'ID'.
utoa(elem_str, id_idx, 10);
strcat(str_in, elem_str);
// Add comma delimiter.
strcat(str_in, del_str);
// Add 'actPwm'.
utoa(elem_str, snode_rx_data_id_p[ id_idx ]->servo_status.pwm_act, 10);
strcat(str_in, elem_str);
// Add comma delimiter.
strcat(str_in, del_str);
// Add 'servoVoltage'.
utoa(elem_str, snode_rx_data_id_p[ id_idx ]->servo_status.servo_voltage, 10);
strcat(str_in, elem_str);
// Add comma delimiter.
strcat(str_in, del_str);
// Add 'vsense1Cor'.
itoa(elem_str, snode_rx_data_id_p[ id_idx ]->vsense_data.vsense1_cor, 10);
strcat(str_in, elem_str);
// Add comma delimiter.
strcat(str_in, del_str);
// Add 'vsense2Cor'.
itoa(elem_str, snode_rx_data_id_p[ id_idx ]->vsense_data.vsense2_cor, 10);
strcat(str_in, elem_str);
// Add comma delimiter.
strcat(str_in, del_str);
}
}
}
// *****************************************************************************
// ************************** Static Functions *********************************
// *****************************************************************************
////////////////////////////////////////////////////////////////////////////////
/// @brief Servo-node control data task.
///
/// Manage the process of storing data and construction of the Control
/// Surface Data Ethernet packet.
////////////////////////////////////////////////////////////////////////////////
static void SNodeCtrlDataTask( void )
{
static enum
{
SM_AGE_EVAL,
SM_ETH_BUILD,
SM_RX_MSGS,
} taskState = SM_RX_MSGS;
static FMUCOMM_CTRL_SURFACE_DATA_PL eth_ctrl_data_msg;
static uint32_t prev_tx_time = 0;
static bool tx_msg_ready = false;
static uint8_t surface_num_of = 0;
// Required time has elapsed since last Control Surface Data transmission ?
if( CoreTime32usGet() - prev_tx_time > 10000 )
{
// Increment tx-time by fixed transmission period time to eliminate
// drift.
prev_tx_time += 10000;
// Transmission period is still already elapsed ?
//
// Note: This could occur if processing inhibited this function's
// execution for an extended amount of time.
//
if( CoreTime32usGet() - prev_tx_time > 10000 )
{
// Update tx-time to the current time. Single or multiple
// periods have elapsed. Setting tx-time to the current time
// prevents repeated identifications of the period having elapsed.
prev_tx_time = CoreTime32usGet();
}
// Transition to start the evaluation and build of a new Ethernet
// packet.
taskState = SM_AGE_EVAL;
}
switch( taskState )
{
case( SM_AGE_EVAL ):
{
// Inhibit request to transmit the Ethernet message. New data is to
// be constructed for the transmission.
tx_msg_ready = false;
// Evaluate age of received CAN data for each Servo-Node. Old/stale
// servo nodes are to be invalidated.
SNodeCANTimeout();
taskState++;
break;
}
case( SM_ETH_BUILD ):
{
// Build fresh Ethernet data for transmission.
surface_num_of = SNodeCtrlDataBuild( ð_ctrl_data_msg );
// Identify request to transmit a message.
tx_msg_ready = true;
taskState++;
break;
}
case( SM_RX_MSGS ):
{
// Read CAN messages into module data.
SNodeCANRx();
break;
}
}
// Ethernet message ready for transmission ?
if( tx_msg_ready == true )
{
bool queue_ok;
// Queue the message for transmission.
queue_ok = FMUCommSet( FMUCOMM_TYPE_CTRL_SURFACE_DATA,
(uint8_t*) ð_ctrl_data_msg,
sizeof( FMUCOMM_CTRL_SURFACE_DATA_PL_FIELD ) * surface_num_of );
if( queue_ok == true )
{
tx_msg_ready = false;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Servo-node control command task.
///
/// This function manages forwarding of Servo Commands to the
/// Servo-node CAN network.
////////////////////////////////////////////////////////////////////////////////
static void SNodeCtrlCmdTask( void )
{
typedef struct
{
uint8_t id;
CAN_TX_SERVO_CMD_U cmd;
} CAN_MSG_S;
static uint32_t prev_tx_time = 0;;
static CAN_MSG_S can_msg[ 10 ];
static uint8_t snodes_max = 0;
const FMUCOMM_RX_PKT* eth_ctrl_cmd_p;
const FMUCOMM_CTRL_SURFACE_CMD_PL* eth_ctrl_cmd_pl_p;
uint8_t snodes_idx;
bool rc_ctrl;
uint16_t rc_servo_val[ 10 ];
rc_ctrl = RCCtrlGet();
// RC commanded control to be performed ?
if( rc_ctrl == true )
{
RCServoGet( &rc_servo_val[ 0 ] );
snodes_max = 10;
// Loop through each node's information.
for( snodes_idx = 0; snodes_idx < snodes_max; snodes_idx++ )
{
// Construct the CAN message to send with the received RC
// data.
can_msg[snodes_idx].id = snodes_idx + 2; // Node ID, servos start at CH2
can_msg[snodes_idx].cmd.cmd_type = 0; // PWM control
can_msg[snodes_idx].cmd.cmd_pwm = rc_servo_val[ snodes_idx ]; // PWM value
can_msg[snodes_idx].cmd.cmd_pos = 0; // N/A, b/c PWM control
}
}
else // Host commanded control is to be performed.
{
eth_ctrl_cmd_p = FMUCommGet( FMUCOMM_TYPE_CTRL_SURFACE_CMD );
// Control Surface Command received ?
if( eth_ctrl_cmd_p->valid == true )
{
// Typecast received packet to controller command type to access packet
// content.
eth_ctrl_cmd_pl_p = (FMUCOMM_CTRL_SURFACE_CMD_PL*) eth_ctrl_cmd_p->pl_p;
// Determine number of nodes commanded
snodes_max = (uint8_t) ( eth_ctrl_cmd_p->wrap.length /
sizeof( FMUCOMM_CTRL_SURFACE_CMD_PL_FIELD ) );
// Loop through each node's information.
for( snodes_idx = 0; snodes_idx < snodes_max; snodes_idx++ )
{
// Construct the CAN message to send with the received Ethernet
// data.
can_msg[snodes_idx].id = eth_ctrl_cmd_pl_p->ctrlSurface[ snodes_idx ].surfaceID;
can_msg[snodes_idx].cmd.cmd_type = eth_ctrl_cmd_pl_p->ctrlSurface[ snodes_idx ].cmdType;
can_msg[snodes_idx].cmd.cmd_pwm = eth_ctrl_cmd_pl_p->ctrlSurface[ snodes_idx ].cmdPwm;
can_msg[snodes_idx].cmd.cmd_pos = eth_ctrl_cmd_pl_p->ctrlSurface[ snodes_idx ].cmdPos;
}
}
}
// Required time has elapsed since last Control Command transmission ?
if( CoreTime32usGet() - prev_tx_time > 10000 )
{
// Increment tx-time by fixed transmission period time to eliminate
// drift.
prev_tx_time += 10000;
// Transmission period is still already elapsed ?
//
// Note: This could occur if processing inhibited this function's
// execution for an extended amount of time.
//
if( CoreTime32usGet() - prev_tx_time > 10000 )
{
// Update tx-time to the current time. Single or multiple
// periods have elapsed. Setting tx-time to the current time
// prevents repeated identifications of the period having elapsed.
prev_tx_time = CoreTime32usGet();
}
// Loop through each node's information.
for( snodes_idx = 0; snodes_idx < snodes_max; snodes_idx++ )
{
// Forward the command to the Servo-node.
CANTxSet( CAN_TX_MSG_SERVO_CMD,
can_msg[snodes_idx].id,
&can_msg[snodes_idx].cmd.data_u32[ 0 ] );
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Servo-node write identification task.
///
/// Once the new ID has been received, this function writes the ID out to the
/// Servo-node CAN network.
////////////////////////////////////////////////////////////////////////////////
static void SNodeIDWriteTask( void )
{
static enum
{
SM_IDLE,
SM_WRITE_REQ,
SM_DELAY,
SM_READ_RESP,
} taskState = SM_IDLE;
switch(taskState)
{
case SM_IDLE:
{
// Calibration write operation to be performed ?
if( snode_write_id_data.status == SNODE_ID_WRITE_IN_PROG )
{
// Kickoff the writing process.
taskState = SM_WRITE_REQ;
}
break;
}
case SM_WRITE_REQ:
{
CAN_TX_WRITE_REQ_U write_req;
// Build the CAN message to transmit.
write_req.cfg_sel = 0; // Node ID selection.
write_req.cfg_val_u8 = snode_write_id_data.newID; // New node ID value.
// Queue the CAN message for transmission.
CANTxSet( CAN_TX_MSG_CFG_WRITE_REQ,
snode_write_id_data.curID,
&write_req.data_u32[0] );
taskState++;
break;
}
// Wait at least 100ms for the CAN node to process the message
// and sent the response back.
case SM_DELAY:
{
static uint32_t delayStart = 0;
// Initialize delay state on first evaluation.
if( delayStart == 0 )
{
delayStart = CoreTime32usGet();
}
// Required time (i.e. 100ms) has elapsed ?
if( CoreTime32usGet() - delayStart > 100000 )
{
taskState++;
// Reset the start time for evaluation on next delay.
delayStart = 0;
}
break;
}
case SM_READ_RESP:
{
CAN_RX_WRITE_RESP_U write_resp;
bool rx_valid;
uint8_t rx_node_id;
// Default the ID write operation status to failed.
snode_write_id_data.status = SNODE_ID_WRITE_FAIL;
// Read the response back.
rx_valid = CANRxGet( CAN_RX_MSG_CFG_WRITE_RESP,
&rx_node_id,
&write_resp.data_u32[0] );
// Message received/valid ?
if( rx_valid == true )
{
// - Message's node ID matches that expected ?
// - Updated value matches that selected ?
// - Response identifies success ?
//
if( ( snode_write_id_data.newID == rx_node_id ) &&
( write_resp.cfg_sel == 0 ) &&
( write_resp.fault_status == 0 ) )
{
snode_write_id_data.status = SNODE_ID_WRITE_SUCCESS;
}
}
// ID operation completed, return to the idle state.
taskState = SM_IDLE;
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Servo-node write calibration task.
///
/// Once calibration data has been received, this function writes the data
/// out to the Servo-node CAN network.
////////////////////////////////////////////////////////////////////////////////
static void SNodeCalWriteTask( void )
{
static enum
{
SM_IDLE,
SM_WRITE_REQ,
SM_DELAY,
SM_WRITE_RESP,
SM_DONE_EVAL,
} taskState = SM_IDLE;
switch(taskState)
{
case SM_IDLE:
{
// Calibration write operation to be performed ?
if( snode_write_cal_data.status == SNODE_CAL_WRITE_IN_PROG )
{
// Kickoff the writing process.
taskState = SM_WRITE_REQ;
}
break;
}
case SM_WRITE_REQ:
{
CAN_TX_WRITE_REQ_U write_req;
// Build the CAN message to transmit.
write_req.cfg_sel = snode_write_cal_data.cfg_sel;
// PWM coefficient value to be transmitted ?
if( snode_write_cal_data.cfg_sel <= 6 )
{
write_req.cfg_val_i32 = snode_write_cal_data.cfg_val.pwm_coeff[ snode_write_cal_data.cfg_sel - 1 ];
}
// VSENSE1 coefficient value to be transmitted ?
else
if(snode_write_cal_data.cfg_sel <= 12 )
{
write_req.cfg_val_i32 = snode_write_cal_data.cfg_val.vsense1_coeff[ snode_write_cal_data.cfg_sel - 7 ];
}
// VSENSE2 coefficient value to be transmitted.
else
{
write_req.cfg_val_i32 = snode_write_cal_data.cfg_val.vsense2_coeff[ snode_write_cal_data.cfg_sel - 13 ];
}
// Queue the CAN message for transmission.
CANTxSet( CAN_TX_MSG_CFG_WRITE_REQ,
snode_write_cal_data.id,
&write_req.data_u32[0] );
taskState++;
break;
}
// Wait at least 100ms for the CAN node to process the message
// and sent the response back.
case SM_DELAY:
{
static uint32_t delayStart = 0;
// Initialize delay state on first evaluation.
if( delayStart == 0 )
{
delayStart = CoreTime32usGet();
}
// Required time (i.e. 100ms) has elapsed ?
if( CoreTime32usGet() - delayStart > 100000 )
{
taskState++;
// Reset the start time for evaluation on next delay.
delayStart = 0;
}
break;
}
case SM_WRITE_RESP:
{
CAN_RX_WRITE_RESP_U write_resp;
bool rx_valid;
uint8_t rx_node_id;
// Read the response back.
rx_valid = CANRxGet( CAN_RX_MSG_CFG_WRITE_RESP,
&rx_node_id,
&write_resp.data_u32[0] );
// - Response received/valid ?
// - Response node ID matches that expected ?
// - Updated value matches that selected ?
// - Response identifies success ?
//
if( ( rx_valid == true ) &&
( snode_write_cal_data.id == rx_node_id ) &&
( write_resp.cfg_sel == snode_write_cal_data.cfg_sel ) &&
( write_resp.fault_status == 0 ) )
{
// Update configuration selection to program the next
// parameter (or identify completion of write process).
snode_write_cal_data.cfg_sel++;
}
else
{
// Expected response not received - fail the write operation.
snode_write_cal_data.status = SNODE_CAL_WRITE_FAIL;
}
taskState++;
break;
}
case SM_DONE_EVAL:
{
// Failure with Calibration write operation ?
if( snode_write_cal_data.status == SNODE_CAL_WRITE_FAIL )
{
taskState = SM_IDLE;
}
else
{
// Calibration is complete ?
if( snode_write_cal_data.cfg_sel > 18 )
{
// Identify success since all calibration values have been
// successfully written.
snode_write_cal_data.status = SNODE_CAL_WRITE_SUCCESS;
taskState = SM_IDLE;
}
else
{
// Still more calibration values to write - continue
// with the next request/response sequence.
taskState = SM_WRITE_REQ;
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Servo-node read calibration task.
///
/// Once a read operation has been commanded, this function reads the data
/// from to the Servo-node CAN network.
////////////////////////////////////////////////////////////////////////////////
static void SNodeCalReadTask( void )
{
static enum
{
SM_IDLE,
SM_READ_REQ,
SM_DELAY,
SM_READ_RESP,
SM_DONE_EVAL,
} taskState = SM_IDLE;
switch(taskState)
{
case SM_IDLE:
{
// Calibration read operation to be performed ?
if( snode_read_cal_data.status == SNODE_CAL_READ_IN_PROG )
{
// Kickoff the writing process.
taskState = SM_READ_REQ;
}
break;
}
case SM_READ_REQ:
{
CAN_TX_READ_REQ_U read_req;
// Build the CAN message to transmit.
read_req.cfg_sel = snode_read_cal_data.cfg_sel;
// Queue the CAN message for transmission.
CANTxSet( CAN_TX_MSG_CFG_READ_REQ,
snode_read_cal_data.id,
&read_req.data_u32[0] );
taskState++;
break;
}
// Wait at least 100ms for the CAN node to process the message
// and sent the response back.
case SM_DELAY:
{
static uint32_t delayStart = 0;
// Initialize delay state on first evaluation.
if( delayStart == 0 )
{
delayStart = CoreTime32usGet();
}
// Required time (i.e. 100ms) has elapsed ?
if( CoreTime32usGet() - delayStart > 100000 )
{
taskState++;
// Reset the start time for evaluation on next delay.
delayStart = 0;
}
break;
}
case SM_READ_RESP:
{
CAN_RX_READ_RESP_U read_resp;
bool rx_valid;
uint8_t rx_node_id;
// Read the response back.
rx_valid = CANRxGet( CAN_RX_MSG_CFG_READ_RESP,
&rx_node_id,
&read_resp.data_u32[0] );
// Save the read value to module data.
if( snode_read_cal_data.cfg_sel <= 6 ) // PWM
{
snode_read_cal_data.cfg_val.pwm_coeff[ snode_read_cal_data.cfg_sel - 1 ] = read_resp.cfg_val_i32;
}
else
if(snode_read_cal_data.cfg_sel <= 12 ) // VSENSE1
{
snode_read_cal_data.cfg_val.vsense1_coeff[ snode_read_cal_data.cfg_sel - 7 ] = read_resp.cfg_val_i32;
}
else // VSENSE2
{
snode_read_cal_data.cfg_val.vsense2_coeff[ snode_read_cal_data.cfg_sel - 13 ] = read_resp.cfg_val_i32;
}
// - Response received/valid ?
// - Response node ID matches that expected ?
// - Updated value matches that selected ?
//
if( ( rx_valid == true ) &&
( snode_read_cal_data.id == rx_node_id ) &&
( read_resp.cfg_sel == snode_read_cal_data.cfg_sel ) )
{
// Update configuration selection to program the next
// parameter (or identify completion of read process).
snode_read_cal_data.cfg_sel++;
}
else
{
// Expected response not received - fail the read operation.
snode_read_cal_data.status = SNODE_CAL_READ_FAIL;
}
taskState++;
break;
}
case SM_DONE_EVAL:
{
// Failure with Calibration read operation ?
if( snode_read_cal_data.status == SNODE_CAL_READ_FAIL )
{
taskState = SM_IDLE;
}
else
{
// Calibration is complete ?
if( snode_read_cal_data.cfg_sel > 18 )
{
// Identify success since all calibration values have been
// successfully read.
snode_read_cal_data.status = SNODE_CAL_READ_SUCCESS;
taskState = SM_IDLE;
}
else
{
// Still more calibration values to read - continue
// with the next request/response sequence.
taskState = SM_READ_REQ;
}
}
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Servo-node receive CAN messages.
///
/// Read up to 'SNODE_CTRL_DATA_MSG_MAX' number of CAN messages into module
/// data.
////////////////////////////////////////////////////////////////////////////////
static void SNodeCANRx( void )
{
CAN_RX_MSG_SNODE_SERVO_STATUS_U servo_status_msg;
CAN_RX_MSG_SNODE_VSENSE_DATA_U vsense_data_msg;
CAN_RX_MSG_SNODE_STATUS_U node_status_msg;
CAN_RX_MSG_SNODE_VERSION_U node_version_msg;
bool rx_valid;
uint8_t msg_idx = 0;
uint8_t src_id;
// Read Servo Status CAN message into module data while message or available
// or 'SNODE_CTRL_DATA_MSG_MAX' message are read for the function's
// execution.
rx_valid = true;
for( ;
( msg_idx < SNODE_CTRL_DATA_MSG_MAX ) && ( rx_valid == true );
msg_idx++ )
{
rx_valid = CANRxGet( CAN_RX_MSG_SNODE_SERVO_STATUS,
&src_id,
&servo_status_msg.data_u32[ 0 ] );
// Data received ?
if( rx_valid == true )
{
// Buffer for Source ID is not valid ?
if( snode_rx_data_id_p[ src_id ] == NULL )
{
// Attempt to setup the buffer in module data.
SNodeBufSetup( src_id );
}
// Buffer for Source ID is valid ?
//
// Note: if buffer for Source ID is still invalid, the setup of
// buffer failed - the received data will be ignored.
//
if( snode_rx_data_id_p[ src_id ] != NULL )
{
snode_rx_data_id_p[ src_id ]->rx_time_us = CoreTime32usGet();
snode_rx_data_id_p[ src_id ]->servo_status = servo_status_msg;
}
}
}
// Read VSENSE Data CAN message into module data while message or available
// or 'SNODE_CTRL_DATA_MSG_MAX' message are read for the function's
// execution.
rx_valid = true;