2.11 I/O View and Other Bare-Metal Programming References

This section describes how you would typically write code in Microchip Studio, independent of a software configuration tool or framework, i.e., bare-metal. This description is covered both as a video (linked below) and a hands-on document. The main focus is on the relevant programming references, how each is accessed, and their use. The project context is to turn ON an LED, then blink with a delay. Although using the ATtiny817 Xplained Pro, the principles are general enough to use with any kit in Microchip Studio, though the principles apply to most devices supported in Microchip Studio.

Getting Started Topics

Video: I/O View and Bare-metal programming references

The list below is an overview of the typically used programming references. Particular emphasis is placed on the I/O view, which provides a way to navigate the data sheet register descriptions when editing or debugging and understanding the current configuration when debugging. This second use of the I/O view when using debugging to test new register configurations.

This topic is closely related to both Debugging 3: I/O View Memory View and Watch as well as Editor: Writing and Re-Factoring Code (Visual Assist).

  • Device data sheet
  • Data sheet (from I/O view)
  • Kit user guide and schematics
  • I/O View (debugging)
  • Editor (Visual Assist)
  • Device header files
  • AVR Libc (AVR specific)
  • Atmel START: ATtiny817 project
In the process, the following code is written. The decision process is described using the list of programming references above, even if the code is simple.
#include <avr/io.h>
#define F_CPU 3333333
#include <util/delay.h>

int main(void)
{
	PORTB.DIR = PIN4_bm; 	

    while (1) 
    {
		_delay_ms(500);
		PORTB.OUTTGL = PIN4_bm;		
    }
}
Warning: Be sure to keep the #include <avr/io.h> line at the top of main.c. This header file will include the correct register map for the selected device, and without this statement, the compiler will not recognize any of the macros referenced in the code above.

Device Data Sheet (PDF)

Although I/O View allows easy access to navigate the data sheet at a register level, the PDF version still has a role. The device data sheet, in PDF format, tends to be used at least to get an understanding of the peripheral through the block diagram and functional description. For example, to understand the PORT peripheral of the ATtiny817, we consulted the PORT Block Diagram and Functional Description > Principle of operation sections of the data sheet. These two sections (connecting the description to the diagram) give a basic understanding of the PORT peripheral.
Figure 2-16. PORT Block Diagram from the PDF Data Sheet
Figure 2-17. Principle of Operation from the PDF Data Sheet of ATtiny817
Note: We used the device data sheet for the peripheral block diagram and a description of the PORT DIR and OUT registers.

I/O View Data Sheet

Microchip Studio allows easy access to the data sheet register descriptions by clicking F1 on the relevant register description. The HTML version of the data sheet opens online (by default). The data sheet will open in the context of the relevant register description.

Note: In this way, we use the Data sheet from I/O View to understand that:
  1. Writing a '1' to PORT.DIR[n] configures and enables pin n as an output pin.
  2. If OUT[n] is written to '0', pin n is driven low.
Figure 2-18. Opening an Online Data Sheet from I/O View

I/O View (Debugging)

This functionality can directly be tested by starting a debug session using Start Debugging and Break. So we are now able to begin testing functionality, as shown in the image below.
Debugging 3: I/O View Memory View and Watch describes I/O View in more detail.
Note: I/O View when debugging is used to:
  1. Verify that writing a '1' to PORT.DIR4 sets the pin as OUTPUT, LOW by default to LED turns ON.
  2. Verify that writing a '1' to PORT.OUT4 turns OFF the LED.
Table 2-2. Microchip Studio Button Functionality (Programming and Launching Debug Sessions)
ButtonFunctionalityKeyboard Shortcut
Start Debugging and BreakAlt + F5
Attach to Target
Start DebuggingF5
Break AllCtrl + Alt + Break
Start Without DebuggingCtrl + F5
Figure 2-19. Turning ON/OFF Kit LEDs Through Manipulating I/O View Registers when Debugging

Downloading Microchip Studio Documentation

The data sheet can also be downloaded by using the Microchip Studio help system. In this case, similar functionality will work offline. Downloading Offline Documentation describes this.

Microchip Studio Editor (Visual Assist)

