.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Committer:
Sissors
Date:
Wed Mar 11 21:00:29 2015 +0000
Revision:
1:782a3ddc329e
Child:
2:8c44f28c122c
Basic UART functional + IAP functional

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 1:782a3ddc329e 1 #include "FreescaleIAP.h"
Sissors 1:782a3ddc329e 2
Sissors 1:782a3ddc329e 3 //#define IAPDEBUG
Sissors 1:782a3ddc329e 4
Sissors 1:782a3ddc329e 5 #ifdef TARGET_K64F
Sissors 1:782a3ddc329e 6 //For K64F
Sissors 1:782a3ddc329e 7 # include "MK64F12.h"
Sissors 1:782a3ddc329e 8 # define USE_ProgramPhrase 1
Sissors 1:782a3ddc329e 9 # define FTFA FTFE
Sissors 1:782a3ddc329e 10 # define FTFA_FSTAT_FPVIOL_MASK FTFE_FSTAT_FPVIOL_MASK
Sissors 1:782a3ddc329e 11 # define FTFA_FSTAT_ACCERR_MASK FTFE_FSTAT_ACCERR_MASK
Sissors 1:782a3ddc329e 12 # define FTFA_FSTAT_RDCOLERR_MASK FTFE_FSTAT_RDCOLERR_MASK
Sissors 1:782a3ddc329e 13 # define FTFA_FSTAT_CCIF_MASK FTFE_FSTAT_CCIF_MASK
Sissors 1:782a3ddc329e 14 # define FTFA_FSTAT_MGSTAT0_MASK FTFE_FSTAT_MGSTAT0_MASK
Sissors 1:782a3ddc329e 15 #else
Sissors 1:782a3ddc329e 16 //Different names used on at least the K20:
Sissors 1:782a3ddc329e 17 # ifndef FTFA_FSTAT_FPVIOL_MASK
Sissors 1:782a3ddc329e 18 # define FTFA FTFL
Sissors 1:782a3ddc329e 19 # define FTFA_FSTAT_FPVIOL_MASK FTFL_FSTAT_FPVIOL_MASK
Sissors 1:782a3ddc329e 20 # define FTFA_FSTAT_ACCERR_MASK FTFL_FSTAT_ACCERR_MASK
Sissors 1:782a3ddc329e 21 # define FTFA_FSTAT_RDCOLERR_MASK FTFL_FSTAT_RDCOLERR_MASK
Sissors 1:782a3ddc329e 22 # define FTFA_FSTAT_CCIF_MASK FTFL_FSTAT_CCIF_MASK
Sissors 1:782a3ddc329e 23 # define FTFA_FSTAT_MGSTAT0_MASK FTFL_FSTAT_MGSTAT0_MASK
Sissors 1:782a3ddc329e 24 # endif
Sissors 1:782a3ddc329e 25 #endif
Sissors 1:782a3ddc329e 26
Sissors 1:782a3ddc329e 27
Sissors 1:782a3ddc329e 28 enum FCMD {
Sissors 1:782a3ddc329e 29 Read1s = 0x01,
Sissors 1:782a3ddc329e 30 ProgramCheck = 0x02,
Sissors 1:782a3ddc329e 31 ReadResource = 0x03,
Sissors 1:782a3ddc329e 32 ProgramLongword = 0x06,
Sissors 1:782a3ddc329e 33 ProgramPhrase = 0x07,
Sissors 1:782a3ddc329e 34 EraseSector = 0x09,
Sissors 1:782a3ddc329e 35 Read1sBlock = 0x40,
Sissors 1:782a3ddc329e 36 ReadOnce = 0x41,
Sissors 1:782a3ddc329e 37 ProgramOnce = 0x43,
Sissors 1:782a3ddc329e 38 EraseAll = 0x44,
Sissors 1:782a3ddc329e 39 VerifyBackdoor = 0x45
Sissors 1:782a3ddc329e 40 };
Sissors 1:782a3ddc329e 41
Sissors 1:782a3ddc329e 42 inline void run_command(void);
Sissors 1:782a3ddc329e 43 bool check_boundary(int address, unsigned int length);
Sissors 1:782a3ddc329e 44 bool check_align(int address);
Sissors 1:782a3ddc329e 45 IAPCode verify_erased(int address, unsigned int length);
Sissors 1:782a3ddc329e 46 IAPCode check_error(void);
Sissors 1:782a3ddc329e 47 IAPCode program_word(int address, char *data);
Sissors 1:782a3ddc329e 48
Sissors 1:782a3ddc329e 49 __attribute__((section(".ARM.__at_0x11000"))) IAPCode erase_sector(int address) {
Sissors 1:782a3ddc329e 50 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 51 printf("IAP: Erasing at %x\r\n", address);
Sissors 1:782a3ddc329e 52 #endif
Sissors 1:782a3ddc329e 53 if (check_align(address))
Sissors 1:782a3ddc329e 54 return AlignError;
Sissors 1:782a3ddc329e 55
Sissors 1:782a3ddc329e 56 //Setup command
Sissors 1:782a3ddc329e 57 FTFA->FCCOB0 = EraseSector;
Sissors 1:782a3ddc329e 58 FTFA->FCCOB1 = (address >> 16) & 0xFF;
Sissors 1:782a3ddc329e 59 FTFA->FCCOB2 = (address >> 8) & 0xFF;
Sissors 1:782a3ddc329e 60 FTFA->FCCOB3 = address & 0xFF;
Sissors 1:782a3ddc329e 61
Sissors 1:782a3ddc329e 62 run_command();
Sissors 1:782a3ddc329e 63
Sissors 1:782a3ddc329e 64 return check_error();
Sissors 1:782a3ddc329e 65 }
Sissors 1:782a3ddc329e 66
Sissors 1:782a3ddc329e 67 __attribute__((section(".ARM.__at_0x11100"))) IAPCode program_flash(int address, char *data, unsigned int length) {
Sissors 1:782a3ddc329e 68 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 69 printf("IAP: Programming flash at %x with length %d\r\n", address, length);
Sissors 1:782a3ddc329e 70 #endif
Sissors 1:782a3ddc329e 71 if (check_align(address))
Sissors 1:782a3ddc329e 72 return AlignError;
Sissors 1:782a3ddc329e 73
Sissors 1:782a3ddc329e 74 IAPCode eraseCheck = verify_erased(address, length);
Sissors 1:782a3ddc329e 75 if (eraseCheck != Success)
Sissors 1:782a3ddc329e 76 return eraseCheck;
Sissors 1:782a3ddc329e 77
Sissors 1:782a3ddc329e 78 IAPCode progResult;
Sissors 1:782a3ddc329e 79 #ifdef USE_ProgramPhrase
Sissors 1:782a3ddc329e 80 for (int i = 0; i < length; i+=8) {
Sissors 1:782a3ddc329e 81 progResult = program_word(address + i, data + i);
Sissors 1:782a3ddc329e 82 if (progResult != Success)
Sissors 1:782a3ddc329e 83 return progResult;
Sissors 1:782a3ddc329e 84 }
Sissors 1:782a3ddc329e 85 #else
Sissors 1:782a3ddc329e 86 for (int i = 0; i < length; i+=4) {
Sissors 1:782a3ddc329e 87 progResult = program_word(address + i, data + i);
Sissors 1:782a3ddc329e 88 if (progResult != Success)
Sissors 1:782a3ddc329e 89 return progResult;
Sissors 1:782a3ddc329e 90 }
Sissors 1:782a3ddc329e 91 #endif
Sissors 1:782a3ddc329e 92 return Success;
Sissors 1:782a3ddc329e 93 }
Sissors 1:782a3ddc329e 94
Sissors 1:782a3ddc329e 95 __attribute__((section(".ARM.__at_0x11300"))) uint32_t flash_size(void) {
Sissors 1:782a3ddc329e 96 uint32_t retval = (SIM->FCFG2 & 0x7F000000u) >> (24-13);
Sissors 1:782a3ddc329e 97 if (SIM->FCFG2 & (1<<23)) //Possible second flash bank
Sissors 1:782a3ddc329e 98 retval += (SIM->FCFG2 & 0x007F0000u) >> (16-13);
Sissors 1:782a3ddc329e 99 return retval;
Sissors 1:782a3ddc329e 100 }
Sissors 1:782a3ddc329e 101
Sissors 1:782a3ddc329e 102 __attribute__((section(".ARM.__at_0x11400"))) IAPCode program_word(int address, char *data) {
Sissors 1:782a3ddc329e 103 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 104 #ifdef USE_ProgramPhrase
Sissors 1:782a3ddc329e 105 printf("IAP: Programming word at %x, %d - %d - %d - %d - %d - %d - %d - %d\r\n", address, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
Sissors 1:782a3ddc329e 106 #else
Sissors 1:782a3ddc329e 107 printf("IAP: Programming word at %x, %d - %d - %d - %d\r\n", address, data[0], data[1], data[2], data[3]);
Sissors 1:782a3ddc329e 108 #endif
Sissors 1:782a3ddc329e 109
Sissors 1:782a3ddc329e 110 #endif
Sissors 1:782a3ddc329e 111 if (check_align(address))
Sissors 1:782a3ddc329e 112 return AlignError;
Sissors 1:782a3ddc329e 113 #ifdef USE_ProgramPhrase
Sissors 1:782a3ddc329e 114 FTFA->FCCOB0 = ProgramPhrase;
Sissors 1:782a3ddc329e 115 FTFA->FCCOB1 = (address >> 16) & 0xFF;
Sissors 1:782a3ddc329e 116 FTFA->FCCOB2 = (address >> 8) & 0xFF;
Sissors 1:782a3ddc329e 117 FTFA->FCCOB3 = address & 0xFF;
Sissors 1:782a3ddc329e 118 FTFA->FCCOB4 = data[3];
Sissors 1:782a3ddc329e 119 FTFA->FCCOB5 = data[2];
Sissors 1:782a3ddc329e 120 FTFA->FCCOB6 = data[1];
Sissors 1:782a3ddc329e 121 FTFA->FCCOB7 = data[0];
Sissors 1:782a3ddc329e 122 FTFA->FCCOB8 = data[7];
Sissors 1:782a3ddc329e 123 FTFA->FCCOB9 = data[6];
Sissors 1:782a3ddc329e 124 FTFA->FCCOBA = data[5];
Sissors 1:782a3ddc329e 125 FTFA->FCCOBB = data[4];
Sissors 1:782a3ddc329e 126 #else
Sissors 1:782a3ddc329e 127 //Setup command
Sissors 1:782a3ddc329e 128 FTFA->FCCOB0 = ProgramLongword;
Sissors 1:782a3ddc329e 129 FTFA->FCCOB1 = (address >> 16) & 0xFF;
Sissors 1:782a3ddc329e 130 FTFA->FCCOB2 = (address >> 8) & 0xFF;
Sissors 1:782a3ddc329e 131 FTFA->FCCOB3 = address & 0xFF;
Sissors 1:782a3ddc329e 132 FTFA->FCCOB4 = data[3];
Sissors 1:782a3ddc329e 133 FTFA->FCCOB5 = data[2];
Sissors 1:782a3ddc329e 134 FTFA->FCCOB6 = data[1];
Sissors 1:782a3ddc329e 135 FTFA->FCCOB7 = data[0];
Sissors 1:782a3ddc329e 136 #endif
Sissors 1:782a3ddc329e 137 run_command();
Sissors 1:782a3ddc329e 138
Sissors 1:782a3ddc329e 139 return check_error();
Sissors 1:782a3ddc329e 140 }
Sissors 1:782a3ddc329e 141
Sissors 1:782a3ddc329e 142 /* Clear possible flags which are set, run command, wait until done */
Sissors 1:782a3ddc329e 143 __attribute__((section(".ARM.__at_0x11500"))) inline void run_command(void) {
Sissors 1:782a3ddc329e 144 //Clear possible old errors, start command, wait until done
Sissors 1:782a3ddc329e 145 __disable_irq(); //Disable IRQs, preventing IRQ routines from trying to access flash (thanks to https://mbed.org/users/mjr/)
Sissors 1:782a3ddc329e 146 FTFA->FSTAT = FTFA_FSTAT_FPVIOL_MASK | FTFA_FSTAT_ACCERR_MASK | FTFA_FSTAT_RDCOLERR_MASK;
Sissors 1:782a3ddc329e 147 FTFA->FSTAT = FTFA_FSTAT_CCIF_MASK;
Sissors 1:782a3ddc329e 148 while (!(FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK));
Sissors 1:782a3ddc329e 149 __enable_irq();
Sissors 1:782a3ddc329e 150 }
Sissors 1:782a3ddc329e 151
Sissors 1:782a3ddc329e 152
Sissors 1:782a3ddc329e 153
Sissors 1:782a3ddc329e 154 /* Check if no flash boundary is violated
Sissors 1:782a3ddc329e 155 Returns true on violation */
Sissors 1:782a3ddc329e 156 __attribute__((section(".ARM.__at_0x11600"))) bool check_boundary(int address, unsigned int length) {
Sissors 1:782a3ddc329e 157 int temp = (address+length - 1) / SECTOR_SIZE;
Sissors 1:782a3ddc329e 158 address /= SECTOR_SIZE;
Sissors 1:782a3ddc329e 159 bool retval = (address != temp);
Sissors 1:782a3ddc329e 160 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 161 if (retval)
Sissors 1:782a3ddc329e 162 printf("IAP: Boundary violation\r\n");
Sissors 1:782a3ddc329e 163 #endif
Sissors 1:782a3ddc329e 164 return retval;
Sissors 1:782a3ddc329e 165 }
Sissors 1:782a3ddc329e 166
Sissors 1:782a3ddc329e 167 /* Check if address is correctly aligned
Sissors 1:782a3ddc329e 168 Returns true on violation */
Sissors 1:782a3ddc329e 169 __attribute__((section(".ARM.__at_0x11700"))) bool check_align(int address) {
Sissors 1:782a3ddc329e 170 bool retval = address & 0x03;
Sissors 1:782a3ddc329e 171 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 172 if (retval)
Sissors 1:782a3ddc329e 173 printf("IAP: Alignment violation\r\n");
Sissors 1:782a3ddc329e 174 #endif
Sissors 1:782a3ddc329e 175 return retval;
Sissors 1:782a3ddc329e 176 }
Sissors 1:782a3ddc329e 177
Sissors 1:782a3ddc329e 178 /* Check if an area of flash memory is erased
Sissors 1:782a3ddc329e 179 Returns error code or Success (in case of fully erased) */
Sissors 1:782a3ddc329e 180 __attribute__((section(".ARM.__at_0x11800"))) IAPCode verify_erased(int address, unsigned int length) {
Sissors 1:782a3ddc329e 181 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 182 printf("IAP: Verify erased at %x with length %d\r\n", address, length);
Sissors 1:782a3ddc329e 183 #endif
Sissors 1:782a3ddc329e 184
Sissors 1:782a3ddc329e 185 if (check_align(address))
Sissors 1:782a3ddc329e 186 return AlignError;
Sissors 1:782a3ddc329e 187
Sissors 1:782a3ddc329e 188 //Setup command
Sissors 1:782a3ddc329e 189 FTFA->FCCOB0 = Read1s;
Sissors 1:782a3ddc329e 190 FTFA->FCCOB1 = (address >> 16) & 0xFF;
Sissors 1:782a3ddc329e 191 FTFA->FCCOB2 = (address >> 8) & 0xFF;
Sissors 1:782a3ddc329e 192 FTFA->FCCOB3 = address & 0xFF;
Sissors 1:782a3ddc329e 193 FTFA->FCCOB4 = (length >> 10) & 0xFF;
Sissors 1:782a3ddc329e 194 FTFA->FCCOB5 = (length >> 2) & 0xFF;
Sissors 1:782a3ddc329e 195 FTFA->FCCOB6 = 0;
Sissors 1:782a3ddc329e 196
Sissors 1:782a3ddc329e 197 run_command();
Sissors 1:782a3ddc329e 198
Sissors 1:782a3ddc329e 199 IAPCode retval = check_error();
Sissors 1:782a3ddc329e 200 if (retval == RuntimeError) {
Sissors 1:782a3ddc329e 201 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 202 printf("IAP: Flash was not erased\r\n");
Sissors 1:782a3ddc329e 203 #endif
Sissors 1:782a3ddc329e 204 return EraseError;
Sissors 1:782a3ddc329e 205 }
Sissors 1:782a3ddc329e 206 return retval;
Sissors 1:782a3ddc329e 207
Sissors 1:782a3ddc329e 208 }
Sissors 1:782a3ddc329e 209
Sissors 1:782a3ddc329e 210 /* Check if an error occured
Sissors 1:782a3ddc329e 211 Returns error code or Success*/
Sissors 1:782a3ddc329e 212 __attribute__((section(".ARM.__at_0x11900"))) IAPCode check_error(void) {
Sissors 1:782a3ddc329e 213 if (FTFA->FSTAT & FTFA_FSTAT_FPVIOL_MASK) {
Sissors 1:782a3ddc329e 214 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 215 printf("IAP: Protection violation\r\n");
Sissors 1:782a3ddc329e 216 #endif
Sissors 1:782a3ddc329e 217 return ProtectionError;
Sissors 1:782a3ddc329e 218 }
Sissors 1:782a3ddc329e 219 if (FTFA->FSTAT & FTFA_FSTAT_ACCERR_MASK) {
Sissors 1:782a3ddc329e 220 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 221 printf("IAP: Flash access error\r\n");
Sissors 1:782a3ddc329e 222 #endif
Sissors 1:782a3ddc329e 223 return AccessError;
Sissors 1:782a3ddc329e 224 }
Sissors 1:782a3ddc329e 225 if (FTFA->FSTAT & FTFA_FSTAT_RDCOLERR_MASK) {
Sissors 1:782a3ddc329e 226 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 227 printf("IAP: Collision error\r\n");
Sissors 1:782a3ddc329e 228 #endif
Sissors 1:782a3ddc329e 229 return CollisionError;
Sissors 1:782a3ddc329e 230 }
Sissors 1:782a3ddc329e 231 if (FTFA->FSTAT & FTFA_FSTAT_MGSTAT0_MASK) {
Sissors 1:782a3ddc329e 232 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 233 printf("IAP: Runtime error\r\n");
Sissors 1:782a3ddc329e 234 #endif
Sissors 1:782a3ddc329e 235 return RuntimeError;
Sissors 1:782a3ddc329e 236 }
Sissors 1:782a3ddc329e 237 #ifdef IAPDEBUG
Sissors 1:782a3ddc329e 238 printf("IAP: No error reported\r\n");
Sissors 1:782a3ddc329e 239 #endif
Sissors 1:782a3ddc329e 240 return Success;
Sissors 1:782a3ddc329e 241 }