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