Program an AVR microcontroller using mbed.

Dependencies:   mbed

Committer:
aberk
Date:
Wed Sep 01 10:22:51 2010 +0000
Revision:
1:276f6df4be7a
Parent:
0:3066745764a5
Child:
2:99c56829a2a8
Added support for non-paged memory AVRs and changed parameters for the program(...) method to include page size (memory size if non-paged memory) and number of pages (=1 if non-paged memory).

Who changed what in which revision?

UserRevisionLine numberNew 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 0:3066745764a5 51 #define ATMEL_VENDOR_CODE 0x1E
aberk 0:3066745764a5 52 #define DEVICE_LOCKED 0x00
aberk 0:3066745764a5 53 #define WRITE_HIGH_BYTE 0x48
aberk 0:3066745764a5 54 #define WRITE_LOW_BYTE 0x40
aberk 0:3066745764a5 55 #define READ_HIGH_BYTE 0x28
aberk 0:3066745764a5 56 #define READ_LOW_BYTE 0x20
aberk 0:3066745764a5 57
aberk 0:3066745764a5 58 #define PAGE_SIZE ATMEGA328P_PAGESIZE
aberk 0:3066745764a5 59 #define NUM_PAGES ATMEGA328P_NUM_PAGES
aberk 0:3066745764a5 60
aberk 0:3066745764a5 61 //ATMega328P
aberk 0:3066745764a5 62 #define ATMEGA328P_PAGESIZE 64 //Size in words.
aberk 0:3066745764a5 63 #define ATMEGA328P_NUM_PAGES 256
aberk 0:3066745764a5 64
aberk 0:3066745764a5 65 /**
aberk 0:3066745764a5 66 * AVR910 ISP
aberk 0:3066745764a5 67 */
aberk 0:3066745764a5 68 class AVR910 {
aberk 0:3066745764a5 69
aberk 0:3066745764a5 70 public:
aberk 0:3066745764a5 71
aberk 0:3066745764a5 72 /**
aberk 0:3066745764a5 73 * Constructor.
aberk 0:3066745764a5 74 *
aberk 0:3066745764a5 75 * @param mosi mbed pin for MOSI SPI line.
aberk 0:3066745764a5 76 * @param miso mbed pin for MISO SPI line.
aberk 0:3066745764a5 77 * @param sclk mbed pin for SCLK SPI line.
aberk 0:3066745764a5 78 * @param nReset mbed pin for not reset line on the ISP interface.
aberk 0:3066745764a5 79 *
aberk 0:3066745764a5 80 * Sends an enable programming command, allowing device registers to be
aberk 0:3066745764a5 81 * read and commands sent.
aberk 0:3066745764a5 82 */
aberk 0:3066745764a5 83 AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset);
aberk 0:3066745764a5 84
aberk 0:3066745764a5 85 /**
aberk 0:3066745764a5 86 * Program the AVR microcontroller connected to the mbed.
aberk 0:3066745764a5 87 *
aberk 0:3066745764a5 88 * Sends a chip erase command followed by writing the binary to the AVR
aberk 0:3066745764a5 89 * page buffer and writing the page buffer to flash memory whenever it is
aberk 0:3066745764a5 90 * full.
aberk 0:3066745764a5 91 *
aberk 0:3066745764a5 92 * @param binary File pointer to the binary file to be loaded onto the
aberk 0:3066745764a5 93 * AVR microcontroller.
aberk 1:276f6df4be7a 94 * @param pageSize The size of one page on the device. If the device does
aberk 1:276f6df4be7a 95 * not use paged memory, set this as the size of memory of
aberk 1:276f6df4be7a 96 * the device.
aberk 1:276f6df4be7a 97 * @param numPages The number of pages on the device. If the device does
aberk 1:276f6df4be7a 98 * not use paged memory, set this to 1 (default).
aberk 0:3066745764a5 99 *
aberk 0:3066745764a5 100 * @return 0 => AVR microcontroller programmed successfully.
aberk 0:3066745764a5 101 * -1 => Problem during programming.
aberk 0:3066745764a5 102 */
aberk 1:276f6df4be7a 103 int program(FILE* binary, int pageSize, int numPages = 1);
aberk 0:3066745764a5 104
aberk 0:3066745764a5 105 /**
aberk 0:3066745764a5 106 * Set the frequency of the SPI communication.
aberk 0:3066745764a5 107 *
aberk 0:3066745764a5 108 * (Wrapper for SPI::frequency)
aberk 0:3066745764a5 109 *
aberk 0:3066745764a5 110 * @param frequency Frequency of the SPI communication in hertz.
aberk 0:3066745764a5 111 */
aberk 0:3066745764a5 112 void setFrequency(int frequency);
aberk 0:3066745764a5 113
aberk 0:3066745764a5 114 /**
aberk 0:3066745764a5 115 * Read the vendor code of the device.
aberk 0:3066745764a5 116 *
aberk 0:3066745764a5 117 * @return The vendor code - should be 0x1E for Atmel.
aberk 0:3066745764a5 118 * 0x00 -> Device is locked.
aberk 0:3066745764a5 119 */
aberk 0:3066745764a5 120 int readVendorCode(void);
aberk 0:3066745764a5 121
aberk 0:3066745764a5 122 /**
aberk 0:3066745764a5 123 * Read the part family and flash size of the device.
aberk 0:3066745764a5 124 *
aberk 0:3066745764a5 125 * @return Code indicating the family of AVR microcontrollers the device comes
aberk 0:3066745764a5 126 * from and how much flash memory it contains.
aberk 0:3066745764a5 127 * 0xFF -> Device code erased or target missing.
aberk 0:3066745764a5 128 * 0x01 -> Device is locked.
aberk 0:3066745764a5 129 */
aberk 0:3066745764a5 130 int readPartFamilyAndFlashSize(void);
aberk 0:3066745764a5 131
aberk 0:3066745764a5 132 /**
aberk 0:3066745764a5 133 * Read the part number.
aberk 0:3066745764a5 134 *
aberk 0:3066745764a5 135 * @return Code identifying the part number.
aberk 0:3066745764a5 136 * 0xFF -> Device code erased or target missing.
aberk 0:3066745764a5 137 * 0x02 -> Device is locked.
aberk 0:3066745764a5 138 */
aberk 0:3066745764a5 139 int readPartNumber(void);
aberk 0:3066745764a5 140
aberk 0:3066745764a5 141 private:
aberk 0:3066745764a5 142
aberk 0:3066745764a5 143 /**
aberk 0:3066745764a5 144 * Issue an enable programming command to the AVR microcontroller.
aberk 0:3066745764a5 145 *
aberk 0:3066745764a5 146 * @param 0 to indicate programming was enabled successfully.
aberk 0:3066745764a5 147 * -1 to indicate programming was not enabled.
aberk 0:3066745764a5 148 */
aberk 0:3066745764a5 149 int enableProgramming(void);
aberk 0:3066745764a5 150
aberk 0:3066745764a5 151 /**
aberk 0:3066745764a5 152 * Poll the device until it has finished its current operation.
aberk 0:3066745764a5 153 */
aberk 0:3066745764a5 154 void poll(void);
aberk 0:3066745764a5 155
aberk 0:3066745764a5 156 /**
aberk 0:3066745764a5 157 * Issue a chip erase command to the AVR microcontroller.
aberk 0:3066745764a5 158 */
aberk 0:3066745764a5 159 void chipErase(void);
aberk 0:3066745764a5 160
aberk 0:3066745764a5 161 /**
aberk 0:3066745764a5 162 * Load a byte into the memory page buffer.
aberk 0:3066745764a5 163 *
aberk 0:3066745764a5 164 * @param highLow Indicate whether the byte being loaded is a high or low
aberk 0:3066745764a5 165 * byte.
aberk 0:3066745764a5 166 * @param data The data byte to load.
aberk 0:3066745764a5 167 */
aberk 0:3066745764a5 168 void loadMemoryPage(int highLow, char address, char data);
aberk 0:3066745764a5 169
aberk 0:3066745764a5 170 /**
aberk 0:3066745764a5 171 * Write the memory page buffer to flash memory.
aberk 0:3066745764a5 172 *
aberk 0:3066745764a5 173 * @param pageNumber The page number to write to in flash memory.
aberk 0:3066745764a5 174 */
aberk 0:3066745764a5 175 void writeFlashMemoryPage(char pageNumber);
aberk 0:3066745764a5 176
aberk 0:3066745764a5 177 /**
aberk 0:3066745764a5 178 * Read a byte from program memory.
aberk 0:3066745764a5 179 *
aberk 0:3066745764a5 180 * @param highLow Indicate whether the byte being read is a low or high byte.
aberk 0:3066745764a5 181 * @param pageNumber The page number to read from.
aberk 0:3066745764a5 182 * @param pageOffset Address of byte in the page.
aberk 0:3066745764a5 183 *
aberk 0:3066745764a5 184 * @return The byte at the specified memory location.
aberk 0:3066745764a5 185 */
aberk 0:3066745764a5 186 char readProgramMemory(int highLow, char pageNumber, char pageOffset);
aberk 0:3066745764a5 187
aberk 0:3066745764a5 188 /**
aberk 0:3066745764a5 189 * Check the binary has been written correctly.
aberk 0:3066745764a5 190 *
aberk 0:3066745764a5 191 * @param numPages The number of pages written to the AVR microcontroller.
aberk 0:3066745764a5 192 * @param binary File pointer to the binary used.
aberk 0:3066745764a5 193 *
aberk 0:3066745764a5 194 * @return 0 -> No inconsistencies between binary file and AVR flash memory.
aberk 0:3066745764a5 195 * -1 -> Binary file was not written correctly.
aberk 0:3066745764a5 196 */
aberk 0:3066745764a5 197 int checkMemory(int numPages, FILE* binary);
aberk 0:3066745764a5 198
aberk 0:3066745764a5 199 SPI spi_;
aberk 0:3066745764a5 200 DigitalOut nReset_;
aberk 0:3066745764a5 201
aberk 0:3066745764a5 202 };
aberk 0:3066745764a5 203
aberk 0:3066745764a5 204 #endif /* AVR910_H */