PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
POKITTO_HW/iap.cpp@28:958b71c4b92a, 2018-01-05 (annotated)
- Committer:
- Pokitto
- Date:
- Fri Jan 05 02:19:51 2018 +0000
- Revision:
- 28:958b71c4b92a
- Parent:
- 20:fa6899411a24
Sound level stored in EEPROM, sound output improved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 20:fa6899411a24 | 1 | #include <stdio.h> |
Pokitto | 20:fa6899411a24 | 2 | #include <stdint.h> |
Pokitto | 20:fa6899411a24 | 3 | #include <iap.h> |
Pokitto | 20:fa6899411a24 | 4 | #include "LPC11U6x.h" |
Pokitto | 20:fa6899411a24 | 5 | #include "PokittoDisk.h" |
Pokitto | 20:fa6899411a24 | 6 | |
Pokitto | 20:fa6899411a24 | 7 | #define TICKRATE_HZ (10) /* 10 ticks per second */ |
Pokitto | 20:fa6899411a24 | 8 | /* SystemTick Counter */ |
Pokitto | 20:fa6899411a24 | 9 | static volatile uint32_t sysTick; |
Pokitto | 20:fa6899411a24 | 10 | |
Pokitto | 20:fa6899411a24 | 11 | /* LPC1347 IAP entry address */ |
Pokitto | 20:fa6899411a24 | 12 | #define IAP_LOCATION 0x1fff1ff1 |
Pokitto | 20:fa6899411a24 | 13 | |
Pokitto | 20:fa6899411a24 | 14 | #define last_sector_flash 0x00038000 //0x0000F000 |
Pokitto | 20:fa6899411a24 | 15 | #define IAP_LAST_SECTOR 28 /* Page number 896 - 1023, 0x00038000 - 0x0003FFFF */ |
Pokitto | 20:fa6899411a24 | 16 | #define IAP_NUM_BYTES_TO_WRITE 256 |
Pokitto | 20:fa6899411a24 | 17 | #define WRITECOUNT (IAP_NUM_BYTES_TO_WRITE / 4) /* when data array is in uint32_t */ |
Pokitto | 20:fa6899411a24 | 18 | |
Pokitto | 20:fa6899411a24 | 19 | #define IAP_PREWRRITE_CMD 50 /* Prepare sector for write operation command */ |
Pokitto | 20:fa6899411a24 | 20 | #define IAP_WRISECTOR_CMD 51 |
Pokitto | 20:fa6899411a24 | 21 | #define IAP_ERSSECTOR_CMD 52 |
Pokitto | 20:fa6899411a24 | 22 | #define IAP_REPID_CMD 54 |
Pokitto | 20:fa6899411a24 | 23 | |
Pokitto | 20:fa6899411a24 | 24 | /* IAP command variables */ |
Pokitto | 20:fa6899411a24 | 25 | static unsigned int command[5], result[4]; |
Pokitto | 20:fa6899411a24 | 26 | |
Pokitto | 20:fa6899411a24 | 27 | /* IAP entry function */ |
Pokitto | 20:fa6899411a24 | 28 | typedef int (*IAP)(unsigned int[], unsigned int[]); |
Pokitto | 20:fa6899411a24 | 29 | IAP iap_entry = (IAP) IAP_LOCATION; |
Pokitto | 20:fa6899411a24 | 30 | |
Pokitto | 20:fa6899411a24 | 31 | int CopyPageToFlash (uint32_t address, uint8_t* data) { |
Pokitto | 20:fa6899411a24 | 32 | IAP iap_call = (IAP) IAP_LOCATION; |
Pokitto | 20:fa6899411a24 | 33 | uint32_t writecount=0; |
Pokitto | 20:fa6899411a24 | 34 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 35 | |
Pokitto | 20:fa6899411a24 | 36 | unsigned int sector, page; |
Pokitto | 20:fa6899411a24 | 37 | bool firstpage=false, erasepage=false; |
Pokitto | 20:fa6899411a24 | 38 | |
Pokitto | 20:fa6899411a24 | 39 | //DEBUG// |
Pokitto | 20:fa6899411a24 | 40 | //for (int i=0;i<256;i++) data[i]=0xBB; |
Pokitto | 20:fa6899411a24 | 41 | |
Pokitto | 20:fa6899411a24 | 42 | /* Calculate sector based on address */ |
Pokitto | 20:fa6899411a24 | 43 | if (address < 0x18000) sector = address/0x1000; // sectors go in 4 k's |
Pokitto | 20:fa6899411a24 | 44 | else if (address >= 0x38000) sector = 28; |
Pokitto | 20:fa6899411a24 | 45 | else if (address >= 0x30000) sector = 27; |
Pokitto | 20:fa6899411a24 | 46 | else if (address >= 0x28000) sector = 26; |
Pokitto | 20:fa6899411a24 | 47 | else if (address >= 0x20000) sector = 25; |
Pokitto | 20:fa6899411a24 | 48 | else sector = 24; |
Pokitto | 20:fa6899411a24 | 49 | |
Pokitto | 20:fa6899411a24 | 50 | /* Check is it the first page in the sector */ |
Pokitto | 20:fa6899411a24 | 51 | if (sector<24) { |
Pokitto | 20:fa6899411a24 | 52 | if (address == sector * 0x1000) firstpage = true; |
Pokitto | 20:fa6899411a24 | 53 | } else { |
Pokitto | 20:fa6899411a24 | 54 | if (address == (sector-24)*0x4000 + 0x18000) firstpage = true; |
Pokitto | 20:fa6899411a24 | 55 | } |
Pokitto | 20:fa6899411a24 | 56 | |
Pokitto | 20:fa6899411a24 | 57 | /* Prepare the sector for writing */ |
Pokitto | 20:fa6899411a24 | 58 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 59 | command[1] = sector; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 60 | command[2] = sector; /* End Sector Number */ |
Pokitto | 20:fa6899411a24 | 61 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 62 | if (result[0]) return 1; |
Pokitto | 20:fa6899411a24 | 63 | |
Pokitto | 20:fa6899411a24 | 64 | /* wipe pages when writing the loader */ |
Pokitto | 20:fa6899411a24 | 65 | if (address==0x39000) { |
Pokitto | 20:fa6899411a24 | 66 | erasepage=true; |
Pokitto | 20:fa6899411a24 | 67 | } |
Pokitto | 20:fa6899411a24 | 68 | |
Pokitto | 20:fa6899411a24 | 69 | /* do sector erase only when writing first page of given sector */ |
Pokitto | 20:fa6899411a24 | 70 | if (firstpage) { |
Pokitto | 20:fa6899411a24 | 71 | /* Erase the last sector */ |
Pokitto | 20:fa6899411a24 | 72 | command[0] = IAP_ERSSECTOR_CMD; /* Erase command code*/ |
Pokitto | 20:fa6899411a24 | 73 | command[1] = sector; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 74 | command[2] = sector; /* End Sector Number */ |
Pokitto | 20:fa6899411a24 | 75 | command[3] = SystemCoreClock / 1000UL; /* Core clock frequency in kHz */ |
Pokitto | 20:fa6899411a24 | 76 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 77 | if (result[0]) return 1; |
Pokitto | 20:fa6899411a24 | 78 | /* Prepare to write/erase the last sector, needs to be done again because succesful erase re-locks sectors */ |
Pokitto | 20:fa6899411a24 | 79 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 80 | command[1] = sector; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 81 | command[2] = sector; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 82 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 83 | if (result[0]) return 1; |
Pokitto | 20:fa6899411a24 | 84 | } |
Pokitto | 20:fa6899411a24 | 85 | |
Pokitto | 20:fa6899411a24 | 86 | /* page erase for bootloader area */ |
Pokitto | 20:fa6899411a24 | 87 | if (erasepage) { |
Pokitto | 20:fa6899411a24 | 88 | command[0] = 59; //erase page command |
Pokitto | 20:fa6899411a24 | 89 | command[1] = 896; |
Pokitto | 20:fa6899411a24 | 90 | command[2] = 1023; |
Pokitto | 20:fa6899411a24 | 91 | command[3] = SystemCoreClock / 1000UL; /* Core clock frequency in kHz */ |
Pokitto | 20:fa6899411a24 | 92 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 93 | if (result[0]) return 1; |
Pokitto | 20:fa6899411a24 | 94 | /* Prepare to write/erase the last sector, needs to be done again because succesful erase re-locks sectors */ |
Pokitto | 20:fa6899411a24 | 95 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 96 | command[1] = sector; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 97 | command[2] = sector; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 98 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 99 | if (result[0]) return 1; |
Pokitto | 20:fa6899411a24 | 100 | } |
Pokitto | 20:fa6899411a24 | 101 | |
Pokitto | 20:fa6899411a24 | 102 | /* Write data to the sectors */ |
Pokitto | 20:fa6899411a24 | 103 | command[0] = IAP_WRISECTOR_CMD; /* Write command code */ |
Pokitto | 20:fa6899411a24 | 104 | command[1] = (uint32_t) (uint32_t*) address; /* Destination Flash Address */ |
Pokitto | 20:fa6899411a24 | 105 | command[2] = (uint32_t) data; /* Source RAM Address */ |
Pokitto | 20:fa6899411a24 | 106 | command[3] = 0x100; /* Number of Bytes to be written */ |
Pokitto | 20:fa6899411a24 | 107 | command[4] = SystemCoreClock / 1000; /* System clock frequency */ |
Pokitto | 20:fa6899411a24 | 108 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 109 | if (result[0]) return 1; |
Pokitto | 20:fa6899411a24 | 110 | |
Pokitto | 20:fa6899411a24 | 111 | /* Re-enable interrupt mode */ |
Pokitto | 20:fa6899411a24 | 112 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 113 | |
Pokitto | 20:fa6899411a24 | 114 | return 0; /*succesful write*/ |
Pokitto | 20:fa6899411a24 | 115 | |
Pokitto | 20:fa6899411a24 | 116 | } |
Pokitto | 20:fa6899411a24 | 117 | |
Pokitto | 20:fa6899411a24 | 118 | __attribute__((section(".IAP_Code"))) int HelloFromIAP() { |
Pokitto | 20:fa6899411a24 | 119 | static uint32_t array_data[WRITECOUNT]; |
Pokitto | 20:fa6899411a24 | 120 | int i; |
Pokitto | 20:fa6899411a24 | 121 | /* Initialize the array data to be written to FLASH */ |
Pokitto | 20:fa6899411a24 | 122 | for (i = 0; i < WRITECOUNT; i++) { |
Pokitto | 20:fa6899411a24 | 123 | array_data[i] = 0xB007AB1E; |
Pokitto | 20:fa6899411a24 | 124 | } |
Pokitto | 20:fa6899411a24 | 125 | |
Pokitto | 20:fa6899411a24 | 126 | IAP iap_call = (IAP) IAP_LOCATION; |
Pokitto | 20:fa6899411a24 | 127 | uint8_t teahupoo; |
Pokitto | 20:fa6899411a24 | 128 | //readEEPROM(0,&teahupoo,1); |
Pokitto | 20:fa6899411a24 | 129 | teahupoo++; |
Pokitto | 20:fa6899411a24 | 130 | //writeEEPROM(0,&teahupoo,1); |
Pokitto | 20:fa6899411a24 | 131 | |
Pokitto | 20:fa6899411a24 | 132 | /** open file **/ |
Pokitto | 20:fa6899411a24 | 133 | pokInitSD(); |
Pokitto | 20:fa6899411a24 | 134 | char fn[20]; |
Pokitto | 20:fa6899411a24 | 135 | char* now; |
Pokitto | 20:fa6899411a24 | 136 | now = (char*)last_sector_flash; |
Pokitto | 20:fa6899411a24 | 137 | switch (now[0]) { |
Pokitto | 20:fa6899411a24 | 138 | case 0xAA: |
Pokitto | 20:fa6899411a24 | 139 | fn[0]='B';fn[1]='B';fn[2]='.';fn[3]='B';fn[4]='I';fn[5]='N';fn[6]='\0';break; |
Pokitto | 20:fa6899411a24 | 140 | case 0xBB: |
Pokitto | 20:fa6899411a24 | 141 | fn[0]='C';fn[1]='C';fn[2]='.';fn[3]='B';fn[4]='I';fn[5]='N';fn[6]='\0';break; |
Pokitto | 20:fa6899411a24 | 142 | default: |
Pokitto | 20:fa6899411a24 | 143 | fn[0]='A';fn[1]='A';fn[2]='.';fn[3]='B';fn[4]='I';fn[5]='N';fn[6]='\0'; |
Pokitto | 20:fa6899411a24 | 144 | } |
Pokitto | 20:fa6899411a24 | 145 | if(fileOpen(fn,FILE_MODE_BINARY)) { |
Pokitto | 20:fa6899411a24 | 146 | return 1; |
Pokitto | 20:fa6899411a24 | 147 | } else { |
Pokitto | 20:fa6899411a24 | 148 | for (i = 0; i < WRITECOUNT; i++) { |
Pokitto | 20:fa6899411a24 | 149 | fileReadBytes((uint8_t*)&array_data[i],4); |
Pokitto | 20:fa6899411a24 | 150 | } |
Pokitto | 20:fa6899411a24 | 151 | } |
Pokitto | 20:fa6899411a24 | 152 | |
Pokitto | 20:fa6899411a24 | 153 | |
Pokitto | 20:fa6899411a24 | 154 | /** write sector in flash **/ |
Pokitto | 20:fa6899411a24 | 155 | /* Read Part Identification Number*/ |
Pokitto | 20:fa6899411a24 | 156 | command[0] = IAP_REPID_CMD; /* Read ID command code */ |
Pokitto | 20:fa6899411a24 | 157 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 158 | |
Pokitto | 20:fa6899411a24 | 159 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 160 | |
Pokitto | 20:fa6899411a24 | 161 | /* Prepare to write/erase the last sector */ |
Pokitto | 20:fa6899411a24 | 162 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 163 | command[1] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 164 | command[2] = IAP_LAST_SECTOR; /* End Sector Number */ |
Pokitto | 20:fa6899411a24 | 165 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 166 | /* Erase the last sector */ |
Pokitto | 20:fa6899411a24 | 167 | command[0] = IAP_ERSSECTOR_CMD; /* Erase command code*/ |
Pokitto | 20:fa6899411a24 | 168 | command[1] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 169 | command[2] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 170 | command[3] = SystemCoreClock / 1000UL; /* Core clock frequency in kHz */ |
Pokitto | 20:fa6899411a24 | 171 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 172 | /* Prepare to write/erase the last sector */ |
Pokitto | 20:fa6899411a24 | 173 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 174 | command[1] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 175 | command[2] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 176 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 177 | /* Write to the last sector */ |
Pokitto | 20:fa6899411a24 | 178 | command[0] = IAP_WRISECTOR_CMD; /* Write command code */ |
Pokitto | 20:fa6899411a24 | 179 | command[1] = (uint32_t) last_sector_flash; /* Destination Flash Address */ |
Pokitto | 20:fa6899411a24 | 180 | command[2] = (uint32_t) &array_data; /* Source RAM Address */ |
Pokitto | 20:fa6899411a24 | 181 | command[3] = IAP_NUM_BYTES_TO_WRITE; /* Number of Bytes to be written */ |
Pokitto | 20:fa6899411a24 | 182 | command[4] = SystemCoreClock / 1000; /* System clock frequency */ |
Pokitto | 20:fa6899411a24 | 183 | iap_call(command, result); |
Pokitto | 20:fa6899411a24 | 184 | |
Pokitto | 20:fa6899411a24 | 185 | /* Re-enable interrupt mode */ |
Pokitto | 20:fa6899411a24 | 186 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 187 | |
Pokitto | 20:fa6899411a24 | 188 | |
Pokitto | 20:fa6899411a24 | 189 | SCB->AIRCR = 0x05FA0004; //issue system reset |
Pokitto | 20:fa6899411a24 | 190 | while(1); //should never come here |
Pokitto | 20:fa6899411a24 | 191 | return teahupoo; |
Pokitto | 20:fa6899411a24 | 192 | } |
Pokitto | 20:fa6899411a24 | 193 | |
Pokitto | 20:fa6899411a24 | 194 | |
Pokitto | 20:fa6899411a24 | 195 | |
Pokitto | 20:fa6899411a24 | 196 | |
Pokitto | 20:fa6899411a24 | 197 | void IAPstacksave() |
Pokitto | 20:fa6899411a24 | 198 | { |
Pokitto | 20:fa6899411a24 | 199 | /*need to save 32 top bytes of RAM to RAM1*/ |
Pokitto | 20:fa6899411a24 | 200 | #define RAM1_0 (*((volatile unsigned long *) 0x20000000)) |
Pokitto | 20:fa6899411a24 | 201 | #define RAM1_1 (*((volatile unsigned long *) 0x20000004)) |
Pokitto | 20:fa6899411a24 | 202 | #define RAM1_2 (*((volatile unsigned long *) 0x20000008)) |
Pokitto | 20:fa6899411a24 | 203 | #define RAM1_3 (*((volatile unsigned long *) 0x2000000C)) |
Pokitto | 20:fa6899411a24 | 204 | #define RAM1_4 (*((volatile unsigned long *) 0x20000010)) |
Pokitto | 20:fa6899411a24 | 205 | #define RAM1_5 (*((volatile unsigned long *) 0x20000014)) |
Pokitto | 20:fa6899411a24 | 206 | #define RAM1_6 (*((volatile unsigned long *) 0x20000018)) |
Pokitto | 20:fa6899411a24 | 207 | #define RAM1_7 (*((volatile unsigned long *) 0x2000001C)) |
Pokitto | 20:fa6899411a24 | 208 | |
Pokitto | 20:fa6899411a24 | 209 | uint32_t *saveloc = (uint32_t*)(0x10002000-0x20); // RAM top - 32 bytes |
Pokitto | 20:fa6899411a24 | 210 | RAM1_0 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 211 | RAM1_1 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 212 | RAM1_2 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 213 | RAM1_3 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 214 | RAM1_4 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 215 | RAM1_5 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 216 | RAM1_6 = *saveloc++; |
Pokitto | 20:fa6899411a24 | 217 | RAM1_7 = *saveloc; |
Pokitto | 20:fa6899411a24 | 218 | } |
Pokitto | 20:fa6899411a24 | 219 | |
Pokitto | 20:fa6899411a24 | 220 | |
Pokitto | 20:fa6899411a24 | 221 | char iaptest() { |
Pokitto | 20:fa6899411a24 | 222 | static uint32_t array_data[WRITECOUNT]; |
Pokitto | 20:fa6899411a24 | 223 | int i; |
Pokitto | 20:fa6899411a24 | 224 | /* Initialize the array data to be written to FLASH */ |
Pokitto | 20:fa6899411a24 | 225 | for (i = 0; i < WRITECOUNT; i++) { |
Pokitto | 20:fa6899411a24 | 226 | array_data[i] = 0x11223340 + i; |
Pokitto | 20:fa6899411a24 | 227 | } |
Pokitto | 20:fa6899411a24 | 228 | |
Pokitto | 20:fa6899411a24 | 229 | /* Read Part Identification Number*/ |
Pokitto | 20:fa6899411a24 | 230 | command[0] = IAP_REPID_CMD; /* Read ID command code */ |
Pokitto | 20:fa6899411a24 | 231 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 232 | |
Pokitto | 20:fa6899411a24 | 233 | /* Reinvoke ISP mode so that reprogamming of Flash possible */ |
Pokitto | 20:fa6899411a24 | 234 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 235 | |
Pokitto | 20:fa6899411a24 | 236 | command[0] = IAP_REPID_CMD; |
Pokitto | 20:fa6899411a24 | 237 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 238 | |
Pokitto | 20:fa6899411a24 | 239 | /* Prepare to write/erase the last sector */ |
Pokitto | 20:fa6899411a24 | 240 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 241 | command[1] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 242 | command[2] = IAP_LAST_SECTOR; /* End Sector Number */ |
Pokitto | 20:fa6899411a24 | 243 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 244 | |
Pokitto | 20:fa6899411a24 | 245 | /* Erase the last sector */ |
Pokitto | 20:fa6899411a24 | 246 | command[0] = IAP_ERSSECTOR_CMD; /* Erase command code*/ |
Pokitto | 20:fa6899411a24 | 247 | command[1] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 248 | command[2] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 249 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 250 | |
Pokitto | 20:fa6899411a24 | 251 | /* Prepare to write/erase the last sector */ |
Pokitto | 20:fa6899411a24 | 252 | command[0] = IAP_PREWRRITE_CMD; /* Prepare to write/erase command code */ |
Pokitto | 20:fa6899411a24 | 253 | command[1] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 254 | command[2] = IAP_LAST_SECTOR; /* Start Sector Number */ |
Pokitto | 20:fa6899411a24 | 255 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 256 | |
Pokitto | 20:fa6899411a24 | 257 | /* Write to the last sector */ |
Pokitto | 20:fa6899411a24 | 258 | command[0] = IAP_WRISECTOR_CMD; /* Write command code */ |
Pokitto | 20:fa6899411a24 | 259 | command[1] = (uint32_t) last_sector_flash; /* Destination Flash Address */ |
Pokitto | 20:fa6899411a24 | 260 | command[2] = (uint32_t) &array_data; /* Source RAM Address */ |
Pokitto | 20:fa6899411a24 | 261 | command[3] = IAP_NUM_BYTES_TO_WRITE; /* Number of Bytes to be written */ |
Pokitto | 20:fa6899411a24 | 262 | command[4] = SystemCoreClock / 1000; /* System clock frequency */ |
Pokitto | 20:fa6899411a24 | 263 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 264 | |
Pokitto | 20:fa6899411a24 | 265 | /* Re-enable interrupt mode */ |
Pokitto | 20:fa6899411a24 | 266 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 267 | |
Pokitto | 20:fa6899411a24 | 268 | //while (1) { |
Pokitto | 20:fa6899411a24 | 269 | // __WFI(); |
Pokitto | 20:fa6899411a24 | 270 | //} |
Pokitto | 20:fa6899411a24 | 271 | |
Pokitto | 20:fa6899411a24 | 272 | return 0; |
Pokitto | 20:fa6899411a24 | 273 | |
Pokitto | 20:fa6899411a24 | 274 | } |
Pokitto | 20:fa6899411a24 | 275 | |
Pokitto | 20:fa6899411a24 | 276 | |
Pokitto | 20:fa6899411a24 | 277 | //1) EEprom Write |
Pokitto | 20:fa6899411a24 | 278 | // |
Pokitto | 20:fa6899411a24 | 279 | //Command code: 61 |
Pokitto | 20:fa6899411a24 | 280 | //Param0: eeprom address (byte, half-word or word aligned) |
Pokitto | 20:fa6899411a24 | 281 | //Param1: RAM address (byte, half-word or word aligned) |
Pokitto | 20:fa6899411a24 | 282 | //Param2: Number of bytes to be written ( Byte, Half-words write are ok) |
Pokitto | 20:fa6899411a24 | 283 | //Param3: System Clock Frequency (CCLK) in kHz |
Pokitto | 20:fa6899411a24 | 284 | // |
Pokitto | 20:fa6899411a24 | 285 | //Return Code CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED |
Pokitto | 28:958b71c4b92a | 286 | __attribute__((section(".IAP_Code"))) void writeEEPROM( uint16_t* eeAddress, uint8_t* buffAddress, uint32_t byteCount ) |
Pokitto | 20:fa6899411a24 | 287 | { |
Pokitto | 20:fa6899411a24 | 288 | unsigned int command[5], result[4]; |
Pokitto | 20:fa6899411a24 | 289 | |
Pokitto | 20:fa6899411a24 | 290 | command[0] = 61; |
Pokitto | 20:fa6899411a24 | 291 | command[1] = (uint32_t) eeAddress; |
Pokitto | 20:fa6899411a24 | 292 | command[2] = (uint32_t) buffAddress; |
Pokitto | 20:fa6899411a24 | 293 | command[3] = byteCount; |
Pokitto | 20:fa6899411a24 | 294 | command[4] = SystemCoreClock/1000; |
Pokitto | 20:fa6899411a24 | 295 | |
Pokitto | 20:fa6899411a24 | 296 | /* Invoke IAP call...*/ |
Pokitto | 20:fa6899411a24 | 297 | #if (EEPROM_PROFILE!=0) |
Pokitto | 20:fa6899411a24 | 298 | LPC_CT32B0->TCR = 1; |
Pokitto | 20:fa6899411a24 | 299 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 300 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 301 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 302 | LPC_CT32B0->TCR = 0; |
Pokitto | 20:fa6899411a24 | 303 | #else |
Pokitto | 20:fa6899411a24 | 304 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 305 | iap_entry(command, result); |
Pokitto | 20:fa6899411a24 | 306 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 307 | #endif |
Pokitto | 20:fa6899411a24 | 308 | if (0 != result[0]) |
Pokitto | 20:fa6899411a24 | 309 | { |
Pokitto | 20:fa6899411a24 | 310 | //Trap error |
Pokitto | 20:fa6899411a24 | 311 | while(1); |
Pokitto | 20:fa6899411a24 | 312 | } |
Pokitto | 20:fa6899411a24 | 313 | return; |
Pokitto | 20:fa6899411a24 | 314 | } |
Pokitto | 20:fa6899411a24 | 315 | |
Pokitto | 20:fa6899411a24 | 316 | //2) EEprom Read |
Pokitto | 20:fa6899411a24 | 317 | //Command code: 62 |
Pokitto | 20:fa6899411a24 | 318 | //Param0: eeprom address (byte, half-word or word aligned) |
Pokitto | 20:fa6899411a24 | 319 | //Param1: RAM address (byte, half-word or word aligned) |
Pokitto | 20:fa6899411a24 | 320 | //Param2: Number of bytes to be read ( Byte, Half-words read are ok) |
Pokitto | 20:fa6899411a24 | 321 | //Param3: System Clock Frequency (CCLK) in kHz |
Pokitto | 20:fa6899411a24 | 322 | // |
Pokitto | 20:fa6899411a24 | 323 | //Return Code CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED |
Pokitto | 28:958b71c4b92a | 324 | __attribute__((section(".IAP_Code"))) void readEEPROM( uint16_t* eeAddress, uint8_t* buffAddress, uint32_t byteCount ) |
Pokitto | 20:fa6899411a24 | 325 | { |
Pokitto | 20:fa6899411a24 | 326 | unsigned int command[5], result[4]; |
Pokitto | 20:fa6899411a24 | 327 | |
Pokitto | 20:fa6899411a24 | 328 | command[0] = 62; |
Pokitto | 20:fa6899411a24 | 329 | command[1] = (uint32_t) eeAddress; |
Pokitto | 20:fa6899411a24 | 330 | command[2] = (uint32_t) buffAddress; |
Pokitto | 20:fa6899411a24 | 331 | command[3] = byteCount; |
Pokitto | 20:fa6899411a24 | 332 | command[4] = SystemCoreClock/1000; |
Pokitto | 20:fa6899411a24 | 333 | |
Pokitto | 20:fa6899411a24 | 334 | /* Invoke IAP call...*/ |
Pokitto | 20:fa6899411a24 | 335 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 336 | iap_entry( command, result); |
Pokitto | 20:fa6899411a24 | 337 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 338 | if (0 != result[0]) |
Pokitto | 20:fa6899411a24 | 339 | { |
Pokitto | 20:fa6899411a24 | 340 | //Trap error |
Pokitto | 20:fa6899411a24 | 341 | while(1); |
Pokitto | 20:fa6899411a24 | 342 | } |
Pokitto | 20:fa6899411a24 | 343 | return; |
Pokitto | 20:fa6899411a24 | 344 | } |
Pokitto | 20:fa6899411a24 | 345 | |
Pokitto | 20:fa6899411a24 | 346 | __attribute__((section(".IAP_Code"))) void IAPreadPartId( uint8_t* eeAddress, uint8_t* buffAddress, uint32_t byteCount ) |
Pokitto | 20:fa6899411a24 | 347 | { |
Pokitto | 20:fa6899411a24 | 348 | unsigned int command[5], result[4]; |
Pokitto | 20:fa6899411a24 | 349 | |
Pokitto | 20:fa6899411a24 | 350 | command[0] = 62; |
Pokitto | 20:fa6899411a24 | 351 | command[1] = (uint32_t) eeAddress; |
Pokitto | 20:fa6899411a24 | 352 | command[2] = (uint32_t) buffAddress; |
Pokitto | 20:fa6899411a24 | 353 | command[3] = byteCount; |
Pokitto | 20:fa6899411a24 | 354 | command[4] = SystemCoreClock/1000; |
Pokitto | 20:fa6899411a24 | 355 | |
Pokitto | 20:fa6899411a24 | 356 | /* Invoke IAP call...*/ |
Pokitto | 20:fa6899411a24 | 357 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 358 | iap_entry( command, result); |
Pokitto | 20:fa6899411a24 | 359 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 360 | if (0 != result[0]) |
Pokitto | 20:fa6899411a24 | 361 | { |
Pokitto | 20:fa6899411a24 | 362 | //Trap error |
Pokitto | 20:fa6899411a24 | 363 | while(1); |
Pokitto | 20:fa6899411a24 | 364 | } |
Pokitto | 20:fa6899411a24 | 365 | return; |
Pokitto | 20:fa6899411a24 | 366 | } |
Pokitto | 20:fa6899411a24 | 367 | |
Pokitto | 28:958b71c4b92a | 368 | uint8_t eeprom_read_byte(uint16_t* index) { |
Pokitto | 20:fa6899411a24 | 369 | uint8_t val; |
Pokitto | 20:fa6899411a24 | 370 | readEEPROM(index,&val,1); |
Pokitto | 20:fa6899411a24 | 371 | return val; |
Pokitto | 20:fa6899411a24 | 372 | } |
Pokitto | 20:fa6899411a24 | 373 | |
Pokitto | 28:958b71c4b92a | 374 | void eeprom_write_byte(uint16_t*index , uint8_t val) { |
Pokitto | 20:fa6899411a24 | 375 | writeEEPROM(index,&val,1); |
Pokitto | 20:fa6899411a24 | 376 | } |
Pokitto | 20:fa6899411a24 | 377 | |
Pokitto | 20:fa6899411a24 | 378 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 379 | * $Id$ |
Pokitto | 20:fa6899411a24 | 380 | * |
Pokitto | 20:fa6899411a24 | 381 | * Project: NXP LPC11U6x In Application Programming |
Pokitto | 20:fa6899411a24 | 382 | * |
Pokitto | 20:fa6899411a24 | 383 | * Description: Provides access to In-Application Programming (IAP) routines |
Pokitto | 20:fa6899411a24 | 384 | * contained within the bootROM sector of LPC11U6x devices. |
Pokitto | 20:fa6899411a24 | 385 | * |
Pokitto | 20:fa6899411a24 | 386 | * Copyright(C) 2010, NXP Semiconductor |
Pokitto | 20:fa6899411a24 | 387 | * All rights reserved. |
Pokitto | 20:fa6899411a24 | 388 | * |
Pokitto | 20:fa6899411a24 | 389 | ***************************************************************************** |
Pokitto | 20:fa6899411a24 | 390 | * Software that is described herein is for illustrative purposes only |
Pokitto | 20:fa6899411a24 | 391 | * which provides customers with programming information regarding the |
Pokitto | 20:fa6899411a24 | 392 | * products. This software is supplied "AS IS" without any warranties. |
Pokitto | 20:fa6899411a24 | 393 | * NXP Semiconductors assumes no responsibility or liability for the |
Pokitto | 20:fa6899411a24 | 394 | * use of the software, conveys no license or title under any patent, |
Pokitto | 20:fa6899411a24 | 395 | * copyright, or mask work right to the product. NXP Semiconductors |
Pokitto | 20:fa6899411a24 | 396 | * reserves the right to make changes in the software without |
Pokitto | 20:fa6899411a24 | 397 | * notification. NXP Semiconductors also make no representation or |
Pokitto | 20:fa6899411a24 | 398 | * warranty that such application will be suitable for the specified |
Pokitto | 20:fa6899411a24 | 399 | * use without further testing or modification. |
Pokitto | 20:fa6899411a24 | 400 | *****************************************************************************/ |
Pokitto | 20:fa6899411a24 | 401 | |
Pokitto | 20:fa6899411a24 | 402 | /* IAP Command Definitions */ |
Pokitto | 20:fa6899411a24 | 403 | #define IAP_CMD_PREPARE_SECTORS 50 |
Pokitto | 20:fa6899411a24 | 404 | #define IAP_CMD_COPY_RAM_TO_FLASH 51 |
Pokitto | 20:fa6899411a24 | 405 | #define IAP_CMD_ERASE_SECTORS 52 |
Pokitto | 20:fa6899411a24 | 406 | #define IAP_CMD_BLANK_CHECK_SECTORS 53 |
Pokitto | 20:fa6899411a24 | 407 | #define IAP_CMD_READ_PART_ID 54 |
Pokitto | 20:fa6899411a24 | 408 | #define IAP_CMD_READ_BOOT_ROM_VERSION 55 |
Pokitto | 20:fa6899411a24 | 409 | #define IAP_CMD_COMPARE 56 |
Pokitto | 20:fa6899411a24 | 410 | #define IAP_CMD_REINVOKE_ISP 57 |
Pokitto | 20:fa6899411a24 | 411 | #define IAP_CMD_READ_UID 58 |
Pokitto | 20:fa6899411a24 | 412 | |
Pokitto | 20:fa6899411a24 | 413 | #define IAP_CMD_ERASE_PAGE 59 //new |
Pokitto | 20:fa6899411a24 | 414 | |
Pokitto | 20:fa6899411a24 | 415 | /* IAP boot ROM location and access function */ |
Pokitto | 20:fa6899411a24 | 416 | #define IAP_ROM_LOCATION 0x1FFF1FF1UL |
Pokitto | 20:fa6899411a24 | 417 | //#define IAP_EXECUTE_CMD(a, b) ((void (*)())(IAP_ROM_LOCATION))(a, b) |
Pokitto | 20:fa6899411a24 | 418 | |
Pokitto | 20:fa6899411a24 | 419 | __attribute__((section(".IAP_Code"))) void IAP_EXECUTE_CMD(uint32_t* a, uint32_t* b) { |
Pokitto | 20:fa6899411a24 | 420 | void (*user_code_entry)(uint32_t*,uint32_t*); |
Pokitto | 20:fa6899411a24 | 421 | uint32_t *p; |
Pokitto | 20:fa6899411a24 | 422 | p = (uint32_t *)IAP_ROM_LOCATION; |
Pokitto | 20:fa6899411a24 | 423 | user_code_entry = (void (*)(uint32_t*,uint32_t*))(*p); |
Pokitto | 20:fa6899411a24 | 424 | user_code_entry(a, b); |
Pokitto | 20:fa6899411a24 | 425 | } |
Pokitto | 20:fa6899411a24 | 426 | |
Pokitto | 20:fa6899411a24 | 427 | |
Pokitto | 20:fa6899411a24 | 428 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 429 | ** Function name: u32IAP_PrepareSectors |
Pokitto | 20:fa6899411a24 | 430 | ** |
Pokitto | 20:fa6899411a24 | 431 | ** Description: Prepares sector(s) for erasing or write operations. This |
Pokitto | 20:fa6899411a24 | 432 | ** command must be executed before executing the "Copy RAM to |
Pokitto | 20:fa6899411a24 | 433 | ** Flash" or "Erase Sector(s)" commands. |
Pokitto | 20:fa6899411a24 | 434 | ** |
Pokitto | 20:fa6899411a24 | 435 | ** Parameters: u32StartSector - Number of first sector to prepare. |
Pokitto | 20:fa6899411a24 | 436 | ** u32EndSector - Number of last sector to prepare. |
Pokitto | 20:fa6899411a24 | 437 | ** |
Pokitto | 20:fa6899411a24 | 438 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 439 | ** |
Pokitto | 20:fa6899411a24 | 440 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 441 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_PrepareSectors(uint32_t u32StartSector, uint32_t u32EndSector) |
Pokitto | 20:fa6899411a24 | 442 | { |
Pokitto | 20:fa6899411a24 | 443 | uint32_t u32Status; |
Pokitto | 20:fa6899411a24 | 444 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 445 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 446 | |
Pokitto | 20:fa6899411a24 | 447 | if (u32EndSector < u32StartSector) |
Pokitto | 20:fa6899411a24 | 448 | { |
Pokitto | 20:fa6899411a24 | 449 | u32Status = IAP_STA_INVALD_PARAM; |
Pokitto | 20:fa6899411a24 | 450 | } |
Pokitto | 20:fa6899411a24 | 451 | else |
Pokitto | 20:fa6899411a24 | 452 | { |
Pokitto | 20:fa6899411a24 | 453 | au32Command[0] = IAP_CMD_PREPARE_SECTORS; |
Pokitto | 20:fa6899411a24 | 454 | au32Command[1] = u32StartSector; |
Pokitto | 20:fa6899411a24 | 455 | au32Command[2] = u32EndSector; |
Pokitto | 20:fa6899411a24 | 456 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 457 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 458 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 459 | u32Status = au32Result[0]; |
Pokitto | 20:fa6899411a24 | 460 | } |
Pokitto | 20:fa6899411a24 | 461 | return u32Status; |
Pokitto | 20:fa6899411a24 | 462 | } |
Pokitto | 20:fa6899411a24 | 463 | |
Pokitto | 20:fa6899411a24 | 464 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 465 | ** Function name: u32IAP_CopyRAMToFlash |
Pokitto | 20:fa6899411a24 | 466 | ** |
Pokitto | 20:fa6899411a24 | 467 | ** Description: Program the flash memory with data stored in RAM. |
Pokitto | 20:fa6899411a24 | 468 | ** |
Pokitto | 20:fa6899411a24 | 469 | ** Parameters: u32DstAddr - Destination Flash address, should be a 256 |
Pokitto | 20:fa6899411a24 | 470 | ** byte boundary. |
Pokitto | 20:fa6899411a24 | 471 | ** u32SrcAddr - Source RAM address, should be a word boundary |
Pokitto | 20:fa6899411a24 | 472 | ** u32Len - Number of 8-bit bytes to write, must be a |
Pokitto | 20:fa6899411a24 | 473 | ** multiple of 256. |
Pokitto | 20:fa6899411a24 | 474 | * |
Pokitto | 20:fa6899411a24 | 475 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 476 | ** |
Pokitto | 20:fa6899411a24 | 477 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 478 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_CopyRAMToFlash(uint32_t u32DstAddr, uint32_t u32SrcAddr, uint32_t u32Len) |
Pokitto | 20:fa6899411a24 | 479 | { |
Pokitto | 20:fa6899411a24 | 480 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 481 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 482 | |
Pokitto | 20:fa6899411a24 | 483 | au32Command[0] = IAP_CMD_COPY_RAM_TO_FLASH; |
Pokitto | 20:fa6899411a24 | 484 | au32Command[1] = u32DstAddr; |
Pokitto | 20:fa6899411a24 | 485 | au32Command[2] = u32SrcAddr; |
Pokitto | 20:fa6899411a24 | 486 | au32Command[3] = u32Len; |
Pokitto | 20:fa6899411a24 | 487 | au32Command[4] = SystemCoreClock / 1000UL; /* Core clock frequency in kHz */ |
Pokitto | 20:fa6899411a24 | 488 | |
Pokitto | 20:fa6899411a24 | 489 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 490 | |
Pokitto | 20:fa6899411a24 | 491 | return au32Result[0]; |
Pokitto | 20:fa6899411a24 | 492 | } |
Pokitto | 20:fa6899411a24 | 493 | |
Pokitto | 20:fa6899411a24 | 494 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 495 | ** Function name: u32IAP_EraseSectors |
Pokitto | 20:fa6899411a24 | 496 | ** |
Pokitto | 20:fa6899411a24 | 497 | ** Description: Erase a sector or multiple sectors of on-chip Flash memory. |
Pokitto | 20:fa6899411a24 | 498 | ** |
Pokitto | 20:fa6899411a24 | 499 | ** Parameters: u32StartSector - Number of first sector to erase. |
Pokitto | 20:fa6899411a24 | 500 | ** u32EndSector - Number of last sector to erase. |
Pokitto | 20:fa6899411a24 | 501 | * |
Pokitto | 20:fa6899411a24 | 502 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 503 | ** |
Pokitto | 20:fa6899411a24 | 504 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 505 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_EraseSectors(uint32_t u32StartSector, uint32_t u32EndSector) |
Pokitto | 20:fa6899411a24 | 506 | { |
Pokitto | 20:fa6899411a24 | 507 | uint32_t u32Status; |
Pokitto | 20:fa6899411a24 | 508 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 509 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 510 | |
Pokitto | 20:fa6899411a24 | 511 | if (u32EndSector < u32StartSector) |
Pokitto | 20:fa6899411a24 | 512 | { |
Pokitto | 20:fa6899411a24 | 513 | u32Status = IAP_STA_INVALD_PARAM; |
Pokitto | 20:fa6899411a24 | 514 | } |
Pokitto | 20:fa6899411a24 | 515 | else |
Pokitto | 20:fa6899411a24 | 516 | { |
Pokitto | 20:fa6899411a24 | 517 | au32Command[0] = IAP_CMD_ERASE_SECTORS; |
Pokitto | 20:fa6899411a24 | 518 | au32Command[1] = u32StartSector; |
Pokitto | 20:fa6899411a24 | 519 | au32Command[2] = u32EndSector; |
Pokitto | 20:fa6899411a24 | 520 | au32Command[3] = SystemCoreClock / 1000UL; /* Core clock frequency in kHz */ |
Pokitto | 20:fa6899411a24 | 521 | |
Pokitto | 20:fa6899411a24 | 522 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 523 | |
Pokitto | 20:fa6899411a24 | 524 | u32Status = au32Result[0]; |
Pokitto | 20:fa6899411a24 | 525 | } |
Pokitto | 20:fa6899411a24 | 526 | return u32Status; |
Pokitto | 20:fa6899411a24 | 527 | } |
Pokitto | 20:fa6899411a24 | 528 | |
Pokitto | 20:fa6899411a24 | 529 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 530 | ** Function name: u32IAP_BlankCheckSectors |
Pokitto | 20:fa6899411a24 | 531 | ** |
Pokitto | 20:fa6899411a24 | 532 | ** Description: Blank check a sector or multiple sectors of on-chip flash |
Pokitto | 20:fa6899411a24 | 533 | ** memory. |
Pokitto | 20:fa6899411a24 | 534 | ** |
Pokitto | 20:fa6899411a24 | 535 | ** Parameters: u32StartSector - Number of first sector to check. |
Pokitto | 20:fa6899411a24 | 536 | ** u32EndSector - Number of last sector to check. |
Pokitto | 20:fa6899411a24 | 537 | ** pu32Result[0] - Offset of the first non blank word location |
Pokitto | 20:fa6899411a24 | 538 | ** if the Status Code is IAP_STA_SECTOR_NOT_BLANK. |
Pokitto | 20:fa6899411a24 | 539 | ** pu32Result[1] - Contents of non blank word location. |
Pokitto | 20:fa6899411a24 | 540 | ** |
Pokitto | 20:fa6899411a24 | 541 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 542 | ** |
Pokitto | 20:fa6899411a24 | 543 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 544 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_BlankCheckSectors(uint32_t u32StartSector, uint32_t u32EndSector, uint32_t *pu32Result) |
Pokitto | 20:fa6899411a24 | 545 | { |
Pokitto | 20:fa6899411a24 | 546 | uint32_t u32Status; |
Pokitto | 20:fa6899411a24 | 547 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 548 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 549 | |
Pokitto | 20:fa6899411a24 | 550 | if (u32EndSector < u32StartSector) |
Pokitto | 20:fa6899411a24 | 551 | { |
Pokitto | 20:fa6899411a24 | 552 | u32Status = IAP_STA_INVALD_PARAM; |
Pokitto | 20:fa6899411a24 | 553 | } |
Pokitto | 20:fa6899411a24 | 554 | else |
Pokitto | 20:fa6899411a24 | 555 | { |
Pokitto | 20:fa6899411a24 | 556 | au32Command[0] = IAP_CMD_BLANK_CHECK_SECTORS; |
Pokitto | 20:fa6899411a24 | 557 | au32Command[1] = u32StartSector; |
Pokitto | 20:fa6899411a24 | 558 | au32Command[2] = u32EndSector; |
Pokitto | 20:fa6899411a24 | 559 | |
Pokitto | 20:fa6899411a24 | 560 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 561 | |
Pokitto | 20:fa6899411a24 | 562 | if (au32Result[0] == IAP_STA_SECTOR_NOT_BLANK) |
Pokitto | 20:fa6899411a24 | 563 | { |
Pokitto | 20:fa6899411a24 | 564 | *pu32Result = au32Result[0]; |
Pokitto | 20:fa6899411a24 | 565 | *(pu32Result + 1) = au32Result[1]; |
Pokitto | 20:fa6899411a24 | 566 | } |
Pokitto | 20:fa6899411a24 | 567 | u32Status = au32Result[0]; |
Pokitto | 20:fa6899411a24 | 568 | } |
Pokitto | 20:fa6899411a24 | 569 | return u32Status; |
Pokitto | 20:fa6899411a24 | 570 | } |
Pokitto | 20:fa6899411a24 | 571 | |
Pokitto | 20:fa6899411a24 | 572 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 573 | ** Function name: u32IAP_ReadPartID |
Pokitto | 20:fa6899411a24 | 574 | ** |
Pokitto | 20:fa6899411a24 | 575 | ** Description: Read the part identification number. |
Pokitto | 20:fa6899411a24 | 576 | ** |
Pokitto | 20:fa6899411a24 | 577 | ** Parameters: pu32PartID - Pointer to storage for part ID number. |
Pokitto | 20:fa6899411a24 | 578 | * |
Pokitto | 20:fa6899411a24 | 579 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 580 | ** |
Pokitto | 20:fa6899411a24 | 581 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 582 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_ReadPartID(uint32_t *pu32PartID) |
Pokitto | 20:fa6899411a24 | 583 | { |
Pokitto | 20:fa6899411a24 | 584 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 585 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 586 | |
Pokitto | 20:fa6899411a24 | 587 | au32Command[0] = IAP_CMD_READ_PART_ID; |
Pokitto | 20:fa6899411a24 | 588 | __disable_irq(); |
Pokitto | 20:fa6899411a24 | 589 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 590 | __enable_irq(); |
Pokitto | 20:fa6899411a24 | 591 | *pu32PartID = au32Result[1]; |
Pokitto | 20:fa6899411a24 | 592 | |
Pokitto | 20:fa6899411a24 | 593 | return au32Result[0]; |
Pokitto | 20:fa6899411a24 | 594 | } |
Pokitto | 20:fa6899411a24 | 595 | |
Pokitto | 20:fa6899411a24 | 596 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 597 | ** Function name: u32IAP_ReadBootVersion |
Pokitto | 20:fa6899411a24 | 598 | ** |
Pokitto | 20:fa6899411a24 | 599 | ** Description: Read the boot code version number. |
Pokitto | 20:fa6899411a24 | 600 | ** |
Pokitto | 20:fa6899411a24 | 601 | ** Parameters: pu32Major - Major version number in ASCII format. |
Pokitto | 20:fa6899411a24 | 602 | ** pu32Minor - Minor version number in ASCII format. |
Pokitto | 20:fa6899411a24 | 603 | ** |
Pokitto | 20:fa6899411a24 | 604 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 605 | ** |
Pokitto | 20:fa6899411a24 | 606 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 607 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_ReadBootVersion(uint32_t *pu32Major, uint32_t *pu32Minor) |
Pokitto | 20:fa6899411a24 | 608 | //uint32_t u32IAP_ReadBootVersion(uint32_t *pu32Major) |
Pokitto | 20:fa6899411a24 | 609 | { |
Pokitto | 20:fa6899411a24 | 610 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 611 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 612 | |
Pokitto | 20:fa6899411a24 | 613 | au32Command[0] = IAP_CMD_READ_BOOT_ROM_VERSION; |
Pokitto | 20:fa6899411a24 | 614 | |
Pokitto | 20:fa6899411a24 | 615 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 616 | |
Pokitto | 20:fa6899411a24 | 617 | |
Pokitto | 20:fa6899411a24 | 618 | *pu32Major = (au32Result[1] & 0x0000FF00UL) >> 8; |
Pokitto | 20:fa6899411a24 | 619 | *pu32Minor = au32Result[1] & 0x000000FFUL; |
Pokitto | 20:fa6899411a24 | 620 | |
Pokitto | 20:fa6899411a24 | 621 | return au32Result[0]; |
Pokitto | 20:fa6899411a24 | 622 | } |
Pokitto | 20:fa6899411a24 | 623 | |
Pokitto | 20:fa6899411a24 | 624 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 625 | ** Function name: u32IAP_Compare |
Pokitto | 20:fa6899411a24 | 626 | ** |
Pokitto | 20:fa6899411a24 | 627 | ** Description: Compares the memory contents at two locations. |
Pokitto | 20:fa6899411a24 | 628 | ** |
Pokitto | 20:fa6899411a24 | 629 | ** Parameters: u32Len - Number of bytes to compare, must be a multiple of 4. |
Pokitto | 20:fa6899411a24 | 630 | ** pu32Offset - Offset of the first mismatch if the Status Code is COMPARE_ERROR |
Pokitto | 20:fa6899411a24 | 631 | ** |
Pokitto | 20:fa6899411a24 | 632 | ** Returned value: Status code returned by IAP ROM function. |
Pokitto | 20:fa6899411a24 | 633 | ** |
Pokitto | 20:fa6899411a24 | 634 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 635 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_Compare(uint32_t u32DstAddr, uint32_t u32SrcAddr, uint32_t u32Len, uint32_t *pu32Offset) |
Pokitto | 20:fa6899411a24 | 636 | { |
Pokitto | 20:fa6899411a24 | 637 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 638 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 639 | |
Pokitto | 20:fa6899411a24 | 640 | au32Command[0] = IAP_CMD_COMPARE; |
Pokitto | 20:fa6899411a24 | 641 | au32Command[1] = u32DstAddr; |
Pokitto | 20:fa6899411a24 | 642 | au32Command[2] = u32SrcAddr; |
Pokitto | 20:fa6899411a24 | 643 | au32Command[3] = u32Len; |
Pokitto | 20:fa6899411a24 | 644 | |
Pokitto | 20:fa6899411a24 | 645 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 646 | |
Pokitto | 20:fa6899411a24 | 647 | if (au32Result[0] == IAP_STA_COMPARE_ERROR) |
Pokitto | 20:fa6899411a24 | 648 | { |
Pokitto | 20:fa6899411a24 | 649 | if (pu32Offset != 0) |
Pokitto | 20:fa6899411a24 | 650 | { |
Pokitto | 20:fa6899411a24 | 651 | *pu32Offset = au32Result[1]; |
Pokitto | 20:fa6899411a24 | 652 | } |
Pokitto | 20:fa6899411a24 | 653 | } |
Pokitto | 20:fa6899411a24 | 654 | return au32Result[0]; |
Pokitto | 20:fa6899411a24 | 655 | } |
Pokitto | 20:fa6899411a24 | 656 | |
Pokitto | 20:fa6899411a24 | 657 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 658 | ** Function name: vIAP_ReinvokeISP |
Pokitto | 20:fa6899411a24 | 659 | ** |
Pokitto | 20:fa6899411a24 | 660 | ** Description: Invoke the bootloader in ISP mode. |
Pokitto | 20:fa6899411a24 | 661 | ** |
Pokitto | 20:fa6899411a24 | 662 | ** Parameters: None. |
Pokitto | 20:fa6899411a24 | 663 | * |
Pokitto | 20:fa6899411a24 | 664 | ** Returned value: None. |
Pokitto | 20:fa6899411a24 | 665 | ** |
Pokitto | 20:fa6899411a24 | 666 | ******************************************************************************/ |
Pokitto | 20:fa6899411a24 | 667 | __attribute__((section(".IAP_Code"))) void vIAP_ReinvokeISP(void) |
Pokitto | 20:fa6899411a24 | 668 | { |
Pokitto | 20:fa6899411a24 | 669 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 670 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 671 | |
Pokitto | 20:fa6899411a24 | 672 | au32Command[0] = IAP_CMD_REINVOKE_ISP; |
Pokitto | 20:fa6899411a24 | 673 | |
Pokitto | 20:fa6899411a24 | 674 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 675 | } |
Pokitto | 20:fa6899411a24 | 676 | |
Pokitto | 20:fa6899411a24 | 677 | // read UID |
Pokitto | 20:fa6899411a24 | 678 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_ReadUID(uint32_t * pu32UID) |
Pokitto | 20:fa6899411a24 | 679 | { |
Pokitto | 20:fa6899411a24 | 680 | uint32_t au32Result[5]; |
Pokitto | 20:fa6899411a24 | 681 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 682 | |
Pokitto | 20:fa6899411a24 | 683 | au32Command[0] = IAP_CMD_READ_UID; |
Pokitto | 20:fa6899411a24 | 684 | |
Pokitto | 20:fa6899411a24 | 685 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 686 | // *pu32UID++ = au32Result[1]; |
Pokitto | 20:fa6899411a24 | 687 | // *pu32UID++ = au32Result[2]; |
Pokitto | 20:fa6899411a24 | 688 | // *pu32UID++ = au32Result[3]; |
Pokitto | 20:fa6899411a24 | 689 | // *pu32UID = au32Result[4]; |
Pokitto | 20:fa6899411a24 | 690 | |
Pokitto | 20:fa6899411a24 | 691 | *pu32UID = au32Result[1]; |
Pokitto | 20:fa6899411a24 | 692 | *pu32UID++ = au32Result[2]; |
Pokitto | 20:fa6899411a24 | 693 | *pu32UID++ = au32Result[3]; |
Pokitto | 20:fa6899411a24 | 694 | *pu32UID++ = au32Result[4]; |
Pokitto | 20:fa6899411a24 | 695 | |
Pokitto | 20:fa6899411a24 | 696 | return au32Result[0]; |
Pokitto | 20:fa6899411a24 | 697 | |
Pokitto | 20:fa6899411a24 | 698 | } |
Pokitto | 20:fa6899411a24 | 699 | |
Pokitto | 20:fa6899411a24 | 700 | //IAP erase Page 256B 64K have 0-255 pages, page0-15 in sector 0, 32K have 0-127 pages, 128k have 0-511 pages, |
Pokitto | 20:fa6899411a24 | 701 | __attribute__((section(".IAP_Code"))) uint32_t u32IAP_ErasePage(uint32_t u32StartPage, uint32_t u32EndPage) |
Pokitto | 20:fa6899411a24 | 702 | { |
Pokitto | 20:fa6899411a24 | 703 | uint32_t u32Status; |
Pokitto | 20:fa6899411a24 | 704 | uint32_t au32Result[3]; |
Pokitto | 20:fa6899411a24 | 705 | uint32_t au32Command[5]; |
Pokitto | 20:fa6899411a24 | 706 | |
Pokitto | 20:fa6899411a24 | 707 | if (u32EndPage < u32StartPage) |
Pokitto | 20:fa6899411a24 | 708 | { |
Pokitto | 20:fa6899411a24 | 709 | u32Status = IAP_STA_INVALD_PARAM; |
Pokitto | 20:fa6899411a24 | 710 | } |
Pokitto | 20:fa6899411a24 | 711 | else |
Pokitto | 20:fa6899411a24 | 712 | { |
Pokitto | 20:fa6899411a24 | 713 | au32Command[0] = IAP_CMD_ERASE_PAGE; |
Pokitto | 20:fa6899411a24 | 714 | au32Command[1] = u32StartPage; |
Pokitto | 20:fa6899411a24 | 715 | au32Command[2] = u32EndPage; |
Pokitto | 20:fa6899411a24 | 716 | au32Command[3] = SystemCoreClock / 1000UL; /* Core clock frequency in kHz */ |
Pokitto | 20:fa6899411a24 | 717 | |
Pokitto | 20:fa6899411a24 | 718 | IAP_EXECUTE_CMD(au32Command, au32Result); |
Pokitto | 20:fa6899411a24 | 719 | |
Pokitto | 20:fa6899411a24 | 720 | u32Status = au32Result[0]; |
Pokitto | 20:fa6899411a24 | 721 | } |
Pokitto | 20:fa6899411a24 | 722 | return u32Status; |
Pokitto | 20:fa6899411a24 | 723 | } |
Pokitto | 20:fa6899411a24 | 724 | |
Pokitto | 20:fa6899411a24 | 725 | |
Pokitto | 20:fa6899411a24 | 726 | /***************************************************************************** |
Pokitto | 20:fa6899411a24 | 727 | ** End Of File |
Pokitto | 20:fa6899411a24 | 728 | *****************************************************************************/ |
Pokitto | 20:fa6899411a24 | 729 |