Python code Simulink HDL coder

7 views (last 30 days)
Diede Hilhorst
Diede Hilhorst on 21 Feb 2024
Commented: Hassaan on 21 Feb 2024
I got a question about Simulink.
HDL coder can generate HDL code for an FPGA from a Simulink model. In Simulink you can also add your custom Python code in the model. But how would HDL coder generate HDL code for a FPGA if there is a Python script inside of the Simulink model?
Kind regards,
Diede

Answers (1)

Hassaan
Hassaan on 21 Feb 2024
Edited: Hassaan on 21 Feb 2024
HDL Coder cannot directly generate HDL code for an FPGA from a Simulink model that contains Python code blocks. This is because HDL Coder is designed to work with Simulink blocks and signals that have well-defined representations in hardware description languages (HDLs) like Verilog or VHDL. Python, being a general-purpose scripting language, doesn't have a direct translation to hardware constructs. To utilize HDL Coder for FPGA design, you will need to ensure that all parts of your model are compatible with HDL code generation, which may involve replacing or bypassing the Python scripts with supported Simulink blocks or MATLAB functions.
External Python Functions:
  • Move the Python code logic to external Python functions outside the Simulink model.
  • Call these functions from Simulink using the External Mode simulation option.
  • HDL Coder will generate code for the Simulink blocks, and you can separately implement the Python logic in your external code.
2. S-Functions:
  • Create an S-Function block in Simulink that encapsulates your Python code.
  • Implement the S-Function using C or C++ code that interacts with the Python interpreter through libraries like PyBind11 or Cython.
  • HDL Coder can then generate HDL code for the S-Function block, treating it as a black box with defined inputs and outputs.
3. Simulink Coder (Alternative):
  • Consider using Simulink Coder instead of HDL Coder if your primary goal is to generate code for embedded systems (e.g., C/C++).
  • Simulink Coder can co-simulate Simulink models with external Python code using the External Mode simulation option.
Choosing the best approach depends on several factors:
  • Complexity of Python code: If the Python code is simple and doesn't interact heavily with Simulink signals, external functions might suffice.
  • Hardware integration requirements: If the Python code needs to interface with specific hardware components, S-Functions might be necessary.
  • Target platform and coding language: If your target platform doesn't support hardware implementation of Python or requires C/C++ code, Simulink Coder could be a better option.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
Diede Hilhorst
Diede Hilhorst on 21 Feb 2024
Thank you for your fast and detailed reply, I really appreciate it!
Hassaan
Hassaan on 21 Feb 2024
@Diede Hilhorst You are welcome.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!