Search This Blog

Oct 25, 2012

USB debug adapter SWD connect with Arm Cortex M3 failure analysis

   There is an issue reported from customer that Silabs USB debug adapter (UDA) cannot
connect with SiM3U1xx MCU card board. When they try to download code into MCU under
preceistion32 IDE, it got below message.

“Error message: 02: Failed on connect: Em(04). Cannot provide power to DAP bus. Emu(0):
Conn&Reset. Was: None. DpID: 2BA01477. Info: UDA000C5EFB”

Once new chip been programmed and UDA cannot connect with MCU any more.

What could cause the connection failure? Normally, set system clock incorrectly or put MCU
into low power mode will cause such issue. So I suggest customer to use si32FlashUtility to erase
internal flash. "si32FlashUtility.exe -r 1 -e 2". In startup code, there is 500ms delay which allows
UDA take control of MCU, and then si32FlashUtility.exe can erase the wrong firmware inside
flash. But things not go smooth. Customer replied that it doesn’t work and error message out:
“ADI_STTUS_API_NOT_CONNECTED_TO_TARGET”. There is weird thing and asked customer
sends their board and source code to me.


Customer’s code Analysis:

I start testing on my board with customer’s code at first. I added back door code to prevent
unexpected situation. Add check GPIO status at beginning of main function.

void check_gpio()
while(SI32_PBSTD_A_read_pin(SI32_PBSTD_2,8) == 0);
SI32_PBSTD_A_set_pins_push_pull_output(SI32_PBSTD_2, 0x00000C00);
SI32_PBSTD_A_write_pins_low(SI32_PBSTD_2, 0x00000C00);

And we also have 500ms delay in Systeminit function by default.

void SystemInit(void)
// To disable the pin reset delay described below, make sure the preprocessor
// symbol si32HalOption_disable_pin_reset_delay is defined by your toolchain.
# if !defined(si32HalOption_disable_pin_reset_delay)
// If the reset pin was the source of the last reset, delay for 500 msec.
// Firmware can disable the debug port by inadvertantly setting the AHB
// clock source to a disabled clock. If this happens too quickly after a
// reset, it is not possible for a debug agent to gain control and thus
// not possible to reprogram the on-chip flash. Adding a delay here gives
// a debug agent sufficient time to connect.

SysTick->VAL = 0;

// Wait for the count down to complete
while (0 == (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)) {}

// Set the SysTick timer to reset values
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;

# endif

// invoke the application's system initialization.


Run the code and I can see connect failure happened, same as customer’s issue. I ran
"si32FlashUtility.exe -r 1 -e 2", it can erase firmware from internal flash. I just check customer’s
code. I found the reason cause connection failure. They try to make system clock configuration
change to external CMOS. But the setting of the XTAL2 pin is wrong. It set it as analog mode
instead of digital input. So when switch system clock to external CMOS, the MCU hang. And also
ETM enabled by customer. It is not necessary. I just comment out the system clock modification
part. And UDA can connect with MCU card board.

Customer’s board analysis:

But customer’s board cannot be recovered by "si32FlashUtility.exe -r 1 -e 2", what happens
with customer’s board? First, I need to confirm whether customer’s code contains 500ms delay.
This is default routine in SDK. And since customer’s code set ETM enable, this setting will draw
down ETM pin, those pins are high when I press RESET pin. So I can compare RESET and ETM
pin to know whether 500ms delay was executed. And the result shows no 500ms delay at all.
That is why si32FlashUtility cannot erase firmware. I have measure timing of the code execution.
It only takes dozens micro seconds to run from power on to system clock modification part.
I checked si32FlashUtility, it issue reset action on RESET pin, and 4ms later, and it start to
communicate with MCU, the communication period about 50mS. So that insufficient to take
control from MCU.


Make sure add back door code in project. Include long delay and GPIO checking.

Oct 19, 2012

With openpyxl for python3 to access excel 2007


    We want to transfer the data from the data sheet to the Excel files, which we call Alias files.  We will use the Alias files to automatically generate documentation, code, and IDE support.


Here’s the SFR definition for ADC0CF from the data sheet.

Here is the same information entered into the Excel File:

Special Notes from the SFR definition
10) The SFR address and page information does not need to be stored in the Excel file.   When we generate documentation and IDE support files, the addresses will come from a different file.
11) The SFR Definition number is not stored in the Excel file.
12) We have a special mechanism for documenting functions in the Description field.  For now, you can simply do an approximation of the equation in the text.
Special Notes from the Excel file
13) Module Name.  Choose any name that is short and appropriate for now.  Use names similar to the corresponding 32-bit peripheral if available.
14) Offset.  In the Excel file, each register must have a unique offset.  Start at 0x00 and increment by 1 for each register.
15)   In this example, the reset value for the AD0SC bits is 11111b, so the reset value is 31, which gets entered into each row.


