dynamic load and run users binary file. self write the flash memory.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Tue Dec 27 16:55:24 2011 +0000
Parent:
0:f8cf4fdf6ab4
Child:
2:c56b5e682f0c
Commit message:

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
handler.cpp 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
--- a/IAP.cpp	Mon Dec 26 15:42:50 2011 +0000
+++ b/IAP.cpp	Tue Dec 27 16:55:24 2011 +0000
@@ -13,7 +13,7 @@
  *        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/programs/IAP_internal_flash_write/latest
+ *     by Tedd OKANO http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/
  */
 
 #include    "mbed.h"
--- a/IAP.h	Mon Dec 26 15:42:50 2011 +0000
+++ b/IAP.h	Tue Dec 27 16:55:24 2011 +0000
@@ -13,7 +13,7 @@
  *        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/programs/IAP_internal_flash_write/latest
+ *     by Tedd OKANO http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/
  */
 
 #ifndef        MBED_IAP
@@ -21,8 +21,8 @@
 
 #include    "mbed.h"
 
-#define     USER_FLASH_AREA_START   FLASH_SECTOR_29
-#define     USER_FLASH_AREA_SIZE    (FLASH_SECTOR_SIZE_16_TO_29 * 1)
+#define     USER_FLASH_AREA_START   FLASH_SECTOR_22
+#define     USER_FLASH_AREA_SIZE    (FLASH_SECTOR_SIZE_16_TO_29 * 8)
 
 /*
  *  memory map information is available in next URL also.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/handler.cpp	Tue Dec 27 16:55:24 2011 +0000
@@ -0,0 +1,143 @@
+#include "mbed.h"
+
+void jump (int vect);
+
+extern "C" {
+
+void SysTick_Handler() {
+    jump(15);
+}
+
+void WDT_IRQHandler () {
+    jump(16);
+}
+
+void TIMER0_IRQHandler() {
+    jump(17);
+}
+
+void TIMER1_IRQHandler() {
+    jump(18);
+}
+
+void TIMER2_IRQHandler() {
+    jump(19);
+}
+
+void TIMER3_IRQHandler() {
+    jump(20);
+}
+
+void UART0_IRQHandler() {
+    jump(21);
+}
+
+void UART1_IRQHandler() {
+    jump(22);
+}
+
+void UART2_IRQHandler() {
+    jump(23);
+}
+
+void UART3_IRQHandler() {
+    jump(24);
+}
+
+void PWM1_IRQHandler() {
+    jump(25);
+}
+
+void I2C0_IRQHandler() {
+    jump(26);
+}
+
+void I2C1_IRQHandler() {
+    jump(27);
+}
+
+void I2C2_IRQHandler() {
+    jump(28);
+}
+
+void SPI_IRQHandler() {
+    jump(29);
+}
+
+void SSP0_IRQHandler() {
+    jump(30);
+}
+
+void SSP1_IRQHandler() {
+    jump(31);
+}
+
+void PLL0_IRQHandler() {
+    jump(32);
+}
+
+void RTC_IRQHandler() {
+    jump(33);
+}
+
+void EINT0_IRQHandler() {
+    jump(34);
+}
+
+void EINT1_IRQHandler() {
+    jump(35);
+}
+
+void EINT2_IRQHandler() {
+    jump(36);
+}
+
+void EINT3_IRQHandler() {
+    jump(37);
+}
+
+void ADC_IRQHandler() {
+    jump(38);
+}
+
+void BOD_IRQHandler() {
+    jump(39);
+}
+
+void USB_IRQHandler() {
+    jump(40);
+}
+
+void CAN_IRQHandler() {
+    jump(41);
+}
+
+void DMA_IRQHandler() {
+    jump(42);
+}
+
+void I2S_IRQHandler() {
+    jump(43);
+}
+
+void ENET_IRQHandler() {
+    jump(44);
+}
+
+void RIT_IRQHandler() {
+    jump(45);
+}
+
+void MCPWM_IRQHandler() {
+    jump(46);
+}
+
+void QEI_IRQHandler() {
+    jump(47);
+}
+
+void PLL1_IRQHandler() {
+    jump(48);
+}
+
+} // extern "C"
--- a/main.cpp	Mon Dec 26 15:42:50 2011 +0000
+++ b/main.cpp	Tue Dec 27 16:55:24 2011 +0000
@@ -3,38 +3,36 @@
     binary -> Flash 0x00078000 & run
 
   lpc17xx.ld
-        IROM (rx) : ORIGIN = 0x00078000, LENGTH = 32k
+        IROM (rx) : ORIGIN = 0x00040000, LENGTH = 256k
         IRAM0 (rwx) : ORIGIN = 0x10002000, LENGTH = 24k
 
   startup_LPC17xx.c
         //void Reset_Handler(void) __attribute__((__interrupt__));
         void Reset_Handler(void);
+        //    SystemInit();
 
   compiled.bin --> test.dat
 */
-#include    "mbed.h"
-#include    "IAP.h"
+#include "mbed.h"
+#include "IAP.h"
 #include <new>
 
-#define     MEM_SIZE        256
-#define     TARGET_SECTOR    29
+#define BUF_SIZE        512
+#define TARGET_SECTOR    22
+#define TARGET_SECTOR_NUM 8
 
 LocalFileSystem local("local");
 
 void    memdump( char *p, int n );
-int     isprint( int c );
 
 IAP     iap;
 
 extern "C"
