This article presents an example of a simple application that can be created in Simulink using NXP’s Model-Based Design Toolbox (MBDT) that will be configured to run on a S32K146 board. The application will implement a calculator that can compute the sum or the product of 2 uint2_t (unsigned integer numbers represented on 2 bits).
By implementing this program, one will be able to get familiar with the toolbox NXP provides for embedded programming design. Furthermore, the implementation uses notions of finite-state machines (FSM) - for which knowledge of Stateflow is welcome -, combinatory logic, and dataflow programming.
Hardware
The application is built on the board S32K146 target, which comes with 2 programmable switches that the application will use as input source and an RGB LED which will be used as a form of displaying the output. The board uses the OpenSDA USB port for alimentation and for downloading the executable from the computer. No other hardware is required for programming and deploying this application.
The application can also be built on other NXP boards that come with the required programmable switches and the RGB LED.
Software
All the programming will occur on MATLAB’s Simulink which will require the following add-ons:
Description
The program gets its inputs from the 2 programmable switches presented on the board, SW2 (right user button) and SW3 (left user button). The user will introduce 1 bit at a time, by pressing the 2 switches:
After deciding upon the value, the user will move to the next selection by pressing both switches at the same time.
The values introduced by the user are:
Finally, the program will display the value of the result using the RGB LED present on the board, each color having a different meaning. In this state, moving to the next step by pressing the 2 switches will rerun the whole process of input selection.
During the bit selections the LED will show the current value of the bit:
Green LED will blink between selections to announce the user that the previous bit has been successfully selected.
After the selections, the value will be represented by 5 different colors:
Design
The program is designed to work as a FSM, having the following states:
These states can be seen in the diagram below.
Variables:
State description:
Similar with operand bit selection except for:
Input processing:
The mechanism is represented by a FSM that registers the switches pressed by the user as follows:
These signals are represented through a flipping value, which means that each time the value flips from 0 to 1 or 1 to 0, the program interprets the signal to be turned on.
Note that:
This step by step mechanism is used to compensate for the fact that the signals the 2 switches send to the processor cannot reach the value “on” at the same time no matter how much precision the user has when pressing the 2 buttons.
Notice in the figure above that when the 2 signals are unsynchronized, there is a short time where the program might only 1 signal active. In this situation, the program would interpret 2 different inputs: one where only one of the switches is pressed and 1 where both are pressed. Thus, waiting to see whether the other switch sends a signal too will solve this issue.
Bit set/reset
The set/reset functions are triggered by 2 signals:
Thus these 2 signals are interpreted and put into an AND gate to check that they are active at the same time.
Notice the block between the signal and the gate (U ~= U/z) that is called “Detect change”. This block is used to detect the flip in the value of the signal. Therefore, the output of this block will be 1 only when the value flips.
Display
The LED will be activated by the nextState variable, which is active between 2 states in which input is to be waited from the user.
During the bit selection, all 3 components of the RGB LED will receive the signal of the bit (1 for on, 0 for off).
In the above circuit component, the WHITE AND gate’s output is linked to all components of the RGB LED.
As can be seen in the block above, the result is passed through some comparison blocks to find the interval that fits it:
For each interval there is an AND gate that will output 1 when the result is in the correspondent interval. From there, the signal will be connected to the input of the AND gates that correspond with each color, where the “on” signal will get passed to the LED only when signalDisplay is set. The colors are formed through the following combinations:
All signals that reach the LED are being sent through an OR gate, so that each LED component will light up when one of the signals is sent.
Model
Configuration
The application requires the model to have a configuration block which sets hardware configuration, the compiler and other settings for the application.
The required configurations for this application are presented in the pictures below:
Build and run
If you want the executable to be downloaded on the MCU, the board must be connected to the computer via the USB port. Following the steps above will automatically download the program on the board and it will start running right after.
Demo
Check the video after the conclusion to see how the application should run on the board!
Conclusion
This application is an example of a model that can be implemented on NXP’s Model-Based Design Toolbox. The configuration is done for the application to run on a S32K146 board. This model can be extended to work with bigger numbers or with other operations, it’s up to you, sky is the limit.
I want to thank you for your interest and wish you good luck on implementing this application!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.