From above requirements, we can see that is hard work with copy and paste by hand. We need to think about make a code to automatically get peripheral register contents and write into excel files with proper style requirement. However, we can see some fields need human intelligence to name the field. That is Okay, we can leave them there and fill by hand later. And most of fields can be done by code.


Python is popular in company, so Python3 is our prefer script language.

Parse data sheet:

Since data sheet is PDF file. I have studied background knowledge of python parse PDF file. And finally I found this blog, The author planed to do similar thing as I want. But he found that is hard to achieve it. I didn’t know PDF file even has not table. So I need other file type. And we have .txt file, it is easy to access.  We will have a look in it in detail later.

Access Excel 2007 file

Compared several 3rd party tools, I choose openpyxl since it supports excel 2007 and python Here are the steps to install it in your PC, of course you already installed Python 3.
1.       Get distribut0.6.28
2.       Get openpyxl 1.5.8 for python 3
3.       Install distribut0.6.28
$ install
4.       Install openpyxl 1.5.8 for python3
$ install

Creating Excel file

Excel file requirement:

1.       Two sheets.
2.       Font Calibri, size 11
3.       First row is freeze pane, and Font is “bold”
4.       Each register first row is filled with yellow color

Achievement by openpyxl:

1.       Two sheets.
 wb = Workbook()
 ws = wb.worksheets[0]
 ws.title = 'version'
 ws = wb.create_sheet()
 ws.title = "Base_alias"
2.       Font Calibri, size 11
For excel 2007, default font is Calibri, and default font size is 11.
3.       First row is freeze pane, and Font is “Bold”
 ws._freeze_panes = 'A2'
 ws.cell('%s%s'%(col, 1)).style.font.bold = True
4.       Each register first row is filled with yellow color
ws.cell('%s%s'%(col, row)).style.fill.fill_type = Fill.FILL_SOLID
ws.cell('%s%s'%(col, row)).style.fill.start_color.index = Color.YELLOW
5.       Write data into cell
ws.cell('%s%s'%(col, row)).value = row_data[i]

Parse .txt file

.txt file overlook

Here is the example opened by notepad++ , it looks like:

It is not so regular, isn’t it?

Analysis txt file

The arrow in the snapshot is tab(‘\t’), and CRLF is ‘\n’.
1.       Every  register description start with "SFR Definition"
2.       Register bit type field start with 'Type\t'.
3.       Register bit reset field start with 'Reset\t'
4.       Register description start with 'Bit            Name    Function'
5.       We have several situations in description part.
a.       “7\t”
b.      “7:0\t”
c.       “1x:” and “01:”
d.      Description row
e.      Double “\n” indicates reach end of this register.

Handle data in txt file

With Python3 internal function we can deal with all string relate context.

Source code

Oct 11, 2012

Access excel file by Python

    I have a task to extract register data from SOC spec and fill in excel file. I chosen Python script language to do this automatic job. I have found two 3rd party tools xlwt and openpyxl.
    I have tried xlwt, it has well documentation and easier to operation. However, it has two major weakness, first, it only support up to excel 2003; second, it doesn't support Python 3 which is default version in my group. So I turn to Openpyxl, before that, I would like record some information for xlwt which might be useful for other who don't care about those two weakness I mentioned above.

1. xlrd
* Version 0.8.0
* c:\python26\python.exe install

2. xlwt
* version 0.7.4
* c:\python26\python.exe install

3. Sample code
These same code can generate all style I needed in my task.
from xlwt import Workbook, easyxf
from xlwt.Utils import rowcol_to_cell

row0_style = easyxf(
    'font: name Calibri, bold True, height 220;'
reg_row_style = easyxf(
    'pattern: pattern solid, fore_colour yellow;'
    'font: name Calibri, height 220;'
common_style = easyxf(
    'font: name Calibri, height 220;'

row0_data = [
    'Bit Num',
    'Bit Access',
    'Bit Field Reset',
    'Enum Name',
    'Enum Value',
    'Short Name',

w = Workbook()
#sheet1 = w.add_sheet('version')

sheet2= w.add_sheet('Base_alias')
sheet2.panes_frozen = True
sheet2.remove_splits = True
sheet2.horz_split_pos = 1
for col in range(12):
for col in range(12):
for col in range(12):
    for row in range(2,80):