Dependencies:   mbed

Committer:
chris
Date:
Mon Nov 01 22:52:40 2010 +0000
Revision:
1:ce6a12e62219
Parent:
0:20fc0ecfb510
Added a #define to determine if the bin file is deleted

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:20fc0ecfb510 1 //****************************************************************************/
chris 0:20fc0ecfb510 2 // Description:
chris 0:20fc0ecfb510 3 //
chris 0:20fc0ecfb510 4 // Program AVR chips with the AVR910 ISP (in-system programming) protocol,
chris 0:20fc0ecfb510 5 // using an mbed.
chris 0:20fc0ecfb510 6 //
chris 0:20fc0ecfb510 7 // AVR910 Application Note:
chris 0:20fc0ecfb510 8 //
chris 0:20fc0ecfb510 9 // http://www.atmel.com/dyn/resources/prod_documents/doc0943.pdf
chris 0:20fc0ecfb510 10 //****************************************************************************/
chris 0:20fc0ecfb510 11
chris 0:20fc0ecfb510 12 #ifndef MBED_AVR910_H
chris 0:20fc0ecfb510 13 #define MBED_AVR910_H
chris 0:20fc0ecfb510 14
chris 0:20fc0ecfb510 15 //****************************************************************************/
chris 0:20fc0ecfb510 16 // Includes
chris 0:20fc0ecfb510 17 //****************************************************************************/
chris 0:20fc0ecfb510 18 #include "mbed.h"
chris 0:20fc0ecfb510 19
chris 0:20fc0ecfb510 20 //****************************************************************************/
chris 0:20fc0ecfb510 21 // Defines
chris 0:20fc0ecfb510 22 //****************************************************************************/
chris 0:20fc0ecfb510 23
chris 0:20fc0ecfb510 24 #define PATH_TO_BINARY "/local/AVRcode.bin"
chris 0:20fc0ecfb510 25
chris 0:20fc0ecfb510 26 //Commands
chris 0:20fc0ecfb510 27 #define ATMEL_VENDOR_CODE 0x1E
chris 0:20fc0ecfb510 28 #define DEVICE_LOCKED 0x00
chris 0:20fc0ecfb510 29 #define WRITE_HIGH_BYTE 0x48
chris 0:20fc0ecfb510 30 #define WRITE_LOW_BYTE 0x40
chris 0:20fc0ecfb510 31 #define READ_HIGH_BYTE 0x28
chris 0:20fc0ecfb510 32 #define READ_LOW_BYTE 0x20
chris 0:20fc0ecfb510 33
chris 0:20fc0ecfb510 34 #define PAGE_SIZE ATMEGA328P_PAGESIZE
chris 0:20fc0ecfb510 35 #define NUM_PAGES ATMEGA328P_NUM_PAGES
chris 0:20fc0ecfb510 36
chris 0:20fc0ecfb510 37 //ATMega328P
chris 0:20fc0ecfb510 38 #define ATMEGA328P_PAGESIZE 64 //Size in words.
chris 0:20fc0ecfb510 39 #define ATMEGA328P_NUM_PAGES 256
chris 0:20fc0ecfb510 40
chris 0:20fc0ecfb510 41
chris 0:20fc0ecfb510 42 class AVR910 {
chris 0:20fc0ecfb510 43
chris 0:20fc0ecfb510 44 public:
chris 0:20fc0ecfb510 45
chris 0:20fc0ecfb510 46 /**
chris 0:20fc0ecfb510 47 * Constructor.
chris 0:20fc0ecfb510 48 *
chris 0:20fc0ecfb510 49 * Parameters:
chris 0:20fc0ecfb510 50 *
chris 0:20fc0ecfb510 51 * mosi - Master out slave in pin for SPI communication.
chris 0:20fc0ecfb510 52 * miso - Master in slave out pin for SPI communication.
chris 0:20fc0ecfb510 53 * sclk - Serial clock pin for SPI communication.
chris 0:20fc0ecfb510 54 * nReset - Not reset line pin on the ISP interface.
chris 0:20fc0ecfb510 55 */
chris 0:20fc0ecfb510 56 AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset);
chris 0:20fc0ecfb510 57
chris 0:20fc0ecfb510 58 /**
chris 0:20fc0ecfb510 59 * Program the AVR microcontroller connected to the mbed.
chris 0:20fc0ecfb510 60 *
chris 0:20fc0ecfb510 61 * Parameters:
chris 0:20fc0ecfb510 62 *
chris 0:20fc0ecfb510 63 * binary - File pointer to the binary file to be loaded onto the
chris 0:20fc0ecfb510 64 * AVR microcontroller.
chris 0:20fc0ecfb510 65 *
chris 0:20fc0ecfb510 66 * Returns:
chris 0:20fc0ecfb510 67 *
chris 0:20fc0ecfb510 68 * 0 -> AVR microcontroller programmed successfully.
chris 0:20fc0ecfb510 69 * -1 -> Problem during programming.
chris 0:20fc0ecfb510 70 */
chris 0:20fc0ecfb510 71 int program(FILE* binary);
chris 0:20fc0ecfb510 72
chris 0:20fc0ecfb510 73 /**
chris 0:20fc0ecfb510 74 * Set the frequency of the SPI communication.
chris 0:20fc0ecfb510 75 *
chris 0:20fc0ecfb510 76 * (Wrapper for SPI::frequency)
chris 0:20fc0ecfb510 77 *
chris 0:20fc0ecfb510 78 * Parameters:
chris 0:20fc0ecfb510 79 *
chris 0:20fc0ecfb510 80 * frequency - Frequency of the SPI communication in hertz.
chris 0:20fc0ecfb510 81 */
chris 0:20fc0ecfb510 82 void setFrequency(int frequency);
chris 0:20fc0ecfb510 83
chris 0:20fc0ecfb510 84 private:
chris 0:20fc0ecfb510 85
chris 0:20fc0ecfb510 86 /**
chris 0:20fc0ecfb510 87 * Issue an enable programming command to the AVR microcontroller.
chris 0:20fc0ecfb510 88 *
chris 0:20fc0ecfb510 89 * Returns:
chris 0:20fc0ecfb510 90 *
chris 0:20fc0ecfb510 91 * 0 to indicate programming was enabled successfully.
chris 0:20fc0ecfb510 92 * -1 to indicate programming was not enabled.
chris 0:20fc0ecfb510 93 */
chris 0:20fc0ecfb510 94 int enableProgramming(void);
chris 0:20fc0ecfb510 95
chris 0:20fc0ecfb510 96 /**
chris 0:20fc0ecfb510 97 * Poll the device until it has finished its current operation.
chris 0:20fc0ecfb510 98 */
chris 0:20fc0ecfb510 99 void poll(void);
chris 0:20fc0ecfb510 100
chris 0:20fc0ecfb510 101 /**
chris 0:20fc0ecfb510 102 * Read the vendor code of the device.
chris 0:20fc0ecfb510 103 *
chris 0:20fc0ecfb510 104 * Returns:
chris 0:20fc0ecfb510 105 *
chris 0:20fc0ecfb510 106 * The vendor code - should be 0x1E for Atmel.
chris 0:20fc0ecfb510 107 * 0x00 -> Device is locked.
chris 0:20fc0ecfb510 108 */
chris 0:20fc0ecfb510 109 int readVendorCode(void);
chris 0:20fc0ecfb510 110
chris 0:20fc0ecfb510 111 /**
chris 0:20fc0ecfb510 112 * Read the part family and flash size of the device.
chris 0:20fc0ecfb510 113 *
chris 0:20fc0ecfb510 114 * Returns:
chris 0:20fc0ecfb510 115 *
chris 0:20fc0ecfb510 116 * Code indicating the family of AVR microcontrollers the device comes
chris 0:20fc0ecfb510 117 * from and how much flash memory it contains.
chris 0:20fc0ecfb510 118 * 0xFF -> Device code erased or target missing.
chris 0:20fc0ecfb510 119 * 0x01 -> Device is locked.
chris 0:20fc0ecfb510 120 */
chris 0:20fc0ecfb510 121 int readPartFamilyAndFlashSize(void);
chris 0:20fc0ecfb510 122
chris 0:20fc0ecfb510 123 /**
chris 0:20fc0ecfb510 124 * Read the part number.
chris 0:20fc0ecfb510 125 *
chris 0:20fc0ecfb510 126 * Returns:
chris 0:20fc0ecfb510 127 *
chris 0:20fc0ecfb510 128 * Code identifying the part number.
chris 0:20fc0ecfb510 129 * 0xFF -> Device code erased or target missing.
chris 0:20fc0ecfb510 130 * 0x02 -> Device is locked.
chris 0:20fc0ecfb510 131 */
chris 0:20fc0ecfb510 132 int readPartNumber(void);
chris 0:20fc0ecfb510 133
chris 0:20fc0ecfb510 134 /**
chris 0:20fc0ecfb510 135 * Issue a chip erase command to the AVR microcontroller.
chris 0:20fc0ecfb510 136 */
chris 0:20fc0ecfb510 137 void chipErase(void);
chris 0:20fc0ecfb510 138
chris 0:20fc0ecfb510 139 /**
chris 0:20fc0ecfb510 140 * Load a byte into the memory page buffer.
chris 0:20fc0ecfb510 141 *
chris 0:20fc0ecfb510 142 * Parameters:
chris 0:20fc0ecfb510 143 *
chris 0:20fc0ecfb510 144 * highLow - Indicate whether the byte being loaded is a high or low byte.
chris 0:20fc0ecfb510 145 * data - The data byte to load.
chris 0:20fc0ecfb510 146 */
chris 0:20fc0ecfb510 147 void loadMemoryPage(int highLow, char address, char data);
chris 0:20fc0ecfb510 148
chris 0:20fc0ecfb510 149 /**
chris 0:20fc0ecfb510 150 * Write the memory page buffer to flash memory.
chris 0:20fc0ecfb510 151 *
chris 0:20fc0ecfb510 152 * Parameters:
chris 0:20fc0ecfb510 153 *
chris 0:20fc0ecfb510 154 * pageNumber - The page number to write to in flash memory.
chris 0:20fc0ecfb510 155 */
chris 0:20fc0ecfb510 156 void writeFlashMemoryPage(char pageNumber);
chris 0:20fc0ecfb510 157
chris 0:20fc0ecfb510 158 /**
chris 0:20fc0ecfb510 159 * Read a byte from program memory.
chris 0:20fc0ecfb510 160 *
chris 0:20fc0ecfb510 161 * Parameters:
chris 0:20fc0ecfb510 162 *
chris 0:20fc0ecfb510 163 * highLow - Indicate whether the byte being read is a low or high byte.
chris 0:20fc0ecfb510 164 * pageNumber - The page number to read from.
chris 0:20fc0ecfb510 165 * pageOffset - Address of byte in the page.
chris 0:20fc0ecfb510 166 *
chris 0:20fc0ecfb510 167 * Returns:
chris 0:20fc0ecfb510 168 *
chris 0:20fc0ecfb510 169 * The byte at the specified memory location.
chris 0:20fc0ecfb510 170 */
chris 0:20fc0ecfb510 171 char readProgramMemory(int highLow, char pageNumber, char pageOffset);
chris 0:20fc0ecfb510 172
chris 0:20fc0ecfb510 173 /**
chris 0:20fc0ecfb510 174 * Check the binary has been written correctly.
chris 0:20fc0ecfb510 175 *
chris 0:20fc0ecfb510 176 * Paramters:
chris 0:20fc0ecfb510 177 *
chris 0:20fc0ecfb510 178 * numPages - The number of pages written to the AVR microcontroller.
chris 0:20fc0ecfb510 179 * binary - File pointer to the binary used.
chris 0:20fc0ecfb510 180 * Returns:
chris 0:20fc0ecfb510 181 *
chris 0:20fc0ecfb510 182 * 0 -> No inconsistencies between binary file and AVR flash memory.
chris 0:20fc0ecfb510 183 * -1 -> Binary file was not written correctly.
chris 0:20fc0ecfb510 184 */
chris 0:20fc0ecfb510 185 int checkMemory(int numPages, FILE* binary);
chris 0:20fc0ecfb510 186
chris 0:20fc0ecfb510 187 DigitalOut* nReset_;
chris 0:20fc0ecfb510 188 SPI* spi_;
chris 0:20fc0ecfb510 189
chris 0:20fc0ecfb510 190 };
chris 0:20fc0ecfb510 191
chris 0:20fc0ecfb510 192 #endif /* AVR910_H */