is static libraries for one chip or more?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

when creating a static library in ATMEL STUDIO, it asks me what AVR type you are using, I want that library to be used by several types of AVR chips not only one specified type,so for example if I choose ATMEGA32 as the AVR chip,  then I used that library on other chips (avoiding the use of peripherals that has different way of programming depending on the type we are using ) will it work fine or I should repeat the same static library for each chip?

This topic has a solution.
Last Edited: Fri. Jul 29, 2016 - 07:03 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

You just discovered why no one uses static libraries for AVR.

 

There are 17 different AVR "architectures". The real small chips don't even have MUL. Then there are some that only have RJMP/RCALL but no JMP/CALL. Then those that use LPM and those that use ELPM.

 

This is why you find:

avr8-gnu-toolchain-linux_x86_64/avr/lib$ find . -name libc.a
./avr3/libc.a
./avr4/libc.a
./avr5/libc.a
./avrtiny/libc.a
./avrxmega6/libc.a
./avr31/libc.a
./avr25/tiny-stack/libc.a
./avr25/libc.a
./avr35/libc.a
./avr51/libc.a
./tiny-stack/libc.a
./avrxmega5/libc.a
./avrxmega4/libc.a
./libc.a
./avrxmega2/libc.a
./avrxmega7/libc.a
./avr6/libc.a

These are all static libraries and they all contain the "standard" C functions like strcpy(), printf(), malloc() and so on. There are 17 of them because the 300+ AVRs split into one of these 17 architectures. When you build a program for AVR and call strcpy() or printf() or something the compiler will choose to link against one of these 17 depending on which model (-mmcu=) you build for.

 

So if you were to build library code for AVR you would need to build at least 17 different ones just like this.

 

But life is not quite that simple. Say you were building a UART library. First some have UDR0, UCSR0A, etc and some have UDR, UCSRA, etc but beyond that some have subtley different bit names and usages too. So you may need code like:

#if mega8 or mega16 or mega32
 USCRA = x;
#elif mega88 or mega168 or mega328
 UCSR0A = x;
etc.

but that is a build time decision. Not a run time decision. So this would not work in a binary library. For that it would need a run-time determination of the target:

 cpu = determine_cpu()
 if (cpu == mega8) || (cpu = mega16) || (cpu == mega32)
    USCRA = x;
 else if (cpu == mega88) || (cpu == mega168) || (cpu == mega328)
    UCSR0A = x;
etc.

(which is further complicated by the fact that a mega16 doesn't know a register called USCR0A etc.)

 

Bottom line. Just given them a .c file and a .h file to match. Forget .a, it ain't gonna work.

 

It does seem such a shame that Atmel included "create static lib" project in Studio when there's no chance (in reality) of anyone ever using it!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I understand now, thanks. So we can't use the #define in the static libraries because they are not time decisions (their object code is already made before), so instead we use the .c and .h files because they are run time decisions as they are source code- not object code- and they are compiled within the program we are putting them into 

Last Edited: Fri. Jul 29, 2016 - 06:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yup, you got it. Lots of examples of such "libraries" out there to see how others have done this. Google "stang" or "fleury" perhaps?