Skip to content

Commit 64c531b

Browse files
committed
Changed weak target family descriptors to references.
- This causes the g_families[] entries to be NULL if the family descriptor is not defined. So the g_families terminator was changed to all Fs and init_families() updated appropriately. - This changed saves quite a bit of .rodata.
1 parent 9e5ebfc commit 64c531b

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

source/target/target_family.c

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,33 @@ const target_family_descriptor_t g_sw_sysresetreq_family = {
4343
.soft_reset_type = SYSRESETREQ,
4444
};
4545

46-
//Weakly define family
47-
__attribute__((weak))
48-
const target_family_descriptor_t g_nxp_kinetis_kseries = {0};
49-
__attribute__((weak))
50-
const target_family_descriptor_t g_nxp_kinetis_lseries = {0};
51-
__attribute__((weak))
52-
const target_family_descriptor_t g_nxp_kinetis_k32w_series = {0};
53-
__attribute__((weak))
54-
const target_family_descriptor_t g_nxp_mimxrt = {0};
55-
__attribute__((weak))
56-
const target_family_descriptor_t g_nxp_rapid_iot = {0};
57-
__attribute__((weak))
58-
const target_family_descriptor_t g_nordic_nrf51 = {0};
59-
__attribute__((weak))
60-
const target_family_descriptor_t g_nordic_nrf52 = {0};
61-
__attribute__((weak))
62-
const target_family_descriptor_t g_realtek_rtl8195am = {0};
63-
__attribute__((weak))
64-
const target_family_descriptor_t g_ti_family = {0};
65-
__attribute__((weak))
66-
const target_family_descriptor_t g_wiznet_family = {0};
67-
__attribute__((weak))
68-
const target_family_descriptor_t g_renesas_family = {0};
69-
__attribute__((weak))
70-
const target_family_descriptor_t g_toshiba_tz_family = {0};
71-
__attribute__((weak))
72-
const target_family_descriptor_t g_ambiq_ama3b1kk = {0};
46+
// Weak references to family definitions.
47+
extern __WEAK const target_family_descriptor_t g_nxp_kinetis_kseries;
48+
extern __WEAK const target_family_descriptor_t g_nxp_kinetis_lseries;
49+
extern __WEAK const target_family_descriptor_t g_nxp_kinetis_k32w_series;
50+
extern __WEAK const target_family_descriptor_t g_nxp_mimxrt;
51+
extern __WEAK const target_family_descriptor_t g_nxp_rapid_iot;
52+
extern __WEAK const target_family_descriptor_t g_nordic_nrf51;
53+
extern __WEAK const target_family_descriptor_t g_nordic_nrf52;
54+
extern __WEAK const target_family_descriptor_t g_realtek_rtl8195am;
55+
extern __WEAK const target_family_descriptor_t g_ti_family;
56+
extern __WEAK const target_family_descriptor_t g_wiznet_family;
57+
extern __WEAK const target_family_descriptor_t g_renesas_family;
58+
extern __WEAK const target_family_descriptor_t g_toshiba_tz_family;
59+
extern __WEAK const target_family_descriptor_t g_ambiq_ama3b1kk;
7360

61+
//! @brief Terminator value for g_families list.
62+
//!
63+
//! This terminator value is chosen so that weak references to the family descriptors that
64+
//! resolve to NULL at link time do not terminate the list early.
65+
#define FAMILY_LIST_TERMINATOR ((const target_family_descriptor_t *)(0xffffffff))
7466

67+
//! @brief Default list of family descriptors.
68+
//!
69+
//! init_family() scans this list searching for a family descriptor with an ID that matches
70+
//! the family ID set in the board info or target config structs. Because each of the family
71+
//! descriptors has a weak reference defined above, the entry in this list for a family whose
72+
//! descriptor is not included in the link will resolve to NULL and init_family() can skip it.
7573
__attribute__((weak))
7674
const target_family_descriptor_t *g_families[] = {
7775
&g_hw_reset_family,
@@ -90,7 +88,7 @@ const target_family_descriptor_t *g_families[] = {
9088
&g_renesas_family,
9189
&g_toshiba_tz_family,
9290
&g_ambiq_ama3b1kk,
93-
0 // list terminator
91+
FAMILY_LIST_TERMINATOR // list terminator
9492
};
9593

9694
__attribute__((weak))
@@ -99,21 +97,25 @@ const target_family_descriptor_t *g_target_family = NULL;
9997

10098
void init_family(void)
10199
{
102-
uint8_t index = 0;
103-
uint16_t family_id = get_family_id();
104-
if (g_target_family != NULL){ //already set
100+
// Check if the family is already set.
101+
if (g_target_family != NULL) {
105102
return;
106103
}
107104

108-
while (g_families[index]!=0) {
109-
if (g_families[index]->family_id && (g_families[index]->family_id == family_id)) {
105+
// Scan families table looking for matching family ID.
106+
uint8_t index = 0;
107+
uint16_t family_id = get_family_id();
108+
109+
while (g_families[index] != FAMILY_LIST_TERMINATOR) {
110+
if ((g_families[index] != NULL) && (g_families[index]->family_id == family_id)) {
110111
g_target_family = g_families[index];
111112
break;
112113
}
113114
index++;
114115
}
115116

116-
if(g_target_family == NULL){ //default family
117+
// Last resort is to use a default family.
118+
if (g_target_family == NULL) {
117119
g_target_family = &g_hw_reset_family;
118120
}
119121
}
@@ -137,11 +139,11 @@ uint8_t target_set_state(target_state_t state)
137139
swd_set_soft_reset(g_target_family->soft_reset_type);
138140
}
139141
return swd_set_target_state_sw(state);
140-
}else {
142+
} else {
141143
return 1;
142144
}
143145
}
144-
}else{
146+
} else {
145147
return 0;
146148
}
147149
}
@@ -150,7 +152,7 @@ void swd_set_target_reset(uint8_t asserted)
150152
{
151153
if (g_target_family && g_target_family->swd_set_target_reset) {
152154
g_target_family->swd_set_target_reset(asserted);
153-
}else {
155+
} else {
154156
(asserted) ? PIN_nRESET_OUT(0) : PIN_nRESET_OUT(1);
155157
}
156158
}

0 commit comments

Comments
 (0)