Ika Shouyu Poppoyaki - LPC82x supported

Dependencies:   MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

main.cpp

Committer:
okano
Date:
2013-09-19
Revision:
27:2b5c1eb39bb5
Parent:
26:a63e73885b21
Child:
28:689c3880e0e4

File content as of revision 27:2b5c1eb39bb5:

/**
 *  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"

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

int     error_state         = 0;

int     post_writing_process( target_param *tpp );
int     file_size( FILE *fp );
char    read_byte( void );
void    success_indicator();


int main()
{
    FILE            *fp;
    target_param    *tpp;
    int             data_size;
    int             last_sector;

    printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" );

    if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) {
        error( "couldn't open the taget" );
        return ( 1 );
    }

    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", SOURCE_FILE );

    if ( NULL == (fp    = fopen( SOURCE_FILE, "rb" )) ) {
        error( "couldn't open source file" );
        return ( 1 );
    }

    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" );

    erase_sectors( last_sector );

    printf( "\r\n  ==== flash writing ====\r\n" );
    write_flash( fp, tpp );
    
    printf( "\r\n  ==== flash reading and verifying ====\r\n" );
    verify_flash( fp, tpp );

    fclose( fp );

    printf( "\r\n  %s\r\n\r\n",
            error_state ?
            "** The data could not be written :(" :
            "** The data has been written successflly :)"
          );

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

    post_writing_process( tpp );


#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 post_writing_process( target_param *tpp )
{
    if ( tpp->write_type == UUENCODE )
        return ( try_and_check( "G 0 T\r\n", "0", 0 ) );
    else
        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 set_leds( char v )
{
    leds    = v;
}