Add to 11U68 11E68

Dependencies:   DirectoryList MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

command_interface.cpp

Committer:
okano
Date:
2013-09-20
Revision:
30:e0d7524661ca
Parent:
29:96e28bc1bd99
Child:
35:0b434ef4af49

File content as of revision 30:e0d7524661ca:

#include    "mbed.h"
#include    "command_interface.h"
#include    "serial_utilities.h"
#include    "_user_settings.h"


int try_and_check( char *command, char *expected_return_str, int mode )
{
    char    rtn_str[ STR_BUFF_SIZE ];
    int     result  = 1;

    print_command( command );
    put_string( command );

    get_string( rtn_str );
    print_result( result  = strcmp( expected_return_str, rtn_str ) );

    if ( result && !mode )
        error( "command failed\r\n" );

    return ( result );
}


int try_and_check2( char *command, char *expected_return_str, int mode )
{
    char    rtn_str[ STR_BUFF_SIZE ];
    int     result  = 1;

    print_command( command );
    put_string( command );

    get_string( rtn_str );  // just readout echoback
    get_string( rtn_str );
    print_result( result  = strcmp( expected_return_str, rtn_str ) );

    if ( result && !mode )
        error( "command failed\r\n" );

    return ( result );
}


void print_command( char *command )
{
    char    s[ STR_BUFF_SIZE ];
    char    *pos;

    strcpy( s, command );

    if ( pos    = strchr( s, '\r' ) )
        *pos    = '\0';

    if ( pos    = strchr( s, '\n' ) )
        *pos    = '\0';

    printf( "  command-\"%s\" : ", s );
}


void print_result( int r )
{
    printf( "%s\r\n", r ? "Fail" : "Pass" );
}


int send_RAM_transfer_checksum( int checksum )
{
    char    command[ 16 ];

    sprintf( command, "%d\n", checksum );
    
    return ( try_and_check( command, "OK", 0 ) );
}


int erase_sectors( int last_sector )
{
    char    command_str[ STR_BUFF_SIZE ];

    sprintf( command_str, "P 0 %d\r\n", last_sector );
    if ( try_and_check( command_str, "0", 0 ) )
        return ( 1 );

    *(command_str)  = 'E';
    return ( try_and_check( command_str, "0", 0 ) );
}