-void Default_Handler() {
-    printf("Default Handler!\n");
-    exit(-1);
-}
-
-extern "C"
 void HardFault_Handler() {
-    printf("Hard Fault!\n");
+    register unsigned int _msp __asm("msp");
+    printf("Hard Fault! %x (%x)\r\n", SCB->HFSR, *((unsigned int *)(_msp + 24)));
+    printf(" - %x\r\n", (*(volatile uint32_t*)0xe000ed24));
+//    printf("Hard Fault! %x\r\n", SCB->HFSR);
     exit(-1);
 }
 
@@ -43,62 +41,88 @@
     exit(-1);
 }
 
-int jump (int vect) {
+void jump (int vect) {
     void (*func)();
     unsigned int *p;
 
-    p = (unsigned int *)(sector_start_adress[ TARGET_SECTOR ] + 4 * vect);
+    p = (unsigned int *)(sector_start_adress[TARGET_SECTOR] + 4 * vect);
     func = (void (*)())*p;
-    printf( "func %08x (%d)\r\n", (unsigned int)func, vect);
-    func();
-    return 0;
+//    printf("jump vector %d (%08x)\r\n", vect, func);
+    if ((char*)*p >= sector_start_adress[TARGET_SECTOR] && (char*)*p < sector_start_adress[TARGET_SECTOR] + FLASH_SECTOR_SIZE_16_TO_29 * TARGET_SECTOR_NUM) {
+        func();
+    }
 }
 
-void SysTick_Handler(void) {
-    jump(15);
+int loadbinary (char *filename) {
+    int i, r, num, sector, size = 0;
+    FILE *fp;
+    char buf[BUF_SIZE];
+    
+    fp = fopen(filename, "r");
+    if (! fp) return -1;
+
+    for (sector = TARGET_SECTOR; sector < TARGET_SECTOR + TARGET_SECTOR_NUM; sector ++) {
+        if (iap.blank_check(sector, sector) == SECTOR_NOT_BLANK) {
+            iap.prepare(sector, sector);
+            r = iap.erase(sector, sector);
+            if (r) {
+                printf("iap.erase (%d) %d %x\r\n", r, sector, i);
+                goto error;
+            }
+            printf("iap erase %d\r\n", sector);
+        }
+
+        for (i = 0; i < FLASH_SECTOR_SIZE_16_TO_29; i += BUF_SIZE) {
+            num = fread(buf, sizeof(char), BUF_SIZE, fp);
+            if (num <= 0) {
+                // EOF
+                goto exit;
+            }
+            
+            // write Flash
+            r = iap.prepare(sector, sector);
+            if (r) {
+                printf("iap.prepare (%d) %d %x\r\n", r, sector, i);
+                goto error;
+            }
+            r = iap.write(buf, sector_start_adress[sector] + i, BUF_SIZE);
+            if (r) {
+                printf("iap.write (%d) %d %x\r\n", r, sector, i);
+                goto error;
+            }
+            r = iap.compare(buf, sector_start_adress[sector] + i, BUF_SIZE);
+            if (r) {
+                printf("iap.compare (%d) %d %x\r\n", r, sector, i);
+                goto error;
+            }
+            size += num;
+        }
+    }
+
+exit:
+    printf("write %d bytes, end sector %d\r\n", size, sector);
+    fclose(fp);
+    return 0;
+
+error:
+    printf("write error\r\n");
+    fclose(fp);
+    return r;
 }
 
 int main() {
-    FILE *fp;
-    int i, r, s;
-    char buf[MEM_SIZE];
     char *heap;
 
     set_new_handler(no_memory); // new handler function
 
     printf( "device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\r\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 );
-    printf( "user reserved flash area: start_address=0x%08X, size=%d bytes\r\n", iap.reserved_flash_area_start(), iap.reserved_flash_area_size() );
 
-    r   = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR );
-    printf( "blank check result = 0x%08X\r\n", r );
-
-    //  erase sector, if required
-    
-    if ( r == SECTOR_NOT_BLANK ) {
-        iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
-        r = iap.erase( TARGET_SECTOR, TARGET_SECTOR );
-        printf( "erase result       = 0x%08X\r\n", r );
-    }
+    loadbinary("/local/test.dat");
 
-    // copy File to Flash
-    fp = fopen("/local/test.dat", "r");
-    if (fp) {
-        iap.prepare( TARGET_SECTOR, TARGET_SECTOR );
-        s = 0;
-        for (i = 0;; i ++) {
-            r = fread(buf, sizeof(char), MEM_SIZE, fp);
-            if (r <= 0) break;
-            s = s + r;
-            r = iap.write(buf, sector_start_adress[TARGET_SECTOR] + MEM_SIZE * i, MEM_SIZE);
-            printf( "copied: File(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\n", buf, sector_start_adress[ TARGET_SECTOR ] + MEM_SIZE * i, MEM_SIZE, r );
-        }
-        fclose(fp);
-        printf("writable %d bytes. (%d)\r\n", s, i);
-    }
+//    memdump( sector_start_adress[ TARGET_SECTOR ], 48 * 4);
 
-    memdump( sector_start_adress[ TARGET_SECTOR ], 16);
-
-    heap = (char*)malloc(2);
+    heap = new char(2);
+    delete[] heap;
     printf("stack %08x, heap %08x\r\n", (unsigned int)&heap, (unsigned int)heap);
 
     {
@@ -111,6 +135,11 @@
     jump(1);
 
     printf("exit\r\n");
+
+    heap = new char(2);
+    delete[] heap;
+    printf("stack %08x, heap %08x\r\n", (unsigned int)&heap, (unsigned int)heap);
+
     return 0;
 }