Code to load a LPC1114 over tx/rx. I have only tested with a 1114 chip but it should work with other LPC uControllers

Dependencies:   DirectoryList MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Files at this revision

API Documentation at this revision

Comitter:
okano
Date:
Thu Jan 29 10:48:57 2015 +0000
Parent:
47:e7d395119a63
Child:
49:380e7be429ef
Commit message:
file selector interface implemented

Changed in this revision

DirectoryList.lib Show annotated file Show diff for this revision Revisions of this file
_user_settings.h Show annotated file Show diff for this revision Revisions of this file
dir_handling/dir_handling.cpp Show annotated file Show diff for this revision Revisions of this file
dir_handling/dir_handling.h Show annotated file Show diff for this revision Revisions of this file
isp.cpp Show annotated file Show diff for this revision Revisions of this file
isp.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
serial_utilities.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DirectoryList.lib	Thu Jan 29 10:48:57 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okano/code/DirectoryList/#3cd1685a4c22
--- a/_user_settings.h	Tue Jan 06 11:05:29 2015 +0000
+++ b/_user_settings.h	Thu Jan 29 10:48:57 2015 +0000
@@ -4,6 +4,7 @@
 ///  file name for the binary data
 //  edit here if you need to use different file name or storage media
 #define     SOURCE_FILE         "/local/bin"
+#define     SUFFIX_FILTER_STR   "*"
 
 
 //  "ISP_BAUD_RATE" is baud rate for ISP operation
@@ -42,6 +43,10 @@
 #define     CHECK_CRP_CODE
 
 
