How do I access STM32 HAL headers in Simulink S-Function-Builder?

I'm using an STM32 Nucleo L476RG working on a controller for an inverted pendulum.
In order to collect RPM readings from the motor's encoder I tried to create an S-Function with the S-Function-Builder block in order to initialize registers and obtain RPM readings. However, when I try to include STM32 HAL header files I get errors like 'fatal error C1083' with messages like '"stm32l4xx_hal_tim.h": No such file or directory'.
I was under the assumption I'd be able to use HAL headers when using the 'Simulink Coder Support Package for STMicroelectronics Nucleo Boards' add-on. Is there anything else I need in order to use them?

1 件のコメント

Elizabeth
Elizabeth 2025 年 5 月 2 日
Were you abe to figure this out? I am also trying to use STM functions in c code in a simulink model and am finding it difficult.

サインインしてコメントする。

回答 (1 件)

Mark McBroom
Mark McBroom 2022 年 3 月 30 日

1 投票

What is the purpose of the C Coder you are trying to integrate with S-Function Builder? The purpose of S-Function builder is to bring algorithmic code into Simulink, not code that access STM32-specific peripherals. S-Function builder compiles and links C/C++ code into a Windows mex functionn ( DLL ) . STM32 specific code/headers will not compile. There are a couple of ways to work around this.
  1. If the C code is mostly algorithmic, you can use preprocessing macros to ignore the STM32-specific code when compiling the code for S-Function builder.
2. If the code is for a device driver, you can create a "stub" version of the C function that provides a simulation of the device... for example if it is a function to read A2D, for simulation the function could just return a number between 0 and 255. And then when compiling code for the target processor, replace the stub C function with the STM32 version of the C function.

1 件のコメント

Arun
Arun 2024 年 10 月 25 日
Hello Mark
Does it mean we cannot directly access STM32 registers directly from MATLAB/SImulink?
I am also looking to create s-function/ Builder to access DWT CYCCNT of STM32 so I can get CPU utilization time, Is it possible to access register DWT directly from MATLAB/Simulink>?

サインインしてコメントする。

製品

リリース

R2021b

質問済み:

2022 年 3 月 24 日

コメント済み:

2025 年 5 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by