Skip to content

Commit e97b46a

Browse files
committed
CMSIS-DAP v2.1 board and target string support.
- Add string fields to board and target structs. - Update dap_strings.h functions to return board and target strings when available. - Refactored code in dap_strings.h to use common return_dap_string() utility.
1 parent 097de17 commit e97b46a

File tree

3 files changed

+72
-55
lines changed

3 files changed

+72
-55
lines changed

source/daplink/cmsis-dap/dap_strings.h

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,111 +20,113 @@
2020

2121
#include "cmsis_compiler.h"
2222
#include "info.h"
23+
#include "util.h"
24+
#include "target_board.h"
25+
26+
#if !defined(CMSIS_DAP_PRODUCT_NAME)
27+
#define CMSIS_DAP_PRODUCT_NAME "DAPLink"
28+
#endif
29+
30+
//! Maximum output buffer length of all these functions.
31+
#define MAX_DAP_STR_LEN (60)
32+
33+
//! @brief Utility to copy string to output buffer and return length.
34+
//!
35+
//! The source string is limited to the maximum output buffer size defined
36+
//! by the MAX_DAP_STR_LEN macro.
37+
static uint8_t return_dap_string(char *dst, const char *src)
38+
{
39+
int length = MIN(MAX_DAP_STR_LEN, strlen(src) + 1);
40+
memcpy(dst, src, length);
41+
dst[MAX_DAP_STR_LEN - 1] = 0; // Ensure there's a terminating NULL.
42+
return length;
43+
}
2344

2445
/** Get Vendor Name string.
2546
\param str Pointer to buffer to store the string (max 60 characters).
2647
\return String length (including terminating NULL character) or 0 (no string).
2748
*/
2849
__STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
29-
(void)str;
30-
return (0U);
50+
#if defined(CMSIS_DAP_VENDOR_NAME)
51+
return return_dap_string(str, CMSIS_DAP_VENDOR_NAME);
52+
#else
53+
(void)str;
54+
return (0U);
55+
#endif
3156
}
3257

3358
/** Get Product Name string.
3459
\param str Pointer to buffer to store the string (max 60 characters).
3560
\return String length (including terminating NULL character) or 0 (no string).
3661
*/
3762
__STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
38-
(void)str;
39-
return (0U);
63+
return return_dap_string(str, CMSIS_DAP_PRODUCT_NAME);
4064
}
4165

4266
/** Get Serial Number string.
4367
\param str Pointer to buffer to store the string (max 60 characters).
4468
\return String length (including terminating NULL character) or 0 (no string).
4569
*/
4670
__STATIC_INLINE uint8_t DAP_GetSerNumString (char *str) {
47-
const char * data = info_get_unique_id();
48-
uint8_t length = (uint8_t)strlen(data) + 1;
49-
memcpy(str, data, length);
50-
return length;
71+
return return_dap_string(str, info_get_unique_id());
5172
}
5273

5374
/** Get Target Device Vendor string.
5475
\param str Pointer to buffer to store the string (max 60 characters).
5576
\return String length (including terminating NULL character) or 0 (no string).
5677
*/
5778
__STATIC_INLINE uint8_t DAP_GetTargetDeviceVendorString (char *str) {
58-
#if TARGET_FIXED != 0
59-
uint8_t len;
60-
61-
strcpy(str, TargetDeviceVendor);
62-
len = (uint8_t)(strlen(TargetDeviceVendor) + 1U);
63-
return (len);
64-
#else
65-
(void)str;
66-
return (0U);
67-
#endif
79+
if (g_board_info.target_cfg && g_board_info.target_cfg->target_vendor) {
80+
return return_dap_string(str, g_board_info.target_cfg->target_vendor);
81+
}
82+
else {
83+
return (0U);
84+
}
6885
}
6986

7087
/** Get Target Device Name string.
7188
\param str Pointer to buffer to store the string (max 60 characters).
7289
\return String length (including terminating NULL character) or 0 (no string).
7390
*/
7491
__STATIC_INLINE uint8_t DAP_GetTargetDeviceNameString (char *str) {
75-
#if TARGET_FIXED != 0
76-
uint8_t len;
77-
78-
strcpy(str, TargetDeviceName);
79-
len = (uint8_t)(strlen(TargetDeviceName) + 1U);
80-
return (len);
81-
#else
82-
(void)str;
83-
return (0U);
84-
#endif
92+
if (g_board_info.target_cfg && g_board_info.target_cfg->target_part_number) {
93+
return return_dap_string(str, g_board_info.target_cfg->target_part_number);
94+
}
95+
else {
96+
return (0U);
97+
}
8598
}
8699

