Compile Arduino Sketch for unsupported AVR Processor (specifically ATMega644)

I have a prototype of my product on a breadboard connected to an Arduino Mega 2560. I am now trying to build a custom PCB, and I'd like to replace the processor on the Arduino board (ATMega2560) with the ATMega644 for 2 reasons:

  1. It comes in a 40-pin DIP that will be much easier to solder than the 100-pin surface-mount ATMega2560
  2. It is cheaper and smaller, and the ATMega2560 is clearly overkill for my project

I have access to an atmel board with a 40-pin socket and AVR Studio, so if I have a properly compiled binary I am confident I will be able to use that board to upload the program. Obviously, all the arduino libraries are open-source so I should be able to copy them to appropriate locations and compile my entire sketch within AVR Studio via the Atmel Board, pull my 644 out of the socket and pop it in my custom PCB.

Now, the problem is code compatibility between the 2560 and the 644. According to the datasheets, the special functions of certain pins that my program requires (such as the ADC to read one analog input and the SPI bus to communicate with an Arduino WiFi shield) are not on the same pins on each chip. For instance, the 2560 has an ADC on pins PF0-PF7 and PK0-PK7 while the 644 has its ADC channels on pins PA0-PA7.

After examining the source code, it appears that I can manually alter this mapping between Arduino Board pin numbers and ATMegaXXXX pin numbers by editing the file hardware/arduino/variants/mega/pins_arduino.h. And if I do it correctly, I believe my original, functioning sketch should compile and run successfully on the 644 without any changes to the actual sketch itself (only the underlying library).

So, the questions I'm hoping to have answered are:

  1. Am I missing anything, is there any reason why this wouldn't work?
  2. Has the pin-mapping code for the 644 (or similar) AVR processor already been written? I could not find it anywhere online, but I'm surely not the first person who wants to take an Arduino prototype to a custom PCB.
  3. Is there an easier way to do this?

Thanks,
Tyler

These will have a ton of information about the ATmega1284 family à la Arduino...
https://www.google.com/search?q=bobuino
https://www.google.com/search?q=maniacbug+atmega1284

1. Am I missing anything, is there any reason why this wouldn't work?

Yes it can be made to work. The current Arduino IDE has the ability to add other chip types in a hardware folder placed in the user's sketch directory. There can be placed the file changes and additions need in pin mapping, bootloader hex files, board.txt file, etc.

2. Has the pin-mapping code for the 644 (or similar) AVR processor already been written? I could not find it anywhere online, but I'm surely not the first person who wants to take an Arduino prototype to a custom PCB.

Member maniac started quite a project to get the IDE to support the AVR 1284P 40 pin DIP chip supported which is the same pinout as the 644P. Building on that others have added the minor changes to support the slight differences need to support the 644P chip. However I would suggest to just go ahead and design for the 1284P as it does have the same pin out but adds lots of extra flash and SRAM memory space over the 644P. It's the current king of the hill in AVR DIP packages.

3. Is there an easier way to do this?

So go here and study up on maniac's web page, and check out the links for details. Several of us have 1284P/644P boards up and running with arduino IDE 1.0.3 and there has been lots posted on the forum in the last month or two about our trials and successes, all ending up at happy places.

I'm using the PCB that member CrossRoads sells that uses through hole components and has the standard arduino shield layout. I can run this board with either a 1284P or 644P chip.

Lefty

Is there an easier way to do this?

yeah: notice that it's already been done. Atmega644 support was added as part of the "Sanguino" project back in 2008: http://sanguino.cc/
Support for the ATmega1284, which is similar (same pinout and package) with even more memory was added more recently (as several people have mentioned.) You may have to adjust these packages "some" to work with YOUR pinout and desired Arduino IDE version, but it will certainly be easier than starting from nothing.

Definitely go for the Atmega1284P [get the "P" version] over the 644. You'll get
2X the code space, 4X the RAM space, one more timer, and 2 more PWM channels.