• Hello, this is a followup to this post https://forum.espruino.com/conversations/378217/#16732065

    SWD protocol is typically used to debug software running on ARM Cortex-M CPUs however it can be also easily used for just doing direct access to memory (RAM, internal flash).

    Bangle 2 has SWD debug pins routed to USB data pins of charging cable so with some extra software and hardware this can be used for talking to Bangle 2 over cable.

    SWD uses 2 pins similar to I2c or bidirectional SPI and the protocol is relatively simple.
    Best introduction I found is here http://markding.github.io/swd_programing_sram/
    Full docs including PDF download https://developer.arm.com/documentation/ihi0031/a?lang=en

    I wrote initial prototype of basic memory read/write over SWD here
    https://github.com/fanoush/EspruinoBoards/blob/master/STLINKV2/swd.js
    I am using STLink V2 clone dongle running Espruino but it should be usable on any ARM based Espruino device with 2 GPIOs available.

    For now I can power up/down the interface and read/write memory

    basic usage is

    swd_setup(clkPin, dioPin)
    swd_init().toString(16) // should print 2ba01477 for cortex M3/4 targets
    dap_poweron() // powers up MEM-AP
    readmem32(0x10001014) // should print bootloader address of nrf52 in UICR
    

    So far there are some small optimizations for speed - for sending 8 or 32 bits shiftOut is used, for receiving of 32 bits software SPI is used (just clk and miso, spi mode 2 seems to work). Unfortunately the protocol is not aligned to byte boundary due to 3bit ACK response in the middle and parity bit in the end so those extra bits are sent by toggling gpios directly.

    Next step is allocating some memory buffer on target device with some signature header (like Segger RTT does) and searching for it over SWD and passing blocks of data over it in both directions.

    One goal is to use any espruino with USB or serial to act as serial to SWD adapter to have Espruino console (and WebIDE) running over it via WebSerial in Chrome.

About

Avatar for fanoush @fanoush started