Chris Styles
/
m3pi_AVRprogrammer
Revision 0:20fc0ecfb510, committed 2010-11-01
- Comitter:
- chris
- Date:
- Mon Nov 01 17:11:45 2010 +0000
- Child:
- 1:ce6a12e62219
- Commit message:
- Initial
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AVR910.cpp Mon Nov 01 17:11:45 2010 +0000 @@ -0,0 +1,302 @@ +//****************************************************************************/ +// Description: +// +// Program AVR chips with the AVR910 ISP (in-system programming) protocol, +// using an mbed. +// +// AVR Application Note: +// +// http://www.atmel.com/dyn/resources/prod_documents/doc0943.pdf +//****************************************************************************/ + +//****************************************************************************/ +// Includes +//****************************************************************************/ +#include "AVR910.h" + +Serial debug(USBTX, USBRX); + +AVR910::AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset) { + + spi_ = new SPI(mosi, miso, sclk); + spi_->frequency(32000); + spi_->format(8, 1); + nReset_ = new DigitalOut(nReset); + +} + +int AVR910::program(FILE* binary) { + + int response = 0; + + debug.printf("mbed AVR910 Programmer starting\n"); + + //Enter serial programming mode by pulling reset line low. + *nReset_ = 0; + + //Wait 20ms before issuing first command. + wait_ms(20); + + //Issue a programming enable command. + response = enableProgramming(); + + if (response < 0) { + debug.printf("Enable programming command not successful\n"); + } else { + debug.printf("Enable programming command successful\n"); + } + + //Read vendor code, part family and part number. + response = readVendorCode(); + + if (response == ATMEL_VENDOR_CODE) { + debug.printf("Microcontroller is an Atmel [0x%02x]\n", response); + } else if (response == DEVICE_LOCKED) { + debug.printf("Device is locked\n"); + } else { + debug.printf("Microcontroller is not an Atmel\n"); + } + + response = readPartFamilyAndFlashSize(); + + if (response == 0xFF) { + debug.printf("Device code erased or target missing\n"); + } else if (response == 0x01) { + debug.printf("Device locked\n"); + } else { + debug.printf("Part family and flash size code is: 0x%02x\n", response); + } + + response = readPartNumber(); + + if (response == 0xFF) { + debug.printf("Device code erased or target missing\n"); + } else if (response == 0x02) { + debug.printf("Device locked\n"); + } else { + debug.printf("Part number code is: 0x%02x\n", response); + } + + //Clear memory contents. + chipErase(); + + char pageOffset = 0; + int pageNumber = 0; + int c = 0; + int highLow = 0; + + while ((c = getc(binary)) != EOF) { + + //Page is fully loaded, time to write it to flash. + if (pageOffset == (PAGE_SIZE)) { + writeFlashMemoryPage(pageNumber); + debug.printf("Page %i written\n", pageNumber); + pageNumber++; + pageOffset = 0; + } + + //Write low byte. + if (highLow == 0) { + loadMemoryPage(WRITE_LOW_BYTE, pageOffset, c); + highLow = 1; + } + //Write high byte. + else { + loadMemoryPage(WRITE_HIGH_BYTE, pageOffset, c); + highLow = 0; + pageOffset++; + } + + } + + //We might have partially filled up a page. + writeFlashMemoryPage(pageNumber); + + int success = -1; + success = checkMemory(pageNumber, binary); + + //Leave serial programming mode by pulling reset line high. + *nReset_ = 1; + + return success; + +} + +void AVR910::setFrequency(int frequency){ + + spi_->frequency(frequency); + +} + +int AVR910::enableProgramming(void) { + + int response = 0; + int error = 0; + + //Programming Enable Command: 0xAC, 0x53, 0x00, 0x00 + //Byte two echo'd back in byte three. + spi_->write(0xAC); + + spi_->write(0x53); + + response = spi_->write(0x00); + + if (response == 0x53) { + error = 0; + } else { + error = -1; + } + + spi_->write(0x00); + + return error; + +} + +void AVR910::poll(void) { + + int response = 0; + + do { + spi_->write(0xF0); + spi_->write(0x00); + spi_->write(0x00); + response = spi_->write(0x00); + } while ((response & 0x01) != 0); + +} + +int AVR910::readVendorCode(void) { + + int response = 0; + + //Issue read signature byte command. + //Address 0x00 is vendor code. + spi_->write(0x30); + spi_->write(0x00); + spi_->write(0x00); + response = spi_->write(0x00); + + return response; + +} + +int AVR910::readPartFamilyAndFlashSize(void) { + + int response = 0; + + //Issue read signature byte command. + //Address 0x01 is part family and flash size code. + spi_->write(0x30); + spi_->write(0x00); + spi_->write(0x01); + response = spi_->write(0x00); + + return response; + +} + +int AVR910::readPartNumber(void) { + + int response = 0; + + //Issue read signature byte command. + //Address 0x02 is part number code. + spi_->write(0x30); + spi_->write(0x00); + spi_->write(0x02); + response = spi_->write(0x00); + + return response; + +} + +void AVR910::chipErase(void) { + + //Issue chip erase command. + spi_->write(0xAC); + spi_->write(0x80); + spi_->write(0x00); + spi_->write(0x00); + + poll(); + + //Temporarily release reset line. + *nReset_ = 1; + *nReset_ = 0; + +} + +void AVR910::loadMemoryPage(int highLow, char address, char data) { + + spi_->write(highLow); + spi_->write(0x00); + spi_->write(address & 0x3F); + spi_->write(data); + + poll(); + +} + +void AVR910::writeFlashMemoryPage(char pageNumber) { + + spi_->write(0x4C); + spi_->write((pageNumber >> 2) & 0x3F); + spi_->write((pageNumber & 0x03) << 6); + spi_->write(0x00); + + poll(); + +} + +char AVR910::readProgramMemory(int highLow, char pageNumber, char pageOffset) { + + int response = 0; + + spi_->write(highLow); + spi_->write((pageNumber >> 2) & 0x3F); + spi_->write(((pageNumber & 0x03) << 6) | (pageOffset & 0x3F)); + response = spi_->write(0x00); + + poll(); + + return response; + +} + +int AVR910::checkMemory(int numPages, FILE* binary){ + + int success = 0; + int response = 0; + char c = 0; + + //Go back to the beginning of the binary file. + fseek(binary, 0, SEEK_SET); + + for(int i = 0; i < numPages; i++){ + for (int j = 0; j < PAGE_SIZE; j++) { + c = getc(binary); + //Read program memory low byte. + response = readProgramMemory(READ_LOW_BYTE, i, j); + //debug.printf("Low byte: 0x%02x\n", response); + if( c != response ){ + debug.printf("page %i low byte %i: 0x%02x\n", i, j, response); + debug.printf("correct byte is 0x%02x\n", c); + success = -1; + } + + c = getc(binary); + //Read program memory high byte. + response = readProgramMemory(READ_HIGH_BYTE, i, j); + //debug.printf("High byte: 0x%02x\n", response); + if( c != response ){ + debug.printf("page %i high byte %i: 0x%02x\n", i, j, response); + debug.printf("correct byte is 0x%02x\n", c); + success = -1; + } + } + } + + return success; + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AVR910.h Mon Nov 01 17:11:45 2010 +0000 @@ -0,0 +1,192 @@ +//****************************************************************************/ +// Description: +// +// Program AVR chips with the AVR910 ISP (in-system programming) protocol, +// using an mbed. +// +// AVR910 Application Note: +// +// http://www.atmel.com/dyn/resources/prod_documents/doc0943.pdf +//****************************************************************************/ + +#ifndef MBED_AVR910_H +#define MBED_AVR910_H + +//****************************************************************************/ +// Includes +//****************************************************************************/ +#include "mbed.h" + +//****************************************************************************/ +// Defines +//****************************************************************************/ + +#define PATH_TO_BINARY "/local/AVRcode.bin" + +//Commands +#define ATMEL_VENDOR_CODE 0x1E +#define DEVICE_LOCKED 0x00 +#define WRITE_HIGH_BYTE 0x48 +#define WRITE_LOW_BYTE 0x40 +#define READ_HIGH_BYTE 0x28 +#define READ_LOW_BYTE 0x20 + +#define PAGE_SIZE ATMEGA328P_PAGESIZE +#define NUM_PAGES ATMEGA328P_NUM_PAGES + +//ATMega328P +#define ATMEGA328P_PAGESIZE 64 //Size in words. +#define ATMEGA328P_NUM_PAGES 256 + + +class AVR910 { + +public: + + /** + * Constructor. + * + * Parameters: + * + * mosi - Master out slave in pin for SPI communication. + * miso - Master in slave out pin for SPI communication. + * sclk - Serial clock pin for SPI communication. + * nReset - Not reset line pin on the ISP interface. + */ + AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset); + + /** + * Program the AVR microcontroller connected to the mbed. + * + * Parameters: + * + * binary - File pointer to the binary file to be loaded onto the + * AVR microcontroller. + * + * Returns: + * + * 0 -> AVR microcontroller programmed successfully. + * -1 -> Problem during programming. + */ + int program(FILE* binary); + + /** + * Set the frequency of the SPI communication. + * + * (Wrapper for SPI::frequency) + * + * Parameters: + * + * frequency - Frequency of the SPI communication in hertz. + */ + void setFrequency(int frequency); + +private: + + /** + * Issue an enable programming command to the AVR microcontroller. + * + * Returns: + * + * 0 to indicate programming was enabled successfully. + * -1 to indicate programming was not enabled. + */ + int enableProgramming(void); + + /** + * Poll the device until it has finished its current operation. + */ + void poll(void); + + /** + * Read the vendor code of the device. + * + * Returns: + * + * The vendor code - should be 0x1E for Atmel. + * 0x00 -> Device is locked. + */ + int readVendorCode(void); + + /** + * Read the part family and flash size of the device. + * + * Returns: + * + * Code indicating the family of AVR microcontrollers the device comes + * from and how much flash memory it contains. + * 0xFF -> Device code erased or target missing. + * 0x01 -> Device is locked. + */ + int readPartFamilyAndFlashSize(void); + + /** + * Read the part number. + * + * Returns: + * + * Code identifying the part number. + * 0xFF -> Device code erased or target missing. + * 0x02 -> Device is locked. + */ + int readPartNumber(void); + + /** + * Issue a chip erase command to the AVR microcontroller. + */ + void chipErase(void); + + /** + * Load a byte into the memory page buffer. + * + * Parameters: + * + * highLow - Indicate whether the byte being loaded is a high or low byte. + * data - The data byte to load. + */ + void loadMemoryPage(int highLow, char address, char data); + + /** + * Write the memory page buffer to flash memory. + * + * Parameters: + * + * pageNumber - The page number to write to in flash memory. + */ + void writeFlashMemoryPage(char pageNumber); + + /** + * Read a byte from program memory. + * + * Parameters: + * + * highLow - Indicate whether the byte being read is a low or high byte. + * pageNumber - The page number to read from. + * pageOffset - Address of byte in the page. + * + * Returns: + * + * The byte at the specified memory location. + */ + char readProgramMemory(int highLow, char pageNumber, char pageOffset); + + /** + * Check the binary has been written correctly. + * + * Paramters: + * + * numPages - The number of pages written to the AVR microcontroller. + * binary - File pointer to the binary used. + * Returns: + * + * 0 -> No inconsistencies between binary file and AVR flash memory. + * -1 -> Binary file was not written correctly. + */ + int checkMemory(int numPages, FILE* binary); + + DigitalOut* nReset_; + SPI* spi_; + +}; + +#endif /* AVR910_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Nov 01 17:11:45 2010 +0000 @@ -0,0 +1,51 @@ +/** + * Program an AVR with an mbed. + */ + +// ATMega328 Datasheet: +// +// http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdf + +#include "AVR910.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); + +LocalFileSystem local("local"); +Serial pc(USBTX, USBRX); + +AVR910 mbedISP(p5, p6, p7, p8); //mosi, miso, sclk, nreset. + +int main() { + + + int success = -1; + + printf("\nAVR Programmer\nLooking for /local/program.avr\n"); + + FILE *fp = fopen("/local/program.avr", "rb"); + + if (fp == NULL) { + pc.printf("Failed to open binary\n"); + } else { + pc.printf("Binary file opened successfully\n"); + led1 = 1; // in progress + success = mbedISP.program(fp); + fclose(fp); + remove("/local/program.avr"); // remove the file when we're done + led2 = 1; // finished + } + + if (success < 0) { + printf("Programming failed.\n"); + } else { + printf("Programming was successful!\n"); + } + + fclose(fp); + + + while (1) { + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Nov 01 17:11:45 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/029aa53d7323