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