Please enable JavaScript to view this site.

Waijung 2 User Guide

Navigation: ESP32 target > Advance topics

Modify Simulink generated C code

Scroll Prev Top Next More

Objective

This tutorial will show how to edit the C code that gets generated by using the Waijung2 block-set. The user will also be introduced to ESP-IDF development environment as well.

Prerequisites

Setup ESP-IDF

Step1: Setup ESP-IDF Command Console

Prepare the model file

This tutorial will use the ADC demo for the demonstration. Check How to use demo file?

Demonstration

Editing the C code.

Step 1: Build the model file.

First set the Build Options to Generate code in the Waijung2_target setup block. By setting this option the C code will be generated but will not get uploaded to the ESP32 board.

modify_simulink_generated_c_code_1

Use the 'Build' button in Matlab Simulink to build the current model. This will generate the C code and relevant build files in the Matlab 'Current Folder'.

modify_simulink_generated_c_code_2

Step 2: Open the generated C code from your favorite editor.

The following three C files are responsible for the program generated by your Matlab model file.

modify_simulink_generated_c_code_3

Step 3: Edit the C code.

Open the esp32_adc_demo.c file. We are are going to change the threshold from which the LED status is determined. Current value is set to 2047 from Matlab. Change it to 3000 as shown in the image.

modify_simulink_generated_c_code_4

Uploading the edited code using ESP-IDF

Step 1: Open pre-configured CMD in Setup ESP-IDF

Step 2: Change the directory to the esp32_adc_demo_esp32_build_system folder

 cd [adc_demo_folder_path]\esp32_adc_demo_esp32_build_system

Step 3: Compile and Upload code

Type the following command in the CMD window.

 idf.py -p COM[COM port number] flash

modify_simulink_generated_c_code_5

Some useful commands for the ESP32 CMD.

To upload and monitor the program.

 idf.py -p COM[COM port number] flash monitor

To just monitor the program.

  idf.py -p COM[COM port number] monitor

To open the configuration menu

  idf.py menuconfig

Reference

Check out the official ESP-IDF.

 

Copyright 2024 Aimagin Co.,Ltd. Rev.1659