AVR910 In-System Programming

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AVR910.h Source File

AVR910.h

00001 //****************************************************************************/
00002 // Description:
00003 //
00004 //  Program AVR chips with the AVR910 ISP (in-system programming) protocol,
00005 //  using an mbed.
00006 //
00007 // AVR910 Application Note:
00008 //
00009 //  http://www.atmel.com/dyn/resources/prod_documents/doc0943.pdf
00010 //****************************************************************************/
00011 
00012 #ifndef MBED_AVR910_H
00013 #define MBED_AVR910_H
00014 
00015 //****************************************************************************/
00016 // Includes
00017 //****************************************************************************/
00018 #include "mbed.h"
00019 
00020 //****************************************************************************/
00021 // Defines
00022 //****************************************************************************/
00023 
00024 #define PATH_TO_BINARY "/local/AVRcode.bin"
00025 
00026 //Commands
00027 #define ATMEL_VENDOR_CODE 0x1E
00028 #define DEVICE_LOCKED     0x00
00029 #define WRITE_HIGH_BYTE   0x48
00030 #define WRITE_LOW_BYTE    0x40
00031 #define READ_HIGH_BYTE    0x28
00032 #define READ_LOW_BYTE     0x20
00033 
00034 #define PAGE_SIZE         ATMEGA328P_PAGESIZE
00035 #define NUM_PAGES         ATMEGA328P_NUM_PAGES
00036 
00037 //ATMega328P
00038 #define ATMEGA328P_PAGESIZE  64 //Size in words.
00039 #define ATMEGA328P_NUM_PAGES 256
00040 
00041 
00042 class AVR910 {
00043 
00044 public:
00045 
00046     /**
00047      * Constructor.
00048      *
00049      * Parameters:
00050      *
00051      *  mosi - Master out slave in pin for SPI communication.
00052      *  miso - Master in slave out pin for SPI communication.
00053      *  sclk - Serial clock pin for SPI communication.
00054      *  nReset - Not reset line pin on the ISP interface.
00055      */
00056     AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset);
00057     
00058     /**
00059      * Program the AVR microcontroller connected to the mbed.
00060      *
00061      * Parameters:
00062      *
00063      *  binary - File pointer to the binary file to be loaded onto the
00064      *           AVR microcontroller.
00065      *
00066      * Returns:
00067      *
00068      *   0 -> AVR microcontroller programmed successfully.
00069      *  -1 -> Problem during programming.
00070      */
00071     int program(FILE* binary);
00072     
00073     /**
00074      * Set the frequency of the SPI communication.
00075      *
00076      *  (Wrapper for SPI::frequency)
00077      *
00078      * Parameters:
00079      *
00080      *  frequency - Frequency of the SPI communication in hertz.
00081      */
00082     void setFrequency(int frequency);
00083 
00084 private:
00085 
00086     /**
00087      * Issue an enable programming command to the AVR microcontroller.
00088      *
00089      * Returns:
00090      *
00091      *   0 to indicate programming was enabled successfully.
00092      *  -1 to indicate programming was not enabled.
00093      */
00094     int enableProgramming(void);
00095     
00096     /**
00097      * Poll the device until it has finished its current operation.
00098      */
00099     void poll(void);
00100     
00101     /**
00102      * Read the vendor code of the device.
00103      *
00104      * Returns:
00105      *
00106      *  The vendor code - should be 0x1E for Atmel.
00107      *  0x00 -> Device is locked.
00108      */
00109     int readVendorCode(void);
00110     
00111     /**
00112      * Read the part family and flash size of the device.
00113      *
00114      * Returns:
00115      *
00116      *  Code indicating the family of AVR microcontrollers the device comes
00117      *  from and how much flash memory it contains.
00118      *  0xFF -> Device code erased or target missing.
00119      *  0x01 -> Device is locked.
00120      */
00121     int readPartFamilyAndFlashSize(void);
00122     
00123     /**
00124      * Read the part number.
00125      *
00126      * Returns:
00127      *
00128      *  Code identifying the part number.
00129      *  0xFF -> Device code erased or target missing.
00130      *  0x02 -> Device is locked.
00131      */
00132     int readPartNumber(void);
00133     
00134     /**
00135      * Issue a chip erase command to the AVR microcontroller.
00136      */
00137     void chipErase(void);
00138     
00139     /**
00140      * Load a byte into the memory page buffer.
00141      *
00142      * Parameters:
00143      *
00144      *  highLow - Indicate whether the byte being loaded is a high or low byte.
00145      *  data    - The data byte to load.
00146      */
00147     void loadMemoryPage(int highLow, char address, char data);
00148     
00149     /**
00150      * Write the memory page buffer to flash memory.
00151      *
00152      * Parameters:
00153      *
00154      *  pageNumber - The page number to write to in flash memory.
00155      */
00156     void writeFlashMemoryPage(char pageNumber);
00157     
00158     /**
00159      * Read a byte from program memory.
00160      *
00161      * Parameters:
00162      *
00163      *  highLow - Indicate whether the byte being read is a low or high byte.
00164      *  pageNumber - The page number to read from.
00165      *  pageOffset - Address of byte in the page.
00166      *
00167      * Returns:
00168      *
00169      *  The byte at the specified memory location.
00170      */
00171     char readProgramMemory(int highLow, char pageNumber, char pageOffset);
00172     
00173     /**
00174      * Check the binary has been written correctly.
00175      *
00176      * Paramters:
00177      *
00178      *  numPages - The number of pages written to the AVR microcontroller.
00179      *  binary - File pointer to the binary used.
00180      * Returns:
00181      *
00182      *   0 -> No inconsistencies between binary file and AVR flash memory.
00183      *  -1 -> Binary file was not written correctly.
00184      */
00185     int checkMemory(int numPages, FILE* binary);
00186 
00187     DigitalOut* nReset_;
00188     SPI*        spi_;
00189 
00190 };
00191 
00192 #endif /* AVR910_H */