IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27....

28
Hyun-Do Lee IAR Visual State

Transcript of IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27....

Page 1: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Hyun-Do Lee

IAR Visual State

Page 2: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Agenda

• Overview

• State Machines

• Visual State

Page 3: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

V Model

Systematic Software testing ‐ Rick D. Craig and Stefan P. Jaskiel

IAR Embedded Workbench

C-STAT, C-RUN

I-jet, I-jet Trace

IAR Visual State

Embedded Trust - Security

Page 4: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

State Machines

Manual coding using switch-case, if..else, tables etc. works to

some point.

switch ( current_state ) {

case ALARM :

if ( event == ALARM_ACK ) {

StopAlarm();

current_state = NO_ALARM;

}

break;

case ...

:

}

• As complexity increases, how do I ensure that I

capture the complete design in code?

• How do I restructure the code if I discover omissions

or errors in the designs?

• How can I verify that there is no risk for deadlocks or

ambivalence in my design?

Page 5: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

IAR Visual State

• Graphical toolset for designing, testing and

implementing embedded applications

• Based on the UML state machine subset

• Advanced verification and validation tools

• Find design inconsistencies and logical gaps

• Pinpoint complexity issues

• Generates C/C++ code 100% consistent with your

design

• Integration with IAR Embedded Workbench enables

full state machine debugging on hardware

Page 6: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Statecharts in Visual State

• Computational models based on automata theory

• At a given point in time, the system is in one of several possible

states.

• The system can change its state depending on input

• As a state change occurs, actions can be performed

• Widely used to describe systems where the current behavior is a function of

previously occurring events

• State machines in IAR Visual State:

1. Map events in the environment, like device driver input or interrupts,

to state machine events.

2. Capture the discrete system logic in states, events, transitions and

actions using UML diagrams.

3. Map actions to functions or device drivers interacting with the

environment.

Source

TimePass

TimeLeft

evCDKey()

/

evCDKey()

/

/

H

Page 7: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Visual State Components Navigator

project handling and

the container of the

other components

Designer the graphical component where the state machines are being designed using the UML notation.

Coder the coder is automatically creating code for your model that can be used in combination with your

manually written code.

Verificator the component for testing your model. Here you make sure that there are no dead ends, un-used

states or other unexpected behaviors. The verificator tries to run all possible paths of your model.

Validator the graphical component for simulating, analyzing and debugging models. This is where the

functionality of your model is being tested.

Documenter the component for creating up-to-date documentation report in RTF or HTML.

Viewer a stand-alone application to make it possible to share your design with customers/partners for

review without a license for the visualSTATE tool.

Verificator

Page 8: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Project Workflow • Create your control logic with Visual State

• Add your board support files

• Write or get your device drivers/FW library

Page 9: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Interfacing with your Project

VisualSTATE

Action

Functions

Global Variables

defined in

visualSTATE

• States

• Signals

• Internal

variables

Own Code

Place event in queue

Call APIs

Define function

Read/write globals

Events

VS API

Page 10: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Building your Application

Page 11: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Adding Events and Action UCC_TYPE SEQ_AddEvent(SEM_EVENT_TYPE event)

Example:

Void myTimerIRQHandler(void)

{

// Original IRQ code stays and I add

SEQ_AddEvent(E_TimerTick)

}

Eve

nt

<ret-type> A_toggleLED(...)

Example:

VS_UINT32 A_toggleLED(VS_UINT8 ledNo)

{

// Add your code here…

}

Action

Page 12: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Demo

Page 13: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

In this lab we will create a very simple state machine, a three state

design.The aim of the lab is to get familiarized with the most basic

concepts in designing a state machine in visualSTATE.

1. You should create new folder that name of “”.

2. Open the IAR Visual State Navigator. Create a new workspace

by click File>New on the tools menu.

3. Set optional file name, for example MyFirstDesign.vnw

4. When your done highlight Workspace Wizard, press OK and

then press next in the follow the steps.

Page 14: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

14

Lab. first design

5. You have now created a new project and started the visualSTATE

Designer. Hopefully your screen now look like this:

6. In the Project Browser on the left, double click on Region1 and

you will open a new blank window (Statechart Diagram) named

System1.Topstate1.Region1. It’s in this window we will start

create our first state machine.

7. Click the Simple State button ( ) on the Diagram toolbar and

click in the Statechart Diagram window to create a state. Right-

click in the window to deactivate the tool. You can resize and

move the state you have drawn as necessary.

8. To give the state a name, click on the default state name and

type the new name of the state. Create three simple states and

call them, for example, RED, YELLOW and GREEN.

Page 15: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

9. Click the Transition button on the Diagram toolbar. In the

Statechart Diagram window, click on the desired source state,

move the pointer to the destination state and click to complete the

transition. Right-click in the window to deactivate the tool. You can

choose three different Transition tools straight, curved, and

orthogonal lines.

10.Choose the straight transitions ( ) and draw from RED to

YELLOW, YELLOW to GREEN, GREEN to YELLOW and

YELLOW to RED.

Page 16: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

11. To add an event to a transition, select Element

Browser from the View menu. In the Transition

Element select TopState1 as the element location.

Use the Event tab to define the events for your

state machine. Press the New button in the lower

sub-window of the Transition Element.

12. Activate the event Name field in the

right-most sub-window of the

Transition Element. Type the name

of your event, eButton1 and maybe

an explanation in the Explanation

