Skip to content

Commit

Permalink
HAL_ChibiOS: fixed stdout in early startup bug
Browse files Browse the repository at this point in the history
this caused a failure to boot on some boards if they tried to print
messages in early startup code before hal was initialised

thanks to @Shadowru for reporting the issue
  • Loading branch information
tridge committed Feb 11, 2020
1 parent b1742b4 commit 02937f2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libraries/AP_HAL_ChibiOS/HAL_ChibiOS_Class.cpp
Expand Up @@ -279,8 +279,8 @@ void HAL_ChibiOS::run(int argc, char * const argv[], Callbacks* callbacks) const
* RTOS is active.
*/

#ifdef HAL_USB_PRODUCT_ID
setup_usb_strings();
#if HAL_USE_SERIAL_USB == TRUE
usb_initiailse();
#endif

#ifdef HAL_STDOUT_SERIAL
Expand Down
31 changes: 31 additions & 0 deletions libraries/AP_HAL_ChibiOS/UARTDriver.cpp
Expand Up @@ -1446,4 +1446,35 @@ uint8_t UARTDriver::get_options(void) const
return _last_options;
}

#if HAL_USE_SERIAL_USB == TRUE
/*
initialise the USB bus, called from both UARTDriver and stdio for startup debug
This can be called before the hal is initialised so must not call any hal functions
*/
void usb_initiailse(void)
{
static bool initialised;
if (initialised) {
return;
}
initialised = true;
setup_usb_strings();
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusbcfg1);
#if HAL_HAVE_DUAL_USB_CDC
sduObjectInit(&SDU2);
sduStart(&SDU2, &serusbcfg2);
#endif
/*
* Activates the USB driver and then the USB bus pull-up on D+.
* Note, a delay is inserted in order to not have to disconnect the cable
* after a reset.
*/
usbDisconnectBus(serusbcfg1.usbp);
chThdSleep(chTimeUS2I(1500));
usbStart(serusbcfg1.usbp, &usbcfg);
usbConnectBus(serusbcfg1.usbp);
}
#endif

#endif //CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
3 changes: 3 additions & 0 deletions libraries/AP_HAL_ChibiOS/UARTDriver.h
Expand Up @@ -238,3 +238,6 @@ class ChibiOS::UARTDriver : public AP_HAL::UARTDriver {
void thread_init();
static void uart_thread(void *);
};

// access to usb init for stdio.cpp
void usb_initiailse(void);
2 changes: 2 additions & 0 deletions libraries/AP_HAL_ChibiOS/stdio.cpp
Expand Up @@ -31,6 +31,7 @@
#include <AP_HAL/AP_HAL.h>
#if HAL_USE_SERIAL_USB == TRUE
#include <AP_HAL_ChibiOS/hwdef/common/usbcfg.h>
#include "UARTDriver.h"
#endif

extern const AP_HAL::HAL& hal;
Expand Down Expand Up @@ -85,6 +86,7 @@ int __wrap_vprintf(const char *fmt, va_list arg)
#ifdef HAL_STDOUT_SERIAL
return chvprintf((BaseSequentialStream*)&HAL_STDOUT_SERIAL, fmt, arg);
#elif HAL_USE_SERIAL_USB == TRUE
usb_initiailse();
return chvprintf((BaseSequentialStream*)&SDU1, fmt, arg);
#else
(void)arg;
Expand Down

0 comments on commit 02937f2

Please sign in to comment.