demo code for internal EEPROM memory access
LPC11U24 の内臓EEPROMアクセス
LPC11U24 は EEPROM を 4KB 内蔵している。
うち、ユーザーが使えるのは64バイト目以降。
ROM内の In-Application Programming (IAP) を経由してアクセスできる。
main.cpp@0:09bfe87fd66a, 2012-05-11 (annotated)
- Committer:
- okini3939
- Date:
- Fri May 11 00:40:02 2012 +0000
- Revision:
- 0:09bfe87fd66a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:09bfe87fd66a | 1 | /* IAP demo : demo code for internal EEPROM memory access |
okini3939 | 0:09bfe87fd66a | 2 | * |
okini3939 | 0:09bfe87fd66a | 3 | * LPC11U24FBD64/401 : EEPROM 4KB |
okini3939 | 0:09bfe87fd66a | 4 | * Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to. |
okini3939 | 0:09bfe87fd66a | 5 | * |
okini3939 | 0:09bfe87fd66a | 6 | * by Tedd OKANO http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/ |
okini3939 | 0:09bfe87fd66a | 7 | * modified by Suga (supported to LPC11U24) |
okini3939 | 0:09bfe87fd66a | 8 | */ |
okini3939 | 0:09bfe87fd66a | 9 | |
okini3939 | 0:09bfe87fd66a | 10 | #include "mbed.h" |
okini3939 | 0:09bfe87fd66a | 11 | #include "IAP.h" |
okini3939 | 0:09bfe87fd66a | 12 | |
okini3939 | 0:09bfe87fd66a | 13 | #define MEM_SIZE 256 |
okini3939 | 0:09bfe87fd66a | 14 | #define TARGET_ADDRESS 64 |
okini3939 | 0:09bfe87fd66a | 15 | |
okini3939 | 0:09bfe87fd66a | 16 | void memdump( char *p, char *b, int n ); |
okini3939 | 0:09bfe87fd66a | 17 | int isprint( int c ); |
okini3939 | 0:09bfe87fd66a | 18 | |
okini3939 | 0:09bfe87fd66a | 19 | IAP iap; |
okini3939 | 0:09bfe87fd66a | 20 | |
okini3939 | 0:09bfe87fd66a | 21 | |
okini3939 | 0:09bfe87fd66a | 22 | int main() { |
okini3939 | 0:09bfe87fd66a | 23 | char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary |
okini3939 | 0:09bfe87fd66a | 24 | char mem2[ MEM_SIZE ]; |
okini3939 | 0:09bfe87fd66a | 25 | int r; |
okini3939 | 0:09bfe87fd66a | 26 | |
okini3939 | 0:09bfe87fd66a | 27 | printf( "IAP: EEPROM writing test\n" ); |
okini3939 | 0:09bfe87fd66a | 28 | printf( " device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 ); |
okini3939 | 0:09bfe87fd66a | 29 | |
okini3939 | 0:09bfe87fd66a | 30 | for ( int i = 0; i < MEM_SIZE; i++ ) |
okini3939 | 0:09bfe87fd66a | 31 | mem[ i ] = i & 0xFF; |
okini3939 | 0:09bfe87fd66a | 32 | |
okini3939 | 0:09bfe87fd66a | 33 | r = iap.write_eeprom( mem, (char*)TARGET_ADDRESS, MEM_SIZE ); |
okini3939 | 0:09bfe87fd66a | 34 | printf( "copied: SRAM(0x%08X)->EEPROM(0x%08X) for %d bytes. (result=0x%08X)\n", mem, TARGET_ADDRESS, MEM_SIZE, r ); |
okini3939 | 0:09bfe87fd66a | 35 | |
okini3939 | 0:09bfe87fd66a | 36 | r = iap.read_eeprom( (char*)TARGET_ADDRESS, mem2, MEM_SIZE ); |
okini3939 | 0:09bfe87fd66a | 37 | printf( "copied: EEPROM(0x%08X)->SRAM(0x%08X) for %d bytes. (result=0x%08X)\n", TARGET_ADDRESS, mem, MEM_SIZE, r ); |
okini3939 | 0:09bfe87fd66a | 38 | |
okini3939 | 0:09bfe87fd66a | 39 | // compare |
okini3939 | 0:09bfe87fd66a | 40 | r = memcmp(mem, mem2, MEM_SIZE); |
okini3939 | 0:09bfe87fd66a | 41 | printf( "compare result = \"%s\"\n", r ? "FAILED" : "OK" ); |
okini3939 | 0:09bfe87fd66a | 42 | |
okini3939 | 0:09bfe87fd66a | 43 | printf( "showing the EEPROM contents...\n" ); |
okini3939 | 0:09bfe87fd66a | 44 | memdump( (char*)TARGET_ADDRESS, mem2, MEM_SIZE ); |
okini3939 | 0:09bfe87fd66a | 45 | } |
okini3939 | 0:09bfe87fd66a | 46 | |
okini3939 | 0:09bfe87fd66a | 47 | |
okini3939 | 0:09bfe87fd66a | 48 | void memdump( char *base, char *buf, int n ) { |
okini3939 | 0:09bfe87fd66a | 49 | unsigned int *p; |
okini3939 | 0:09bfe87fd66a | 50 | |
okini3939 | 0:09bfe87fd66a | 51 | printf( " memdump from 0x%08X for %d bytes", (unsigned long)base, n ); |
okini3939 | 0:09bfe87fd66a | 52 | |
okini3939 | 0:09bfe87fd66a | 53 | p = (unsigned int *)((unsigned int)buf & ~(unsigned int)0x3); |
okini3939 | 0:09bfe87fd66a | 54 | |
okini3939 | 0:09bfe87fd66a | 55 | for ( int i = 0; i < (n >> 2); i++, p++ ) { |
okini3939 | 0:09bfe87fd66a | 56 | if ( !(i % 4) ) |
okini3939 | 0:09bfe87fd66a | 57 | printf( "\n 0x%08X :", (unsigned int)base + i * 4 ); |
okini3939 | 0:09bfe87fd66a | 58 | |
okini3939 | 0:09bfe87fd66a | 59 | printf( " 0x%08X", *p ); |
okini3939 | 0:09bfe87fd66a | 60 | } |
okini3939 | 0:09bfe87fd66a | 61 | |
okini3939 | 0:09bfe87fd66a | 62 | printf( "\n" ); |
okini3939 | 0:09bfe87fd66a | 63 | } |