PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
Pokitto
Date:
Tue Sep 19 08:47:36 2017 +0000
Revision:
2:968589ca3484
Parent:
0:e8b8f36b4505
Got rid of warnings at compile time

Who changed what in which revision?

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