Arduino Due With Atmel Studio 6.1 and ASF

Hi all.

I am currently using the Atmel Studio 6.1 and the Atmel Software Framework with my Arduino Due. The reason I am doing this is the Arduino IDE keeps a lot of things under the hood that I am trying to see. So, I am using the Atmel Studio 6.1 in order to better learn the Cortex M3 ARM microcontroller.

Unfortunately, the complexity has already gotten the best of me. I searched for the answers myself but came up empty-handed.

What I am basically trying to do is a simple program to toggle a pin, say what is commonly referred to as PIN 13 on the Arduino Due board, which is LED "L". I used the New Project template, specifying the Arduino Due board and also added the GPIO, System Clock, and Delay ASF libraries. I cannot get the LED to flash on and off at a 1 Hz rate, and I am stumped as to why. Here's the body of my main:

int main (void)
{
	board_init();
	sysclk_init();
	delay_init(sysclk_get_cpu_hz());
	
	

	pio_set_output(PIOB, LED0_GPIO, HIGH, DISABLE, DISABLE);
	
	

	while(1)
	{
		gpio_set_pin_high(LED0_GPIO);
		delay_ms(1000);
		gpio_set_pin_low(LED0_GPIO);
		delay_ms(1000);
	}
	
	

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

I tried some of the other gpio and pio configure and set functions to no avail. It would be nice to have documentation that shows how the ASF relates to the chip itself. For instance, to configure the port bin PB_27 as output, first do this then that. I have yet to find any documentation like this, so I have resorted to trial and error.

Oh, I am also aware that the Atmel database has an example on PWMing PWM Channels 0 and 1 to make an LED glow, but my scope shows nothing is happening. I believe I am programming the board correctly with bossac, as the verify portion of the programming says it was successful.

Does using 1.5.x of the IDE result in the behavior that you want? In other words, is it a hardware or a software problem?

The answer is...both. Using the Arduino 1.5.2 IDE results in the behavior I want, which is a blinking LED on my board.

I actually resolved my issue. It's a combination of following an outdated video from Atmel using a deprecated ASF API and my burning the wrong file onto the chip. I was using the gpio configure and set functions. They have actually been superceded by the IOPORT functions. Additionally, I was burning the hex instead of the bin file. Here's my code:

#include <asf.h>

#define MY_LED    IOPORT_CREATE_PIN(PIOB, 27)

int main (void)
{
	board_init();
	sysclk_init();
	delay_init(sysclk_get_cpu_hz());
	ioport_init();
	
	ioport_set_pin_dir(MY_LED, IOPORT_DIR_OUTPUT);	
	

	

	while(1)
	{
		ioport_set_pin_level(MY_LED, true);
		delay_ms(2500);
		ioport_set_pin_level(MY_LED, false);
		delay_ms(2500);
	}

}

This will cause the LED marked "L" on the Due to flash. It will also cause Pin 13 to go high for 2.5 seconds, then low, cyclically.

Additionally, here is the command to burn the bin file to your Arduino Due board:

@C:\arduino-1.5.2\hardware\tools\bossac.exe --port=COM5 -U false -e -w -v -b  $(TargetDir)\$(TargetName).bin -R

Replace COM5 with the COM port your Arduino Due is connected. Of course, also hold the Erase and Reset button then let go after one second if bossac complains it cannot find a device on the specified COM port. bossac should then be able to locate the device. Also, once the Due is programmed, you may have to press the Reset button on the board to start the program. On my board, I have had to do this even though I specified the "-R" parameter to bossac.exe

Next, can you explain how:

	ioport_init();
	
	ioport_set_pin_dir(MY_LED, IOPORT_DIR_OUTPUT);	
  :
		ioport_set_pin_level(MY_LED, true);

is less "under the hood" than pinMode/digitalWrite? :slight_smile:

If you want to learn Atmel CM3, you should probably ignore most of ASF...

I am using the Atmel Studio 6.1 in order to better learn the Cortex M3 ARM microcontroller.

Very little of ASF is about the Cortex M3 core. All ASF I/O except the Nested Vector Interrupt Controller is SAM3X8E specific and doesn't apply to other CM3 processors.

I see a lot of "I want to learn more about ARM programming" threads, but the truth is 99% of programming on ARMs is nothing to do with the ARM CPU, it's all about peripherals or just plane C code.

gpio_set_pin_high(LED0_GPIO) is no closer to the hardware than digitalWrite() in terms of learning anything, it's just another name for a function to set a pin. As noted, even if you get down and dirty with the port control registers that's nothing to do with "ARM".

You could start writing code in assembler, that really is learning ARM. Apart from that offhand I think the only ARM things you can play with is the NVIC or the systick counter.


Rob

gpio_set_pin_high(LED0_GPIO) is no closer to the hardware than digitalWrite() in terms of learning anything, it's just another name for a function to set a pin. As noted, even if you get down and dirty with the port control registers that's nothing to do with "ARM".

You could start writing code in assembler, that really is learning ARM. Apart from that offhand I think the only ARM things you can play with is the NVIC or the systick counter.

I'm quickly realizing this as I delve further into the ASF: I'm really just learning what APIs to use and in what order so as to configure the peripherals I want. I may have to go back to the modest days of assembly, like I did when I was playing with the 8051, in order to really learn the hardware. Of course, I have to be pragmatic about things, too. A big reason I am doing this is to say I know how to program the ARM to prospective employers. I think they lean more towards C code bases and API calls rather than expecting a prospective employee to intimately know the chip's architecture well.

I may have to go back to the modest days of assembly ... in order to really learn the hardware.

Nah, it's all there and accessible to C; you just have to bypass the ASF functions. And maybe CMSIS as well. Things are to the point where most ARM vendors document their chips with C examples, so you shouldn't really need to go deeper than that.

say I know how to program the ARM to prospective employers.

I guess. When an employer says "I want someone who programs ARM", what they really want is someone who will be able to sit down and start churning out code with a minimal amount of downtime for training/learning. Unfortunately, with ARM, that can range from "can you write a CMSIS library for a bare metal CMx family" to "can you administer an ARM linux distribution" to "can you write apps for Android?" It seems to be pretty common for "can you program in x" to mean "are you familiar with the standard libraries used to build our sort of applications in our sort of environment. If someone asks you whether you know C#; they're not really wanting you to offer a paper on the technical differences between C# and C++; they want to know whether you can write windows apps, including the libraries/etc. (perhaps ESPECIALLY the libraries/etc.)

Only to say that I just started new blog where I'm writing articles about ASF

Blog is in Italian but also a computer-translated version is avaible with the right panel on website

http://electro-logic.blogspot.it

Feel free to leave comments
Leonardo

I'm just following this now - however, when I go to build the project, my build fails and I get the following errors

'implicit declaratio of function delay_init' and the same for delay_ms

It almost seems I'm not importing something correctly, yet my code is identical to yours? Any ideas?

jtw11:

implicit declaration of function 'delay_init' 

implicit declaration of function 'delay_ms'

@jtw11 I was getting those error messages too. Here's how I got past them in Atmel Studio 6.1 update 2.0 (build 2730)

  • Project menu
  • ASF Wizard
  • Under Available Modules, select Delay routines
  • Add >>
  • Apply

Now it builds without errors, now to get the debugger tool thingie to download to the board...

I am very new to Arduino Due. I tried to compile and upload ARMGuy's example from AtmelStudio 6.1. No error at all. But the LED does not blink. What's wrong I might be doing. Please help. :roll_eyes: