필터 지우기
필터 지우기

How to properly generate s-functions for functions from a static library?

조회 수: 5 (최근 30일)
Nils Herjürgen
Nils Herjürgen 2018년 6월 8일
댓글: Nils Herjürgen 2018년 6월 15일
Hello,
i work with an Infineon TriCore and Embedded Coder. I want to include hardware access into my Simulink project (e.g. toggle a pin).
I have a static library (library.a) containing low level drivers (e.g. there are functions like toggle_pin(Port port, Pin pin)). The data Types "Port" and "Pin" are also somewhere defined in the static libary.
My approach was to include this static library into Configuration Parameters -> Simulation Target -> Custom Code -> Libraries. Then i want to create a c-function like e.g. sfun_toggle_pin(int port, int pin). Within this function a statemachine maps the integer arguments (port and pin) to the data types Port and Pin and calls my toggle_pin-function from the library. How can i create a s-function block that calls sfun_toggle_pin? Do i have to tell this s-function block about the static library? Or is it enough to tell it Simulink in the configuration parameters?
The main functionality will be implemented in Stateflow. Later, more complicated functions shall be implemented, like UART. For example, if we get into a certain state in stateflow a message shall be send via UART. It would be nice to just have a s-function Block with a mask where the UART parameters could be set. Is something like this generally possible with a defensible amount of work?
Thank you in advance!

답변 (1개)

Mark McBroom
Mark McBroom 2018년 6월 9일
Use the legacy_code tool in Simulink to create an S-Function that calls externally written C code. There are options in the legacy_code tool for specifying .c/.h/.lib files which contain the external C code. Note that you may want to have 2 implementations of your toggle_pin() function, one for simulating your model on the host computer, and a second for the target hardware. The host computer version might write the state of the pin to the display or a file, while the target version would access actual hardware registers.
  댓글 수: 1
Nils Herjürgen
Nils Herjürgen 2018년 6월 15일
Hi Mark, thanks for your response. Sorry for my late answer, i was busy with another project. I want to clarify what i want with an example:
Imagine i have a Microcontroller and an operating system running on this mikrocontroller. Thus, i already have plenty of .c and .h files representing the OS. I compile and flash using the TASKING compiler in the Eclipse IDE.
Ok, now i want to add functionality, like e.g. a simple PWM-control of a fan connected to a pin of the uC. Until now, i directly coded this functionality in C and compiled it with the OS and flashed to the uC. But now i want to develop modelbased software. I want to create and simulate my Fan controller in Simulink and if it works i want to use the Embedded Coder to directly generate (NOT compile) C-Code which i want to copy into my Eclipse/Tasking environment and compile here with the Tasking Compiler.
Now, somehow my controller needs to access the pin, where the fan is connected to. I want to do this directly in Simulink (for a coouple of reasons, please accept that ;) ). My approach was to take an S-Function which calls the corresponding C-Code (e.g. toggle_pin(Pin pin, Port port)). This c-code depends on a number of headers, in which assembler calls are performed. Those are compiler dependent and cannot be compiled with gcc or VisualC++, just with TASKING.
The Problem: I can not find a possibility to just generate a S-Function WITHOUT compiling the source files to a mex-function. I just want a Block in Simulink representing the call to my toggle_pin C-function. OF course, i can not simulate with uncompiled S-Functions, but i don't want that anyway. I activated "Generate Code Only" in Code Generation Options but that didn't help. I also read this in the doc: "When an S-function is not inlined with a TLC file, the Simulink Coder build process must compile the S-function's source file." I concluded, if i have a TLC file i don't need to compile the source files. I did an wrapper s-function and a TLC file but didn't get it to run without compiling. There is always the error "S-function does not exist".
Can i somehow just generate (NOT compile) code with s-functions without compiling them to mex-files?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by