Microc Tutorial
Microc Tutorial
Microc Tutorial
MicroC Tutorial
Before using the information in this manual, be sure to read the Notices section of the Help or the PDF file available from Help > List of Books.
This edition applies to Telelogic Statemate 4.5 and to all subsequent releases and modifications until otherwise indicated in new editions. Copyright IBM Corporation 1997, 2008. US Government Users Restricted RightsUse, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
ii
Contents
Exercise 2 - Defining Textual Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Exercise 3 - Defining Design Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Exercise 4 - Constructing Statecharts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Drawing States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Naming States. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Drawing Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Labeling Transitions and Placing Labels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 25 25 26
Exercise 5 - Constructing a Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Exercise 6 - MicroC Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Exercise 7 - Using the Test Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Statemate
iii
iv
MicroC Tutorial
This tutorial walks through the process of creating MicroC code using Statemate. The example project is a traffic light with red, yellow, and green indicators. The design is simple so you can fully concentrate on MicroC code generation, rather than on the design problem itself. This tutorial contains the following topics:
Tutorial Setup Exercise 1 - Constructing an Activity Chart Exercise 2 - Defining Textual Elements Exercise 3 - Defining Design Attributes Exercise 4 - Constructing Statecharts Exercise 5 - Constructing a Panel Exercise 6 - MicroC Code Generation Exercise 7 - Using the Test Driver
Statemate
Tutorial Setup
Prior to starting the exercises, you must set up your system and create a project within Statemate following these steps: 1. Create a folder/directory called stm_projects on your system in which to store your project data. 2. To begin to create a project prior to starting the lab exercises, start Statemate. a. From Windows select Start > Telelogic > Statemate > Statemate <version>. b. From UNIX select $STM_ROOT/run_stmm.bat. The Statemate Main windows opens.
Statemate Main Window (No Project Open)
MicroC Tutorial
Tutorial Setup 3. Select File > New Project. The Create New Project window opens.
Create New Project
Clicking on the '' opens an explorer window for browsing. The explorer window
4. Enter the following data in the Create New Project window. Refer to the figure.
Statemate
9. Click OK. A Question window appears asking if you want to create the Workarea.
10. Click Yes. The work area appears. Refer to the following figure.
MicroC Tutorial
Tutorial Setup
Statemate Main Window (Project Open)
Statemate
MicroC Tutorial
Exercise 1 - Constructing an Activity Chart Use the following steps to create the activity chart: 1. Click the Graphic Editors icon in the Statemate Main window.
Graphics Editor Icon
Statemate
Creating a Traffic Light in MicroC 2. Create an activity chart called TRAFFIC_LIGHT_SS based on the selections shown in the figure.
Open/Create Chart
MicroC Tutorial
Exercise 1 - Constructing an Activity Chart 3. Click New to create and open a new diagram. Refer to the figure. You can now edit the chart. 4. Click Create Internal Activity .
Statemate
2. Click in the location where you want the Activity name or type the Activity name and then place it where you want.
Note: Enter the names for objects using lowercase letters. Statemate, where
appropriate, automatically performs the conversion to uppercase letters. Do not enter words in uppercase letters. Doing so may prevent you from making further selections. 3. Type the name traffic_light_ss. You can select the standard pointer drag the traffic_light_ss name to place it, if desired. 4. Click Create External Activity . and click-and-
5. Draw the external activities for the TRAFFIC_LIGHT_SS. The same drawing rules apply for drawing and naming external activities. Use the following figure as a guide.
10
MicroC Tutorial
Statemate
11
7. Draw a flow line from the external activity called 'INPUTS' to the internal activity called TRAFFIC_LIGHT_SS. Refer the following sections.
Note
In order to create flow lines with angles, you may click at each location an angle is desired prior to clicking/releasing the middle mouse button.
2. Click on the flow line that you want to label and type the name change or type the name change before clicking on the flow line and then place the label onto the flow line. 3. Click Pointer , and move the label as desired.
4. Draw a flow line from the internal activity called TRAFFIC_LIGHT_SS to the external activity OUTPUTS and label the flow red_light. 5. Draw a flow line from the internal activity called TRAFFIC_LIGHT_SS to the external activity OUTPUTS and label the flow yellow_light. 6. Draw a flow line from the internal activity called TRAFFIC_LIGHT_SS to the external activity OUTPUTS and label the flow green_light. 7. Click Create Control Activity .
8. Draw and name the control activity, traffic_light_ctrl Use the same drawing techniques to draw the other activities. 9. Select File > Save from the chart and then File > Exit.
12
MicroC Tutorial
Element Matrix
Statemate
13
Creating a Traffic Light in MicroC 2. Select the element called CHANGE from within the element matrix. 3. Select Edit > Properties. This invokes the Properties window for this element. Refer to the following figure.
Note
Double-clicking on the element name also opens the properties window for this element.
Properties - CHANGE
4. Define the textual element CHANGE using the following steps: a. Change the Type: of the element from Information Flow to Event. Refer to the figure. b. c. Click Save Exit .
14
MicroC Tutorial
Type Menu
5. Set the Type: to Condition for the other three elements (RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT), and save the settings. 6. Open the external activities, INPUTS and OUTPUTS, and set the Type: to Environment. Refer to the figure. a. Change the Type from External to Environment b. c. Click Save Exit
Statemate
15
Type Menu
16
MicroC Tutorial
Note: Double-clicking on the element name also opens the properties window for this
element.
TRAFFIC_LIGHT_SS Properties
Statemate
17
Creating a Traffic Light in MicroC 2. Select the Design Attributes tab within the properties window. a. Change the Value of Type to Task. Refer to the figure. b. c. Click Save Exit
Design Attribute Task
Value Menu
18
MicroC Tutorial
Exercise 3 - Defining Design Attributes 3. Open the properties window for the element called CHANGE and select the Design Attributes tab. Refer to the figure.
Design Attributes CHANGE
Choose Icon
Statemate
19
Creating a Traffic Light in MicroC 4. Select the Name Its Task and then click Choose to select the task for the event change. Refer to the figures. a. The Choose window appears. Click Dismiss to close the window. b. c. Click Save. Exit.
'Its Task' Values
20
MicroC Tutorial
Statemate
21
Creating a Traffic Light in MicroC 1. Click Graphic Editor from the main Statemate window.
Graphics Editor Icon
22
MicroC Tutorial
Exercise 4 - Constructing Statecharts 2. Name the statechart TRAFFIC_LIGHT_CTRL based on the selections in the figure.
Statemate
23
Creating a Traffic Light in MicroC 3. Select New from the Open Chart window to create and open a new diagram. The chart is now available for editing. 4. Click Create State .
New Statechart
24
MicroC Tutorial
Drawing States
To draw states in the statechart, follow these steps, place the cursor in the upper-left corner of the state and click and drag to the lower-right corner of the state. A ghost image shows the state outline.
Naming States
To name the newly created states in the statechart, follow these steps: 1. Click the Create State or Name Existing State icon and begin typing the state name traffic_light_ctrl. The name of the state appears next to the cursor. 2. Click where you want the state name to reside.
Note: Enter the names for boxes using lowercase letters. Statemate, where
appropriate, automatically performs the conversion to uppercase letters. DO NOT select the CAPS LOCK key before typing. 3. Using the Statechart TRAFFIC_LIGHT_CTRL figure as a guideline, create and name the other states within your chart. 4. Click Create Transition .
Drawing Transitions
To draw transitions in the statechart, follow these steps: 1. Locate the state called RED_STATE_ON. This is the source state. Click on the edge of the box to enter the tail of the arrow. 2. Locate the state called GREEN_STATE_ON. This is the target state. Click on the edge of the box and click to enter the arrowhead. 3. With the Create Transition icon still selected, label the transition: CHANGE/RED_LIGHT=false; GREEN_LIGHT=true; YELLOW_LIGHT=false;'
Statemate
25
2. The label appears next to the cursor and follows the cursor. 3. Click on the transition line you want to label to place the label. 4. Select the Create Default Transition icon .
5. Draw a Default Transition into the state RED_STATE_ON. Default transitions do not have a source state and may not have triggers. Use the Statechart TRAFFIC_LIGHT_CTRL figure as a guide as where to draw the default transition. 6. With the Create Default Transition icon still selected, type the following label for the the transition: '/RED_LIGHT=false; GREEN_LIGHT=true; YELLOW_LIGHT=false;' Place the transition label on the transition if you have not already done so. 7. Using the techniques in the above steps complete the design of the statechart based on the Statechart TRAFFIC_LIGHT_CTRL figure. 8. Select File > Save from the chart and then File > Exit. 9. The statecharts that were developed in this exercise is used to describe the behavior for the activity TRAFFIC_LIGHT_SS. To associate the statecharts with the activity complete the following steps: a. Open the activity chart called TRAFFIC_LIGHT_SS. b. Modify the name of the control activity by placing an @ symbol in front of the name as shown in the figure. c. Select File > Save from the chart and then File > Exit.
26
MicroC Tutorial
Statemate
27
28
MicroC Tutorial
Exercise 5 - Constructing a Panel 1. Select Panel Editor from the main Statemate window. The Open Panel window appears
Panel Editor Icon
2. . Enter the name TRAFFIC_LIGHT_PNL for the Panel Name. Refer to the figure.
Statemate
29
3. To begin the panel design, add a background to separate the input and output elements. Select Create Filled Box from the Panel Graphic Editor and stretch this to fill the panel canvas, as shown in the following figure.
Create Filled Box
30
MicroC Tutorial
Exercise 5 - Constructing a Panel 4. Select the new Filled Box and select Tools >Properties. The Bindings/Properties window appears. Refer to the figure.
Filled Box Properties
5. Change the Line Width to 3. Refer to the figure. 6. Select the Fill Color field and click Choose... The Color Viewer for PGE window appears. Refer to the figure. 7. Select the desired color (BLUE in this example) and click OK.
Statemate
31
9. Add the text to the panel by clicking on the top of the panel canvas. The text follows the cursor. Refer to the figure.
32
MicroC Tutorial
Statemate
33
34
MicroC Tutorial
11.
Click Label Existing Interactor , type the word CHANGE. The label name follows the cursor. Place the label over the push button object and click. Refer to the figure.
Label
Statemate
35
Creating a Traffic Light in MicroC 12. Select the push button object that you created and then select Tools > Bind. Select the following for the binding values for the object labeled CHANGE. Refer to the figure. Bind Method - Input Output Controlled by - CHANGE Element type - Event Button type - Flash
Note: The Controlled by: value must be manually typed into the field. All other
values are selected from the drop-down menu that appears when clicking on the value.
Push Button Bindings
Values
13. Click Create Lamp Interactor and create three lamps as shown in the figure.
36
MicroC Tutorial
14. Select the top lamp interactor and then select Tools > Bind to invoke the object's Bindings/Properties window. Enter the following information as the Binding values for the object: Bind Method - Output Controlled by - RED_LIGHT Element type - Condition Button type - Toggle 15. Click Apply.
Statemate
37
Creating a Traffic Light in MicroC 16. Switch to the Properties page by selecting the Bindings/Properties tab. Refer to the figure.
Bindings Page
17. Make sure your settings reflect those shown in the figure. The Properties values appear. 18. Click OK.
38
MicroC Tutorial
19. Select the middle lamp interactor and then select Tools > Bind to invoke the object's Bindings/Properties window. Enter the following for the Binding values for the object: Bind Method: Output Controlled by: YELLOW_LIGHT Element type: Condition Button type: Toggle 20. Click Apply. 21. Switch to the Properties page by selecting the Bindings/Properties tab and enter the following information for the Property values: Highlight Color: YELLOW 22. Click OK.
Statemate
39
Creating a Traffic Light in MicroC 23. Select the bottom Lamp Interactor and then select Tools > Bind to invoke the object's Bindings/Properties window. 24. Enter the following for the Binding values for the object: Bind Method - Output Controlled by - GREEN_LIGHT Element type - Condition Button type - Toggle 25. Click Apply. 26. Switch to the Properties page by selecting the Bindings/Properties tab and enter the following for the Property values for the object: Bind Method - Output Highlight Color - GREEN 27. Click OK. 28. Select File > Save from the PGE and then File > Exit.
40
MicroC Tutorial
Statemate
41
Creating a Traffic Light in MicroC 2. If no profiles currently exist, the New Compilation Profile window automatically opens. If other profiles exist, select File > New Profile. The New Compilation Profile window appears. Refer to the figure. 3. Type the name TRAFFIC_LIGHT_UC in the Profile Name: field.
MicroC Profile
5. Click on the module and then click Add Selected Chart with Descendents to Profile This opens a Chart Tree View of your current design. Refer to the figure.
42
MicroC Tutorial
6. Select the chart called TRAFFIC_LIGHT_SS from the Chart Tree View then click OK. 7. Click Add Selected Panel to Profile from the MicroC profile window. The Add Panels to Profile window appears. Refer to the figure.
Statemate
43
8. Select the TRAFFIC_LIGHT_PNL that was created earlier in the exercise. 9. Click OK. 10. Select the Options > Set Target Configuration from the MicroC Profile window. 11. Select the MAINLOOP_SC_NT configuration. Refer to the figure.
13. Select File > Save from the MicroC Profile window.
44
MicroC Tutorial
14. Select Generate Code for Current Profile in the MicroC Profile Window. This opens the Output directory for the generated code. Refer to the figure.
Generated Code Output Directory
15. Click OK in the Output Directory window to accept the location for the generated code. 16. Click Dismiss in the generated Code window.
Statemate
45
17. Select Compile Generated Code in the MicroC Profile window to compile the generated code. This opens the Selected Makefile window. Refer to the figure.
46
MicroC Tutorial
18. Select Open to begin compilation of the code. The resulting compilation output is sent to a Command Prompt Window. Refer to the figure. Once compilation is completed, exit out of the command prompt window, if desired.
Compilation Results
Statemate
47
Creating a Traffic Light in MicroC 19. The GBA Server allows you to open charts for animation. Select Tools > Open GBA from the MicroC Profile window. The GBA Server window appears. Refer to the figure.
GBA Server
20. Click Execute Compiled Code from the MicroC Profile window and then select the Open option from the Run Command window to start the code execution. Refer to the figure.
48
MicroC Tutorial
Note: The model is now running as an executable with the panel available to provide
user interaction. 21. Click CHANGE multiple times. Different light indicators illuminate with their respective colors. Refer to the figure.
Panel Execution
Statemate
49
50
MicroC Tutorial
Exercise 7 - Using the Test Driver 4. Regenerate and recompile the model as done in the previous exercise. 5. Select Tools > Open GBA from the MicroC Profile window. 6. Click Execute Compiled Code from the MicroC Profile window and when the panel is available press the CHANGE push button any number of times. Within the Command Prompt you can view the Test Driver data stream output. Refer to the figure.
Test Driver Data Stream
7. Stop the model execution. 8. Use a text editor such as WordPad (or Notepad) to create a batch file named dos_box.bat in the code directory. Refer to the figure.
Dos_box.bat File
Statemate
51
Note: Setting these environment variables redirects the standard input and output to
files, making it possible to post-process the results. 9. Select Tools > Open GBA from the MicroC Profile window. 10. Click Execute Compiled Code from the MicroC Profile window. 11. Within the Run Command window, select the dos_box.bat file. A Command Prompt window appears as a result. See the following figure. 12. Enter the name of the executable image (in this case, traffic_light_uc.exe).
Dos_box.bat Command Result
13. Click the CHANGE push button any number of times when the panel is invoked.
Note: Each time you click the CHANGE push button, information is written to the
files TESTDRIVER_INPUT_FROM_PNL_FILE.txt (refer to the figure TestDriver Input File) and TESTDRIVER_OUT_FILE.txt (see the TestDriver Output File figure). These files can be viewed once the model execution has been stopped.
52
MicroC Tutorial
Statemate
53
Creating a Traffic Light in MicroC The files are both of a similar format and are explained in the following tables.
Time
Absolute (Default) or Relative
Variable Name
As in Properties
Variable Type
See the following table.
Variable Value
The new value it is set to.
Comments
As shown in file.
Data Type
INTEGER_DATA_ITEM REAL_DATA_ITEM BIT BIT_ARRAY STRING_DATA_ITEM CONDITION EVENT STATE ACTIVITY ENUM_DATA_ITEM 0 1 2 3 4 5 6 7 8 9
Identifier
14. Edit the dos_box.bat file to enable the line that was commented out. See the following figure.
Modified dos_box.bat File
54
MicroC Tutorial
Exercise 7 - Using the Test Driver 15. Rename the file in the code directory called TESTDRIVER_INPUT_FROM_PNL_FILE.txt to TESTDRIVER_IN_FILE.txt. 16. Open the file that was just renamed and modify the header (first line) of the file. Refer to the figure.
Modified TestDriver In File
18. Select the dos_box.bat file in the Run Command window. A Command Prompt window opens. 19. Enter the name of the executable image (in this case, traffic_light_uc.exe). 20. When the panel is invoked, the TESTDRIVER_IN_FILE.txt is responsible for providing the inputs to the system. The files TESTDRIVER_INPUT_FROM_PNL_FILE.txt and TESTDRIVER_OUT_FILE.txt is created. These files can be viewed once the model execution has been stopped.
Statemate
55
56
MicroC Tutorial