ATMEL Studio 7 Arduino Due User Board Tutorial

Programming Arduino Due using ATMEL ASF custom board

This ATMEL Studio 7 Arduino Due User Board Tutorial teaches you how to program Arduino Due configured as User Board Template using ATMEL Studio 7. A Simple LED Blinking C program is used for demonstration.

We assume that you have already installed and configured ATMEL Studio 7 to work with Arduino Due. If not you can read the tutorial How to program Arduino Due using ATMEL Studio where we have explained how to download and install and configure ATMEL Studio 7 so that you can upload the C/C++ compiled code to Arduino Due.

This ATMEL Studio 7 Arduino Due User Board Tutorial is different than the previous tutorial where we had selected and used the ATSAM3X8E board project. That board project had pre-configured hardware setting such as use clocks, disabled WatchDog timer etc. Now in this User Board Tutorial we have to configure and initialize the hardware. This tutorial is beneficial to learn because often you may design your own board with some CPU like ATSAM3X8E. Arduino Due is a board with ATSAM3X8E microprocessor.

Steps for ATMEL Studio 7 Arduino Due User Board Tutorial

We want to outline the steps earlier on here because this allows you to know what to expect and also to skip to the section that you need help with.

  1. Create GCC C ASF Board Project
  2. Add ASF services
  3. Configure and Initialize Hardware
  4. Write LED Blinking Program
  5. Build/Compile and Upload

1. Create GCC C ASF Board Project

The fist step is to create GCC C ASF Board Project. Open ATMEL Studio if haven’t already and then click on New > Project. Select GCC C ASF Board Project. Provide some meaningful project name and specify where you want to save the project. That’s shown below.

Click on OK.

In the next window appears, search for SAM3X8E microprocessor. In the pane just below the list of of devices you should see User Board Template – SAM3X listed. Select that and then click Ok.

Now your custom board project will be created and you should see the following project user interface. On the right side, you should see the project properties window. There under src folder you should find main.c file. Click on that and it will open in the editor pane. We will be writing our C program in this File.

2. Add ASF services

We want to write a simple LED blinking program. The LED will be connected to the digital pin 10 on the Arduino Due. To set up the direction of the port and pin, to set the logic level we will need functions that can do that. These functions to control port and pin is available in the IOPORT service. Similarly instead of writing your own delay function we can use the delay routine service. These two function or services are available to the user via the ASF services.

To add these two services click on ASF > ASF Wizard as shown below.

add asf services via asf wizard

In the ASF Wizard window, search, select and add the Delay routines(service) and IOPORT – General purpose I/O service(service) as shown below.

add delay and IOPORT services

After you have added those services they will be listed under the Selected Modules pane. Click on Apply then OK in the message window that appears.

At this point you have added the services and you can now use the aforementioned functions in your C program. To confirm or to view what services you have already in your project, click on the src > ASF > common > services folder. As shown in the figure below, in our project, the clock, delay, ioport services are added. The clock service module was added automatically when we added the delay service because delay services is dependent on the clock service module.

 

3. Configure and Initialize Hardware

The next step in this ATMEL Studio 7 Arduino Due User Board tutorial is to configure and initialize the hardware(board). This step involves setting and initializing the clock, disabling watch dog timer, configure the ports and pins.

3.1 Initialize Clock and set label for LED pin

First we will initialize and set the clock frequency to be used. Then we will add a variable name label for the digital pin 10 on the Arduino due board. These settings are to be entered in the conf_board.h file which is available in the src > config folder.  Open this file as shown below. Initially this file is empty except the beginning and end code for the header file.

Now enter the following code between the #define CONF_BOARD_H and #endif lines.

The first lines under the clock comments are setting for the clock. The second single line under LED comment is for labelling the port C pin 29 as LED. This ATSAM3X8E port C pin 29 is the pin corresponding to the digital pin 10 on Arduino board. The IOPORT_CREATE_PIN() macro is defined in the IOPORT service that we added previously.

After you have added the above lines it looks like this,

 

Now save the conf_board.h file.

3.2 Initialize and configure Watch dog timer, Port direction and pin voltage level

The next thing we need to do is to disable watch dog timer, configure the digital pin 10 to output and set its initial logic level to high. This can be either directly written into main.c file or they can also be written in the board_init() function in the src > ASF > common > boards > user_board > init.c file.

We will write this configuration directly into the main.c file. Copy and paste the following code into the main.c under the board_init() function.

After you have added these lines your main.c program should look like the following.

 

The line WDT->WDT_MR = WDT_MR_WDDIS;  disables watchdog timer, the line ioport_init(); is a function call so that the IOPORT functions ioport_set_pin_dir() and ioport_set_pin_level() can be used. The ioport_set_pin_dir(LED0, IOPORT_DIR_OUTPUT);  sets the pin 10 called LED to an output pin, and the ioport_set_pin_level(LED, IOPORT_PIN_LEVEL_HIGH);  sets the initial state of the pin to high.

3.3 Enable sysclk_init() function

The last thing we need to do here is to enable sysclk_init() function. Just add sysclk_init() above the board_init() function as shown in the figure below.

4. Write LED Blinking Program

The next step for this ATMEL Studio 7 Arduino Due User Board Tutorial is to write the LED blinking program in C programming language.

Here we just have to add the indefinite while(1) loop with the setting that the port pin 10 is toggled and write a delay between the toggle. Copy and paste the following code into the main.c file as shown in the figure down below.

 

Here we have used the ioport_toggle_pin_level() function defined in the IOPORT service module that we had added earlier through ASF Wizard. This function toggles pin voltage level between HIGH and LOW after repetition of the while loop for the given pin as parameter which in this case is LED. The delay_ms() creates delay and this function is available to us because we added the delay routine services earlier. The 1000 in delay_ms means 1000ms delay.

5. Build/Compile and Upload

The final step of this ATMEL Studio 7 Arduino Due User Board Tutorial is to build the solution which also compiles the above C program. Then we have to upload the compiled hex code into Arduino Due using the Arduino Due programmer external tool.

To build/compile the project solution and the C program in ATMEL Studio 7, click on the Build then Build Solution. This is as shown below.

In the output window you should get message Build Succeeded.

Then to upload the hex code into Arduino Due using Bassac.exe upload programmer, click on the Tools > Arduino Due Programmer.

After the upload the LED connected to the digital pin 10 on the Arduino Due board should now be blinking.

If you did not understand anything above, please leave your questions as comment below. See other Arduino Tutorials.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

LED blinking C program for Arduino

LED blinking C program for Arduino

Reading analog signal with Arduino Due and Simulink Part 1 tutorial