Hello manujagrawal,
1.Since, the paths to SDK include and source folder has spaces in them; INC_PATH and SRC_PATH change (as in the attached before and after images) in the "Library/Object/Source files" section of the "S-function builder" library dialog box when I build the model; hence, headers files are not found while building and an error is thrown.
A: You can add the paths even if they have spaces in them, using INC_PATH, etc. The only thing that I have to mention here is that they have to be on the same line - e.g.:
INC_PATH C:\ccr\mbdt_ccr\S32K\src\S32_Platform_SDK\plat form\drivers\inc
So this is a one-liner, and the next path has to be on the next line, etc.
2.Source code and Wrapper files are generated by the s-function builder just outside the folder in which the Embedded Coder generates the code, hence some macro declarations (which are found in the generated code) are not visible to the C file of the generated S-function.
A: When you click on Build inside the S-Function Builder, it generates the files (.c, .tlc, .mex, .m) inside the current folder. I'm not aware of an option from which you can select where to generate the code for your custom S-function. You can try and ask this on the mathworks forum, or you can copy the files generated (after you clicked on Build from the S-Function Builder block) to the model_name_mbd_rtw folder generated by our toolbox (manually), and then build your model.
Another thing I have to mention here is that I'm not aware of an option where you can enter compile options. For example, device_registers.h needs a define to be made (e.g: CPU_S32K144HFT0VLLT or CPU_S32K142, etc.). Unfortunately sfunctionwizard is not officially supported (like mathworks staff member states here).
3.The auto generated ProjectInfo.xml file for the generated code does not contain paths to the files generated by the S-function builder; thus, I am unable to find those files in the S32 design studio project when I import the generated code in it using the project xml file.
A: The ProjectInfo.xml indeed only takes files from inside the folder we generate with our toolbox. If you go for copying the files manually after generating them from the S-function builder, you can call
mbd_s32k14_ds(gcs)
Note: call this from the folder in which you are building the model; do not enter inside the generated <modelName>_mbd_rtw folder.
Note: gcs should be the name of the model you have opened (get current Simulink system).
So if you take the following steps:
- build the model
- manually copy the files built by S-function builder
- run mbd_s32k14_ds(gcs)
You should be able to import successfully the whole project (including your custom blocks) to S32 Design Studio.
As you can see, you'll be unable to do this exactly as you intend to, using S-function builder. The lack of compiler options and generation path settings is a notable drawback. I'll try and get an answer from Mathworks and get back to you on this matter.
Now lets talk alternatives:
First of all, the simplest way to add custom code is by using System blocks:
- System Start - inserts custom code inside of the Start function - function used for initialization, is ran only once, at the beginning
- System Outputs - inserts custom code inside of the Step function - function that is called in a loop - if it's inside the top model, it will be ran in the main loop (at whatever fixed step you have set); if it's inside a subsystem triggered by an ISR block, it will insert code to be ran on that interrupt.
On all of these blocks you have 3 zones:
- Declaration
- Execution
- Exit
Basically Simulink coder uses these 3 'buffers' / zones for Start/Step functions. Declaration is the first, upper zone; Execution is the middle zone while Exit is last. So you can use any of these 2 blocks, with any of these 3 zones to insert custom code, as per your needs. The limitation with this approach is that you can't have inputs/outputs. You can declare data store memory blocks, setting the Storage class to Volatile, to make sure they will be visible and will have the same name as you set them in Simulink. Then you can just use those variables inside the custom code, and set them in Simulink. By doing this, you can have inputs/outputs to your custom code.
Note: you should edit mbd_s32k14_user_copy_required_files.m script to copy whatever files you need for building your code.
Another option is to make a custom library with custom blocks, similar to the library we ship within our MBDT releases. This would be the best option as it provides the flexibility you are looking for, but for details on how to do this we should have some sort of meeting as there are more steps you'll need to be taking and there is the problem of IPs. I'll need to be checking this beforehand.
Hope this helps,
Razvan.