Incorporating C2000 driver library code in Simulink Stateflow.

49 views (last 30 days)
We are developing state machine of our project using Simulink State Flow. The project is based on TI Delfino TMS32028377S DSP. There are discrete states of the system like initialization, standby, run and fault. In each state the DSP needs to execute some hardware related tasks that will need the use of driver library functions. For example in the initialize state we need to call various driver library functions to intialize various device peripherals. Similarly in the running state it is required to read various ADCs, performing some conversions,doing some output operations and then reporting the data over some communication interface such as SPI/UART. So the majority code in the state machine functions needs access to hardware functions provided as driver library. From my understanding state flow allows calling of Matlab functions and Simulink functions code but I am not sure about incorporating the C code that makes usage of device driver library functions.
The following two approaches come to my mind:
1- Manually provide all the states transition data via state flow inputs and take out a state variable from Simulink state flow that indicate the current state and then based on the present state, I call appropriate function that are written outside the state machine code. So I will basically be processing only the state transition logic via state flow generated code. The actual code will be running outside the state flow code.
2- Make use of simulink function calls and using device library blocks, perform the hardware functions. This will be a complete redesign on our side as our exisiting code is all based on drive library. We are trying to migrate it to state flow only for better testing and future developments.
Kindly guide if there is a better approach to solve this requirement.

Answers (1)

Abhishek Kumar Singh
Abhishek Kumar Singh on 24 Jan 2024
Hi Sajid,
I understand that you want to maintain a separation of concerns in your design.
For your case, I assume that many common hardware-related tasks you wish to achieve, such as initialization, reading from peripherals and interfaces- including ADC, digital I/O, ePWM, SPI, I2C, and more, the C2000 Microcontroller Blockset provides dedicated blocks that are specifically designed for these purposes and those can be achieved though correct configuration of the dedicated blocks.
These blocks are tailored for the Texas Instruments C2000 series microcontrollers and are optimized to work with the hardware peripherals without the need for writing custom C code or S-function wrappers. It allows for algorithm partitioning with seamless inter-processor communication and efficient co-processor (Control Law Accelerator) workflows.
For instance, in your initialization state, instead of calling driver library functions directly, you can utilize the initialization blocks provided by the C2000 blockset to configure your device peripherals. Similarly, for the running state, you can use the dedicated blocks for ADCs to read sensor data, PWM blocks to control actuators, and communication interface blocks for SPI/UART to manage data transmission, all directly within your Simulink model.
For rare cases where you might need to implement specialized functionality that is not available as a dedicated block within the C2000 blockset, you can indeed use Custom Code blocks. Refer to this MATLAB Answer where such a solution was implemented:
Also, I would like to point that from R2023a, the C2000 Microcontroller Blockset has become the successor to the Embedded Coder Support Package for Texas Instruments C2000 Processors and SoC Blockset Support Package for Texas Instruments C2000 Processors.
You can refer to the documentation of the same: https://www.mathworks.com/help/ti-c2000/index.html
I hope it helps!

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!