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@1:276f6df4be7a, 2010-09-01 (annotated)
- Committer:
- aberk
- Date:
- Wed Sep 01 10:22:51 2010 +0000
- Revision:
- 1:276f6df4be7a
- Parent:
- 0:3066745764a5
- Child:
- 3:df6782d01720
Added support for non-paged memory AVRs and changed parameters for the program(...) method to include page size (memory size if non-paged memory) and number of pages (=1 if non-paged memory).
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 | 1:276f6df4be7a | 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 | 1:276f6df4be7a | 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 | } else if (response == 0x02) { |
aberk | 0:3066745764a5 | 47 | pc.printf("Device locked\n"); |
aberk | 0:3066745764a5 | 48 | return -1; |
aberk | 0:3066745764a5 | 49 | } else { |
aberk | 0:3066745764a5 | 50 | pc.printf("Part number code is: 0x%02x\n", response); |
aberk | 0:3066745764a5 | 51 | } |
aberk | 1:276f6df4be7a | 52 | |
aberk | 0:3066745764a5 | 53 | //Open binary file to write to AVR. |
aberk | 0:3066745764a5 | 54 | FILE *fp = fopen(PATH_TO_BINARY, "rb"); |
aberk | 1:276f6df4be7a | 55 | |
aberk | 1:276f6df4be7a | 56 | if (fp == NULL) { |
aberk | 0:3066745764a5 | 57 | pc.printf("Failed to open binary. Please check the file path\n"); |
aberk | 0:3066745764a5 | 58 | return -1; |
aberk | 1:276f6df4be7a | 59 | } else { |
aberk | 0:3066745764a5 | 60 | //Program it! |
aberk | 0:3066745764a5 | 61 | pc.printf("Binary file opened successfully\n"); |
aberk | 1:276f6df4be7a | 62 | success = mAVRISP.program(fp, |
aberk | 1:276f6df4be7a | 63 | ATMEGA328P_PAGESIZE, |
aberk | 1:276f6df4be7a | 64 | ATMEGA328P_NUM_PAGES); |
aberk | 0:3066745764a5 | 65 | fclose(fp); |
aberk | 0:3066745764a5 | 66 | } |
aberk | 1:276f6df4be7a | 67 | |
aberk | 1:276f6df4be7a | 68 | if (success < 0) { |
aberk | 0:3066745764a5 | 69 | printf("Programming failed.\n"); |
aberk | 1:276f6df4be7a | 70 | } else { |
aberk | 0:3066745764a5 | 71 | printf("Programming was successful!\n"); |
aberk | 0:3066745764a5 | 72 | } |
aberk | 1:276f6df4be7a | 73 | |
aberk | 0:3066745764a5 | 74 | } |