Add to 11U68 11E68

Dependencies:   DirectoryList MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

main.cpp

Committer:
okano
Date:
2013-09-20
Revision:
29:96e28bc1bd99
Parent:
28:689c3880e0e4
Child:
30:e0d7524661ca

File content as of revision 29:96e28bc1bd99:

/**
 *  Sample of ISP operation for NXP MCUs
 *
 *  @author  Tedd OKANO
 *  @version 0.9
 *  @date    Sep-2013
 *
 *  This program programs MCU flash memory through UART. It uses
 *  "In-System Programming (ISP)" interface in target MCU (NXP LPC micro-
 *  controllers).
 *
 *  The ISP is done by PC and serial cable normally. The ISP protocol is
 *  executed software on a PC. The software reads a data file and transfers
 *  the data with the ISP protocol.
 *  This program does same process of that. The mbed perform the function like
 *  "FlashMagic" and "lpc21isp".
 *  (This program not just copies the binary but also insert 4 byte checksum at
 *  address 0x1C.)
 *
 *  This program currently supports LPC1114(LPC1114FN28/102 - DIP28-ARM) and
 *  LPC810(LPC810M021FN8 - DIP8-ARM).
 */

#include    "mbed.h"
#include    "target_table.h"
#include    "serial_utilities.h"
#include    "command_interface.h"
#include    "writing.h"
#include    "uu_coding.h"
#include    "target_handling.h"
#include    "verification.h"
#include    "_user_settings.h"
#include    "ika.h"
#include    "error_code.h"

BusOut          leds( LED4, LED3, LED2, LED1 );
LocalFileSystem local( "local" );
Ticker          success;

int     error_state         = 0;

int     isp_flash_write( char *file_name );
int     file_size( FILE *fp );
char    read_byte( void );
void    success_indicator();


int main()
{
    int     err;
    
    err     = isp_flash_write( SOURCE_FILE );
    
    printf( "\r\n  %s\r\n\r\n",
            err ?
            "** The data could not be written :(" :
            "** The data has been written successflly :)"
          );

    if ( err )
        error( "  ** ISP failed\r\n" );

#ifdef  AUTO_PROGRAM_START
    set_target_baud_rate( TARGET_OPERATION_BAUD_RATE );

    reset_target( NO_ISP_MODE );
    printf( "  ** The program in flash has been started!!\r\n" );
#endif

    printf( "     (now the mbed is working in \"serial through mode\")\r\n\r\n" );

    success.attach( &success_indicator, 0.1 );

    usb_serial_bridge_operation();  //  doesn't return. infinite loop in this function
}


int isp_flash_write( char *file_name )
{
    FILE            *fp;
    target_param    *tpp;
    int             data_size;
    int             last_sector;
    int             transferred_size;
    int             err;

    if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) {
        return ( ERROR_AT_TARGET_OPEN );
    }

    printf( "  target device found : type       = \"%s\"\r\n",     tpp->type_name );
    printf( "                        ID         = 0x%08X\r\n",     tpp->id );
    printf( "                        RAM size   = %10d bytes\r\n", tpp->ram_size );
    printf( "                        flash size = %10d bytes\r\n", tpp->flash_size );

    printf( "  opening file: \"%s\"\r\n", file_name );

    if ( NULL == (fp    = fopen( file_name, "rb" )) ) {
        return ( ERROR_AT_FILE_OPEN );
    }

    data_size   = file_size( fp );
    last_sector = data_size / tpp->sector_size;

    printf( "  data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 );
    printf( "  resetting target\r\n" );

    if ( erase_sectors( last_sector ) )
        return ( ERROR_AT_SECTOR_ERASE );

    printf( "\r\n  ==== flash writing ====\r\n" );
    
    if ( err    = write_flash( fp, tpp, &transferred_size ) )
        return ( err );

    printf( "  -- %d bytes data are written\r\n", transferred_size );
        
    
    printf( "\r\n  ==== flash reading and verifying ====\r\n" );
    
    if ( err    = verify_flash( fp, tpp, &transferred_size ) )
        return ( err );

    printf( "  -- %d bytes data are read and verified\r\n", transferred_size );

    fclose( fp );

    post_writing_process( tpp );
    
    return ( 0 );
}



int file_size( FILE *fp )
{
    int     size;

    fseek( fp, 0, SEEK_END ); // seek to end of file
    size    = ftell( fp );       // get current file pointer
    fseek( fp, 0, SEEK_SET ); // seek back to beginning of file

    return size;
}


void success_indicator()
{
    static int  i   = 0;

    leds    = 0x1 << (i++ & 0x3);
}


void toggle_led( char v )
{
    leds    = leds ^ (0x1 << v);
}