How to program Arduino Due using user board template in ATMEL Studio 7 | applied electronics engineering

Latest

How to program Arduino Due using user board template in ATMEL Studio 7

By Applied Electronics - Saturday, March 18, 2017 No Comments
In this we will show how to program Arduino Due using user board template in ATMEL Studio 7. Arduino Due uses ARM cortex M3 processor which is 32 bit processor. Programming ARM cortex is not so straightforward as programming other microcontrollers. ATMEL provides ASF(ATMEL Software Framework) to help in programming their line of microprocessors and microcontrollers. It means it can also be used to help us writing program for Arduino Due. In this tutorial we will create a simple LED blinking program but the process can be extended with little modification to work with other kinds of programs and controls.

We recommend to see the earlier tutorial How to program Arduino Due using ATMEL studio 7 which is similar to this. But instead of using custom board or user defined board template as in this case, there, we use the ASF buildin board template.

1. Create GCC C ASF Board Project with User Board template


In ATMEL studio 7, click on File > New Project. Then in the windows that shows up, select GCC C ASF Board Project. Provide some project name and select some directory to save the project files.


In the windows that pops up, write SAM3X to search for ATSAM3X8E microprocessor. Select User Board Template - SAM3X as shown below.


Select and open main.c in the folder ASF on the right side pane.



2. Add Delay routines and IOPORT services


In order for our LED blinking program to work, we will add two ASF services/modules. One is the delay routine service for creating delay between LED ON and OFF states and the other is the IOPORT service for using input/output inbuilt functions(easy to use). The IOPORT service was addded automatically in the GCC C ASF Board Project.

To add these services we use the ASF > ASF Wizard. Open it and add these two services.





After adding them, click on the Apply button then OK in the window that shows up.

You should see now these services in the ASF > common > services folder as shown below.



3. Writing Program


Writing program for the user defined board is little involved. First we must initialize the hardware. What that means is we have to set the watchdog timer, set the port direction, initialize the state of the LED pin, configure clock and define the LED variable.

First lets look at the hardware initialization. 

i)Write the following code below board_init(); in the program code above,

WDT->WDT_MR = WDT_MR_WDDIS; 
ioport_init();
ioport_set_pin_dir(LED0, IOPORT_DIR_OUTPUT);
ioport_set_pin_level(LED0, IOPORT_PIN_LEVEL_HIGH);

After adding these codes you program main.c should like this,

 #include <asf.h>

int main (void)
{
    /* Insert system clock initialization code here (sysclk_init()). */
    board_init();
   
    WDT->WDT_MR = WDT_MR_WDDIS;                    
    ioport_init();     
    ioport_set_pin_dir(LED0, IOPORT_DIR_OUTPUT);       
    ioport_set_pin_level(LED0, IOPORT_PIN_LEVEL_HIGH); 

    /* Insert application code here, after the board has been initialized. */
 }

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

ii) Next, add the following hardware configuration into conf_board.h(src\config\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

// LED
#define LED0    IOPORT_CREATE_PIN(PIOB, 27)

#endif // CONF_BOARD_H

These codes configures the clock and digital pin 13 as LED0. Don't forget to save the file.

Now the hardware configuration setting is completed.

iii) Add sysclk_init() function above board_init().

iv) The final step is to add while(1) loop and use the ioport_toogle_pin_level() function and delay_ms() functions. This was explained in the last tutorial How to program Arduino Due using ATMEL studio 7 in details.

while (1) {
        ioport_toggle_pin_level(LED0);
        delay_ms(1000);
    }

Now the complete code looks like this,

#include <asf.h>

int main (void)
{
 /* Insert system clock initialization code here (sysclk_init()). */
        sysclk_init();
 board_init();
 
 WDT->WDT_MR = WDT_MR_WDDIS; 
 ioport_init();
 ioport_set_pin_dir(LED0, IOPORT_DIR_OUTPUT);
 ioport_set_pin_level(LED0, IOPORT_PIN_LEVEL_HIGH);

 /* Insert application code here, after the board has been initialized. */
 while (1) {
  ioport_toggle_pin_level(LED0);
  delay_ms(1000);
 }
 
}

See figure below also,

4. Build/Compile and Upload


Click on Build > Build Solution

See for any error message. If there isn't any then the Build is success.

Now upload the produced hex file into Arduino Due.


At this point the LED should be blinking.

Now see other arduino tutorials:

No Comment to " How to program Arduino Due using user board template in ATMEL Studio 7 "