AVR CAN Bus Project – Step 1: Programming AT90CAN128 with Arduino
Last month, I bought a Nissan Leaf EV. It’s pretty cool driving around in an all-electric car. Luckily, there’s a great forum called MyNissanLeaf, where Leaf owners can learn a lot, and share information. I’m currently collaborating with one of the forum members to design and build a Level 2 EVSE. I will document that project on this blog at a later date. Having a serious case of project ADHD, I discovered that forum members had set about hacking the Leaf’s CAN buses, and couldn’t resist joining the fray.
Being most familiar with ATmel AVR microcontrollers and Arduino, I decided to use that platform for this project. ATmel has a subtype of the AVR with CAN bus capabilities, the AT90CANxxx. I found an interesting development board containing an AT90CAN128 and headers for access to all the pins on the MCU. I ordered one from Sparkfun for $29.95. They also sell another board, the AVR-CAN, but it contains things that I don’t need, such as an RS-232 interface, and can be programmed only via JTAG – I only have a USBtinyISP, which is an ICSP programmer. Unlike the AVR-CAN, the AT90CAN128 Header Board doesn’t contain a CAN Bus transceiver. I decided to go with the Microchip MCP2551, since that’s what the AVR-CAN uses.
The AT90CAN128 header board is from Olimex, and comes in a cute little box:
Here’s a closeup of the front:
and the back:
I decided to try to get this board working with Arduino, since it’s a lot easier to set up than WinAVR. The first problem to solve is how to adapt the Arduino IDE to work with the AT90CAN128 and my USBtinyISP. After much Googling, I found SuperCow had already done the dirty work and posted it to the Arduino forum. He packaged the core files, bootloader, and a couple of examples into a handy zip file (which has since been taken offline). Since he used an unknown JTAG programmer, I had to adapt his files to work with the USBtinyISP and JTAG ICE mk1. Also, I modified them to work with Arduino 1.x+.
You can download the latest version from github: https://github.com/lincomatic/AT90CAN To install it, simply unzip the atcan90 directory into <your arduino directory>/hardware/at90can. Next time you restart Arduino, you can select it from Tools->Board->[usbtinyisp]AT90CAN128.
If you have a JTAG ICE mk1 programmer instead, select [JTAG ICE mk1]AT90CAN128.
I connected up my USBtinyISP via the 10-pin ICSP header, burned the Blink sketch, hooked up an LED, and bingo! It’s working flawlessly.
In at90can/cores/at90can, I found can_lib.h and can_lib.cpp, which appear to be all we need to interface to the CAN bus. Since I currently know ZERO about CAN bus, I have a lot of reading to do before I can commence programming. SuperCow’s original zip file contains a couple of rudimentary examples.
Before I start programming, I need to build a little interface board containing the MCP2551 CAN bus transceiver, which should arrive next week. I already have an OBD-II cable to tap into the Leaf CAN bus via the OBD-II connector. Currently, obdcables.com is having a special on the 9-ft model.
Downloads: https://github.com/lincomatic/AT90CAN
Next: AVR CAN Bus Project – Step 2: Programming Low Fuse





There are 14 Comments to "AVR CAN Bus Project – Step 1: Programming AT90CAN128 with Arduino"
Nice your using my school project’s at90can to arduino.
Yup, thanks!
The link to the SuperCow lib is down.
God work. Very interesting.
You should use my adapted file instead: http://blog.lincomatic.com/wp-content/uploads/2011/07/at90can_arduino.zip
Can you put the JTAG programmer to arduino? Please.
Thanks
Check the article above. I just added info on how to use Arduino with JTAG ICE Mk1
Thanks but now I have a problem when I upload the program. The program shows me a sincronize error. Have I made a mistake? Which can be the reason?
Are you using a JTAG ICE Mk1? Does it work when you use avrdude directly from the command line?
My error is this:
Binary sketch size: 1086 bytes (of a 126976 byte maximum)
avrdude: jtagmkI_open(): failed to synchronize to ICE
avrdude: jtagmkI_close(): unsupported baudrate -1
Can you help me? please
I using this USB JTAG debugger:
http://www.olimex.com/dev/arm-usb-ocd.html
Regards
I don’t have one of those. If you can get it to work with avrdude, then I can help you get it working with Arduino. But it has to work with avrdude, or you’re out of luck.
Hello.
I´m a frind of Ivan. We are looking for a new programer, may be this?
http://www.atmel.com/Images/AVRISPmkII_UG.pdf
It seems to be a 6 pins connector and the board is a 10 pin one…
You can adapt it for a 10-pin.. http://www.hardwarebook.info/AVR_ISP
But I thought you guys wanted JTAG. The AVRISP is an ICSP programmer, not JTAG.
Hi lincomatic,
glad you continued supercow’s work. I downloaded the repo and copied the at90can dir into the hardware subfolder of the arduino installation. I tried to do a dry-test (no hardware yet, but compiling should be successful). So I selected the USBtinyISP and the corresponding board entry but compiling fails for the blink example:
C:\Program Files\Arduino\hardware\at90can\cores\at90can\main.cpp: In function ‘int main()’:
C:\Program Files\Arduino\hardware\at90can\cores\at90can\main.cpp:5: error: ‘init’ was not declared in this scope
C:\Program Files\Arduino\hardware\at90can\cores\at90can\main.cpp:7: error: ‘setup’ was not declared in this scope
C:\Program Files\Arduino\hardware\at90can\cores\at90can\main.cpp:10: error: ‘loop’ was not declared in this scope
Can you imagine what I have done wrong?
Another question: I’ve got a AVRISP mkII programmer. Is ist enough to define following in the programmers.txt:
avrispmkii.name=AVRISP mkII
avrispmkii.communication=usb
avrispmkii.protocol=stk500v2
and this in boards.txt:
aat90can128.name=[avrispmkii]AT90CAN128
aat90can128.upload.using=AVRISP mkII
aat90can128.upload.maximum_size=131072
…(and the rest corresponding..)
Thanks,
juergen
(heavily trying to learn about Atmel/Arduino world of programming)
Did you restart arduino after copying the files? It should work.
You can try this pre-configured version and see what you did wrong:
http://www.reprap-usa.com/download/arduino0022.zip
I don’t have an avrispmkii, but it looks like your configuration should work.