Skip to content

Commit 58d3a72

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 d84a064 commit 58d3a72

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

source/target/target_family.c

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,32 @@ 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};
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+
60+
//! @brief Terminator value for g_families list.
61+
//!
62+
//! This terminator value is chosen so that weak references to the family descriptors that
63+
//! resolve to NULL at link time do not terminate the list early.
64+
#define FAMILY_LIST_TERMINATOR ((const target_family_descriptor_t *)(0xffffffff))
7165

66+
//! @brief Default list of family descriptors.
67+
//!
68+
//! init_family() scans this list searching for a family descriptor with an ID that matches
69+
//! the family ID set in the board info or target config structs. Because each of the family
70+
//! descriptors has a weak reference defined above, the entry in this list for a family whose
71+
//! descriptor is not included in the link will resolve to NULL and init_family() can skip it.
7272
__attribute__((weak))
7373
const target_family_descriptor_t *g_families[] = {
7474
&g_hw_reset_family,
@@ -86,7 +86,7 @@ const target_family_descriptor_t *g_families[] = {
8686
&g_wiznet_family,
8787
&g_renesas_family,
8888
&g_toshiba_tz_family,
89-
0 // list terminator
89+
FAMILY_LIST_TERMINATOR // list terminator
9090
};
9191

9292
__attribute__((weak))
@@ -95,21 +95,25 @@ const target_family_descriptor_t *g_target_family = NULL;
9595

9696
void init_family(void)
9797
{
98-
uint8_t index = 0;
99-
uint16_t family_id = get_family_id();
100-
if (g_target_family != NULL){ //already set
98+
// Check if the family is already set.
99+
if (g_target_family != NULL) {
101100
return;
102101
}
103102

104-
while (g_families[index]!=0) {
105-
if (g_families[index]->family_id && (g_families[index]->family_id == family_id)) {
103+
// Scan families table looking for matching family ID.
104+
uint8_t index = 0;
105+
uint16_t family_id = get_family_id();
106+
107+
while (g_families[index] != FAMILY_LIST_TERMINATOR) {
108+
if ((g_families[index] != NULL) && (g_families[index]->family_id == family_id)) {
106109
g_target_family = g_families[index];
107110
break;
108111
}
109112
index++;
110113
}
111114

112-
if(g_target_family == NULL){ //default family
115+
// Last resort is to use a default family.
116+
if (g_target_family == NULL) {
113117
g_target_family = &g_hw_reset_family;
114118
}
115119
}
@@ -133,11 +137,11 @@ uint8_t target_set_state(target_state_t state)
133137
swd_set_soft_reset(g_target_family->soft_reset_type);
134138
}
135139
return swd_set_target_state_sw(state);
136-
}else {
140+
} else {
137141
return 1;
138142
}
139143
}
140-
}else{
144+
} else {
141145
return 0;
142146
}
143147
}
@@ -146,7 +150,7 @@ void swd_set_target_reset(uint8_t asserted)
146150
{
147151
if (g_target_family && g_target_family->swd_set_target_reset) {
148152
g_target_family->swd_set_target_reset(asserted);
149-
}else {
153+
} else {
150154
(asserted) ? PIN_nRESET_OUT(0) : PIN_nRESET_OUT(1);
151155
}
152156
}

0 commit comments

Comments
 (0)