Added response payload size calculation for non-HID transfer layers

This commit is contained in:
Alex Taradov 2021-03-07 14:39:30 -08:00
parent 1a294dbae6
commit e985493f58
3 changed files with 338 additions and 269 deletions

141
dap.c
View File

@ -384,28 +384,34 @@ static int dap_swd_transfer_word(int req, uint32_t *data)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_swd_transfer(uint8_t *req, uint8_t *resp) static int dap_swd_transfer(uint8_t *req, uint8_t *resp)
{ {
int req_count, resp_count, request, ack; int req_count, req_size, resp_count, resp_size, request, ack;
uint8_t *req_data, *resp_data; uint8_t *req_data, *resp_data;
bool posted_read, verify_write; bool posted_read, verify_write;
uint32_t data, match_value; uint32_t data, match_value;
req_count = req[1]; req_count = req[1];
req_data = &req[2]; req_data = &req[2];
req_size = 2;
ack = DAP_TRANSFER_INVALID;
resp_count = 0; resp_count = 0;
resp_data = &resp[2]; resp_data = &resp[2];
resp_size = 2;
posted_read = false; posted_read = false;
verify_write = false; verify_write = false;
ack = DAP_TRANSFER_INVALID;
while (req_count && !dap_abort) while (req_count && !dap_abort)
{ {
if (req_size == DAP_CONFIG_PACKET_SIZE)
break;
verify_write = false; verify_write = false;
request = req_data[0]; request = req_data[0];
req_data++; req_data++;
req_size++;
if (posted_read) if (posted_read)
{ {
@ -422,20 +428,28 @@ static void dap_swd_transfer(uint8_t *req, uint8_t *resp)
if (ack != DAP_TRANSFER_OK) if (ack != DAP_TRANSFER_OK)
break; break;
if (resp_size > (DAP_CONFIG_PACKET_SIZE-4))
break;
resp_data[0] = data; resp_data[0] = data;
resp_data[1] = data >> 8; resp_data[1] = data >> 8;
resp_data[2] = data >> 16; resp_data[2] = data >> 16;
resp_data[3] = data >> 24; resp_data[3] = data >> 24;
resp_data += 4; resp_data += 4;
resp_size += 4;
} }
if (request & DAP_TRANSFER_RnW) if (request & DAP_TRANSFER_RnW)
{ {
if (request & DAP_TRANSFER_MATCH_VALUE) if (request & DAP_TRANSFER_MATCH_VALUE)
{ {
if (req_size > (DAP_CONFIG_PACKET_SIZE-4))
break;
match_value = ((uint32_t)req_data[3] << 24) | ((uint32_t)req_data[2] << 16) | match_value = ((uint32_t)req_data[3] << 24) | ((uint32_t)req_data[2] << 16) |
((uint32_t)req_data[1] << 8) | req_data[0]; ((uint32_t)req_data[1] << 8) | req_data[0];
req_data += 4; req_data += 4;
req_size += 4;
for (int i = 0; i < dap_match_retry_count; i++) for (int i = 0; i < dap_match_retry_count; i++)
{ {
@ -472,19 +486,27 @@ static void dap_swd_transfer(uint8_t *req, uint8_t *resp)
if (DAP_TRANSFER_OK != ack) if (DAP_TRANSFER_OK != ack)
break; break;
if (resp_size > (DAP_CONFIG_PACKET_SIZE-4))
break;
resp_data[0] = data; resp_data[0] = data;
resp_data[1] = data >> 8; resp_data[1] = data >> 8;
resp_data[2] = data >> 16; resp_data[2] = data >> 16;
resp_data[3] = data >> 24; resp_data[3] = data >> 24;
resp_data += 4; resp_data += 4;
resp_size += 4;
} }
} }
} }
else else
{ {
if (req_size > (DAP_CONFIG_PACKET_SIZE-4))
break;
data = ((uint32_t)req_data[3] << 24) | ((uint32_t)req_data[2] << 16) | data = ((uint32_t)req_data[3] << 24) | ((uint32_t)req_data[2] << 16) |
((uint32_t)req_data[1] << 8) | req_data[0]; ((uint32_t)req_data[1] << 8) | req_data[0];
req_data += 4; req_data += 4;
req_size += 4;
if (request & DAP_TRANSFER_MATCH_MASK) if (request & DAP_TRANSFER_MATCH_MASK)
{ {
@ -512,12 +534,15 @@ static void dap_swd_transfer(uint8_t *req, uint8_t *resp)
{ {
ack = dap_swd_transfer_word(SWD_DP_R_RDBUFF | DAP_TRANSFER_RnW, &data); ack = dap_swd_transfer_word(SWD_DP_R_RDBUFF | DAP_TRANSFER_RnW, &data);
// Save data regardless of the ACK status, at this point it does not matter // Save the data regardless of the ACK status, at this point it does not matter
if (resp_size <= (DAP_CONFIG_PACKET_SIZE-4))
{
resp_data[0] = data; resp_data[0] = data;
resp_data[1] = data >> 8; resp_data[1] = data >> 8;
resp_data[2] = data >> 16; resp_data[2] = data >> 16;
resp_data[3] = data >> 24; resp_data[3] = data >> 24;
} }
}
else if (verify_write) else if (verify_write)
{ {
ack = dap_swd_transfer_word(SWD_DP_R_RDBUFF | DAP_TRANSFER_RnW, NULL); ack = dap_swd_transfer_word(SWD_DP_R_RDBUFF | DAP_TRANSFER_RnW, NULL);
@ -526,29 +551,33 @@ static void dap_swd_transfer(uint8_t *req, uint8_t *resp)
resp[0] = resp_count; resp[0] = resp_count;
resp[1] = ack; resp[1] = ack;
return resp_size;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_swd_transfer_block(uint8_t *req, uint8_t *resp) static int dap_swd_transfer_block(uint8_t *req, uint8_t *resp)
{ {
int req_count, resp_count, request, ack; int req_count, req_size, resp_count, resp_size, request, ack;
uint8_t *req_data, *resp_data; uint8_t *req_data, *resp_data;
uint32_t data; uint32_t data;
req_count = ((int)req[2] << 8) | req[1]; req_count = ((int)req[2] << 8) | req[1];
request = req[3]; request = req[3];
req_data = &req[4]; req_data = &req[4];
req_size = 4;
ack = DAP_TRANSFER_INVALID; ack = DAP_TRANSFER_INVALID;
resp_count = 0; resp_count = 0;
resp_data = &resp[3]; resp_data = &resp[3];
resp_size = 3;
resp[0] = 0; resp[0] = 0;
resp[1] = 0; resp[1] = 0;
resp[2] = DAP_TRANSFER_INVALID; resp[2] = DAP_TRANSFER_INVALID;
if (0 == req_count) if (0 == req_count)
return; return resp_size;
if (request & DAP_TRANSFER_RnW) if (request & DAP_TRANSFER_RnW)
{ {
@ -567,11 +596,15 @@ static void dap_swd_transfer_block(uint8_t *req, uint8_t *resp)
if ((0 == i) && (request & DAP_TRANSFER_APnDP)) if ((0 == i) && (request & DAP_TRANSFER_APnDP))
continue; continue;
if (resp_size > (DAP_CONFIG_PACKET_SIZE-4))
break;
resp_data[0] = data; resp_data[0] = data;
resp_data[1] = data >> 8; resp_data[1] = data >> 8;
resp_data[2] = data >> 16; resp_data[2] = data >> 16;
resp_data[3] = data >> 24; resp_data[3] = data >> 24;
resp_data += 4; resp_data += 4;
resp_size += 4;
resp_count++; resp_count++;
} }
} }
@ -579,9 +612,13 @@ static void dap_swd_transfer_block(uint8_t *req, uint8_t *resp)
{ {
for (int i = 0; i < req_count; i++) for (int i = 0; i < req_count; i++)
{ {
if (req_size > (DAP_CONFIG_PACKET_SIZE-4))
break;
data = ((uint32_t)req_data[3] << 24) | ((uint32_t)req_data[2] << 16) | data = ((uint32_t)req_data[3] << 24) | ((uint32_t)req_data[2] << 16) |
((uint32_t)req_data[1] << 8) | ((uint32_t)req_data[0] << 0); ((uint32_t)req_data[1] << 8) | ((uint32_t)req_data[0] << 0);
req_data += 4; req_data += 4;
req_size += 4;
ack = dap_swd_transfer_word(request, &data); ack = dap_swd_transfer_word(request, &data);
@ -598,10 +635,12 @@ static void dap_swd_transfer_block(uint8_t *req, uint8_t *resp)
resp[0] = resp_count; resp[0] = resp_count;
resp[1] = resp_count >> 8; resp[1] = resp_count >> 8;
resp[2] = ack; resp[2] = ack;
return resp_size;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_info(uint8_t *req, uint8_t *resp) static int dap_info(uint8_t *req, uint8_t *resp)
{ {
int index = req[0]; int index = req[0];
@ -640,10 +679,12 @@ static void dap_info(uint8_t *req, uint8_t *resp)
resp[1] = DAP_CONFIG_PACKET_SIZE & 0xff; resp[1] = DAP_CONFIG_PACKET_SIZE & 0xff;
resp[2] = (DAP_CONFIG_PACKET_SIZE >> 8) & 0xff; resp[2] = (DAP_CONFIG_PACKET_SIZE >> 8) & 0xff;
} }
return resp[0] + 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_led(uint8_t *req, uint8_t *resp) static int dap_led(uint8_t *req, uint8_t *resp)
{ {
int index = req[0]; int index = req[0];
int state = req[1]; int state = req[1];
@ -651,10 +692,12 @@ static void dap_led(uint8_t *req, uint8_t *resp)
DAP_CONFIG_LED(index, state); DAP_CONFIG_LED(index, state);
resp[0] = DAP_OK; resp[0] = DAP_OK;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_connect(uint8_t *req, uint8_t *resp) static int dap_connect(uint8_t *req, uint8_t *resp)
{ {
int port = req[0]; int port = req[0];
@ -680,10 +723,12 @@ static void dap_connect(uint8_t *req, uint8_t *resp)
#endif #endif
resp[0] = dap_port; resp[0] = dap_port;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_disconnect(uint8_t *req, uint8_t *resp) static int dap_disconnect(uint8_t *req, uint8_t *resp)
{ {
DAP_CONFIG_DISCONNECT(); DAP_CONFIG_DISCONNECT();
@ -692,37 +737,42 @@ static void dap_disconnect(uint8_t *req, uint8_t *resp)
resp[0] = DAP_OK; resp[0] = DAP_OK;
(void)req; (void)req;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_transfer_configure(uint8_t *req, uint8_t *resp) static int dap_transfer_configure(uint8_t *req, uint8_t *resp)
{ {
dap_idle_cycles = req[0]; dap_idle_cycles = req[0];
dap_retry_count = ((int)req[2] << 8) | req[1]; dap_retry_count = ((int)req[2] << 8) | req[1];
dap_match_retry_count = ((int)req[4] << 8) | req[3]; dap_match_retry_count = ((int)req[4] << 8) | req[3];
resp[0] = DAP_OK; resp[0] = DAP_OK;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_transfer(uint8_t *req, uint8_t *resp) static int dap_transfer(uint8_t *req, uint8_t *resp)
{ {
resp[0] = 0; resp[0] = 0;
resp[1] = DAP_TRANSFER_INVALID; resp[1] = DAP_TRANSFER_INVALID;
#ifdef DAP_CONFIG_ENABLE_SWD #ifdef DAP_CONFIG_ENABLE_SWD
if (DAP_PORT_SWD == dap_port) if (DAP_PORT_SWD == dap_port)
dap_swd_transfer(req, resp); return dap_swd_transfer(req, resp);
#endif #endif
#ifdef DAP_CONFIG_ENABLE_JTAG #ifdef DAP_CONFIG_ENABLE_JTAG
if (DAP_PORT_JTAG == dap_port) if (DAP_PORT_JTAG == dap_port)
dap_jtag_transfer(req, resp); return dap_jtag_transfer(req, resp);
#endif #endif
return 2;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_transfer_block(uint8_t *req, uint8_t *resp) static int dap_transfer_block(uint8_t *req, uint8_t *resp)
{ {
resp[0] = 0; resp[0] = 0;
resp[1] = 0; resp[1] = 0;
@ -730,26 +780,29 @@ static void dap_transfer_block(uint8_t *req, uint8_t *resp)
#ifdef DAP_CONFIG_ENABLE_SWD #ifdef DAP_CONFIG_ENABLE_SWD
if (DAP_PORT_SWD == dap_port) if (DAP_PORT_SWD == dap_port)
dap_swd_transfer_block(req, resp); return dap_swd_transfer_block(req, resp);
#endif #endif
#ifdef DAP_CONFIG_ENABLE_JTAG #ifdef DAP_CONFIG_ENABLE_JTAG
if (DAP_PORT_JTAG == dap_port) if (DAP_PORT_JTAG == dap_port)
dap_jtag_transfer_block(req, resp); return dap_jtag_transfer_block(req, resp);
#endif #endif
return 3;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_transfer_abort(uint8_t *req, uint8_t *resp) static int dap_transfer_abort(uint8_t *req, uint8_t *resp)
{ {
// This request is handled outside of the normal queue. // This request is handled outside of the normal queue.
// We should never get here. // We should never get here.
resp[0] = DAP_OK; resp[0] = DAP_OK;
(void)req; (void)req;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_write_abort(uint8_t *req, uint8_t *resp) static int dap_write_abort(uint8_t *req, uint8_t *resp)
{ {
#ifdef DAP_CONFIG_ENABLE_SWD #ifdef DAP_CONFIG_ENABLE_SWD
if (DAP_PORT_SWD == dap_port) if (DAP_PORT_SWD == dap_port)
@ -772,10 +825,12 @@ static void dap_write_abort(uint8_t *req, uint8_t *resp)
resp[0] = DAP_OK; resp[0] = DAP_OK;
} }
#endif #endif
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_delay(uint8_t *req, uint8_t *resp) static int dap_delay(uint8_t *req, uint8_t *resp)
{ {
int delay; int delay;
@ -784,21 +839,26 @@ static void dap_delay(uint8_t *req, uint8_t *resp)
dap_delay_us(delay); dap_delay_us(delay);
resp[0] = DAP_OK; resp[0] = DAP_OK;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_reset_target(uint8_t *req, uint8_t *resp) static int dap_reset_target(uint8_t *req, uint8_t *resp)
{ {
resp[0] = DAP_OK; resp[0] = DAP_OK;
#ifdef DAP_CONFIG_RESET_TARGET_FN #ifdef DAP_CONFIG_RESET_TARGET_FN
resp[1] = 1; resp[1] = 1;
DAP_CONFIG_RESET_TARGET_FN(); DAP_CONFIG_RESET_TARGET_FN();
#endif #endif
(void)req; (void)req;
return 2;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_swj_pins(uint8_t *req, uint8_t *resp) static int dap_swj_pins(uint8_t *req, uint8_t *resp)
{ {
int value = req[0]; int value = req[0];
int select = req[1]; int select = req[1];
@ -832,10 +892,12 @@ static void dap_swj_pins(uint8_t *req, uint8_t *resp)
(DAP_CONFIG_nRESET_read() ? DAP_SWJ_nRESET : 0); (DAP_CONFIG_nRESET_read() ? DAP_SWJ_nRESET : 0);
resp[0] = value; resp[0] = value;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_swj_clock(uint8_t *req, uint8_t *resp) static int dap_swj_clock(uint8_t *req, uint8_t *resp)
{ {
uint32_t freq; uint32_t freq;
@ -845,10 +907,12 @@ static void dap_swj_clock(uint8_t *req, uint8_t *resp)
dap_setup_clock(freq); dap_setup_clock(freq);
resp[0] = DAP_OK; resp[0] = DAP_OK;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_swj_sequence(uint8_t *req, uint8_t *resp) static int dap_swj_sequence(uint8_t *req, uint8_t *resp)
{ {
int size = req[0]; int size = req[0];
uint8_t *data = &req[1]; uint8_t *data = &req[1];
@ -865,10 +929,12 @@ static void dap_swj_sequence(uint8_t *req, uint8_t *resp)
} }
resp[0] = DAP_OK; resp[0] = DAP_OK;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_swd_configure(uint8_t *req, uint8_t *resp) static int dap_swd_configure(uint8_t *req, uint8_t *resp)
{ {
#ifdef DAP_CONFIG_ENABLE_SWD #ifdef DAP_CONFIG_ENABLE_SWD
uint8_t data = req[0]; uint8_t data = req[0];
@ -881,10 +947,11 @@ static void dap_swd_configure(uint8_t *req, uint8_t *resp)
(void)req; (void)req;
(void)resp; (void)resp;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_jtag_sequence(uint8_t *req, uint8_t *resp) static int dap_jtag_sequence(uint8_t *req, uint8_t *resp)
{ {
#ifdef DAP_CONFIG_ENABLE_JTAG #ifdef DAP_CONFIG_ENABLE_JTAG
// TODO: implement // TODO: implement
@ -893,10 +960,11 @@ static void dap_jtag_sequence(uint8_t *req, uint8_t *resp)
(void)req; (void)req;
(void)resp; (void)resp;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_jtag_configure(uint8_t *req, uint8_t *resp) static int dap_jtag_configure(uint8_t *req, uint8_t *resp)
{ {
#ifdef DAP_CONFIG_ENABLE_JTAG #ifdef DAP_CONFIG_ENABLE_JTAG
// TODO: implement // TODO: implement
@ -905,10 +973,11 @@ static void dap_jtag_configure(uint8_t *req, uint8_t *resp)
(void)req; (void)req;
(void)resp; (void)resp;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void dap_jtag_idcode(uint8_t *req, uint8_t *resp) static int dap_jtag_idcode(uint8_t *req, uint8_t *resp)
{ {
#ifdef DAP_CONFIG_ENABLE_JTAG #ifdef DAP_CONFIG_ENABLE_JTAG
// TODO: implement // TODO: implement
@ -917,6 +986,7 @@ static void dap_jtag_idcode(uint8_t *req, uint8_t *resp)
(void)req; (void)req;
(void)resp; (void)resp;
return 1;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -954,12 +1024,12 @@ bool dap_filter_request(uint8_t *req)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void dap_process_request(uint8_t *req, uint8_t *resp) int dap_process_request(uint8_t *req, uint8_t *resp)
{ {
const struct const struct
{ {
int cmd; int cmd;
void (*handler)(uint8_t *, uint8_t *); int (*handler)(uint8_t *, uint8_t *);
} handlers[] = } handlers[] =
{ {
{ ID_DAP_INFO, dap_info }, { ID_DAP_INFO, dap_info },
@ -994,16 +1064,15 @@ void dap_process_request(uint8_t *req, uint8_t *resp)
for (int i = 0; -1 != handlers[i].cmd; i++) for (int i = 0; -1 != handlers[i].cmd; i++)
{ {
if (cmd == handlers[i].cmd) if (cmd == handlers[i].cmd)
{ return handlers[i].handler(&req[1], &resp[1]);
handlers[i].handler(&req[1], &resp[1]);
return;
}
} }
if (ID_DAP_VENDOR_0 <= cmd && cmd <= ID_DAP_VENDOR_31) if (ID_DAP_VENDOR_0 <= cmd && cmd <= ID_DAP_VENDOR_31)
return; return 2;
resp[0] = ID_DAP_INVALID; resp[0] = ID_DAP_INVALID;
return 2;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

2
dap.h
View File

@ -36,7 +36,7 @@
/*- Prototypes --------------------------------------------------------------*/ /*- Prototypes --------------------------------------------------------------*/
void dap_init(void); void dap_init(void);
bool dap_filter_request(uint8_t *req); bool dap_filter_request(uint8_t *req);
void dap_process_request(uint8_t *req, uint8_t *resp); int dap_process_request(uint8_t *req, uint8_t *resp);
void dap_clock_test(int delay); void dap_clock_test(int delay);
#endif // _DAP_H_ #endif // _DAP_H_

View File

@ -63,7 +63,7 @@
#define DAP_CONFIG_VENDOR_STR "Alex Taradov" #define DAP_CONFIG_VENDOR_STR "Alex Taradov"
#define DAP_CONFIG_PRODUCT_STR "Generic CMSIS-DAP Adapter" #define DAP_CONFIG_PRODUCT_STR "Generic CMSIS-DAP Adapter"
#define DAP_CONFIG_SER_NUM_STR usb_serial_number #define DAP_CONFIG_SER_NUM_STR usb_serial_number
#define DAP_CONFIG_FW_VER_STR "v0.1" #define DAP_CONFIG_FW_VER_STR "v0.3"
#define DAP_CONFIG_DEVICE_VENDOR_STR NULL #define DAP_CONFIG_DEVICE_VENDOR_STR NULL
#define DAP_CONFIG_DEVICE_NAME_STR NULL #define DAP_CONFIG_DEVICE_NAME_STR NULL