Arduino on ATmega169P

Hi,
I'm currently in the process of porting the Arduino platform to ATmega169P.
So far, I have

  • created variants/atmega169p/pins_arduino.h and
  • updated cores/arduino/wiring_analog.c (based on arduino 1.0.5).

I attached both files.

Then there are the following files in cores/arduino/ for which it seems (at first glance) that they don't need any update. But I will check them again, once I need any of the functionality or I find the time:

  • WInterrupts.c
  • Tone.cpp
  • HardwareSerial.cpp
  • wiring_private.c

I don't have a bootloader yet. But it's possible to use JTAG and the "Upload using programmer" menu item, for this I created the following entry in /etc/programmers.txt:

jtag1.name=USB JTAG
jtag1.communication=serial
jtag1.protocol=jtag1

I also had to update the "desc" in the m169 section of /etc/avrdude/avrdude.conf:
part

    id               = "m169";
    desc             = "ATMEGA169P";

I.e. I added a "P" to the desc. This is because - as far as I understand - the Arduino IDE uses the "xxx.build.mcu" entry from the boards.txt for both compiling and uploading. In case of the ATmega169P it should use "atmega169p" for compiling and "m169" or "atmega169" (without "p") for uploading. But I could not find a way to configure this in the boards.txt. Hence I changed the avrdude.conf as described above. Better ideas are appreciated.

In addition I have created a board.txt section for the HR20 (a radiator thermostat) which uses an ATmega169P:

hr20.name=HR20 (not Open Hardware)
hr20.upload.protocol=arduino
hr20.upload.maximum_size=15872
hr20.upload.speed=115200
hr20.bootloader.low_fuses=0x62
hr20.bootloader.high_fuses=0x91
hr20.bootloader.extended_fuses=0xfd
hr20.bootloader.path=optiboot
hr20.bootloader.file=optiboot_atmega169.hex
hr20.bootloader.unlock_bits=0x3F
hr20.bootloader.lock_bits=0x0F
hr20.build.mcu=atmega169p
hr20.build.f_cpu=1000000L
hr20.build.core=arduino
hr20.build.variant=atmega169p

I've compiled, uploaded and run a first sketch (blink of course). Did I forget something? Is anyone else interested in this? I'd like to have optiboot on the ATmega169P at one point in the future, but that's a low priority task for me.

BR,
Frank

pins_arduino.h (7.15 KB)

wiring_analog.c (7.6 KB)

hr20.build.f_cpu=1000000L

Running it at 1 MHz?

Yes, running at 1 MHz probably for powersaving. You can find a nice analysis of the board in use in the OpenHR20 code repository. Unfortunately, it's only available in German.

Is that how you are using it? With the OpenHR20 code?

No, OpenHR20 is not based on Arduino. My aim is to reproduce a subset of what is implemented in OpenHR20, but using the Arduino platform, so that I can re-use Arduino libraries.

#if 0
static const uint8_t LED_BUILTIN = ??;
#endif

LED_BUILTIN is supposed to be a macro rather than a typed constant. Do not #define it if the board does not have an on-board LED. An example for the Uno...

https://github.com/arduino/Arduino/blob/master/hardware/arduino/variants/standard/pins_arduino.h#L47

Brown Out Detection at 1.8 volts?

federvieh:
My aim is to reproduce a subset of what is implemented in OpenHR20, but using the Arduino platform, so that I can re-use Arduino libraries.

Will you be directly driving an LCD display?

Ok, thanks, I was a bit uncertain, if some parts of the platform code use LED_BUILTIN, so I just commented it out for a start. I've now completely removed it.

For getting ATmega169P into the distro, I should probably fork Arduino on github and make a pull request once I know it's working reliably. I still get some compiler warning from a few of the standard files, so there's work to do still.

