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, 0x00};
00240 static const char SWD_CMD_JTAG2SWD[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x79, 0xe7};
00242 static const char SWD_CMD_SWD2JTAG[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3c, 0xe7};
00244 static const char SWD_CMD_IDLE[] = {0x00};
00245
00247
00248 typedef enum SWD_ERROR_CODE {
00249 SWD_OK = 0,
00250 SWD_ERROR_GENERAL =-1,
00251 SWD_ERROR_NULLPOINTER =-2,
00252 SWD_ERROR_NULLQUEUE =-3,
00253 SWD_ERROR_NULLTRN =-4,
00254 SWD_ERROR_PARAM =-5,
00255 SWD_ERROR_OUTOFMEM =-6,
00256 SWD_ERROR_RESULT =-7,
00257 SWD_ERROR_RANGE =-8,
00258 SWD_ERROR_DEFINITION =-9,
00259 SWD_ERROR_NULLCONTEXT =-10,
00260 SWD_ERROR_QUEUE =-11,
00261 SWD_ERROR_ADDR =-12,
00262 SWD_ERROR_APnDP =-13,
00263 SWD_ERROR_RnW =-14,
00264 SWD_ERROR_PARITY =-15,
00265 SWD_ERROR_ACK =-16,
00266 SWD_ERROR_ACKUNKNOWN =-19,
00267 SWD_ERROR_ACKNOTDONE =-20,
00268 SWD_ERROR_ACKMISSING =-21,
00269 SWD_ERROR_ACKMISMATCH =-22,
00270 SWD_ERROR_ACKORDER =-23,
00271 SWD_ERROR_BADOPCODE =-24,
00272 SWD_ERROR_NODATACMD =-25,
00273 SWD_ERROR_DATAPTR =-26,
00274 SWD_ERROR_NOPARITYCMD =-27,
00275 SWD_ERROR_PARITYPTR =-28,
00276 SWD_ERROR_NOTDONE =-29,
00277 SWD_ERROR_QUEUEROOT =-30,
00278 SWD_ERROR_QUEUETAIL =-31,
00279 SWD_ERROR_BADCMDTYPE =-32,
00280 SWD_ERROR_BADCMDDATA =-33,
00281 SWD_ERROR_TURNAROUND =-34,
00282 SWD_ERROR_DRIVER =-35,
00283 SWD_ERROR_ACK_WAIT =-36,
00284 SWD_ERROR_ACK_FAULT =-37,
00285 SWD_ERROR_QUEUENOTFREE=-38,
00286 SWD_ERROR_TRANSPORT =-39,
00287 SWD_ERROR_DIRECTION =-40,
00288 SWD_ERROR_LOGLEVEL =-41
00289 } swd_error_code_t;
00290
00292
00293 typedef enum SWD_LOGLEVEL{
00294 SWD_LOGLEVEL_MIN = 0,
00295 SWD_LOGLEVEL_SILENT = 0,
00296 SWD_LOGLEVEL_ERROR = 1,
00297 SWD_LOGLEVEL_WARNING = 2,
00298 SWD_LOGLEVEL_INFO = 3,
00299 SWD_LOGLEVEL_DEBUG = 4,
00300 SWD_LOGLEVEL_MAX = 4
00301 } swd_loglevel_t;
00302
00304
00305 #define SWD_DATA_MAXBITCOUNT 32
00306
00307 #define SWD_DATA_BYTESIZE 8
00308
00309 #define SWD_DATA_BITLEN 32
00310
00311 #define SWD_CMDQLEN_DEFAULT 1024;
00312
00318
00319 typedef enum SWD_CMDTYPE {
00320 SWD_CMDTYPE_MOSI_DATA =-7,
00321 SWD_CMDTYPE_MOSI_REQUEST =-6,
00322 SWD_CMDTYPE_MOSI_TRN =-5,
00323 SWD_CMDTYPE_MOSI_PARITY =-4,
00324 SWD_CMDTYPE_MOSI_BITBANG =-3,
00325 SWD_CMDTYPE_MOSI_CONTROL =-2,
00326 SWD_CMDTYPE_MOSI =-1,
00327 SWD_CMDTYPE_UNDEFINED =0,
00328 SWD_CMDTYPE_MISO =1,
00329 SWD_CMDTYPE_MISO_ACK =2,
00330 SWD_CMDTYPE_MISO_BITBANG =3,
00331 SWD_CMDTYPE_MISO_PARITY =4,
00332 SWD_CMDTYPE_MISO_TRN =5,
00333 SWD_CMDTYPE_MISO_DATA =6
00334 } swd_cmdtype_t;
00335
00337 typedef enum SWD_SHIFTDIR {
00338 SWD_DIR_LSBFIRST =0,
00339 SWD_DIR_MSBFIRST =1
00340 } swd_shiftdir_t;
00341
00343 typedef enum SWD_OPERATION {
00344 SWD_OPERATION_FIRST =1,
00345 SWD_OPERATION_ENQUEUE =1,
00346 SWD_OPERATION_EXECUTE =2,
00347 SWD_OPERATION_TRANSMIT_HEAD =3,
00348 SWD_OPERATION_TRANSMIT_TAIL =4,
00349 SWD_OPERATION_TRANSMIT_ALL =5,
00350 SWD_OPERATION_TRANSMIT_ONE =6,
00351 SWD_OPERATION_TRANSMIT_LAST =7,
00352 SWD_OPERATION_LAST =7
00353 } swd_operation_t;
00354
00361 typedef struct swd_cmd_t {
00362 union {
00363 char TRNnMOSI;
00364 char request;
00365 char ack;
00366 int misodata;
00367 int mosidata;
00368 int data32;
00369 char misobit;
00370 char mosibit;
00371 char parity;
00372 char control;
00373 char data8;
00374 };
00375 char bits;
00376 swd_cmdtype_t cmdtype;
00377 char done;
00378 struct swd_cmd_t *prev, *next;
00379 } swd_cmd_t;
00380
00382 typedef struct {
00383 char initialized;
00384 char trnlen;
00385 int maxcmdqlen;
00386 swd_loglevel_t loglevel;
00387 } swd_context_config_t;
00388
00390 typedef struct {
00391 char ack;
00392 char parity;
00393 int idcode;
00394 int abort;
00395 int ctrlstat;
00396 int wcr;
00397 int select;
00398 int rdbuf;
00399 } swd_swdp_t;
00400
00402 typedef struct {
00403 char ack;
00404 int controlstatus;
00405 int tar;
00406 int drw;
00407 int bd0;
00408 int bd1;
00409 int bd2;
00410 int bd3;
00411 int dromt;
00412 int idr;
00413 } swd_ahbap_t;
00414
00416 typedef struct {
00417 char request;
00418 char ack;
00419 int data;
00420 char parity;
00421 } swd_transaction_t;
00422
00426 typedef struct {
00427 void *device;
00428 } swd_driver_t;
00429
00431 typedef enum {
00432 SWD_FALSE=0,
00433 SWD_TRUE=1
00434 } swd_bool_t;
00435
00445 typedef struct {
00446 swd_cmd_t *cmdq;
00447 swd_context_config_t config;
00448 swd_driver_t *driver;
00449 struct {
00450 swd_swdp_t dp_r;
00451 swd_swdp_t dp_w;
00452 swd_ahbap_t ap_r;
00453 swd_ahbap_t ap_w;
00454 swd_transaction_t transaction;
00455 } log;
00456 } swd_ctx_t;
00457
00460 int swd_bin8_parity_even(char *data, char *parity);
00461 int swd_bin32_parity_even(int *data, char *parity);
00462 int swd_bin8_print(char *data);
00463 int swd_bin32_print(int *data);
00464 char *swd_bin8_string(char *data);
00465 char *swd_bin32_string(int *data);
00466 int swd_bin8_bitswap(unsigned char *buffer, int bitcount);
00467 int swd_bin32_bitswap(unsigned int *buffer, int bitcount);
00468
00469 int swd_cmdq_init(swd_cmd_t *cmdq);
00470 swd_cmd_t* swd_cmdq_find_root(swd_cmd_t *cmdq);
00471 swd_cmd_t* swd_cmdq_find_tail(swd_cmd_t *cmdq);
00472 int swd_cmdq_append(swd_cmd_t *cmdq, swd_cmd_t *cmd);
00473 int swd_cmdq_free(swd_cmd_t *cmdq);
00474 int swd_cmdq_free_head(swd_cmd_t *cmdq);
00475 int swd_cmdq_free_tail(swd_cmd_t *cmdq);
00476 int swd_cmdq_flush(swd_ctx_t *swdctx, swd_operation_t operation);
00477
00478 int swd_cmd_enqueue(swd_ctx_t *swdctx, swd_cmd_t *cmd);
00479 int swd_cmd_enqueue_mosi_request(swd_ctx_t *swdctx, char *request);
00480 int swd_cmd_enqueue_mosi_trn(swd_ctx_t *swdctx);
00481 int swd_cmd_enqueue_miso_trn(swd_ctx_t *swdctx);
00482 int swd_cmd_enqueue_miso_nbit(swd_ctx_t *swdctx, char *data, int count);
00483 int swd_cmd_enqueue_mosi_nbit(swd_ctx_t *swdctx, char *data, int count);
00484 int swd_cmd_enqueue_mosi_parity(swd_ctx_t *swdctx, char *parity);
00485 int swd_cmd_enqueue_miso_parity(swd_ctx_t *swdctx, char *parity);
00486 int swd_cmd_enqueue_miso_data(swd_ctx_t *swdctx, int *data);
00487 int swd_cmd_enqueue_miso_data_p(swd_ctx_t *swdctx, int *data, char *parity);
00488 int swd_cmd_enqueue_miso_n_data_p(swd_ctx_t *swdctx, int **data, char **parity, int count);
00489 int swd_cmd_enqueue_mosi_data(swd_ctx_t *swdctx, int *data);
00490 int swd_cmd_enqueue_mosi_data_ap(swd_ctx_t *swdctx, int *data);
00491 int swd_cmd_enqueue_mosi_data_p(swd_ctx_t *swdctx, int *data, char *parity);
00492 int swd_cmd_enqueue_mosi_n_data_ap(swd_ctx_t *swdctx, int **data, int count);
00493 int swd_cmd_enqueue_mosi_n_data_p(swd_ctx_t *swdctx, int **data, char **parity, int count);
00494 int swd_cmd_enqueue_miso_ack(swd_ctx_t *swdctx, char *ack);
00495 int swd_cmd_enqueue_mosi_control(swd_ctx_t *swdctx, char *ctlmsg, int len);
00496 int swd_cmd_enqueue_mosi_dap_reset(swd_ctx_t *swdctx);
00497 int swd_cmd_enqueue_mosi_idle(swd_ctx_t *swdctx);
00498 int swd_cmd_enqueue_mosi_jtag2swd(swd_ctx_t *swdctx);
00499 int swd_cmd_enqueue_mosi_swd2jtag(swd_ctx_t *swdctx);
00500
00501 char *swd_cmd_string_cmdtype(swd_cmd_t *cmd);
00502
00503 int swd_bus_setdir_mosi(swd_ctx_t *swdctx);
00504 int swd_bus_setdir_miso(swd_ctx_t *swdctx);
00505 int swd_bus_write_request
00506 (swd_ctx_t *swdctx, swd_operation_t operation, char *APnDP, char *RnW, char *addr);
00507 int swd_bus_read_ack(swd_ctx_t *swdctx, swd_operation_t operation, char *ack);
00508 int swd_bus_write_data_p(swd_ctx_t *swdctx, swd_operation_t operation, int *data, char *parity);
00509 int swd_bus_write_data_ap(swd_ctx_t *swdctx, swd_operation_t operation, int *data);
00510 int swd_bus_read_data_p(swd_ctx_t *swdctx, swd_operation_t operation, int *data, char *parity);
00511 int swd_bus_write_control(swd_ctx_t *swdctx, swd_operation_t operation, char *ctlmsg, int len);
00512 int swd_bus_write_jtag2swd(swd_ctx_t *swdctx, swd_operation_t operation);
00513 int swd_bus_transmit(swd_ctx_t *swdctx, swd_cmd_t *cmd);
00514
00515 int swd_bitgen8_request(swd_ctx_t *swdctx, char *APnDP, char *RnW, char *addr, char *request);
00516
00517 extern int swd_drv_mosi_8(swd_ctx_t *swdctx, char *data, int bits, int nLSBfirst);
00518 extern int swd_drv_mosi_32(swd_ctx_t *swdctx, int *data, int bits, int nLSBfirst);
00519 extern int swd_drv_miso_8(swd_ctx_t *swdctx, char *data, int bits, int nLSBfirst);
00520 extern int swd_drv_miso_32(swd_ctx_t *swdctx, int *data, int bits, int nLSBfirst);
00521
00522 int swd_dp_reset(swd_ctx_t *swdctx, swd_operation_t operation);
00523 int swd_dp_activate(swd_ctx_t *swdctx, swd_operation_t operation);
00524 int swd_dp_read_idcode(swd_ctx_t *swdctx, swd_operation_t operation, int *idcode);
00525 int swd_dp_detect(swd_ctx_t *swdctx, swd_operation_t operation, int *idcode);
00526
00527 int swd_log(swd_ctx_t *swdctx, swd_loglevel_t loglevel, char *msg, ...);
00528 char *swd_error_string(swd_error_code_t error);
00529
00530 swd_ctx_t *swd_init(void);
00531 int swd_deinit_ctx(swd_ctx_t *swdctx);
00532 int swd_deinit_cmdq(swd_ctx_t *swdctx);
00533 int swd_deinit(swd_ctx_t *swdctx);
00534
00535 int swd_drv_mosi_trn(swd_ctx_t *swdctx, int clks);
00536 int swd_drv_miso_trn(swd_ctx_t *swdctx, int clks);
00537
00538 #endif