A fork of mAVRISP by Aaron Berk. This version does not use a local file system to hold the AVR program. Instead it uses a serial connection to a PC and a python script to send the AVR program to the mbed.

Dependencies:   mbed

Fork of mAVRISP by Aaron Berk

Committer:
jeroenmbed
Date:
Sat Jan 31 22:44:09 2015 +0000
Revision:
4:ceee1eb7062e
Cleaned up.

Who changed what in which revision?

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