+//  enable "ENABLE_VERIFYING" to let perform verification by comparing "bin" file and flash read data.
+#define     ENABLE_FILE_SELECTOR
+
+
 #endif  //  MBED_ISP___USER_SETTINGS__
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dir_handling/dir_handling.cpp	Thu Jan 29 10:48:57 2015 +0000
@@ -0,0 +1,201 @@
+#include    "mbed.h"
+#include    "dir_handling.h"
+#include    "_user_settings.h"
+#include    "DirectoryList.h"
+#include    "isp.h"
+
+#include    <iostream>
+#include    <string>
+#include    <cctype>
+#include    <algorithm>
+
+extern Serial      pc;
+extern BusOut      leds;
+
+void show_file_list( std::vector<std::string> list );
+void user_action_waiting_indicator();
+
+
+std::string get_file_name( std::string &default_path, const char *suffix )
+{
+    Ticker  led_blink;
+    int     last_slash;
+
+    last_slash  = (int)default_path.rfind( "/" );
+
+    if ( last_slash == std::string::npos ) {
+        default_path.clear();
+        return ( default_path );
+    }
+
+    string file_name( default_path.c_str() + last_slash + 1 );
+    string path_name( default_path.c_str(), default_path.c_str() + last_slash );
+
+    //printf( "path_name : %s\r\n", path_name.c_str() );
+    //printf( "file_name : %s\r\n", file_name.c_str() );
+
+    file_name.push_back( '.' );
+    std::transform( file_name.begin(), file_name.end(), file_name.begin(), toupper );
+
+    DirectoryList               dir( path_name );
+    std::vector<std::string>    list;
+
+    for ( int i = 0; i < dir.size(); i++ ) {
+        if ( dir[ i ] == file_name ) {
+            return ( default_path );
+        }
+
+        if ( *suffix == '*' ) {
+            list    = dir;
+        } else {
+            //printf( "-- [%s][%d]\r\n", dir[ i ].c_str(), dir[ i ].find( ".BIN" ) );
+            //if ( (8 == dir[ i ].find( ".BIN" )) && (0 != dir[ i ].find( "IKA" )) ) {
+            if ( (9 == dir[ i ].find( suffix )) ) {
+                list.push_back( dir[ i ] );
+            }
+        }
+    }
+
+    led_blink.attach( &user_action_waiting_indicator, 0.05 );
+
+    printf( "\r\n  WARNING : No file which has name of \"%s\" was found.\r\n", default_path.c_str() );
+    printf( "  now showing \"%s\" directory to select a source file.\r\n\r\n", path_name.c_str() );
+
+    if ( !list.size() ) {
+        printf( "  WARNING : No file which has suffix of \"%s\" was found.\r\n", suffix );
+        default_path.clear();
+        return ( default_path );
+    }
+
+#define FORMAT_FOR_SELECTED_FILE_NAME   "    selected file : [%3d] : \"%-12s\"            \r"
+
+    //printf( "  ==== directory by \"%s/*.%s\" (excluding \"%s/ika*.bin\") ====\r\n", path_name.c_str(), suffix, path_name.c_str() );
+    printf( "  ==== directory by \"%s/*.%s\", %d files ====\r\n", path_name.c_str(), suffix, list.size() );
+
+    show_file_list( list );
+
+    int     done    = false;
+    int     index   = 0;
+    int     c;
+
+    printf( FORMAT_FOR_SELECTED_FILE_NAME, index, list[ index ].c_str() );
+    fflush( stdout );
+
+    while ( !done ) {
+        if ( pc.readable() ) {
+            switch ( c  = pc.getc() ) {
+                case '\r' :
+                case '\n' :
+                    done    = true;
+                    break;
+
+                case 'e' :
+                case 'E' :
+                    printf( "\r\n  Erasing the flash memory\r\n" );
+                    default_path    = "\xFF";
+                    return ( default_path );
+                    //break;
+
+                case 'a' :
+                case 'A' :
+                case 'c' :
+                case 'C' :
+                    printf( "\r\n  Aborted to cancel the ISP execution\r\n" );
+                    default_path.clear();
+                    return ( default_path );
+                    //break;
+
+                case '0' :
+                case '1' :
+                case '2' :
+                case '3' :
+                case '4' :
+                case '5' :
+                case '6' :
+                case '7' :
+                case '8' :
+                case '9' :
+                    index   = c - '0';
+                    index   = ( (list.size() - 1) < index ) ? (list.size() - 1) : index;
+                    break;
+                case '?' :
+                    show_file_list( list );
+                    break;
+
+
+                case 0x1B :
+                    if ( 0x5B == pc.getc() )
+                        //  arrow key pressed
+                        switch ( c  = pc.getc() ) {
+                            case 0x41 :
+                            case 0x43 :
+                                index   += ( index == (list.size() - 1) ) ? 0 : 1;
+                                break;
+                            case 0x42 :
+                            case 0x44 :
+                                index   -= ( index == 0 ) ? 0 : 1;
+                                break;
+                            default :
+                                break;
+                        }
+            }
+
+            printf( FORMAT_FOR_SELECTED_FILE_NAME, index, list[ index ].c_str() );
+            fflush( stdout );
+        }
+    }
+
+    default_path.clear();
+    default_path    = path_name + "/" + list[ index ];
+
+    printf( "\r\n    flash writing source file : \"%s\"\r\n\r\n", default_path.c_str() );
+
+    return ( default_path );
+}
+
+void show_file_list( std::vector<std::string> list )
+{
+    int width;
+    int height;
+    int d_i;
+
+    width   = (list.size() / 5) + 1;
+    width   = (4 < width) ? 4 : width;
+    height  = (list.size() + (width - 1)) / width;
+
+    for ( int i = 0; i < height; i++ ) {
+        for ( int j = 0; j < width; j++ ) {
+
+            d_i     = i + j * height;
+
+            if ( list.size() <= d_i )
+                break;
+
+            printf( "  %3d : %-12s", d_i, list[ d_i ].c_str() );
+        }
+        printf( "\r\n" );
+    }
+    printf( "\r\n" );
+    printf( "  ** Use arrow key (up/down or left/right) to select a file\r\n" );
+    printf( "  ** or ..\r\n" );
+    printf( "  **   press [a] key for abort and go to serial through mode\r\n" );
+    printf( "  **   press [e] key for erasing flash\r\n" );
+    printf( "  **   press [?] key to display this message again\r\n\r\n" );
+}
+
+
+void user_action_waiting_indicator()
+{
+    static int  i   = 0;
+
+    if ( (i == 0) || (i == 3) )
+        leds    = 0xF;
+    else
+        leds    = 0x0;
+
+    i++;
+    i   &= 0xF;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dir_handling/dir_handling.h	Thu Jan 29 10:48:57 2015 +0000
@@ -0,0 +1,4 @@
+#include    <string>
+
+//std::string get_file_name( std::string &defaultpath );
+std::string get_file_name( std::string &default_path, const char *suffix );
--- a/isp.cpp	Tue Jan 06 11:05:29 2015 +0000
+++ b/isp.cpp	Thu Jan 29 10:48:57 2015 +0000
@@ -20,9 +20,10 @@
 unsigned int    read_crp( FILE *fp );
 unsigned int    crp_check( FILE *fp );
 void            success_indicator();
+void            user_action_waiting_indicator();
 
 
-int isp_flash_write( char *file_name )
+int isp_flash_write( const char *file_name )
 {
     FILE            *fp;
     target_param    *tpp;
@@ -40,6 +41,29 @@
     printf( "                        RAM size   =  %10d bytes\r\n", tpp->ram_size );
     printf( "                        flash size =  %10d bytes\r\n", tpp->flash_size );
 
+    //
+    //  If user selected "flash erase only"
+    //
+
+    if ( *file_name == 0xFF ) {
+        last_sector = find_sector( tpp->flash_size - 1, tpp );
+        
+        //  The file selector returns 0xFF as first character of the string
+        //      if user selected "flash erase".
+
+        printf( "\r\n  ==== flash erasing ====\r\n" );
+        
+        if ( erase_sectors( last_sector ) )
+            return ( ERROR_AT_SECTOR_ERASE );
+
+        printf( "  sectors from 0 to %d are erased. bye\r\n", last_sector );
+        exit ( 0 ); //  quit from app
+    }
+
+    //
+    //  Normal operation
+    //
+
     printf( "  opening file: \"%s\"\r\n", file_name );
 
     if ( NULL == (fp    = fopen( file_name, "rb" )) ) {
@@ -118,7 +142,7 @@
 
     post_writing_process( tpp );
 
-    return ( 0 );
+    return ( NO_ERROR );
 }
 
 
@@ -193,7 +217,6 @@
     leds    = 0x1 << (i++ & 0x3);
 }
 
-
 void toggle_led( char v )
 {
     leds    = leds ^ (0x1 << v);
--- a/isp.h	Tue Jan 06 11:05:29 2015 +0000
+++ b/isp.h	Thu Jan 29 10:48:57 2015 +0000
@@ -2,7 +2,7 @@
 #define MBED_ISP__ISP__
 
 
-int     isp_flash_write( char *file_name );
+int     isp_flash_write( const char *file_name );
 void    start_target_in_normal_mode( int baud_rate );
 void    start_success_indicator( void );
 void    toggle_led( char v );
--- a/main.cpp	Tue Jan 06 11:05:29 2015 +0000
+++ b/main.cpp	Thu Jan 29 10:48:57 2015 +0000
@@ -2,7 +2,7 @@
  *  Sample of ISP operation for NXP MCUs
  *
  *  @author  Tedd OKANO
- *  @version 1.1.1
+ *  @version 1.1.2
  *  @date    Jan-2015
  *
  *  This program programs MCU flash memory through UART. It uses
@@ -24,6 +24,7 @@
 #include    "isp.h"
 #include    "serial_utilities.h"
 #include    "_user_settings.h"
+#include    "dir_handling.h"
 
 
 LocalFileSystem local( "local" );
@@ -36,9 +37,16 @@
 {
     int     err;
 
-    printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed (v1.1.1)\r\n" );
+    printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed (v1.1.2)\r\n" );
 
+#ifndef ENABLE_FILE_SELECTOR
     err     = isp_flash_write( SOURCE_FILE );
+#else
+    std::string str( SOURCE_FILE );
+    if ( "" == get_file_name( str, SUFFIX_FILTER_STR ) )
+        goto skip_ISP;
+    err     = isp_flash_write( str.c_str() );
+#endif
 
     printf( "\r\n  %d: %s\r\n\r\n",err,
             err ?
@@ -49,6 +57,8 @@
     if ( err )
         error( "  ** ISP failed\r\n" );
 
+skip_ISP:
+
 #ifdef  AUTO_PROGRAM_START
 
     start_target_in_normal_mode( TARGET_OPERATION_BAUD_RATE );
@@ -63,4 +73,3 @@
     usb_serial_bridge_operation();  //  doesn't return. infinite loop in this function
 }
 
-
--- a/serial_utilities.cpp	Tue Jan 06 11:05:29 2015 +0000
+++ b/serial_utilities.cpp	Thu Jan 29 10:48:57 2015 +0000
@@ -10,7 +10,8 @@
 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 512
 #include "MODSERIAL.h"
-MODSERIAL       target( p28, p27 ); //
+MODSERIAL       target( p28, p27 );   //
+//MODSERIAL       target(  p9, p10 ); //    if you need to change the UART pin, change here
 #endif
 
 Timeout timeout;