From e40457d98f63c8bb4ac2150bf8ccf78b239819c5 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 25 Jun 2018 14:10:06 +1000 Subject: [PATCH] HAL_ChibiOS: avoid malloc in usbcfg this avoids the need for malloc in the bootloader --- libraries/AP_HAL_ChibiOS/Scheduler.h | 2 ++ libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c | 14 ++++++++++ libraries/AP_HAL_ChibiOS/hwdef/common/stubs.c | 2 +- .../AP_HAL_ChibiOS/hwdef/common/usbcfg.c | 26 ++++++++++--------- .../hwdef/scripts/chibios_hwdef.py | 3 ++- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/libraries/AP_HAL_ChibiOS/Scheduler.h b/libraries/AP_HAL_ChibiOS/Scheduler.h index 55decbc287..cdeda49ab3 100644 --- a/libraries/AP_HAL_ChibiOS/Scheduler.h +++ b/libraries/AP_HAL_ChibiOS/Scheduler.h @@ -117,8 +117,10 @@ private: #if HAL_WITH_UAVCAN thread_t* _uavcan_thread_ctx; #endif +#if CH_CFG_USE_SEMAPHORES == TRUE binary_semaphore_t _timer_semaphore; binary_semaphore_t _io_semaphore; +#endif static void _timer_thread(void *arg); static void _rcin_thread(void *arg); static void _io_thread(void *arg); diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c index 545486056e..023ff2a402 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c @@ -59,6 +59,11 @@ int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) str[ms.eos] = 0; /* Return number of bytes that would have been written.*/ +#else + (void)str; + (void)size; + (void)fmt; + (void)ap; #endif return retval; } @@ -75,6 +80,9 @@ int snprintf(char *str, size_t size, const char *fmt, ...) return done; #else + (void)str; + (void)size; + (void)fmt; return 0; #endif } @@ -94,6 +102,9 @@ int vasprintf(char **strp, const char *fmt, va_list ap) (*strp) = buf; return len; #else + (void)strp; + (void)fmt; + (void)ap; return 0; #endif } @@ -107,6 +118,8 @@ int asprintf(char **strp, const char *fmt, ...) va_end(ap); return ret; #else + (void)strp; + (void)fmt; return 0; #endif } @@ -136,6 +149,7 @@ int printf(const char *fmt, ...) return done; #else + (void)fmt; return 0; #endif } diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stubs.c b/libraries/AP_HAL_ChibiOS/hwdef/common/stubs.c index e220b8f6d7..6eb240bc66 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/stubs.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stubs.c @@ -152,7 +152,7 @@ int _close(struct _reent *r, int file) __attribute__((used)) caddr_t _sbrk(struct _reent *r, int incr) { -#if CH_CFG_USE_MEMCORE +#if CH_CFG_USE_MEMCORE && CH_CFG_USE_HEAP == TRUE void *p; chDbgCheck(incr >= 0); diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/usbcfg.c b/libraries/AP_HAL_ChibiOS/hwdef/common/usbcfg.c index cd6daf6eb8..6c2102c216 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/usbcfg.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/usbcfg.c @@ -178,6 +178,9 @@ static USBDescriptor vcom_strings[] = { {0, NULL}, // version }; +#define USB_DESC_MAX_STRLEN 100 +static uint8_t vcom_buffers[3][2+2*USB_DESC_MAX_STRLEN]; + /* check if one string contains another */ @@ -196,7 +199,7 @@ static bool string_contains(const char *haystack, const char *needle) /* handle substitution of variables in strings for USB descriptors */ -static char *string_substitute(const char *str) +static void string_substitute(const char *str, char *str2) { uint8_t new_len = strlen(str); if (string_contains(str, "%BOARD%")) { @@ -205,7 +208,10 @@ static char *string_substitute(const char *str) if (string_contains(str, "%SERIAL%")) { new_len += 24 - 8; } - char *str2 = malloc(new_len+1); + if (new_len+1 > USB_DESC_MAX_STRLEN) { + strcpy(str2, str); + return; + } char *p = str2; while (*str) { char c = *str; @@ -231,19 +237,18 @@ static char *string_substitute(const char *str) *p++ = *str++; } *p = 0; - return str2; } /* dynamically allocate a USB descriptor string */ -static void setup_usb_string(USBDescriptor *desc, const char *str) +static void setup_usb_string(USBDescriptor *desc, const char *str, uint8_t *b) { - char *str2 = string_substitute(str); + char str2[USB_DESC_MAX_STRLEN]; + string_substitute(str, str2); uint8_t len = strlen(str2); desc->ud_size = 2+2*len; - uint8_t *b = (uint8_t *)calloc(1, desc->ud_size); desc->ud_string = (const uint8_t *)b; b[0] = USB_DESC_BYTE(desc->ud_size); b[1] = USB_DESC_BYTE(USB_DESCRIPTOR_STRING); @@ -252,9 +257,6 @@ static void setup_usb_string(USBDescriptor *desc, const char *str) b[2+i*2] = str2[i]; b[2+i*2+1] = 0; } - if (str2 != str) { - free(str2); - } } /* @@ -262,9 +264,9 @@ static void setup_usb_string(USBDescriptor *desc, const char *str) */ void setup_usb_strings(void) { - setup_usb_string(&vcom_strings[1], HAL_USB_STRING_MANUFACTURER); - setup_usb_string(&vcom_strings[2], HAL_USB_STRING_PRODUCT); - setup_usb_string(&vcom_strings[3], HAL_USB_STRING_SERIAL); + setup_usb_string(&vcom_strings[1], HAL_USB_STRING_MANUFACTURER, vcom_buffers[0]); + setup_usb_string(&vcom_strings[2], HAL_USB_STRING_PRODUCT, vcom_buffers[1]); + setup_usb_string(&vcom_strings[3], HAL_USB_STRING_SERIAL, vcom_buffers[2]); } /* diff --git a/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py b/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py index bfbb649a15..3c21ae478d 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py +++ b/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py @@ -453,7 +453,8 @@ def write_mcu_config(f): #define CH_CFG_USE_DYNAMIC FALSE #define CH_CFG_USE_MEMPOOLS FALSE #define CH_DBG_FILL_THREADS FALSE -#define CH_CFG_USE_SEMAPHORES TRUE +#define CH_CFG_USE_SEMAPHORES FALSE +#define CH_CFG_USE_HEAP FALSE #define CH_CFG_USE_MUTEXES FALSE #define CH_CFG_USE_CONDVARS FALSE #define CH_CFG_USE_CONDVARS_TIMEOUT FALSE