Trying to port ChibiOS to ATmega64

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderators: utzig, tfAteba

Liss
Posts: 2
Joined: Tue Feb 10, 2015 11:09 pm

Trying to port ChibiOS to ATmega64

Postby Liss » Tue Feb 10, 2015 11:49 pm

I would like to use ChibiOS in ATmega64. The problem is, currently ChibiOS freezes on start, I'm not sure why, so I would greatly appreciate any clues or ideas.

Only change I had to make to ChibiOS compile for ATmega64 is to add these defines at the top of board.c right after its #includes:

Code: Select all

#if defined(__AVR_ATmega64__)
  #define OCIE0A OCIE0
  #define OCR0A OCR0
  #define TIMSK0 TIMSK
#endif

Then I was able to successfully compile chBlink example (it supposed to blink LED with two tasks and a semaphore, it works for me on ATmega2560). Unfortunatelly, it does not work on ATmega64 because ChibiOS halts before turning on the LED.

So far I found out that ChibiOS on ATmega64 fails to reach chSetup() function after chBegin(chSetup) call (I tried to place code to turn on the LED in the beginning of chSetup, it never lights up).

I would appreciate any ideas why ChibiOS could halt somewhere within chBegin() function or how to debug this issue...

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: Trying to port ChibiOS to ATmega64

Postby utzig » Wed Feb 11, 2015 12:17 am

Try also changing:

Code: Select all

CH_IRQ_HANDLER(TIMER0_COMPA_vect)


to:

Code: Select all

CH_IRQ_HANDLER(TIMER0_COMP_vect)


The timer 0 vectors are different numbers for these families.

Cheers,
Fabio Utzig

Liss
Posts: 2
Joined: Tue Feb 10, 2015 11:09 pm

Re: Trying to port ChibiOS to ATmega64

Postby Liss » Wed Feb 11, 2015 1:42 am

Thank you very much! It is so obvious, can't believe I missed this. Now it works.

To make this thread as useful as possible I describe all the steps I did to make ATmega64 work properly with ChibiOS and Arduino environment:

1) To add ATmega64 support to Arduino IDE download hardware.zip and install to ~/sketchbook. This will add support for many ATmega and ATtiny chips including ATmega64. Here is the original article where I found the file (it is in Japanese and you do not need to read it, I link to it just to give credit to the author). Note: at the moment of writing, this is intended for Arduino IDE 1.0, so if you are using 1.5/1.6 version of Arduino IDE, you may need to read Arduino Hardware Cores migration guide from 1.0 to 1.6.

2) Edit hardware/atmega64/boards.txt and add "memduino64_8MHz.upload.using=arduinoisp" right after upload.maximum_size. This is needed so Upload button in the IDE will use ISP programmer by default (in my case Mega with Arduino ISP sketch). In IDE don't forget to set Programmer to "Arduino as ISP" (or whatever programmer you are using) and burn dummy "bootloader" to set fuses, this will make ATmega64 run at 8Mhz without external crystal.

3) We need small change in hardware/atmega64/cores/arduino/wiring.c to make functions such as delay() or chThdSleepMilliseconds() to work correctly on ATmega64:

Code: Select all

--- hardware/atmega64/cores/arduino/wiring.c.orig    2013-05-17 20:20:20.000000000 +0000
+++ hardware/atmega64/cores/arduino/wiring.c  2015-02-11 00:50:30.354521529 +0000
@@ -201,8 +201,8 @@
 #endif 
 
        // set timer 0 prescale factor to 64
-#if defined(__AVR_ATmega128__)
-       // CPU specific: different values for the ATmega128
+#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
+       // CPU specific: different values for the ATmega64 and ATmega128
        sbi(TCCR0, CS02);
 #elif defined(TCCR0) && defined(CS01) && defined(CS00)
        // this combination is for the standard atmega8

4) Get ChibiOS for Arduino from here and install it to Arduino libraries.

5) Apply the following patch to board.c:

Code: Select all

--- utility/board.c.orig       2015-02-11 00:00:26.046434832 +0000
+++ utility/board.c    2015-02-11 00:01:45.828170957 +0000
@@ -27,7 +27,14 @@
 // WHG Arduino
 #include "ch.h"
 #include "hal.h"
+#if defined(__AVR_ATmega64__)
+  #define OCIE0A OCIE0
+  #define OCR0A OCR0
+  #define TIMSK0 TIMSK
+CH_IRQ_HANDLER(TIMER0_COMP_vect) {
+#else
 CH_IRQ_HANDLER(TIMER0_COMPA_vect) {
+#endif
   CH_IRQ_PROLOGUE();
   chSysLockFromIsr();
   chSysTimerHandlerI();

6) Enjoy!


Return to “AVR Support”

Who is online

Users browsing this forum: No registered users and 4 guests