-1

I have microcontroller ATmega328p and Raspberry PI and i wan to program this avr. Iam using avrdude.

I was trying to compile and make hex file and have a BIG BIG problem.

here is the code :

#include <avr/io.h>
#include <util/delay.h>

int main (void)
{
    DDRB |= (1<<PINB0);      

    while (1){
        PORTB |= (1<<PINB0);  
        _delay_ms(2000);  
        PORTB ^= (1<<PINB0); 
        _delay_ms(2000);  
    }
}

this code execute in atmel studio on my laptop without any problem but on RPI i have one.

first I write in console to make .o file

avr-gcc -Os -mmcu=atmega328p -I/usr/lib/avr/include -c blink.c 

and then i want to link to .elf with:

avr-gcc -mmcu=atmega328p -o blink.elf blink.o

but here i get one error "undefined reference to main" and I was looking on the internet but I have not found any response to exactly this problem. I found only topics about "undefined reference to" char or smth But I don't understand what should i do with this "main". Sorry for my small knowledge about linux terminal etc. I'm new in RPI and linux but I really need help.

Greetings

EDIT,

I have made some SS:

Code from SS here:

/usr/lib/gcc/avr/5.4.0/../../../avr/lib/avr5/crtatmega328p.o:(.init9+0x0): undefined reference to 'main' collect2: error: ld returned 1 exit status

  • Not really, i was looking for error like that but found only 'undefined reference to ' char or smth but i don't understand why I have error with main ? is this linker error ? – Disco Delfin May 12 '20 at 21:26
  • There are many answers to that post. Read more than one of them. None of them have to do with `char` or smth. It's a linker issue. – Ken White May 12 '20 at 21:34
  • Also use the search box at the top of this page and search for **undefined reference to main**, and you'll see several hits here already for that issue. – Ken White May 12 '20 at 21:39
  • 1
    It is odd that the linker cannot find `main`, given that you defined it in `blink.c`. Can you please try running this simple command which attempts to compile your program in one step, and post the *entire* output of it here? `avr-gcc -Os -DF_CPU=16000000 -mmcu=atmega328p blink.c -o blink.elf`. How did you install `avr-gcc` on your Raspberry Pi? – David Grayson May 13 '20 at 00:04
  • Does anything change if you move `blink.o` to **before** `-o blink.elf`? – Mark Setchell May 13 '20 at 00:17
  • @DavidGrayson I have attached SS with entire output in last line of my question. When i was installing i used this commands: `$sudo apt-get install avrdude` and then `$sudo apt-get install gcc-avr avr-libc` I was following steps from here : [link](https://davecturner.github.io/2019/02/23/programming-avr-microcontrollers.html). I Really don't know what is wrong, still searching in stack overflow but nothing helps. – Disco Delfin May 13 '20 at 10:24
  • Please don't add screenshots if they show text. Post the text, please. – the busybee May 13 '20 at 10:34
  • What would `avr-objdump -t blink.o` output? or `avr-nm blink.o` or similar? – KamilCuk May 13 '20 at 10:38
  • @KamilCuk After `avr-objdump -t blink.o` [here is code](https://pastebin.com/tkm4Sm82) and after `avr-nm blink.o` i [I got this](https://pastebin.com/krs0pCSc) – Disco Delfin May 13 '20 at 11:38
  • 1
    Make sure you are compiling the file you think you are compiling. Type `cat blink.c` in the same terminal you run your compilation command. Is this your file? – n. m. could be an AI May 13 '20 at 11:52
  • 2
    So there is no `main` symbol in the object file. Make sure you are using proper files. Remove the `.o` file and recompile it. – KamilCuk May 13 '20 at 12:12

1 Answers1

0
sudo apt-get install avrdude gcc-avr avr-libc

main.c

int main ( void )
{
    return 7;
}

avr-gcc -Os -mmcu=atmega328p -I/usr/lib/avr/include so.c -o so.elf
avr-objdump -D so.elf


    Disassembly of section .text:
    
    00000000 <__vectors>:
       0:   0c 94 34 00     jmp 0x68    ; 0x68 <__ctors_end>
       4:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
       8:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
       c:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      10:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      14:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      18:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      1c:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      20:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      24:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      28:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      2c:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      30:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      34:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      38:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      3c:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      40:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      44:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      48:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      4c:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      50:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      54:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      58:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      5c:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      60:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
      64:   0c 94 3e 00     jmp 0x7c    ; 0x7c <__bad_interrupt>
    
    00000068 <__ctors_end>:
      68:   11 24           eor r1, r1
      6a:   1f be           out 0x3f, r1    ; 63
      6c:   cf ef           ldi r28, 0xFF   ; 255
      6e:   d8 e0           ldi r29, 0x08   ; 8
      70:   de bf           out 0x3e, r29   ; 62
      72:   cd bf           out 0x3d, r28   ; 61
      74:   0e 94 40 00     call    0x80    ; 0x80 <main>
      78:   0c 94 43 00     jmp 0x86    ; 0x86 <_exit>
    
    0000007c <__bad_interrupt>:
      7c:   0c 94 00 00     jmp 0   ; 0x0 <__vectors>
    
    00000080 <main>:
      80:   87 e0           ldi r24, 0x07   ; 7
      82:   90 e0           ldi r25, 0x00   ; 0
      84:   08 95           ret
    
    00000086 <_exit>:
      86:   f8 94           cli
    
    00000088 <__stop_program>:
      88:   ff cf           rjmp    .-2         ; 0x88 <__stop_program>


rm so.o
rm so.elf
avr-gcc -Os -mmcu=atmega328p -I/usr/lib/avr/include -c so.c
avr-gcc -mmcu=atmega328p so.o -o so.elf
avr-objdump -D so.elf

...
00000080 <main>:
  80:   87 e0           ldi r24, 0x07   ; 7
  82:   90 e0           ldi r25, 0x00   ; 0
  84:   08 95           ret
...

No, I am not on a Raspberry Pi, but that shouldn't matter; it's a cross compiler either way. You didn't specify the output file when you broke the compile and link up, which I can appreciate separate linking, especially if you use the linker. But don't always assume the output filename. Sometimes you get a.out instead of whatever.o or whatever.obj.

You can disassemble the object as well

avr-objdump -D so.o

so.o:     file format elf32-avr


Disassembly of section .text.startup:

00000000 <main>:
   0:   87 e0           ldi r24, 0x07   ; 7
   2:   90 e0           ldi r25, 0x00   ; 0
   4:   08 95           ret

and confirm the symbol is in there.

Not finding main generally means means that you didn't provide an input that has that symbol. Your question didn't specify the object file filename in the compile but then linked with a specific file name, nor did you show you confirmed the file name was there and had the symbol or even resembles compiled output for that source.

You indicated Atmel Studio on your laptop, did you try command line tools on your laptop before attempting it elsewhere? If your laptop is not running Linux there are countless live usb or dvd bootable ones that don't require any installation on your computer, you can perform the steps you are doing on Linux on the Pi and see if this is a Pi specific Linux thing or a generic problem.

halfer
  • 19,824
  • 17
  • 99
  • 186
old_timer
  • 69,149
  • 8
  • 89
  • 168
  • so re-reading your edit you tried it from .c to elf. cat the .c file make sure it really has what you think it has in it, is the text editor waiting for you to save changes to the file? or as mentioned are you compiling the same .c file as you are editing, are you in the right directory, etc? – old_timer May 13 '20 at 22:08