Author Topic: Hacking through the Arduino Library  (Read 15463 times)

0 Members and 1 Guest are viewing this topic.

Offline reefatTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Hacking through the Arduino Library
« on: January 07, 2009, 09:47:45 PM »
Today I have got my new Arduino Duemilanove (2009) board (http://arduino.cc/en/Main/ArduinoBoardDuemilanove), and started running the example programs. Everything is so easy and simple! I was little bit curious about their Programming architecture in C/C++. It doesn't use the traditional main() function. Instead of it, Arduino uses two functions: setup() and loop(). How is this working?

  • Are they using any interpreter to interpret Arduino-standard code to traditional C (AVR GCC)?  ???

Before using Arduino, I was using my own designed ATmega8/ATmega168 Board (http://robotalk.codecake.com/2008/09/14/atmega8-development-board/) and AVR Studio 4 with AVRISP mkII. The reason I have jumped (not really jumped; just for curiosity) to Arduino is it's easy-to-use software library.

  • Is it possible to run Arduino Code on my own custom board (specified the link above)?  ???
  • Will it work if I just burn the HEX file to the ATmega168 on my custom board? Does the hardware need to be changed then?  ???

I was writing code using traditional AVR GCC (not C++), which is a big hassle comparing with the easy programming technique of Arduino. That is why I need to switch to their Library, and off course, without using their Hardware.

  • Is it also possible to mix up both programming style: the regular AVR GCC and Arduino C/C++?  ???

As far I know, AVR GCC only supports C-style code, not C++. Please, correct me if I am wrong.

Finally one more question:

  • Is it possible to write an Arduino Library using AVR GCC? Or, do I have to use their style of Coding to build a new library?  ???
« Last Edit: January 07, 2009, 10:00:53 PM by reefat »

Offline Kirk

  • Jr. Member
  • **
  • Posts: 44
  • Helpful? 2
Re: Hacking through the Arduino Library
« Reply #1 on: January 07, 2009, 10:26:48 PM »
If you dig in to the Arduino files you will find a main() function that calls setup and loop.

The only unique things to any other ATmega8/168 board is the boot loader on the chip the USB to serial chip, the way they number the pins and the 16MHz Xtal.
By editing the board.txt file you can write programs in Arduino IDE for other boards with other Xtal speeds. 

I often write code in Arduino IDE and use a parallel progamer to load.

As far as libraries are concerned you need another advisor than I.

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: Hacking through the Arduino Library
« Reply #2 on: January 07, 2009, 11:34:58 PM »
If its the same processor, and the pins are broken out the same, then the hex files are compatible. You can compile the code in the Arduino 0012 IDE and upload the hex from that compilation found in the sketches applet folder using AVR Studio 4 if you like.

This pin map might help:
http://www.arduino.cc/en/Hacking/PinMapping

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: Hacking through the Arduino Library
« Reply #3 on: January 08, 2009, 07:28:24 AM »
Today I have got my new Arduino Duemilanove (2009) board (http://arduino.cc/en/Main/ArduinoBoardDuemilanove), and started running the example programs. Everything is so easy and simple! I was little bit curious about their Programming architecture in C/C++. It doesn't use the traditional main() function. Instead of it, Arduino uses two functions: setup() and loop(). How is this working?

  • Are they using any interpreter to interpret Arduino-standard code to traditional C (AVR GCC)?  ???

No, they are not using any interpreter. They are using a makefile to compile the code into a hex file.

Quote
Before using Arduino, I was using my own designed ATmega8/ATmega168 Board (http://robotalk.codecake.com/2008/09/14/atmega8-development-board/) and AVR Studio 4 with AVRISP mkII. The reason I have jumped (not really jumped; just for curiosity) to Arduino is it's easy-to-use software library.

  • Is it possible to run Arduino Code on my own custom board (specified the link above)?  ???

You can upload that code using your programmer and AVRdude.
Quote
  • Will it work if I just burn the HEX file to the ATmega168 on my custom board? Does the hardware need to be changed then?  ???

Yes, it will work. Nothing needs to be changed to your hardware, but you need to know the pin configuration.
Quote
I was writing code using traditional AVR GCC (not C++), which is a big hassle comparing with the easy programming technique of Arduino. That is why I need to switch to their Library, and off course, without using their Hardware.

  • Is it also possible to mix up both programming style: the regular AVR GCC and Arduino C/C++?  ???

Yes it is possible to mix up both programming styles in Arduino software, because is based on AVR GCC.
Quote
As far I know, AVR GCC only supports C-style code, not C++. Please, correct me if I am wrong.

Finally one more question:

  • Is it possible to write an Arduino Library using AVR GCC? Or, do I have to use their style of Coding to build a new library?  ???

Yes, it is possible. But they are using their allready written libraries to write the new libraries, that's why the style looks a bit different. If you take the code from all libraries linked and paste it in a new file, you will end up having C code. So yes, you can write a library any way you like as long as it compiles good.

Check out the uBotino robot controller!

Offline reefatTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Re: Hacking through the Arduino Library
« Reply #4 on: January 08, 2009, 08:25:03 AM »
Thanks Ro-Bot-X. Now everything seems clear to me. I already used another ATmega chip and programed it with Arduino. But before doing that, I had to burn the ATmegaBOOT_168_diecimila.hex bootloader.

  • Although I used diecimila, my arduino is the newer one : Duemilanove. I didn't find any bootloader file with this name. So, I used diecimila.

I tried to do that from "Tools>Burn Bootloader>w/ AVRISP mkII" in Arduino IDE. It gives me an error avrdude: usbdev_open(): did not find any USB device "usb" . So I just opened my AVR Studio 4 IDE and did that manually.

  • I changed the fuses (Ex, Hi, Lo) and the Lock Bit. Then burned the bootloader. And that works just fine.

The newly born ATmega chip works fine on the Arduino board. But when I plug it onto my custom ATmega board, it doesn't work. Because I have 11.0592 MHz crystal on it. But the chip requires Arduino standard crystal which is 16.0000 MHz. There was my bad luck. I have almost every possible values of crystals (4,8,10,12,18,20MHz), except 16MHz. Now I am looking for a local store in New York where I can go and buy it real quick.

  • Does anyone know any local store in New York where I can buy the crystal? Online order may take long. I can't wait to see my new progress on Arduino.
« Last Edit: January 08, 2009, 08:29:00 AM by reefat »

Offline reefatTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Re: Hacking through the Arduino Library
« Reply #5 on: January 08, 2009, 10:58:30 AM »
I am good now. I am just using my existing crystal 11.0592 MHz. This circuit is being used for Serial Data Transfer at 2400 Bps. Instead of ATmega168, I am now using ATmega8. I just found the source code (inside Arduino directory) for Arduino bootloader for ATmega8. The only thing I changed is the F_CPU value to 11095200 in the bootloader souce. That's it. Now everything is working pretty well.

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: Hacking through the Arduino Library
« Reply #6 on: January 08, 2009, 11:04:49 AM »
You can't use the AVRISPMK2 with both AVR Studio and AVRDude (Which the Arduino IDE uses)

They use different MK2 drivers.

It sounds like you installed AVR Studio 4 first, so that will be the driver that works with the MK2.

Offline reefatTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Re: Hacking through the Arduino Library
« Reply #7 on: January 10, 2009, 04:34:34 AM »
Here happened a new problem. I was using ATmega168 with a crystal 9.2160 MHz to have an accurate BAUD rate. That's why I need to recompile the bootloader of Arduino with the new F_CPU value. I build it, burned it to my ATmega168 and plugged into my proto board. It didn't work.

So, for debugging purpose, I again recompile the bootloader and burned it again. This time with the Arduino compatible F_CPU (16.0000 MHz). Then I plugged it to my Arduino Duemilanove board. And burned the skech of the "Blink LED". Then Arduino IDE shows me the following error:
Quote
Binary sketch size: 1124 bytes (of a 14336 byte maximum)

avrdude: verification error, first mismatch at byte 0x0002
         0x5a != 0x34
avrdude: verification error; content mismatch


Note: I was using the bootloader source from "arduino\hardware\bootloaders\atmega168\ATmegaBOOT_168.c", and AVR Studio 4 to build it.

By the way, if I burn the bootloader "arduino\hardware\bootloaders\atmega168\ATmegaBOOT_168_diecimila.hex", it works fine. The problem is - when I build it myself, it doesn't work.

Is this happening because of different makefile files? I was not using the makefile provided with the bootloader. But I added 3 items from their makefile to my makefile. Which are -DF_CPU=16000000UL, -DMAX_TIME_COUNT=F_CPU>>1 and -DNUM_LED_FLASHES=3

My makefile looks like this:
Code: [Select]
###############################################################################
# Makefile for the project mega168_boot
###############################################################################

## General Flags
PROJECT = mega168_boot
MCU = atmega168
TARGET = mega168_boot.elf
CC = avr-gcc.exe

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2 -std=gnu99    -DMAX_TIME_COUNT=F_CPU>>4  -DNUM_LED_FLASHES=1     -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=mega168_boot.map


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings


## Objects that must be built in order to link
OBJECTS = mega168_boot.o

## Objects explicitly added by the user
LINKONLYOBJECTS =

## Build
all: $(TARGET) mega168_boot.hex mega168_boot.eep mega168_boot.lss size

## Compile
mega168_boot.o: ../mega168_boot.c
$(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
avr-objdump -h -S $< > $@

size: ${TARGET}
@echo
@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
-rm -rf $(OBJECTS) mega168_boot.elf dep/* mega168_boot.hex mega168_boot.eep mega168_boot.lss mega168_boot.map


## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)


Offline wlewis

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
Re: Hacking through the Arduino Library
« Reply #8 on: August 24, 2010, 07:47:26 PM »
Well.. Im also very fond of using the ARDUINO LIBRARIES.. as they are excellent and get the job done. Only thing is im not interested in the Arduino environment.. why would i want that.. im limited in the variety of chips i can use.

THEREFORE...

HELP!!!!!!!!!!! Can anybody help me to TRANSLATE / PORT these libraries to the AVR STUDIO environment???? I tried using porting the arduino eeprom lib and it gets stuck.. not only that but because it uses Cpp files.

I would appreciate any help here.


 


Get Your Ad Here