The Microchip Studio Editor, powered by Visual Assist, has powerful features to help you write and refactor code and easily navigate large projects. Suggestion functionality is shown in Figure 2-20, while Figure 2-21 shows an overview of the code navigation.. In the next section, Editor: Writing and Re-Factoring Code (Visual Assist), the editor features are detailed.
Figure 2-20. Suggestion Functionality in the Microchip Studio Editor for Writing Code
Figure 2-21. Microchip Studio Editor Navigation Overview

Specifically, in the video related to this section, the editor is used for the following.

Device Header Files

Through the Goto Definition functionality of the editor, it is easy to access the MCU device header files, i.e., by clicking on any register and then clicking on the goto button or typing Alt+G. Writing PORTB gives a suggestion list of potential registers from the PORT structure, shown in figure Suggestion lists and the MCU device header files. For more information about how the AVR header files are structured, see AVR1000.

Figure 2-22. Suggestion Lists and the MCU Device Header Files

Kit Schematics and User Guide

The kit schematics and user guide help understand MCU pin connections on the kit. Complete schematics and kit design files, such as Gerbers, are available on www.microchip.com, on the kit's product page.
Figure 2-23. How to Find Schematics for a Particular Development Board
The LED and button connect to the pins from the ATtiny817 Xplained Pro User Guide, as shown in the table below.
Table 2-3. ATtiny817 Xplained Pro GPIO Connections
Silkscreen TextATtiny817 GPIO Pin
LED0PB4
SW0PB5
The ATtiny817 Xplained Pro design documentation schematic shows the connections for the LED and button, as in the figure below.
Figure 2-24. ATtiny827 Xplained Pro GPIO Connection Schematics
From the schematics, the conclusion is:
  • The LED can be turned ON by driving PB4 low
  • SW0 is connected directly to GND and PB5 through a current limiting resistor
  • SW0 does not have an external pull-up resistor
  • SW0 will be read as '0' when pushed and as '1' when released, if the ATtiny817 internal pull-up is enabled

AVR® Libc

All the references covered to this point are just as relevant for SAM as for AVR. However, as the name suggests, this one is specific to AVR. AVR Libc is a Free Software project whose goal is to provide a high-quality C library for use together with GCC on AVR microcontrollers. Together, avr-binutils, avr-gcc, and avr-libc form the heart of the Free Software toolchain for the AVR microcontrollers. Further, they are accompanied by projects for in-system programming software (avrdude), simulation (simulavr), and debugging (avr-gdb, AVaRICE).

The library reference is usually a quick interface into AVR Libc, as shown in Figure 2-25. One can quickly search the page for a relevant library. The module name indicates the relevant header files added to the project. For example searching for 'interrupts', the relevant include will be #include <avr/interrupt.h>. A list of available functions and relevant interrupt callbacks shows when clicking into the module. See also Figure 2-26.

Figure 2-25. AVR® Libc Library Reference
Figure 2-26. Using Interrupts with AVR® Libc

Atmel START

Atmel START is a web-based software configuration tool for various software frameworks, which help you get started with MCU development. Starting from either a new project or an example project, Atmel START allows you to select and configure software components (from ASF4 and AVR Code), such as drivers and middleware to tailor your embedded application in a usable and optimized manner. Once doing an optimized software configuration, you can download the generated code project and open it in the IDE of your choice, including Microchip Studio, MPLAB X, IAR Embedded Workbench, Keil μVision, or set up a make-file.

Although Atmel START is a tool for MCU and software configuration, it can still be helpful even in bare-metal development, i.e., writing code from scratch using the list of programming references described in this section. Creating a new project for the kit you are using can be a helpful alternative to the board schematic, using the PINMUX view. In addition, the CLOCKS view can be of use to check the default clocks of a device. Furthermore, viewing the configuration code, pieces of use can be pasted back into your project. For example, the AVR Libc delay functions require that the clock frequency is defined, as shown in Figure 2-29. For the ATtiny817, this default value would be: #define F_CPU 3333333.

Figure 2-27. Using START to Creating a New Project for a Relevant Board
Figure 2-28. Showing Board Labels in START as an Alternative to the Kit Schematic
Figure 2-29. Checking Default Clock Configuration and Using VIEW CODE to Find F_CPU Define