Skip to content

Commit e4c1448

Browse files
Remove exception handling code, save RAM and FLASH (#245)
Use a libstdc++ compiled with -fno-exceptions to avoid including the code needed to unwind C++ exceptions. Saves ~4K RAM and ~5K flash.
1 parent 05356da commit e4c1448

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

cores/rp2040/RP2040USB.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,41 @@ int __USBGetMouseReportID() {
122122
}
123123

124124
static uint8_t *GetDescHIDReport(int *len) {
125+
static uint8_t *report = nullptr;
126+
int report_len = 0;
127+
128+
if (report) {
129+
free(report);
130+
}
131+
125132
if (__USBInstallKeyboard && __USBInstallMouse) {
126-
static uint8_t desc_hid_report[] = {
133+
uint8_t desc_hid_report[] = {
127134
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)),
128135
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2))
129136
};
130-
if (len) {
131-
*len = sizeof(desc_hid_report);
132-
}
133-
return desc_hid_report;
137+
report_len = sizeof(desc_hid_report);
138+
report = (uint8_t *)malloc(report_len);
139+
memcpy(report, desc_hid_report, report_len);
134140
} else if (__USBInstallKeyboard && ! __USBInstallMouse) {
135-
static uint8_t desc_hid_report[] = {
141+
uint8_t desc_hid_report[] = {
136142
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1))
137143
};
138-
if (len) {
139-
*len = sizeof(desc_hid_report);
140-
}
141-
return desc_hid_report;
144+
report_len = sizeof(desc_hid_report);
145+
report = (uint8_t *)malloc(report_len);
146+
memcpy(report, desc_hid_report, report_len);
142147
} else { // if (!__USBInstallKeyboard && __USBInstallMouse) {
143-
static uint8_t desc_hid_report[] = {
148+
uint8_t desc_hid_report[] = {
144149
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1))
145150
};
146-
if (len) {
147-
*len = sizeof(desc_hid_report);
148-
}
149-
return desc_hid_report;
151+
report_len = sizeof(desc_hid_report);
152+
report = (uint8_t *)malloc(report_len);
153+
memcpy(report, desc_hid_report, report_len);
154+
}
155+
156+
if (len) {
157+
*len = report_len;
150158
}
159+
return report;
151160
}
152161

153162
// Invoked when received GET HID REPORT DESCRIPTOR
@@ -161,35 +170,35 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) {
161170

162171
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
163172
(void)index;
164-
static uint8_t *usbd_desc_cfg = NULL;
173+
static uint8_t *usbd_desc_cfg = nullptr;
165174

166175
if (!usbd_desc_cfg) {
167176
bool hasHID = __USBInstallKeyboard || __USBInstallMouse;
168177

169178
uint8_t interface_count = (__USBInstallSerial ? 2 : 0) + (hasHID ? 1 : 0) + (__USBInstallMIDI ? 2 : 0);
170179

171-
static uint8_t cdc_desc[TUD_CDC_DESC_LEN] = {
180+
uint8_t cdc_desc[TUD_CDC_DESC_LEN] = {
172181
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
173182
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE)
174183
};
175184

176185
int hid_report_len;
177186
GetDescHIDReport(&hid_report_len);
178187
uint8_t hid_itf = __USBInstallSerial ? 2 : 0;
179-
static uint8_t hid_desc[TUD_HID_DESC_LEN] = {
188+
uint8_t hid_desc[TUD_HID_DESC_LEN] = {
180189
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
181190
TUD_HID_DESCRIPTOR(hid_itf, 0, HID_ITF_PROTOCOL_NONE, hid_report_len, EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
182191
};
183192

184193
uint8_t midi_itf = hid_itf + (hasHID ? 1 : 0);
185-
static uint8_t midi_desc[TUD_MIDI_DESC_LEN] = {
194+
uint8_t midi_desc[TUD_MIDI_DESC_LEN] = {
186195
// Interface number, string index, EP Out & EP In address, EP size
187196
TUD_MIDI_DESCRIPTOR(midi_itf, 0, EPNUM_MIDI, 0x80 | EPNUM_MIDI, 64)
188197
};
189198

190199
int usbd_desc_len = TUD_CONFIG_DESC_LEN + (__USBInstallSerial ? sizeof(cdc_desc) : 0) + (hasHID ? sizeof(hid_desc) : 0) + (__USBInstallMIDI ? sizeof(midi_desc) : 0);
191200

192-
static uint8_t tud_cfg_desc[TUD_CONFIG_DESC_LEN] = {
201+
uint8_t tud_cfg_desc[TUD_CONFIG_DESC_LEN] = {
193202
// Config number, interface count, string index, total length, attribute, power in mA
194203
TUD_CONFIG_DESCRIPTOR(1, interface_count, USBD_STR_0, usbd_desc_len, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA)
195204
};
@@ -277,6 +286,7 @@ void __USBStart() {
277286
}
278287

279288
mutex_init(&__usb_mutex);
289+
280290
tusb_init();
281291

282292
irq_set_exclusive_handler(USB_TASK_IRQ, usb_irq);

cores/rp2040/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extern "C" int main() {
7676
#endif
7777

7878
#if defined DEBUG_RP2040_PORT
79-
DEBUG_RP2040_PORT.begin();
79+
DEBUG_RP2040_PORT.begin(115200);
8080
#endif
8181

8282
#ifndef NO_USB

lib/libstdc++.a

18.2 MB
Binary file not shown.

platform.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ compiler.includes="-iprefix{runtime.platform.path}/" "@{runtime.platform.path}/l
4343
compiler.flags=-Os -march=armv6-m -mcpu=cortex-m0plus -mthumb -ffunction-sections -fdata-sections -fno-exceptions
4444
compiler.wrap="@{runtime.platform.path}/lib/platform_wrap.txt"
4545
compiler.libpico="{runtime.platform.path}/lib/libpico.a"
46+
compiler.libstdcpp="{runtime.platform.path}/lib/libstdc++.a"
4647

4748
compiler.c.cmd=arm-none-eabi-gcc
4849
compiler.c.flags=-c {compiler.warning_flags} {compiler.defines} {compiler.flags} {compiler.includes} -std=gnu17 -g
@@ -113,7 +114,7 @@ recipe.hooks.linking.prelink.1.pattern="{runtime.tools.pqt-python3.path}/python3
113114
recipe.hooks.linking.prelink.2.pattern="{compiler.path}{compiler.S.cmd}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -c "{runtime.platform.path}/boot2/{build.boot2}.S" "-I{runtime.platform.path}/pico-sdk/src/rp2040/hardware_regs/include/" "-I{runtime.platform.path}/pico-sdk/src/common/pico_binary_info/include" -o "{build.path}/boot2.o"
114115

115116
## Combine gc-sections, archives, and objects
116-
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {compiler.ldflags} "-Wl,--script={build.path}/memmap_default.ld" "-Wl,-Map,{build.path}/{build.project_name}.map" -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{build.path}/{archive_file}" "{build.path}/boot2.o" {compiler.libpico} -lm -lc -lstdc++ -lc -Wl,--end-group
117+
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {compiler.ldflags} "-Wl,--script={build.path}/memmap_default.ld" "-Wl,-Map,{build.path}/{build.project_name}.map" -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{build.path}/{archive_file}" "{build.path}/boot2.o" {compiler.libpico} -lm -lc {compiler.libstdcpp} -lc -Wl,--end-group
117118

118119
## Create output (UF2 file)
119120
recipe.objcopy.uf2.pattern="{runtime.tools.pqt-elf2uf2.path}/elf2uf2" "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.uf2"

0 commit comments

Comments
 (0)