HT66FB550 How to Update the Flash Program Introduction

HT66FB550 How to update the Flash ROM using the USB Module and IAP Function
HT66FB550 How to Update the Flash Program
Memory using the USB Module and IAP Function
D/N: HA0345E
Introduction
The HT6xFB5x0 series are Flash type Program Memory microcontrollers which, in
addition to their ISP function, also have an additional IAP function. IAP, which stands for
In Application Programming, can update a part of data stored in the program memory,
such as an array storing constant values, using its internal program, without requiring an
external Writer. The following content describes how to update the Flash Program
Memory using the USB module and IAP function in the HT66FB550.
Theory of Operation
This example uses the USB module in the HT66FB550 to send a data update command or
read data command to the Flash Program Memory space of the specified addresses via a PC.
It then uses the IAP function to write the updated data received from the PC to the Flash
Program Memory, or to read the data from Flash Program Memory and return to the AP.
IAP Function
This device offers an IAP function to implement erasing, writing or reading the contents of
the Flash Program Memory using a series of registers specifically provided for this purpose.
The HT66FB550 includes several registers related with read or write operations on the
Flash Program Memory. The address registers are named FARL and FARH and the data
registers are named FDnL and FDnH, where n=0~3. There are also two control registers
named FCR and FRCR. When data is written to the data registers, it is necessary to write
the low byte first and then the high byte. After the data is written to the data register high
byte, the address register will automatically be incremented by 1.
As the FARL and FDnL registers are located in Bank 0, they can be directly accessed in the
same way as any other Special Function Register. However, the FARH, FDnH and FCR
registers, being located in Bank 1, cannot be addressed directly and can only be read from
or written to indirectly using the MP1 Memory Pointer and Indirect Addressing Register,
IAR1.
How to update the Flash ROM using the USB Module and IAP Function
Operating Principles
IAP programming is used mainly to implement erase, write and read operations to the
Flash Program Memory space. It is used to update data to the Flash Program Memory
using the USB module and IAP function. In this application, the HT6xFB5x0 device side
program will define a 16-byte constant array, which is used to store LED display data. The
Program Memory space occupied by the array is the area to be updated. The AP will send
a Read Status command, the specific format of which refers to the “Communication
Protocol" section, to read the address range of the area from the device and display them
in the ADDR of the AP. The AP can update the Flash Memory using its USB module, and
then display the updated data on the LED display.
First, the AP of the PC specifies the program read and erase addresses in the area to be
updated, and then a command is sent to the device using the transmission method
controlled by endpoint 1, such as Get Report or Set Report. If the command is a program
command, then the data to be updated, which is input from the AP interface, will be sent
to the device via endpoint 1 in the interrupt transmission method, and written to the Flash
Program Memory using the IAP function. If it is a read command, the Flash Program
Memory data of the specified addresses will be returned to the AP via endpoint 2 using
the interrupt transmission method.
Communication Protocol
Erase Command (Action_ Erase =0x01)
Setup
OUT
Setup
IN
Byte 0
21
Action_
Erase
A1
Action_
Erase
Byte 1
09
Byte 2
00
Byte 3
03
Byte 4
02
Byte 6
08
00
Byte 5
00
Page
Count
00
08
Byte 7
00
Check
Sum
00
StartAddrH
StartAddrL
Bytes_S
Bytes_E
01
Verify
Checksum
00
03
00
00
00
00
00
00
00
 Action_ Erase (0x01)
This command will erase a certain area of Flash Program Memory data in the device.
 StartAddrH/StartAddrL
The high byte and low byte of the start address to be erased.
 Bytes_S
The erased byte number of the first page (if only one page, it is the home page). The
erase operation is executed according to the Flash Program Memory address order.
The address is incremented by 1 and a word will be erased correspondingly. Therefore,
the value is an even number of 2~64 (maximum page size), including 2 and 64.
 Bytes_E
The erased byte number of the last page, the value is also an even number of 2~64.
 PageCount
The page number to be erased, including the first and last page.
 CheckSum