87100
/** Get Target Board Vendor string.
88101
\param str Pointer to buffer to store the string (max 60 characters).
89102
\return String length (including terminating NULL character) or 0 (no string).
90103
*/
91104
__STATIC_INLINE uint8_t DAP_GetTargetBoardVendorString (char *str) {
92-
#if TARGET_FIXED != 0
93-
uint8_t len;
94-
95-
strcpy(str, TargetBoardVendor);
96-
len = (uint8_t)(strlen(TargetBoardVendor) + 1U);
97-
return (len);
98-
#else
99-
(void)str;
100-
return (0U);
101-
#endif
105+
if (g_board_info.board_vendor) {
106+
return return_dap_string(str, g_board_info.board_vendor);
107+
}
108+
else {
109+
return (0U);
110+
}
102111
}
103112

104113
/** Get Target Board Name string.
105114
\param str Pointer to buffer to store the string (max 60 characters).
106115
\return String length (including terminating NULL character) or 0 (no string).
107116
*/
108117
__STATIC_INLINE uint8_t DAP_GetTargetBoardNameString (char *str) {
109-
#if TARGET_FIXED != 0
110-
uint8_t len;
111-
112-
strcpy(str, TargetBoardName);
113-
len = (uint8_t)(strlen(TargetBoardName) + 1U);
114-
return (len);
115-
#else
116-
(void)str;
117-
return (0U);
118-
#endif
118+
if (g_board_info.board_name) {
119+
return return_dap_string(str, g_board_info.board_name);
120+
}
121+
else {
122+
return (0U);
123+
}
119124
}
120125

121126
/** Get Product Firmware Version string.
122127
\param str Pointer to buffer to store the string (max 60 characters).
123128
\return String length (including terminating NULL character) or 0 (no string).
124129
*/
125130
__STATIC_INLINE uint8_t DAP_GetProductFirmwareVersionString (char *str) {
126-
const char * data = info_get_version();
127-
uint8_t length = (uint8_t)strlen(data) + 1;
128-
memcpy(str, data, length);
129-
return length;
131+
return return_dap_string(str, info_get_version());
130132
}

source/target/target_board.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum _board_info_flags {
5555
* by the device family.
5656
*/
5757
typedef struct board_info {
58-
uint16_t info_version; /*!< Version number of the board info */
58+
uint16_t info_version; /*!< Version number of the board info */
5959
uint16_t family_id; /*!< Use to select or identify target family from defined target family or custom ones */
6060
char board_id[5]; /*!< 4-char board ID plus null terminator */
6161
uint8_t _padding[3];
@@ -76,6 +76,12 @@ typedef struct board_info {
7676
uint8_t (*target_set_state)(target_state_t state); /*!< Boards can customize target debug states with precedence over target family */
7777
uint32_t soft_reset_type; /*!< Boards can override software reset type to VECTRESET or SYSRESETREQ */
7878
//@}
79+
80+
//! @name CMSIS-DAP v2.1 board strings
81+
//@{
82+
char *board_vendor; //!< Board vendor. Maximum 60 characters including terminal NULL.
83+
char *board_name; //!< Board name. Maximum 60 characters including terminal NULL.
84+
//@}
7985
} board_info_t;
8086

8187
//! @brief Information describing the board on which DAPLink is running.

source/target/target_config.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
//! @brief Current target configuration version.
3232
//!
33-
//! - Version 0: Initial version.
33+
//! - Version 1: Initial version.
3434
enum _target_config_version {
35-
kTargetConfigVersion = 0, //!< The current board info version.
35+
kTargetConfigVersion = 1, //!< The current board info version.
3636
};
3737

3838
//! This can vary from target to target and should be in the structure or flash blob
@@ -71,6 +71,15 @@ typedef struct __attribute__((__packed__)) target_cfg {
7171
uint16_t rt_family_id; /*!< If assigned, this is a flexible family ID */
7272
uint8_t erase_reset; /*!< Reset after performing an erase */
7373
uint8_t pad;
74+
75+
//! @name CMSIS-DAP v2.1 target strings
76+
//@{
77+
char *target_vendor; /*!< Must match the Dvendor attribute value of the CMSIS DFP, excluding the
78+
colon and vendor code suffix when present. Maximum 60 characters including
79+
terminal NULL. */
80+
char *target_part_number; /*!< Part number of the target device. Must match the Dname attribute value
81+
of the device's CMSIS DFP. Maximum 60 characters including terminal NULL. */
82+
//@}
7483
} target_cfg_t;
7584

7685
extern target_cfg_t target_device;

0 commit comments

Comments
 (0)