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 23 Comments to "AVR CAN Bus Project – Step 1: Programming AT90CAN128 with Arduino"

  • SuperCow says:

    Nice your using my school project’s at90can to arduino.

  • Iván says:

    Can you put the JTAG programmer to arduino? Please.


  • Iván says:

    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?

  • Iván says:

    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:



  • lincomatic says:

    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.

  • justr says:

    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

    and this in boards.txt:
    aat90can128.upload.using=AVRISP mkII
    …(and the rest corresponding..)

    (heavily trying to learn about Atmel/Arduino world of programming)

  • Dario Armellin says:

    Hi, i’ve tryed your method for an AVR-CAR, i made an adapter with this schematic


    But i had no connection with the USBtiny, what i should try ?

    • lincomatic says:

      Sorry, I’m not familiar with AVR-CAR. What is it?

      • Dario Armellin says:

        Sorry, misspell the AVR-CAN :)
        I bought the AVR-CAN instead of the board that you’ve used hoping that it could had worked the same but with no luck.
        I’ve tryed to map on an adapter the same pin that in your board are mapped to the ICSP connector according to the schematic i’ve posted but i had no luck and i don’t know what is the problem.
        I was suprised it didn’t worked since the IC is the same an all the pin that the ICSP uses on the header board are unused in the AVR-CAN board, i just connected the to the USBtinyISP connector.
        I’ve ordered a Jtag programmer to see if with that i will be able to burn the arduino boot loader, but i still don’t figure out why it does not work like i did.
        Thanks for your help.

        • lincomatic says:

          The AVR-CAN really is compatible.. I based my design on it. Also, I have a friend who uses it, instead. You don’t need a JTAG programmer to burn a bootloader – a USBtinyISP will work just as well.

          • lincomatic says:

            OK, now I see what you meant in your original post.. your wiring for the ICSP looks correct. Unless you hooked it up wrong, the problem might be that the SPIEN bit is turned off in the high fuse, which would disable serial programming.

          • Dario Armellin says:

            Ohh that’s a good news, i will have back a look on the wiring if i done them well, in case how i change the bit you are mentioning about ? Sorry for the silly question but i’m not that much in the arduino low level stuff.
            Thanks again for your help.

  • lincomatic says:

    If SPIEN is turned off, then your easiest option is to use a JTAG programmer. You can switch on the fuse bit with the JTAG programmer, and then you can use either programmer. Just make sure that you don’t turn off the JTAGEN bit when you turn on the SPIEN, or JTAG will be disabled. This is not the vendor I bought from, but a cheap JTAGICE clone like this one is what worked for me: http://www.ebay.com/itm/51-AVR-JTAG-USB-Programmer-ISP-Downloader-Emulator-Debugger-ICE-Buffer-Chip-/171310889811?pt=LH_DefaultDomain_0&hash=item27e2ecbb53

  • Dario Armellin says:

    Thanks for your help, i’ve asked to Olimex what is the default fuse configuration and meanwhile i’ve orderd this jtag, i hope it will work aswell.


    • lincomatic says:

      I think it should work … avrdude works with JTAG ICE. But they don’t say which version it is. It might be prudent to ask them if they know which programmer option to use with avrdude, as there are many options:

      jtag2dw = Atmel JTAG ICE mkII in debugWire mode [C:\WinAVR\bin\avrdude.conf:454]
      jtag2isp = Atmel JTAG ICE mkII in ISP mode [C:\WinAVR\bin\avrdude.conf:446]
      jtag2 = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:438]
      jtag2fast = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:430]
      jtag2slow = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:422]
      jtagmkII = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:414]
      jtag1slow = Atmel JTAG ICE (mkI) [C:\WinAVR\bin\avrdude.conf:407]
      jtag1 = Atmel JTAG ICE (mkI) [C:\WinAVR\bin\avrdude.conf:399]
      jtagmkI = Atmel JTAG ICE (mkI) [C:\WinAVR\bin\avrdude.conf:391]

Write a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>