The checksum, which is calculated by the AP (invert the sum of the first 7bytes and
add one), is compared with the checksum, which is returned from the GetReport stage
and calculated by the device. If they are different, then the data is incorrect and the
Erase, Read, Program and other command operations will not be executed.
2
How to update the Flash ROM using the USB Module and IAP Function
 VerifyChecksum
The checksum calculated by the device, i.e. in the SetReport stage invert the sum of
the first 7bytes and add one.
Read Command (Action_ Read =0x02)
Byte 0
Byte 1
Byte 2
Byte 3
Byte 4
Byte 5
Byte 6
21
09
00
03
02
00
08
00
OUT
Action_ Read
Start
AddrH
StartAddrL
Bytes_S
Bytes_E
Page
Count
00
Check
Sum
Setup
A1
01
00
03
00
00
08
00
Action_ Read
Verify
Checksum
00
00
00
00
00
00
Setup
IN
IN
(EP2)
Byte 7
64-byte
 Action_ Read (0x02)
This command will read the Flash Program Memory data in the device.
When the AP performs a verify and blank and check of the checksum, send the read
command to the device too.
Program Command (Action_ Program =0x03)
Byte 0
Byte 1
Byte 2
Byte 3
Byte 4
Byte 5
Byte 6
21
09
00
03
02
00
08
00
Action_Program
Start
AddrH
Bytes_E
Page
Count
00
Check
Sum
Setup
OUT
StartAddrL
Bytes_S
Byte 7
Setup
A1
01
00
03
00
00
08
00
IN
Action_Program
Verify
Checksum
00
00
00
00
00
00
OUT
(EP1)
64-byte
 Action_ Program (0x03)
This command will program the Flash Program Memory data in the device.
The AP executing the program action includes the following three commands:
Action_ Erase (Erase first)  Action_ Program (Program)  Action_ Read (Verify).
Read Status (Action_ReadStatus =0x04)
Setup
OUT
Byte 1
Byte 2
Byte 3
Byte 4
Byte 5
Byte 6
21
09
00
03
02
00
08
Action_
ReadStatus
Setup
IN
Byte 0
00
00
00
03
A1
01
00
Action_
Verify
Action
ReadStatus
Checksum
Status
ICType
00
00
00
Byte 7
00
Check
Sum
00
00
08
00
Program
Program
Program
Program
StartAddrH
StartAddrL EndAddrH
EndAddrL
 Action_ ReadStatus (0x04)
When the device is connected to the PC or when executing a Program, Read or Erase
operation, the AP will send the command to read back the related information of the
device.
 ActionStatus
The current state of the device, IDLE (0x00), erase, read or write.
 ICType
Return the IC model code, the HT66FB550 ICType is tentatively "01".
 ProgramStartAddrH/L
High and low byte of the start address in the area to be updated.
3
How to update the Flash ROM using the USB Module and IAP Function
 ProgramEndAddrH/L