field.

13. Create a second

event and named it

eButton2.

Lab. first design

Page 17: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

14. Return to the Statechart Diagram window, for example

by double-clicking on one of the states shown in the

Project browser. Double-click on the text box

accompanying the transition from the RED to YELLOW

states. This will display the Edit Transition dialog box.

Single-click on Trigger in the Rule section. Then in the

right pane, double-click the Button1 event to add it to the

trigger.

15. When you have completed composing the transition,

click OK.

16. Repeat step 13 – 14 for the other transitions. Add trigger

event Button1 to the YELLOW to GREEN transition and

Button2 for the remaining two transitions.

Page 18: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

17. Now it’s time to define some actions. Open the Transition Element again. Click the Action Function tab to go to the page where you can define the action functions for your state machine. Press the New button.

18. Activate the Name field in the right-most sub-window of the Transition Element. Type the name of your action function, aRED_ON. If you like, add an explanation in the Explanation field.

19. Return to the Statechart Diagram window, for example by double-clicking on one of the states displayed in the Project browser. Double-click on the text box accompanying the transition YELLOW to RED state. This will display the Compose Transition dialog box. Single-click on Action Expression in the Rule section. Then in the right pane, double-click the aRED_ON action function to add it to the transition.

Page 19: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

20. When you have completed composing the transition,

click OK.

21. Create 2 more action functions, aYELLOW_ON and

aGREEN_ON and compose the remaining transitions

with suitable actions.

Page 20: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

22. When you are done your design hopefully resemble

something like the picture on the right.

23. We have one thing left to define before we can consider

our first design fulfilled. We need to define the initiate

state i.e. which state should our system start with after

e.g. a reset.

24. On the Diagram toolbar, click the Initial State button ( ).

Add an initial state to the state chart. Also add an

transition with a proper action from the initial state to one

of the simple states (for example the state RED as

initial).

Page 21: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. first design

25. When you have completed designing your statechart,

save your project (File>Save) and exit the Designer.

26. Click Yes when prompted to reload files in the Navigator.

Check the Do not show this message again and select

Reload files silently if you wish to hide the save details in

the future.

27. Congratulations, you have now designed your first state

machine! We will use this design in later labs to

generate code and connect the model to the real world.

However, before we can do that we need to test our

model first which we will do in the next lab.

Page 22: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. test the design

This lab describes how you test the state design model you created in the previous lab. For testing you use the Validator and Verificator tools. The Validator for simulating and analyzing and the Verificator for checking the logic consistency of the model so you don’t have any dead ends, unreachable transitions and conflicting behavior of transitions.

1. If it’s not open, start the Navigator and open your MyFirstDesign system.

2. Open the Validator for example by pressing F8 or the button ( ) on up on the toolbar.

3. When the validator is launched open the simulation layout if it isn’t already open.

4. To start the simulation, double-click on SE_RESET in the Event window. This places your System in its initial state.

Page 23: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. test the design

5. Step through your system by double-clicking on

events in the Validator Event window and see if it

behaves as you expect. In the System window you

can see the resulting state combination. In the

Action window you can see which actions were

generated by the event.

Page 24: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. test the design

6. Beside the simulation layout the Validator has a

graphical view. On the Validator menu, select

Debug>Graphical Animation to step through your

System graphically. Resize or reposition the Validator

window in order to see the Designer Simulation Mode

window (or even better, use two screens).

7. When you double-click events in the

Event window, the resulting state

combination will be shown with red

borders in your statechart. Blue

borders are used for the states that

were last active.

8. Exit the Validator and

return to the

Navigator when you

are done.

Page 25: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

9. After you have exited the Validator

select Project>Verify System on

the menu or press Ctrl+F10.

10. Select the option Verify in the

Verificator Notification dialog and

press OK.

11. In the Verificator window you can view the results of the verification and

hopefully you haven’t any errors. If there are any errors or warnings in the

verification message, return to the Designer and make the necessary

changes. Try to use trace and see if helps you with the troubleshooting.

12. Finally, select

Project>Document on the

menu or press F11 to

generate a report of your

state machine model.

Lab. test the design

Page 26: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. Generate code

To complete your application you need the following: An ANSI C compiler, IAR Visual State generated code, and user-written code. This lab describes how to generate C code for the statechart design we did in the previous lab and how to implement it in IAR Embedded Workbench.

1. Start the Navigator and open your MyFirstDesign system.

2. Before we press the button and generate some code we want to see if we need to change some options such as output path and if we want readable or table based code. Open the code generation option in Projet>Options>Code generation or use Alt+F9.

3. In the MyFirstDesign>Configuration page set the option Readable code generation and keep the deafult setting for all the other options. In the File output page confirm that the Output path is set to the default as coder\. The location of the generated code is relative to the project files and in our case ..\FirstDesign\vS\coder\.

Page 27: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

Lab. Generate code

4. Now it’s time to generate some code. Use F9, Project>Code Generate or ( ) on the toolbar to activate the Coder.

5. The Navigator will display a coder report file where you can see the names of the generated files and their location. This file also

contains statistics on the various elements contained in the generated code. In the coder folder you can see all the files that

have been generated, both c source files and header files.

Page 28: IAR Visual State · Call APIs Define function Read/write globals Events VS API ... 27. Congratulations, you have now designed your first state machine! We will use this design in

www.iar.com/visualstate

www.iar.com/introduction_vs

Thank you for your attention!