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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Program an AVR with an mbed.
00003  * The mbed is connected to an ATMEGA328 via ISP.
00004  * The PC has a serial connection to the mbed.
00005  * The AVR program on the PC is send via the serial connection to the mbed using a python script.
00006  * The mbed then programs the ATMEGA328 via ISP.
00007  */
00008 
00009 #include "AVR910_Serial.h"
00010 #include "mbed.h"
00011 
00012 // serial connection to a pc
00013 Serial pc(USBTX, USBRX);
00014 // ISP programmer
00015 AVR910 mAVRISP(PB_15, PB_14, PB_13, PB_2); //mosi, miso, sclk, nreset.
00016 
00017 int main()
00018 {
00019     int success  = -1;
00020     int response =  0;
00021 
00022     //Read the vendor code [0x1E == Atmel].
00023     response = mAVRISP.readVendorCode();
00024 
00025     if (response == ATMEL_VENDOR_CODE) {
00026         pc.printf("Microcontroller is an Atmel [0x%02x]\n", response);
00027     } else if (response == DEVICE_LOCKED) {
00028         pc.printf("Device is locked\n");
00029         return -1;
00030     } else {
00031         pc.printf("Microcontroller is not an Atmel\n");
00032         return -1;
00033     }
00034 
00035     //Read part family and flash size - see datasheet for code meaning.
00036     response = mAVRISP.readPartFamilyAndFlashSize();
00037 
00038     if (response == 0xFF) {
00039         pc.printf("Device code erased or target missing\n");
00040     } else if (response == 0x01) {
00041         pc.printf("Device locked\n");
00042         return -1;
00043     } else {
00044         pc.printf("Part family and flash size code is: 0x%02x\n", response);
00045     }
00046 
00047     //Read part number.
00048     response = mAVRISP.readPartNumber();
00049 
00050     if (response == 0xFF) {
00051         pc.printf("Device code erased or target missing\n");
00052     } else if (response == 0x02) {
00053         pc.printf("Device locked\n");
00054         return -1;
00055     } else {
00056         pc.printf("Part number code is: 0x%02x\n", response);
00057     }
00058 
00059     // signal the python script it can start sending the file size
00060     printf("@");
00061 
00062     int hs, ls;
00063     while (!pc.readable());
00064     hs = pc.getc();
00065     while (!pc.readable());
00066     ls = pc.getc();
00067     int dataSize = (hs<<8) + ls;
00068 
00069     // signal the python script it can start sending the file
00070     printf("#");
00071 
00072     char *myFile;
00073     myFile = new char [dataSize];
00074     int readChar=0;
00075     while (readChar < dataSize) {
00076         if (pc.readable()) {
00077             myFile[readChar]=pc.getc();
00078             readChar++;
00079         }
00080     }
00081 
00082     // program the atmega
00083     success = mAVRISP.programData(myFile, dataSize, ATMEGA328P_PAGESIZE, ATMEGA328P_NUM_PAGES);
00084 
00085     if (success < 0) {
00086         printf("Programming failed.\n");
00087     } else {
00088         printf("Programming was successful!\n");
00089     }
00090     
00091     // signal the python script it can stop
00092     printf("$");
00093 
00094 }
00095 
00096 /* Python script to be run on the pc:
00097 import serial
00098 import sys
00099 import os
00100 
00101 fileName=sys.argv[1]
00102 fileSize=os.path.getsize(fileName)
00103 
00104 serdev='/dev/ttyACM0'
00105 s = serial.Serial(serdev)
00106 
00107 while True:
00108     if s.readable():
00109         c = s.read()
00110         if '@' in c:
00111             hs = (fileSize>>8) & 0xff
00112             s.write(chr(hs))
00113             ls = fileSize & 0xff
00114             s.write(chr(ls))
00115         elif '#' in c:
00116             with open(fileName, "rb") as f:
00117                 byte = f.read(1)
00118                 while byte != "":
00119                     s.write(byte)
00120                     byte = f.read(1)
00121         elif '$' in c:
00122             break
00123         else:
00124             sys.stdout.write(c)
00125 */
00126 
00127 /* avr test file:
00128 #define F_CPU 1000000UL // 1 MHz
00129 #include <avr/io.h>
00130 #include <util/delay.h>
00131 
00132 int main(void)
00133 {
00134 // Set Port D pins as all outputs
00135     DDRD = 0xff;
00136 
00137 // Set all Port D pins as HIGH
00138     while (1) {
00139         PORTD = 0xFF;
00140         _delay_ms(500.);
00141         PORTD = 0x00;
00142         _delay_ms(500.);
00143     }
00144     return 1;
00145 }
00146 
00147 The avr test file needs to be compiled, linked (make + make hex) to get a .hex file.
00148 This hex needs to be transformed into a .bin file via hex2bin.
00149 Make sure the mbed is up and running with the code in this file.
00150 Then uploaded the .bin via the python script: python scriptName programName.bin
00151 A reset of the mbed board may be needed.
00152 */