High and low byte of the end address in the area to be updated.
Note: The address range of the area to be updated is what the device specifies for the
constant array. After the AP reads back the returned addresses, they will be displayed on
the "ADDR" interface.
Application Circuit
ICP/OCDS
ICP/OCDS
VDD
U2
1
2
3
UDN
4
UDP
5
V33O
6
VDD
7
VDD
8
GND
9
10
OCDSCK 11
12
PA0
VDD
R29
100K
R30
OCDSCK
300R
C12
0.1uF
PE5
C13
0.1uF
V33O
C14
0.1uF
PE5
PD0/TP3_1/OSC1
PD1/TP2_1/OSC2
PD2/TCK2
PD3
PD4/TCK3
PD5/TP1_1
PD6/TP3_0
PD7/TP2_0
NC
NC
NC
PC3/AN11
PC2/AN10
PC1/AN9
PC0/AN8
PB7
PB6/INT1/AN6
PB5/PCK/AN5
PB4/TP0_1/AN4
PB3/SCS/AN3
PB2/SCK/AN2
PB1/SDI/SCL/AN1
PB0/SDO/SDA/AN0
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
3
2
1
PA0
UDN
miniUSB
PC7
PC6
PC5
PC4
VUSB
DD+
NC
GND
SHELD
PC3
PC2
PC1
PC0
HT66FB550
1
2
3
4
5
6
VDD
Bead FB1
R16
R17
C10
2
4
6
8
10
12
33
33
C11
47P
47P
PC0
PC1
PC2
PC3
PC4
PC5
PC6
PC7
R18
R19
R20
R21
R22
R23
R24
R25
470
470
470
470
470
470
470
470
10
9
8
5
4
2
3
7
a
b
c
d
e
f
g
DP
K
K
UDN
UDP
USB
7
VDD
13
14
15
16
17
18
19
20
21
22
23
24
PA0/TCK1/OCDSDA
PA1/SDOA/C0X
NC
PA2/SDIA/C0+
UDN/GPIO0
PA3/SCKA/C0UDP/GPIO1
PA4/SCSA/TP0_0/C1X
V33O
PA5/TP1_0/C1+
UBUS/PE1/AVDD/VDD
PA6/TCK0/C1HVDD
PA7/INT0/AN7
VSS
PE0/VDDIO/VREF
PE3/XT1
PC7/AN15
PE4/XT2
PC6/AN14
/RES/OCDSCK
PC5/AN13
PE2
PC4/AN12
1
3
5
7
9
11
OCDSCK
J16
1
6
Q5
8050
R33 PE5
10K
DS0
This application circuit is mainly comprised of the USB communication interface, an LED
display circuit, reset circuit and MCU etc.
In the USB interface circuit, the resistors and capacitors in the USB cable are mainly used
to reduce interference, improve communication stability and reduce the possibility of data
errors during USB data transmission. The USB host device provides the power source for
the whole circuit. When it needs to write programs to the MCU using a writer, the USB
cable must be removed.
In this paper, the related operation Flash memory addresses, used to store the 16byte
constant array, are set to 0x0800~0x0807, which correspond to the LED display codes of
0~F. After connecting to the AP, the element order in the array will be rearranged by the
USB communication and IAP function, so that the LED display order of 0~F will be
changed. It can also be connected to the e-WriterPro, to read the data of the
corresponding address for comparison.
4
How to update the Flash ROM using the USB Module and IAP Function
S/W Function Block Flowchart
The main functions of the program for the HT66FB550 are to display the ROM data in the
area to be updated on the LED after power on, enable USB module and then enumerate.
After which and connected to the AP of the PC, according to the AP sent commands and
the endpoint 1, 2 sent and received data, the Flash Program Memory of the specified
addresses will be performed program, read, erase and other actions. After programming,
the LED displays the updated data.
The program structure can be divided into main program, USB enumeration, data
transmission and IAP related operations.
Main Program Flowchart
The main functions include: LED display and to determine whether to perform IAP related
operations.
5
How to update the Flash ROM using the USB Module and IAP Function
Access Endpoints to Receive and Send Data Subroutine
EnableUSBBoot() Flowchart
The main functions include: according to the accessed USB endpoints, transmit the
corresponding data. Endpoint 0 is mainly used to enumerate, receive and return the AP
sent commands. Endpoint 1 is used to receive the data to be programmed. Endpoint 2 is
used to return the AP read ROM data. The endpoint 1 received data is always 64 bytes,
and sent by page, that is 64 bytes per page. When the first and last page size is less than
a page, the AP will automatically fill out the remaining part with zeros and then send to the
device. Similar to endpoint 2, when sending the data to the AP, parts of less than a page
will be zeroed out.
6
How to update the Flash ROM using the USB Module and IAP Function
Endpoint 0 Data Processing Subroutine AccessFIFO0() Flowchart
The main functions include:
 According to the endpoint 0 received USB requests, return the corresponding data to
complete the enumeration
 Analyse the set report request to find out whether the AP performs a program, read or
erase operation next and calculate the checksum
 Analyse the get report request and according to the AP sent commands in the set
