Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 dynamic load the binary file. 00003 binary -> Flash 0x00040000 & run 00004 00005 lpc17xx.ld 00006 IROM (rx) : ORIGIN = 0x00040000, LENGTH = 256k 00007 IRAM0 (rwx) : ORIGIN = 0x10002000, LENGTH = 24k 00008 00009 startup_LPC17xx.c 00010 //void Reset_Handler(void) __attribute__((__interrupt__)); 00011 void Reset_Handler(void); 00012 // SystemInit(); 00013 00014 compiled.bin --> test.dat 00015 */ 00016 #include "mbed.h" 00017 #include "IAP.h" 00018 #include <new> 00019 00020 #define BUF_SIZE 512 00021 #define TARGET_SECTOR 22 00022 #define TARGET_SECTOR_NUM 8 00023 00024 LocalFileSystem local("local"); 00025 00026 void memdump( char *p, int n ); 00027 00028 IAP iap; 00029 00030 extern "C" 00031 void HardFault_Handler() { 00032 register unsigned int _msp __asm("msp"); 00033 printf("Hard Fault! %x (%x)\r\n", SCB->HFSR, *((unsigned int *)(_msp + 24))); 00034 printf(" - %x\r\n", (*(volatile uint32_t*)0xe000ed24)); 00035 // printf("Hard Fault! %x\r\n", SCB->HFSR); 00036 exit(-1); 00037 } 00038 00039 void no_memory () { 00040 printf("panic: can't allocate to memory!\r\n"); 00041 exit(-1); 00042 } 00043 00044 void jump (int vect) { 00045 void (*func)(); 00046 unsigned int *p; 00047 00048 p = (unsigned int *)(sector_start_adress[TARGET_SECTOR] + 4 * vect); 00049 func = (void (*)())*p; 00050 // printf("jump vector %d (%08x)\r\n", vect, func); 00051 if ((char*)*p >= sector_start_adress[TARGET_SECTOR] && (char*)*p < sector_start_adress[TARGET_SECTOR] + FLASH_SECTOR_SIZE_16_TO_29 * TARGET_SECTOR_NUM) { 00052 func(); 00053 } 00054 } 00055 00056 int loadbinary (char *filename) { 00057 int i, r, num, sector, size = 0; 00058 FILE *fp; 00059 char buf[BUF_SIZE]; 00060 00061 fp = fopen(filename, "r"); 00062 if (! fp) return -1; 00063 00064 for (sector = TARGET_SECTOR; sector < TARGET_SECTOR + TARGET_SECTOR_NUM; sector ++) { 00065 if (iap.blank_check(sector, sector) == SECTOR_NOT_BLANK) { 00066 iap.prepare(sector, sector); 00067 r = iap.erase(sector, sector); 00068 if (r) { 00069 printf("iap.erase (%d) %d %x\r\n", r, sector, i); 00070 goto error; 00071 } 00072 printf("iap erase %d\r\n", sector); 00073 } 00074 00075 for (i = 0; i < FLASH_SECTOR_SIZE_16_TO_29; i += BUF_SIZE) { 00076 num = fread(buf, sizeof(char), BUF_SIZE, fp); 00077 if (num <= 0) { 00078 // EOF 00079 goto exit; 00080 } 00081 00082 // write Flash 00083 r = iap.prepare(sector, sector); 00084 if (r) { 00085 printf("iap.prepare (%d) %d %x\r\n", r, sector, i); 00086 goto error; 00087 } 00088 r = iap.write(buf, sector_start_adress[sector] + i, BUF_SIZE); 00089 if (r) { 00090 printf("iap.write (%d) %d %x\r\n", r, sector, i); 00091 goto error; 00092 } 00093 r = iap.compare(buf, sector_start_adress[sector] + i, BUF_SIZE); 00094 if (r) { 00095 printf("iap.compare (%d) %d %x\r\n", r, sector, i); 00096 goto error; 00097 } 00098 size += num; 00099 } 00100 } 00101 00102 exit: 00103 printf("write %d bytes, end sector %d\r\n", size, sector); 00104 fclose(fp); 00105 return 0; 00106 00107 error: 00108 printf("write error\r\n"); 00109 fclose(fp); 00110 return r; 00111 } 00112 00113 int main() { 00114 char *heap; 00115 00116 set_new_handler(no_memory); // new handler function 00117 00118 printf( "device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\r\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 ); 00119 00120 if (loadbinary("/local/test.dat")) { 00121 printf("error loadbinary\r\n"); 00122 return -1; 00123 } 00124 00125 // memdump( sector_start_adress[ TARGET_SECTOR ], 48 * 4); 00126 00127 heap = new char(2); 00128 delete[] heap; 00129 printf("stack %08x, heap %08x\r\n", (unsigned int)&heap, (unsigned int)heap); 00130 00131 { 00132 register unsigned int _control __asm("control"); 00133 register unsigned int _msp __asm("msp"); 00134 register unsigned int _psp __asm("psp"); 00135 printf("MSP %08x, PSP %08x, CONTROL %x\r\n", _msp, _psp, _control & 3); 00136 } 00137 00138 jump(1); 00139 00140 printf("exit\r\n"); 00141 00142 heap = new char(2); 00143 delete[] heap; 00144 printf("stack %08x, heap %08x\r\n", (unsigned int)&heap, (unsigned int)heap); 00145 00146 return 0; 00147 } 00148 00149 00150 void memdump( char *base, int n ) { 00151 unsigned int *p; 00152 00153 printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n ); 00154 00155 p = (unsigned int *)((unsigned int)base & ~(unsigned int)0x3); 00156 00157 for ( int i = 0; i < (n >> 2); i++, p++ ) { 00158 if ( !(i % 4) ) 00159 printf( "\r\n 0x%08X :", (unsigned int)p ); 00160 00161 printf( " %08X", *p ); 00162 } 00163 00164 printf( "\r\n" ); 00165 }
Generated on Sat Jul 16 2022 13:37:35 by
1.7.2