GitHub - MarkDing:swd - Programing - Sram: Programming Internal SRAM Over ARM Cortex M3 SWD
GitHub - MarkDing:swd - Programing - Sram: Programming Internal SRAM Over ARM Cortex M3 SWD
MIT license
High_Level/src Fine tune Python script and … 10 years ago
Activity
Ref Programming internal SRAM… 10 years ago 151 stars
78 forks
images Add images 10 years ago
Report repository
No packages published
Contributors 2
1. Introduction
Languages
This documentation describe how to programming ARM Cortex M3 internal SRAM over SWD(Serial Wire Debug) interface. For this purpose,
something we too know. C 68.6% Python 16.1%
Objective-C 13.6% Assembly 1.5%
SWD communication protocol. We need to know Low level timing requirement, that is foundation toBatchfile
exchange command and data
0.2%
between debugger and target MCU.
Read/write data from/to internal SRAM. We need to know how to program firmware into desired address, like SRAM(0x20000000). That
needs us to know SW-DAP registers usage. Pass address and data over those registers and then into internal SRAM.
Make code running from SRAM. We need to change vector table entry from internal flash to SRAM; And SP and PC also needed change
to SRAM location. That need us to know the cortex M3 debug and system registers usage.
We choose Silabs SiM3U167 as target MCU in this implementation. We implement a high level protocol with python script, it calls Silabs
provided DLL file interface to access Silabs USB Debug Adapter. Also, we provide a firmware running from C8051F380, it contains a full
implementation on both low level communicating timing and high level programming SRAM protocol.
https://github.com/MarkDing/swd_programing_sram Page 1 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
The Debug Access Port(DAP) is split into two main control units. the Debug Port (DP) and the Access Port (AP), and the physical connection
to the debugger is part of the DP. The DAP supports two types of access, Debug Port (DP) accesses and Access Port (AP)accesses.
External device to communicate directly with Serial Wire Debug Port(SW-DP) over SWDIO/SCLK pins. And SW-DP in turn can access one or
several Access Ports(APs) the give access to the rest of the system. The MEM-AP is important AP which provide a way to access all
memory and peripheral registers residing on the internal AHB/APB buses.
One of the four registers within the DP is the AP Select Register, SELECT. This register specifies a particular Access Port, and a bank of four
32-bit words within the register map of that AP. It enables up to 256 Access Ports to be implemented, and gives access to any one of 16
four-word banks of registers on the selected AP.
3. SWD protocol
This section gives an overview of the bi-directional operation of the protocol. It illustrates each of the possible sequences of operations on
the Serial Wire Debug interface data connection.
SWJ-DP enables either an SWD or JTAG protocol to be used on the debug port. To do this, it implements a watcher circuit that detects a
specific 16-bit selection sequence on the SWDIOTMSpin:
The 16-bit JTAG-to-SWD select sequence is defined to be 0b0111100111100111, MSB first. This can be represented as 16'h79E7 if
transmitted MSB first or 16'hE79E if transmitted LSB first.
The host must read IDCODE register after line request sequence. This requirement gives confirmation that correct packet frame alignment
has been achieved.
https://github.com/MarkDing/swd_programing_sram Page 2 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
The request phase consists of 8 bits. The meaning of each bit in the request is illustrated below.
APnDP -- A single bit, indicating whether the Debug Port or the Access Port Access Register is to be accessed. 1 for accessing AP.
RnW -- A single bit, this bit is 0 for an write access, or 1 for a read access.
A[2:3] -- Two bits, giving the A[3:2] address field for the DP or AP register address.
Parity -- A single even parity bit is made over the APnDP, RnW and A[2:3] bits. The number of bits set to 1 is odd, then the parity bit
is set to 1.
Stop -- A single stop bit. In the synchronousSWD protocol this is always 0.
WAIT response -- The host must retry the operation later, value is b010.
FAULT response -- If the target responds with FAULT, an error has occurred and one of the sticky bits in CTRL/STAT is set. The host
can check the sticky error bits to see what kind of error has occurred. It must clear the sticky bits in ABORT register before using any
AP commands, because the target will always respond with FAULT as long as one of the sticky error bits are set. Value is b100.
This section will give a brief overview over the SW-DP registers.
[31:5] - Reserved
https://github.com/MarkDing/swd_programing_sram Page 3 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
[4] ORUNERRCLR Write 1 to this bit to clear the STICKYORUN overrun error flag to 0.
[3] WDERRCLR Write 1 to this bit to clear the WDATAERR write data error flag to 0
[2] STKERRCLR Write 1 to this bit to clear the STICKYERR sticky error flag to 0.
[1] STKCMPCLRa Write 1 to this bit to clear the STICKYCMP sticky compare flag to 0.
Write 1 to this bit to generate a DAP abort. This aborts the current AP transaction. Do this only if the
[0] DAPABORT
debugger has received WAIT responses over an extended period.
You might want to find what caused the flag to be set to 1. Typically:
For the STICKYCMP or STICKYERR flag, you must find which location was accessed to cause the flag to be set to 1.
For the WDATAERR flag, you must resend the corrupted data.
For the STICKYORUN flag, you must find which DP or AP transaction caused the overflow. You then have to repeat your transactions
from that point.
Bit [28] CDBGPWRUPREQ is the signal from the debug interface to the power controller, used to request the system power controller to
fully power-up and enable clocks in the debug power domain.
Bit [30] CSYSPWRUPREQ is the signal from the debug interface to the power controller, used to request the system power controller to
fully power-up and enable clocks in the system power domain.
The debugger must write a 1 to the CDBGPWRUPREQ and CSYSPWRUPREQ bits before using the AHB-AP. The STICKYERR bit is set if an
error is returned by a AP transaction. While the STICKYERR bit is set any SWD request will return a FAULT response. To clear the
STICKYERR bit, use the ABORT register.
[7:4] APBANKSEL Selects the active four-word register bank on the current AP
If you require the value from an AP register read, that A read of the DP Read Buffer must be followed.
https://github.com/MarkDing/swd_programing_sram Page 4 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
A MEM-AP provides a DAP with access to a memory subsystem. Since memory, peripherals and debug components are all memory
mapped, the MEM-AP can be used to both program and debug Cortex M3.
Table 5 Bit assignments for the MEM-AP Control/Status Word Register, CSW
[30:24] Prot Bus access protection control. This field enables the debugger to specify protection flags for a debug access.
This section will go through the Cortex M3 Debug registers. With debug registers, we can set core into halt mode and can change core
registers, like SP or PC.
https://github.com/MarkDing/swd_programing_sram Page 5 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
Debug key: A debugger must write 0xA05Fto this field to enable write accesses to bits [15:0], otherwise the
[31:16] DBGKEY
processor ignores the write access.
A handshake flag for transfers through the DCRDR: 0 = There has been a write to the DCRDR, but the
[16] S_REGRDY
transfer is not complete; 1 = The transfer to or from the DCRDR is complete.
Specifies the ARM core register, special-purpose register, or Floating-point extension register, to transfer: R0-R12,
[6:0] REGSEL
SP, LR, DebugReturnAddr, xPSR, MSP, PSP, etc.
DebugReturnAddress is the address of the first instruction to be executed on exit from Debug state.
To transfer a data word to an ARM core register, special-purpose register, orFloating-point extension register, a debugger:
Writes the required word to DCRDR.
Writes to the DCRSR, with the REGSEL value indicating the required register, and the REGWnR bit as 1 to indicate a write access.
This write clears the DHCSR S_REGRDY bit to 0.
If required, polls DHCSR until DHCSR.S_REGRDY reads-as-one. This shows that the processor has transferred the DCRDR value to
READMEthe selected
MIT license
register.
To transfer a data word from an ARM core register, special-purpose register, or Floating-point extension register, a debugger:
Writes to the DCRSR, with the REGSEL value indicating the required register, and the REGWnR bit as 0 to indicate a read access.
This write clears the DHCSR.S_REGRDY bit to 0.
Polls DHCSR until DHCSR.S_REGRDY reads-as-one. This shows that the processor has transferred the value of the selected
register to DCRDR.
Reads the required value from DCRDR.
https://github.com/MarkDing/swd_programing_sram Page 6 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
[9] VC_INTERR Enable halting debug trap on a fault occurring during exception entry or exception return.
Enable halting debug trap on a UsageFault exception caused by a state information error, for example an
[7] VC_STATERR
Undefined Instruction exception.
Enable halting debug trap on a UsageFault exception caused by a checking error, for example an alignment
[6] VC_CHKERR
check error.
[5] VC_NOCPERR Enable halting debug trap on a UsageFault caused by an access to a Coprocessor.
[0] VC_CORERESET Enable Reset Vector Catch. This causes a Local reset to halt a running system.
Vector catch is the mechanism for generating a debug event and entering Debug state when a particular exception occurs. Vector catching
is only supported by halting debug.
Vector Key. Register writes must write 0x05FAto this field, otherwise the write is ignored.On reads,
[31:16] VECTKEY
returns 0xFA05.
[2] SYSRESETREQ Writing 1 to this bit asserts a signal to the external system to request a Local reset.
[1] VECTCLRACTIVE Writing 1 to this bit clears all active state information for fixed and configurable exceptions.
7. SRAM programing
This section demonstrate step by step how to programming internal SRAM over SWD.
7.1. Initialization
Before suing the SW-DP an initialization sequence must be performed to establish communication and bring the SW-DP to a know state.
Send more than 50 SWCLKTCK cycles with SWDIOTMS=1. This ensures that both SWD and JTAG are in their reset states.
Send the 16-bit JTAG-to-SWD select sequence on SWDIOTMS
Send more than 50 SWCLKTCK cycles with SWDIOTMS=1. This ensures that if SWJ-DP was already in SWD mode, before sending the
select sequence, the SWD goes to line reset.
Perform a READID to validate that SWJ-DP has switched to SWD operation.
There debug and system registers and one Silabs extent AP register CHIPAP_CTRL1 are used for this purpose. CHIPAP_CTRL1 address =
0x1, APSEL = 0x0A. bit 3 core_reset_ap, To hold the CortexM3 core in reset this bit should be written to one.
https://github.com/MarkDing/swd_programing_sram Page 7 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
Now the core will be halted on first instruction and all peripherals and registers will have their reset value.
Write:
https://github.com/MarkDing/swd_programing_sram Page 8 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
f = open("sim3u1xx_USBHID_ram.bin",mode = 'rb')
data = f.read()
swd_write_mem(uda, 0x20000000, data, len)
.
.
.
swd_write_mem(uda, 0xe000ed08, 0x20000000, 1)
swd_write_core_register(uda, 15, data[1]& 0xFFFFFFFE)
swd_write_core_register(uda, 13, data[0])
write_AHB(uda, DHCSR, 0xA05F0000)
8. Source code
9. Reference
LICENSE
The MIT License (MIT)
https://github.com/MarkDing/swd_programing_sram Page 9 of 10
GitHub - MarkDing/swd_programing_sram: Programming internal SRAM over ARM Cortex M3 SWD 21/5/24, 5:40 PM
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
https://github.com/MarkDing/swd_programing_sram Page 10 of 10