00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00063 #ifndef __LIBSWD_H__
00064 #define __LIBSWD_H__
00065
00066
00067
00069
00070 #define SWD_REQUEST_START_BITNUM 7
00071
00072 #define SWD_REQUEST_APnDP_BITNUM 6
00073
00074 #define SWD_REQUEST_RnW_BITNUM 5
00075
00076 #define SWD_REQUEST_ADDR_BITNUM 4
00077
00078 #define SWD_REQUEST_A2_BITNUM 4
00079
00080 #define SWD_REQUEST_A3_BITNUM 3
00081
00082 #define SWD_REQUEST_PARITY_BITNUM 2
00083
00084 #define SWD_REQUEST_STOP_BITNUM 1
00085
00086 #define SWD_REQUEST_PARK_BITNUM 0
00087
00089 #define SWD_REQUEST_START_VAL 1
00090
00091 #define SWD_REQUEST_STOP_VAL 0
00092
00093 #define SWD_REQUEST_PARK_VAL 1
00094
00095 #define SWD_REQUEST_BITLEN 8
00096
00098 #define SWD_ADDR_MINVAL 0
00099
00100 #define SWD_ADDR_MAXVAL 3
00101
00103 #define SWD_ACK_BITLEN 3
00104
00105 #define SWD_ACK_OK_VAL 4
00106
00107 #define SWD_ACK_WAIT_VAL 2
00108
00109 #define SWD_ACK_FAULT_VAL 1
00110
00112 #define SWD_DP_ADDR_IDCODE 0
00113
00114 #define SWD_DP_ADDR_ABORT 0
00115
00116 #define SWD_DP_ADDR_CTRLSTAT 1
00117
00118 #define SWD_DP_ADDR_WCR 1
00119
00120 #define SWD_DP_ADDR_RESEND 2
00121
00122 #define SWD_DP_ADDR_SELECT 2
00123
00124 #define SWD_DP_ADDR_RDBUF 3
00125
00127
00128 #define SWD_ABORT_BITNUM_DAPABORT 0
00129
00130 #define SWD_ABORT_BITNUM_DSTKCMPCLR 1
00131
00132 #define SWD_ABORT_BITNUM_DSTKERRCLR 2
00133
00134 #define SWD_ABORT_BITNUM_DWDERRCLR 3
00135
00136 #define SWD_ABORT_BITNUM_DORUNERRCLR 4
00137
00139
00140 #define SWD_CTRLSTAT_BITNUM_ORUNDETECT 0
00141
00142 #define SWD_CTRLSTAT_BITNUM_OSTICKYORUN 1
00143
00144 #define SWD_CTRLSTAT_BITNUM_OTRNMODE 2
00145
00146 #define SWD_CTRLSTAT_BITNUM_OSTICKYCMP 4
00147
00148 #define SWD_CTRLSTAT_BITNUM_OSTICKYERR 5
00149
00150 #define SWD_CTRLSTAT_BITNUM_OREADOK 6
00151
00152 #define SWD_CTRLSTAT_BITNUM_OWDATAERR 7
00153
00154 #define SWD_CTRLSTAT_BITNUM_OMASKLANE 8
00155
00156 #define SWD_CTRLSTAT_BITNUM_OTRNCNT 12
00157
00158 #define SWD_CTRLSTAT_BITNUM_OCDBGRSTREQ 26
00159
00160 #define SWD_CTRLSTAT_BITNUM_OCDBGRSTACK 27
00161
00162 #define SWD_CTRLSTAT_BITNUM_OCDBGPWRUPREQ 28
00163
00164 #define SWD_CTRLSTAT_BITNUM_OCDBGPWRUPACK 29
00165
00166 #define SWD_CTRLSTAT_BITNUM_OCSYSPWRUPREQ 30
00167
00168 #define SWD_CTRLSTAT_BITNUM_OCSYSPWRUPACK 31
00169
00170
00171 #define SWD_MASKLANE_0 0b0001
00172
00173 #define SWD_MASKLANE_1 0b0010
00174
00175 #define SWD_MASKLANE_2 0b0100
00176
00177 #define SWD_MASKLANE_3 0b1000
00178
00180
00181 #define SWD_SELECT_BITNUM_CTRLSEL 0
00182
00183 #define SWD_SELECT_BITNUM_APBANKSEL 4
00184
00185 #define SWD_SELECT_BITNUM_APSEL 24
00186
00188
00189 #define SWD_WCR_BITNUM_PRESCALER 0 ///< PRESCALER bit number.
00190
00191 #define SWD_WCR_BITNUM_WIREMODE 6 ///< WIREMODE bit number.
00192
00193 #define SWD_WCR_BITNUM_TURNROUND 8 ///< TURNROUND bit number.
00194
00195
00196 #define SWD_TURNROUND_1_CODE 0 ///< TRN takes one CLK cycle.
00197 #define SWD_TURNROUND_1_VAL 1
00198
00199 #define SWD_TURNROUND_2_CODE 1 ///< TRN takes two CLK cycles.
00200 #define SWD_TURNROUNT_2_VAL 2
00201
00202 #define SWD_TURNROUND_3_CODE 2 ///< TRN takes three CLK cycles.
00203 #define SWD_TURNROUND_3_VAL 3
00204
00205 #define SWD_TURNROUND_4_CODE 3 ///< TRN takes four CLK cycles. ????
00206 #define SWD_TURNROUND_4_VAL 4
00207
00208 #define SWD_TURNROUND_MIN_VAL SWD_TURNROUND_1_VAL ///< shortest TRN time.
00209 #define SWD_TURNROUND_MIN_CODE SWD_TURNOUND_1_CODE
00210
00211 #define SWD_TURNROUND_MAX_VAL SWD_TURNROUND_4_VAL ///< longest TRN time.
00212 #define SWD_TURNROUND_MAX_CODE SWD_TURNROUND_4_CODE
00213
00214 #define SWD_TURNROUND_DEFAULT_VAL SWD_TURNROUND_1_VAL ///< Default TRN length is one CLK.
00215
00217
00218 #define AHB_AP_CONTROLSTATUS 0x00 ///< R/W, 32bit, reset value: 0x43800042
00219
00220 #define AHB_AP_TAR 0x04 ///< R/W, 32bit, reset value: 0x00000000
00221
00222 #define AHB_AP_DRW 0x0C ///< R/W, 32bit
00223
00224 #define AHB_AP_BD0 0x10 ///< R/W, 32bit
00225
00226 #define AHB_AP_BD1 0x14 ///< R/W, 32bit
00227
00228 #define AHB_AP_BD2 0x18 ///< R/W, 32bit
00229
00230 #define AHB_AP_BD3 0x1C ///< R/W, 32bit
00231
00232 #define AHB_AP_DROMT 0xF8 ///< RO, 32bit, reset value: 0xE00FF000
00233
00234 #define AHB_AP_IDR 0xFC ///< RO, 32bit, reset value: 0x24770001
00235
00237
00238 static const char SWD_CMD_SWDPRESET[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
00240 static const char SWD_CMD_JTAG2SWD[] = {0x79, 0xe7};
00242 static const char SWD_CMD_SWD2JTAG[] = {0x3c, 0xe7};
00243
00245
00246 typedef enum SWD_ERROR_CODE {
00247 SWD_OK = 0,
00248 SWD_ERROR_GENERAL =-1,
00249 SWD_ERROR_NULLPOINTER =-2,
00250 SWD_ERROR_NULLQUEUE =-3,
00251 SWD_ERROR_NULLTRN =-4,
00252 SWD_ERROR_PARAM =-5,
00253 SWD_ERROR_OUTOFMEM =-6,
00254 SWD_ERROR_RESULT =-7,
00255 SWD_ERROR_RANGE =-8,
00256 SWD_ERROR_DEFINITION =-9,
00257 SWD_ERROR_NULLCONTEXT =-10,
00258 SWD_ERROR_QUEUE =-11,
00259 SWD_ERROR_ADDR =-12,
00260 SWD_ERROR_APnDP =-13,
00261 SWD_ERROR_RnW =-14,
00262 SWD_ERROR_PARITY =-15,
00263 SWD_ERROR_ACK =-16,
00264 SWD_ERROR_ACKUNKNOWN =-19,
00265 SWD_ERROR_ACKNOTDONE =-20,
00266 SWD_ERROR_ACKMISSING =-21,
00267 SWD_ERROR_ACKMISMATCH =-22,
00268 SWD_ERROR_ACKORDER =-23,
00269 SWD_ERROR_BADOPCODE =-24,
00270 SWD_ERROR_NODATACMD =-25,
00271 SWD_ERROR_DATAPTR =-26,
00272 SWD_ERROR_NOPARITYCMD =-27,
00273 SWD_ERROR_PARITYPTR =-28,
00274 SWD_ERROR_NOTDONE =-29,
00275 SWD_ERROR_QUEUEROOT =-30,
00276 SWD_ERROR_QUEUETAIL =-31,
00277 SWD_ERROR_BADCMDTYPE =-32,
00278 SWD_ERROR_BADCMDDATA =-33,
00279 SWD_ERROR_TURNAROUND =-34,
00280 SWD_ERROR_DRIVER =-35,
00281 SWD_ERROR_ACK_WAIT =-36,
00282 SWD_ERROR_ACK_FAULT =-37,
00283 SWD_ERROR_QUEUENOTFREE=-38,
00284 SWD_ERROR_TRANSPORT =-39,
00285 SWD_ERROR_DIRECTION =-40,
00286 SWD_ERROR_LOGLEVEL =-41
00287 } swd_error_code_t;
00288
00290
00291 typedef enum SWD_LOGLEVEL{
00292 SWD_LOGLEVEL_MIN = 0,
00293 SWD_LOGLEVEL_SILENT = 0,
00294 SWD_LOGLEVEL_ERROR = 1,
00295 SWD_LOGLEVEL_WARNING = 2,
00296 SWD_LOGLEVEL_INFO = 3,
00297 SWD_LOGLEVEL_DEBUG = 4,
00298 SWD_LOGLEVEL_MAX = 4
00299 } swd_loglevel_t;
00300
00302
00303 #define SWD_DATA_MAXBITCOUNT 32
00304
00305 #define SWD_DATA_BYTESIZE 8
00306
00307 #define SWD_DATA_BITLEN 32
00308
00309 #define SWD_CMDQLEN_DEFAULT 1024;
00310
00316
00317 typedef enum SWD_CMDTYPE {
00318 SWD_CMDTYPE_MOSI_DATA =-7,
00319 SWD_CMDTYPE_MOSI_REQUEST =-6,
00320 SWD_CMDTYPE_MOSI_TRN =-5,
00321 SWD_CMDTYPE_MOSI_PARITY =-4,
00322 SWD_CMDTYPE_MOSI_BITBANG =-3,
00323 SWD_CMDTYPE_MOSI_CONTROL =-2,
00324 SWD_CMDTYPE_MOSI =-1,
00325 SWD_CMDTYPE_UNDEFINED =0,
00326 SWD_CMDTYPE_MISO =1,
00327 SWD_CMDTYPE_MISO_ACK =2,
00328 SWD_CMDTYPE_MISO_BITBANG =3,
00329 SWD_CMDTYPE_MISO_PARITY =4,
00330 SWD_CMDTYPE_MISO_TRN =5,
00331 SWD_CMDTYPE_MISO_DATA =6
00332 } swd_cmdtype_t;
00333
00335 typedef enum SWD_SHIFTDIR {
00336 SWD_DIR_LSBFIRST =0,
00337 SWD_DIR_MSBFIRST =1
00338 } swd_shiftdir_t;
00339
00341 typedef enum SWD_OPERATION {
00342 SWD_OPERATION_FIRST =1,
00343 SWD_OPERATION_ENQUEUE =1,
00344 SWD_OPERATION_EXECUTE =2,
00345 SWD_OPERATION_TRANSMIT_HEAD =3,
00346 SWD_OPERATION_TRANSMIT_TAIL =4,
00347 SWD_OPERATION_TRANSMIT_ALL =5,
00348 SWD_OPERATION_TRANSMIT_ONE =6,
00349 SWD_OPERATION_TRANSMIT_LAST =7,
00350 SWD_OPERATION_LAST =7
00351 } swd_operation_t;
00352
00359 typedef struct swd_cmd_t {
00360 union {
00361 char TRNnMOSI;
00362 char request;
00363 char ack;
00364 int misodata;
00365 int mosidata;
00366 int data32;
00367 char misobit;
00368 char mosibit;
00369 char parity;
00370 char control;
00371 char data8;
00372 };
00373 char bits;
00374 swd_cmdtype_t cmdtype;
00375 char done;
00376 struct swd_cmd_t *prev, *next;
00377 } swd_cmd_t;
00378
00380 typedef struct {
00381 char initialized;
00382 char trnlen;
00383 int maxcmdqlen;
00384 swd_loglevel_t loglevel;
00385 } swd_context_config_t;
00386
00388 typedef struct {
00389 char ack;
00390 char parity;
00391 int idcode;
00392 int abort;
00393 int ctrlstat;
00394 int wcr;
00395 int select;
00396 int rdbuf;
00397 } swd_swdp_t;
00398
00400 typedef struct {
00401 int controlstatus;
00402 int tar;
00403 int drw;
00404 int bd0;
00405 int bd1;
00406 int bd2;
00407 int bd3;
00408 int dromt;
00409 int idr;
00410 } swd_ahbap_t;
00411
00415 typedef struct {
00416 void *device;
00417 } swd_driver_t;
00418
00420 typedef enum {
00421 SWD_FALSE=0,
00422 SWD_TRUE=1
00423 } swd_bool_t;
00424
00434 typedef struct {
00435 swd_cmd_t *cmdq;
00436 swd_context_config_t config;
00437 swd_driver_t *driver;
00438 swd_swdp_t misoswdp;
00439 swd_swdp_t mosiswdp;
00440 swd_ahbap_t misoahbap;
00441 swd_ahbap_t mosiahbap;
00442 } swd_ctx_t;
00443
00446 int swd_bin8_parity_even(char *data, char *parity);
00447 int swd_bin32_parity_even(int *data, char *parity);
00448 int swd_bin8_print(char *data);
00449 int swd_bin32_print(int *data);
00450 char *swd_bin8_string(char *data);
00451 char *swd_bin32_string(int *data);
00452 int swd_bin8_bitswap(unsigned char *buffer, int bitcount);
00453 int swd_bin32_bitswap(unsigned int *buffer, int bitcount);
00454
00455 int swd_cmdq_init(swd_cmd_t *cmdq);
00456 swd_cmd_t* swd_cmdq_find_root(swd_cmd_t *cmdq);
00457 swd_cmd_t* swd_cmdq_find_tail(swd_cmd_t *cmdq);
00458 int swd_cmdq_append(swd_cmd_t *cmdq, swd_cmd_t *cmd);
00459 int swd_cmdq_free(swd_cmd_t *cmdq);
00460 int swd_cmdq_free_head(swd_cmd_t *cmdq);
00461 int swd_cmdq_free_tail(swd_cmd_t *cmdq);
00462
00463 int swd_cmd_enqueue(swd_ctx_t *swdctx, swd_cmd_t *cmd);
00464 int swd_cmd_enqueue_mosi_request(swd_ctx_t *swdctx, char *request);
00465 int swd_cmd_enqueue_mosi_trn(swd_ctx_t *swdctx);
00466 int swd_cmd_enqueue_miso_trn(swd_ctx_t *swdctx);
00467 int swd_cmd_enqueue_miso_nbit(swd_ctx_t *swdctx, char **data, int count);
00468 int swd_cmd_enqueue_mosi_nbit(swd_ctx_t *swdctx, char *data, int count);
00469 int swd_cmd_enqueue_mosi_parity(swd_ctx_t *swdctx, char *parity);
00470 int swd_cmd_enqueue_miso_parity(swd_ctx_t *swdctx, char *parity);
00471 int swd_cmd_enqueue_miso_data(swd_ctx_t *swdctx, int *data);
00472 int swd_cmd_enqueue_miso_data_p(swd_ctx_t *swdctx, int *data, char *parity);
00473 int swd_cmd_enqueue_miso_n_data_p(swd_ctx_t *swdctx, int **data, char **parity, int count);
00474 int swd_cmd_enqueue_mosi_data(swd_ctx_t *swdctx, int *data);
00475 int swd_cmd_enqueue_mosi_data_ap(swd_ctx_t *swdctx, int *data);
00476 int swd_cmd_enqueue_mosi_data_p(swd_ctx_t *swdctx, int *data, char *parity);
00477 int swd_cmd_enqueue_mosi_n_data_ap(swd_ctx_t *swdctx, int **data, int count);
00478 int swd_cmd_enqueue_mosi_n_data_p(swd_ctx_t *swdctx, int **data, char **parity, int count);
00479 int swd_cmd_enqueue_miso_ack(swd_ctx_t *swdctx, char *ack);
00480 int swd_cmd_enqueue_mosi_control(swd_ctx_t *swdctx, char *ctlmsg, int len);
00481 int swd_cmd_enqueue_mosi_dap_reset(swd_ctx_t *swdctx);
00482 int swd_cmd_enqueue_mosi_jtag2swd(swd_ctx_t *swdctx);
00483 int swd_cmd_enqueue_mosi_swd2jtag(swd_ctx_t *swdctx);
00484
00485 char *swd_cmd_string_cmdtype(swd_cmd_t *cmd);
00486
00487 int swd_bus_setdir_mosi(swd_ctx_t *swdctx);
00488 int swd_bus_setdir_miso(swd_ctx_t *swdctx);
00489 int swd_bus_transmit(swd_ctx_t *swdctx, swd_cmd_t *cmd);
00490
00491 int swd_bitgen8_request(swd_ctx_t *swdctx, char *APnDP, char *RnW, char *addr, char *request);
00492
00493 extern int swd_drv_mosi_8(swd_ctx_t *swdctx, char *data, int bits, int nLSBfirst);
00494 extern int swd_drv_mosi_32(swd_ctx_t *swdctx, int *data, int bits, int nLSBfirst);
00495 extern int swd_drv_miso_8(swd_ctx_t *swdctx, char *data, int bits, int nLSBfirst);
00496 extern int swd_drv_miso_32(swd_ctx_t *swdctx, int *data, int bits, int nLSBfirst);
00497
00498 int swd_cmdq_flush(swd_ctx_t *swdctx, swd_operation_t operation);
00499
00500 int swd_mosi_request
00501 (swd_ctx_t *swdctx, swd_operation_t operation, char *APnDP, char *RnW, char *addr);
00502 int swd_miso_ack(swd_ctx_t *swdctx, swd_operation_t operation, char *ack);
00503 int swd_mosi_data_p(swd_ctx_t *swdctx, swd_operation_t operation, int *data, char *parity);
00504 int swd_mosi_data_ap(swd_ctx_t *swdctx, swd_operation_t operation, int *data);
00505 int swd_miso_data_p(swd_ctx_t *swdctx, swd_operation_t operation, int *data, char *parity);
00506 int swd_mosi_control(swd_ctx_t *swdctx, swd_operation_t operation, char *ctlmsg, int len);
00507 int swd_mosi_jtag2swd(swd_ctx_t *swdctx, swd_operation_t operation);
00508 int swd_miso_idcode(swd_ctx_t *swdctx, swd_operation_t operation, int *idcode, char *ack, char *parity);
00509
00510 int swd_dap_reset(swd_ctx_t *swdctx, swd_operation_t operation);
00511 int swd_dap_select_swj(swd_ctx_t *swdctx, swd_operation_t operation);
00512 int swd_dap_idcode(swd_ctx_t *swdctx, swd_operation_t operation);
00513 int swd_dap_detect(swd_ctx_t *swdctx, swd_operation_t operation);
00514
00515 int swd_log(swd_ctx_t *swdctx, swd_loglevel_t loglevel, char *msg, ...);
00516 char *swd_error_string(swd_error_code_t error);
00517
00518 swd_ctx_t *swd_init(void);
00519 int swd_deinit_ctx(swd_ctx_t *swdctx);
00520 int swd_deinit_cmdq(swd_ctx_t *swdctx);
00521 int swd_deinit(swd_ctx_t *swdctx);
00522
00523 int swd_drv_mosi_trn(swd_ctx_t *swdctx, int clks);
00524 int swd_drv_miso_trn(swd_ctx_t *swdctx, int clks);
00525
00526 #endif