report stage, send back the corresponding data, the specific format of which refers to
the “Communication Protocol" section.
7
How to update the Flash ROM using the USB Module and IAP Function
IAP Programming Subroutine IAPProgram () Flowchart
The main functions include: according to the AP sent command which can be a program,
read or erase command, perform the corresponding action.
AP Program Main Functions Description
The API functions in the program, used to access the USB interface, belongs to hid.lib
and setupapi.lib. The hid.lib can refer to the attached AP program file, and the setupapi.lib
is the VS2008 own library. The following describes several functions used to find a USB
device, send commands and receive/send data in the program:
1. API_HIDFindDevice(HIDPARAMENT *nHIDParament,int nReadWrite);
Function: According to the nHIDParament information, find a HID device and return a
handle. The HIDPARAMENT is a structure declared in the HID_Lib.h, includes the VID,
PID, Usage and other informations. When the nReadWrite is 0, return a read handle;
when the nReadWrite is 1, return a write handle.
2. API_HIDReadFeture(HANDLE hHandle,BYTE *Buf1,BYTE *Buf2);
Function: The hHandle is a write handle obtained by the API_HIDFindDevice, the Buf1
is used for storing the command given by the device, and the information returned from
the device is stored in the Buf2. The first byte in the two buffers is Report ID.
3. ReadFile(
hReadHandle,
//Read file handle
ReadReportBuffer, //Save the data read from the device to the ReadReportBuffer
65,
//The byte number to be read, the Report ID takes up one byte,
//in addition to the transferred data size
NULL,
//A pointer to the actual read byte number
&ReadOverlapped //Specified as asynchronous read data
);
//Used to read the Flash Program Memory data from the device
8
How to update the Flash ROM using the USB Module and IAP Function
4. WriteFile(
hWriteHandle,
//Write file handle
WriteReportBuffer,//Data buffer pointer, sending the WriteReportBuffer data
//to the device
65,
//The byte number to be sent, the Report ID takes up one byte,
//in addition to the transferred data size
&BytesWritten,
//Storage area pointer for storing the actual written byte number
NULL
//Specified as synchronous read data
);
//Used to send the data written to the Flash Program Memory to the device
PC AP Use Introduction
PC AP Interface
When the device is connected to the PC, the AP will automatically search the device. The
device VID, PID and the used IC type will be displayed on the interface, and the
programmable address range will be displayed on the "ADDR" interface. The AP can
implement erase, program, read and other operations on the Flash Program Memory
space of the device corresponding to the address range on "ADDR". Note that the
"ADDR" specified addresses should be within the programmable address range
otherwise the AP will give an "Address format is error!" warning message.
After the device program starts running, the LED displays from 0 ~ F as default. After
setting the address, click “Read”, read back the 16 bytes of constant array data as shown
above. When the LED display order is to be updated, such as from F ~ 0, as shown below,
enter the following data for the address of 0x0800 ~ 0x0807, click “Program”, to complete
the device LED constant array update.
9
How to update the Flash ROM using the USB Module and IAP Function
Button Functions
Open: Open the data file to be programmed (.txt format), the data will be displayed in the
blue area on the left.
Program: Update the data in the blue area on the left to the Flash Program Memory of the
specified addresses.
NewFile: Setup the programmed content. The data in the blue area of the main interface
can be directly changed, or click on “NewFile” to change. The NewFile dialog is shown
below. Enter the data to be programmed on the right side, the default display is the data
read from the device (DeviceData), FileData is to display the data content of the files
opened by "Open", the left side is the preview area.
Program Example
Device:
IDE Version: 7.82 or above
configuration option:
High Freq.OSC: HIRC
; other option select by users.
AP:Microsoft Visual Studio 2008 / C++
For program code and description refer to the attached files
Conclusions
Using the HT66FB550 as the master MCU, this application note has introduced how to
use the USB module and IAP function to communicate with the PC side AP, to perform
related operations on the Flash Program Memory area.
10
How to update the Flash ROM using the USB Module and IAP Function
Accessory
Device Side Source Code File
HT66FB550_IAP_DEMO_Device.rar
PC Side Programming Software and Source Code File
IAP_DEMO_AP.rar
11