Yepp, the board is battery powered. Brown out seems to be a wise choice (it's also set by the manufacturer of the HR20 in the original firmware and OpenHR20).

Yes, I already created a first version of an ATmega169PLCD lib. Playing with it, as we speak.

federvieh:
For getting ATmega169P into the distro, I should probably fork Arduino on github and make a pull request once I know it's working reliably.

Excellent. Thank you.

I still get some compiler warning from a few of the standard files, so there's work to do still.

Before knocking out a bunch of warnings I suggest switching to Arduino IDE 1.5. I believe there has been some effort cleaning up the Arduino core.

You may be able to get help with the effort from the Developers List...
https://groups.google.com/a/arduino.cc/forum/?fromgroups#!forum/developers

Yes, I already created a first version of an ATmega169PLCD lib. Playing with it, as we speak.

Nice.

Thanks for the hint regarding 1.5 and the dev list. I've now switched to the latest 1.5 nightly build. Some things are done differently in 1.5, so I had to change my programmers.txt:

jtag1.name=USB JTAG
jtag1.communication=serial
jtag1.protocol=jtag1
jtag1.program.protocol=jtag1
jtag1.program.tool=avrdude
jtag1.program.extra_params=-P{serial.port}

and boards.txt:

hr20.name=HR20 (not Open Hardware)
hr20.upload.protocol=arduino
hr20.upload.maximum_size=15872
hr20.upload.speed=115200
hr20.upload.tool=avrdude
hr20.bootloader.low_fuses=0x62
hr20.bootloader.high_fuses=0x91
hr20.bootloader.extended_fuses=0xfd
hr20.bootloader.path=optiboot
hr20.bootloader.file=optiboot_atmega169.hex
hr20.bootloader.unlock_bits=0x3F
hr20.bootloader.lock_bits=0x0F
hr20.build.mcu=atmega169p
hr20.build.f_cpu=1000000L
hr20.build.core=arduino
hr20.build.variant=atmega169p

About the warnings in 1.5: I was so happy, that I didn't see any warnings anymore and then I discovered this thread discussing warnings in 1.5:
https://groups.google.com/a/arduino.cc/forum/?fromgroups#!topic/developers/5iaId6nPzqA

Long story short: I don't get any warnings, because warnings have been disabled in 1.5 even in verbose mode. :fearful:

Now I'll go figure out how to enable them again. XD

To whom it may concern, I've created a fork on github, after hunting down three compiler warnings. One warning remains. I've started a topic on the developer mailing list, but I assume it takes some time to show up there.

Here's the code:

federvieh:
Now I'll go figure out how to enable them again. XD

You may be able to modify the platform.txt file to get what you want.

In any case, please add your 2¢ to that thread.

federvieh:
To whom it may concern, I've created a fork on github, after hunting down three compiler warnings. One warning remains. I've started a topic on the developer mailing list, but I assume it takes some time to show up there.

The most effective way to get changes added to the Arduino software is to...

  1. Create an issue here...
    Issues · arduino/Arduino · GitHub
    I suggest keeping the description very short. Something like "fixed warnings in whatever".

  2. Create a "pull request" referencing the issue you created. I don't know how to do that.

Well, the warnings were actually introduced by me, when adding the new processor. I'll create the pull request, once I figured out, how to proceed with the remaining warning. Maybe someone cares to help:
https://groups.google.com/a/arduino.cc/forum/m/?fromgroups#!topic/developers/-_pSWRcdme8

It depends on what you want the core to do with timer 2. For supported processors, all timers except 0 are configured for Phase Correct PWM. Essentially they are initialized to support analogWrite. If you want analogWrite to work on the OC2A pin (B7, physical pin 17) then the warning will have to be corrected.

I suspect there are a few other spots that will also need attention for OC2A to support PWM.

Okay, thanks. I fixed it in wiring.c, so that OC2A can be used for analogWrite and uploaded to my github repository.

Do you have any idea, which sections this might be. I only figured I should set the responsible timers in pins_arduino.h, which I did.

This looks like a problem...
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/wiring.c#L257

This looks like a problem...
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Tone.cpp#L177

I found them by grepping for TCCR2B.

This was the warning we were discussing earlier, so that was already fixed:

In my first post I mentioned, that I didn't look at Tone.cpp until later, but ok, why not fix it now. Here you go:

Unfortunately, I can test neither tone nor PWM, since I lack a real development board for the ATmega169P. (Edit: Maybe I should mention that it does compile the tone examples without errors or warnings.)

Thanks a lot for your feedback and support!