Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $630

Donations Received

Top Donor: C.C. $100

X

Adding ASF to an Existing Atmel Studio Project

Created on: 23 April 2016

Part 4 of the ASF ARM Tutorial

ASF can be added to an existing Atmel Studio project that was not started as an Atmel Software Framework project.

So far in this tutorial series we have looked at creating a new Atmel board ASF project and creating a new user board ASF project. A third scenario is adding ASF to a project that has already been created and has some code in it.

In this part of the ASF tutorial series we will create a new Atmel Studio project without ASF and write some application code for it. We will then change the project into an ASF project. A simple LED blink application will be used again.

Example Project without ASF

Start by creating a new Atmel Studio project, but this time make a GCC C Executable Project as shown in the image below. I am calling this project add_asf.

Create a New GCC C Executable Project

Create a New GCC C Executable Project

In the Device Selection dialog box, filter for the device on the board and select the device. This tutorial uses the Atmel SAM4N Xplained Pro again which has an ATSAM4N16C device on the board. This is the device selected in the image below.

Selecting a Device for the New Project

Selecting a Device for the New Project

The new project in Atmel Studio opens with some skeleton code in main.c as shown below.

New Atmel Studio GCC C Executable Project with Skeleton Code in main.c

New Atmel Studio GCC C Executable Project with Skeleton Code in main.c

Modify the skeleton code in main.c as follows. This is our application code.

#include "sam.h"

int main (void)
{
    SystemInit();

    REG_PIOB_PER = PIO_PER_P14;         // enable PIO to control pin PB14
    REG_PIOB_OER = PIO_OER_P14;         // enable PB14 as an output pin
    
    while (1) {
        REG_PIOB_CODR = PIO_CODR_P14;   // switch LED on
        // delay needed
        REG_PIOB_SODR = PIO_SODR_P14;   // switch LED off
        // delay needed
    }
}
Application Code for the add_asf Project in Atmel Studio without ASF

Application Code for the add_asf Project in Atmel Studio without ASF

As can be seen in the above application code, pin 14 on PORT B is set as an output pin. This is the pin that LED0 is attached to on the Atmel SAM4N Xplained Pro board. The LED is then switched on and off to blink or flash it, but a delay function is missing from the code. Because we have not created an ASF project, the LED is accessed using the ARM microcontroller registers.

Let's say that we started this project and then at some point decided that we did not want to write our own delay function, but rather reuse some code that was already written to perform the delay. We decide to use the delay function from the ASF library and so need to convert the project to an ASF project.

Amazon.com

Amazon.co.uk

Adding ASF to the Project

The Board Selection Wizard is used to add ASF to a project in Atmel Studio. Select ASF → Board Wizard from the top menu bar in Atmel Studio to open the Board Selection Wizard dialog box.

A board can now be selected in the Board Selection Wizard dialog box. For this project and microcontroller, the user board template for the ATSAM4N16C is selected as shown in the image below.

The Atmel Studio Board Selection Wizard Dialog Box

The Atmel Studio Board Selection Wizard Dialog Box

After clicking the Next button in the dialog box, a summary of the files that will be added to the project is displayed.

A warning dialog box, as shown below, will be displayed after clicking the Finish button.

The Warning Dialog Box Displayed After Adding a Board to the Project

The Warning Dialog Box Displayed After Adding a Board to the Project

The dialog box warns that ASF has its own main() function and initialization code. This means that the project will not compile until the project has been modified.

Modifying the ASF Project Code

The project now has two main.c files each containing its own main() function. Code from the original main.c file must be copied to the new main.c file. The original main.c file must then be deleted. The original start-up code must also be deleted for the project to compile correctly.

The image below shows the project with ASF added. Marked with a red dot from top to bottom is the original start-up folder, the new main.c file and the old main.c file.

Project with ASF Added

Project with ASF Added

Start by copying the code from the old main.c file to the new main.c file so that it looks as follows. Then right-click the original main.c file and delete it by selecting Remove from the pop-up menu.

Modify the Code in the main.c File

Modify the Code in the main.c File

After selecting to remove the file, the following dialog box will pop up.

Remove File Dialog Box

Remove File Dialog Box

The file can either be removed from the project, but not deleted by clicking the Remove button; or the file can be removed from the project and deleted by selecting the Delete button.

The Device_Startup folder and its contents must also be removed from the project for it to compile properly because ASF includes its own start-up functions which will clash with the original ones.

Right-click the Device_Startup folder in Solution Explorer and select Remove on the pop-up menu as shown below.

Removing the Start-up Files by Deleting the Device_Startup Folder

Removing the Start-up Files by Deleting the Device_Startup Folder

At this stage the project should compile successfully, but we still need to add the delay function to the code.

Adding the ASF Delay Module and Function

To use the delay function, we must first add the ASF delay module to the project using the ASF Wizard. This is done in the same way as was done in the previous part of this tutorial. In this case we only need to add the delay module and not the IOPORT module because our existing code operates the LED using registers.

Add the Delay routines (service) module to the project using the ASF Wizard.

Adding the ASF Delay Module to the Project

Adding the ASF Delay Module to the Project

The delay_ms() function can now be used in the project. Add the timing delays to the code in main() as shown below.

while (1) {
    REG_PIOB_CODR = PIO_CODR_P14;	// switch LED on
    delay_ms(500);
    REG_PIOB_SODR = PIO_SODR_P14;	// switch LED off
    delay_ms(500);
}

We still need to set up the clock resonator values in conf_board.h and call sysclk_init() in main() in order for the delay function to work. The code is shown below with resonator settings for the SAM4N Xplained Pro board.

conf_board.h

#ifndef CONF_BOARD_H
#define CONF_BOARD_H

// clock resonators
#define BOARD_FREQ_SLCK_XTAL      (32768U)
#define BOARD_FREQ_SLCK_BYPASS    (32768U)
#define BOARD_FREQ_MAINCK_XTAL    (12000000U)
#define BOARD_FREQ_MAINCK_BYPASS  (12000000U)
#define BOARD_MCK                 CHIP_FREQ_CPU_MAX
#define BOARD_OSC_STARTUP_US      15625

#endif // CONF_BOARD_H
The conf_board.h File with Clock Resonator Definitions Added

The conf_board.h File with Clock Resonator Definitions Added

main.h

#include <asf.h>

int main (void)
{
    /* Insert system clock initialization code here (sysclk_init()). */
    sysclk_init();
    board_init();
    WDT->WDT_MR = WDT_MR_WDDIS;

    REG_PIOB_PER = PIO_PER_P14;         // enable PIO to control pin PB14
    REG_PIOB_OER = PIO_OER_P14;         // enable PB14 as an output pin
    
    while (1) {
        REG_PIOB_CODR = PIO_CODR_P14;   // switch LED on
        delay_ms(500);
        REG_PIOB_SODR = PIO_SODR_P14;   // switch LED off
        delay_ms(500);
    }
}
The main.c File with Application Code Added

The main.c File with Application Code Added

The project should now compile and run just as the previous two examples did from the previous two parts of this tutorial.

← Go back to Part 3 of the ASF ARM TutorialGo to Part 5 of ASF ARM Tutorial →