Tedd OKANO
/
test_iap_20150205
small test code for IAP
main.cpp@2:806eb710bbb0, 2015-02-08 (annotated)
- Committer:
- okano
- Date:
- Sun Feb 08 12:28:24 2015 +0000
- Revision:
- 2:806eb710bbb0
- Parent:
- 1:90c9fa3e7462
- Child:
- 3:09b4670e337a
"blank checking + erasing" added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:57ed1971604d | 1 | #define TEST_ON_MBED |
okano | 0:57ed1971604d | 2 | |
okano | 0:57ed1971604d | 3 | #ifdef TEST_ON_MBED |
okano | 0:57ed1971604d | 4 | #include "mbed.h" |
okano | 0:57ed1971604d | 5 | #else |
okano | 0:57ed1971604d | 6 | #include <lpc17xx.h> |
okano | 0:57ed1971604d | 7 | #include <absacc.h> |
okano | 0:57ed1971604d | 8 | #endif |
okano | 0:57ed1971604d | 9 | |
okano | 0:57ed1971604d | 10 | void IAP_Write(void); |
okano | 0:57ed1971604d | 11 | |
okano | 0:57ed1971604d | 12 | #ifdef TEST_ON_MBED |
okano | 0:57ed1971604d | 13 | unsigned char *a; |
okano | 0:57ed1971604d | 14 | unsigned char ram_buffer[ 5 + 3 ]; |
okano | 0:57ed1971604d | 15 | #else |
okano | 0:57ed1971604d | 16 | unsigned char a[5] at(0x10000400); |
okano | 0:57ed1971604d | 17 | #endif |
okano | 0:57ed1971604d | 18 | |
okano | 0:57ed1971604d | 19 | unsigned int command[ 5 ]; |
okano | 0:57ed1971604d | 20 | unsigned int result[ 5 ]; |
okano | 0:57ed1971604d | 21 | typedef void (*IAP)( unsigned int [], unsigned int[] ); |
okano | 0:57ed1971604d | 22 | |
okano | 0:57ed1971604d | 23 | IAP iap_entry = (IAP)0x1FFF1FF1; |
okano | 0:57ed1971604d | 24 | |
okano | 0:57ed1971604d | 25 | #define FLASH_TARGET_ADDR ((char *)0x00008100) |
okano | 2:806eb710bbb0 | 26 | #define FLASH_TARGET_SECTOR 8 |
okano | 0:57ed1971604d | 27 | |
okano | 0:57ed1971604d | 28 | int main(void) |
okano | 0:57ed1971604d | 29 | { |
okano | 0:57ed1971604d | 30 | unsigned char i = 0; |
okano | 0:57ed1971604d | 31 | |
okano | 1:90c9fa3e7462 | 32 | a = (unsigned char *)((unsigned long)ram_buffer & ~0x3); // to align to word boundary |
okano | 0:57ed1971604d | 33 | |
okano | 0:57ed1971604d | 34 | for( i = 0; i < 5; i++ ) |
okano | 0:57ed1971604d | 35 | a[ i ] = i; |
okano | 0:57ed1971604d | 36 | |
okano | 0:57ed1971604d | 37 | for( i = 0; i < 5; i++ ) |
okano | 1:90c9fa3e7462 | 38 | printf( "RAM : *((char *)0x%08X) == 0x%02X\r\n", (unsigned long)a + i, *(a + i) ); |
okano | 0:57ed1971604d | 39 | |
okano | 0:57ed1971604d | 40 | IAP_Write(); |
okano | 0:57ed1971604d | 41 | |
okano | 0:57ed1971604d | 42 | char *cp = FLASH_TARGET_ADDR; |
okano | 0:57ed1971604d | 43 | |
okano | 0:57ed1971604d | 44 | for( i = 0; i < 5; i++, cp++ ) |
okano | 1:90c9fa3e7462 | 45 | printf( "flash : *((char *)0x%08X) == 0x%02X\r\n", (unsigned long)cp, *cp ); |
okano | 0:57ed1971604d | 46 | } |
okano | 0:57ed1971604d | 47 | |
okano | 0:57ed1971604d | 48 | void IAP_Write(void) |
okano | 0:57ed1971604d | 49 | { |
okano | 2:806eb710bbb0 | 50 | // blank check |
okano | 2:806eb710bbb0 | 51 | |
okano | 2:806eb710bbb0 | 52 | command[0] = 53; // "blank check" command |
okano | 2:806eb710bbb0 | 53 | command[1] = FLASH_TARGET_SECTOR; // Start sector number is 8 |
okano | 2:806eb710bbb0 | 54 | command[2] = FLASH_TARGET_SECTOR; // Finish sector number is 8 |
okano | 2:806eb710bbb0 | 55 | iap_entry( command, result ); |
okano | 2:806eb710bbb0 | 56 | |
okano | 2:806eb710bbb0 | 57 | printf( "error code for \"blank check\" = %d\r\n", result[ 0 ] ); |
okano | 2:806eb710bbb0 | 58 | |
okano | 2:806eb710bbb0 | 59 | // if the sector is not blank |
okano | 2:806eb710bbb0 | 60 | |
okano | 2:806eb710bbb0 | 61 | if ( result[ 0 ] ) { |
okano | 2:806eb710bbb0 | 62 | |
okano | 2:806eb710bbb0 | 63 | // prepare for erasing |
okano | 2:806eb710bbb0 | 64 | |
okano | 2:806eb710bbb0 | 65 | command[0] = 50; // "prepare" command |
okano | 2:806eb710bbb0 | 66 | command[1] = FLASH_TARGET_SECTOR; // Start sector number is 8 |
okano | 2:806eb710bbb0 | 67 | command[2] = FLASH_TARGET_SECTOR; // Finish sector number is 8 |
okano | 2:806eb710bbb0 | 68 | iap_entry( command, result ); |
okano | 2:806eb710bbb0 | 69 | |
okano | 2:806eb710bbb0 | 70 | printf( "error code for \"prepare\" = %d\r\n", result[ 0 ] ); |
okano | 2:806eb710bbb0 | 71 | |
okano | 2:806eb710bbb0 | 72 | // erase |
okano | 2:806eb710bbb0 | 73 | |
okano | 2:806eb710bbb0 | 74 | command[0] = 52; // "erase" command |
okano | 2:806eb710bbb0 | 75 | command[1] = FLASH_TARGET_SECTOR; // Start sector number is 8 |
okano | 2:806eb710bbb0 | 76 | command[2] = FLASH_TARGET_SECTOR; // Finish sector number is 8 |
okano | 2:806eb710bbb0 | 77 | command[3] = 96000; // Frequency in khz |
okano | 2:806eb710bbb0 | 78 | iap_entry( command, result ); |
okano | 2:806eb710bbb0 | 79 | |
okano | 2:806eb710bbb0 | 80 | printf( "error code for \"erase\" = %d\r\n", result[ 0 ] ); |
okano | 2:806eb710bbb0 | 81 | } |
okano | 2:806eb710bbb0 | 82 | |
okano | 2:806eb710bbb0 | 83 | // prepare for writing |
okano | 2:806eb710bbb0 | 84 | |
okano | 2:806eb710bbb0 | 85 | command[0] = 50; // "prepare" command |
okano | 2:806eb710bbb0 | 86 | command[1] = FLASH_TARGET_SECTOR; // Start sector number is 8 |
okano | 2:806eb710bbb0 | 87 | command[2] = FLASH_TARGET_SECTOR; // Finish sector number is 8 |
okano | 0:57ed1971604d | 88 | iap_entry( command, result ); |
okano | 0:57ed1971604d | 89 | |
okano | 0:57ed1971604d | 90 | printf( "error code for \"prepare\" = %d\r\n", result[ 0 ] ); |
okano | 0:57ed1971604d | 91 | |
okano | 2:806eb710bbb0 | 92 | // write |
okano | 0:57ed1971604d | 93 | |
okano | 0:57ed1971604d | 94 | command[0] = 51; // "copy from ram to flash" command |
okano | 2:806eb710bbb0 | 95 | command[1] = (unsigned int)FLASH_TARGET_ADDR; // Addrees of Flash |
okano | 2:806eb710bbb0 | 96 | command[2] = (unsigned int)a; // Addrees of Sram |
okano | 0:57ed1971604d | 97 | command[3] = 256; // How many bytes want to transfer |
okano | 0:57ed1971604d | 98 | command[4] = 96000; // Frequency in khz |
okano | 0:57ed1971604d | 99 | iap_entry( command, result ); |
okano | 0:57ed1971604d | 100 | |
okano | 0:57ed1971604d | 101 | printf( "error code for \"copy from ram to flash\" = %d\r\n", result[ 0 ] ); |
okano | 0:57ed1971604d | 102 | } |