AVR® Devices in MPLAB® XC8 v2.05
Contents
Version 2.0 MPLAB® XC8 C compiler introduced many new features, but one of the most significant was preliminary support for Microchip AVR® devices, using the GCC-based 8-bit AVR compiler that was included in this version. The MPLAB XC8 v2.05 release has taken AVR support further, consolidating many new AVR compiler features, and together with the latest MPLAB X IDE, the compiler allows programmers to develop code for any Microchip 8-bit device. This article looks at using the MPLAB XC8 compiler for AVR devices; if you use 8-bit PIC® devices with this compiler, see "Moving to the v2.0 MPLAB® XC8 C Compiler".
Moving to MPLAB XC8
If you have used the open-source AVR-GCC compiler, moving forward with MPLAB XC8 v2.x is relatively straightforward.
Command-line Projects
To move from the AVR toolchain to MPLAB XC8, switch your commands and scripts to use the new xc8-cc driver. This application is located in the top-level bin directory in the compiler's installation directory.
Most of the options with which you are familiar are accepted by this new driver.
Atmel® Studio Projects
The Atmel® Studio IDE continues to use the open-source AVR-GCC compiler. To move your Atmel Studio projects over to use the MPLAB XC8 compiler:
- Import your projects from inside the MPLAB X IDE using the File > Import > Atmel Studio Project menu item.
- This creates MPLAB X IDE projects that can use the XC8 compiler and whose options have a similar effect to those used by Studio projects.
What’s New
If you are migrating from the open source AVR-GCC to MPLAB XC8, here are some new features that XC8 offers:
Device Architectures
When building with MPLAB XC8, you will see that the compiler can build for 8-bit PIC devices, as well as supporting all the 8-bit AVR devices that were available using AVR-GCC. If you choose to create and build projects for PIC targets, you are now using different internal compiler applications, which are customized for the PIC architecture and which require slightly different source code to that used by AVR devices. The MPLAB® XC8 C Compiler User’s Guide for PIC® MCU contains information specific to projects that target PIC devices.
Top-level Driver
The new top-level driver allows you to more freely migrate between the internal compiler tools bundled by the MPLAB XC8 package. The driver is called xc8-cc and can be used to build both PIC- and AVR-based projects. This driver is automatically called by the MPLAB X IDE for projects that use the new 2.x compiler. This driver accepts most of the options used by the open-source AVR-GCC compiler, so its use should be familiar. The old avr-gcc compiler driver is still distributed with the compiler, should you wish to use it.
Librarian Driver
Libraries are now built using a new librarian driver called xc8-ar, which can be used for both PIC and AVR library generation. The old archiver, avr-ar, is still present, should you wish to use it.
Configuration Bits
Configuration bits or fuses are used to set up fundamental device operation, and these can now be easily specified in your source code using the config pragma. It has the form:
#pragma config setting =state|value
Here, setting is a configuration setting descriptor, e.g., WDT, and state is a textual description of the desired state, e.g., SET, as shown in the following.
#pragma config WDTON = SET #pragma config EESAVE = CLEAR #pragma config BODLEVEL = BODLEVEL_4V3
The value field is a constant that can be used in preference to a descriptor, as illustrated below.
#pragma config SUT_CKSEL = 0x10
Const Objects in Flash
Implemented in the v2.05 release, the compiler can now place const-qualified objects in the program flash memory, rather than having these located in more precious RAM. The compiler has been modified so that const-qualified global data is stored in the program flash memory and this data can be directly and indirectly accessed using the appropriate program-memory instructions.
This new feature is enabled by default but can be disabled using the -mno-const-data-in-progmem option. For avrxmega3 and tiny architectures, this feature is not required and is always disabled, since program memory is mapped into the data address space for these devices. The preprocessor defines the symbol __AVR_CONST_DATA_IN_PROGMEM__ when the feature is enabled.
As program memory data must be accessible via a pointer, the compiler will dereference pointers to const objects (const type *) using the ADDR_SPACE_MEMX address space, i.e. treat such addresses as if they had the type (const __memx type *). Any pointer defined as a pointer to const can access both data and program memories but may be larger than a pointer not using the const target qualification. An updated libgcc library caters to these changes.
Documentation
A separate user’s guide has been produced specifically for MPLAB XC8 users that target AVR devices. "MPLAB® XC8 C Compiler User’s Guide for AVR® MCU" is available as a PDF in the docs directory of the compiler, but with an AVR project loaded, it can be accessed from within the MPLAB X IDE using the Help button.