C/C++ code integration tools introduction
MicroDAQ toolbox for Scilab allows easy integration of legacy or custom C/C++ code making Xcos model creation more flexible. User can automatically generate Xcos blocks which executes custom C/C++ code. Moreover block code can be debugged with Code Composer Studio like during normal DSP application development. User can optimize some parts of Xcos model by repleacing Xcos standard block with custom block which contains optimized C code for faster execution. Especially C/C++ programmers will benefit from code integration tools giving easy way to write custom Xcos block which sometimes is easer then making it with standard Xcos blocks. Together with 'Execution profiling' feature which allows precisly determine model execution times code integration tools can be used creating high performance real-time control and measurement applications.
Define block input/output ports and block properties
XCos block have input/output ports and properties which user access after double click on it. Block properties are set and after XCos model compilation they cannot be changed. On the other hand input/output block is changing during every step of model execution. Block code contains three functions which are executed depending on model stage execution. The init() function is executed after model is loaded on target. This function should be used to allocate memory, initialize hardware etc. The most important function is inout() which is executed on every model step. The end() function is called at the end of model execution when user hit XCos Stop button. This function should free allocated resources and de-initialize hardware if is used.
Use mdaqBlock() to initialize MicroDAQ block structure with default values
Modify structure to meet your input/output and block parameters requirements
Use mdaqBlockAdd() function to generate block macros and C source code
Modify generated C source file by adding code to init(), inout() and end() functions
DSPLib library (installed with MicroDAQ toolbox) can be used to have access to MicroDAQ hardware e.g create custom ADC block. In order to do that header file needs to be include in a generated file. The DSPLib headers files are located in mdaqToolboxPath() + "etc\mdaqlib\" toolbox directory.
If you use external code copy it to userlib directory
Modify generated block macros (e.g add event input/output, change block input/output port value type)
In order to modify block input/output ports and properties structure which describes block needs to modified and mdaqBlockAdd() shall be run again. In order to modify e.g. input port value type e.g. to int32 generated block macros needs to be modified. The mdaqBlockAdd() generated two block macros, the one with suffix '_sim' contains Scilab code which is executed during simulation. This code can be modified to reflect DSP C code and can be used in simulation.
Use mdaqBlockBuild() to rebuild modified/added C/C++ sources
If you want to debug your code with Code Composer Studio use mdaqBlockBuild(%T)
The mdaqBlockBuild() called with %T argument will compile user block library with debug symbols and without optimization
Restart Scilab to reload 'MicroDAQ User' palette
Place newly created block into XCos diagram
If block C/C++ code needs modification - modify code and use mdaqBlockBuild() (no neet to restart Scilab)
Build and deploy your model on MicroDAQ DSP
Generated C code
/* Generated with MicroDAQ toolbox ver: 1.0. */ #include "scicos_block4.h" extern double get_scicos_time( void ); /* This function will executed once at the beginning of model execution */ static void init(scicos_block *block) { /* Block parameters */ double *params = GetRparPtrs(block); /* param size = 1 */ double param1 = params[0]; int param2_size = 2; double *param2 = ¶ms[1]; int param3_size = 4; double *param3 = ¶ms[3]; /* Add block init code here */ } /* This function will be executed on every model step */ static void inout(scicos_block *block) { /* Block parameters */ double *params = GetRparPtrs(block); /* param size = 1 */ double param1 = params[0]; int param2_size = 2; double *param2 = ¶ms[1]; int param3_size = 4; double *param3 = ¶ms[3]; /* Block input ports */ double *u1 = GetRealInPortPtrs(block,1); int u1_size = GetInPortRows(block,1); /* u1_size = 1 */ double *u2 = GetRealInPortPtrs(block,2); int u2_size = GetInPortRows(block,2); /* u2_size = 1 */ /* Block output ports */ double *y1 = GetRealOutPortPtrs(block,1); int y1_size = GetOutPortRows(block,1); /* y1_size = 1 */ /* Add block code here (executed every model step) */ } /* This function will be executed once at the end of model execution (only in Ext mode) */ static void end(scicos_block *block) { /* Prameters */ double *params = GetRparPtrs(block); /* param size = 1 */ double param1 = params[0]; int param2_size = 2; double *param2 = ¶ms[1]; int param3_size = 4; double *param3 = ¶ms[3]; /* Add block end code here */ } void mdaq_test(scicos_block *block,int flag) { if (flag == 1){ /* set output */ inout(block); } else if (flag == 5){ /* termination */ end(block); } else if (flag == 4){ /* initialisation */ init(block); } }