Newsletters - MATLAB Digest
Stateflow Tips and Tricks
Whilst this article demonstrates how Stateflow Coder can be configured to generate code for the Palm OS, the techniques employed are more universally applicable. Some of these methods are detailed here.
Chart Properties
In the examples given, graphical functions are used as a means of broadcasting an event inside a single Stateflow chart. An alternative approach is to make the event global, but this has the drawback that when a global event is broadcast, it is broadcast to the entire Stateflow machine. If the event is only used in one chart, broadcasting it to the entire machine - i.e. all charts in the model - is inefficient.
For graphical functions to be used in this way, they must be visible outside the chart where they are defined. This option must be enabled by checking 'Export Chart Level Graphical Functions' in the Chart Properties Dialog.
![]() |
| Click on image to see enlarged view |
If this option is left unchecked, grapical functions are defined as 'static' and are local to the chart where they are defined.
Data Types
The C language does not define the word length of its intrinsic data types. That is to say one compiler may compile the int data type to a 16-bit integer, whereas another may compile it to an 8-bit integer.
To isolate the automatically generated code from this type of hardware dependency, Stateflow Coder uses its own data types. For example, uint32_T is a 32-bit unsigned integer.
To compile this code for a particular target, it is necessary to tell the compiler how to interpret the data types used in the generated code. The file tmw2palm.h contains typedefs that specify how the Stateflow Coder data types must be converted when targeting the Palm OS. For example:
typedef UInt32 uint32_T;
makes a Stateflow uint32_T equivalent to the UInt32 used by the C compiler for Palm OS.
Note that default type conversions are provided in the MathWorks supplied file tmwtypes.h. For some compilers, these defaults are appropriate.
Creating a New Target
A Stateflow target can be created from the menu Add/Target.... Various settings can be configured for the new target, and, when it is built, these settings are applied during code generation. The basic code generation process is complete once C code has been created. At this stage, the user can manually run the remainder of the build process, e.g., by invoking a Makefile.
Alternatively, it is possible to complete the build process automatically. This is done by writing a MATLAB M-file that gets executed once code has been generated. To identify whether such a file is available, Stateflow searches for a directory sftarget on the MATLABPATH; if a directory with this exact name is found Stateflow looks inside it for an M-file whose name matches the name of the target, i.e. palm.m in the case of the Palm OS target. The file palm.m, supplied with this article, can be used as a template for new custom targets.
In the Palm OS example, the name of a CodeWarrior project file must be specified as a 'Custom Make File' in Palm target 'Target Options'. This project file is picked up by palm.m and is used to automatically complete the build.
![]() |
| Click on image to see enlarged view |
Conclusions
This article shows how Stateflow and Stateflow Coder can be used to develop an application for specific target hardware-in this case a Palm handheld computer. By developing the application in a high-level graphical environment, a complex algorithm can be specified, refined, and tested in simulation before being run on the target hardware. The benefits of this process include faster, graphical based development, which means less time spent on labour intensive C programming, and more time available for overall architectural design.
The Author
David Maclay is a Principal Engineer with The MathWorks Ltd in Cambridge, UK. He is a member of the Consulting Services Group, specializing in embedded applications and development methodology tools.
Acknowledgments
This article was written in collaboration with the Stateflow development team.
Appendix A - Configuring the Development Tools
Tools required to run the examples described in this article:
- MATLAB 6, Simulink 4, Stateflow 4, Stateflow Coder 4; www.mathworks.com
- CodeWarrior for Palm Computing® Platform, Release 6, full or 'lite' version; www.palmos.com
- The Palm OS® Software Development Kit version 3.5; www.palmos.com
- A Palm™ handheld computer, or,
- The Palm OS® Emulator; www.palmos.com
To make a template Stateflow project available within CodeWarrior:
- Unzip the contents of the file sfpalmexamples.zip
- Assuming that CodeWarrior is installed under c:\Program Files, copy the folder sfpalmexamples\Palm OS Stateflow App to C:\Program Files\Metrowerks\CW for Palm OS R6\Stationery\Palm OS 3.5\Palm OS Stateflow App
You are now ready to use Stateflow to create a Palm OS application.
Appendix B - Creating A New Project
Once the steps for configuring the tools have been completed, a new project can be created by using the files under Palm OS Stateflow App as a template. If these files have been copied to the CodeWarrior Stationery directory, a new project can be created from the File/New menu within the CodeWarrior IDE. Or, you can use Windows Explorer to make a copy of the directory Palm OS Stateflow App.
For reference, the contents of the directory Palm OS Stateflow App and details of all of the source files required to build the application are as follows:
Contents of the top level directory
Stateflow |
Stateflow charts and all associated MATLAB code and Simulink models |
Obj |
Object code |
sftarget |
Files used by Stateflow to build the Palm OS application |
simple.mcp |
CodeWarrior project file for the simple example |
StateflowSrc |
Automatically generated source code |
Src |
Non-automatically generated source code |
Rsc |
Resources used by CodeWarrior, e.g., user interface controls |
Source files required to build the application
sfdemo_palm.c, sfdemo_palm.h |
Automatically generated by Stateflow from sfdemo.mdl |
mainform.c, mainform.h |
Automatically generated by Stateflow to implement behaviour defined by chart mainform within sfdemo.mdl |
sfapp.h |
Header file that must be modified if the name of the model |
sfevent.c |
Manually written file as described in the article |
starter.c, tmw2palm.h, tmwtypes.h |
Standard files that do not normally need to be modified |
© 2000 by The MathWorks, Inc. MATLAB, Simulink and Stateflow are registered trademarks of The MathWorks, Inc. Palm and PalmPilot are trademarks, and Palm OS and Palm Computing are registered trademarks of Palm, Inc. or its subsidiaries. Metrowerks and CodeWarrior are registered trademarks of Metrowerks, Inc. Motorola is a registered trademark of Motorola, Inc.
Store

