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

151
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,11 +534,14 @@ 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
resp_data[0] = data; if (resp_size <= (DAP_CONFIG_PACKET_SIZE-4))
resp_data[1] = data >> 8; {
resp_data[2] = data >> 16; resp_data[0] = data;
resp_data[3] = data >> 24; resp_data[1] = data >> 8;
resp_data[2] = data >> 16;
resp_data[3] = data >> 24;
}
} }
else if (verify_write) else if (verify_write)
{ {
@ -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,26 +839,31 @@ 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];
int wait; int wait;
wait = ((int)req[5] << 24) | ((int)req[4] << 16) | ((int)req[3] << 8) | req[2]; wait = ((int)req[5] << 24) | ((int)req[4] << 16) | ((int)req[3] << 8) | req[2];
if (select & DAP_SWJ_SWCLK_TCK) if (select & DAP_SWJ_SWCLK_TCK)
@ -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

@ -1,227 +1,227 @@
/* /*
* Copyright (c) 2017, Alex Taradov <alex@taradov.com> * Copyright (c) 2017, Alex Taradov <alex@taradov.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* 1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products * 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef _DAP_CONFIG_H_ #ifndef _DAP_CONFIG_H_
#define _DAP_CONFIG_H_ #define _DAP_CONFIG_H_
/*- Includes ----------------------------------------------------------------*/ /*- Includes ----------------------------------------------------------------*/
#include "samd11.h" #include "samd11.h"
#include "hal_gpio.h" #include "hal_gpio.h"
/*- Definitions -------------------------------------------------------------*/ /*- Definitions -------------------------------------------------------------*/
//#define BOARD_SWD_USB_MINI //#define BOARD_SWD_USB_MINI
#define BOARD_SWD_USB_STD #define BOARD_SWD_USB_STD
#if defined(BOARD_SWD_USB_MINI) #if defined(BOARD_SWD_USB_MINI)
HAL_GPIO_PIN(SWCLK_TCK, A, 8) HAL_GPIO_PIN(SWCLK_TCK, A, 8)
HAL_GPIO_PIN(SWDIO_TMS, A, 5) HAL_GPIO_PIN(SWDIO_TMS, A, 5)
HAL_GPIO_PIN(nRESET, A, 9) HAL_GPIO_PIN(nRESET, A, 9)
#elif defined(BOARD_SWD_USB_STD) #elif defined(BOARD_SWD_USB_STD)
HAL_GPIO_PIN(SWCLK_TCK, A, 14) HAL_GPIO_PIN(SWCLK_TCK, A, 14)
HAL_GPIO_PIN(SWDIO_TMS, A, 15) HAL_GPIO_PIN(SWDIO_TMS, A, 15)
HAL_GPIO_PIN(SWO_DTO, A, 9) HAL_GPIO_PIN(SWO_DTO, A, 9)
HAL_GPIO_PIN(TDI, A, 8) HAL_GPIO_PIN(TDI, A, 8)
HAL_GPIO_PIN(nRESET, A, 5) HAL_GPIO_PIN(nRESET, A, 5)
#endif #endif
#define DAP_CONFIG_ENABLE_SWD #define DAP_CONFIG_ENABLE_SWD
//#define DAP_CONFIG_ENABLE_JTAG //#define DAP_CONFIG_ENABLE_JTAG
#define DAP_CONFIG_DEFAULT_PORT DAP_PORT_SWD #define DAP_CONFIG_DEFAULT_PORT DAP_PORT_SWD
#define DAP_CONFIG_DEFAULT_CLOCK 1000000 // Hz #define DAP_CONFIG_DEFAULT_CLOCK 1000000 // Hz
#define DAP_CONFIG_PACKET_SIZE 64 #define DAP_CONFIG_PACKET_SIZE 64
#define DAP_CONFIG_PACKET_COUNT 1 #define DAP_CONFIG_PACKET_COUNT 1
// Set the value to NULL if you want to disable a string // Set the value to NULL if you want to disable a string
// DAP_CONFIG_PRODUCT_STR must contain "CMSIS-DAP" to be compatible with the standard // DAP_CONFIG_PRODUCT_STR must contain "CMSIS-DAP" to be compatible with the standard
#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
//#define DAP_CONFIG_RESET_TARGET_FN target_specific_reset_function //#define DAP_CONFIG_RESET_TARGET_FN target_specific_reset_function
// A value at which dap_clock_test() produces 1 kHz output on the SWCLK pin // A value at which dap_clock_test() produces 1 kHz output on the SWCLK pin
#define DAP_CONFIG_DELAY_CONSTANT 4700 #define DAP_CONFIG_DELAY_CONSTANT 4700
// A threshold for switching to fast clock (no added delays) // A threshold for switching to fast clock (no added delays)
// This is the frequency produced by dap_clock_test(1) on the SWCLK pin // This is the frequency produced by dap_clock_test(1) on the SWCLK pin
#define DAP_CONFIG_FAST_CLOCK 3600000 // Hz #define DAP_CONFIG_FAST_CLOCK 3600000 // Hz
/*- Prototypes --------------------------------------------------------------*/ /*- Prototypes --------------------------------------------------------------*/
extern void app_led_set_state(int state); extern void app_led_set_state(int state);
extern char usb_serial_number[16]; extern char usb_serial_number[16];
/*- Implementations ---------------------------------------------------------*/ /*- Implementations ---------------------------------------------------------*/
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWCLK_TCK_write(int value) static inline void DAP_CONFIG_SWCLK_TCK_write(int value)
{ {
HAL_GPIO_SWCLK_TCK_write(value); HAL_GPIO_SWCLK_TCK_write(value);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWDIO_TMS_write(int value) static inline void DAP_CONFIG_SWDIO_TMS_write(int value)
{ {
HAL_GPIO_SWDIO_TMS_write(value); HAL_GPIO_SWDIO_TMS_write(value);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_TDO_write(int value) static inline void DAP_CONFIG_TDO_write(int value)
{ {
(void)value; (void)value;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_nTRST_write(int value) static inline void DAP_CONFIG_nTRST_write(int value)
{ {
(void)value; (void)value;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_nRESET_write(int value) static inline void DAP_CONFIG_nRESET_write(int value)
{ {
HAL_GPIO_nRESET_write(value); HAL_GPIO_nRESET_write(value);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline int DAP_CONFIG_SWCLK_TCK_read(void) static inline int DAP_CONFIG_SWCLK_TCK_read(void)
{ {
return HAL_GPIO_SWCLK_TCK_read(); return HAL_GPIO_SWCLK_TCK_read();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline int DAP_CONFIG_SWDIO_TMS_read(void) static inline int DAP_CONFIG_SWDIO_TMS_read(void)
{ {
return HAL_GPIO_SWDIO_TMS_read(); return HAL_GPIO_SWDIO_TMS_read();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline int DAP_CONFIG_TDI_read(void) static inline int DAP_CONFIG_TDI_read(void)
{ {
return 0; return 0;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline int DAP_CONFIG_TDO_read(void) static inline int DAP_CONFIG_TDO_read(void)
{ {
return 0; return 0;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline int DAP_CONFIG_nTRST_read(void) static inline int DAP_CONFIG_nTRST_read(void)
{ {
return 0; return 0;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline int DAP_CONFIG_nRESET_read(void) static inline int DAP_CONFIG_nRESET_read(void)
{ {
return HAL_GPIO_nRESET_read(); return HAL_GPIO_nRESET_read();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWCLK_TCK_set(void) static inline void DAP_CONFIG_SWCLK_TCK_set(void)
{ {
HAL_GPIO_SWCLK_TCK_set(); HAL_GPIO_SWCLK_TCK_set();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWCLK_TCK_clr(void) static inline void DAP_CONFIG_SWCLK_TCK_clr(void)
{ {
HAL_GPIO_SWCLK_TCK_clr(); HAL_GPIO_SWCLK_TCK_clr();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWDIO_TMS_in(void) static inline void DAP_CONFIG_SWDIO_TMS_in(void)
{ {
HAL_GPIO_SWDIO_TMS_in(); HAL_GPIO_SWDIO_TMS_in();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SWDIO_TMS_out(void) static inline void DAP_CONFIG_SWDIO_TMS_out(void)
{ {
HAL_GPIO_SWDIO_TMS_out(); HAL_GPIO_SWDIO_TMS_out();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_SETUP(void) static inline void DAP_CONFIG_SETUP(void)
{ {
HAL_GPIO_SWCLK_TCK_in(); HAL_GPIO_SWCLK_TCK_in();
HAL_GPIO_SWDIO_TMS_in(); HAL_GPIO_SWDIO_TMS_in();
HAL_GPIO_nRESET_in(); HAL_GPIO_nRESET_in();
HAL_GPIO_SWDIO_TMS_pullup(); HAL_GPIO_SWDIO_TMS_pullup();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_DISCONNECT(void) static inline void DAP_CONFIG_DISCONNECT(void)
{ {
HAL_GPIO_SWCLK_TCK_in(); HAL_GPIO_SWCLK_TCK_in();
HAL_GPIO_SWDIO_TMS_in(); HAL_GPIO_SWDIO_TMS_in();
HAL_GPIO_nRESET_in(); HAL_GPIO_nRESET_in();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_CONNECT_SWD(void) static inline void DAP_CONFIG_CONNECT_SWD(void)
{ {
HAL_GPIO_SWDIO_TMS_out(); HAL_GPIO_SWDIO_TMS_out();
HAL_GPIO_SWDIO_TMS_set(); HAL_GPIO_SWDIO_TMS_set();
HAL_GPIO_SWCLK_TCK_out(); HAL_GPIO_SWCLK_TCK_out();
HAL_GPIO_SWCLK_TCK_set(); HAL_GPIO_SWCLK_TCK_set();
HAL_GPIO_nRESET_out(); HAL_GPIO_nRESET_out();
HAL_GPIO_nRESET_set(); HAL_GPIO_nRESET_set();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_CONNECT_JTAG(void) static inline void DAP_CONFIG_CONNECT_JTAG(void)
{ {
HAL_GPIO_SWDIO_TMS_out(); HAL_GPIO_SWDIO_TMS_out();
HAL_GPIO_SWDIO_TMS_set(); HAL_GPIO_SWDIO_TMS_set();
HAL_GPIO_SWCLK_TCK_out(); HAL_GPIO_SWCLK_TCK_out();
HAL_GPIO_SWCLK_TCK_set(); HAL_GPIO_SWCLK_TCK_set();
HAL_GPIO_nRESET_out(); HAL_GPIO_nRESET_out();
HAL_GPIO_nRESET_set(); HAL_GPIO_nRESET_set();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void DAP_CONFIG_LED(int index, int state) static inline void DAP_CONFIG_LED(int index, int state)
{ {
if (0 == index) if (0 == index)
app_led_set_state(state); app_led_set_state(state);
} }
#endif // _DAP_CONFIG_H_ #endif // _DAP_CONFIG_H_