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.
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:
- Make a copy of the standard .FLM file
- Add any helper functions needed for your SWD commands
- Call the functions in appropriate places – e.g. EraseSector() before erasing, ProgramPage() before programming, etc.
- 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:
- Create SWD write packets to set the needed System Control Space registers
- Make a new Init() function in your custom .FLM file
- Call Init() at the start of the algorithm to send your SWD configs
- 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.