go_away

Author Topic: WinAVR and AVRlib ( not AVRlib-c !!!! )  (Read 8854 times)

0 Members and 1 Guest are viewing this topic.

Offline Steel_monkeyTopic starter

  • Full Member
  • ***
  • Posts: 85
  • Helpful? 0
WinAVR and AVRlib ( not AVRlib-c !!!! )
« on: April 27, 2008, 04:28:42 AM »
I am trying to use AVRlib http://hubbard.engr.scu.edu/avr/avrlib/docs/html/index.html with WinAVR 200712.. , particularly UART procedures, but failed to get reasonable output. First of all, I moved  files I need (  uart.c uart.h) and files included with them (global.h buffer.h buffer.c avrlibdefs.h avrlibtypes.h ) into my WinAVR project folder. Then, I added  includes to *.h files

Code: [Select]
#include "uart.h"
#include "global.h"
#include "buffer.h"
#include "avrlibdefs.h"
#include "avrlibtypes.h"

Then, I put uartInit() into my program.
While compiling, I got  main.c:108: undefined reference to `uartInit'
Which follows linking, so it is linker error.

Second attempt,I included *.c files
Code: [Select]
#include "uart.h"
#include "uart.c"
#include "global.h"
#include "buffer.h"
#include "buffer.c"
#include "avrlibdefs.h"
#include "avrlibtypes.h"
Now, linking works, but all code from library sources is added to my project, so it grows in wights unacceptably.

Third attempt, I added Makefile

Code: [Select]
# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c buffer.c uart.c

and included only *.h files.
Code: [Select]
#include "uart.h"
#include "global.h"
#include "buffer.h"
#include "avrlibdefs.h"
#include "avrlibtypes.h"

Same result as above- all library code included ( even if uartInit() is removed from my program code).
My goal is use this library as standard AVRlib-c from WinAVR installation folder- it includes to final hex only things I use in program, and not all code from AVRlib-c. Unfortunately, I have not find any really valuable info.




« Last Edit: April 27, 2008, 04:36:45 AM by Steel_monkey »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #1 on: April 28, 2008, 09:35:41 AM »
Its not working because your library isn't in the search path.

In AVR Studio, go to Project->Configuration Options->

and then do either the Include Directories or Libraries tab to add in your library.

Offline Steel_monkeyTopic starter

  • Full Member
  • ***
  • Posts: 85
  • Helpful? 0
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #2 on: April 28, 2008, 11:14:42 AM »
I am not using AVRstudio, Programmer Notepad only. As I said, all required files ( *.c and *.h) are placed at the project folder. And I include them with #include "..." meaning this file is in project folder. Should I add path to makefile for linker to start working?

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #3 on: April 29, 2008, 03:05:05 PM »
Are you absolutely sure that *everything* is in the very same folder?

Offline Steel_monkeyTopic starter

  • Full Member
  • ***
  • Posts: 85
  • Helpful? 0
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #4 on: April 30, 2008, 01:54:16 PM »
Yes, I am. Everything is there.
I also trird to add *.c files to sources in makefile for this project, like in UART tutorial:
SRC = $(TARGET).c  buffer.c  uart.c
But as it is easy to predict, it again added all code from buffer.c and uart.c to my hex, and it became 2400 bytes instead of 400.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,121
  • Helpful? 108
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #5 on: April 30, 2008, 04:25:48 PM »

Quote
SRC = $(TARGET).c  buffer.c  uart.c
But as it is easy to predict, it again added all code from buffer.c and uart.c to my hex, and it became 2400 bytes instead of 400.
Yes - if you add the c files to your project then they will always get linked.

What you need to do is create a library of the files you download - ie it ends with
Code: [Select]
.a
I'm not sure if your download already includes one - as the original link you gave seems to be broken. If the library file is there then fine. If not then you need to create a makefile in the library folder (or modify any existing one). The key part to change is:
Code: [Select]
all: $(PRG).a
Which says the final thing to create is the library file

Later in the makefile you need something like:
Code: [Select]
# make the library file from all of the object files
$(PRG).a: $(OBJ)
avr-ar -v -ru $(PRG).a $(OBJ)
Which creates the library file and adds all the compiled object files into it.

Now in your main project - you should really change the #include command to point to the files in their original location so that you dont copy the files around. Or you can pass additional source file paths for the compiler to search via. Cant remember how off the top of my head - worry about that later - so keep the copied header files in your project for now but delete the copied C files.

Now change the makefile for your project as follows:

1. Define a variable that points at the exact location where the library file got created - ie you will need to change this line to match what came out of the earlier makefile.
Code: [Select]
LIBS           = ..\avrlib\lib.a

2. When creating the elf file you need to add the above library to the list of libraries that the linker will use
Code: [Select]
$(PRG).elf: $(OBJ)
avr-gcc -o $@ $^ $(LIBS)

The linker will then only pull in the required code from your library file.

If this doesn't help then perhaps you could post the current makefile(s) for the library and for your project.

NB There is one thing to note with this approach:- If the library code compiles differently for different processors and CPU speeds then the compiler library file will be specific to the values at the time it was compiled. So if you then 'blow a fuse' and change your processor speed you may well need to recompile the libraries as well as your own code.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Steel_monkeyTopic starter

  • Full Member
  • ***
  • Posts: 85
  • Helpful? 0
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #6 on: May 01, 2008, 09:05:04 AM »
Great thanks for answer!
In AVRlib-c manual it is told that an archive ( file with *.a extension) contains object modules. When there is function call from source being compiled, linker searches that library and find necessary object module. Then, it adds all object module code into application, even if we have ten functions in that object module, and only one is used. As I understand it, I can make library from two source files ( uart.c and buffer.c) , they will be translated into two object modules (uart.o and buffer.o) , and "burned" into archive. After function call, one of them will be again inserted in full scale (depending of function called). As I understand, for real efficiency, only one function in one object module is required, so  unnecessary code  will not be included into final application code.
Perhaps, one of this links will work
http://hubbard.engr.scu.edu/embedded/avr/avrlib/index.html
http://www.mil.ufl.edu/~chrisarnold/components/microcontrollerBoard/AVR/avrlib/
« Last Edit: May 01, 2008, 09:18:28 AM by Steel_monkey »

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,121
  • Helpful? 108
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #7 on: May 01, 2008, 10:47:08 AM »
The linker works on an object file basis. So if you call one function in 'uart.c' then the whole of 'uart.c' will get included. If 'uart.c' calls functions in another *.c file then that will also get included and so on.

So its good practice to break your *.c files into useable bits. ie if you had a file called 'servo.c' that allows you to control a servo via an output pin or by PWM then if you choose to use the out pin then all the PWM code would never get used but would inflate the size of the file. So you should split it into two files: one with all the core code and another with all of the occassional code. However you only need one *.h file.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Steel_monkeyTopic starter

  • Full Member
  • ***
  • Posts: 85
  • Helpful? 0
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #8 on: May 03, 2008, 02:52:19 AM »
I decided just to use source code from this libraries  :) . It looks like simplest solution. Also functions are told to be overheaded-  for example, for UART transmitter/receiver circular buffer is used.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,121
  • Helpful? 108
Re: WinAVR and AVRlib ( not AVRlib-c !!!! )
« Reply #9 on: May 03, 2008, 06:53:23 PM »
Cut and paste is always the easiest answer  ;D But gives problems later when you have many robot programs and copies of the same files all over the place.

But, anyway, good luck!!
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list