A fork of mAVRISP by Aaron Berk. This version does not use a local file system to hold the AVR program. Instead it uses a serial connection to a PC and a python script to send the AVR program to the mbed.

Dependencies:   mbed

Fork of mAVRISP by Aaron Berk

main.cpp

Committer:
jeroenmbed
Date:
2015-01-31
Revision:
3:df6782d01720
Parent:
1:276f6df4be7a
Child:
4:ceee1eb7062e

File content as of revision 3:df6782d01720:

/**
 * Program an AVR with an mbed.
 */

#include "AVR910.h"

//LocalFileSystem local("local");
Serial pc(USBTX, USBRX);
//AVR910 mAVRISP(PA_7, PA_6, PA_5, PB_6); //mosi, miso, sclk, nreset.
//AVR910 mAVRISP(PB_5, PB_4, PB_3, PA_10); //mosi, miso, sclk, nreset.
AVR910 mAVRISP(PB_15, PB_14, PB_13, PB_2); //mosi, miso, sclk, nreset.

DigitalOut working(LED1);


int main()
{
    working = 1;
    int success  = -1;
    int response =  0;

    pc.printf("Starting\n");

    //Read the vendor code [0x1E == Atmel].
    response = mAVRISP.readVendorCode();
    printf("vendorcode=%d\n",response);
    if (response == ATMEL_VENDOR_CODE) {
        pc.printf("Microcontroller is an Atmel [0x%02x]\n", response);
    } else if (response == DEVICE_LOCKED) {
        pc.printf("Device is locked\n");
        return -1;
    } else {
        pc.printf("Microcontroller is not an Atmel\n");
        return -1;
    }

    //Read part family and flash size - see datasheet for code meaning.
    response = mAVRISP.readPartFamilyAndFlashSize();

    if (response == 0xFF) {
        pc.printf("Device code erased or target missing\n");
    } else if (response == 0x01) {
        pc.printf("Device locked\n");
        return -1;
    } else {
        pc.printf("Part family and flash size code is: 0x%02x\n", response);
    }

    //Read part number.
    response = mAVRISP.readPartNumber();

    if (response == 0xFF) {
        pc.printf("Device code erased or target missing\n");
    } else if (response == 0x02) {
        pc.printf("Device locked\n");
        return -1;
    } else {
        pc.printf("Part number code is: 0x%02x\n", response);
    }

    //Open binary file to write to AVR.
    //FILE *fp = fopen(PATH_TO_BINARY, "rb");
    printf("@");
    
    int hs, ls;
    while (!pc.readable());
    hs = pc.getc();
    printf("hs=%d\n",hs);
    while (!pc.readable());
    ls = pc.getc();
    printf("ls=%d\n",ls);
    int fileSize = (hs<<8) + ls;
    printf ("file size = %d\n", fileSize);

    printf("#");
    
    char *myFile;
    myFile = new char [fileSize];
    int readChar=0;
    while (readChar < fileSize) {
        if (pc.readable()) {
            myFile[readChar]=pc.getc();
            readChar++;
        }
    }
    for (int i=0; i<fileSize; i++) {
        printf("r %d: 0x%02x\n",i, myFile[i]);
    }
    wait_ms(1000);
    success = mAVRISP.programData(myFile, fileSize, ATMEGA328P_PAGESIZE, ATMEGA328P_NUM_PAGES);

//    if (fp == NULL) {
//        pc.printf("Failed to open binary. Please check the file path\n");
//        return -1;
//    } else {
//        //Program it!
//        pc.printf("Binary file opened successfully\n");
//        success = mAVRISP.program(fp,
//                                  ATMEGA328P_PAGESIZE,
//                                  ATMEGA328P_NUM_PAGES);
//        fclose(fp);
//    }

    if (success < 0) {
        printf("Programming failed.\n");
    } else {
        printf("Programming was successful!\n");
    }

}

/* Python file for on the pc:
import serial
import sys
import os

fileName=sys.argv[1]
fileSize=os.path.getsize(fileName)

serdev='/dev/ttyACM0'
s = serial.Serial(serdev)

while True:
    if s.readable():
        c = s.read()
        if '@' in c:
            print "Start sending file size\n"
            hs = (fileSize>>8) & 0xff
            s.write(chr(hs))
            ls = fileSize & 0xff
            s.write(chr(ls))
        if '#' in c:
            print "Start sending file\n"
            with open(fileName, "rb") as f:
                byte = f.read(1)
                while byte != "":
#                    while not s.writable():
#                        pass
#                    print "send: 0x",format(ord(byte),'02x')
                    s.write(byte)
                    byte = f.read(1)
        else:
            sys.stdout.write(c)
*/

/* avr test file:
#define F_CPU 1000000UL // 1 MHz
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
// Set Port D pins as all outputs
DDRD = 0xff;

// Set all Port D pins as HIGH
while (1) {
PORTD = 0xFF;
_delay_ms(500.);
PORTD = 0x00;
_delay_ms(500.);
}
return 1;
}
*/