AlmuHS
AlmuHS

Reputation: 347

How to assemble to Arduino UNO R3 (atmega 16u2)?

I'm learning about assembly programming for Arduino over GNU/Linux, using as test board an Arduino UNO R3 with AVR Atmega 16u2 microcontroller.

As I read in this article https://www.cypherpunk.at/2014/09/native-assembler-programming-on-arduino/ the instructions must be:

avr-as -g -mmcu=atmega16u2 -o simple_led_blink.o simple_led_blink.s #to assemble

avr-ld -o simple_led_blink.elf simple_led_blink.o #to link

But, when I try to execute this second instruction, it shows this error:

avr-ld: avr:35 architecture of input file `simple_led_blink.o' is incompatible with avr output

I've checked in avr-as architecture support, and this architecture is supported.

What could be the problem?

Update: Finally, as David says, the main microcontroller is the 328p,not 16u2 I use the instructions as been written in the guide, and it runs well

Upvotes: 0

Views: 498

Answers (1)

David Grayson
David Grayson

Reputation: 87486

By running avr-gcc test.c -v -mmcu=atmega16u2 we can tell that the correct AVR architecture version for ATmega16U2 is avr35.

So to compile your assembly, run commands like this:

avr-as -mavr35 -g -o test.o test.s
avr-ld -mavr35 -o test.elf test.o

Note that you might need to pass additional options to the linker to ensure that the program size and RAM size are set correctly, but that will probably not matter if your program is small.

Also note that the ATmega16U2 is not the main processor on the Arduino UNO; the main one is an ATmega328P.

I don't know why the -mmcu option in the tutorial would not work any more. Perhaps the AVR port of binutils was simplified in recent years to only know about AVR architectures and not know about every single chip.

Upvotes: 1

Related Questions