 2 of the ASF ARM Tutorial
In the second part of the Atmel Software Framework tutorial, a new ASF project is created for running on a SAM4N Xplained Pro board. Creating an ASF project for an Atmel board is an easy first step in using the ASF because the board hardware is already defined in the new project.
The next part of this tutorial series shows how to use ASF on a custom board with a user board template. In this case the underlying hardware must be defined by the user.
The following tutorial steps show how to create a new Atmel Studio ASF project, add the necessary ASF module and write the code to blink an LED.
Create a new Atmel Studio project by selecting New Project... from the Start Page, or by selecting File → New → Project... from the top menu.
In the New Project dialog box, select GCC C ASF Board Project, select the location for the project and give the project a name. I am calling this project atmel_board_led_blink.
Creating a New GCC C ASF Board Project
In the Board Selection dialog box, search for the microcontroller name that is on your board. Once the name has been filtered, it can be selected. Below the filtered device name, select the board for the device. For example, the ATSAM4N16C has SAM4N Xplained Pro - ATSAM4N16C as the available board option.
In this part of the tutorial, an Atmel board must be selected and not the "User Board template", which will be covered in the next part of this tutorial.
An alternative method for searching for the board is to select the Select By Board radio button at the top of the Board Selection dialog box. Click Ok when the board is selected.
ASF Project Board Selection
The new ASF Atmel board project has now been created. If main.c can not be seen in the Solution Explorer pane, click src to expand it. Double click main.c to open it. If Solution Explorer is not visible, click View → Solution Explorer from the top menu.
Atmel Studio Solution Explorer
Some skeleton code can be seen inside main.c after scrolling past the comment block at the top of the file. This is where we will write our LED blink application.
ASF Skeleton Code
To blink or flash an LED on and off, we need to be able to toggle the LED state and call a delay routine each time the LED is toggled. Default ASF settings allow us to access the on-board LED, so we only need to add support for a delay routine.
ASF delay routines are available as a module and are added to the Atmel Studio project using the ASF Wizard.
Open the ASF Wizard by selecting ASF → ASF Wizard from the top menu or by clicking the ASF Wizard icon on the top toolbar.
In the ASF Wizard, select the current project in the Project drop-down box if it is not selected by default.
ASF Wizard in Atmel Studio
Filter the available modules by typing delay in the search box in the ASF Wizard under the Available Modules pane on the left. Delay routines (service) will now be displayed in the left pane of ASF Wizard.
Filtering Modules in ASF Wizard
Click Delay routines (service) in the left ASF Wizard pane to select it and then click the Add >> button at the bottom.
Now click the Apply button to confirm the added module. A dialog box will pop up that contains a summary of the changes. Click OK to close the dialog box. The delay routines module has now been added to the project.
Open the main.c file in Atmel Studio so that the application code can be added to the project.
Before continuing, we need to find out what the name of the on-board LED has been defined as in the code. The SAM4N Xplained Pro board has an LED marked as LED0 on the board silkscreen. This is the LED that we want to blink. If you are using a different board, choose an LED on the board that you want to blink and find out what the name of the LED is by looking at the board silkscreen or consulting the user guide for the board.
LED0 on the SAM4N Xplained Pro Board
Use Solution Explorer in Atmel Studio to find the header file for the board that you are using. The header file can be found in src\ASF\sam\boards\<board_name>\<board_name>.h For the SAM4N Xplained Pro board, the name of the board folder is sam4n_xplained_pro and the name of the board file is sam4n_xplained_pro.h
Board Files in Solution Explorer
Open the board header file and scroll down to find where the LEDs are defined. As can be seen in the image below, LED0 is defined as LED0_GPIO for the SAM4N Xplained Pro board. Select this name and copy it so that we can use it in main.c.
LED0 Definition in the Board Header File
In main.c, create a while(1) loop below the board_init() function. Call a new function in the while(1) loop called ioport_toggle_pin_level() and pass the LED name that you copied to it as shown below.
ASF Pin Toggle Function Called in the Atmel Studio Project
Call the delay_ms() function below the pin toggle function. Pass the delay in milliseconds to the delay_ms() function. In the code shown below the function is set to generate a 400ms delay.
ASF delay_ms() Function Added to the Code
The ASF project is nearly finished. There is only one thing to add to make it work properly and that is to call a function that initializes the system clock. Call sysclk_init() before the call to board_init().
ASF Project System Clock Initialization
The finished code is presented below in text format for easier copying.
#include <asf.h> int main (void) { /* Insert system clock initialization code here (sysclk_init()). */ sysclk_init(); board_init(); /* Insert application code here, after the board has been initialized. */ while (1) { ioport_toggle_pin_level(LED0_GPIO); delay_ms(400); } }
The project can now be built and loaded to the board. The LED should toggle state every 400ms as shown in the video below.
Can't see the video? View on YouTube →
In the next part of this tutorial series an ASF project for a custom or user defined board is created. It is not necessary to have a custom board to follow the tutorial. The same Atmel board can be used in the next part of the tutorial, but when the ASF project is created there won't be any hardware names defined which would be the case with a custom board. The tutorial shows how to use ASF with a custom board.
Amazon.com
Amazon.co.uk