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:
Mon Sep 18 11:47:51 2017 +0000
Revision:
0:e8b8f36b4505
Child:
2:968589ca3484
Initial;

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