What if you could go from an idea to a running application on an NXP target using just a few prompts?
In this tutorial, an AI agent takes a button-controlled RGB LED application from hardware understanding to a validated Simulink and Stateflow model, then to code generation and deployment on the FRDM-A-S32K312 using Model-Based Design Toolbox (MBDT). The workflow continues into generated-code debugging with S32 Design Studio and finishes by adapting the same application to a hardware change, without redesigning the application logic.
Set MATLAB workspace to C:/helloworld. Set the AI agent working directory for this project to C:/helloworld. Use this folder to store and create all additional dependencies for this project.
I want to build a simple demo on the FRDM-A-S32K312 board using the onboard button and RGB LED. Each button press should advance through a color sequence. The first press turns the RGB LED Red for 1 second, the second press turns it Green for 1 second, the third press turns it Blue for 1 second, and then the sequence repeats. After each 1-second indication, the LED should turn off and wait for the next button press. Please analyze the board documentation and schematics, identify the button and RGB LED connections, determine whether they are active-high or active-low, and tell me exactly what logic values are required to detect a button press and turn each LED color on and off. Also confirm that the board can support this application without any hardware modifications. Summarize the GPIO assignments and signal behavior so they can be reused in the next development steps.
Using the hardware information from the previous step, create a Simulink model called s32k312_rgb_led. Do not set a target yet, use plain Simulink for this step. Build the Simulink application logic in Stateflow. Every BTN_ADVANCE button press should advance through a repeating color sequence. The first press shows Red for 1 second, the second press shows Green for 1 second, the third press shows Blue for 1 second, and the sequence repeats. After each 1-second indication, the LED color turns off and the application waits for the next button press. Organize the model cleanly and generate a Stateflow implementation that is easy to understand and ready for the next stages of the workflow.
Using the model created in the previous step, prepare a simulation scenario to validate the application behavior. Configure a 15-second simulation and generate a sequence of 7 button press events distributed over the simulation time. At least one interval in between presses shall be 3 seconds long. Create a comprehensive testcase to identify any possible errors in the Stateflow. Run the simulation, verify that the color sequence behaves as expected, and create suitable visualizations showing the button activity, color transitions, and internal Stateflow state changes. Debug the Stateflow and fix any issues. Summarize the simulation results and highlight any issues found.
Using the validated s32k312_rgb_led model from the previous step, prepare it for deployment on the FRDM-A-S32K312 board. Configure the hardware board as NXP S32K3xx and then set the board as FRDM-A-S32K312 with the S32 Configuration Tools. Add the required Dio blocks for the signals discovered by the hardware analysis and rename the configured signals to RGB_RED, RGB_GREEN, RGB_BLUE and BTN_ADVANCE. Use the GPIO pin names discovered during the hardware analysis, take into account the logic levels to turn the LEDs on or off and detect the button, and preserve the signal names RGB_RED, RGB_GREEN, RGB_BLUE and BTN_ADVANCE. Connect the hardware I/O blocks to the existing application logic and add a variable that counts the number of valid button presses inside Stateflow. When the hardware integration is complete, verify the model configuration, generate the code, build the application, and deploy it to the target board.
Application code is generated and it is currently running on the target. Open the generated code in S32 Design Studio, add a breakpoint in the code right after the line where the application is checking that the button is pressed and the Red LED needs to be turned on, and start the debug session. Once the debug session is started, run the application on the board.
RGB LED pins were rerouted in the hardware. Hardware schematic has been changed and the RGB LED has been added externally to the board on the pins PTA0 - RGB_RED; PTA1 - RGB_GREEN and PTA2 - RGB_BLUE. Back up first the S32 Configuration Tools project associated with the model, then modify the S32 Configuration Tools external project to use the new LED configuration. De-initialize the previous pins for the RGB LED and initialize the new pins. Preserve the application logic and preserve the naming from the previous prompts.
| Symptom | What to inspect |
|---|---|
| Wrong color or inverted LED behavior | Recheck the hardware-analysis result and the discovered active levels used at the hardware interface. |
| One press advances more than once | Inspect the button stimulus and Stateflow press-event handling so that one valid press produces one sequence advance. |
| Color does not turn off after one second | Inspect Stateflow temporal logic and the output assignments on the transition back to the waiting state. |
| Simulation differs from hardware | Compare the deployed Dio mapping and polarity handling with the GPIO facts established during hardware analysis. |
| Breakpoint is not visible in the Breakpoints GUI | Document the observed GUI limitation and verify the intended debug location using the generated source and actual debugger behavior. |
| Problems after rerouting the LEDs | Verify that the former RGB pins were de-initialized and PTA0, PTA1, and PTA2 were initialized in the updated S32 Configuration Tools project. |