igormiktor / AVRTools

A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
37 stars 10 forks source link

How to import this Library #4

Closed justmeeeeeee closed 5 years ago

justmeeeeeee commented 5 years ago

Dear Creator,

I am very new at programming an microcrontroller (atmega2560). I want to import this project to my Project in Atmel Studio 7, so I downloaded it and importet all files in the AVRTools-folder to my Project (also the Files ending with .S). I also defined F_CPU in the Projectproperties under Symbols and in the main.c-file. However, there is an error at SimpleDelays.S with: SimpleDelays.o implemented for CPU speeds of 8 Mhz, ...". Is there any tutorial how to import this library? Thank you very much, kind regards!!!

igormiktor commented 5 years ago

Don't know -- never used Atmel Studio.

My guess is that issue is that the assembler files (files ending in .S) in AVRTools are coded using GCC "relaxed" syntax which allows C/C++ style inclusion of headers (#include...), defines (#define...), conditionals (#if...) and comments (/ / & //). However, I vaguely recall reading that Atmel expects either non-relaxed GCC syntax ( .include, .def, and ; for comments) or perhaps a completely different assembler syntax.

In particular, if you look in SimpleDelays.S, you can see the code uses the C preprocessor directives #if ... #elif ... #else ... #end to select the appropriate variant of the assembler implementation for a given CPU speed. If the Atmel Studio assembler doesn't understand C preprocessor directives (or cannot be made to understand them via some option or switch), then this won't work with Atmel Studio.

Sorry, the code was not written for Atmel Studio. If you can figure out what syntax Atmel wants, it shouldn't be too hard to recode the *.S assembler files. In particular, if you just want one speed (e.g., 16 MHz), you can simply delete SimpleDelays.S, and rename SimpleDelays16MHz.inc.S to SimpleDelays.S. Then the transformation is easier: delete/convert all the C/C++ style comments, change #include to .include, #define to .def, etc...

Or even more simply, delete all the SimpleDelayXXX.S files and live without the SimpleDelay functionality (it's not essential to the rest of the library).

Regards,

Igor

justmeeeeeee commented 5 years ago

Dear Igor,

thank you very much for your response, i've now deleted finally all <>.S files, but as soon as I import eg. ArduinoPins.h (#include "ArduinoPins.h"), the following errors appear:

Error recipe for target 'main.o' failed Error unknown type name 'class' Error expected '=', ',', ';', 'asm' or 'attribute' before '{' token Error unknown type name 'bool' (the first 3 of 15) Could this problem be caused by the .cpp instead of .c files?

Which IDE do you use for programming (Which programm is this Library written for?).

Thanks a lot, best regards,

Mike

igormiktor commented 5 years ago

Mike,

Very confusing. Clearly the Atmel compiler doesn't recognize inline assembler ('asm') or gcc attributes. It also doesn't seem to recognize "class" and "bool", but that could either be side-effects of the prior problems or a sign that Atmel is treating the files as C code instead of C++ code (treating .cpp files as if they were .c files). Can't tell for sure. But it seems that the compiler that Atmel uses either doesn't accept any of the GCC "extensions" (inline assembler, attributes) or doesn't speak C++ or needs some special switches or configuration options to compile C++ correctly and accept GCC extensions.

AVRTools is written for the AVR-GCC compiler and AVR-LIBC runtime library. It isn't written for any particular IDE. If Atmel can be configured to use AVR-GCC, or at least to be compatible with it, then the code should work. If not, I'm sorry to say this library is not going to work with Atmel.

Because for microcontrollers the code has to do things "beyond" what "standard" C/C++ can do (e.g., place certain variables at a specific memory address; execute instructions that don't actually exist in C (e.g., instructions to turn interrupts on/off), create functions that don't follow C conventions (e.g., interrupt functions)), the code in AVRTools cannot be "standard C/C++ -- it has to use compiler-specific features (“extensions” to the language) to go "outside the lines" of standard C/C++. In the case of AVRTools, I went with GCC and the GCC-specific "extensions". But these clearly are not portable to whatever compiler Atmel uses under the hood.

BTW I don't use an IDE. I use a vanilla editor to write the code, and then compile the code with AVR-GCC. For most things you can write a makefile to drive the compile+link+upload process. I actually use cmake as my build tool because I have complex builds (e.g., some of my projects generate both RPi code and AVR code for a system that includes both a raspberry Pi and an AVR-based Trinket that talk to each other). But cmake is overkill if you are just getting started with microcontrollers.

Regards,

Igor

justmeeeeeee commented 5 years ago

Thank you very much for your patience, I solved the problem:)

igormiktor commented 5 years ago

Great -- but please share the solution, both for my curiosity and in case someone else has this problem. Are there configuration settings for Atmel that allow it to understand GCC dialects?

justmeeeeeee commented 5 years ago

Of course (it works for 90%):

1) Creating a new Project, click at "GCC C++ Executable Project" and not at "GCC C Executable Project".

2) Delete all Simpledelay...... files.

3) Go to the menu on the top, click project -> properties ->toolchain -> Symbols and add "F_CPU=16000000"

4) Press f7 and add "#define F_CPU 16000000" on the top of every .h file, which appears (I do not know why F_CPU is not defined there)

5) Remove I2cSlave.cpp (there is an error: __vector_39 is already defined).

Now you can import the library and pressing f7 now error will apear anymore:)

igormiktor commented 5 years ago

Excellent. Thanks for sharing.

Re: (4) -- Do you mean you had to manually edit every .h file? F_CPU should be automatically passed when you set it in item (3). That's why it is not defined in every file: F_CPU is designed to be #defined at the compiler level. In my usage, the F_CPU definition is passed as a command-line switch (-DF_CPU=16000000UL) to the g++ compile command. I wonder if instead of "symbols" in step (3) there is an option for "defines" or "precompiler symbols" or "definitions". If so, that might be the place to define F_CPU so you don't have to edit every file.

Re: (5) -- Yes, only one of I2cMaster.cpp or I2cSlave.cpp should be included (as explained in the documentation), depending on whether your executable is designed to operate in I2C "master" mode or "slave" mode.

Thanks again for sharing this.

Enjoy the world of microcontrollers!

justmeeeeeee commented 5 years ago

So, I probably know, where the Miskake causing the F_CPU: I pluged in the Arduino via USB and recreated a new Project, now I just had to delete the .S files and one of I2cMaster.cpp or I2cSlave.cpp, added F_CPU to the symbols and vourla, working:)

Thank you very much for your time and help,

Mike:)

Cjkeenan commented 4 years ago

So looking back at this thread, the solution to this issues is to ignore the SimpleDelay assembly files? Given that, what, if any, functionality am I losing? I am trying to import using MPLAB X v5.35 on an atmega 2560 as well.

igormiktor commented 4 years ago

See the relevant documentation or feel free to review the code.