Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap On Kinetis Microcontrollers
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap On Kinetis Microcontrollers
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap On Kinetis Microcontrollers
1 Introduction 1 Introduction................................................................1
2 Application requirements
A reliable over-the-air system update process is required for
many applications. A mechanism is needed to update system
software/firmware remotely to fix bugs and make important
software improvements. Critical applications, such as
electrical metering, require extremely minimal system
downtime during updates, must be tolerant of communication
errors, and should have no risk of problems causing the system
to cease to operate. The update process must be tolerant of
difficult environments. In many applications with unreliable electrical power, the system update process must provide
consistent operation in the presence of power loss.
Updating system software remotely requires new code to be transmitted through a communication interface (for example,
Wi-Fi, Zigbee®, UART, SPI, I2C, USB, Ethernet, and so on). A software uploader program executing on the system must
receive and program the code to local memory. Typically, in-factory programming is accomplished with a local debug
connection like JTAG or Background Debug Mode (BDM), however these connections are not possible remotely. It is
economically infeasible to send a technician out to a system in the field for reprogramming. Therefore, a reliable over-the-air
update process is mandatory.
Disadvantages
• The main application must stop during the update process
• Not possible to go back to known working application
• May not be tolerant of power loss during the update process
Disadvantages
• Additional memory space required to store backup copy.
• Requires a bootloader (startup routine that selects which application to run).
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
2 Freescale Semiconductor, Inc.
Program flash swap overview
Disadvantages
• Additional flash memory space required to store backup copy.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 3
Program flash swap overview
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
4 Freescale Semiconductor, Inc.
Program flash swap overview
This command initializes the swap system. When issuing this command, you must provide an address for the flash swap
indicators (discussed below). This command sets the swap enable word and swap indicator address which is used by the
swap system. This step is required only once when for the first time a swap is performed.
Set Swap in Update Mode
This command programs the flash swap indicator value to indicate to the system that content updates of the upper block has
been planned. Additionally, it un-protects the sector holding the flash swap indicator within the nonactive block to allow it
to be erased. Erasing this sector during the Update (or Update-Erased) state is a requirement of the swap process.
Set Swap in Complete Mode
This command programs the flash swap indicator value to indicate to the system that reprogramming the nonactive block is
complete and is ready to swap the flash blocks. This command is to be issued after the contents of the nonactive (upper)
block are erased/reprogrammed as required to update the system software. This includes erasing the sector in the nonactive
block that contains the flash swap indicator.
Report Swap Status
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 5
Swap steps
This command checks the status of the swap system. It returns the current swap state, current swap block status (which
block is currently located at address 0x0000), the next swap block status (which block will be located at 0x0000 after a
system reset), and if there are any errors.
It is recommended to issue this command in the system initialization code that executes on a system reset. This will help the
system software identify if there were any errors with the swap system that could potentially be caused by power loss during
a swap command execution.
4 Swap steps
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
6 Freescale Semiconductor, Inc.
Swap steps
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 7
Swap steps
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
8 Freescale Semiconductor, Inc.
Swap details
5 Swap details
The flash swap indicators are used by the swap system to store codes that the system uses to ensure robust operation in the
presence of potential power losses during swap operations. The swap system programs the flash swap indicators with
specific codes at each step of the swap process. This provides the system with a tracking mechanism so that it can reliably
determine the swap status on a system reset.
On a reset, the swap system interrogates the swap indicators to determine the swap state and whether there was any
interruption during a swap flow. You have to do nothing with the swap indicators other than erase them during the Update
stage of the swap process.
How big are the flash swap indicators?
The system programmer does not need to program the flash swap indicators or scrutinize their contents because the flash
swap system handles this automatically. However, the swap state report mapping table in the reference manual provides
details of the codes programmed into the swap indicators. The table describes codes during each step in the swap process or
in cases where an error was detected (possible power loss during a swap).
Where to locate the flash swap indicators?
The system programmer specifies where the flash swap indicators are to be located when initializing the flash swap system.
Any flash sector can be used except the first two sectors which contain the vectors and flash configuration fields. Depending
on the device, the swap indicator must be 32-bit, 64-bit, or 128-bit aligned.
Consider for example a device that contains two p-flash blocks of 256 KB (block 0: 0x00000-0x3FFFF, block 1:
0x40000-0x7FFFF) and sector size of 2 KB (0x800). If the swap indicators are placed in the last sector of the flash blocks,
the flash swap system will designate 0x3F800 as the swap indicator location for block 0 and 0x7F800 as the swap indicator
location for block 1. In this example, the address provided to the swap system is simply 0x3F800.
NOTE
It is recommended to place the flash swap indicators in the last sector of the p-flash
block. This will allow the largest contiguous flash area for the application code.
Where is the flash swap indicator address (offset) stored?
The address of the flash swap indicators (offset address from the base of each block) is stored in an area of flash called the
program flash block 1 IFR.
Can the flash swap indicator location be changed?
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 9
Swap details
The flash swap indicator address cannot be modified unless the Erase All Blocks command is issued that clears the swap
system back to uninitialized (on supported devices). Therefore, it is important not to forget the address provided to the swap
system during swap initialization.
Flash Swap Indicator Protection
Once set, the flash swap indicator locations cannot be programmed by the user. Additionally, the sector containing the flash
swap indicator in the active block cannot be erased. The sector containing the flash swap indicator in the nonactive block
can only be erased when the swap system is in the Update or Update-Erased state. Be sure not to program over the swap
indicators as this causes an error.
Figure 6. When it is allowable to erase sectors other than the flash swap indicator
sectors
* You can erase any sector other than the swap indicator sectors at any time. However, you must not erase within the block
that you are currently executing. In rare cases in which you want to erase the active block, jump to a subroutine in RAM and
execute from RAM.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
10 Freescale Semiconductor, Inc.
Swap details
Reading the swap documentation may lead you to the question, “why is swap so complicated?” The answer is power loss
tolerance. In less robust systems, if the power is lost at a vulnerable time, the system could become corrupted. For systems
using the flash memory swap feature, if a power loss occurs during the swap process, the built-in system detects errors and
reliably determines the last known valid flash block configuration.
Are systems using swap limited to applications that fit within one of the two flash blocks (half the total flash memory
size)?
In most use cases, the system application will be limited to executing within one flash block (active block). However, the
swap system does not restrict code from executing in the nonactive block. Therefore, code can execute from either block at
any time.
It is possible to use advanced techniques to expand the size of the application over the size of half the p-flash and still use
flash swap. This would sacrifice some protection against problems, but gain more room for code. This is outside the scope of
this application note.
Can the sector with the flash swap indicator be used for data/program storage?
Typically, the user specifies an unused flash sector to place the flash swap indicators. However, the swap system will not
disturb the other data in the flash sector containing the indicators. So, that space can be used for program or data storage.
The space should be designated in the linker file accordingly. See Important considerations for details.
You must program the interrupt vector table and flash configuration fields (FSEC, FOPT, and so on) to the nonactive upper
block before swapping. If not, the vector table and flash configuration will not be set correctly in the new active block after
the swap. This will result in unwanted configurations and could potentially secure the device.
Project linker file considerations
No special considerations are required in the code. Just link your code as it would normally fill the lower block. It is
recommended to allocate a section in the linker file for the active (lower) flash swap indicator location to avoid the
application attempting to overwrite it.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 11
Multitasking application overview
Uploader Task
When activated, the uploader task performs the following:
• Issues command to put swap system to the Update state
• Erases the upper block sectors to be reprogrammed
• Receive new line of application code (from SCI, TCP/IP, and so on)
• Programs new line of code to the upper flash block
• Checks the code for errors
• Issues command to put swap in Complete state
• Performs software reset
• After the system is reset, the flash blocks are swapped and the new application boots up
• The process can be repeated again with the exact same steps
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
12 Freescale Semiconductor, Inc.
Software example
7 Software example
A software example is provided with this application note to demonstrate the swap feature. The example is not based on a
multi-tasking operating system.
7.2 Hardware
Kinetis K60 100 MHz MCU Module (TWR-K60N512) with MCU device marked 4N30D or later required. See note below.
NOTE
Kinetis 100 MHz revision 1.4 or later required. The mask set number marked on the
package is 4N30D. Subsequent revisions may be marked 5N30D, and so on. Revisions
earlier than revision 1.4, may not support swap or may disable the Erase-All-Blocks
command after swap initialized. Thus, on those early revisions, some debuggers will not
be able to re-download to flash if they rely on the Erase-All-Blocks command to pre-
erase the flash. Additionally, it was not possible to set the swap system back to
uninitialized on those early devices.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 13
Software example
7.3.1.1 PFlashGetSwapStatus
Located in the file PFlashGetSwapStatus.c. This function returns the swap state details and checks whether any errors were
detected in the swap state determination.
7.3.1.2 PFlashSwap
These are located in the files: PFlashSwap.c. This function performs the swap control commands.
7.4.1 Instructions
1. Download and install the demo software. Install to a directory of your choice. This directory will be referred to <Install
Dir>.
2. Plug in the USB cable between your computer and the board to provide power and debugger connection.
3. Open the PE Terminal Utility as described in the Quick Start Guide. Ensure the baud rate is 115200, no parity, and 8
bits. Click Open Serial Port.
4. Open IAR or CodeWarrior.
5. Open the swap demo project
IAR
• Open the workspace <Install Dir>\build\iar\swap_demo\swap_demo.eww
CodeWarrior
• Set the workspace to <Install Dir>\
• Import the project in the folder <Install Dir>\build\cw\swap_demo\
6. Select the target configuration
IAR
• Select the FLASH_512KB_PFLASH configuration.
CodeWarrior
• Select the MK60N512VMD100_INTERNAL_FLASH configuration
7. Compile the project
8. Download and Debug
IAR
• Click the Download and Debug button
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
14 Freescale Semiconductor, Inc.
Software example
CodeWarrior
• Open Debug Configurations
• Select k60-swap_demo_MK60N512VMD100_INTERNAL_FLASH_PnE_OSJTAG
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 15
Software example
NOTE
This example places the flash swap indicator in the last sector of each block.
Programming the flash swap indicator is not allowed, so when programming the
nonactive (upper) block, this location is avoided.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
16 Freescale Semiconductor, Inc.
Software example
2. The New Connection window will open. Under CodeWarrior Bareboard Debugging, select Hardware or Simulator.
3. Write in a name of your choice (e.g. K60_OSJTAG) for the Connection name and select K60DN512Z for System
type. Click Finish.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 17
Software example
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
18 Freescale Semiconductor, Inc.
Conclusion
8 Conclusion
Program Flash Memory Swap is one of the many attractive features of Freescale’s Kinetis microcontrollers. The Swap
feature is ideal for systems that require a very robust firmware update mechanism that provides the safety of a backup copy
and minimal application downtime. Program Flash Swap is simple to use and reduces software complexity. It is well suited
for multi-tasking operating systems such as Freescale’s complimentary MQX RTOS.
For further resources, please review the software example provided with this application note as well as the References
section.
9 References
1. Kinetis Microcontroller documentation, software, and tools available at http://www.freescale.com/kinetis.
2. Flash Driver Software for Kinetis Microcontrollers. Keyword search C90TFS_FLASH_DRIVER at http://
www.freescale.com.
3. Kinetis Sample Code. Keyword search KINETIS512_SC at http://www.freescale.com.
Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers,
Rev. 0, 06/2012
Freescale Semiconductor, Inc. 19
How to Reach Us: Information in this document is provided solely to enable system and software
implementers to use Freescale Semiconductors products. There are no express or implied
Home Page: copyright licenses granted hereunder to design or fabricate any integrated circuits or
www.freescale.com integrated circuits based on the information in this document.
Freescale Semiconductor reserves the right to make changes without further notice to any
Web Support: products herein. Freescale Semiconductor makes no warranty, representation, or
http://www.freescale.com/support guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor assume any liability arising out of the application or use of any
USA/Europe or Locations Not Listed: product or circuit, and specifically disclaims any liability, including without limitation
Freescale Semiconductor consequential or incidental damages. "Typical" parameters that may be provided in
Technical Information Center, EL516 Freescale Semiconductor data sheets and/or specifications can and do vary in different
applications and actual performance may vary over time. All operating parameters,
2100 East Elliot Road
including "Typicals", must be validated for each customer application by customer's
Tempe, Arizona 85284 technical experts. Freescale Semiconductor does not convey any license under its patent
+1-800-521-6274 or +1-480-768-2130 rights nor the rights of others. Freescale Semiconductor products are not designed,
www.freescale.com/support intended, or authorized for use as components in systems intended for surgical implant
into the body, or other applications intended to support or sustain life, or for any other
Europe, Middle East, and Africa: application in which failure of the Freescale Semiconductor product could create a
Freescale Halbleiter Deutschland GmbH situation where personal injury or death may occur. Should Buyer purchase or use
Technical Information Center Freescale Semiconductor products for any such unintended or unauthorized application,
Buyer shall indemnify Freescale Semiconductor and its officers, employees, subsidiaries,
Schatzbogen 7 affiliates, and distributors harmless against all claims, costs, damages, and expenses, and
81829 Muenchen, Germany reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury
+44 1296 380 456 (English) or death associated with such unintended or unauthorized use, even if such claims alleges
+46 8 52200080 (English) that Freescale Semiconductor was negligent regarding the design or manufacture of
the part.
+49 89 92103 559 (German)
+33 1 69 35 48 48 (French) RoHS-compliant and/or Pb-free versions of Freescale products have the functionality and
electrical characteristics as their non-RoHS-complaint and/or non-Pb-free counterparts.
www.freescale.com/support
For further information, see http://www.freescale.com or contact your Freescale
Japan: sales representative.
Freescale Semiconductor Japan Ltd. For information on Freescale's Environmental Products program, go to
Headquarters http://www.freescale.com/epp.
ARCO Tower 15F Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
1-8-1, Shimo-Meguro, Meguro-ku, All other product or service names are the property of their respective owners.
Tokyo 153-0064 © 2012 Freescale Semiconductor, Inc.
Japan
0120 191014 or +81 3 5437 9125
[email protected]
Asia/Pacific:
Freescale Semiconductor China Ltd.
Exchange Building 23F
No. 118 Jianguo Road
Chaoyang District
Beijing 100022
China
+86 10 5879 8000
[email protected]