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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AVR910_Serial.h Source File

AVR910_Serial.h

00001 /**
00002  * * @author Aaron Berk, Jeroen Voogd
00003  *
00004  * @section LICENSE
00005  *
00006  * Copyright (c) 2010 Aaron Berk
00007  * Copyright (c) 2015 Jeroen Voogd
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  *
00027  * @section DESCRIPTION
00028  *
00029  * Program AVR chips with the AVR910 ISP (in-system programming) protocol,
00030  * using an mbed. The version of Aaron Berk used a local file system, this version
00031  * useses a python script to feed the data via a serial connection.
00032  *
00033  * AVR910 Application Note:
00034  *
00035  * http://www.atmel.com/dyn/resources/prod_documents/doc0943.pdf
00036  */
00037 
00038 #ifndef MBED_AVR910_H
00039 #define MBED_AVR910_H
00040 
00041 /**
00042  * Includes
00043  */
00044 #include "mbed.h"
00045 
00046 /**
00047  * Defines
00048  */
00049 
00050 //Commands
00051 #define ATMEL_VENDOR_CODE     0x1E
00052 #define DEVICE_LOCKED         0x00
00053 #define WRITE_HIGH_BYTE       0x48
00054 #define WRITE_LOW_BYTE        0x40
00055 #define READ_HIGH_BYTE        0x28
00056 #define READ_LOW_BYTE         0x20
00057 #define WRITE_HIGH_FLASH_BYTE 0x68
00058 #define WRITE_LOW_FLASH_BYTE  0x60
00059 
00060 #define PAGE_SIZE         ATMEGA328P_PAGESIZE
00061 #define NUM_PAGES         ATMEGA328P_NUM_PAGES
00062 
00063 //ATMega328P
00064 #define ATMEGA328P_PAGESIZE  64 //Size in words.
00065 #define ATMEGA328P_NUM_PAGES 256
00066 
00067 /**
00068  * AVR910 ISP
00069  */
00070 class AVR910 {
00071 
00072 public:
00073 
00074     /**
00075      * Constructor.
00076      *
00077      * @param mosi mbed pin for MOSI SPI line.
00078      * @param miso mbed pin for MISO SPI line.
00079      * @param sclk mbed pin for SCLK SPI line.
00080      * @param nReset mbed pin for not reset line on the ISP interface.
00081      *
00082      * Sends an enable programming command, allowing device registers to be
00083      * read and commands sent.
00084      */
00085     AVR910(PinName mosi, PinName miso, PinName sclk, PinName nReset);
00086 
00087     /**
00088      * Program the AVR microcontroller connected to the mbed.
00089      *
00090      * Sends a chip erase command followed by writing the binary to the AVR
00091      * page buffer and writing the page buffer to flash memory whenever it is
00092      * full.
00093      * @param myData The char array with the bytes of the program to be flashed.
00094      * @param dataSize The number of bytes to be flashed
00095      * @param pageSize The size of one page on the device in words. If the
00096      *                 device does not use paged memory, set this as the size
00097      *                 of memory of the device in words.
00098      * @param numPages The number of pages on the device. If the device does
00099      *                 not use paged memory, set this to 1 (default).
00100      *
00101      * @return  0 => AVR microcontroller programmed successfully.
00102      *         -1 => Problem during programming.
00103      */
00104     int programData(char* myData, int dataSize, int pageSize, int numPages = 1);
00105 
00106     /**
00107      * Set the frequency of the SPI communication.
00108      *
00109      * (Wrapper for SPI::frequency)
00110      *
00111      * @param frequency Frequency of the SPI communication in hertz.
00112      */
00113     void setFrequency(int frequency);
00114 
00115     /**
00116      * Read the vendor code of the device.
00117      *
00118      * @return The vendor code - should be 0x1E for Atmel.
00119      *         0x00 -> Device is locked.
00120      */
00121     int readVendorCode(void);
00122 
00123     /**
00124      * Read the part family and flash size of the device.
00125      *
00126      * @return Code indicating the family of AVR microcontrollers the device comes
00127      *         from and how much flash memory it contains.
00128      *         0xFF -> Device code erased or target missing.
00129      *         0x01 -> Device is locked.
00130      */
00131     int readPartFamilyAndFlashSize(void);
00132 
00133     /**
00134      * Read the part number.
00135      *
00136      * @return Code identifying the part number.
00137      *         0xFF -> Device code erased or target missing.
00138      *         0x02 -> Device is locked.
00139      */
00140     int readPartNumber(void);
00141 
00142 private:
00143 
00144     /**
00145      * Issue an enable programming command to the AVR microcontroller.
00146      *
00147      * @param  0 to indicate programming was enabled successfully.
00148      *        -1 to indicate programming was not enabled.
00149      */
00150     int enableProgramming(void);
00151 
00152     /**
00153      * Poll the device until it has finished its current operation.
00154      */
00155     void poll(void);
00156 
00157     /**
00158      * Issue a chip erase command to the AVR microcontroller.
00159      */
00160     void chipErase(void);
00161 
00162     /**
00163      * Load a byte into the memory page buffer.
00164      *
00165      * @param highLow Indicate whether the byte being loaded is a high or low
00166      *                byte.
00167      * @param data The data byte to load.
00168      */
00169     void loadMemoryPage(int highLow, char address, char data);
00170 
00171     /**
00172      * Write a byte into the flash memory.
00173      *
00174      * @param highLow Indicate whether the byte being loaded is a high or low
00175      *                byte.
00176      * @param address The address to load the byte at.
00177      * @param data The data byte to load.
00178      */
00179     void writeFlashMemoryByte(int highLow, int address, char data);
00180 
00181     /**
00182      * Write the memory page buffer to flash memory.
00183      *
00184      * @param pageNumber The page number to write to in flash memory.
00185      */
00186     void writeFlashMemoryPage(char pageNumber);
00187 
00188     /**
00189      * Read a byte from program memory.
00190      *
00191      * @param highLow Indicate whether the byte being read is a low or high byte.
00192      * @param pageNumber The page number to read from.
00193      * @param pageOffset Address of byte in the page.
00194      *
00195      * @return The byte at the specified memory location.
00196      */
00197     char readProgramMemory(int highLow, char pageNumber, char pageOffset);
00198 
00199     /**
00200      * Check the binary has been written correctly.
00201      *
00202      * @param numPages The number of pages written to the AVR microcontroller.
00203      * @param myData The char array with the bytes of the program to be flashed.
00204      * @param dataSize The number of bytes to be flashed
00205      *
00206      * @return  0 -> No inconsistencies between binary file and AVR flash memory.
00207      *         -1 -> Binary file was not written correctly.
00208      */
00209     int checkMemoryData(int numPages, char* myData, int dataSize);
00210 
00211     SPI        spi_;
00212     DigitalOut nReset_;
00213 
00214 };
00215 
00216 #endif /* AVR910_H */