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.
Fork of mAVRISP by
main.cpp@0:3066745764a5, 2010-08-31 (annotated)
- Committer:
- aberk
- Date:
- Tue Aug 31 14:09:53 2010 +0000
- Revision:
- 0:3066745764a5
- Child:
- 1:276f6df4be7a
Version 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aberk | 0:3066745764a5 | 1 | /** |
aberk | 0:3066745764a5 | 2 | * Program an AVR with an mbed. |
aberk | 0:3066745764a5 | 3 | */ |
aberk | 0:3066745764a5 | 4 | |
aberk | 0:3066745764a5 | 5 | #include "AVR910.h" |
aberk | 0:3066745764a5 | 6 | |
aberk | 0:3066745764a5 | 7 | LocalFileSystem local("local"); |
aberk | 0:3066745764a5 | 8 | Serial pc(USBTX, USBRX); |
aberk | 0:3066745764a5 | 9 | AVR910 mAVRISP(p5, p6, p7, p8); //mosi, miso, sclk, nreset. |
aberk | 0:3066745764a5 | 10 | |
aberk | 0:3066745764a5 | 11 | int main() { |
aberk | 0:3066745764a5 | 12 | |
aberk | 0:3066745764a5 | 13 | int success = -1; |
aberk | 0:3066745764a5 | 14 | int response = 0; |
aberk | 0:3066745764a5 | 15 | |
aberk | 0:3066745764a5 | 16 | //Read the vendor code [0x1E == Atmel]. |
aberk | 0:3066745764a5 | 17 | response = mAVRISP.readVendorCode(); |
aberk | 0:3066745764a5 | 18 | |
aberk | 0:3066745764a5 | 19 | if (response == ATMEL_VENDOR_CODE) { |
aberk | 0:3066745764a5 | 20 | pc.printf("Microcontroller is an Atmel [0x%02x]\n", response); |
aberk | 0:3066745764a5 | 21 | } else if (response == DEVICE_LOCKED) { |
aberk | 0:3066745764a5 | 22 | pc.printf("Device is locked\n"); |
aberk | 0:3066745764a5 | 23 | return -1; |
aberk | 0:3066745764a5 | 24 | } else { |
aberk | 0:3066745764a5 | 25 | pc.printf("Microcontroller is not an Atmel\n"); |
aberk | 0:3066745764a5 | 26 | return -1; |
aberk | 0:3066745764a5 | 27 | } |
aberk | 0:3066745764a5 | 28 | |
aberk | 0:3066745764a5 | 29 | //Read part family and flash size - see datasheet for code meaning. |
aberk | 0:3066745764a5 | 30 | response = mAVRISP.readPartFamilyAndFlashSize(); |
aberk | 0:3066745764a5 | 31 | |
aberk | 0:3066745764a5 | 32 | if (response == 0xFF) { |
aberk | 0:3066745764a5 | 33 | pc.printf("Device code erased or target missing\n"); |
aberk | 0:3066745764a5 | 34 | } else if (response == 0x01) { |
aberk | 0:3066745764a5 | 35 | pc.printf("Device locked\n"); |
aberk | 0:3066745764a5 | 36 | return -1; |
aberk | 0:3066745764a5 | 37 | } else { |
aberk | 0:3066745764a5 | 38 | pc.printf("Part family and flash size code is: 0x%02x\n", response); |
aberk | 0:3066745764a5 | 39 | } |
aberk | 0:3066745764a5 | 40 | |
aberk | 0:3066745764a5 | 41 | //Read part number. |
aberk | 0:3066745764a5 | 42 | response = mAVRISP.readPartNumber(); |
aberk | 0:3066745764a5 | 43 | |
aberk | 0:3066745764a5 | 44 | if (response == 0xFF) { |
aberk | 0:3066745764a5 | 45 | pc.printf("Device code erased or target missing\n"); |
aberk | 0:3066745764a5 | 46 | return -1; |
aberk | 0:3066745764a5 | 47 | } else if (response == 0x02) { |
aberk | 0:3066745764a5 | 48 | pc.printf("Device locked\n"); |
aberk | 0:3066745764a5 | 49 | return -1; |
aberk | 0:3066745764a5 | 50 | } else { |
aberk | 0:3066745764a5 | 51 | pc.printf("Part number code is: 0x%02x\n", response); |
aberk | 0:3066745764a5 | 52 | } |
aberk | 0:3066745764a5 | 53 | |
aberk | 0:3066745764a5 | 54 | //Open binary file to write to AVR. |
aberk | 0:3066745764a5 | 55 | FILE *fp = fopen(PATH_TO_BINARY, "rb"); |
aberk | 0:3066745764a5 | 56 | |
aberk | 0:3066745764a5 | 57 | if(fp == NULL){ |
aberk | 0:3066745764a5 | 58 | pc.printf("Failed to open binary. Please check the file path\n"); |
aberk | 0:3066745764a5 | 59 | return -1; |
aberk | 0:3066745764a5 | 60 | } |
aberk | 0:3066745764a5 | 61 | else{ |
aberk | 0:3066745764a5 | 62 | //Program it! |
aberk | 0:3066745764a5 | 63 | pc.printf("Binary file opened successfully\n"); |
aberk | 0:3066745764a5 | 64 | success = mAVRISP.program(fp); |
aberk | 0:3066745764a5 | 65 | fclose(fp); |
aberk | 0:3066745764a5 | 66 | } |
aberk | 0:3066745764a5 | 67 | |
aberk | 0:3066745764a5 | 68 | if(success < 0){ |
aberk | 0:3066745764a5 | 69 | printf("Programming failed.\n"); |
aberk | 0:3066745764a5 | 70 | } |
aberk | 0:3066745764a5 | 71 | else{ |
aberk | 0:3066745764a5 | 72 | printf("Programming was successful!\n"); |
aberk | 0:3066745764a5 | 73 | } |
aberk | 0:3066745764a5 | 74 | |
aberk | 0:3066745764a5 | 75 | } |