time delay in simulink to be used with c code generation

28 visualizaciones (últimos 30 días)
Sadia
Sadia el 22 de Ag. de 2024
Comentada: Umar el 29 de Ag. de 2024
I am trying to interface a STM32F411RE board with a 4 digital seven segment display via simulink and embedded coder support. To do so i need to introduce various time delays for example: the clock pin goes high and then after 2 micro-seconds it goes low etc. but i an unable to model such time delay. I have tried introducing a for loop that does nothing but the code generator optimizes the code and omits the for loop as it is not providing any fruitful output. is there any way that i can turn this optimization off so that it keeps the for loop? I have tried another way where i introduce a dummy variable inside for loop but the c code generator changes the order of the commands and moves the for loop in the start and not where i want the delay to be. please let me know if anyone has dealt with a similar problem? memory block/delay block also doesn't work.
Thank you!!
  14 comentarios
Sadia
Sadia el 28 de Ag. de 2024
Dear Umar,
Thank you very much for your detailed reply.
My problem is whenever I try to include the a C function (using coder.ceval etc) in a Matlab function block, initially it gives errors such as cannot find header files such as "stm32f4xx_hal_conf.h" etc. Then when i include all the directories, I get some assembler errors such as:
operand mismatch for ds, Error: no such instruction: `dmb 0xF etc.
I am not very well versed with C and assembly language so I don't really understand this. I have tried googling it but no luck specifically in Matlab.
If you have any idea about these errors let me know. Thankyou.
Umar
Umar el 29 de Ag. de 2024

Hi @Sadia,

Please see my response to your comments below.

To address your first query regarding, My problem is whenever I try to include the a C function (using coder.ceval etc) in a Matlab function block, initially it gives errors such as cannot find header files such as "stm32f4xx_hal_conf.h" etc

The error regarding missing header files such as "stm32f4xx_hal_conf.h" indicates that the MATLAB environment cannot locate the necessary STM32 HAL library files. This is often due to incorrect paths or missing configurations in the MATLAB setup.

Now, addressing your second query, operand mismatch for ds, Error: no such instruction: `dmb 0xF etc.

The assembler errors, such as "operand mismatch for ds" and "no such instruction: `dmb 0xF," suggest that the assembly code generated by the C code is not compatible with the target architecture or that the compiler settings are not correctly configured for the STM32 platform.

My suggestion would be at this point would be integrating the C code into MATLAB, test the C code in a standalone environment (e.g., an STM32 IDE) to ensure that it compiles and runs correctly. This can help isolate whether the issue is with the C code itself or the integration with MATLAB.

After reading your comments about “I am not very well versed with C and assembly language so I don't really understand this. I have tried googling it but no luck specifically in Matlab.”

I may suggest how exactly are you compiling code since I have no clue what is going on your side of “ interface a STM32F411RE board with a 4 digital seven segment display via simulink

So, please provide some detailed instructions and let’s follow a step by step process in order to accomplish your goal. Otherwise, exchanging these ideas back and forth and not knowing how are you interfacing your board with simulink will not help reach your target goal. Hope, you understand.

Iniciar sesión para comentar.

Respuestas (1)

Saurav
Saurav el 23 de Ag. de 2024
Hey @Sadia,
Adjusting code generation optimization settings can help embedded system applications manage time delays. Here are some steps that will help resolve the timing issues:
1. Disable Block Reduction:
  • Open the Model Configuration Parameters dialog box from the Settings. Then, navigate to the Simulation Target tab.
  • In the advanced parameters section, uncheck the option labeled Block reduction. This will prevent the code generator from simplifying blocks that are crucial for timing accuracy.
2. Modify Optimization Levels:
  • Go to the Code Generation tab and then select Optimization.
  • Consider setting the Optimization levels option to Minimum. This setting reduces the extent to which the code is optimized, preserving your intended logic.
  • Alternatively, it can be set to Balanced with readability or Maximum if those settings better suit the needs.
  • If either Balanced with readability or Maximum is chosen, enable Specify custom optimization and uncheck the option Eliminate superfluous local variables (expression folding). This prevents the code generator from folding expressions that might interfere with timing logic.
These settings can be carefully adjusted to maintain code structure and time delays, ensuring the application works on the hardware.
Let me know if this workaround helps or if any further help is needed!
  3 comentarios
Saurav
Saurav el 23 de Ag. de 2024
Can You try this if it works:
  1. Create a MATLAB Function Block:
  • In your Simulink model, add a MATLAB Function block.
  • Double-click the block to open the editor and define the delay function. Use coder.ceval to call a custom C function:
function delayMicrosecondsWrapper(microseconds)
%#codegen
coder.cinclude('delay.h');
coder.ceval('delayMicroseconds', microseconds);
2. Write the Custom C Code:
  • Create a file named delay.c with the following content:
#include "stm32f4xx_hal.h"
void delayMicroseconds(uint32_t microseconds) {
volatile uint32_t count;
const uint32_t delay = microseconds * (SystemCoreClock / 1000000) / 5; % Adjust as necessary
for (count = 0; count < delay; count++) {
__asm volatile("nop");
}
}
3. Create a Header File:
  • Create a delay.h file to declare the function:
#ifndef DELAY_H
#define DELAY_H
#include <stdint.h>
void delayMicroseconds(uint32_t microseconds);
#endif
4. Include Custom Code in Simulink:
  • In the Model Settings under Code Generation > Custom Code, specify the delay.c and delay.h files in the appropriate sections for source and header files.
  • In the Source files field, enter the name of your custom source file, e.g., delay.c. If the file is not in the current working directory, provide the full path or relative path from the model file location.
  • In the Header files field, enter the name of your custom header file, e.g., delay.h. Similar to source files, provide the full path if necessary.
  • If you need to include any specific initialization code or additional functions, you can add them in the Include directives field. For example, you might add #include "delay.h" if it is not already included in your code.
Sadia
Sadia el 26 de Ag. de 2024
hello,
Thank you for your reply. I have actually already tried doing this but initially it gives errors such as cannot find header files such as "stm32f4xx_hal_conf.h" etc. Then when i include all the directories, I get some assembler errors :
operand mismatch for ds, Error: no such instruction: `dmb 0xF etc.
I am not very well versed with C and assembly language so I don't really understand this. I have tried googling it but no luck specifically in Matlab.
If you have any more leads do let me know. Thankyou.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Simulink Design Optimization en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by