Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: kashamalasha on February 26, 2015, 08:50:38 pm

Title: [SOLVED] AVR-GCC: setup build process
Post by: kashamalasha on February 26, 2015, 08:50:38 pm
Hi, everybody!

Next question. I'm a newbie at microchip bulk programming.
Earlier I did program the microchip using Arduino IDE, but now I'm trying to setup a proper environment to handy work with them via pure AVR-GCC.
I prefer to use Linux distro on my laptop. And there is why I can't use Atmel Studio, CodeVision or AVRStudio application.
I chose: AVR-GCC, AVR Toolchain and CodeBlocks.
All of these stuff works together fine with one note:
actually I can't properly setup internal clock setting for build process in Code::Blocks. I mean, I am doing next:


But after loading this hex file to the chip, led isn't blink with proper interval. It is blinking much faster than 1 time per second. Like if I setup F_CPU 800000UL. But if I load this code to chip via Arduino IDE it works fine.

My test code:
Code
#define F_CPU 1000000L
#include <avr/io.h>
#include <util/delay.h>

int main(void){
    DDRB = 0x10;
    while(1){
        PORTB |= (1 << PB4);
        _delay_ms(1000);
        PORTB &= ~(1 << PB4);
        _delay_ms(1000);
    }
    return 0;
}

I set two tools in Code::Blocks for setting fuses on microchip by using /usr/bin/avrdude:

    ATTiny45 (int. 1MHz) with next string: "-p attiny45 -P /dev/ttyACM0 -b 19200 -c arduino -U efuse:w:0xFF:m -U hfuse:w:0xDF:m -U lfuse:w:0x62:m"
    ATTiny45 (int. 8MHz) with next string: "-p attiny45 -P /dev/ttyACM0 -b 19200 -c arduino -U efuse:w:0xFF:m -U hfuse:w:0xDF:m -U lfuse:w:0xE2:m"
 

And if I do "avrdude -p attiny45 -P /dev/ttyACM0 -b 19200 -c arduino -v", then I'll get this:

Code
Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9206
avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as FF

avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as FF
avrdude: safemode: Fuses OK (H:FF, E:DF, L:62)

avrdude done.  Thank you.

That's mean that the fuses are correct. And there is why I think that something wrong with the build process in Code::Blocks and AVR-GCC

Here are logs from build systems. The first one from Arduino IDE, second from C::B:
Code
/opt/arduino-1.0.6/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=attiny45 -DF_CPU=1000000L -MMD 
-DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -I/opt/arduino-1.0.6/hardware/arduino/cores/arduino -I/home/me/sketchbook/hardware/attiny/variants/tiny8
 /tmp/build6896905435354427440.tmp/TestFCPU.cpp -o /tmp/build6896905435354427440.tmp/TestFCPU.cpp.o
...
opt/arduino-1.0.6/hardware/tools/avr/bin/avr-gcc -Os -Wl,--gc-sections -mmcu=attiny45 -o /tmp/build6896905435354427440.tmp/TestFCPU.cpp.elf
/tmp/build6896905435354427440.tmp/TestFCPU.cpp.o /tmp/build6896905435354427440.tmp/core.a -L/tmp/build6896905435354427440.tmp -lm
/opt/arduino-1.0.6/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings
--change-section-lma .eeprom=0 /tmp/build6896905435354427440.tmp/TestFCPU.cpp.elf /tmp/build6896905435354427440.tmp/TestFCPU.cpp.eep
/opt/arduino-1.0.6/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom /tmp/build6896905435354427440.tmp/TestFCPU.cpp.elf
/tmp/build6896905435354427440.tmp/TestFCPU.cpp.hex
Binary sketch size: 132 bytes (of a 4,096 byte maximum)

Code
avr-gcc -mmcu=attiny45 -Os -Wall -Os -I/usr/include -c main.c -o obj/Release/main.o
avr-g++ -L/usr/lib -o bin/Release/TestFCPU.elf obj/Release/main.o  -mmcu=attiny45 
Output file is bin/Release/TestFCPU.elf with size 3,66 KB
Running project post-build steps
avr-size bin/Release/TestFCPU.elf
   text       data        bss        dec        hex    filename
     82          0          0         82         52    bin/Release/TestFCPU.elf
avr-objcopy -R .eeprom -R .fuse -R .lock -R .signature -O ihex bin/Release/TestFCPU.elf bin/Release/TestFCPU.hex
avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex bin/Release/TestFCPU.elf bin/Release/TestFCPU.eep



Thanks in advance for any help.

=== SOLUTION ===
I did next changes in Settings => Compiler...
1. Changed the Linker for dynamic libs in Toolchain executables form "avr-g++" to "avr-gcc":
(http://funkyimg.com/p/UBii.png) (http://funkyimg.com/view/UBii)

2. Changed the Search directories for Linker and Compiler from /usr/include to /usr/lib/avr/include and from /usr/lib to /usr/lib/avr/lib:
(http://funkyimg.com/p/UBhD.jpg) (http://funkyimg.com/view/UBhD)

Thanks for help to everyone.
Title: Re: AVR-GCC: setup build process
Post by: oBFusCATed on February 26, 2015, 09:34:41 pm
You can probably test my arduino template if it works correctly with your cpu.
I've tested it with an arduino uno and a micro.

See here https://github.com/obfuscated/cb_arduino_template for the repo and install instructions.
Title: Re: AVR-GCC: setup build process
Post by: kashamalasha on February 26, 2015, 10:43:06 pm
I've tried, but I can't implement it.
Title: Re: AVR-GCC: setup build process
Post by: LETARTARE on February 26, 2015, 11:16:13 pm
Hello,
You can also find 'GRBL' projects 'Code::Blocks' at:
https://github.com/LETARTARE/Mega2560-grbl-0.8xx/tree/devGrbl (https://github.com/LETARTARE/Mega2560-grbl-0.8xx/tree/devGrbl)
Best regards
Title: Re: AVR-GCC: setup build process
Post by: kashamalasha on February 27, 2015, 05:03:27 pm
I fix the problem. Solution is in the first post.
Title: Re: AVR-GCC: setup build process
Post by: scarphin on February 27, 2015, 05:19:16 pm
It might work but I don't think that is the correct solution unless you have special kind of avr-gcc distribution.
Title: Re: [SOLVED] AVR-GCC: setup build process
Post by: kashamalasha on February 27, 2015, 10:46:47 pm
Yep, that's right.
I clean it all and all works fine. I don't why it was there by default.