cancel
Showing results for 
Search instead for 
Did you mean: 

How do we run inbuilt DAC of STM32 using MATLAB Simulink?

AAgra.5
Associate

DAC block is not directly available on MATLAB Simulink.

We tried interfacing CubeIDE with Simulink using S-Function builder but were not able to run. We are facing problem in creating S-function.

Any idea on how to do so directly or indirectly would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

Hello @AAgra.5​ 

 I would like to share with you an answer from our partner Mathworks about your issue.

There are multiple ways to add DAC driver in Simulink model to leverage DAC peripheral.

  1. You can create driver blocks following “DriverGuide�? from https://mathworks.com/matlabcentral/fileexchange/39354-device-drivers?s_tid=srchtitle_driver%20blocks_3
  2. Using Custom code blocks. For example, you can refer DC-DC Buck Converter Simulink Model using STM32 BG474EDPOW1 for detailed explanation.

Attached is an example model to use DAC and ADC with Embedded Coder Support Package for STMicroelectronics STM32 Processors that uses second way. Model is created with MATLAB version R2022b.

The attached example uses custom code way to trigger DAC and the voltage is read by ADC. The output port 1 in the model is mapped/configured to generate “DAC_Output�? global variable as shown below:

  1. Select in “Apps -> Embedded Coder -> C Code -> Code Interface -> Default Code Mappings�? to open “Code Mapping – Component Interface�? window at the bottom of model. Go to “Outputs�? tab and map output port 1 to generate “DAC_Output�? global variable.
  2. In the STM32CubeMX project, “DAC�? peripheral is enabled and configured. During the code generation of Simulink model, the code for STM32CubeMX project is also generated. STM32CubeMX generates DAC peripheral initialization function to configure DAC. This initialization function is automatically populated in “main()�? in generated code for model.
  3. Add “System Initialize�? and “System Outputs�? custom code blocks to enable DAC channels at init and trigger DAC to convert “DAC_Output�? respectively.
  4. You can run the model with “Monitor and Tune�? to view the results in Simulink in real-time following the example “Monitoring and Tuning Using STMicroelectronics STM32 Processor Based Boards�?.

 0693W00000YA8UyQAL.pngSpecial thanks dedicated to Mathworks Team for the support.

Firas

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

4 REPLIES 4
FBL
ST Employee

Hello @AAgra.5​ 

You need to Install Simulink Coder Support Package for STM32 Nucleo Boards

Also you may need to check Getting Started with STM32 Nucleo Boards Using Simulink - MATLAB & Simulink (mathworks.com)

Also make sure that you are using a Nucleo board that is supported in the support package.

Hope this helps

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

AAgra.5
Associate

Hii @F.Belaid​ 

Thank you so much for the insights.

But the thing is I am facing a particular problem with DAC convertors as no MATLAB block is available.

As referenced in the link given ADC, Digital Input and Digital Output are applied.

I have tried using S-function builder as the code was available in CubeIDE but was not able to do it.

Could you guide me in the same.

FBL
ST Employee

Well @AAgra.5​, 

I guess you may need to check Level-2 MATLAB S-function API which allows you to create S-function block in Simulink not in STM32CubeIDE.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

FBL
ST Employee

Hello @AAgra.5​ 

 I would like to share with you an answer from our partner Mathworks about your issue.

There are multiple ways to add DAC driver in Simulink model to leverage DAC peripheral.

  1. You can create driver blocks following “DriverGuide�? from https://mathworks.com/matlabcentral/fileexchange/39354-device-drivers?s_tid=srchtitle_driver%20blocks_3
  2. Using Custom code blocks. For example, you can refer DC-DC Buck Converter Simulink Model using STM32 BG474EDPOW1 for detailed explanation.

Attached is an example model to use DAC and ADC with Embedded Coder Support Package for STMicroelectronics STM32 Processors that uses second way. Model is created with MATLAB version R2022b.

The attached example uses custom code way to trigger DAC and the voltage is read by ADC. The output port 1 in the model is mapped/configured to generate “DAC_Output�? global variable as shown below:

  1. Select in “Apps -> Embedded Coder -> C Code -> Code Interface -> Default Code Mappings�? to open “Code Mapping – Component Interface�? window at the bottom of model. Go to “Outputs�? tab and map output port 1 to generate “DAC_Output�? global variable.
  2. In the STM32CubeMX project, “DAC�? peripheral is enabled and configured. During the code generation of Simulink model, the code for STM32CubeMX project is also generated. STM32CubeMX generates DAC peripheral initialization function to configure DAC. This initialization function is automatically populated in “main()�? in generated code for model.
  3. Add “System Initialize�? and “System Outputs�? custom code blocks to enable DAC channels at init and trigger DAC to convert “DAC_Output�? respectively.
  4. You can run the model with “Monitor and Tune�? to view the results in Simulink in real-time following the example “Monitoring and Tuning Using STMicroelectronics STM32 Processor Based Boards�?.

 0693W00000YA8UyQAL.pngSpecial thanks dedicated to Mathworks Team for the support.

Firas

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.