diff --git a/conf/Makefile.chibios-libopencm3 b/conf/Makefile.chibios-libopencm3 index 102ac4110c..91280de61b 100644 --- a/conf/Makefile.chibios-libopencm3 +++ b/conf/Makefile.chibios-libopencm3 @@ -37,12 +37,12 @@ endif # # This is the common Makefile for target using chibios - CHIBIOS_BOARD_DIR = $(PAPARAZZI_SRC)/sw/airborne/boards/$(BOARD)/chibios-libopencm3 CHIBIOS_LIB_DIR = $(PAPARAZZI_SRC)/sw/airborne/subsystems/chibios-libopencm3 CHIBIOS_EXT = $(PAPARAZZI_SRC)/sw/ext/chibios OPENCM3_EXT = $(PAPARAZZI_SRC)/sw/ext/libopencm3 PPRZ_GENERATED = $(PAPARAZZI_SRC)/var/$(AIRCRAFT)/generated +PPRZ_CHIBIOS_OCM3 = $(SRC_FIRMWARE)/chibios-libopencm3 # Launch with "make Q=''" to get full command display Q=@ @@ -208,7 +208,8 @@ INCDIR = $(PORTINC) $(KERNINC) $(TESTINC) \ $(HALINC) $(PLATFORMINC) $(BOARDINC) \ $(CHIBIOS)/os/various $(OPENCM3_INC) \ $(CHIBIOS_BOARD_DIR) $(CHIBIOS_LIB_DIR) \ - $(PPRZ_GENERATED) $(FATFSINC) + $(PPRZ_GENERATED) $(FATFSINC) \ + $(PPRZ_CHIBIOS_OCM3) BUILDDIR := $(OBJDIR) # diff --git a/sw/airborne/boards/apogee/chibios-libopencm3/mcuconf.h b/sw/airborne/boards/apogee/chibios-libopencm3/mcuconf.h index 8805490d28..f05cac9c79 100644 --- a/sw/airborne/boards/apogee/chibios-libopencm3/mcuconf.h +++ b/sw/airborne/boards/apogee/chibios-libopencm3/mcuconf.h @@ -249,7 +249,7 @@ #define STM32_USB_OTG2_IRQ_PRIORITY 14 #define STM32_USB_OTG1_RX_FIFO_SIZE 512 #define STM32_USB_OTG2_RX_FIFO_SIZE 512 -#define STM32_USB_OTG_THREAD_PRIO LOWPRIO +#define STM32_USB_OTG_THREAD_PRIO HIGHPRIO #define STM32_USB_OTG_THREAD_STACK_SIZE 256 #define STM32_USB_OTGFIFO_FILL_BASEPRI 0 diff --git a/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.c b/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.c index 4b2848516a..77f00a03e6 100644 --- a/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.c +++ b/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.c @@ -32,6 +32,7 @@ #include "pprz_stub.h" #include "rtcAccess.h" #include "airframe.h" +#include "chibios_init.h" // Delay before starting SD log #ifndef SDLOG_START_DELAY @@ -51,6 +52,8 @@ static __attribute__((noreturn)) msg_t thd_heartbeat(void *arg); #define MAX(x , y) (((x) > (y)) ? (x) : (y)) #define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0])) +Thread *pprzThdPtr = NULL; + static WORKING_AREA(wa_thd_heartbeat, 2048); void chibios_launch_heartbeat (void); bool_t sdOk = FALSE; @@ -79,7 +82,7 @@ bool_t chibios_init(void) { static WORKING_AREA(pprzThd, 4096); void launch_pprz_thd (int32_t (*thd) (void *arg)) { - chThdCreateStatic(pprzThd, sizeof(pprzThd), NORMALPRIO+1, thd, NULL); + pprzThdPtr = chThdCreateStatic(pprzThd, sizeof(pprzThd), NORMALPRIO+1, thd, NULL); } @@ -92,9 +95,9 @@ static __attribute__((noreturn)) msg_t thd_heartbeat(void *arg) chRegSetThreadName("pprz heartbeat"); chThdSleepSeconds (SDLOG_START_DELAY); - if (usbStorageIsItRunning ()) + if (usbStorageIsItRunning ()) chThdSleepSeconds (20000); // stuck here for hours - else + else sdOk = chibios_logInit(true); while (TRUE) { diff --git a/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.h b/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.h index 6b0ca7603e..7660a5360f 100644 --- a/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.h +++ b/sw/airborne/firmwares/fixedwing/chibios-libopencm3/chibios_init.h @@ -27,8 +27,10 @@ #ifndef CHIBIOS_INIT_H #define CHIBIOS_INIT_H +#include #include "std.h" +extern Thread *pprzThdPtr; extern bool_t chibios_init(void); extern void launch_pprz_thd (int32_t (*thd) (void *arg)); diff --git a/sw/airborne/firmwares/fixedwing/main_chibios_libopencm3.c b/sw/airborne/firmwares/fixedwing/main_chibios_libopencm3.c index cd6e2db71b..241a8471e1 100644 --- a/sw/airborne/firmwares/fixedwing/main_chibios_libopencm3.c +++ b/sw/airborne/firmwares/fixedwing/main_chibios_libopencm3.c @@ -84,7 +84,7 @@ static int32_t pprz_thd (void *arg) (void) arg; chibios_chRegSetThreadName ("pprz big loop"); - while (TRUE) { + while (!chThdShouldTerminate()) { Fbw(handle_periodic_tasks); Ap(handle_periodic_tasks); Fbw(event_task); diff --git a/sw/airborne/subsystems/chibios-libopencm3/usbStorage.c b/sw/airborne/subsystems/chibios-libopencm3/usbStorage.c index 1020d21a7a..baaf7dc6d3 100644 --- a/sw/airborne/subsystems/chibios-libopencm3/usbStorage.c +++ b/sw/airborne/subsystems/chibios-libopencm3/usbStorage.c @@ -29,10 +29,13 @@ #include "usb_msd.h" #include "usbStorage.h" #include "chibios_sdlog.h" +#include "chibios_init.h" #include #include #include +static uint8_t nibbleToHex (uint8_t nibble); +static void populateSerialNumberDescriptorData (void); static msg_t thdUsbStorage(void *arg); static Thread* usbStorageThreadPtr=NULL; /* USB mass storage driver */ @@ -148,9 +151,9 @@ static const USBDescriptor vendorDescriptor = /* Product descriptor */ static const uint8_t productDescriptorData[] = { - USB_DESC_BYTE(24), + USB_DESC_BYTE(20), USB_DESC_BYTE(USB_DESCRIPTOR_STRING), - 'A', 0, 'p', 0, 'o', 0, 'g', 0, 'e', 0, 'e', 0, ' ', 0, 'V', 0, '1', 0, '0', 0, '0', 0 + 'A', 0, 'u', 0, 't', 0, 'o', 0, 'p', 0, 'i', 0, 'l', 0, 'o', 0, 't', 0 }; static const USBDescriptor productDescriptor = { @@ -158,12 +161,15 @@ static const USBDescriptor productDescriptor = productDescriptorData }; -/* Serial number descriptor */ -static const uint8_t serialNumberDescriptorData[] = +/* Serial number descriptor from stm32 uniq id */ +/* uniq id is 12 bytes which gives 24 char which require 50 bytes for usb description */ +/* this array is the only one in ram (others in flash), and should be populated before */ +/* usb initialisation */ +uint8_t serialNumberDescriptorData[50] = { - USB_DESC_BYTE(24), + USB_DESC_BYTE(50), USB_DESC_BYTE(USB_DESCRIPTOR_STRING), - '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '1', 0, '.', 0, '0', 0, '0', 0 + 0 }; static const USBDescriptor serialNumberDescriptor = { @@ -252,10 +258,11 @@ static USBMassStorageConfig msdConfig = }; -static WORKING_AREA(waThsUsbStorage, 1024); +static WORKING_AREA(waThdUsbStorage, 1024); void usbStorageStartPolling (void) { - usbStorageThreadPtr = chThdCreateStatic (waThsUsbStorage, sizeof(waThsUsbStorage), + populateSerialNumberDescriptorData (); + usbStorageThreadPtr = chThdCreateStatic (waThdUsbStorage, sizeof(waThdUsbStorage), NORMALPRIO, thdUsbStorage, NULL); } @@ -283,6 +290,7 @@ static msg_t thdUsbStorage(void *arg) (void) arg; // unused chRegSetThreadName("UsbStorage:polling"); uint antiBounce=5; + EventListener connected; // Should use EXTI interrupt instead of active polling, // but in the chibios_opencm3 implementation, since EXTI is @@ -301,8 +309,10 @@ static msg_t thdUsbStorage(void *arg) isRunning = true; chRegSetThreadName("UsbStorage:connected"); + /* Stop the logs*/ chibios_logFinish (); + /* connect sdcard sdc interface sdio */ if (sdioConnect () == false) chThdExit (RDY_TIMEOUT); @@ -319,11 +329,24 @@ static msg_t thdUsbStorage(void *arg) usbStart(&USBD1, &usbConfig); usbConnectBus(&USBD1); - /* watch the mass storage events */ + /* wait for a real usb storage connexion before shutting down autopilot */ + chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1)); + chEvtWaitOne(EVENT_MASK(1)); + + /* stop autopilot */ + if (pprzThdPtr != NULL) { + chThdTerminate (pprzThdPtr); + chThdWait (pprzThdPtr); + pprzThdPtr = NULL; + } + + /* wait until usb-storage is unmount and usb cable is unplugged*/ while (!chThdShouldTerminate() && palReadPad (GPIOA, GPIOA_OTG_FS_VBUS)) { chThdSleepMilliseconds(10); } + + /* then close open descriptors and reboot autopilot */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(500); msdStop(&UMSD1); @@ -337,3 +360,25 @@ bool_t usbStorageIsItRunning (void) { return isRunning; } + +static uint8_t nibbleToHex (uint8_t nibble) +{ + nibble &= 0x0F; + return nibble < 10 ? nibble + '0' : nibble + 'A' - 10; +} + + +static void populateSerialNumberDescriptorData (void) +{ + const uint8_t *UniqProcessorId = (uint8_t *) 0x1FFF7A10; + const uint8_t UniqProcessorIdLen = 12; + + uint8_t *baseDdPtr = &serialNumberDescriptorData[2]; + for(uint32_t i=0; i> 4); + *baseDdPtr++ = 0; + *baseDdPtr++ = nibbleToHex(UniqProcessorId[i]); + *baseDdPtr++ = 0; + } +} +