Code Stepping in Atmel Studio

 Objective

In the code comments from the "Hello World Project" page, the assumption was made that the LED is turned on by driving the connected GPIO pin high. This lab uses code stepping to check if this assumption is correct.

This page assumes that you've already completed the Hello World Project. If you haven't yet done so, please go through those steps before continuing with this exercise.

 Materials

Hardware Tools

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

 Procedure

The key buttons used for code stepping are illustrated below, found in the top menu bar or in the 'Debug' menu. The corresponding functionality and keyboard shortcuts are outlined in the table.

CodeStepping.png
Button Functionality Keyboard Shortcut
StepInto.png
Step Into Function Call F11
StepOver.png
Step Over F10
StepOut.png
Step Out of Function Call Shift + F11
RunToCursor.png
Run to Cursor Ctrl + F10
SystemReset.png
Issue System Reset

1

Start Debug Session

Start a debug session by clicking the Start Debugging button or pressing F5.

If you're coming from the "Hello World Project" page, you don't need to start a debug session unless you ended the previous one.

2

Halt Execution

Click the Break All button to halt execution.

The yellow arrow on the left edge of the editor window points to the line of code that is about to be executed.

NextInstruction.png

3

Step Through Code

Step through the code by clicking the Step Into Function Call button to discern whether the comments reflect the code.

4

Extract Method

The result should be that the LED drive assumption is proven wrong, and the "on" and "off" comments are swapped. The line of code commented with /* Turn LED off */ is executed when SW0 is pressed. Highlight this line of code, right-click and go to Refactor > Extract Method.

ExtractMethod.png

A Extract Method dialog will appear. Name the function LED_on.

LED_on.png

Click OK and the code should change. A new function called LED_on should appear at the top of the file, with a function call where the line of code used to be.

5

LED_off()

Use the same method to implement LED_off().

6

Introduce Variable

Next, it is necessary to create a variable for the SW0 state. Highlight the condition inside the if() in the main() while(1) loop. Right-click and go to Refractor > Introduce Variable.

IntroduceVariable.png

The Introduce Variable dialog will appear. Name the variable uint8_t SW0_state.

VariableName.png

Click OK and the code should change. The condition inside the if() statement should now reference a variable assigned to the variable on the line above it.

7

Select the right side of the SW0_state assignment and extract a method for SW0_get_state.

Change the automatically generated bool return value to uint8_t to avoid having to include an extra header to deal with boolean values.

8

Now it is possible to implement a method for void LED_set_state(uint8_t state). Extract the method. Atmel Studio will detect the argument SW0_state.

ExtractMethod2.png

Click OK and the code should change. Now there is a separate method for setting the LED state.

9

In a larger application, this function may be used for setting the LED state in a context that is irrelevant to the SW0 state. Atmel Studio is capable of contextual renaming, so this feature can be used to easily rename the argument and avoid confusion. Inside the LED_set_state() function, right-click on the SW0_state variable and go to Refactor > Rename.

ContextualRename.png

10

The Rename dialog will appear. Rename the SW0_state variable to state. Atmel Studio will detect all occurrences of the variable with the same context as the one which has been selected, which are presented in a list and able to be individually selected or deselected.

RenameDialog.png

Click 'Rename' and the code should change. Observe that the argument of LED_set_state() and all of its references inside the function have been renamed, but the references to SW0_state in main() have remained the same.

11

The updated main() should now look something like this:

Click 'Start Debugging and Break' to recompile and start a new debug session.

12

Use 'Step Over' to show how the device is able to execute a function call before code execution is halted on the line of code following the function call.

13

Use 'Step Out' to show how the device can complete the current function call before code execution is halted on the line of code following the function call. This is best observed after stepping into LED_set_state().

14

Press 'Reset' and observe how this causes the device to reset and halt code execution at the beginning of main().

15

Place the cursor at the LED_set_state() line and click the Run to Cursor button to show that this causes the device to halt execution when the line is hit for the first time.

 Results

The code is now more readable, and Atmel Studio has been used to step through code running on the ATtiny817.

© 2019 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.