PSoC 4000 Family: PSoC 4 Architecture TRM, Document No. 001-89309 Rev. *D 143
Program and Debug Interface
18.5 Programming the PSoC 4
Device
PSoC 4 is programmed using the following sequence.
1. Acquire the SWD port in PSoC 4.
2. Enter the programming mode.
3. Execute the device programming routines such as Sili-
con ID Check, Flash Programming, Flash Verification,
and Checksum Verification.
18.5.1 SWD Port Acquisition
18.5.1.1 Primary and Secondary SWD Pin
Pairs
The first step in device programming is to acquire the SWD
port in PSoC 4. Refer to the device datasheet for information
on SWD pins.
If two SWD pin pairs are available in the device, the
SWD_CONFIG register in the supervisory flash region is
used to select between one of the two SWD pin pairs that
can be used for programming and debugging. Note that only
one of the SWD pin pairs can be used during any program-
ming or debugging session. The default selection for
devices coming from the factory is the primary SWD pin pair.
To select the secondary SWD pin pair, it is necessary to pro-
gram the device using the primary pair with the hex file that
enables the secondary pin pair configuration. Afterwards,
the secondary SWD pin pair may be used.
18.5.1.2 SWD Port Acquire Sequence
The first step in device programming is for the host to
acquire the target's SWD port. The host first performs a
device reset by asserting the external reset (XRES) pin.
After removing the XRES signal, the host must send an
SWD connect sequence for the device within the acquire
window to connect to the SWD interface in the DAP. The
pseudo code for the sequence is given here.
Code 1. SWD Port Acquire Pseudo Code
ToggleXRES(); // Toggle XRES pin to reset
device
//Execute ARM’s connection sequence to
acquire SWD-port
do
{
SWD_LineReset();
//perform a line reset
(50+ SWDCK clocks with SWDIO high)
ack = Read_DAP ( IDCODE, out ID); //Read
the IDCODE DP register
}while ((ack != OK) && time_elapsed < 2 ms); //
retry connection until OK ACK or timeout
if
(time_elapsed >= 2 ms) return FAIL; //check
for acquire time out
if
(ID != CM0_ID) return FAIL; //confirm SWD ID
of Cortex-M0 CPU. (0x0BB11477)
In this pseudo code, SWD_LineReset() is the standard ARM
command to reset the debug access port. It consists of more
than 49 SWDCK clock cycles with SWDIO high. The trans-
action must be completed by sending at least one SWDCK
clock cycle with SWDIO asserted LOW. This sequence syn-
chronizes the programmer and the chip. Read_DAP() refers
to the read of the IDCODE register in the debug port. The
sequence of line reset and IDCODE read should be
repeated until an OK ACK is received for the IDCODE read
or a timeout (
2 ms) occurs. The SWD port is said to be in the
acquired state if an OK ACK is received within the time win-
dow and the IDCODE read matches with that of the Cortex-
M0 DAP.
18.5.2 SWD Programming Mode Entry
After the SWD port is acquired, the host must enter the
device programming mode within a specific time window.
This is done by setting the TEST_MODE bit (bit 31) in the
test mode control register (MODE register). The debug port
should also be configured before entering the device pro-
gramming mode. Timing specifications and pseudo code for
entering the programming mode are detailed in the docu-
ment.
18.5.3 SWD Programming Routines
Executions
When the device is in programming mode, the external pro-
grammer can start sending the SWD packet sequence for
performing programming operations such as flash erase,
flash program, checksum verification, and so on. The pro-
gramming routines are explained in the Nonvolatile Memory
Programming chapter on page 147.