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

Revision:
4:ceee1eb7062e
Parent:
3:df6782d01720
diff -r df6782d01720 -r ceee1eb7062e main.cpp
--- a/main.cpp	Sat Jan 31 21:54:05 2015 +0000
+++ b/main.cpp	Sat Jan 31 22:44:09 2015 +0000
@@ -1,29 +1,27 @@
 /**
  * Program an AVR with an mbed.
+ * The mbed is connected to an ATMEGA328 via ISP.
+ * The PC has a serial connection to the mbed.
+ * The AVR program on the PC is send via the serial connection to the mbed using a python script.
+ * The mbed then programs the ATMEGA328 via ISP.
  */
 
-#include "AVR910.h"
+#include "AVR910_Serial.h"
+#include "mbed.h"
 
-//LocalFileSystem local("local");
+// serial connection to a pc
 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.
+// ISP programmer
 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) {
@@ -58,58 +56,44 @@
         pc.printf("Part number code is: 0x%02x\n", response);
     }
 
-    //Open binary file to write to AVR.
-    //FILE *fp = fopen(PATH_TO_BINARY, "rb");
+    // signal the python script it can start sending the file size
     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);
+    int dataSize = (hs<<8) + ls;
 
+    // signal the python script it can start sending the file
     printf("#");
-    
+
     char *myFile;
-    myFile = new char [fileSize];
+    myFile = new char [dataSize];
     int readChar=0;
-    while (readChar < fileSize) {
+    while (readChar < dataSize) {
         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);
-//    }
+    // program the atmega
+    success = mAVRISP.programData(myFile, dataSize, ATMEGA328P_PAGESIZE, ATMEGA328P_NUM_PAGES);
 
     if (success < 0) {
         printf("Programming failed.\n");
     } else {
         printf("Programming was successful!\n");
     }
+    
+    // signal the python script it can stop
+    printf("$");
 
 }
 
-/* Python file for on the pc:
+/* Python script to be run on the pc:
 import serial
 import sys
 import os
@@ -124,21 +108,18 @@
     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"
+        elif '#' in c:
             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)
+        elif '$' in c:
+            break
         else:
             sys.stdout.write(c)
 */
@@ -151,15 +132,21 @@
 int main(void)
 {
 // Set Port D pins as all outputs
-DDRD = 0xff;
+    DDRD = 0xff;
 
 // Set all Port D pins as HIGH
-while (1) {
-PORTD = 0xFF;
-_delay_ms(500.);
-PORTD = 0x00;
-_delay_ms(500.);
+    while (1) {
+        PORTD = 0xFF;
+        _delay_ms(500.);
+        PORTD = 0x00;
+        _delay_ms(500.);
+    }
+    return 1;
 }
-return 1;
-}
+
+The avr test file needs to be compiled, linked (make + make hex) to get a .hex file.
+This hex needs to be transformed into a .bin file via hex2bin.
+Make sure the mbed is up and running with the code in this file.
+Then uploaded the .bin via the python script: python scriptName programName.bin
+A reset of the mbed board may be needed.
 */
\ No newline at end of file