Hi @Maciek ,
I'm coming back with information on integrating device driver code with C function from Mathworks team.
One of the issues I had on C function was the host simulation steps did before code generation - it was trying to build the MCU SDK driver sources with the setup host MEX compiler.
Following the steps from a simple tutorial on this block, it mentioned that in Simulated Target must be specified the sources and header files that included the custom code I wanted to integrate.
Since I just wanted code generation, the settings done in Simulation Target must be cleared(or uncheck the Import Custom Code) and for Code Generation only, the code inside C Function block must be placed inside of “#ifndef MATLAB_MEX_FILE <output code> #endif” macro control.
https://www.mathworks.com/help/simulink/ug/call-and-integrate-external-c-algorithms-into-simulink-us...
As for the external structures/datatypes used in SDK, this was their suggestion (I used as example the GPIO_GetPinsInterruptFlags fct call):
"C Function block still has to able to parse the Code Generation code using host machine MEX compiler.
For the following device driver call, I need to define and declare dependent types, functions, variables used by the call. All declarations must be compatible and understood with selected MEX compiler.
“out = GPIO_GetPinsInterruptFlags(GPIO9);”
So, this is the code I wrote in the C Function block that worked with code generation.
==================== BEGIN =========================
#ifndef MATLAB_MEX_FILE
/* Code generation only. Target specific code. */
#define __I volatile const
#define __O volatile
#define __IO volatile
#define uint32_t unsigned int
#define uint8_t unsigned char
/* Define required types used by C function declaration */
typedef struct {
__IO uint32_t DR;
__IO uint32_t GDIR;
__I uint32_t PSR;
__IO uint32_t ICR1;
__IO uint32_t ICR2;
__IO uint32_t IMR;
__IO uint32_t ISR;
__IO uint32_t EDGE_SEL;
uint8_t RESERVED_0[100];
__O uint32_t DR_SET;
__O uint32_t DR_CLEAR;
__O uint32_t DR_TOGGLE;
} GPIO_Type;
/* Declare external C functions, variables accessed in output code */
extern uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type *base);
extern GPIO_Type* GPIO9;
/* Output code: Call driver function */
out = GPIO_GetPinsInterruptFlags(GPIO9);
#endif
If structure type “GPIO_Type” is not used as any model signal, parameter etc. type, or in another word, it is entirely opaque to the model, then you can typedef it to any type for simplification.
Otherwise, the typedef in the above example code has to be consistent to the original type in external C header file.
Following is the simplified code also worked
==================== BEGIN =========================
#ifndef MATLAB_MEX_FILE
/* Code generation only. Target specific code. */
/* Define required types used by C function declaration */
/* if GPIO_Type is not used as model signal type, and is entirely opaque to model, you can typedef it to any type. */
typedef int GPIO_Type;
/* Declare external C functions, variables accessed in output code */
extern unsigned int GPIO_GetPinsInterruptFlags(GPIO_Type *base);
extern GPIO_Type* GPIO9;
/* Output code: Call driver function */
out = GPIO_GetPinsInterruptFlags(GPIO9);
#endif
I tried both forms on my side and I got the same results when running on the board.
Comparing with System Outputs block, C function block involves some additional adjustments.
I hope this helps,
Best regards,
Alexandra