+
+ return ERROR_OK;
+}
+
+/**
+ * Sets the SWO transport mode.
+ * @param[in] transport The transport mode. Can be None, SWO_Data or
+ * WinUSB (requires CMSIS-DAP v2).
+ */
+static int cmsis_dap_cmd_dap_swo_transport(uint8_t transport)
+{
+ uint8_t *command = cmsis_dap_handle->command;
+
+ command[0] = CMD_DAP_SWO_TRANSPORT;
+ command[1] = transport;
+
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
+ if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
+ LOG_ERROR("CMSIS-DAP: command CMD_SWO_Transport(%d) failed.", transport);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ return ERROR_OK;
+}
+
+/**
+ * Sets the SWO trace capture mode.
+ * @param[in] mode Trace capture mode. Can be UART or MANCHESTER.
+ */
+static int cmsis_dap_cmd_dap_swo_mode(uint8_t mode)
+{
+ uint8_t *command = cmsis_dap_handle->command;
+
+ command[0] = CMD_DAP_SWO_MODE;
+ command[1] = mode;
+
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
+ if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
+ LOG_ERROR("CMSIS-DAP: command CMD_SWO_Mode(%d) failed.", mode);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ return ERROR_OK;
+}
+
+/**
+ * Sets the baudrate for capturing SWO trace data.
+ * Can be called iteratively to determine supported baudrates.
+ * @param[in] in_baudrate Requested baudrate.
+ * @param[out] dev_baudrate Actual baudrate or 0 (baudrate not configured).
+ * When requested baudrate is not achievable the
+ * closest configured baudrate can be returned or
+ * 0 which indicates that baudrate was not configured.
+ */
+static int cmsis_dap_cmd_dap_swo_baudrate(
+ uint32_t in_baudrate,
+ uint32_t *dev_baudrate)
+{
+ uint8_t *command = cmsis_dap_handle->command;
+
+ command[0] = CMD_DAP_SWO_BAUDRATE;
+ h_u32_to_le(&command[1], in_baudrate);
+
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 4);
+ uint32_t rvbr = le_to_h_u32(&cmsis_dap_handle->response[1]);
+ if (retval != ERROR_OK || rvbr == 0) {
+ LOG_ERROR("CMSIS-DAP: command CMD_SWO_Baudrate(%u) -> %u failed.", in_baudrate, rvbr);
+ if (dev_baudrate)
+ *dev_baudrate = 0;
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ if (dev_baudrate)
+ *dev_baudrate = rvbr;
+
+ return ERROR_OK;
+}
+
+/**
+ * Controls the SWO trace data capture.
+ * @param[in] control Start or stop a trace. Starting capture automatically
+ * flushes any existing trace data in buffers which has
+ * not yet been read.
+ */
+static int cmsis_dap_cmd_dap_swo_control(uint8_t control)
+{
+ uint8_t *command = cmsis_dap_handle->command;
+
+ command[0] = CMD_DAP_SWO_CONTROL;
+ command[1] = control;
+
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
+ if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
+ LOG_ERROR("CMSIS-DAP: command CMD_SWO_Control(%d) failed.", control);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ return ERROR_OK;
+}
+
+/**
+ * Reads the SWO trace status.
+ * @param[out] trace_status The trace's status.
+ * Bit0: Trace Capture (1 - active, 0 - inactive).
+ * Bit6: Trace Stream Error.
+ * Bit7: Trace Buffer Overrun.
+ * @param[out] trace_count Number of bytes in Trace Buffer (not yet read).
+ */
+static int cmsis_dap_cmd_dap_swo_status(
+ uint8_t *trace_status,
+ size_t *trace_count)
+{
+ uint8_t *command = cmsis_dap_handle->command;
+
+ command[0] = CMD_DAP_SWO_STATUS;
+
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("CMSIS-DAP: command CMD_SWO_Status failed.");
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ if (trace_status)
+ *trace_status = cmsis_dap_handle->response[1];
+ if (trace_count)
+ *trace_count = le_to_h_u32(&cmsis_dap_handle->response[2]);
+
+ return ERROR_OK;
+}
+
+/**
+ * Reads the captured SWO trace data from Trace Buffer.
+ * @param[in] max_trace_count Maximum number of Trace Data bytes to read.
+ * @param[out] trace_status The trace's status.
+ * @param[out] trace_count Number of Trace Data bytes read.
+ * @param[out] data Trace Data bytes read.
+ */
+static int cmsis_dap_cmd_dap_swo_data(
+ size_t max_trace_count,
+ uint8_t *trace_status,
+ size_t *trace_count,
+ uint8_t *data)
+{
+ uint8_t *command = cmsis_dap_handle->command;
+
+ command[0] = CMD_DAP_SWO_DATA;
+ h_u16_to_le(&command[1], max_trace_count);
+
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("CMSIS-DAP: command CMD_SWO_Data failed.");
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ *trace_status = cmsis_dap_handle->response[1];
+ *trace_count = le_to_h_u16(&cmsis_dap_handle->response[2]);
+
+ if (*trace_count > 0)
+ memcpy(data, &cmsis_dap_handle->response[4], *trace_count);
+