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 21:54:05 2015 +0000
Revision:
3:df6782d01720
Parent:
2:99c56829a2a8
IT WORKS!!! I can now program an atmega328 with ISP via the mbed. No bootloader.

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
jeroenmbed 3:df6782d01720 48 #define PATH_TO_BINARY "local/T.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);
jeroenmbed 3:df6782d01720 106 int programData(char* myData, int fileSize, int pageSize, int numPages = 1);
aberk 0:3066745764a5 107
aberk 0:3066745764a5 108 /**
aberk 0:3066745764a5 109 * Set the frequency of the SPI communication.
aberk 0:3066745764a5 110 *
aberk 0:3066745764a5 111 * (Wrapper for SPI::frequency)
aberk 0:3066745764a5 112 *
aberk 0:3066745764a5 113 * @param frequency Frequency of the SPI communication in hertz.
aberk 0:3066745764a5 114 */
aberk 0:3066745764a5 115 void setFrequency(int frequency);
aberk 2:99c56829a2a8 116
aberk 0:3066745764a5 117 /**
aberk 0:3066745764a5 118 * Read the vendor code of the device.
aberk 0:3066745764a5 119 *
aberk 0:3066745764a5 120 * @return The vendor code - should be 0x1E for Atmel.
aberk 0:3066745764a5 121 * 0x00 -> Device is locked.
aberk 0:3066745764a5 122 */
aberk 0:3066745764a5 123 int readVendorCode(void);
aberk 0:3066745764a5 124
aberk 0:3066745764a5 125 /**
aberk 0:3066745764a5 126 * Read the part family and flash size of the device.
aberk 0:3066745764a5 127 *
aberk 0:3066745764a5 128 * @return Code indicating the family of AVR microcontrollers the device comes
aberk 0:3066745764a5 129 * from and how much flash memory it contains.
aberk 0:3066745764a5 130 * 0xFF -> Device code erased or target missing.
aberk 0:3066745764a5 131 * 0x01 -> Device is locked.
aberk 0:3066745764a5 132 */
aberk 0:3066745764a5 133 int readPartFamilyAndFlashSize(void);
aberk 0:3066745764a5 134
aberk 0:3066745764a5 135 /**
aberk 0:3066745764a5 136 * Read the part number.
aberk 0:3066745764a5 137 *
aberk 0:3066745764a5 138 * @return Code identifying the part number.
aberk 0:3066745764a5 139 * 0xFF -> Device code erased or target missing.
aberk 0:3066745764a5 140 * 0x02 -> Device is locked.
aberk 0:3066745764a5 141 */
aberk 0:3066745764a5 142 int readPartNumber(void);
aberk 0:3066745764a5 143
aberk 0:3066745764a5 144 private:
aberk 0:3066745764a5 145
aberk 0:3066745764a5 146 /**
aberk 0:3066745764a5 147 * Issue an enable programming command to the AVR microcontroller.
aberk 0:3066745764a5 148 *
aberk 0:3066745764a5 149 * @param 0 to indicate programming was enabled successfully.
aberk 0:3066745764a5 150 * -1 to indicate programming was not enabled.
aberk 0:3066745764a5 151 */
aberk 0:3066745764a5 152 int enableProgramming(void);
aberk 0:3066745764a5 153
aberk 0:3066745764a5 154 /**
aberk 0:3066745764a5 155 * Poll the device until it has finished its current operation.
aberk 0:3066745764a5 156 */
aberk 0:3066745764a5 157 void poll(void);
aberk 0:3066745764a5 158
aberk 0:3066745764a5 159 /**
aberk 0:3066745764a5 160 * Issue a chip erase command to the AVR microcontroller.
aberk 0:3066745764a5 161 */
aberk 0:3066745764a5 162 void chipErase(void);
aberk 0:3066745764a5 163
aberk 0:3066745764a5 164 /**
aberk 0:3066745764a5 165 * Load a byte into the memory page buffer.
aberk 0:3066745764a5 166 *
aberk 2:99c56829a2a8 167 * @param highLow Indicate whether the byte being loaded is a high or low
aberk 0:3066745764a5 168 * byte.
aberk 0:3066745764a5 169 * @param data The data byte to load.
aberk 0:3066745764a5 170 */
aberk 0:3066745764a5 171 void loadMemoryPage(int highLow, char address, char data);
aberk 0:3066745764a5 172
aberk 0:3066745764a5 173 /**
aberk 2:99c56829a2a8 174 * Write a byte into the flash memory.
aberk 2:99c56829a2a8 175 *
aberk 2:99c56829a2a8 176 * @param highLow Indicate whether the byte being loaded is a high or low
aberk 2:99c56829a2a8 177 * byte.
aberk 2:99c56829a2a8 178 * @param address The address to load the byte at.
aberk 2:99c56829a2a8 179 * @param data The data byte to load.
aberk 2:99c56829a2a8 180 */
aberk 2:99c56829a2a8 181 void writeFlashMemoryByte(int highLow, int address, char data);
aberk 2:99c56829a2a8 182
aberk 2:99c56829a2a8 183 /**
aberk 0:3066745764a5 184 * Write the memory page buffer to flash memory.
aberk 0:3066745764a5 185 *
aberk 0:3066745764a5 186 * @param pageNumber The page number to write to in flash memory.
aberk 0:3066745764a5 187 */
aberk 0:3066745764a5 188 void writeFlashMemoryPage(char pageNumber);
aberk 0:3066745764a5 189
aberk 0:3066745764a5 190 /**
aberk 0:3066745764a5 191 * Read a byte from program memory.
aberk 0:3066745764a5 192 *
aberk 0:3066745764a5 193 * @param highLow Indicate whether the byte being read is a low or high byte.
aberk 0:3066745764a5 194 * @param pageNumber The page number to read from.
aberk 0:3066745764a5 195 * @param pageOffset Address of byte in the page.
aberk 0:3066745764a5 196 *
aberk 0:3066745764a5 197 * @return The byte at the specified memory location.
aberk 0:3066745764a5 198 */
aberk 0:3066745764a5 199 char readProgramMemory(int highLow, char pageNumber, char pageOffset);
aberk 0:3066745764a5 200
aberk 0:3066745764a5 201 /**
aberk 0:3066745764a5 202 * Check the binary has been written correctly.
aberk 0:3066745764a5 203 *
aberk 0:3066745764a5 204 * @param numPages The number of pages written to the AVR microcontroller.
aberk 0:3066745764a5 205 * @param binary File pointer to the binary used.
aberk 2:99c56829a2a8 206 *
aberk 0:3066745764a5 207 * @return 0 -> No inconsistencies between binary file and AVR flash memory.
aberk 0:3066745764a5 208 * -1 -> Binary file was not written correctly.
aberk 0:3066745764a5 209 */
aberk 0:3066745764a5 210 int checkMemory(int numPages, FILE* binary);
jeroenmbed 3:df6782d01720 211 int checkMemoryData(int numPages, char* myData, int fileSize);
aberk 2:99c56829a2a8 212
aberk 0:3066745764a5 213 SPI spi_;
aberk 0:3066745764a5 214 DigitalOut nReset_;
aberk 2:99c56829a2a8 215
aberk 0:3066745764a5 216 };
aberk 0:3066745764a5 217
aberk 0:3066745764a5 218 #endif /* AVR910_H */