#include <libswd.h>
#include <urjtag/libswd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
Functions | |
int | swd_bin8_parity_even (char *data, char *parity) |
Data parity calculator, calculates even parity on char type. | |
int | swd_bin32_parity_even (int *data, char *parity) |
Data parity calculator, calculates even parity on integer type. | |
int | swd_bin8_print (char *data) |
Prints binary data of a char value on the screen. | |
int | swd_bin32_print (int *data) |
Prints binary data of an integer value on the screen. | |
char * | swd_bin8_string (char *data) |
Generates string containing binary data of a char value. | |
char * | swd_bin32_string (int *data) |
Generates string containing binary data of an integer value. | |
int | swd_bin8_bitswap (unsigned char *buffer, int bitcount) |
Bit swap helper function that reverse bit order in char *buffer. | |
int | swd_bin32_bitswap (unsigned int *buffer, int bitcount) |
Bit swap helper function that reverse bit order in int *buffer. | |
int | swd_cmdq_init (swd_cmd_t *cmdq) |
Initialize new queue element in memory that becomes a queue root. | |
swd_cmd_t * | swd_cmdq_find_root (swd_cmd_t *cmdq) |
Find queue root (first element). | |
swd_cmd_t * | swd_cmdq_find_tail (swd_cmd_t *cmdq) |
Find queue tail (last element). | |
int | swd_cmdq_append (swd_cmd_t *cmdq, swd_cmd_t *cmd) |
Append element pointed by *cmd at the end of the quque pointed by *cmdq. | |
int | swd_cmdq_free (swd_cmd_t *cmdq) |
Free queue pointed by *cmdq element. | |
int | swd_cmdq_free_head (swd_cmd_t *cmdq) |
Free queue head up to *cmdq element. | |
int | swd_cmdq_free_tail (swd_cmd_t *cmdq) |
Free queue tail starting after *cmdq element. | |
int | swd_cmd_enqueue (swd_ctx_t *swdctx, swd_cmd_t *cmd) |
Append selected command to a context's command queue. | |
int | swd_cmd_enqueue_mosi_request (swd_ctx_t *swdctx, char *request) |
Appends command queue with SWD Request packet header. | |
int | swd_cmd_enqueue_mosi_trn (swd_ctx_t *swdctx) |
Append command queue with Turnaround activating MOSI mode. | |
int | swd_cmd_enqueue_miso_trn (swd_ctx_t *swdctx) |
Append command queue with Turnaround activating MISO mode. | |
int | swd_cmd_enqueue_miso_nbit (swd_ctx_t *swdctx, char *data, int count) |
Append command queue with bus binary read bit-by-bit operation. | |
int | swd_cmd_enqueue_mosi_nbit (swd_ctx_t *swdctx, char *data, int count) |
Append command queue with bus binary write bit-by-bit operation. | |
int | swd_cmd_enqueue_mosi_parity (swd_ctx_t *swdctx, char *parity) |
Append command queue with parity bit write. | |
int | swd_cmd_enqueue_miso_parity (swd_ctx_t *swdctx, char *parity) |
Append command queue with parity bit read. | |
int | swd_cmd_enqueue_miso_data (swd_ctx_t *swdctx, int *data) |
Append command queue with data read. | |
int | swd_cmd_enqueue_miso_data_p (swd_ctx_t *swdctx, int *data, char *parity) |
Append command queue with data and parity read. | |
int | swd_cmd_enqueue_miso_n_data_p (swd_ctx_t *swdctx, int **data, char **parity, int count) |
Append command queue with series of data and parity read. | |
int | swd_cmd_enqueue_mosi_data (swd_ctx_t *swdctx, int *data) |
Append command queue with data and parity write. | |
int | swd_cmd_enqueue_mosi_data_ap (swd_ctx_t *swdctx, int *data) |
Append command queue with data and automatic parity write. | |
int | swd_cmd_enqueue_mosi_data_p (swd_ctx_t *swdctx, int *data, char *parity) |
Append command queue with data and provided parity write. | |
int | swd_cmd_enqueue_mosi_n_data_ap (swd_ctx_t *swdctx, int **data, int count) |
Append command queue with series of data and automatic parity writes. | |
int | swd_cmd_enqueue_mosi_n_data_p (swd_ctx_t *swdctx, int **data, char **parity, int count) |
Append command queue with series of data and provided parity writes. | |
int | swd_cmd_enqueue_miso_ack (swd_ctx_t *swdctx, char *ack) |
Append queue with ACK read. | |
int | swd_cmd_enqueue_mosi_control (swd_ctx_t *swdctx, char *ctlmsg, int len) |
Append command queue with len-octet size control seruence. | |
int | swd_cmd_enqueue_mosi_dap_reset (swd_ctx_t *swdctx) |
Append command queue with SW-DP-RESET sequence. | |
int | swd_cmd_enqueue_mosi_idle (swd_ctx_t *swdctx) |
Append command queue with idle sequence. | |
int | swd_cmd_enqueue_mosi_jtag2swd (swd_ctx_t *swdctx) |
Append command queue with JTAG-TO-SWD DAP-switch sequence. | |
int | swd_cmd_enqueue_mosi_swd2jtag (swd_ctx_t *swdctx) |
Append command queue with SWD-TO-JTAG DAP-switch sequence. | |
char * | swd_cmd_string_cmdtype (swd_cmd_t *cmd) |
int | swd_bitgen8_request (swd_ctx_t *swdctx, char *APnDP, char *RnW, char *addr, char *request) |
Generate 8-bit SWD-REQUEST packet contents with provided parameters. | |
int | swd_drv_mosi_8 (swd_ctx_t *swdctx, char *data, int bits, int nLSBfirst) |
int | swd_drv_mosi_32 (swd_ctx_t *swdctx, int *data, int bits, int nLSBfirst) |
int | swd_drv_miso_8 (swd_ctx_t *swdctx, char *data, int bits, int nLSBfirst) |
int | swd_drv_miso_32 (swd_ctx_t *swdctx, int *data, int bits, int nLSBfirst) |
int | swd_drv_mosi_trn (swd_ctx_t *swdctx, int bits) |
int | swd_drv_miso_trn (swd_ctx_t *swdctx, int bits) |
int | swd_bus_transmit (swd_ctx_t *swdctx, swd_cmd_t *cmd) |
Transmit selected command from the command queue to the interface driver. | |
int | swd_cmdq_flush (swd_ctx_t *swdctx, swd_operation_t operation) |
Flush command queue contents into interface driver. | |
int | swd_bus_setdir_mosi (swd_ctx_t *swdctx) |
Append command queue with TRN WRITE/MOSI, if previous command was READ/MISO. | |
int | swd_bus_setdir_miso (swd_ctx_t *swdctx) |
Append command queue with TRN READ/MISO, if previous command was WRITE/MOSI. | |
int | swd_bus_write_request (swd_ctx_t *swdctx, swd_operation_t operation, char *APnDP, char *RnW, char *addr) |
Perform Request. | |
int | swd_bus_read_ack (swd_ctx_t *swdctx, swd_operation_t operation, char *ack) |
Perform ACK read into *ack and verify received data. | |
int | swd_bus_write_data_p (swd_ctx_t *swdctx, swd_operation_t operation, int *data, char *parity) |
Perform (MOSI) data write with provided parity value. | |
int | swd_bus_write_data_ap (swd_ctx_t *swdctx, swd_operation_t operation, int *data) |
Perform (MOSI) data write with automatic parity calculation. | |
int | swd_bus_read_data_p (swd_ctx_t *swdctx, swd_operation_t operation, int *data, char *parity) |
Perform (MISO) data read. | |
int | swd_bus_write_control (swd_ctx_t *swdctx, swd_operation_t operation, char *ctlmsg, int len) |
Write CONTROL byte to the Target's DAP. | |
int | swd_bus_write_jtag2swd (swd_ctx_t *swdctx, swd_operation_t operation) |
Switch DAP into SW-DP. | |
int | swd_dp_reset (swd_ctx_t *swdctx, swd_operation_t operation) |
Debug Access Port Reset sends 50 CLK with TMS high that brings both SW-DP and JTAG-DP into reset state. | |
int | swd_dp_activate (swd_ctx_t *swdctx, swd_operation_t operation) |
Activate SW-DP by sending out RESET and JTAG-TO-SWD sequence on SWDIOTMS line. | |
int | swd_dp_read_idcode (swd_ctx_t *swdctx, swd_operation_t operation, int *idcode) |
Macro: Read out IDCODE register and return its value on function return. | |
int | swd_dp_detect (swd_ctx_t *swdctx, swd_operation_t operation, int *idcode) |
Macro: Reset target DAP, select SW-DP, read out IDCODE. | |
int | swd_log (swd_ctx_t *swdctx, swd_loglevel_t loglevel, char *msg,...) |
Put a message into swd context log at specified verbosity level. | |
char * | swd_error_string (swd_error_code_t error) |
swd_ctx_t * | swd_init (void) |
LibSWD initialization routine. | |
int | swd_deinit_ctx (swd_ctx_t *swdctx) |
De-initialize selected swd context and free its memory. | |
int | swd_deinit_cmdq (swd_ctx_t *swdctx) |
De-initialize command queue and free its memory on selected swd context. | |
int | swd_deinit (swd_ctx_t *swdctx) |
De-initialize selected swd context and its command queue. |
int swd_bin32_bitswap | ( | unsigned int * | buffer, | |
int | bitcount | |||
) |
Bit swap helper function that reverse bit order in int *buffer.
Most Significant Bit becomes Least Significant Bit. It is possible to swap only n-bits from int (32-bit) *buffer.
*buffer | unsigned char (32-bit) data pointer. | |
bitcount | how many bits to swap. |
int swd_bin32_parity_even | ( | int * | data, | |
char * | parity | |||
) |
Data parity calculator, calculates even parity on integer type.
*data | source data pointer. | |
*parity | resulting data pointer. |
int swd_bin32_print | ( | int * | data | ) |
Prints binary data of an integer value on the screen.
*data | source data pointer. |
char* swd_bin32_string | ( | int * | data | ) |
Generates string containing binary data of an integer value.
*data | source data pointer. |
int swd_bin8_bitswap | ( | unsigned char * | buffer, | |
int | bitcount | |||
) |
Bit swap helper function that reverse bit order in char *buffer.
Most Significant Bit becomes Least Significant Bit. It is possible to swap only n-bits from char (8-bit) *buffer.
*buffer | unsigned char (8-bit) data pointer. | |
bitcount | how many bits to swap. |
int swd_bin8_parity_even | ( | char * | data, | |
char * | parity | |||
) |
Data parity calculator, calculates even parity on char type.
Some comments on the function behavior.
*data | source data pointer. | |
*parity | resulting data pointer. |
int swd_bin8_print | ( | char * | data | ) |
Prints binary data of a char value on the screen.
*data | source data pointer. |
char* swd_bin8_string | ( | char * | data | ) |
Generates string containing binary data of a char value.
*data | source data pointer. |
int swd_bitgen8_request | ( | swd_ctx_t * | swdctx, | |
char * | APnDP, | |||
char * | RnW, | |||
char * | addr, | |||
char * | request | |||
) |
Generate 8-bit SWD-REQUEST packet contents with provided parameters.
Note that parity bit value is calculated automatically.
*swdctx | swd context pointer. | |
*APnDP | AccessPort (high) or DebugPort (low) access type pointer. | |
*RnW | Read (high) or Write (low) operation type pointer. | |
*addr | target register address value pointer. | |
*request | pointer where to store resulting packet. |
int swd_bus_read_ack | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
char * | ack | |||
) |
Perform ACK read into *ack and verify received data.
*swdctx | swd context pointer. | |
operation | type of action to perform with generated request. | |
*ack | pointer to the result location. |
int swd_bus_read_data_p | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
int * | data, | |||
char * | parity | |||
) |
Perform (MISO) data read.
*swdctx | swd context pointer. | |
operation | type of action to perform on generated command. | |
*data | payload value pointer. | |
*parity | payload parity value pointer. |
int swd_bus_setdir_miso | ( | swd_ctx_t * | swdctx | ) |
Append command queue with TRN READ/MISO, if previous command was WRITE/MOSI.
*swdctx | swd context pointer. |
int swd_bus_setdir_mosi | ( | swd_ctx_t * | swdctx | ) |
Append command queue with TRN WRITE/MOSI, if previous command was READ/MISO.
*swdctx | swd context pointer. |
Transmit selected command from the command queue to the interface driver.
*swdctx | swd context pointer. | |
*cmd | pointer to the command to be sent. |
int swd_bus_write_control | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
char * | ctlmsg, | |||
int | len | |||
) |
Write CONTROL byte to the Target's DAP.
*swdctx | swd context. | |
operation | can be SWD_OPERATION_ENQUEUE or SWD_OPERATION_EXECUTE. | |
*ctlmsg | byte/char array that contains control payload. | |
len | number of bytes in the *ctlmsg to send. |
int swd_bus_write_data_ap | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
int * | data | |||
) |
Perform (MOSI) data write with automatic parity calculation.
*swdctx | swd context pointer. | |
operation | type of action to perform on generated command. | |
*data | payload value pointer. |
int swd_bus_write_data_p | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
int * | data, | |||
char * | parity | |||
) |
Perform (MOSI) data write with provided parity value.
*swdctx | swd context pointer. | |
operation | type of action to perform on generated command. | |
*data | payload value pointer. | |
*parity | payload parity value pointer. |
int swd_bus_write_jtag2swd | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation | |||
) |
Switch DAP into SW-DP.
According to ARM documentation target's DAP use JTAG transport by default and so JTAG-DP is active after power up. To use SWD user must perform predefined sequence on SWDIO/TMS lines, then read out the IDCODE to ensure proper SW-DP operation. This function only sends switching sequence.
*swdctx | swd context. | |
operation | type, can be ENQEUE or EXECUTE. |
int swd_bus_write_request | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
char * | APnDP, | |||
char * | RnW, | |||
char * | addr | |||
) |
Perform Request.
*swdctx | swd context pointer. | |
operation | type of action to perform with generated request. | |
*APnDP | AccessPort (high) or DebugPort (low) access value pointer. | |
*RnW | Read (high) or Write (low) access value pointer. | |
*addr | target register address value pointer. |
Append selected command to a context's command queue.
*swdctx | swd context pointer containing the command queue. | |
*cmd | command to be appended to the context's command queue. |
int swd_cmd_enqueue_miso_ack | ( | swd_ctx_t * | swdctx, | |
char * | ack | |||
) |
Append queue with ACK read.
*swdctx | swd context pointer. | |
*ack | packet value pointer. |
int swd_cmd_enqueue_miso_data | ( | swd_ctx_t * | swdctx, | |
int * | data | |||
) |
Append command queue with data read.
*swdctx | swd context pointer. | |
*data | data pointer. |
int swd_cmd_enqueue_miso_data_p | ( | swd_ctx_t * | swdctx, | |
int * | data, | |||
char * | parity | |||
) |
Append command queue with data and parity read.
*swdctx | swd context pointer. | |
*data | data value pointer. | |
*parity | parity value pointer. |
int swd_cmd_enqueue_miso_n_data_p | ( | swd_ctx_t * | swdctx, | |
int ** | data, | |||
char ** | parity, | |||
int | count | |||
) |
Append command queue with series of data and parity read.
*swdctx | swd context pointer. | |
**data | data value array pointer. | |
**parity | parity value array pointer. | |
count | number of (data+parity) elements to read. |
int swd_cmd_enqueue_miso_nbit | ( | swd_ctx_t * | swdctx, | |
char * | data, | |||
int | count | |||
) |
Append command queue with bus binary read bit-by-bit operation.
This function will append command to the queue for each bit, and store one bit into single char array element, so read is not constrained to 8 bits. On error memory is released and apropriate error code is returned. Important: Memory pointed by *data must be allocated prior call!
*swdctx | swd context pointer. | |
**data | allocated data array to write result into. | |
count | number of bits to read (also the **data size). |
int swd_cmd_enqueue_miso_parity | ( | swd_ctx_t * | swdctx, | |
char * | parity | |||
) |
Append command queue with parity bit read.
*swdctx | swd context pointer. | |
*parity | parity value pointer. |
int swd_cmd_enqueue_miso_trn | ( | swd_ctx_t * | swdctx | ) |
Append command queue with Turnaround activating MISO mode.
*swdctx | swd context pointer. |
int swd_cmd_enqueue_mosi_control | ( | swd_ctx_t * | swdctx, | |
char * | ctlmsg, | |||
int | len | |||
) |
Append command queue with len-octet size control seruence.
This control sequence can be used for instance to send payload of packets switching DAP between JTAG and SWD mode.
*swdctx | swd context pointer. | |
*ctlmsg | control message array pointer. | |
len | number of elements to send from *ctlmsg. |
int swd_cmd_enqueue_mosi_dap_reset | ( | swd_ctx_t * | swdctx | ) |
Append command queue with SW-DP-RESET sequence.
*swdctx | swd context pointer. |
int swd_cmd_enqueue_mosi_data | ( | swd_ctx_t * | swdctx, | |
int * | data | |||
) |
Append command queue with data and parity write.
*swdctx | swd context pointer. | |
*data | data value pointer. |
int swd_cmd_enqueue_mosi_data_ap | ( | swd_ctx_t * | swdctx, | |
int * | data | |||
) |
Append command queue with data and automatic parity write.
*swdctx | swd context pointer. | |
*data | data value pointer. |
int swd_cmd_enqueue_mosi_data_p | ( | swd_ctx_t * | swdctx, | |
int * | data, | |||
char * | parity | |||
) |
Append command queue with data and provided parity write.
*swdctx | swd context pointer. | |
*data | data value pointer. | |
*parity | parity value pointer. |
int swd_cmd_enqueue_mosi_idle | ( | swd_ctx_t * | swdctx | ) |
Append command queue with idle sequence.
*swdctx | swd context pointer. |
int swd_cmd_enqueue_mosi_jtag2swd | ( | swd_ctx_t * | swdctx | ) |
Append command queue with JTAG-TO-SWD DAP-switch sequence.
*swdctx | swd context pointer. |
int swd_cmd_enqueue_mosi_n_data_ap | ( | swd_ctx_t * | swdctx, | |
int ** | data, | |||
int | count | |||
) |
Append command queue with series of data and automatic parity writes.
*swdctx | swd context pointer. | |
**data | data value array pointer. | |
count | number of (data+parity) elements to read. |
int swd_cmd_enqueue_mosi_n_data_p | ( | swd_ctx_t * | swdctx, | |
int ** | data, | |||
char ** | parity, | |||
int | count | |||
) |
Append command queue with series of data and provided parity writes.
*swdctx | swd context pointer. | |
**data | data value array pointer. | |
**parity | parity value array pointer. | |
count | number of (data+parity) elements to read. |
int swd_cmd_enqueue_mosi_nbit | ( | swd_ctx_t * | swdctx, | |
char * | data, | |||
int | count | |||
) |
Append command queue with bus binary write bit-by-bit operation.
This function will append command to the queue for each bit and store one bit into single char array element, so read is not constrained to 8 bits. On error memory is released and apropriate error code is returned. Important: Memory pointed by *data must be allocated prior call!
*swdctx | swd context pointer. | |
**data | allocated data array to write result into. | |
count | number of bits to read (also the **data size). |
int swd_cmd_enqueue_mosi_parity | ( | swd_ctx_t * | swdctx, | |
char * | parity | |||
) |
Append command queue with parity bit write.
*swdctx | swd context pointer. | |
*parity | parity value pointer. |
int swd_cmd_enqueue_mosi_request | ( | swd_ctx_t * | swdctx, | |
char * | request | |||
) |
Appends command queue with SWD Request packet header.
Note that contents is not validated, so bad request can be sent as well.
*swdctx | swd context pointer. | |
*request | pointer to the 8-bit request payload. |
int swd_cmd_enqueue_mosi_swd2jtag | ( | swd_ctx_t * | swdctx | ) |
Append command queue with SWD-TO-JTAG DAP-switch sequence.
*swdctx | swd context pointer. |
int swd_cmd_enqueue_mosi_trn | ( | swd_ctx_t * | swdctx | ) |
Append command queue with Turnaround activating MOSI mode.
*swdctx | swd context pointer. |
Append element pointed by *cmd at the end of the quque pointed by *cmdq.
After this operation queue will be pointed by appended element (ie. last element added becomes actual quque pointer to show what was added recently).
*cmdq | pointer to any element on command queue | |
*cmd | pointer to the command to be appended |
Find queue root (first element).
*cmdq | pointer to any queue element |
Find queue tail (last element).
*cmdq | pointer to any queue element |
int swd_cmdq_flush | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation | |||
) |
Flush command queue contents into interface driver.
Operation is specified by SWD_OPERATION and can be used to select how to flush the queue, ie. head-only, tail-only, one, all, etc.
*swdctx | swd context pointer. | |
operation | tells how to flush the queue. |
int swd_cmdq_free | ( | swd_cmd_t * | cmdq | ) |
Free queue pointed by *cmdq element.
*cmdq | pointer to any element on command queue |
int swd_cmdq_free_head | ( | swd_cmd_t * | cmdq | ) |
Free queue head up to *cmdq element.
*cmdq | pointer to the element that becomes new queue root. |
int swd_cmdq_free_tail | ( | swd_cmd_t * | cmdq | ) |
Free queue tail starting after *cmdq element.
*cmdq | pointer to the last element on the new queue. |
int swd_cmdq_init | ( | swd_cmd_t * | cmdq | ) |
Initialize new queue element in memory that becomes a queue root.
*cmdq | pointer to the command queue element of type swd_cmd_t |
int swd_deinit | ( | swd_ctx_t * | swdctx | ) |
De-initialize selected swd context and its command queue.
*swdctx | swd context pointer. |
int swd_deinit_cmdq | ( | swd_ctx_t * | swdctx | ) |
De-initialize command queue and free its memory on selected swd context.
*swdctx | swd context pointer. |
int swd_deinit_ctx | ( | swd_ctx_t * | swdctx | ) |
De-initialize selected swd context and free its memory.
Note: This function will not free command queue for selected context!
*swdctx | swd context pointer. |
int swd_dp_activate | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation | |||
) |
Activate SW-DP by sending out RESET and JTAG-TO-SWD sequence on SWDIOTMS line.
*swdctx | swd context. |
int swd_dp_detect | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
int * | idcode | |||
) |
Macro: Reset target DAP, select SW-DP, read out IDCODE.
This is the proper SW-DP initialization as stated by ARM Information Center.
*swdctx | swd context pointer. | |
operation | type (SWD_OPERATION_ENQUEUE or SWD_OPERATION_EXECUTE). |
int swd_dp_read_idcode | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation, | |||
int * | idcode | |||
) |
Macro: Read out IDCODE register and return its value on function return.
*swdctx | swd context pointer. | |
operation | operation type. |
int swd_dp_reset | ( | swd_ctx_t * | swdctx, | |
swd_operation_t | operation | |||
) |
Debug Access Port Reset sends 50 CLK with TMS high that brings both SW-DP and JTAG-DP into reset state.
*swdctx | swd context pointer. | |
operation | type (SWD_OPERATION_ENQUEUE or SWD_OPERATION_EXECUTE). |
swd_ctx_t* swd_init | ( | void | ) |
LibSWD initialization routine.
It should be called prior any operation made with libswd. It initializes command queue and basic parameters for context that is returned as pointer.
int swd_log | ( | swd_ctx_t * | swdctx, | |
swd_loglevel_t | loglevel, | |||
char * | msg, | |||
... | ||||
) |
Put a message into swd context log at specified verbosity level.
If specified message's log level is lower than actual context configuration, message will be omitted. Verbosity level increases from 0 (silent) to 4 (debug).
*swdctx | swd context. | |
loglevel | at which to put selected message. | |
*msg | message body with variable arguments as in "printf". |