
AVRisp tested on ATMega88A inspired by various implementations around: - AVR_SPI_Programmer_v2 ( BO Qiang ) - mAVRISP (Aaron Berk) - TestAVRISP(Chester Hamilton)
AVRisp implementation for mBED
main.cpp@1:4b883042df7d, 2015-04-28 (annotated)
- Committer:
- Ellips
- Date:
- Tue Apr 28 11:01:19 2015 +0000
- Revision:
- 1:4b883042df7d
- Parent:
- 0:29abe3c0b902
AVRisp , v0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ellips | 0:29abe3c0b902 | 1 | #include "mbed.h" |
Ellips | 0:29abe3c0b902 | 2 | |
Ellips | 0:29abe3c0b902 | 3 | #define ATMEGA88A 1 |
Ellips | 0:29abe3c0b902 | 4 | #include "AVRISP.h" |
Ellips | 0:29abe3c0b902 | 5 | |
Ellips | 0:29abe3c0b902 | 6 | #define FUSE_LOW 0xD0 |
Ellips | 0:29abe3c0b902 | 7 | #define FUSE_HIGH 0xD9 |
Ellips | 0:29abe3c0b902 | 8 | #define FUSE_EXTENDED 0xFF |
Ellips | 0:29abe3c0b902 | 9 | #define LOCK_BYTE 0xFF |
Ellips | 0:29abe3c0b902 | 10 | |
Ellips | 0:29abe3c0b902 | 11 | //most chips use by default the internal 8Mhz clock with DIV8 fuse so 1Mhz seems a good starting point |
Ellips | 0:29abe3c0b902 | 12 | #define TARGET_CLK 1000000L |
Ellips | 0:29abe3c0b902 | 13 | |
Ellips | 0:29abe3c0b902 | 14 | Serial pc(USBTX, USBRX); |
Ellips | 0:29abe3c0b902 | 15 | DigitalOut led1(LED1); |
Ellips | 0:29abe3c0b902 | 16 | |
Ellips | 0:29abe3c0b902 | 17 | void ProgressLed() |
Ellips | 0:29abe3c0b902 | 18 | { |
Ellips | 0:29abe3c0b902 | 19 | led1 = !led1; |
Ellips | 0:29abe3c0b902 | 20 | pc.printf("."); |
Ellips | 0:29abe3c0b902 | 21 | } |
Ellips | 0:29abe3c0b902 | 22 | |
Ellips | 0:29abe3c0b902 | 23 | #define ERROR -1 |
Ellips | 0:29abe3c0b902 | 24 | |
Ellips | 0:29abe3c0b902 | 25 | int main() |
Ellips | 0:29abe3c0b902 | 26 | { |
Ellips | 0:29abe3c0b902 | 27 | int result = ERROR; |
Ellips | 0:29abe3c0b902 | 28 | uint8_t signatureByte1 = 0xff,signatureByte2 = 0xff,signatureByte3 = 0xff; |
Ellips | 0:29abe3c0b902 | 29 | bool ProgMode = false; |
Ellips | 0:29abe3c0b902 | 30 | AVRISP *avrisp = NULL; |
Ellips | 0:29abe3c0b902 | 31 | LocalFileSystem local("local"); |
Ellips | 0:29abe3c0b902 | 32 | FILE * hexFile = fopen("/local/avr.hex","r"); |
Ellips | 0:29abe3c0b902 | 33 | |
Ellips | 0:29abe3c0b902 | 34 | if(!hexFile){ |
Ellips | 0:29abe3c0b902 | 35 | pc.printf("Unable to load avr.hex file\n"); |
Ellips | 0:29abe3c0b902 | 36 | goto exit; |
Ellips | 0:29abe3c0b902 | 37 | } |
Ellips | 0:29abe3c0b902 | 38 | |
Ellips | 0:29abe3c0b902 | 39 | avrisp = new AVRISP(p5,p6,p7,p8, TARGET_CLK, PAGE_SIZE, &ProgressLed); |
Ellips | 0:29abe3c0b902 | 40 | //pc.printf("Press any button to program the AVR.\n"); |
Ellips | 0:29abe3c0b902 | 41 | //char c = pc.getc(); |
Ellips | 0:29abe3c0b902 | 42 | |
Ellips | 0:29abe3c0b902 | 43 | pc.printf("Enter Programming Mode: "); |
Ellips | 0:29abe3c0b902 | 44 | ProgMode = avrisp->EnableProgrammingMode(); |
Ellips | 0:29abe3c0b902 | 45 | if(ProgMode) { |
Ellips | 0:29abe3c0b902 | 46 | pc.printf(" Successful.\n"); |
Ellips | 0:29abe3c0b902 | 47 | } else { |
Ellips | 0:29abe3c0b902 | 48 | pc.printf(" Failed.\n"); |
Ellips | 0:29abe3c0b902 | 49 | goto exit; |
Ellips | 0:29abe3c0b902 | 50 | } |
Ellips | 0:29abe3c0b902 | 51 | |
Ellips | 0:29abe3c0b902 | 52 | signatureByte1 = avrisp->ReadChipSignatureByte( AVRISP::eSB1 ); |
Ellips | 0:29abe3c0b902 | 53 | signatureByte2 = avrisp->ReadChipSignatureByte( AVRISP::eSB2 ); |
Ellips | 0:29abe3c0b902 | 54 | signatureByte3 = avrisp->ReadChipSignatureByte( AVRISP::eSB3 ); |
Ellips | 0:29abe3c0b902 | 55 | if ( signatureByte1 != SIGNATURE_BYTE_1 || signatureByte2 != SIGNATURE_BYTE_2 || signatureByte3 != SIGNATURE_BYTE_3 ) { |
Ellips | 0:29abe3c0b902 | 56 | pc.printf("Chip signature mismatch 0x%02x 0x%02x 0x%02x\n",signatureByte1,signatureByte2,signatureByte3); |
Ellips | 0:29abe3c0b902 | 57 | goto exit; |
Ellips | 0:29abe3c0b902 | 58 | } |
Ellips | 0:29abe3c0b902 | 59 | |
Ellips | 0:29abe3c0b902 | 60 | pc.printf("Erasing Chip: "); |
Ellips | 0:29abe3c0b902 | 61 | avrisp->ChipErase(); |
Ellips | 1:4b883042df7d | 62 | pc.printf(" Done\n"); |
Ellips | 0:29abe3c0b902 | 63 | |
Ellips | 0:29abe3c0b902 | 64 | pc.printf("Programing Flash:"); |
Ellips | 0:29abe3c0b902 | 65 | if(avrisp->ProgramFlash(hexFile)) { |
Ellips | 0:29abe3c0b902 | 66 | pc.printf(" Complete.\n"); |
Ellips | 0:29abe3c0b902 | 67 | //verify programming |
Ellips | 0:29abe3c0b902 | 68 | pc.printf("Verifying Flash:"); |
Ellips | 0:29abe3c0b902 | 69 | rewind(hexFile); |
Ellips | 0:29abe3c0b902 | 70 | if( avrisp->VerifyFlash(hexFile) ) { |
Ellips | 0:29abe3c0b902 | 71 | pc.printf(" Complete.\n"); |
Ellips | 0:29abe3c0b902 | 72 | } else { |
Ellips | 0:29abe3c0b902 | 73 | pc.printf(" Failed.\n"); |
Ellips | 0:29abe3c0b902 | 74 | goto exit; |
Ellips | 0:29abe3c0b902 | 75 | } |
Ellips | 0:29abe3c0b902 | 76 | } else { |
Ellips | 0:29abe3c0b902 | 77 | pc.printf("Failed.\n"); |
Ellips | 0:29abe3c0b902 | 78 | } |
Ellips | 0:29abe3c0b902 | 79 | |
Ellips | 1:4b883042df7d | 80 | //fuses |
Ellips | 1:4b883042df7d | 81 | /*pc.printf("Write Fuse Low Byte.\n"); |
Ellips | 1:4b883042df7d | 82 | avrisp->WriteFuse(AVRISP::eFTLo, FUSE_LOW); |
Ellips | 1:4b883042df7d | 83 | pc.printf("Fuse Low Byte Readback = %x.\n",avrisp->ReadFuse(AVRISP::eFTLo)); |
Ellips | 1:4b883042df7d | 84 | |
Ellips | 1:4b883042df7d | 85 | pc.printf("Write Fuse High Byte.\n"); |
Ellips | 1:4b883042df7d | 86 | avrisp->WriteFuse(AVRISP::eFTHi, FUSE_HIGH); |
Ellips | 1:4b883042df7d | 87 | pc.printf("Fuse High Byte Readback = %x.\n",avrisp->ReadFuse(AVRISP::eFTHi)); |
Ellips | 1:4b883042df7d | 88 | |
Ellips | 1:4b883042df7d | 89 | pc.printf("Write Fuse Extended Byte.\n"); |
Ellips | 1:4b883042df7d | 90 | avrisp->WriteFuse(AVRISP::eFTEx, FUSE_EXTENDED); |
Ellips | 1:4b883042df7d | 91 | pc.printf("Fuse Extended Byte Readback = %x.\n",avrisp->ReadFuse(AVRISP::eFTEx)); |
Ellips | 1:4b883042df7d | 92 | |
Ellips | 1:4b883042df7d | 93 | pc.printf("Write Lock Byte.\n"); |
Ellips | 1:4b883042df7d | 94 | avrisp->WriteFuse( AVRISP::eFTLock, LOCK_BYTE); |
Ellips | 1:4b883042df7d | 95 | pc.printf("Lock Byte Readback = %x.\n",avrisp->ReadFuse(AVRISP::eFTLock));*/ |
Ellips | 1:4b883042df7d | 96 | |
Ellips | 0:29abe3c0b902 | 97 | avrisp->LeaveProgrammingMode(); |
Ellips | 0:29abe3c0b902 | 98 | |
Ellips | 0:29abe3c0b902 | 99 | while(true) |
Ellips | 1:4b883042df7d | 100 | __WFI();//do something nice |
Ellips | 0:29abe3c0b902 | 101 | |
Ellips | 0:29abe3c0b902 | 102 | exit: |
Ellips | 0:29abe3c0b902 | 103 | if(ProgMode) |
Ellips | 0:29abe3c0b902 | 104 | avrisp->LeaveProgrammingMode(); |
Ellips | 0:29abe3c0b902 | 105 | |
Ellips | 1:4b883042df7d | 106 | if(avrisp) |
Ellips | 0:29abe3c0b902 | 107 | delete avrisp; |
Ellips | 0:29abe3c0b902 | 108 | |
Ellips | 0:29abe3c0b902 | 109 | if (hexFile) |
Ellips | 1:4b883042df7d | 110 | fclose(hexFile); |
Ellips | 0:29abe3c0b902 | 111 | |
Ellips | 0:29abe3c0b902 | 112 | return result; |
Ellips | 0:29abe3c0b902 | 113 | } |