SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Can I write SWD commands and include it into Keil as part of flash download routine?
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

Can I write SWD commands and include it into Keil as part of flash download routine?

Neil Salmon
Last updated: September 16, 2023 2:37 pm
Neil Salmon 6 Min Read
Share
SHARE

Yes, it is possible to write SWD commands and include them into Keil as part of the flash download routine. The Serial Wire Debug (SWD) interface allows you to directly access the debug port of your ARM Cortex-M microcontroller and send commands to program flash memory. With some configuration, you can leverage Keil’s flash loader utility to automatically execute your custom SWD command sequence during the debug build flash programming process.

Contents
Overview of SWD and JTAG InterfacesAccessing SWD Interface from Keil1. Using Trace Scripts2. Custom Flash AlgorithmsBuilding Custom SWD SequencesIntegrating Custom SWD into Keil Flash AlgorithmExample: Chip Configuration via SWD before Flash ProgrammingConclusion

Overview of SWD and JTAG Interfaces

Most ARM Cortex-M MCUs like the STM32 series provide two different debug interface options – SWD and JTAG. JTAG has been around longer but requires more pins. SWD uses only two pins (SWDIO and SWCLK) making it a more compact and efficient alternative for chip debugging and flash programming. The SWD protocol provides access to all the debug components of the Cortex-M core – this includes setting breakpoints, accessing core and peripheral registers, uploading code, and programming flash memory. SWD uses a bi-directional data line (SWDIO) and clock line (SWCLK) to communicate debug commands and data between the host and target device.

Accessing SWD Interface from Keil

The Keil MDK toolkit integrates seamlessly with ULINK debug probes that support SWD interface. When you start a debug session, Keil will initialize the debug probe and open a connection to the target device through the SWD port. At this point, theprobe has full access to send arbitrary SWD commands to interact with the microcontroller. There are a couple of ways to leverage this access:

1. Using Trace Scripts

Keil allows you to create trace scripts with commands like SWDWrite, SWDRead, and other SWD operations. You can include these scripts in your projects and have them run automatically when you build and flash the program. The scripts get downloaded and executed on the probe side. This is useful for sending initial configuration commands during startup.

2. Custom Flash Algorithms

The standard flash algorithms included in MDK may not work for all chips or memory configurations. Keil lets you supply custom flash programming algorithms, which ultimately transmit SWD sequences to erase and program flash. With some HEX file parsing and logic, you can insert additional SWD commands into the flash algorithm to support your needs.

Building Custom SWD Sequences

Constructing valid SWD command and data packets requires understanding the SWD protocol. An SWD write packet starts with a header byte, followed by up to 4 bytes of payload. The payload contains a register address and the data to write. SWD read packets are similar, containing the register address to read from. The debug probe handles sending packets, receiving ACKs, and retrying on errors.

Some common SWD operations include:

  • Reading and writing debug registers in Cortex-M core (DHCSR, DCRSR, etc)
  • Accessing System Control Space registers to configure chip modes
  • Reading and writing flash memory through debug interface
  • Entering and exiting debug state

With knowledge of the specific target chip’s SWD implementation, you can send whatever supported commands are needed for your use case.

Integrating Custom SWD into Keil Flash Algorithm

The built-in flash algorithms in Keil are defined in .FLM files. These contain C functions like Init(), EraseSector(), ProgramPage(), etc. To customize the algorithm:

  1. Make a copy of the standard .FLM file
  2. Add any helper functions needed for your SWD commands
  3. Call the functions in appropriate places – e.g. EraseSector() before erasing, ProgramPage() before programming, etc.
  4. Reference your custom .FLM in the project settings

The algorithm gets compiled into the flash loader on the probe. When you build and flash within Keil, your extra SWD logic will automatically execute as part of the programming process.

Example: Chip Configuration via SWD before Flash Programming

Here is an example of custom SWD command integration to illustrate the concept. Let’s say you want to preconfigure some chip modes via SWD before flashing code. For example, you may want to:

  • Unlock flash control registers
  • Enable flash cache and prefetch
  • Load custom flash wait states

To do this:

  1. Create SWD write packets to set the needed System Control Space registers
  2. Make a new Init() function in your custom .FLM file
  3. Call Init() at the start of the algorithm to send your SWD configs
  4. Now flash programming will be preceeded by your chip initialization

This same concept can be extended to insert any custom SWD commands anywhere in the flash algorithm as needed.

Conclusion

The Keil MDK toolkit and ULINK debug probes provide convenient access to target devices through SWD interface. While the built-in flashing mechanisms generally work well, you can customize the process by directly inserting SWD commands as needed. With some knowledge of the SWD protocol and your chip’s architecture, you can leverage these features to enhance and optimize your embedded debug sessions.

Usage of arm technologies continues to grow for IoT, industrial, and other applications. For further help integrating SWD access into your workflow, the large knowledgable community at keil.com forums is an excellent resource. The Keil tools combined with the ecosystem’s expertise offer a robust platform for unlocking the full potential of ARM’s powerful Cortex-M series microcontrollers.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article How to Get Compiled Code into a Custom SoC through the SWD Interface for Cortex M0?
Next Article Will code debugging / breakpoint still work with a custom routine? Arm Cortex M0
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Where is arm cortex-M0 used?

The ARM Cortex-M0 is a 32-bit microcontroller core from ARM…

6 Min Read

Why STM32 is Better Than ESP32?

Both STM32 and ESP32 are popular 32-bit microcontroller units (MCUs)…

7 Min Read

What Is the Architecture of the Arm Cortex-M3?

The Arm Cortex-M3 is a 32-bit processor core designed for…

7 Min Read

How to Install GCC for ARM

The GNU Compiler Collection (GCC) is a widely used compiler…

4 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account