Flash Programming Serial (Old)
Flash Programming Serial (Old)
Trey German, Salvatore Pezzino, Shashank Kulkarni, Strong Zhang and Terry Lin
ABSTRACT
Often times, embedded processors must be programmed in situations where JTAG is not a viable option
for programming the target device. When this is the case, the engineer must rely on some type of serial
programming solution. C2000 devices aid in this endeavor through their inclusion of several program
loading utilities included in ROM. These utilities are useful, but only solve half of the programming problem
because they only allow loading program code to RAM. This application report builds on these ROM
loaders by introducing the idea of a flash kernel. A flash kernel is loaded using one of the ROM loaders
and is then executed and used to program the target device’s flash with the end application. This
document details one possible implementation for C2000 devices and provides PC utilities to evaluate the
solution with.
Contents
1 Introduction ................................................................................................................... 2
2 Programming Fundamentals ............................................................................................... 2
3 ROM Bootloader ............................................................................................................ 3
4 Flash Kernel A ............................................................................................................... 5
5 Flash Kernel B ............................................................................................................... 6
6 Example Implementation .................................................................................................. 11
7 References .................................................................................................................. 14
List of Figures
1 F2802x Boot Flowchart ..................................................................................................... 3
2 Bootloader Data Structure .................................................................................................. 5
3 serial_flash_programmer Prompting for Next Command ............................................................. 14
List of Tables
1 Device Standalone Boot Modes F2802x Example ...................................................................... 3
2 Packet Format................................................................................................................ 8
3 ACK/NAK Values ............................................................................................................ 8
4 CPU1 Kernel Commands ................................................................................................... 8
5 CPU2 Kernel Commands ................................................................................................... 9
6 Erase Packet................................................................................................................ 10
7 Unlock Packet .............................................................................................................. 10
8 Run Packet .................................................................................................................. 10
9 Status Codes................................................................................................................ 11
Trademarks
C2000, controlSUITE, Code Composer Studio are trademarks of Texas Instruments.
Microsoft Visual Studio is a registered trademark of Microsoft Corporation in the United States and/or
other countries.
All other trademarks are the property of their respective owners.
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 1
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
Introduction www.ti.com
1 Introduction
As applications become more and more complex, the need to fix bugs, add features, and otherwise modify
embedded firmware is increasingly critical in end applications. Often times, end equipment customers are
asked to do these firmware upgrades themselves in order to save the manufacturer maintenance costs.
Enabling functionality like this can easily and cheaply be accomplished through the use of bootloaders.
A bootloader is a small piece of code that resides in the target device’s memory that allows it to load and
execute code from an external source. In most cases, a communication peripheral such as Universal
Asynchronous Receiver/Transmitter (UART) or Controller Area Network (CAN) is used to load code into
the device. This allows the end customer to use a more common communications channel to upgrade
their embedded device’s firmware rather than JTAG, which requires an expensive specialized tool.
C2000 devices partially solve the problem of boot-loading by including some basic loading utilities in
ROM. Depending on the device and the communications peripherals present, code can be loaded into
RAM on C2000 devices using: UART, Serial Peripheral Interface (SPI), Inter-Integrated Circuit (I2C),
Ethernet, CAN, and even a parallel mode using General-Purpose Input/Outputs (GPIOs). A subset of
these loaders is present in every C2000 device and they are very easy to use, but they can only load code
into RAM. How does one bridge the gap and program their application code into non-volatile memory?
This application report aims to solve this problem by introducing the idea of a flash kernel. The concept of
a flash kernel is not new or unique. This technique has been used time and time again, but this document
discusses the specifics of the kernels and the host application tool found in controlSUITE™. While this
implementation is targeted at C2000 devices using the Serial Communications Interface (SCI) UART
peripheral, the same principles apply to all devices in the C2000 product line and all communications
options supported in the ROM loaders. A command line tool is provided to parse and transmit the
application from the host PC (Windows and Linux) to the embedded device.
2 Programming Fundamentals
Before programming a device, you need to understand how the non-volatile memory of C2000 devices
works. For the most part, all C2000 devices use flash as their non-volatile memory technology. Flash is a
non-volatile memory technology that allows you to easily erase and program your memory. Erase
operations set all of the bits in a sector to ‘1’ while programming operations selectively clear bits to ‘0’.
This is one of the main limitations of flash, it can only be erased a sector at a time.
The underlying principle for how a flash memory functions is the same between different devices, families,
and even different companies, but the implementation varies quite a bit. flash memory comes in many
variants, each with its own design tradeoffs. For example, some flash may operate faster but may be
larger and more expensive to manufacture. There are also differences in terms of programming interface.
Some flash memories have dedicated hardware that is used to program and erase flash via a set of
registers, while others use algorithms, which run on the CPU in order to perform flash operations.
In all cases, flash operations on C2000 devices are performed using the CPU. Algorithms are loaded into
RAM and executed by the CPU to perform ANY flash operation. For example, erasing or programming the
flash of a C2000 device with Code Composer Studio™ software is actually loading flash algorithms into
RAM and letting the processor execute them. There are no special JTAG commands that are used. Flash
operations are always performed using the same underlying software, the flash API. Because flash
operations are always done using the CPU, this opens a world of possibilities for device programming.
Any way that information can enter the chip can be used to load code into the device for flash
programming.
2 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com ROM Bootloader
3 ROM Bootloader
3.1 Functionality
To begin, the device boots and decides if it should execute code already programmed into the device or
load in code using one of the loaders in ROM.
NOTE: The material in this section is based on the 2802x, including boot flow, pin numbers, boot
modes, and so forth. Specific information for a particular device can be found in the Boot
ROM section of the device-specific technical reference manual (TRM).
Figure 1 describes the sequence of events that take place just after the controller is reset.
Reset
(Power-On Reset or Emulator Not Connected
Warm Reset)
Silicon Sets the Following: Determine Boot Mode
PIE Disabled (ENPIE = 0) TRST = 1? Based on 2 GPIO Pins
VMAP = 1 and 2 OTP Loations
OBJMODE = 0 (OTP_KEY and
AMODE = 0 OTP_BMODE)
M0M1MAP = 1
Call Device_cal()
PLLSTST[DIVSEL]=3
The ultimate goal is to be able to program the flash on a blank device without any external hardware, so
this application report focuses on the boot execution path of when the emulator is not connected (TRST
== 0 as standalone boot).
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 3
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
ROM Bootloader www.ti.com
After the boot ROM readies the device for use, it decides where it should start executing. In the case of a
standalone boot, it does this by examining the state of two GPIOs (for example, GPIO 34 and 37) and in
some cases two values programmed into one-time programmable (OTP). In the implementation described
in this application report, the SCI (UART) loader is used, so at power up GPIO34 must be forced high and
GPIO37 must be forced low. If this is the case when the device boots, the SCI loader in ROM begins
executing and waits for a character to be received in order to determine the baud rate where the
communications will occur at. At this point, the device is ready to receive the code from the host.
The bootloader requires data to be presented to it in a specific structure. This structure is common to all
bootloaders and it is described in detail in the Bootloader Data Stream Structure section of [1]. You can
easily generate your application in this format by using the hex2000 utility included with the TI C2000
compiler. This file format can even be generated as part of the Code Composer Studio™ build process by
adding a build step with the following options:
"${CG_TOOL_HEX}" "${BuildArtifactFileName}" -boot -sci8 -a -o "${BuildArtifactFileBaseName}.txt"
Alternatively, you can use the TI hex2000 utility to convert COFF .out files into the correct boot hex format.
hex2000.exe -boot -sci8 -a -o <file.txt> <file.out>
4 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com Flash Kernel A
Auto-Baud Lock
character - ‘a’ or ‘A’
Incorrect
Exit Bootloader Basic Value - 0x08AA for
Key SCI Boot
Correct
Key
Entry Address -
Two Words - MSW First
Size is
Clean Up and Exit Block Size - One Word -
Zero Number of Words in Block
Size is
Non-Zero
4 Flash Kernel A
This flash kernel runs on:
• F2802x
• F2803x
• F2805x
• F2806x
• F2833x (4)
4.1 Implementation
This flash kernel is actually surprisingly similar to the SCI loader in ROM. In fact, the flash kernel was
based off the SCI loader sources. To enable this code to erase and program flash, the flash API must be
incorporated into the SCI loader, which was accomplished by linking against the flash API contained in
ROM. Before any application data is received, the flash kernel erases the flash of the device readying it
for programming. Instead of using pointers to copy received data to the proper location in RAM, a buffer
was added that holds contiguous pieces of application code. When the buffer is full or a new block of non-
contiguous data is detected, the code in the buffer is programmed. This continues until the entire
application is received.
(4)
Does not have flash API in ROM.
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 5
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
Flash Kernel B www.ti.com
The protocol used to communicate the application data has been slightly modified from the ROM SCI
loader protocol. This was done to improve the speed of programming while also ensuring robust
communications. When writing PC side loader applications it was found that most of the time is spent not
transferring data, but waiting for the data to propagate through the different layers of the operating system.
This problem is compounded by the fact that data must be sent a single byte at a time with the stock SCI
loader (due to the echo based flow control), so every byte incurs the OS transport delay. The flash kernel
uses the same protocol but drops the echo flow control for a checksum that is sent after every block of
data. This allows the PC side application to send many bytes at a time through the different layers of the
operating system, substantially decreasing the latency of communications.
This flash kernel can be used with a CSM locked device. If the device is locked, the serial flash
programmer can still be used by loading the flash kernel into unsecure RAM and modifying the kernel to
unlock the device before it erases and programs the flash. In that case, CsmUnlock() should be modified
to write the correct CSM passwords to the CSM registers. This will unlock the device. Additionally, the
flash kernel should be linked to run from unsecure RAM.
5 Flash Kernel B
This Flash Kernel B runs on:
• F2807x
• F2837xD
• F2837xS
• F28004x
•
6 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com Flash Kernel B
5.1 Implementation
This flash kernel is an enhanced version of Flash Kernel A. It has increased functionality and is more
suitable for a broader flash programming solution.
Flash Kernel B gives the user flexibility with a variety of functions to perform on the device including
device firmware upgrade (DFU) (that erases the flash and loads and programs an application into flash), a
strict erase operation, verifies flash contents, unlocks dual code security module (DCSM), runs the device
and resets the device. For the F2837xD device, two kernels are provided: one for each core and some
additional functionality for CPU1 that is used to boot CPU2 to SCI boot mode in order to load its kernel in
the same way CPU1 does. More details and an example using the F2837xD are provided in Section 6.2.4.
Functions of Flash Kernel B:
• Device Firmware Upgrade (DFU)
• Erase
• Verify
• Unlock Zone 1
• Unlock Zone 2
• Run
• Reset
Flash Kernel B is a more robust kernel than Kernel A. It communicates with the host PC application
provided in controSUITE (controlSUITE/device_support/~Utilities/serial_flash_programmer) and provides
feedback to the host on the receiving of packets and completion of commands given to it.
After loading the kernel into RAM and executing it via the SCI bootloader, the kernel first initializes the
PLLs of the device, initializes SCIA, and seizes the flash pump, if necessary. It then waits for an 'a' or ‘A’
from the host in order to perform an autobaud lock with the host. After this, the kernel begins a while loop,
which waits on commands from the host, executes the commands, and sends a status packet back to the
host. This while loop breaks when a Run or Reset command is sent. Commands are sent in a packet
described in Table 2 and each packet is either acknowledged or not-acknowledged. All commands, except
for Run and Reset, send a packet after completion with the status of the operation. The status packet
sends a 16-bit status code and 32-bit address. In case of an error, the address in the data specifies the
address of the first error. In case of NO_ERROR, the address is 0x12345678.
In the case of a DFU, the kernel receives a file in the hex boot format byte-by-byte from the SCI module
and calculates a checksum for the block size. After receiving the block of data, it sends back the
checksum. This helps to increase performance. After receiving a block of data and storing it in a buffer,
the kernel erases the sector if it has not been previously erased, and programs the data into flash at the
correct address with ECC enabled using the F021_api_f2837xD_C28x.lib. Afterwards, it verifies that the
data and ECC were programmed correctly into flash. This kernel only erases sectors that are needed to
program the application and data into flash. This is different from Flash Kernel A that erases the entire
flash at the start of the kernel. However, Flash Kernel B provides an erase function independent of the
DFU, which gives the user the ability to erase specific sectors or the entire flash of the device.
Similarly, the verify operation receives a file in the hex boot format and in place of erasing and
programming the flash, it only verifies the contents of the flash.
Section 5.1.1 details the packet format, commands, and protocols. All command packets except for DFU
and Reset require data to be sent to the kernel, which is used for that command. The details discussed in
Section 5.1.2 and Section 5.1.3 are for the F2837xD flash kernels for CPU1 and CPU2, respectively. They
have identical functionality except for two additional commands that CPU1 can process in order to boot
CPU2. However, the commands have different values for CPU1 and CPU2. This helps to ensure
correctness when using the flash kernels for flash solutions. Single core devices (F2807x, F2837xS,
F28004x) accept CPU1 commands minus the two boot CPU2 commands.
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 7
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
Flash Kernel B www.ti.com
The host and the device both send packets a word at a time (16-bits), the LSB followed by the MSB. Both
the host and device respond to a packet with an ACK or NAK.
8 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com Flash Kernel B
(1)
This command is not available to F2807x and F2837xS single core device kernels.
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 9
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
Flash Kernel B www.ti.com
• Unlock
– 1st 32 bits is Key 1
– 2nd 32 bits is Key 2
– 3rd 32 bits is Key 3
– 4th 32 bits is Key 4
• Run
– 32-bit address
10 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com Example Implementation
6 Example Implementation
The kernels described above are available in controlSUITE under examples folder for the specific device
within the \device_support folder. The host application is found in the \device_support\~Utilities folder in
controlSUITE. The source and executable are found in the serial_flash_programmer folder. This section
details the serial_flash_programmer: how to build, run and use it with Flash Kernel A and B.
NOTE: The flash kernel of the appropriate device must be supplied to the tool being used to
program the flash. The serial_flash_programmer starts the same way independent of the
kernel or device. It first loads the kernel to the device which is using the SCI bootloader.
After this, the tool’s functionality differs depending on the device and kernel being used.
6.1.1 Kernels
The source files and project files for Code Composer Studio (CCS) are provided in contolSUITE
(www.ti.com/controlsuite), in the corresponding device’s device support directory. Load the project into
CCS and build the project. In these projects is a post-build step which converts the compiled and linked
.out file to the correct boot hex format needed for the serial_flash_prorgammer and saves it as the
example name with a .txt file extension.
6.1.2 Hardware
After building the kernels in CCS, it is important to setup the device correctly to be able to communicate
with the host PC running the serial_flash_programmer. The first thing to do is make sure the boot mode
pins are configured properly to boot the device to SCI boot mode (see Section 3.1). Next, connect the
appropriate SCI boot loader GPIO pins to the Rx and Tx pins that are connected to the host PC COM
port. A transceiver is often needed in order to convert a Virtual COM port from the PC to two GPIO pins,
which can connect to the device. On some systems, like the controlCARD, an FTDI chip can be used to
connect the GPIO pins used for SCI communication via a USB Virtual COM port. In this case, the PC
must connect to the mini-usb on the device and use channel B of the FTDI to connect to the GPIO pins.
After the hardware is setup correctly to communicate with the host, reset the device. This should boot the
device to SCI boot mode.
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 11
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
Example Implementation www.ti.com
6.2.1 Overview
The command line PC utility is a lightweight (~128KB executable) programming solution that can easily be
incorporated into scripting environments for applications like production line programming. It was written
using Microsoft Visual Studio® in C++. The project and its source can be found in controlSUITE
(www.ti.com/controlsuite) in the device_support\~Utilities\serial_flash_programmer folder.
To use this tool to program the C2000 device, ensure that the target board has been reset and is currently
in the SCI boot mode and connected to the PC COM port. Below describes the command line usage of
the tool:
-d, -k, -a, -p are mandatory parameters. If the baudrate is omitted, the communication will occur at 9600
baud.
NOTE: Both the flash kernels and flash application MUST be in the SCI8 boot format. This was
discussed earlier in Section 3.1 and can be generated from the OUT file using the hex2000
utility.
NOTE: If Microsoft Visual Studio is not installed, a free version of Microsoft Visual Studio express
can be found here.
12 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com Example Implementation
(b) Example:
This will first load the f28069_flash_kernel into RAM of the device using the bootloader. Then, the kernel
will execute and load and program flash with the file specified by the ‘-a’ command line argument.
This will automatically connect to the device, perform an autobaud lock, and download the CPU1
kernel into RAM and execute it. Now, the CPU1 kernel is running and waiting for a packet from the
host.
3. The serial_flash_programmer prints the options to the screen to choose from that will be sent to the
device kernel (see Figure 3). Select the appropriate number and then provide any necessary
information when asked for that command (described in Section 5.1).
SPRABV4B – March 2014 – Revised February 2017 Serial Flash Programming of C2000™ Microcontrollers 13
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
References www.ti.com
7 References
1. TMS320x2802x Piccolo Boot ROM Reference Guide (SPRUFN6)
2. ROM Code and Peripheral Booting section from the TMS320F2837xD Dual-Core Delfino
Microcontrollers Technical Reference Manual (SPRUHM8)
3. Piccolo Flash API User’s Guide - located within controlSUITE at:
(/controlSUITE/libs/utilities/flash_api/DEVICE/VERSION/doc)
4. C2000 F021 Flash API Reference Guide (SPNU595)
5. TMS320C28x Assembly Language Tools User's Guide (SPRU513)
14 Serial Flash Programming of C2000™ Microcontrollers SPRABV4B – March 2014 – Revised February 2017
Submit Documentation Feedback
Copyright © 2014–2017, Texas Instruments Incorporated
www.ti.com Revision History
Revision History
NOTE: Page numbers for previous revisions may differ from page numbers in the current version.
Texas Instruments Incorporated (‘TI”) technical, application or other design advice, services or information, including, but not limited to,
reference designs and materials relating to evaluation modules, (collectively, “TI Resources”) are intended to assist designers who are
developing applications that incorporate TI products; by downloading, accessing or using any particular TI Resource in any way, you
(individually or, if you are acting on behalf of a company, your company) agree to use it solely for this purpose and subject to the terms of
this Notice.
TI’s provision of TI Resources does not expand or otherwise alter TI’s applicable published warranties or warranty disclaimers for TI
products, and no additional obligations or liabilities arise from TI providing such TI Resources. TI reserves the right to make corrections,
enhancements, improvements and other changes to its TI Resources.
You understand and agree that you remain responsible for using your independent analysis, evaluation and judgment in designing your
applications and that you have full and exclusive responsibility to assure the safety of your applications and compliance of your applications
(and of all TI products used in or for your applications) with all applicable regulations, laws and other applicable requirements. You
represent that, with respect to your applications, you have all the necessary expertise to create and implement safeguards that (1)
anticipate dangerous consequences of failures, (2) monitor failures and their consequences, and (3) lessen the likelihood of failures that
might cause harm and take appropriate actions. You agree that prior to using or distributing any applications that include TI products, you
will thoroughly test such applications and the functionality of such TI products as used in such applications. TI has not conducted any
testing other than that specifically described in the published documentation for a particular TI Resource.
You are authorized to use, copy and modify any individual TI Resource only in connection with the development of applications that include
the TI product(s) identified in such TI Resource. NO OTHER LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE TO
ANY OTHER TI INTELLECTUAL PROPERTY RIGHT, AND NO LICENSE TO ANY TECHNOLOGY OR INTELLECTUAL PROPERTY
RIGHT OF TI OR ANY THIRD PARTY IS GRANTED HEREIN, including but not limited to any patent right, copyright, mask work right, or
other intellectual property right relating to any combination, machine, or process in which TI products or services are used. Information
regarding or referencing third-party products or services does not constitute a license to use such products or services, or a warranty or
endorsement thereof. Use of TI Resources may require a license from a third party under the patents or other intellectual property of the
third party, or a license from TI under the patents or other intellectual property of TI.
TI RESOURCES ARE PROVIDED “AS IS” AND WITH ALL FAULTS. TI DISCLAIMS ALL OTHER WARRANTIES OR
REPRESENTATIONS, EXPRESS OR IMPLIED, REGARDING TI RESOURCES OR USE THEREOF, INCLUDING BUT NOT LIMITED TO
ACCURACY OR COMPLETENESS, TITLE, ANY EPIDEMIC FAILURE WARRANTY AND ANY IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL
PROPERTY RIGHTS.
TI SHALL NOT BE LIABLE FOR AND SHALL NOT DEFEND OR INDEMNIFY YOU AGAINST ANY CLAIM, INCLUDING BUT NOT
LIMITED TO ANY INFRINGEMENT CLAIM THAT RELATES TO OR IS BASED ON ANY COMBINATION OF PRODUCTS EVEN IF
DESCRIBED IN TI RESOURCES OR OTHERWISE. IN NO EVENT SHALL TI BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL,
COLLATERAL, INDIRECT, PUNITIVE, INCIDENTAL, CONSEQUENTIAL OR EXEMPLARY DAMAGES IN CONNECTION WITH OR
ARISING OUT OF TI RESOURCES OR USE THEREOF, AND REGARDLESS OF WHETHER TI HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
You agree to fully indemnify TI and its representatives against any damages, costs, losses, and/or liabilities arising out of your non-
compliance with the terms and provisions of this Notice.
This Notice applies to TI Resources. Additional terms apply to the use and purchase of certain types of materials, TI products and services.
These include; without limitation, TI’s standard terms for semiconductor products http://www.ti.com/sc/docs/stdterms.htm), evaluation
modules, and samples (http://www.ti.com/sc/docs/sampterms.htm).
Mailing Address: Texas Instruments, Post Office Box 655303, Dallas, Texas 75265
Copyright © 2017, Texas Instruments Incorporated