Example of a Serial bootloader for the K64F platform

Dependencies:   mbed

Introduction

Once in a while on mbed questions arise regarding making your own bootloader. So I decided to make one. Due to the device specific parts in the code this only works on the K64F. Porting it to other Freescale devices should be fairly straightforward. Devices from other manufacturers will require completely different code, although it could still be used as a basic guide.

Disclaimer

Bricking your device!

This rewrites your flash memory, including the security byte which determines among others the access external programmers get, and which can completely block future access.

Additionally this is an example, it is not intended to be used in a mission critical system in a nuclear power plant.

Requirements

I set some requirements for this bootloader. To start with, it should work when build from the online compiler. So no special compiler options to place functions in the correct sections. Even more important, it should be able to load regular programs from the mbed online compiler.

There are different options to get the data to the mbed, but for simplicity the goal is to just use the USBTX/USBRX serial pins. Finally there are some different types of bootloaders, for example they can launch at start up. That is not going to be easy with the requirement to also be able to load regular programs, although it might be possible, but it is not a requirement for here. In principle it is sufficient if the new user program has to call the bootloader, but an alternative is presented which allows a button to be able to enter bootloader mode, regardless of user code (not completely regardless, but it does not need to explictitly add any code for the bootloader).

Bootloader options

When a bootloader recides in the first part of the flash memory, it will run at start up and allows you to always upload a new program, regardless of your old program (it is always possible to brick it). However this means your user program needs to be compiled with an offset, and the online compiler cannot do this.

The alternative is putting the bootloader at the end of the flash memory. From here it can reprogram the first parts of the flash memory, but it will not automatically start up. Still this options has been used here.

The bootloader

The first requirement is placing the functions at the end of the flash memory. The online compiler (and Keil, other compilers will have different options) uses the following syntax to place functions at an absolute location:

__attribute__((section(".ARM.__at_0x10000"))) void bootloader(void)

Here obviously the 0x10000 is the absolute address where the function is placed. You may realise this is in fact not the end of the flash memory on the K64F. You are correct, I randomly choose this and cannot be bothered to change it. It allows 64kB for your user program, which is sufficient for just trying it out.

You do not need to worry about placing it on top of other functions/values: The compiler checks this, and gives a clear error message on the location and sizes of the collision.

It is important to realise that you cannot depend on any functions in the 'regular' area of your flash! This can be reprogrammed to contain any value/data, so every single functions and variable used by your bootloader needs to be specifically placed in your bootloader section. This also means that we cannot use mbed library functions, unless you manage to move the entire mbed library. Which could be done for the C API functions, but for here we use an easier solution, we just code it ourselves :D.

Serial code

Using void setupserial(); the serial peripheral is set up. This assumes the clock is already running correctly, so it depends a bit on the user program, but for any normal mbed user program this will be the case. I started using the K20D50M serial_api.c file from the mbed library, which contains all registers which are being set (The K64F code uses the KSDK drivers, which besides harder to use also cannot be used, because they will not reside in the correct memory area). The code simply hardcodes some register values to be correct for 9600 baud rate at USBTX/USBRX.

void write(char *value); is used to write messages on the serial connection.

Flash programming

Now we can send serial data (and receiving it is also pretty straight forward), we need to be able to program or flash memory. We use this program for this: http://developer.mbed.org/users/Sissors/code/FreescaleIAP/. However also this needs to be placed in the correct flash location. Since I have not been able yet to do it in a nice way in the library, it was done here manually.

To program flash it first needs to be erased. This takes a relative large amount of time, and due to the setup of this bootloader we do not know beforehand how many sectors need to be erased. So it just erases 15 4kB sectors, allowing for a program size of 60kB.

Once erased, the program will wait until the first char arrived. From this moment on it will enter a loop where received chars are placed in a 16-byte buffer. In principle the K64F should be able to program per 8-byte, however this resulted in error statuses from the flash peripheral. Since it works fine with programming per 16-bytes we use this. Every time 16-new bytes have arrived they are programmed into the flash memory, and a counter is increased.

In the loop also a timeout counter runs. Once no new chars have arrived for a period of time it assumes the program has been completely sent. The remaining bytes in the buffer are programmed, and the MCU resets itself, loading automatically the new user program.

Non-Maskable Interrupt

All (mbed) Freescale MCUs have a NMI setup which is called when the correct pin is pulled to zero. This interrupt vector resides in bytes 8-9-10-11, and on the K64F is connected to SW3, which makes it easy to use. The code currently simply replaces those 4 bytes with the location of the bootloader function. This means that regardless of the user program, it will be modified to start the bootloader upon pressing SW3 on your board.

Usage

After loading this program on your K64F the regular way, it will directly start the bootloader. Later on you can enter the bootloader by pressing SW3, or call it from your user code (for this you need to make a function pointer towards the location of the bootloader).

I assume for this Teraterm is used, other terminal programs might have similar options. After confirming you want to start the bootloader by typing 'y', it will erase the current program. Next you need to send the binary intact to the K64F via Teraterm. You can NOT do this via drag and dropping files into it, Teraterm will mutilate them. Instead go to File > Send File, and locate your file. Next make sure the "Binary" option is checked. If everything goes correctly you see a stream '#'s appear in your serial window, followed by a "Done programming!". It resets itself and your bootloaded program should run directly.

The bootloader simply expects the binary data to be transmitted via the Serial connection, so any programs which does so will do.

Remarks and Comments

Since you cannot rely on any functions outside the bootloader, you have to write everything yourself, or move existing libraries one function at a time. This makes it easiest if you stick to simple peripherals, such as SPI or Serial. It might be doable to move USBDevice over, although it would be far from trivial. I wouldn't even consider starting with Ethernet.

But what if you really want to use Ethernet? Let your user program handle it. Downside is of course your user program needs to handle this, and you cannot use any random program. But one example would be loading the new binary via whatever means you want, and storing it on an external memory chip. Then your bootloader program only needs to read it from your memory chip back.

If your code needs to be able to call the bootloader, for example after you loaded it via Ethernet, you need to add a pointer to the bootloader in your code, even though your newly loaded code does not contain the bootloader itself. This can be done using:

void *(*bootloader)(void) = (void *(*)(void))0x10001;

This creates a bootloader point of type void bootloader(void), pointing at a bootloader at 0x10000. If your bootloader function is at another memory location: you need the memory location plus 1. After this your code can call the bootloader simply as a normal function: bootloader();.

If you want it to start always upon reset, you can overwrite the Reset Handler. This is located right before the NMI, at bytes 4-5-6-7. It should work the same way as doing it the NMI way, with two differences. First of all currently the code relies on the clock being set up correctly. That won't be the case. In addition I do not know if it will correctly be able to make new variables on the stack, without code having run to initialize that. (My guess: It won't and you need to do that manually in your code).

The second difference is that you of course still need to be able to run the user code, so it needs to store the old reset vector in another location.

Committer:
Sissors
Date:
Tue Dec 06 19:07:18 2016 +0000
Revision:
12:3d0391cda333
Parent:
11:a7a0730e20db
Disable IRQs during bootloader, and don't reenable them in FreescaleIAP.

Who changed what in which revision?

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