Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SWDP driver interface API and bit-bang driver, add CMSIS-DAP compatible controller #53798

Merged
merged 14 commits into from
Jun 14, 2024

Conversation

jfischer-no
Copy link
Collaborator

@jfischer-no jfischer-no commented Jan 14, 2023

Add Serial Wire Debug Port interface driver API and bit-bang driver.
Add CMSIS-DAP compatible controller which is a handler between
the host interface and SWD driver.
Add cmsis dap sample using USB HID as interface bulk transfers.

This is a rebase and followup of #20183

@jfischer-no jfischer-no added the Experimental Experimental features not enabled by default label Jan 14, 2023
@zephyrbot zephyrbot added the area: Devicetree Binding PR modifies or adds a Device Tree binding label Jan 14, 2023
Comment on lines 64 to 84
static uint8_t sw_request_lut[16] = {0U};

static void mk_sw_request_lut(void)
{
uint32_t parity = 0U;

for (int request = 0; request < sizeof(sw_request_lut); request++) {
parity = request;
parity ^= parity >> 2;
parity ^= parity >> 1;

/*
* Move A[3:3], RnW, APnDP bits to their position,
* add start bit, stop bit(6), and park bit.
*/
sw_request_lut[request] = BIT(7) | (request << 1) | BIT(0);
/* Add parity bit */
if (parity & 0x01U) {
sw_request_lut[request] |= BIT(5);
}
}

LOG_HEXDUMP_DBG(sw_request_lut, sizeof(sw_request_lut), "request lut");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why compute it at runtime instead of just having fixed const array with the values (with comment stating how the values were generated)? Using precomputed array would save a bit of RAM and code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was done with the latest changes 👍🏻

samples/subsys/dap/src/main.c Outdated Show resolved Hide resolved
drivers/dp/swdp_bitbang.c Outdated Show resolved Hide resolved
drivers/dp/swdp_bitbang.c Show resolved Hide resolved
drivers/dp/swdp_bitbang.c Outdated Show resolved Hide resolved
@MaureenHelm
Copy link
Member

@flit fyi

drivers/dp/swdp_bitbang.c Outdated Show resolved Hide resolved
drivers/dp/swdp_bitbang.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@nordicjm nordicjm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not able to get this working with an nrf52840dk connected to an nrf52832, ACK timed out for every request. Re-wired the nrf52832 directly to a DAPLINK programmer (without nRESET) and it works fine through that using pyocd.

dts/bindings/misc/zephyr,swdp-gpio.yaml Outdated Show resolved Hide resolved
dts/bindings/misc/zephyr,swdp-gpio.yaml Outdated Show resolved Hide resolved
@cfriedt
Copy link
Member

cfriedt commented Mar 10, 2023

I love this - thanks @jfischer-no 👍

@jfischer-no jfischer-no self-assigned this Mar 23, 2023
@jfischer-no
Copy link
Collaborator Author

Rebased. I have not addressed all the comments yet, will do so later.

@raveslave
Copy link

raveslave commented Apr 19, 2023

shouldn't this make use of CMSIS-DAP v2 (USB bulk) instead of HID?
or at least provide an option to choose bulk/v2 vs hid as larger packages and high-speed usb is important for speed

Use CMSIS-DAP v2.x instead that provides high-speed SWO trace streaming and does not require driver installation in modern operating systems (Mac OS, Linux, Windows).

@jfischer-no
Copy link
Collaborator Author

shouldn't this make use of CMSIS-DAP v2 (USB bulk) instead of HID?
or at least provide an option to choose bulk/v2 vs hid as larger packages and high-speed usb is important for speed

No. USB transfer is only an option, it is not the main scope of this PR, it is there for demonstration and testing. Bit-bang controller performance is far far away from the possible USB FS interrupt transfers maximum bandwidth. I see no need to discuss this until we have stable API with few fast SWDP controllers. Also, no one forces you to use sample in this PR, you are free to implement own host backend.

@raveslave
Copy link

shouldn't this make use of CMSIS-DAP v2 (USB bulk) instead of HID?
or at least provide an option to choose bulk/v2 vs hid as larger packages and high-speed usb is important for speed

No. USB transfer is only an option, it is not the main scope of this PR, it is there for demonstration and testing. Bit-bang controller performance is far far away from the possible USB FS interrupt transfers maximum bandwidth.

yes, true for swd communication which needs a mcu-specific optimization for speed.

but for vendor-commands running over daplink you can make use of the packet-size and added speed of v2 compared to HID, even when using FS phy's

@flit
Copy link
Contributor

flit commented Apr 19, 2023

Regardless of performance, I strongly recommend switching the example/test to use CMSIS-DAP v2 / bulk endpoints. HID for CMSIS-DAP is deprecated and should no longer be used for new code or examples.

Fwiw, bit bang SWD is able to achieve 2-10 MHz, sometimes even higher, depending on the MCU and how well the code is implemented (eg, direct register writes with constant register addresses and bit masks). However, the duty cycle tends to be all over the place (not a problem protocol-wise, but ugly as hell on a scope 😝). Point being that on some controller MCUs, you can achieve faster SWD performance with bulk.

@te-johan
Copy link

Great work @jfischer-no! I've tested to integrate it into a probe we have and it was rather simple to get it going. Added support for nxp lpc series and could run it with 512 byte packet size on high speed usb.
I could not get HID to work but i repurposed another usb class driver(midi) I had to get the CMSIS-DAPv2 bulk interface to work.

please feel free to integrate them into your rebase branch. I did not include the usb class driver since i do not have it in the zephyr tree.
https://github.com/te-johan/zephyr/tree/cmsis-dap-lpc-fixes

@maxd-nordic
Copy link
Contributor

FYI: I'm currently working on updating this to CMSIS-DAPv2. Generic device support and simpler PIN configurations are already there. I based it on @te-johan 's version.
https://github.com/maxd-nordic/sdk-zephyr/tree/pr-cmsis-dap-rebase-additions

@te-johan
Copy link

te-johan commented Jun 9, 2023

I can upstream my cmsis-dapv2 class driver next week.
I’ve also fixed some reset issues as well as bumped the specification to 1.3 to support raspberry pico.
It can easily be lifted to 2.1.1 since all new features are optional.

edit:I see that you also added swd sequence support.

@maxd-nordic
Copy link
Contributor

@te-johan I'm sure we can find a way to merge our additions.

* @param turnaround Line turnaround cycles
* @param data_phase Always generate Data Phase (also on WAIT/FAULT)
* @return 0 on success, or error code
*/

This comment was marked as outdated.

build_only: true
platform_allow: nrf52840dk_nrf52840
integration_platforms:
- nrf52840dk_nrf52840
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- nrf52840dk_nrf52840
- nrf52840dk/nrf52840

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(occurs multiple times)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked sample configuration and removed thingy91 which is only supported in NCS.

maxd-nordic and others added 6 commits June 11, 2024 12:25
Move low-level GPIO functions to a separate file and use GPIO driver
API if low-level GPIO support is not available for the platform.
Allows alternative pin configuration using only two pins, clk and dio.
Improve binding description.

Signed-off-by: Maximilian Deubel <[email protected]>
Signed-off-by: Johann Fischer <[email protected]>
Add API to read count bits from SWDIO into data LSB first.

Signed-off-by: Maximilian Deubel <[email protected]>
Signed-off-by: Johann Fischer <[email protected]>
Implement wait for SWJ pins command.

Signed-off-by: Maximilian Deubel <[email protected]>
Add support for DAP_INFO string elements.

Signed-off-by: Maximilian Deubel <[email protected]>
Add ID_DAP_UART_* command definitions and react properly to
unsupported UART commands.

Signed-off-by: Maximilian Deubel <[email protected]>
Signed-off-by: Johann Fischer <[email protected]>
DAP SWD sequence command is a requirement to support CMSIS-DAPv2.
Raise supported version to "2.1.0".

Signed-off-by: Maximilian Deubel <[email protected]>
Signed-off-by: Johann Fischer <[email protected]>
rerickson1
rerickson1 previously approved these changes Jun 11, 2024
tmon-nordic
tmon-nordic previously approved these changes Jun 12, 2024
jfischer-no and others added 4 commits June 13, 2024 09:45
Add CMSIS DAP sample using USB as interface.

Signed-off-by: Maximilian Deubel <[email protected]>
Signed-off-by: Johann Fischer <[email protected]>
Use k_timepoint_t for timeout handling in the swj_pins function.

Signed-off-by: Maximilian Deubel <[email protected]>
Hardcode the lookup table for SWDP requests.
This is an optimization to save some space.
Documentation was added to understand the values.

Signed-off-by: Maximilian Deubel <[email protected]>
This patch adds documentation for the SWDP API.

Signed-off-by: Maximilian Deubel <[email protected]>
Signed-off-by: Johann Fischer <[email protected]>
@aescolar aescolar merged commit 96112ad into zephyrproject-rtos:main Jun 14, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Binding PR modifies or adds a Device Tree binding area: Samples Samples Experimental Experimental features not enabled by default
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.