11u24 Eeprom utility.

Dependencies:   TextLCD mbed

Files at this revision

API Documentation at this revision

Comitter:
kstech
Date:
Fri Sep 26 09:22:57 2014 +0000
Commit message:
11u24 Eeprom utility

Changed in this revision

IAP.cpp Show annotated file Show diff for this revision Revisions of this file
IAP.h Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD/TextLCD.cpp Show annotated file Show diff for this revision Revisions of this file
TextLCD/TextLCD.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IAP.cpp	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,248 @@
+/**    IAP : internal Flash memory access library
+ *
+ *        The internal Flash memory access is described in the LPC1768 usermanual. 
+ *            http://www.nxp.com/documents/user_manual/UM10360.pdf
+ *
+ *            Chapter  2: "LPC17xx Memory map"
+ *            Chapter 32: "LPC17xx Flash memory interface and programming"
+ *                refering Rev. 01 - 4 January 2010
+ *
+ *        Released under the MIT License: http://mbed.org/license/mit
+ *
+ *        revision 1.0  09-Mar-2010   1st release
+ *        revision 1.1  12-Mar-2010   chaged: to make possible to reserve flash area for user
+ *                                            it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
+ *
+ *     by Tedd OKANO http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/
+ *     modified by Suga (supported to LPC11U24)
+ */
+
+#include    "mbed.h"
+#include    "IAP.h"
+
+#define     USER_FLASH_AREA_START_STR( x )      STR( x )
+#define     STR( x )                            #x
+
+unsigned char user_area[ USER_FLASH_AREA_SIZE ] __attribute__((section( ".ARM.__at_" USER_FLASH_AREA_START_STR( USER_FLASH_AREA_START ) ), zero_init));
+
+
+/*
+ *  Reserve of flash area is explained by Igor. Please refer next URL
+ *    http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/?page=1#comment-271
+ */
+ 
+//unsigned char user_area[ size ] __attribute__((section(".ARM.__at_0x78000"), zero_init));
+
+/*
+ *  IAP command codes
+ *  Table 589. "IAP Command Summary", Chapter 8. "IAP commands", usermanual
+ */
+
+enum command_code
+        {
+            IAPCommand_Prepare_sector_for_write_operation    = 50,
+            IAPCommand_Copy_RAM_to_Flash,
+            IAPCommand_Erase_sector,
+            IAPCommand_Blank_check_sector,
+            IAPCommand_Read_part_ID,
+            IAPCommand_Read_Boot_Code_version,
+            IAPCommand_Compare,
+            IAPCommand_Reinvoke_ISP,
+            IAPCommand_Read_device_serial_number,
+            IAPCommand_EEPROM_Write = 61,
+            IAPCommand_EEPROM_Read,
+        };
+
+
+/** Read part identification number
+ *
+ *  @return    device ID
+ *  @see       read_serial()
+ */
+
+int IAP::read_ID( void ) {
+    IAP_command[ 0 ]    = IAPCommand_Read_part_ID;
+    
+    iap_entry( IAP_command, IAP_result );
+    
+    //  return ( (int)IAP_result[ 0 ] );
+    return ( (int)IAP_result[ 1 ] );    //  to return the number itself (this command always returns CMD_SUCCESS)
+}
+
+
+/** Read device serial number
+ *
+ *  @return    device serial number
+ *  @see       read_ID()
+ */
+
+int IAP::read_serial( void ) {
+    IAP_command[ 0 ]    = IAPCommand_Read_device_serial_number;
+    
+    iap_entry( IAP_command, IAP_result );
+    
+    //  return ( (int)IAP_result[ 0 ] );
+    return ( (int)IAP_result[ 1 ] );    //  to return the number itself (this command always returns CMD_SUCCESS)
+}
+
+
+/** Blank check sector(s)
+ *  
+ *  @param    start    a Start Sector Number
+ *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
+ *  @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_BLANK | INVALID_SECTOR
+ */
+
+int IAP::blank_check( int start, int end ) {
+    IAP_command[ 0 ]    = IAPCommand_Blank_check_sector;
+    IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
+    IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number)
+
+    iap_entry( IAP_command, IAP_result );
+
+    return ( (int)IAP_result[ 0 ] );
+}
+
+
+/** Erase Sector(s)
+ *  
+ *  @param    start    a Start Sector Number
+ *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
+ *  @return   error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR
+ */
+
+int IAP::erase( int start, int end ) {
+    IAP_command[ 0 ]    = IAPCommand_Erase_sector;
+    IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
+    IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number)
+    IAP_command[ 3 ]    = cclk_kHz;             //  CPU Clock Frequency (CCLK) in kHz
+
+    iap_entry( IAP_command, IAP_result );
+
+    return ( (int)IAP_result[ 0 ] );
+}
+
+
+/** Prepare sector(s) for write operation
+ *  
+ *  @param    start    a Start Sector Number
+ *  @param    end      an End Sector Number (should be greater than or equal to start sector number).
+ *  @return   error code: CMD_SUCCESS | BUSY | INVALID_SECTOR
+ */
+
+int IAP::prepare( int start, int end ) {
+    IAP_command[ 0 ]    = IAPCommand_Prepare_sector_for_write_operation;
+    IAP_command[ 1 ]    = (unsigned int)start;  //  Start Sector Number
+    IAP_command[ 2 ]    = (unsigned int)end;    //  End Sector Number (should be greater than or equal to start sector number).
+    
+    iap_entry( IAP_command, IAP_result );
+    
+    return ( (int)IAP_result[ 0 ] );
+}
+
+
+/** Copy RAM to Flash
+ *  
+ *  @param    source_addr    Source RAM address from which data bytes are to be read. This address should be a word boundary.
+ *  @param    target_addr    Destination flash address where data bytes are to be written. This address should be a 256 byte boundary.
+ *  @param    size           Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
+ *  @return   error code: CMD_SUCCESS | SRC_ADDR_ERROR (Address not a word boundary) | DST_ADDR_ERROR (Address not on correct boundary) | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED | COUNT_ERROR (Byte count is not 256 | 512 | 1024 | 4096) | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | BUSY
+ */
+
+int IAP::write( char *source_addr, char *target_addr, int size ) {
+    IAP_command[ 0 ]    = IAPCommand_Copy_RAM_to_Flash;
+    IAP_command[ 1 ]    = (unsigned int)target_addr;    //  Destination flash address where data bytes are to be written. This address should be a 256 byte boundary.
+    IAP_command[ 2 ]    = (unsigned int)source_addr;    //  Source RAM address from which data bytes are to be read. This address should be a word boundary.
+    IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
+    IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
+
+    iap_entry( IAP_command, IAP_result );
+
+    return ( (int)IAP_result[ 0 ] );
+}
+
+
+/** Compare <address1> <address2> <no of bytes>
+ *  
+ *  @param    source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
+ *  @param    target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
+ *  @param    size         Number of bytes to be compared; should be a multiple of 4.
+ *  @return   error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED     
+ */
+
+int IAP::compare( char *source_addr, char *target_addr, int size ) {
+    IAP_command[ 0 ]    = IAPCommand_Compare;
+    IAP_command[ 1 ]    = (unsigned int)target_addr;    //  Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
+    IAP_command[ 2 ]    = (unsigned int)source_addr;    //  Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
+    IAP_command[ 3 ]    = size;                         //  Number of bytes to be compared; should be a multiple of 4.
+
+    iap_entry( IAP_command, IAP_result );
+
+    return ( (int)IAP_result[ 0 ] );
+}
+
+
+/** Get user reserved flash start address
+ *
+ *  @return    start address of user reserved flash memory
+ *  @see       reserved_flash_area_size()
+ */
+
+char * IAP::reserved_flash_area_start( void )
+{
+    return ( (char *)USER_FLASH_AREA_START );
+}
+
+
+/** Get user reserved flash size
+ *
+ *  @return    size of user reserved flash memory
+ *  @see       reserved_flash_area_start()
+ */
+
+int IAP::reserved_flash_area_size( void )
+{
+    return ( USER_FLASH_AREA_SIZE );
+}
+
+#if defined(TARGET_LPC11U24)
+/** Copy RAM to EEPROM (LPC11U24)
+ *  
+ *  @param    source_addr    Source RAM address from which data bytes are to be read.
+ *  @param    target_addr    Destination EEPROM address where data bytes are to be written.
+ *  @param    size           Number of bytes to be written.
+ *  @return   error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED
+ *  Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
+ */
+int IAP::write_eeprom( char *source_addr, char *target_addr, int size ) {
+    IAP_command[ 0 ]    = IAPCommand_EEPROM_Write;
+    IAP_command[ 1 ]    = (unsigned int)target_addr;    //  Destination EEPROM address where data bytes are to be written. This address should be a 256 byte boundary.
+    IAP_command[ 2 ]    = (unsigned int)source_addr;    //  Source RAM address from which data bytes are to be read. This address should be a word boundary.
+    IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
+    IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
+
+    iap_entry( IAP_command, IAP_result );
+
+    return ( (int)IAP_result[ 0 ] );
+}
+
+/** Copy EEPROM to RAM (LPC11U24)
+ *  
+ *  @param    source_addr    Source EEPROM address from which data bytes are to be read.
+ *  @param    target_addr    Destination RAM address where data bytes are to be written.
+ *  @param    size           Number of bytes to be written.
+ *  @return   error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED
+ *  Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
+ */
+int IAP::read_eeprom( char *source_addr, char *target_addr, int size ) {
+    IAP_command[ 0 ]    = IAPCommand_EEPROM_Read;
+    IAP_command[ 1 ]    = (unsigned int)source_addr;    //  Source EEPROM address from which data bytes are to be read. This address should be a word boundary.
+    IAP_command[ 2 ]    = (unsigned int)target_addr;    //  Destination RAM address where data bytes are to be written. This address should be a 256 byte boundary.
+    IAP_command[ 3 ]    = size;                         //  Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
+    IAP_command[ 4 ]    = cclk_kHz;                     //  CPU Clock Frequency (CCLK) in kHz.
+
+    iap_entry( IAP_command, IAP_result );
+
+    return ( (int)IAP_result[ 0 ] );
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IAP.h	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,237 @@
+/**    IAP : internal Flash memory access library
+ *
+ *        The internal Flash memory access is described in the LPC1768 usermanual. 
+ *            http://www.nxp.com/documents/user_manual/UM10360.pdf
+ *
+ *            Chapter  2: "LPC17xx Memory map"
+ *            Chapter 32: "LPC17xx Flash memory interface and programming"
+ *                refering Rev. 01 - 4 January 2010
+ *
+ *        Released under the MIT License: http://mbed.org/license/mit
+ *
+ *        revision 1.0  09-Mar-2010   1st release
+ *        revision 1.1  12-Mar-2010   chaged: to make possible to reserve flash area for user
+ *                                            it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
+ *
+ *     by Tedd OKANO http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/
+ *     modified by Suga (supported to LPC11U24)
+ */
+
+#ifndef        MBED_IAP
+#define        MBED_IAP
+
+#include    "mbed.h"
+
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+#define     USER_FLASH_AREA_START   FLASH_SECTOR_29
+#define     USER_FLASH_AREA_SIZE    (FLASH_SECTOR_SIZE_16_TO_29 * 1)
+#elif defined(TARGET_LPC11U24)
+#define     USER_FLASH_AREA_START   FLASH_SECTOR_7
+#define     USER_FLASH_AREA_SIZE    (FLASH_SECTOR_SIZE_0_TO_15 * 1)
+#endif
+
+/*
+ *  memory map information is available in next URL also.
+ *    http://mbed.org/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h
+ */
+ 
+/**    Table for start adress of sectors
+ *    
+ *        LPC1768 internal flash memory sector numbers and addresses
+ *
+ *        LPC1768 flash memory are and sector number/size
+ *        Table 568 "Sectors in a LPC17xx device", Section 5. "Sector numbers", usermanual
+ *
+ *        0x00000000 - 0x0007FFFF        flash (29 sectors)
+ *
+ *      Sector0:     0x00000000 - 0x00000FFF        4K
+ *      Sector1:     0x00001000 - 0x00001FFF        4K
+ *      Sector2:     0x00002000 - 0x00002FFF        4K
+ *      Sector3:     0x00003000 - 0x00003FFF        4K
+ *      Sector4:     0x00004000 - 0x00004FFF        4K
+ *      Sector5:     0x00005000 - 0x00005FFF        4K
+ *      Sector6:     0x00006000 - 0x00006FFF        4K
+ *      Sector7:     0x00007000 - 0x00007FFF        4K
+ *      Sector8:     0x00008000 - 0x00008FFF        4K
+ *      Sector9:     0x00009000 - 0x00009FFF        4K
+ *      Sector10:    0x0000A000 - 0x0000AFFF        4K
+ *      Sector11:    0x0000B000 - 0x0000BFFF        4K
+ *      Sector12:    0x0000C000 - 0x0000CFFF        4K
+ *      Sector13:    0x0000D000 - 0x0000DFFF        4K
+ *      Sector14:    0x0000E000 - 0x0000EFFF        4K
+ *      Sector15:    0x0000F000 - 0x0000FFFF        4K
+ *
+ *      Sector16:    0x00010000 - 0x00017FFF        32K
+ *      Sector17:    0x00018000 - 0x0001FFFF        32K
+ *      Sector18:    0x00020000 - 0x00027FFF        32K
+ *      Sector19:    0x00028000 - 0x0002FFFF        32K
+ *      Sector20:    0x00030000 - 0x00037FFF        32K
+ *      Sector21:    0x00038000 - 0x0003FFFF        32K
+ *      Sector22:    0x00040000 - 0x00047FFF        32K
+ *      Sector23:    0x00048000 - 0x0004FFFF        32K
+ *      Sector24:    0x00050000 - 0x00057FFF        32K
+ *      Sector25:    0x00058000 - 0x0005FFFF        32K
+ *      Sector26:    0x00060000 - 0x00067FFF        32K
+ *      Sector27:    0x00068000 - 0x0006FFFF        32K
+ *      Sector28:    0x00070000 - 0x00077FFF        32K
+ *      Sector29:    0x00078000 - 0x0007FFFF        32K
+ */
+ 
+#define     FLASH_SECTOR_0       0x00000000
+#define     FLASH_SECTOR_1       0x00001000
+#define     FLASH_SECTOR_2       0x00002000
+#define     FLASH_SECTOR_3       0x00003000
+#define     FLASH_SECTOR_4       0x00004000
+#define     FLASH_SECTOR_5       0x00005000
+#define     FLASH_SECTOR_6       0x00006000
+#define     FLASH_SECTOR_7       0x00007000
+#define     FLASH_SECTOR_8       0x00008000
+#define     FLASH_SECTOR_9       0x00009000
+#define     FLASH_SECTOR_10      0x0000A000
+#define     FLASH_SECTOR_11      0x0000B000
+#define     FLASH_SECTOR_12      0x0000C000
+#define     FLASH_SECTOR_13      0x0000D000
+#define     FLASH_SECTOR_14      0x0000E000
+#define     FLASH_SECTOR_15      0x0000F000
+#define     FLASH_SECTOR_16      0x00010000
+#define     FLASH_SECTOR_17      0x00018000
+#define     FLASH_SECTOR_18      0x00020000
+#define     FLASH_SECTOR_19      0x00028000
+#define     FLASH_SECTOR_20      0x00030000
+#define     FLASH_SECTOR_21      0x00038000
+#define     FLASH_SECTOR_22      0x00040000
+#define     FLASH_SECTOR_23      0x00048000
+#define     FLASH_SECTOR_24      0x00050000
+#define     FLASH_SECTOR_25      0x00058000
+#define     FLASH_SECTOR_26      0x00060000
+#define     FLASH_SECTOR_27      0x00068000
+#define     FLASH_SECTOR_28      0x00070000
+#define     FLASH_SECTOR_29      0x00078000
+
+#define     FLASH_SECTOR_SIZE_0_TO_15    ( 4 * 1024)
+#define     FLASH_SECTOR_SIZE_16_TO_29   (32 * 1024)
+
+static char * sector_start_adress[]    = {
+    (char *)FLASH_SECTOR_0,
+    (char *)FLASH_SECTOR_1,
+    (char *)FLASH_SECTOR_2,
+    (char *)FLASH_SECTOR_3,
+    (char *)FLASH_SECTOR_4,
+    (char *)FLASH_SECTOR_5,
+    (char *)FLASH_SECTOR_6,
+    (char *)FLASH_SECTOR_7,
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    (char *)FLASH_SECTOR_8,
+    (char *)FLASH_SECTOR_9,
+    (char *)FLASH_SECTOR_10,
+    (char *)FLASH_SECTOR_11,
+    (char *)FLASH_SECTOR_12,
+    (char *)FLASH_SECTOR_13,
+    (char *)FLASH_SECTOR_14,
+    (char *)FLASH_SECTOR_15,
+    (char *)FLASH_SECTOR_16,
+    (char *)FLASH_SECTOR_17,
+    (char *)FLASH_SECTOR_18,
+    (char *)FLASH_SECTOR_19,
+    (char *)FLASH_SECTOR_20,
+    (char *)FLASH_SECTOR_21,
+    (char *)FLASH_SECTOR_22,
+    (char *)FLASH_SECTOR_23,
+    (char *)FLASH_SECTOR_24,
+    (char *)FLASH_SECTOR_25,
+    (char *)FLASH_SECTOR_26,
+    (char *)FLASH_SECTOR_27,
+    (char *)FLASH_SECTOR_28,
+    (char *)FLASH_SECTOR_29    
+#endif
+};
+
+
+/**    Error code by IAP routine
+ *  
+ *        Table 588 "ISP Return Codes Summary", Section 7.15 "ISP Return Codes", usermanual
+ */
+
+enum error_code
+    {
+            CMD_SUCCESS,
+            INVALID_COMMAND,
+            SRC_ADDR_ERROR,
+            DST_ADDR_ERROR,
+            SRC_ADDR_NOT_MAPPED,
+            DST_ADDR_NOT_MAPPED,
+            COUNT_ERROR,
+            INVALID_SECTOR,
+            SECTOR_NOT_BLANK,
+            SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION,
+            COMPARE_ERROR,
+            BUSY,
+            PARAM_ERROR,
+            ADDR_ERROR,
+            ADDR_NOT_MAPPED,
+            CMD_LOCKED,
+            INVALID_CODE,
+            INVALID_BAUD_RATE,
+            INVALID_STOP_BIT,
+            CODE_READ_PROTECTION_ENABLED
+    };
+
+
+
+/*
+ *  IAP routine entry
+ *
+ *        Chapter 8. "IAP commands", usermanual
+ */
+
+#define     IAP_LOCATION    0x1fff1ff1
+typedef     void (*IAP_call)(unsigned int [], unsigned int []);
+
+/**    IAP class
+ *  
+ *        Interface for internal flash memory access
+ */
+
+
+class IAP {
+public:
+
+    /*
+     *  SystemCoreClock ??? :  
+     *    http://mbed.org/forum/mbed/topic/229/
+     *    http://mbed.org/users/simon/programs/SystemCoreClock/16mhsh/
+     */
+
+    
+    /**    Constructor for IAP
+     *
+     */
+
+    IAP() : iap_entry( reinterpret_cast<IAP_call>(IAP_LOCATION) ), cclk_kHz( SystemCoreClock / 1000 ) {}
+    int read_ID( void );
+    int read_serial( void );
+    int blank_check( int start, int end );
+    int erase( int start, int end );
+    int prepare( int start, int end );
+    int write( char *source_addr, char *target_addr, int size );
+    int compare( char *source_addr, char *target_addr, int size );
+    
+    char *reserved_flash_area_start( void );
+    int   reserved_flash_area_size( void );
+
+#if defined(TARGET_LPC11U24)
+    int write_eeprom( char *source_addr, char *target_addr, int size );
+    int read_eeprom( char *source_addr, char *target_addr, int size );
+#endif
+
+private:
+    IAP_call        iap_entry;
+    unsigned int    IAP_command[ 5 ];
+    unsigned int    IAP_result[ 5 ];
+    int             cclk_kHz;
+    
+    //int cpu_clock( void );
+}
+;
+
+#endif    //  #ifndef  MBED_IAP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#0bffc59d590b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD/TextLCD.cpp	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,159 @@
+/* mbed TextLCD Library, for a 4-bit LCD based on HD44780
+ * Copyright (c) 2007-2010, sford, http://mbed.org
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "TextLCD.h"
+#include "mbed.h"
+
+TextLCD::TextLCD(PinName rs, PinName e, PinName d0, PinName d1,
+                 PinName d2, PinName d3, LCDType type) : _rs(rs),
+        _e(e), _d(d0, d1, d2, d3),
+        _type(type) {
+
+    _e  = 1;
+    _rs = 0;            // command mode
+
+    wait(0.015);        // Wait 15ms to ensure powered up
+
+    // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
+    for (int i=0; i<3; i++) {
+        writeByte(0x3);
+        wait(0.00164);  // this command takes 1.64ms, so wait for it
+    }
+    writeByte(0x2);     // 4-bit mode
+    wait(0.000040f);    // most instructions take 40us
+
+    writeCommand(0x28); // Function set 001 BW N F - -
+    writeCommand(0x0C);
+    writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
+    cls();
+}
+
+void TextLCD::character(int column, int row, int c) {
+    int a = address(column, row);
+    writeCommand(a);
+    writeData(c);
+}
+
+void TextLCD::cls() {
+    writeCommand(0x01); // cls, and set cursor to 0
+    wait(0.00164f);     // This command takes 1.64 ms
+    locate(0, 0);
+}
+
+void TextLCD::locate(int column, int row) {
+    _column = column;
+    _row = row;
+}
+
+int TextLCD::_putc(int value) {
+    if (value == '\n') {
+        _column = 0;
+        _row++;
+        if (_row >= rows()) {
+            _row = 0;
+        }
+    } else {
+        character(_column, _row, value);
+        _column++;
+        if (_column >= columns()) {
+            _column = 0;
+            _row++;
+            if (_row >= rows()) {
+                _row = 0;
+            }
+        }
+    }
+    return value;
+}
+
+int TextLCD::_getc() {
+    return -1;
+}
+
+void TextLCD::writeByte(int value) {
+    _d = value >> 4;
+    wait(0.000040f); // most instructions take 40us
+    _e = 0;
+    wait(0.000040f);
+    _e = 1;
+    _d = value >> 0;
+    wait(0.000040f);
+    _e = 0;
+    wait(0.000040f);  // most instructions take 40us
+    _e = 1;
+}
+
+void TextLCD::writeCommand(int command) {
+    _rs = 0;
+    writeByte(command);
+}
+
+void TextLCD::writeData(int data) {
+    _rs = 1;
+    writeByte(data);
+}
+
+int TextLCD::address(int column, int row) {
+    switch (_type) {
+        case LCD20x4:
+            switch (row) {
+                case 0:
+                    return 0x80 + column;
+                case 1:
+                    return 0xc0 + column;
+                case 2:
+                    return 0x94 + column;
+                case 3:
+                    return 0xd4 + column;
+            }
+        case LCD16x2B:
+            return 0x80 + (row * 40) + column;
+        case LCD16x2:
+        case LCD20x2:
+        default:
+            return 0x80 + (row * 0x40) + column;
+    }
+}
+
+int TextLCD::columns() {
+    switch (_type) {
+        case LCD20x4:
+        case LCD20x2:
+            return 20;
+        case LCD16x2:
+        case LCD16x2B:
+        default:
+            return 16;
+    }
+}
+
+int TextLCD::rows() {
+    switch (_type) {
+        case LCD20x4:
+            return 4;
+        case LCD16x2:
+        case LCD16x2B:
+        case LCD20x2:
+        default:
+            return 2;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD/TextLCD.h	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,111 @@
+/* mbed TextLCD Library, for a 4-bit LCD based on HD44780
+ * Copyright (c) 2007-2010, sford, http://mbed.org
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_TEXTLCD_H
+#define MBED_TEXTLCD_H
+
+#include "mbed.h"
+
+/** A TextLCD interface for driving 4-bit HD44780-based LCDs
+ *
+ * Currently supports 16x2, 20x2 and 20x4 panels
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "TextLCD.h"
+ * 
+ * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d0-d3
+ * 
+ * int main() {
+ *     lcd.printf("Hello World!\n");
+ * }
+ * @endcode
+ */
+class TextLCD : public Stream {
+public:
+
+    /** LCD panel format */
+    enum LCDType {
+        LCD16x2     /**< 16x2 LCD panel (default) */
+        , LCD16x2B  /**< 16x2 LCD panel alternate addressing */
+        , LCD20x2   /**< 20x2 LCD panel */
+        , LCD20x4   /**< 20x4 LCD panel */
+    };
+
+    /** Create a TextLCD interface
+     *
+     * @param rs    Instruction/data control line
+     * @param e     Enable line (clock)
+     * @param d0-d3 Data lines
+     * @param type  Sets the panel size/addressing mode (default = LCD16x2)
+     */
+    TextLCD(PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, LCDType type = LCD16x2);
+
+#if DOXYGEN_ONLY
+    /** Write a character to the LCD
+     *
+     * @param c The character to write to the display
+     */
+    int putc(int c);
+
+    /** Write a formated string to the LCD
+     *
+     * @param format A printf-style format string, followed by the
+     *               variables to use in formating the string.
+     */
+    int printf(const char* format, ...);
+#endif
+
+    /** Locate to a screen column and row
+     *
+     * @param column  The horizontal position from the left, indexed from 0
+     * @param row     The vertical position from the top, indexed from 0
+     */
+    void locate(int column, int row);
+
+    /** Clear the screen and locate to 0,0 */
+    void cls();
+
+    int rows();
+    int columns();
+
+protected:
+
+    // Stream implementation functions
+    virtual int _putc(int value);
+    virtual int _getc();
+
+    int address(int column, int row);
+    void character(int column, int row, int c);
+    void writeByte(int value);
+    void writeCommand(int command);
+    void writeData(int data);
+
+    DigitalOut _rs, _e;
+    BusOut _d;
+    LCDType _type;
+
+    int _column;
+    int _row;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,156 @@
+
+
+#include "mbed.h"
+#include    "IAP.h"
+#include "TextLCD.h"
+#include "stdlib.h"
+#include <stdio.h>
+
+#define     FLOAT_SIZE        4
+#define     TARGET_ADDRESS   64
+#define     NEXT_PLACE 1
+#define     PLACE   0
+
+IAP     iap;
+float getEepromFloat(int place);
+void teletype(char* text, int row);
+
+typedef char * string;
+
+// ====== Pins =========
+
+DigitalOut gLED(P1_18);
+DigitalOut rLED(P1_24);
+
+
+TextLCD lcd(P1_22, P1_14, P0_9, P0_8, P1_21, P1_2); // rs, e, d4-d7
+
+
+//Tie unused analogue pins LOW
+
+DigitalOut a1(P0_15);
+DigitalOut a2(P0_16);
+DigitalOut a3(P0_22);
+DigitalOut a4(P0_23);
+
+// ====== Variables =========
+
+float test_float = 12.5;
+float display_float = 0.0;
+
+int twodp =0;
+
+float _ON;
+float _OFF;
+
+float calib;
+float voltage;
+float displayVoltage;
+float displaySet;
+
+float difference = 0;
+float calibrate=0.0;
+
+
+
+
+// ====== CODE BEGINS=========
+
+typedef union _data {
+    float f;
+    char  s[4];
+} myData;
+
+
+float getEepromFloat(int place)
+{
+    myData returnedFloat;
+    char someBytes[4];
+
+    iap.read_eeprom( (char*)(TARGET_ADDRESS+(place*4)), someBytes, FLOAT_SIZE );
+
+    returnedFloat.s[0] = someBytes[0];
+    returnedFloat.s[1] = someBytes[1];
+    returnedFloat.s[2] = someBytes[2];
+    returnedFloat.s[3] = someBytes[3];
+    return returnedFloat.f;
+
+}
+
+void setEepromFloat(int place, float incomingFloat)
+{
+    char theBytes[4];
+    myData aFloat;
+
+    aFloat.f = incomingFloat;
+
+    theBytes[0] = aFloat.s[0];
+    theBytes[1] = aFloat.s[1];
+    theBytes[2] = aFloat.s[2];
+    theBytes[3] = aFloat.s[3];
+
+    iap.write_eeprom( theBytes, (char*) (TARGET_ADDRESS+(place*4)), FLOAT_SIZE );
+
+}
+
+
+int main()
+{
+
+    teletype("Hello", 0);
+    float aValue=0.0;
+    float theReturned=99.99;
+    
+    while (1) {
+                wait(2.0);
+                
+                aValue++;     // increment it
+                
+                setEepromFloat(PLACE,aValue);         //put it in place 0 (Global variable)
+                        
+                theReturned = getEepromFloat(PLACE); //get it back
+                
+                lcd.cls();
+                teletype("Value: %f", theReturned);  //output it to the screen
+                
+
+    };
+}
+
+
+
+
+char* left(string Source,int NewLen)
+{
+
+    char* result;
+    char* temp;
+    if (NewLen <=0)
+        NewLen =1; /* Minimum length */
+
+    result = (char*)malloc(NewLen + 1); /* +1 = DFTTZ! */
+    *result=' ';   /* default for unassigned strings */
+    temp=result;
+    if (Source && *Source) { /* don't copy an empty string */
+        while (NewLen-- >0)
+            *temp++=*Source++;
+    } else temp++;
+    *temp='\0';
+    return result;
+}
+
+void teletype(char* text, int row)
+{
+    lcd.locate(0,row);
+    lcd.printf("                ");
+
+    int whole=strlen(text);
+
+    for (int i=1; i<=whole; i++) {
+        lcd.locate(0,row);
+        lcd.printf(left(text,i));
+        wait(0.15);
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Sep 26 09:22:57 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06
\ No newline at end of file