@@ -36,6 +36,9 @@ void CiMainGenerator::Generate(DbcMessageList_t& dlist, const AppSettings_t& fsd
36
36
// Load income messages to sig printer
37
37
sigprt.LoadMessages (dlist.msgs );
38
38
39
+ // save max dlc value from message list for printing on the following generation steps
40
+ val_maxDlcValueFromDbcList = dlist.maxDlcValue ;
41
+
39
42
// save pointer to output file descriptor struct to
40
43
// enable using this information inside class member functions
41
44
fdesc = &fsd;
@@ -101,22 +104,56 @@ void CiMainGenerator::Gen_MainHeader()
101
104
fwriter.Append ();
102
105
103
106
fwriter.Append (" // include current dbc-driver compilation config" );
104
- fwriter.Append (" #include < %s-config.h> " , fdesc->gen .drvname .c_str ());
107
+ fwriter.Append (" #include \" %s-config.h\" " , fdesc->gen .drvname .c_str ());
105
108
fwriter.Append ();
106
109
107
110
fwriter.Append (" #ifdef %s" , fdesc->gen .usemon_def .c_str ());
108
111
109
112
fwriter.Append (
110
113
" // This file must define:\n "
111
114
" // base monitor struct\n "
112
- " #include < canmonitorutil.h> \n "
115
+ " #include \" canmonitorutil.h\" \n"
113
116
" \n "
114
117
);
115
118
116
119
fwriter.Append (" #endif // %s" , fdesc->gen .usemon_def .c_str ());
117
120
fwriter.Append (2 );
118
121
119
- for (size_t num = 0 ; num < sigprt.sigs_expr .size (); num++)
122
+ // set macro name for max dlc value based on driver name
123
+ std::string maxDlcMacroName = fdesc->gen .DRVNAME + " _MAX_DLC_VALUE" ;
124
+
125
+ // set macro name for dlc validation
126
+ prt_dlcValidateMacroName = fdesc->gen .DRVNAME + " _VALIDATE_DLC" ;
127
+
128
+ // set macro name for initial data byte value based on driver name
129
+ prt_initialDataByteValueName = fdesc->gen .DRVNAME + " _INITIAL_BYTE_VALUE" ;
130
+
131
+ // print part with max DLC macro
132
+ fwriter.Append (" // DLC maximum value which is used as the limit for frame's data buffer size." );
133
+ fwriter.Append (" // Client can set its own value (not sure why) in driver-config" );
134
+ fwriter.Append (" // or can test it on some limit specified by application" );
135
+ fwriter.Append (" // e.g.: static_assert(TESTDB_MAX_DLC_VALUE <= APPLICATION_FRAME_DATA_SIZE, \" Max DLC value in the driver is too big\" )" );
136
+ fwriter.Append (" #ifndef %s" , maxDlcMacroName.c_str ());
137
+ fwriter.Append (" // The value which was found out by generator (real max value)" );
138
+ fwriter.Append (" #define %s %uU" , maxDlcMacroName.c_str (), val_maxDlcValueFromDbcList);
139
+ fwriter.Append (" #endif" );
140
+ fwriter.Append (1 );
141
+
142
+ // actual macro for final DLC validation in the driver
143
+ fwriter.Append (" // The limit is used for setting frame's data bytes" );
144
+ fwriter.Append (" #define %s(msgDlc) (((msgDlc) <= (%s)) ? (msgDlc) : (%s))" ,
145
+ prt_dlcValidateMacroName.c_str (), maxDlcMacroName.c_str (), maxDlcMacroName.c_str ());
146
+
147
+ // print initial data byte section
148
+ fwriter.Append (1 );
149
+ fwriter.Append (" // Initial byte value to be filles in data bytes of the frame before pack signals" );
150
+ fwriter.Append (" // User can define its own custom value in driver-config file" );
151
+ fwriter.Append (" #ifndef %s" , prt_initialDataByteValueName.c_str ());
152
+ fwriter.Append (" #define %s 0U" , prt_initialDataByteValueName.c_str ());
153
+ fwriter.Append (" #endif" );
154
+ fwriter.Append (2 );
155
+
156
+ for (size_t num = 0u ; num < sigprt.sigs_expr .size (); num++)
120
157
{
121
158
// write message typedef s and additional expressions
122
159
MessageDescriptor_t& m = sigprt.sigs_expr [num]->msg ;
@@ -130,7 +167,7 @@ void CiMainGenerator::Gen_MainHeader()
130
167
fwriter.Append (" // def @%s CAN Message (%-4d %#x)" , m.Name .c_str (), m.MsgID , m.MsgID );
131
168
fwriter.Append (" #define %s_IDE (%uU)" , m.Name .c_str (), m.IsExt );
132
169
fwriter.Append (" #define %s_DLC (%uU)" , m.Name .c_str (), m.DLC );
133
- fwriter.Append (" #define %s_CANID (%#x )" , m.Name .c_str (), m.MsgID );
170
+ fwriter.Append (" #define %s_CANID (%#xU )" , m.Name .c_str (), m.MsgID );
134
171
135
172
if (m.Cycle > 0 )
136
173
{
@@ -139,6 +176,13 @@ void CiMainGenerator::Gen_MainHeader()
139
176
140
177
size_t max_sig_name_len = 27 ;
141
178
179
+ if (!m.frameNotEmpty )
180
+ {
181
+ // do nothing with empty frame, leave only id other relevant constants
182
+ fwriter.Append ();
183
+ continue ;
184
+ }
185
+
142
186
for (size_t signum = 0 ; signum < m.Signals .size (); signum++)
143
187
{
144
188
SignalDescriptor_t& s = m.Signals [signum];
@@ -261,6 +305,12 @@ void CiMainGenerator::Gen_MainHeader()
261
305
// write message typedef s and additional expressions
262
306
MessageDescriptor_t& m = sigprt.sigs_expr [num]->msg ;
263
307
308
+ if (!m.frameNotEmpty )
309
+ {
310
+ // do nothing with empty frame
311
+ continue ;
312
+ }
313
+
264
314
fwriter.Append (" uint32_t Unpack_%s_%s(%s_t* _m, const uint8_t* _d, uint8_t dlc_);" ,
265
315
m.Name .c_str (), fdesc->gen .DrvName_orig .c_str (), m.Name .c_str ());
266
316
@@ -312,7 +362,7 @@ void CiMainGenerator::Gen_MainSource()
312
362
" // Function prototypes to be called each time CAN frame is unpacked\n "
313
363
" // FMon function may detect RC, CRC or DLC violation\n " );
314
364
315
- fwriter.Append (" #include < %s-fmon.h> " , fdesc->gen .drvname .c_str ());
365
+ fwriter.Append (" #include \" %s-fmon.h\" " , fdesc->gen .drvname .c_str ());
316
366
fwriter.Append ();
317
367
318
368
fwriter.Append (" #endif // %s" , fdesc->gen .usemon_def .c_str ());
@@ -345,6 +395,12 @@ void CiMainGenerator::Gen_MainSource()
345
395
// write message typedef s and additional expressions
346
396
MessageDescriptor_t& m = sigprt.sigs_expr [num]->msg ;
347
397
398
+ if (!m.frameNotEmpty )
399
+ {
400
+ // do nothing, no pack and unpack functions for empty frames
401
+ continue ;
402
+ }
403
+
348
404
// first function
349
405
fwriter.Append (" uint32_t Unpack_%s_%s(%s_t* _m, const uint8_t* _d, uint8_t dlc_)\n {" ,
350
406
m.Name .c_str (), fdesc->gen .DrvName_orig .c_str (), m.Name .c_str ());
@@ -641,12 +697,14 @@ void CiMainGenerator::WriteUnpackBody(const CiExpr_t* sgs)
641
697
642
698
if (sgs->msg .Signals [num].Signed )
643
699
{
644
- fwriter.Append (" _m->%s = %s(( %s ), %d);" ,
645
- sname, ext_sig_func_name, expr.c_str (), (int32_t )sgs->msg .Signals [num].LengthBit );
700
+ fwriter.Append (" _m->%s = (%s) %s(( %s ), %d);" ,
701
+ sname, PrintType ((int )sgs->msg .Signals [num].TypeRo ).c_str (),
702
+ ext_sig_func_name, expr.c_str (), (int32_t )sgs->msg .Signals [num].LengthBit );
646
703
}
647
704
else
648
705
{
649
- fwriter.Append (" _m->%s = %s;" , sname, expr.c_str ());
706
+ fwriter.Append (" _m->%s = (%s) ( %s );" , sname,
707
+ PrintType ((int )sgs->msg .Signals [num].TypeRo ).c_str (), expr.c_str ());
650
708
}
651
709
652
710
// print sigfloat conversion
@@ -662,8 +720,10 @@ void CiMainGenerator::WriteUnpackBody(const CiExpr_t* sgs)
662
720
}
663
721
else
664
722
{
665
- fwriter.Append (" _m->%s = %s_%s_fromS(_m->%s);" ,
666
- sgs->msg .Signals [num].NameFloat .c_str (), fdesc->gen .DRVNAME .c_str (), sname, sname);
723
+ fwriter.Append (" _m->%s = (%s) %s_%s_fromS(_m->%s);" ,
724
+ sgs->msg .Signals [num].NameFloat .c_str (),
725
+ PrintType ((int )sgs->msg .Signals [num].TypePhys ).c_str (),
726
+ fdesc->gen .DRVNAME .c_str (), sname, sname);
667
727
}
668
728
669
729
fwriter.Append (" #endif // %s" , fdesc->gen .usesigfloat_def .c_str ());
@@ -721,9 +781,9 @@ void CiMainGenerator::WritePackStructBody(const CiExpr_t* sgs)
721
781
{
722
782
fwriter.Append (" {" );
723
783
PrintPackCommonText (" cframe->Data" , sgs);
724
- fwriter.Append (" cframe->MsgId = %s_CANID;" , sgs->msg .Name .c_str ());
725
- fwriter.Append (" cframe->DLC = %s_DLC;" , sgs->msg .Name .c_str ());
726
- fwriter.Append (" cframe->IDE = %s_IDE;" , sgs->msg .Name .c_str ());
784
+ fwriter.Append (" cframe->MsgId = (uint32_t) %s_CANID;" , sgs->msg .Name .c_str ());
785
+ fwriter.Append (" cframe->DLC = (uint8_t) %s_DLC;" , sgs->msg .Name .c_str ());
786
+ fwriter.Append (" cframe->IDE = (uint8_t) %s_IDE;" , sgs->msg .Name .c_str ());
727
787
fwriter.Append (" return %s_CANID;" , sgs->msg .Name .c_str ());
728
788
fwriter.Append (" }" );
729
789
fwriter.Append ();
@@ -733,21 +793,22 @@ void CiMainGenerator::WritePackArrayBody(const CiExpr_t* sgs)
733
793
{
734
794
fwriter.Append (" {" );
735
795
PrintPackCommonText (" _d" , sgs);
736
- fwriter.Append (" *_len = %s_DLC;" , sgs->msg .Name .c_str ());
737
- fwriter.Append (" *_ide = %s_IDE;" , sgs->msg .Name .c_str ());
796
+ fwriter.Append (" *_len = (uint8_t) %s_DLC;" , sgs->msg .Name .c_str ());
797
+ fwriter.Append (" *_ide = (uint8_t) %s_IDE;" , sgs->msg .Name .c_str ());
738
798
fwriter.Append (" return %s_CANID;" , sgs->msg .Name .c_str ());
739
799
fwriter.Append (" }" );
740
800
fwriter.Append ();
741
801
}
742
802
743
803
void CiMainGenerator::PrintPackCommonText (const std::string& arrtxt, const CiExpr_t* sgs)
744
804
{
745
- // this function will print part of pack function
746
- // which is differs only by arra var name
805
+ // this function will print body of packing function
747
806
748
807
// pring array content clearin loop
749
- fwriter.Append (" uint8_t i; for (i = 0; (i < %s_DLC) && (i < 8); %s[i++] = 0);" ,
750
- sgs->msg .Name .c_str (), arrtxt.c_str ());
808
+ fwriter.Append (" uint8_t i; for (i = 0u; i < %s(%s_DLC); %s[i++] = %s);" ,
809
+ prt_dlcValidateMacroName.c_str (),
810
+ sgs->msg .Name .c_str (), arrtxt.c_str (),
811
+ prt_initialDataByteValueName.c_str ());
751
812
fwriter.Append ();
752
813
753
814
if (sgs->msg .RollSig != nullptr )
@@ -763,7 +824,7 @@ void CiMainGenerator::PrintPackCommonText(const std::string& arrtxt, const CiExp
763
824
{
764
825
// code for clearing checksum
765
826
fwriter.Append (" #ifdef %s" , fdesc->gen .usecsm_def .c_str ());
766
- fwriter.Append (" _m->%s = 0U ;" , sgs->msg .CsmSig ->Name .c_str ());
827
+ fwriter.Append (" _m->%s = (%s) 0 ;" , sgs->msg .CsmSig ->Name . c_str (), PrintType (( int )sgs-> msg . CsmSig -> TypeRo ) .c_str ());
767
828
fwriter.Append (" #endif // %s" , fdesc->gen .usecsm_def .c_str ());
768
829
fwriter.Append ();
769
830
}
@@ -779,8 +840,10 @@ void CiMainGenerator::PrintPackCommonText(const std::string& arrtxt, const CiExp
779
840
if (sgs->msg .Signals [n].IsSimpleSig == false )
780
841
{
781
842
// print toS from *_phys to original named sigint (integer duplicate of signal)
782
- fwriter.Append (" _m->%s = %s_%s_toS(_m->%s);" ,
783
- sgs->msg .Signals [n].Name .c_str (), fdesc->gen .DRVNAME .c_str (),
843
+ fwriter.Append (" _m->%s = (%s) %s_%s_toS(_m->%s);" ,
844
+ sgs->msg .Signals [n].Name .c_str (),
845
+ PrintType ((int ) sgs->msg .Signals [n].TypeRo ).c_str (),
846
+ fdesc->gen .DRVNAME .c_str (),
784
847
sgs->msg .Signals [n].Name .c_str (), sgs->msg .Signals [n].NameFloat .c_str ());
785
848
}
786
849
}
@@ -796,7 +859,7 @@ void CiMainGenerator::PrintPackCommonText(const std::string& arrtxt, const CiExp
796
859
continue ;
797
860
}
798
861
799
- fwriter.Append (" %s[%d] |= %s ;" , arrtxt.c_str (), i, sgs->to_bytes [i].c_str ());
862
+ fwriter.Append (" %s[%d] |= (uint8_t) ( %s ) ;" , arrtxt.c_str (), i, sgs->to_bytes [i].c_str ());
800
863
}
801
864
802
865
fwriter.Append (" " );
@@ -810,7 +873,7 @@ void CiMainGenerator::PrintPackCommonText(const std::string& arrtxt, const CiExp
810
873
sgs->msg .CsmSig ->Name .c_str (), arrtxt.c_str (), sgs->msg .Name .c_str (),
811
874
sgs->msg .Name .c_str (), sgs->msg .CsmMethod .c_str (), sgs->msg .CsmOp );
812
875
813
- fwriter.Append (" %s[%d] |= %s ;" , arrtxt.c_str (), sgs->msg .CsmByteNum , sgs->msg .CsmToByteExpr .c_str ());
876
+ fwriter.Append (" %s[%d] |= (uint8_t) ( %s ) ;" , arrtxt.c_str (), sgs->msg .CsmByteNum , sgs->msg .CsmToByteExpr .c_str ());
814
877
815
878
fwriter.Append (" #endif // %s" , fdesc->gen .usecsm_def .c_str ());
816
879
fwriter.Append ();
0 commit comments