Aaron Berk
/
mAVRISP
Program an AVR microcontroller using mbed.
AVR910.h@2:99c56829a2a8, 2010-09-09 (annotated)
- Committer:
- aberk
- Date:
- Thu Sep 09 11:11:37 2010 +0000
- Revision:
- 2:99c56829a2a8
- Parent:
- 1:276f6df4be7a
Improved support and documentation for non-paged memory devices.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aberk | 0:3066745764a5 | 1 | /** |
aberk | 0:3066745764a5 | 2 | * @author Aaron Berk |
aberk | 0:3066745764a5 | 3 | * |
aberk | 0:3066745764a5 | 4 | * @section LICENSE |
aberk | 0:3066745764a5 | 5 | * |
aberk | 0:3066745764a5 | 6 | * Copyright (c) 2010 Aaron Berk |
aberk | 0:3066745764a5 | 7 | * |
aberk | 0:3066745764a5 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
aberk | 0:3066745764a5 | 9 | * of this software and associated documentation files (the "Software"), to deal |
aberk | 0:3066745764a5 | 10 | * in the Software without restriction, including without limitation the rights |
aberk | 0:3066745764a5 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
aberk | 0:3066745764a5 | 12 | * copies of the Software, and to permit persons to whom the Software is |
aberk | 0:3066745764a5 | 13 | * furnished to do so, subject to the following conditions: |
aberk | 0:3066745764a5 | 14 | * |
aberk | 0:3066745764a5 | 15 | * The above copyright notice and this permission notice shall be included in |
aberk | 0:3066745764a5 | 16 | * all copies or substantial portions of the Software. |
aberk | 0:3066745764a5 | 17 | * |
aberk | 0:3066745764a5 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
aberk | 0:3066745764a5 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
aberk | 0:3066745764a5 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
aberk | 0:3066745764a5 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
aberk | 0:3066745764a5 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
aberk | 0:3066745764a5 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
aberk | 0:3066745764a5 | 24 | * THE SOFTWARE. |
aberk | 0:3066745764a5 | 25 | * |
aberk | 0:3066745764a5 | 26 | * @section DESCRIPTION |
aberk | 0:3066745764a5 | 27 | * |
aberk | 0:3066745764a5 | 28 | * Program AVR chips with the AVR910 ISP (in-system programming) protocol, |
aberk | 0:3066745764a5 | 29 | * using an mbed. |
aberk | 0:3066745764a5 | 30 | * |
aberk | 0:3066745764a5 | 31 | * AVR910 Application Note: |
aberk | 0:3066745764a5 | 32 | * |
aberk | 0:3066745764a5 | 33 | * http://www.atmel.com/dyn/resources/prod_documents/doc0943.pdf |
aberk | 0:3066745764a5 | 34 | */ |
aberk | 0:3066745764a5 | 35 | |
aberk | 0:3066745764a5 | 36 | #ifndef MBED_AVR910_H |
aberk | 0:3066745764a5 | 37 | #define MBED_AVR910_H |
aberk | 0:3066745764a5 | 38 | |
aberk | 0:3066745764a5 | 39 | /** |
aberk | 0:3066745764a5 | 40 | * Includes |
aberk | 0:3066745764a5 | 41 | */ |
aberk | 0:3066745764a5 | 42 | #include "mbed.h" |
aberk | 0:3066745764a5 | 43 | |
aberk | 0:3066745764a5 | 44 | /** |
aberk | 0:3066745764a5 | 45 | * Defines |
aberk | 0:3066745764a5 | 46 | */ |
aberk | 0:3066745764a5 | 47 | |
aberk | 0:3066745764a5 | 48 | #define PATH_TO_BINARY "/local/AVRCODE.bin" |
aberk | 0:3066745764a5 | 49 | |
aberk | 0:3066745764a5 | 50 | //Commands |
aberk | 2:99c56829a2a8 | 51 | #define ATMEL_VENDOR_CODE 0x1E |
aberk | 2:99c56829a2a8 | 52 | #define DEVICE_LOCKED 0x00 |
aberk | 2:99c56829a2a8 | 53 | #define WRITE_HIGH_BYTE 0x48 |
aberk | 2:99c56829a2a8 | 54 | #define WRITE_LOW_BYTE 0x40 |
aberk | 2:99c56829a2a8 | 55 | #define READ_HIGH_BYTE 0x28 |
aberk | 2:99c56829a2a8 | 56 | #define READ_LOW_BYTE 0x20 |
aberk | 2:99c56829a2a8 | 57 | #define WRITE_HIGH_FLASH_BYTE 0x68 |
aberk | 2:99c56829a2a8 | 58 | #define WRITE_LOW_FLASH_BYTE 0x60 |
aberk | 0:3066745764a5 | 59 | |
aberk | 0:3066745764a5 | 60 | #define PAGE_SIZE ATMEGA328P_PAGESIZE |
aberk | 0:3066745764a5 | 61 | #define NUM_PAGES ATMEGA328P_NUM_PAGES |
aberk | 0:3066745764a5 | 62 | |
aberk | 0:3066745764a5 | 63 | //ATMega328P |
aberk | 0:3066745764a5 | 64 | #define ATMEGA328P_PAGESIZE 64 //Size in words. |
aberk | 0:3066745764a5 | 65 | #define ATMEGA328P_NUM_PAGES 256 |
aberk | 0:3066745764a5 | 66 | |
aberk | 0:3066745764a5 | 67 | /** |
aberk | 0:3066745764a5 | 68 | * AVR910 ISP |
aberk | 0:3066745764a5 | 69 | */ |
aberk | 0:3066745764a5 | 70 | class AVR910 { |
aberk | 0:3066745764a5 | 71 | |
aberk | 0:3066745764a5 | 72 | public: |
aberk | 0:3066745764a5 | 73 | |
aberk | 0:3066745764a5 | 74 | /** |
aberk | 0:3066745764a5 | 75 | * Constructor. |
aberk | 0:3066745764a5 | 76 | * |
aberk | 0:3066745764a5 | 77 | * @param mosi mbed pin for MOSI SPI line. |
aberk | 0:3066745764a5 | 78 | * @param miso mbed pin for MISO SPI line. |
aberk | 0:3066745764a5 | 79 | * @param sclk mbed pin for SCLK SPI line. |
aberk | 0:3066745764a5 | 80 | * @param nReset mbed pin for not reset line on the ISP interface. |
aberk | 0:3066745764a5 | 81 | * |
aberk | 0:3066745764a5 | 82 | * Sends an enable programming command, allowing device registers to be |
aberk | 0:3066745764a5 | 83 | * read and commands sent. |
aberk | 0:3066745764a5 | 84 | */ |
aberk | 0:3066745764a5 | 85 | AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset); |
aberk | 0:3066745764a5 | 86 | |
aberk | 0:3066745764a5 | 87 | /** |
aberk | 0:3066745764a5 | 88 | * Program the AVR microcontroller connected to the mbed. |
aberk | 0:3066745764a5 | 89 | * |
aberk | 0:3066745764a5 | 90 | * Sends a chip erase command followed by writing the binary to the AVR |
aberk | 0:3066745764a5 | 91 | * page buffer and writing the page buffer to flash memory whenever it is |
aberk | 0:3066745764a5 | 92 | * full. |
aberk | 0:3066745764a5 | 93 | * |
aberk | 0:3066745764a5 | 94 | * @param binary File pointer to the binary file to be loaded onto the |
aberk | 0:3066745764a5 | 95 | * AVR microcontroller. |
aberk | 2:99c56829a2a8 | 96 | * @param pageSize The size of one page on the device in words. If the |
aberk | 2:99c56829a2a8 | 97 | * device does not use paged memory, set this as the size |
aberk | 2:99c56829a2a8 | 98 | * of memory of the device in words. |
aberk | 1:276f6df4be7a | 99 | * @param numPages The number of pages on the device. If the device does |
aberk | 1:276f6df4be7a | 100 | * not use paged memory, set this to 1 (default). |
aberk | 0:3066745764a5 | 101 | * |
aberk | 0:3066745764a5 | 102 | * @return 0 => AVR microcontroller programmed successfully. |
aberk | 0:3066745764a5 | 103 | * -1 => Problem during programming. |
aberk | 0:3066745764a5 | 104 | */ |
aberk | 1:276f6df4be7a | 105 | int program(FILE* binary, int pageSize, int numPages = 1); |
aberk | 0:3066745764a5 | 106 | |
aberk | 0:3066745764a5 | 107 | /** |
aberk | 0:3066745764a5 | 108 | * Set the frequency of the SPI communication. |
aberk | 0:3066745764a5 | 109 | * |
aberk | 0:3066745764a5 | 110 | * (Wrapper for SPI::frequency) |
aberk | 0:3066745764a5 | 111 | * |
aberk | 0:3066745764a5 | 112 | * @param frequency Frequency of the SPI communication in hertz. |
aberk | 0:3066745764a5 | 113 | */ |
aberk | 0:3066745764a5 | 114 | void setFrequency(int frequency); |
aberk | 2:99c56829a2a8 | 115 | |
aberk | 0:3066745764a5 | 116 | /** |
aberk | 0:3066745764a5 | 117 | * Read the vendor code of the device. |
aberk | 0:3066745764a5 | 118 | * |
aberk | 0:3066745764a5 | 119 | * @return The vendor code - should be 0x1E for Atmel. |
aberk | 0:3066745764a5 | 120 | * 0x00 -> Device is locked. |
aberk | 0:3066745764a5 | 121 | */ |
aberk | 0:3066745764a5 | 122 | int readVendorCode(void); |
aberk | 0:3066745764a5 | 123 | |
aberk | 0:3066745764a5 | 124 | /** |
aberk | 0:3066745764a5 | 125 | * Read the part family and flash size of the device. |
aberk | 0:3066745764a5 | 126 | * |
aberk | 0:3066745764a5 | 127 | * @return Code indicating the family of AVR microcontrollers the device comes |
aberk | 0:3066745764a5 | 128 | * from and how much flash memory it contains. |
aberk | 0:3066745764a5 | 129 | * 0xFF -> Device code erased or target missing. |
aberk | 0:3066745764a5 | 130 | * 0x01 -> Device is locked. |
aberk | 0:3066745764a5 | 131 | */ |
aberk | 0:3066745764a5 | 132 | int readPartFamilyAndFlashSize(void); |
aberk | 0:3066745764a5 | 133 | |
aberk | 0:3066745764a5 | 134 | /** |
aberk | 0:3066745764a5 | 135 | * Read the part number. |
aberk | 0:3066745764a5 | 136 | * |
aberk | 0:3066745764a5 | 137 | * @return Code identifying the part number. |
aberk | 0:3066745764a5 | 138 | * 0xFF -> Device code erased or target missing. |
aberk | 0:3066745764a5 | 139 | * 0x02 -> Device is locked. |
aberk | 0:3066745764a5 | 140 | */ |
aberk | 0:3066745764a5 | 141 | int readPartNumber(void); |
aberk | 0:3066745764a5 | 142 | |
aberk | 0:3066745764a5 | 143 | private: |
aberk | 0:3066745764a5 | 144 | |
aberk | 0:3066745764a5 | 145 | /** |
aberk | 0:3066745764a5 | 146 | * Issue an enable programming command to the AVR microcontroller. |
aberk | 0:3066745764a5 | 147 | * |
aberk | 0:3066745764a5 | 148 | * @param 0 to indicate programming was enabled successfully. |
aberk | 0:3066745764a5 | 149 | * -1 to indicate programming was not enabled. |
aberk | 0:3066745764a5 | 150 | */ |
aberk | 0:3066745764a5 | 151 | int enableProgramming(void); |
aberk | 0:3066745764a5 | 152 | |
aberk | 0:3066745764a5 | 153 | /** |
aberk | 0:3066745764a5 | 154 | * Poll the device until it has finished its current operation. |
aberk | 0:3066745764a5 | 155 | */ |
aberk | 0:3066745764a5 | 156 | void poll(void); |
aberk | 0:3066745764a5 | 157 | |
aberk | 0:3066745764a5 | 158 | /** |
aberk | 0:3066745764a5 | 159 | * Issue a chip erase command to the AVR microcontroller. |
aberk | 0:3066745764a5 | 160 | */ |
aberk | 0:3066745764a5 | 161 | void chipErase(void); |
aberk | 0:3066745764a5 | 162 | |
aberk | 0:3066745764a5 | 163 | /** |
aberk | 0:3066745764a5 | 164 | * Load a byte into the memory page buffer. |
aberk | 0:3066745764a5 | 165 | * |
aberk | 2:99c56829a2a8 | 166 | * @param highLow Indicate whether the byte being loaded is a high or low |
aberk | 0:3066745764a5 | 167 | * byte. |
aberk | 0:3066745764a5 | 168 | * @param data The data byte to load. |
aberk | 0:3066745764a5 | 169 | */ |
aberk | 0:3066745764a5 | 170 | void loadMemoryPage(int highLow, char address, char data); |
aberk | 0:3066745764a5 | 171 | |
aberk | 0:3066745764a5 | 172 | /** |
aberk | 2:99c56829a2a8 | 173 | * Write a byte into the flash memory. |
aberk | 2:99c56829a2a8 | 174 | * |
aberk | 2:99c56829a2a8 | 175 | * @param highLow Indicate whether the byte being loaded is a high or low |
aberk | 2:99c56829a2a8 | 176 | * byte. |
aberk | 2:99c56829a2a8 | 177 | * @param address The address to load the byte at. |
aberk | 2:99c56829a2a8 | 178 | * @param data The data byte to load. |
aberk | 2:99c56829a2a8 | 179 | */ |
aberk | 2:99c56829a2a8 | 180 | void writeFlashMemoryByte(int highLow, int address, char data); |
aberk | 2:99c56829a2a8 | 181 | |
aberk | 2:99c56829a2a8 | 182 | /** |
aberk | 0:3066745764a5 | 183 | * Write the memory page buffer to flash memory. |
aberk | 0:3066745764a5 | 184 | * |
aberk | 0:3066745764a5 | 185 | * @param pageNumber The page number to write to in flash memory. |
aberk | 0:3066745764a5 | 186 | */ |
aberk | 0:3066745764a5 | 187 | void writeFlashMemoryPage(char pageNumber); |
aberk | 0:3066745764a5 | 188 | |
aberk | 0:3066745764a5 | 189 | /** |
aberk | 0:3066745764a5 | 190 | * Read a byte from program memory. |
aberk | 0:3066745764a5 | 191 | * |
aberk | 0:3066745764a5 | 192 | * @param highLow Indicate whether the byte being read is a low or high byte. |
aberk | 0:3066745764a5 | 193 | * @param pageNumber The page number to read from. |
aberk | 0:3066745764a5 | 194 | * @param pageOffset Address of byte in the page. |
aberk | 0:3066745764a5 | 195 | * |
aberk | 0:3066745764a5 | 196 | * @return The byte at the specified memory location. |
aberk | 0:3066745764a5 | 197 | */ |
aberk | 0:3066745764a5 | 198 | char readProgramMemory(int highLow, char pageNumber, char pageOffset); |
aberk | 0:3066745764a5 | 199 | |
aberk | 0:3066745764a5 | 200 | /** |
aberk | 0:3066745764a5 | 201 | * Check the binary has been written correctly. |
aberk | 0:3066745764a5 | 202 | * |
aberk | 0:3066745764a5 | 203 | * @param numPages The number of pages written to the AVR microcontroller. |
aberk | 0:3066745764a5 | 204 | * @param binary File pointer to the binary used. |
aberk | 2:99c56829a2a8 | 205 | * |
aberk | 0:3066745764a5 | 206 | * @return 0 -> No inconsistencies between binary file and AVR flash memory. |
aberk | 0:3066745764a5 | 207 | * -1 -> Binary file was not written correctly. |
aberk | 0:3066745764a5 | 208 | */ |
aberk | 0:3066745764a5 | 209 | int checkMemory(int numPages, FILE* binary); |
aberk | 2:99c56829a2a8 | 210 | |
aberk | 0:3066745764a5 | 211 | SPI spi_; |
aberk | 0:3066745764a5 | 212 | DigitalOut nReset_; |
aberk | 2:99c56829a2a8 | 213 | |
aberk | 0:3066745764a5 | 214 | }; |
aberk | 0:3066745764a5 | 215 | |
aberk | 0:3066745764a5 | 216 | #endif /* AVR910_H */ |