AN117 Using The Utilites DLL
AN117 Using The Utilites DLL
Relevant Devices
This application note applies to all Silicon Laboratories C8051Fxxx devices.
1. Introduction
The Interface Utilities Dynamic Link Library (DLL) provides the following functionality: download an Intel hex file to
Flash; connect and disconnect to a C8051Fxxx processor; “Run” and “Halt” the microprocessor; and read and write
internal, external, and code memory spaces. All this functionality is provided via a PC’s COM port or USB port and
a Silicon Laboratories Debug Adapter.
When using C++, the functions described in this document must be declared as imported functions. Add the C++
Prototype to the header file (*.h) of the source file (*.cpp) from which the function will be called.
The procedures and guidelines presented in this document illustrate how to link the Interface Utilities DLL to a
client executable. A DLL is not a standalone application. It is a library of exported functions that are linked at run-
time and called by a Microsoft Windows® application. To write a client using Visual Basic, please refer to Section 14
on page 20 for more information.
An example Interface Utilities application (including program source code) is provided along with the Interface
Utilities DLL as a means of testing or using the Utilities DLL as a base application. The Interface Utilities application
uses implicit linking, which requires the DLL to be placed in a specific directory (see Section 11 on page 16).
Note: In version 2.60 major changes in the generation of error codes were implemented. See Section 12 on
page 16 for the new error code definition and function.
3.1. Connect()
Description: This function is used to connect to a target C8051Fxxx device using a Serial Adapter. Establish-
ing a valid connection is necessary for all memory operations to succeed.
3.2. Disconnect()
Description: This function is used to disconnect from a target C8051Fxxx device using a Serial Adapter.
2 Rev. 3.2
AN117
3.3. ConnectUSB()
Description: This function is used to connect to a target C8051Fxxx device using a USB Debug Adapter.
Establishing a valid connection is necessary for all memory operations to succeed.
Parameters: 1. sSerialNumber—The serial number of the USB Debug Adapter. See Section 8 for information
on obtaining the serial number of each USB Debug Adapter connected. If only one USB
Debug Adapter is connected, an empty string can be used. The default is an empty string.
2. nECprotocol—Connection protocol used by the target device; JTAG (0) or Silicon Laboratories
2-Wire (C2) (1). The default is 0. The C2 interface is used with C8051F3xx derivative devices
and the JTAG interface is used with C8051F0xx, C8051F1xx, and C8051F2xx derivative
devices.
3. nPowerTarget—If this parameter is set to 1, the USB Debug Adapter will be configured to
continue supplying power after it has been disconnected from the target device. The default is
0, configuring the adapter to discontinue supplying power when disconnected.
4. nDisableDialogBoxes—Disable (1) or enable (0) dialogs boxes within the DLL. The default is 0.
3.4. DisconnectUSB()
Description: This function is used to disconnect from a target C8051Fxxx device using a USB Debug Adapter.
Parameters: none
3.5. Connected()
Description: This function returns a value representing the connection state of the target C8051Fxxx.
Parameters: none
Rev. 3.2 3
AN11 7
4. Program Interface Functions
The following program interface functions are available for use in the Interface Utilities DLL.
Download() - Downloads a hex file to the target C8051Fxxx device.
ISupportBanking() - Checks to see if banking is supported.
GetSAFirmwareVersion() - Returns the Serial Adapter firmware version.
GetUSBFirmwareVersion() - Returns the USB Debug Adapter firmware version.
GetDLLVersion() - Returns the Utilities DLL version.
GetDeviceName() - Returns the name of the target C8051Fxxx device.
4.1. Download()
Description: This function is used to download a hex file to a target C8051Fxxx device. After a successful exit
from the Download() function, the target C8051xxx will be in a “Halt” state. If the device is left in
the “Halt” state, it will not begin code execution until the device is reset by a Power-On reset or
by a SetTargetGo() DLL function call.
Parameters: 1. sDownloadFile—A character pointer to the beginning of a character array (string) containing
the full path and filename of the file to be downloaded.
2. nDevice Erase—When set to 1, performs a device erase before the download initiates. If set
to 0, the part will not be erased. A device erase will erase the entire contents of the device’s
Flash. The default is 0.
3. nDisableDialogBoxes—Disable (1) or enable (0) dialogs boxes within the DLL. The default is 0.
4. nDownloadScratchPadSFLE—This parameter is only for use with devices that have a
Scratchpad Flash memory block. Currently, this includes the C8051F02x, C8051F04x,
C8051F06x, and C8051F12x devices. For all other devices, this parameter should be left in
the default state. Set this parameter to 1 in order to download to Scratchpad memory. When
accessing and downloading to Scratchpad memory, the only valid address range is
0x0000 to 0x007F. The default is 0.
5. nBankSelect—This parameter is only for use with C8051F12x devices. For all other devices,
this parameter should be left in the default state. When using a C8051F12x derivative, set this
parameter to 1, 2, or 3 in order to download to a specific bank. The default is –1.
6. nLockFlash—Set this parameter to 1 to lock the Flash following the download. If Flash is locked,
the DLL will no longer be able to connect to the device.
7. bPersistFlash—If set to 1, the contents of Flash will be read prior to programming. Flash pages
are erased prior to programming. If the pages to be programmed contain any data in Flash that
need to be preserved, then set this parameter to 1.
4 Rev. 3.2
AN117
4.2. ISupportBanking()
Description: This function checks to see if banking is supported.
Parameters: none
Parameters: none
Parameters: none
Parameters: 1. psDeviceName - A pointer to a character string location where the device name will be copied.
Rev. 3.2 5
AN11 7
5. Get Memory Functions
The following functions for reading device memory locations are available for use in the Interface Utilities DLL.
GetRAMMemory() - Read RAM memory from a specified address.
GetXRAMMemory() - Read XRAM memory from a specified address.
GetCodeMemory() - Read Code memory from a specified address.
The “GetMemory” functions all expect to receive a pointer to an initialized, unsigned char (BYTE) array of nLength
as the first parameter. If the “GetMemory” functions complete successfully, the ptrMem variable will contain the
requested memory.
The following example shows how to properly initialize an array in C++:
unsigned char* ptrMem;
ptrMem = new unsigned char[length]; //Assumes that length has been declared
//and set elsewhere
Alternatively:
BYTE ptrMem[10] = {0x00}; // Must initialize the array prior to passing
// it into the DLL
5.1. GetRAMMemory()
Description: This function will read the requested memory from the Internal Data Address Space. The
requested RAM memory must be located in the target device’s Internal Data Address Space.
Parameters: 1. ptrMem—Pointer to the receive buffer; an initialized, unsigned char (BYTE) array of length
nLength. If the function completes successfully, the ptrMem variable will contain the
requested memory.
2. wStartAddress—The start address of the memory location to be referenced.
3. nLength—The number of bytes to read from memory.
6 Rev. 3.2
AN117
5.2. GetXRAMMemory()
Description: This function will read the requested memory from the External Data Address Space. The
requested XRAM memory must be located in the target device’s External Data Address Space.
Pay special attention to insure proper referencing of the External Data Address Space.
Parameters: 1. ptrMem—Pointer to the receive buffer; an initialized, unsigned char (BYTE) array of length
nLength. If the function completes successfully, the ptrMem variable will contain the
requested memory.
2. wStartAddress—The start address of the memory location to be referenced.
3. nLength—The number of bytes to read from memory.
5.3. GetCodeMemory()
Description: This function will read the requested memory from the program memory space, including the lock
byte(s). The requested Code memory must be located in the target device’s program memory
space. Use caution when reading from a sector that has been read locked. Reading from a sec-
tor that has been read locked will always return 0s. Also, reading from the reserved space is not
allowed and will return an error.
Parameters: 1. ptrMem—Pointer to the receive buffer, an initialized unsigned char (BYTE) array of length
nLength. If the function completes successfully, the ptrMem variable will contain the
requested memory.
2. wStartAddress—The start address of the memory location to be referenced.
3. nLength—The number of bytes to read from memory.
Rev. 3.2 7
AN11 7
6. Set Memory Functions
The following functions for writing to device memory locations are available for use in the Interface Utilities DLL.
SetRAMMemory() - Writes value to a specified address in RAM memory.
SetXRAMMemory() - Writes value to a specified address in XRAM memory.
SetCodeMemory() - Writes value to a specified address in code memory.
The “SetMemory” functions expect to receive a pointer to an initialized unsigned char (BYTE) array of nLength as
the first parameter. This array should contain nLength number of elements initialized prior to calling into the DLL’s
“SetMemory” functions. If the “SetMemory” functions complete successfully, the ptrMem variable will have
successfully programmed the requested memory.
The following example shows how to properly initialize an array in C++:
unsigned char* ptrMem;
ptrMem = new unsigned char[length]; //Assumes that length has been declared
//and set elsewhere
// next populate your array with the bytes that you want to set in memory
Alternatively:
// Must initialize the array prior to calling the DLL
BYTE ptrMem[10] = {0x00, 0x14, 0xAE, 0x50, 0xAD, 0x66, 0x01, 0x05, 0x77, 0xFF};
6.1. SetRAMMemory()
Description: This function will write to memory in the Internal Data Address Space. The target RAM memory
must be located in the target device’s Internal Data Address Space.
Parameters: 1. ptrMem—Pointer to the transmit buffer, an initialized unsigned char (BYTE) array of length
nLength. If the function completes successfully, the ptrMem variable will have successfully
programmed the requested memory.
2. wStartAddress—The start address of the memory location to be referenced.
3. nLength —The length of memory to be read.
8 Rev. 3.2
AN117
6.2. SetXRAMMemory()
Description: This function will write to memory in the External Data Address Space. The target XRAM
memory must be located in the target device’s External Data Address Space. Pay special
attention to insure proper referencing of the External Data Address Space.
Parameters: 1. ptrMem—Pointer to the transmit buffer, an initialized unsigned char (BYTE) array of length
nLength. If the function completes successfully, the ptrMem variable will have successfully
programmed the requested memory.
2. wStartAddress—The start address of the memory location to be referenced.
3. nLength—The length of memory to be read.
6.3. SetCodeMemory()
Description: This function will write to memory in the program memory space. If a client tries to write more
than one page of data at one time, or tries to write to the reserved area of Flash or to a write/
erase locked sector, the write function will not complete successfully. To write to a read/write lock
byte, wStartAddress should correspond to the device read/write lock byte address and nLength
should have a value of 1 or 2. Please refer to the relevant device data sheets for additional
information. If SetCodeMemory() completes successfully, only the specified range,
wStartAddress + nLength, will have successfully been written. All other values within the page
will retain their initial values.
Parameters: 1. ptrMem—Pointer to the transmit buffer, an initialized unsigned char (BYTE) array of length
nLength. If the function completes successfully, the ptrMem variable will have successfully
programmed the requested memory.
2. wStartAddress—The start address of the memory location to be referenced.
3. nLength—The length of memory to be read.
4. nDisableDialogs—Disable (1) or enable (0) dialogs boxes within the DLL. The default is 0.
Rev. 3.2 9
AN11 7
7. Target Control Functions
The following functions for controlling a device are available for use in the Interface Utilities DLL.
SetTargetGo() - Puts the target C8051Fxxx device in a “Run” state.
SetTargetHalt() - Puts the target C8051Fxxx device in a “Halt” state.
7.1. SetTargetGo()
Description: After a successful exit from the SetTargetGo() function, the target C8051xxx will be in a “Run” state.
Parameters: none
7.2. SetTargetHalt()
Description: After a successful exit from the SetTargetHalt() function, the target C8051xxx will be in a “Halt” state.
Parameters: none
Return Value: See Section "12. Test Results" on page 16 for error return information.
10 Rev. 3.2
AN117
8. USB Debug Adapter Communication Functions
The following functions are available in the Interface Utilities DLL to query and configure a USB Debug Adapter.
These functions are useful when more than one USB Debug Adapter is connected to the PC. Call
USBDebugDevices() first to determine the number of USB Debug Adapters connected. Next, use
GetUSBDeviceSN() to determine the serial number of each adapter. The serial number can then be used with
other functions to communicate with an individual USB Debug Adapter.
USBDebugDevices() - Determines how many USB Debug Adapters are present.
GetUSBDeviceSN() - Obtains the serial number of the enumerated USB Debug Adapters.
GetUSBDLLVersion() - Returns the version of the USB Debug Adapter driver file.
8.1. USBDebugDevices()
Description: This function will query the USB bus and identify how many USB Debug Adapter devices are
present.
Parameters: 1. dwDevices—A pointer to the value that will contain the number of devices that are found.
8.2. GetUSBDeviceSN()
Description: This function obtains a serial number string for the USB Debug Adapter specified by an index
passed in the dwDeviceNum parameter. The index of the first device is 0 and the index of the last
device is the value returned by USBDebugDevices() – 1.
Parameters: 1. dwDeviceNum—Index of the device for which the serial number is desired. To obtain the
serial number of the first device, use 0.
2. psSerialNum—A pointer to a character string location where the serial number will be copied.
8.3. GetUSBDLLVersion()
Description: This function will return the version of the driver for the USB Debug Adapter.
Parameters: 1. pVersionString—Pointer to a character string location where the version string of the USB
Debug Adapter driver will be copied.
Rev. 3.2 11
AN11 7
9. Stand-Alone Functions
The following stand-alone functions are available for use in the Interface Utilities DLL. These functions do not
require the use of the “Connect” or “Disconnect” functions and do not interact with any other functions.
FLASHErase() - Erase the Flash program memory using a Serial Adapter.
FLASHEraseUSB() - Erase the Flash program memory using a USB Debug Adapter.
9.1. FLASHErase()
Description: This function is used to erase the Flash program memory of a device using a Serial Adapter. This
function must be used to “unlock” a device whose Flash read and/or write lock bytes have been
written.
9.2. FLASHEraseUSB()
Description: This function is used to erase the Flash program memory of a device using a USB Debug
Adapter. This function must be used to “unlock” a device whose Flash read and/or write lock
bytes have been written.
Parameters: 1. sSerialNumber—The serial number of the USB Debug Adapter. See Section 8 for information
on obtaining the serial number of each USB Debug Adapter connected. If only one USB
Debug Adapter is connected, an empty string can be used. The default is an empty string.
2. nDisableDialogBoxes—Disable (1) or enable (0) dialogs boxes within the DLL. The default is 0.
3. nECprotocol—Connection protocol used by the target device; JTAG (0) or Silicon Laboratories
2-Wire (C2) (1). The default is 0. The C2 interface is used with C8051F3xx derivative devices
and the JTAG interface is used with C8051F0xx, C8051F1xx, and C8051F2xx derivative
devices.
12 Rev. 3.2
AN117
10. Multi-Device JTAG Programming
Multiple Silicon Laboratories C8051Fxxx devices that use the JTAG communications protocol can be connected
and programmed in a JTAG chain. Currently this includes the C8051F0xx, C8051F1xx, and C8051F2xx device
families. Configure the JTAG chain as shown in Figure 1. It will be necessary to know the instruction register length
of each device in the JTAG chain. The instruction register length of all Silicon Laboratories JTAG devices is 16 bits.
The following functions to configure and connect to a C8051Fxxx device on a JTAG chain is available for use in the
Interface Utilities DLL.
SetJTAGDeviceAndConnect() - Configure a connection to a C8051Fxxx device on a JTAG chain
using a Serial Adapter.
SetJTAGDeviceAndConnectUSB() - Configure a connection to a C8051Fxxx device on a JTAG chain
using a USB Debug Adapter.
Once the “SetJTAGDeviceAndConnect” function has returned successfully and a connection has been established
with a Silicon Laboratories device in the JTAG chain, any of the previously mentioned Communication, Program
Interface, and memory functions may be used on the isolated device. When finished interfacing with the device,
call a “Disconnect” function as usual to disconnect from the device.
Rev. 3.2 13
AN11 7
10.1. SetJTAGDeviceAndConnect()
Description: This function is used to connect to a single target JTAG device in a JTAG chain using a Serial
Adapter.
14 Rev. 3.2
AN117
10.2. SetJTAGDeviceAndConnectUSB()
Description: This function is used to connect to a single target JTAG device in a JTAG chain using a USB
Debug Adapter.
Parameters: 1. sSerialNumber—The serial number of the USB Debug Adapter. See Section 8 for information
on obtaining the serial number of each USB Debug Adapter connected. If only one USB
Debug Adapter is connected, an empty string can be used. The default is an empty string.
2. nPowerTarget—If this parameter is set to 1, the USB Debug Adapter will be configured to
continue supplying power after it has been disconnected from the target device. The default is
0, which configures the adapter to discontinue supplying power when disconnected.
3. nDisableDialogBoxes—Disable (1) or enable (0) dialogs boxes within the DLL. The default is 0.
4. DevicesBeforeTarget—Number of devices in the JTAG chain before the target device. The
default is 0.
5. DevicesAfterTarget—Number of devices in the JTAG chain after the target device. The default
is 0.
6. IRBitsBeforeTarget—The sum of instruction register bits in the JTAG chain before the target
device. The default is 0.
7. IRBitsAfterTarget—The sum of instruction register bits in the JTAG chain after the target
device. The default is 0.
Following Figure 1, assuming all devices in the JTAG chain are Silicon Laboratories devices, call
SetJTAGDeviceAndConnectUSB() as shown in the following examples:
To access JTAG Device #0:
SetJTAGDeviceAndConnectUSB(““, 0, 0, 0, 2, 0, 32);
sSerialNumber=””DevicesBeforeTarget=0
nPowerTarget=0DevicesAfterTarget=2
nDisableDialogBoxes=0IRBitsBeforeTarget=0
IRBitsAfterTarget=32
Rev. 3.2 15
AN11 7
11. Linking
Unless using explicit linking, it is necessary to provide the linker with the path of the “SiUtil.lib” library file before
building the client executable. In Microsoft Visual C++, this is accomplished by selecting Settings… from the
Project menu and then the Link tab. In the Object/library modules box, enter the full path and filename of the library
file. For example, “c:\project\release\SiUtil.lib” is the path and filename you would use for the library file titled,
“SiUtil.” The library file is not needed after the client executable is built.
If the DLL is implicitly linked, the DLL must be placed in one the following directories:
1. The directory containing the EXE client file.
2. The process’s current directory.
3. The Windows system directory.
4. The Windows directory.
5. A directory listed in the PATH environment variable.
16 Rev. 3.2
AN117
The Windows HRESULT implementation also provides macros that should be used to test the function's success
or failure:
//
// Generic test for success on any status value (non-negative
// numbers indicate success).
//
//
// and the inverse
//
Note that it is possible for functions to return non-zero positive "success" error codes. Therefore, you should
always check for success or failure by using either the SUCCEEDED() or FAILED() macros.
Bit 15 of the Code field is the Target Disconnected Flag. If Bit 15 is set, then this indicates that the target device has
been disconnected as a result of invoking the function. The following macro may be used to help test this bit:
#define TARGET_DISCONNECTED_FLAG 0x8000
When a non-zero HRESULT value is returned, the Facility field will indicate the source of the error.
It is possible to receive the following standard Windows errors (as defined in winerror.h):
Additional error return values for each Facility and Code value are defined in Tables 1, 2, 3, and 4.
Rev. 3.2 17
AN11 7
18 Rev. 3.2
AN117
Rev. 3.2 19
AN11 7
The GetErrorMsg() function can be used to get a string describing the error so that an application can display a
message with error information.
12.2. GetErrorMsg()
Description: This function returns a string describing an error which has been generated by the DLL.
20 Rev. 3.2
AN117
DOCUMENT CHANGE LIST Revision 2.6 to Revision 2.7
Added description for new GetDeviceName()
Revision 2.2 to Revision 2.3 function as Section 4.6 on page 5.
Instructions for declaring functions moved to Section Moved function descriptions for
1. GetSAFirmwareVersion(),
Reference to the SetJTAGDeviceAndConnect() GetUSBFirmwareVersion(), and GetDLLVersion() to
function moved from Section 9 to Section 10. Section 4, “Program Interface Functions”.
Replaced incorrect function descriptions in the Revision 2.7 to Revision 2.8
Section 9 function list.
Updated Section 4.6 on page 5.
Revision 2.3 to Revision 2.4 Updated Section 8.2 on page 11.
“Supported Debug Adapter” field added to each Replaced “dllexport” with “dllimport”.
function definition.
Revision 2.8 to Revision 2.9
The following new functions added to support the
USB Debug Adapter: ConnectUSB(), Error code return type changed from int_stdcall to
DisconnectUSB(), USBDebugDevices(), HRESULT_stdcall.
GetUSBDeviceSN(), GetHIDDLLVersion(), New parameter, bPersistFlash, added to Section
FLASHEraseUSB(), GetUSBFirmwareVersion(), "4.1. Download()" on page 4.
SetJTAGDeviceAndConnectUSB() Section “12. Test Results” replaced with new error
Section 4, “Program Interface Functions” split into 2 code implementation information.
sections, creating Section 7, “Target Control
Functions”. Revision 2.9 to Revision 3.0
Section 8, “USB Debug Adapter Communication In Section "7.2. SetTargetHalt()" on page 10, return
Functions” added. value type changed from BOOL to HRESULT with
Added Return Codes 9 and 10 to Table 1, “Return the function using the new error code
Codes”. implementation.
Rev. 3.2 21
AN11 7
CONTACT INFORMATION
Silicon Laboratories Inc.
400 West Cesar Chavez
Austin, TX 78701
Tel: 1+(512) 416-8500
Fax: 1+(512) 416-9669
Toll Free: 1+(877) 444-3032
Email: [email protected]
Internet: www.silabs.com
The information in this document is believed to be accurate in all respects at the time of publication but is subject to change without notice.
Silicon Laboratories assumes no responsibility for errors and omissions, and disclaims responsibility for any consequences resulting from
the use of information included herein. Additionally, Silicon Laboratories assumes no responsibility for the functioning of undescribed features
or parameters. Silicon Laboratories reserves the right to make changes without further notice. Silicon Laboratories makes no warranty, rep-
resentation or guarantee regarding the suitability of its products for any particular purpose, nor does Silicon Laboratories assume any liability
arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation conse-
quential or incidental damages. Silicon Laboratories products are not designed, intended, or authorized for use in applications intended to
support or sustain life, or for any other application in which the failure of the Silicon Laboratories product could create a situation where per-
sonal injury or death may occur. Should Buyer purchase or use Silicon Laboratories products for any such unintended or unauthorized ap-
plication, Buyer shall indemnify and hold Silicon Laboratories harmless against all claims and damages.
Silicon Laboratories and Silicon Labs are trademarks of Silicon Laboratories Inc.
Other products or brandnames mentioned herein are trademarks or registered trademarks of their respective holders
22 Rev. 3.2