mAVRISP

Table of Contents

    An mbed can be used for In-System Programming [ISP] of AVR microcontrollers over a simple three-wire SPI interface.

    The current version uses the simple AVR910 protocol to flash the Atmel chip.

    The programmer should work with any AVR microcontrollers that support In-System Programming.

    Example

    This example will run through programming an ATmega328 with the mAVRISP program.

    The Arduino IDE can be used to compile code for the ATmega328. The example Blink program that comes with the Arduino IDE can be compiled by first selecting the appropriate target [in this case an ATmega328 (running at the default internal 8MHz oscillator)]:

    http://mbed.org/media/uploads/aberk/arduinoboard.jpg

    And then selecting Sketch -> Verify/Compile:

    http://mbed.org/media/uploads/aberk/arduinocompile.jpg

    Under Windows XP the resulting output files can be found in the following directory:

    \Documents and Settings\username\Local Settings\Temp\build[number].tmp

    This directory will contain a .HEX file with the same name as the sketch. In this case, the name of the file is Blink.cpp.hex. This can be converted to a raw binary file using the free tool hex2bin. Note that you will have to rename the file from name.cpp.hex to name.hex:

    http://mbed.org/media/uploads/aberk/hex2bin.jpg

    The ATmega328 and mbed are now connected up as follows:

    855

    Put the AVR binary and the mAVRISP binary [in that order - otherwise the mbed will try to run the AVR binary!] onto the mbed. Ensure that the AVR binary is either appropriately named according to the #define PATH_TO_BINARY in AVR910.h or pass in the name you're using to the program method (see below).

    If you open up your favourite serial terminal program you should see the following output when you reset the mbed:

    1244

    Put an LED and appropriate resistor across digital pin 13 on the ATmega328 and watch it blink!

    Hello World!

    Import program

    00001 /**
    00002  * Program an AVR with an mbed.
    00003  */
    00004 
    00005 #include "AVR910.h"
    00006 
    00007 LocalFileSystem local("local");
    00008 Serial pc(USBTX, USBRX);
    00009 AVR910 mAVRISP(p5, p6, p7, p8); //mosi, miso, sclk, nreset.
    00010 
    00011 int main() {
    00012 
    00013     int success  = -1;
    00014     int response =  0;
    00015 
    00016     //Read the vendor code [0x1E == Atmel].
    00017     response = mAVRISP.readVendorCode();
    00018 
    00019     if (response == ATMEL_VENDOR_CODE) {
    00020         pc.printf("Microcontroller is an Atmel [0x%02x]\n", response);
    00021     } else if (response == DEVICE_LOCKED) {
    00022         pc.printf("Device is locked\n");
    00023         return -1;
    00024     } else {
    00025         pc.printf("Microcontroller is not an Atmel\n");
    00026         return -1;
    00027     }
    00028 
    00029     //Read part family and flash size - see datasheet for code meaning.
    00030     response = mAVRISP.readPartFamilyAndFlashSize();
    00031 
    00032     if (response == 0xFF) {
    00033         pc.printf("Device code erased or target missing\n");
    00034     } else if (response == 0x01) {
    00035         pc.printf("Device locked\n");
    00036         return -1;
    00037     } else {
    00038         pc.printf("Part family and flash size code is: 0x%02x\n", response);
    00039     }
    00040 
    00041     //Read part number.
    00042     response = mAVRISP.readPartNumber();
    00043 
    00044     if (response == 0xFF) {
    00045         pc.printf("Device code erased or target missing\n");
    00046     } else if (response == 0x02) {
    00047         pc.printf("Device locked\n");
    00048         return -1;
    00049     } else {
    00050         pc.printf("Part number code is: 0x%02x\n", response);
    00051     }
    00052 
    00053     //Open binary file to write to AVR.
    00054     FILE *fp = fopen(PATH_TO_BINARY, "rb");
    00055 
    00056     if (fp == NULL) {
    00057         pc.printf("Failed to open binary. Please check the file path\n");
    00058         return -1;
    00059     } else {
    00060         //Program it!
    00061         pc.printf("Binary file opened successfully\n");
    00062         success = mAVRISP.program(fp,
    00063                                   ATMEGA328P_PAGESIZE,
    00064                                   ATMEGA328P_NUM_PAGES);
    00065         fclose(fp);
    00066     }
    00067 
    00068     if (success < 0) {
    00069         printf("Programming failed.\n");
    00070     } else {
    00071         printf("Programming was successful!\n");
    00072     }
    00073 
    00074 }
    

    Wiring

    Atmel Pinmbed Pin
    MOSIp5
    MISOp6
    SCKp7
    nRESETp8
    VCCVout
    AVCCVout
    AREFVout
    GNDGND

    API

    Import program

    Public Member Functions

    AVR910 (PinName mosi, PinName miso, PinName sclk, PinName nReset)
    Constructor.
    int program (FILE *binary, int pageSize, int numPages=1)
    Program the AVR microcontroller connected to the mbed.
    void setFrequency (int frequency)
    Set the frequency of the SPI communication.
    int readVendorCode (void)
    Read the vendor code of the device.
    int readPartFamilyAndFlashSize (void)
    Read the part family and flash size of the device.
    int readPartNumber (void)
    Read the part number.

    References


    All wikipages