Skip to content

Commit

Permalink
CMSIS DAP/DAP Link Debugger (#1897)
Browse files Browse the repository at this point in the history
* Apps: DAP Link
* API: furi_hal_console_init

Co-authored-by: あく <alleteam@gmail.com>
  • Loading branch information
DrZlo13 and skotopes authored Oct 20, 2022
1 parent 42df7aa commit f56c949
Show file tree
Hide file tree
Showing 28 changed files with 3,000 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
[submodule "lib/cxxheaderparser"]
path = lib/cxxheaderparser
url = https://github.com/robotpy/cxxheaderparser.git
[submodule "applications/plugins/dap_link/lib/free-dap"]
path = applications/plugins/dap_link/lib/free-dap
url = https://github.com/ataradov/free-dap.git
105 changes: 105 additions & 0 deletions applications/plugins/dap_link/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Flipper Zero as CMSIS DAP/DAP Link
Flipper Zero as a [Free-DAP](https://github.com/ataradov/free-dap) based SWD\JTAG debugger. Free-DAP is a free and open source firmware implementation of the [CMSIS-DAP](https://www.keil.com/pack/doc/CMSIS_Dev/DAP/html/index.html) debugger.

## Protocols
SWD, JTAG , CMSIS-DAP v1 (18 KiB/s), CMSIS-DAP v2 (46 KiB/s), VCP (USB-UART).

WinUSB for driverless installation for Windows 8 and above.

## Usage

### VSCode + Cortex-Debug
Set `"device": "cmsis-dap"`

<details>
<summary>BluePill configuration example</summary>

```json
{
"name": "Attach (DAP)",
"cwd": "${workspaceFolder}",
"executable": "./build/firmware.elf",
"request": "attach",
"type": "cortex-debug",
"servertype": "openocd",
"device": "cmsis-dap",
"configFiles": [
"interface/cmsis-dap.cfg",
"target/stm32f1x.cfg",
],
},
```
</details>

<details>
<summary>Flipper Zero configuration example</summary>

```json
{
"name": "Attach (DAP)",
"cwd": "${workspaceFolder}",
"executable": "./build/latest/firmware.elf",
"request": "attach",
"type": "cortex-debug",
"servertype": "openocd",
"device": "cmsis-dap",
"svdFile": "./debug/STM32WB55_CM4.svd",
"rtos": "FreeRTOS",
"configFiles": [
"interface/cmsis-dap.cfg",
"./debug/stm32wbx.cfg",
],
"postAttachCommands": [
"source debug/flipperapps.py",
],
},
```
</details>

### OpenOCD
Use `interface/cmsis-dap.cfg`. You will need OpenOCD v0.11.0.

Additional commands:
* `cmsis_dap_backend hid` for CMSIS-DAP v1 protocol.
* `cmsis_dap_backend usb_bulk` for CMSIS-DAP v2 protocol.
* `cmsis_dap_serial DAP_Oyevoxo` use DAP-Link running on Flipper named `Oyevoxo`.
* `cmsis-dap cmd 81` - reboot connected DAP-Link.

<details>
<summary>Flash BluePill</summary>

```
openocd -f interface/cmsis-dap.cfg -f target/stm32f1x.cfg -c init -c "program build/firmware.bin reset exit 0x8000000"
```
</details>

<details>
<summary>Flash Flipper Zero using DAP v2 protocol</summary>

```
openocd -f interface/cmsis-dap.cfg -c "cmsis_dap_backend usb_bulk" -f debug/stm32wbx.cfg -c init -c "program build/latest/firmware.bin reset exit 0x8000000"
```
</details>

<details>
<summary>Reboot connected DAP-Link on Flipper named Oyevoxo</summary>

```
openocd -f interface/cmsis-dap.cfg -c "cmsis_dap_serial DAP_Oyevoxo" -c "transport select swd" -c "adapter speed 4000000" -c init -c "cmsis-dap cmd 81" -c "exit"
```
</details>

### PlatformIO
Use `debug_tool = cmsis-dap` and `upload_protocol = cmsis-dap`. [Documentation](https://docs.platformio.org/en/latest/plus/debug-tools/cmsis-dap.html#debugging-tool-cmsis-dap). Remember that Windows 8 and above do not require drivers.

<details>
<summary>BluePill platformio.ini example</summary>

```
[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
debug_tool = cmsis-dap
upload_protocol = cmsis-dap
```
</details>
24 changes: 24 additions & 0 deletions applications/plugins/dap_link/application.fam
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
App(
appid="dap_link",
name="DAP Link",
apptype=FlipperAppType.PLUGIN,
entry_point="dap_link_app",
requires=[
"gui",
"dialogs",
],
stack_size=4 * 1024,
order=20,
fap_icon="dap_link.png",
fap_category="Tools",
fap_private_libs=[
Lib(
name="free-dap",
cincludes=["."],
sources=[
"dap.c",
],
),
],
fap_icon_assets="icons",
)
234 changes: 234 additions & 0 deletions applications/plugins/dap_link/dap_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Alex Taradov <alex@taradov.com>. All rights reserved.

#ifndef _DAP_CONFIG_H_
#define _DAP_CONFIG_H_

/*- Includes ----------------------------------------------------------------*/
#include <furi_hal_gpio.h>

/*- Definitions -------------------------------------------------------------*/
#define DAP_CONFIG_ENABLE_JTAG

#define DAP_CONFIG_DEFAULT_PORT DAP_PORT_SWD
#define DAP_CONFIG_DEFAULT_CLOCK 4200000 // Hz

#define DAP_CONFIG_PACKET_SIZE 64
#define DAP_CONFIG_PACKET_COUNT 1

#define DAP_CONFIG_JTAG_DEV_COUNT 8

// DAP_CONFIG_PRODUCT_STR must contain "CMSIS-DAP" to be compatible with the standard
#define DAP_CONFIG_VENDOR_STR "Flipper Zero"
#define DAP_CONFIG_PRODUCT_STR "Generic CMSIS-DAP Adapter"
#define DAP_CONFIG_SER_NUM_STR usb_serial_number
#define DAP_CONFIG_CMSIS_DAP_VER_STR "2.0.0"

#define DAP_CONFIG_RESET_TARGET_FN dap_app_target_reset
#define DAP_CONFIG_VENDOR_FN dap_app_vendor_cmd

// Attribute to use for performance-critical functions
#define DAP_CONFIG_PERFORMANCE_ATTR

// A value at which dap_clock_test() produces 1 kHz output on the SWCLK pin
// #define DAP_CONFIG_DELAY_CONSTANT 19000
#define DAP_CONFIG_DELAY_CONSTANT 6290

// A threshold for switching to fast clock (no added delays)
// This is the frequency produced by dap_clock_test(1) on the SWCLK pin
#define DAP_CONFIG_FAST_CLOCK 2400000 // Hz

/*- Prototypes --------------------------------------------------------------*/
extern char usb_serial_number[16];

/*- Implementations ---------------------------------------------------------*/
extern GpioPin flipper_dap_swclk_pin;
extern GpioPin flipper_dap_swdio_pin;
extern GpioPin flipper_dap_reset_pin;
extern GpioPin flipper_dap_tdo_pin;
extern GpioPin flipper_dap_tdi_pin;

extern void dap_app_vendor_cmd(uint8_t cmd);
extern void dap_app_target_reset();
extern void dap_app_disconnect();
extern void dap_app_connect_swd();
extern void dap_app_connect_jtag();

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWCLK_TCK_write(int value) {
furi_hal_gpio_write(&flipper_dap_swclk_pin, value);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWDIO_TMS_write(int value) {
furi_hal_gpio_write(&flipper_dap_swdio_pin, value);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_TDI_write(int value) {
#ifdef DAP_CONFIG_ENABLE_JTAG
furi_hal_gpio_write(&flipper_dap_tdi_pin, value);
#else
(void)value;
#endif
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_TDO_write(int value) {
#ifdef DAP_CONFIG_ENABLE_JTAG
furi_hal_gpio_write(&flipper_dap_tdo_pin, value);
#else
(void)value;
#endif
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_nTRST_write(int value) {
(void)value;
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_nRESET_write(int value) {
furi_hal_gpio_write(&flipper_dap_reset_pin, value);
}

//-----------------------------------------------------------------------------
static inline int DAP_CONFIG_SWCLK_TCK_read(void) {
return furi_hal_gpio_read(&flipper_dap_swclk_pin);
}

//-----------------------------------------------------------------------------
static inline int DAP_CONFIG_SWDIO_TMS_read(void) {
return furi_hal_gpio_read(&flipper_dap_swdio_pin);
}

//-----------------------------------------------------------------------------
static inline int DAP_CONFIG_TDO_read(void) {
#ifdef DAP_CONFIG_ENABLE_JTAG
return furi_hal_gpio_read(&flipper_dap_tdo_pin);
#else
return 0;
#endif
}

//-----------------------------------------------------------------------------
static inline int DAP_CONFIG_TDI_read(void) {
#ifdef DAP_CONFIG_ENABLE_JTAG
return furi_hal_gpio_read(&flipper_dap_tdi_pin);
#else
return 0;
#endif
}

//-----------------------------------------------------------------------------
static inline int DAP_CONFIG_nTRST_read(void) {
return 0;
}

//-----------------------------------------------------------------------------
static inline int DAP_CONFIG_nRESET_read(void) {
return furi_hal_gpio_read(&flipper_dap_reset_pin);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWCLK_TCK_set(void) {
LL_GPIO_SetOutputPin(flipper_dap_swclk_pin.port, flipper_dap_swclk_pin.pin);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWCLK_TCK_clr(void) {
LL_GPIO_ResetOutputPin(flipper_dap_swclk_pin.port, flipper_dap_swclk_pin.pin);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWDIO_TMS_in(void) {
LL_GPIO_SetPinMode(flipper_dap_swdio_pin.port, flipper_dap_swdio_pin.pin, LL_GPIO_MODE_INPUT);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWDIO_TMS_out(void) {
LL_GPIO_SetPinMode(flipper_dap_swdio_pin.port, flipper_dap_swdio_pin.pin, LL_GPIO_MODE_OUTPUT);
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SETUP(void) {
furi_hal_gpio_init(&flipper_dap_swdio_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_swclk_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_reset_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
#ifdef DAP_CONFIG_ENABLE_JTAG
furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
#endif
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_DISCONNECT(void) {
furi_hal_gpio_init(&flipper_dap_swdio_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_swclk_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_reset_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
#ifdef DAP_CONFIG_ENABLE_JTAG
furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
#endif
dap_app_disconnect();
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_CONNECT_SWD(void) {
furi_hal_gpio_init(
&flipper_dap_swdio_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_swdio_pin, true);

furi_hal_gpio_init(
&flipper_dap_swclk_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_swclk_pin, true);

furi_hal_gpio_init(
&flipper_dap_reset_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_reset_pin, true);

#ifdef DAP_CONFIG_ENABLE_JTAG
furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
#endif
dap_app_connect_swd();
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_CONNECT_JTAG(void) {
furi_hal_gpio_init(
&flipper_dap_swdio_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_swdio_pin, true);

furi_hal_gpio_init(
&flipper_dap_swclk_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_swclk_pin, true);

furi_hal_gpio_init(
&flipper_dap_reset_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_reset_pin, true);

#ifdef DAP_CONFIG_ENABLE_JTAG
furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);

furi_hal_gpio_init(
&flipper_dap_tdi_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_write(&flipper_dap_tdi_pin, true);
#endif
dap_app_connect_jtag();
}

//-----------------------------------------------------------------------------
static inline void DAP_CONFIG_LED(int index, int state) {
(void)index;
(void)state;
}

//-----------------------------------------------------------------------------
__attribute__((always_inline)) static inline void DAP_CONFIG_DELAY(uint32_t cycles) {
asm volatile("1: subs %[cycles], %[cycles], #1 \n"
" bne 1b \n"
: [cycles] "+l"(cycles));
}

#endif // _DAP_CONFIG_H_
Loading

0 comments on commit f56c949

Please sign in to comment.