Running Arduino code on an ATmega4809 Xplained-Pro board...

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

I'm not really that interested in WiFi, so I figured I'd see if I could get the new "Arduino WiFi v2" (which uses the ATmega4809, and mEDBG chip, and an ESP32 module, plus some other stuff) to run on my Xplained-Pro 4809 board.  (without the features that it doesn't have, of course.)

 

It went relatively smoothly copying the Uno2018 entry in boards.txt and changing the upload.protocol from xplainedmini_updi to xplainedpro_updi - uploads worked, and if I manually looked up which pins went where, I could get blink to work, at what seemed to be about the right speed...

 

Serial Communications were more complicated - the Arduino board uses a different uart than the Xplained board.  I THOUGHT I had that worked out, but things still weren't working.   Worse, they didn't work in weird ways - I got SOME traffic on the serial port, but there was what looked like a baud rate error.  WTF - The Mega-0 chips select clocks internally rather than via fuses, and BLINK looked about right - how could the baud rate be 15%+ wrong (I eventually got traffic working by using Serial.begin(8100) to get 9600bps.)  And ... 15% off wasn't the integer multiplier I'd expect if the fuses were wrong, right?

 

So I spent an embarrassingly long time trying to figure this out.  :-(

 

I should have looked more carefully at the clock rate - The 4809 on the Xplained board was running at 20MHz instead of 16MHz.

It turns out that while you can select different oscillators for the CPU at runtime using SFRs, the 16MHz "oscillator" is the same hardware as the 20MHz oscillator, and which frequency it actually runs at is controlled by ... a FUSE value!  (I believe that the fuse determines which of several OSCCAL values get loaded into CLKCTRL.OSC20MCALIBA (except that OSC20MCALIBA isn't much mentioned in the datasheet - the field where its set in iom4809.h is "reserved"...)

 

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

About the 20MHz - why don't you just set correct f_cpu in boards.txt for the Xplained board? But yes it'd be about 20% off not just 15%.

 

Btw: in the Arduino IDE 1.8.7 there is:

    register8_t OSC20MCTRLA;  /* OSC20M Control A */
    register8_t OSC20MCALIBA;  /* OSC20M Calibration A */
    register8_t OSC20MCALIBB;  /* OSC20M Calibration B */

 

in iom4809.h. But yes, in the 0-Series manual it's just mentioned but ommited in detailed description.

Computers don't make errors - What they do they do on purpose.