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