[usb] improve usb storage

° when an usb is plugged, wait for an usb-storage cnxion, then
  stop the autopilot to reserve cp for usb-storage thread.
  Yes, we should avoid false detection, so there is two level of detection
  before shutting down autopilot thread.

remove spurious white char @ eol
This commit is contained in:
Alexandre Bustico
2014-02-12 15:47:55 +01:00
committed by Gautier Hattenberger
parent 6bbc295f03
commit 5ce3896d37
6 changed files with 67 additions and 16 deletions
+3 -2
View File
@@ -37,12 +37,12 @@ endif
# #
# This is the common Makefile for target using chibios # This is the common Makefile for target using chibios
CHIBIOS_BOARD_DIR = $(PAPARAZZI_SRC)/sw/airborne/boards/$(BOARD)/chibios-libopencm3 CHIBIOS_BOARD_DIR = $(PAPARAZZI_SRC)/sw/airborne/boards/$(BOARD)/chibios-libopencm3
CHIBIOS_LIB_DIR = $(PAPARAZZI_SRC)/sw/airborne/subsystems/chibios-libopencm3 CHIBIOS_LIB_DIR = $(PAPARAZZI_SRC)/sw/airborne/subsystems/chibios-libopencm3
CHIBIOS_EXT = $(PAPARAZZI_SRC)/sw/ext/chibios CHIBIOS_EXT = $(PAPARAZZI_SRC)/sw/ext/chibios
OPENCM3_EXT = $(PAPARAZZI_SRC)/sw/ext/libopencm3 OPENCM3_EXT = $(PAPARAZZI_SRC)/sw/ext/libopencm3
PPRZ_GENERATED = $(PAPARAZZI_SRC)/var/$(AIRCRAFT)/generated PPRZ_GENERATED = $(PAPARAZZI_SRC)/var/$(AIRCRAFT)/generated
PPRZ_CHIBIOS_OCM3 = $(SRC_FIRMWARE)/chibios-libopencm3
# Launch with "make Q=''" to get full command display # Launch with "make Q=''" to get full command display
Q=@ Q=@
@@ -208,7 +208,8 @@ INCDIR = $(PORTINC) $(KERNINC) $(TESTINC) \
$(HALINC) $(PLATFORMINC) $(BOARDINC) \ $(HALINC) $(PLATFORMINC) $(BOARDINC) \
$(CHIBIOS)/os/various $(OPENCM3_INC) \ $(CHIBIOS)/os/various $(OPENCM3_INC) \
$(CHIBIOS_BOARD_DIR) $(CHIBIOS_LIB_DIR) \ $(CHIBIOS_BOARD_DIR) $(CHIBIOS_LIB_DIR) \
$(PPRZ_GENERATED) $(FATFSINC) $(PPRZ_GENERATED) $(FATFSINC) \
$(PPRZ_CHIBIOS_OCM3)
BUILDDIR := $(OBJDIR) BUILDDIR := $(OBJDIR)
# #
@@ -249,7 +249,7 @@
#define STM32_USB_OTG2_IRQ_PRIORITY 14 #define STM32_USB_OTG2_IRQ_PRIORITY 14
#define STM32_USB_OTG1_RX_FIFO_SIZE 512 #define STM32_USB_OTG1_RX_FIFO_SIZE 512
#define STM32_USB_OTG2_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_OTG_THREAD_STACK_SIZE 256
#define STM32_USB_OTGFIFO_FILL_BASEPRI 0 #define STM32_USB_OTGFIFO_FILL_BASEPRI 0
@@ -32,6 +32,7 @@
#include "pprz_stub.h" #include "pprz_stub.h"
#include "rtcAccess.h" #include "rtcAccess.h"
#include "airframe.h" #include "airframe.h"
#include "chibios_init.h"
// Delay before starting SD log // Delay before starting SD log
#ifndef SDLOG_START_DELAY #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 MAX(x , y) (((x) > (y)) ? (x) : (y))
#define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0])) #define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
Thread *pprzThdPtr = NULL;
static WORKING_AREA(wa_thd_heartbeat, 2048); static WORKING_AREA(wa_thd_heartbeat, 2048);
void chibios_launch_heartbeat (void); void chibios_launch_heartbeat (void);
bool_t sdOk = FALSE; bool_t sdOk = FALSE;
@@ -79,7 +82,7 @@ bool_t chibios_init(void) {
static WORKING_AREA(pprzThd, 4096); static WORKING_AREA(pprzThd, 4096);
void launch_pprz_thd (int32_t (*thd) (void *arg)) 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);
} }
@@ -27,8 +27,10 @@
#ifndef CHIBIOS_INIT_H #ifndef CHIBIOS_INIT_H
#define CHIBIOS_INIT_H #define CHIBIOS_INIT_H
#include <ch.h>
#include "std.h" #include "std.h"
extern Thread *pprzThdPtr;
extern bool_t chibios_init(void); extern bool_t chibios_init(void);
extern void launch_pprz_thd (int32_t (*thd) (void *arg)); extern void launch_pprz_thd (int32_t (*thd) (void *arg));
@@ -84,7 +84,7 @@ static int32_t pprz_thd (void *arg)
(void) arg; (void) arg;
chibios_chRegSetThreadName ("pprz big loop"); chibios_chRegSetThreadName ("pprz big loop");
while (TRUE) { while (!chThdShouldTerminate()) {
Fbw(handle_periodic_tasks); Fbw(handle_periodic_tasks);
Ap(handle_periodic_tasks); Ap(handle_periodic_tasks);
Fbw(event_task); Fbw(event_task);
@@ -29,10 +29,13 @@
#include "usb_msd.h" #include "usb_msd.h"
#include "usbStorage.h" #include "usbStorage.h"
#include "chibios_sdlog.h" #include "chibios_sdlog.h"
#include "chibios_init.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sdio.h> #include <sdio.h>
static uint8_t nibbleToHex (uint8_t nibble);
static void populateSerialNumberDescriptorData (void);
static msg_t thdUsbStorage(void *arg); static msg_t thdUsbStorage(void *arg);
static Thread* usbStorageThreadPtr=NULL; static Thread* usbStorageThreadPtr=NULL;
/* USB mass storage driver */ /* USB mass storage driver */
@@ -148,9 +151,9 @@ static const USBDescriptor vendorDescriptor =
/* Product descriptor */ /* Product descriptor */
static const uint8_t productDescriptorData[] = static const uint8_t productDescriptorData[] =
{ {
USB_DESC_BYTE(24), USB_DESC_BYTE(20),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), 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 = static const USBDescriptor productDescriptor =
{ {
@@ -158,12 +161,15 @@ static const USBDescriptor productDescriptor =
productDescriptorData productDescriptorData
}; };
/* Serial number descriptor */ /* Serial number descriptor from stm32 uniq id */
static const uint8_t serialNumberDescriptorData[] = /* 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), 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 = static const USBDescriptor serialNumberDescriptor =
{ {
@@ -252,10 +258,11 @@ static USBMassStorageConfig msdConfig =
}; };
static WORKING_AREA(waThsUsbStorage, 1024); static WORKING_AREA(waThdUsbStorage, 1024);
void usbStorageStartPolling (void) void usbStorageStartPolling (void)
{ {
usbStorageThreadPtr = chThdCreateStatic (waThsUsbStorage, sizeof(waThsUsbStorage), populateSerialNumberDescriptorData ();
usbStorageThreadPtr = chThdCreateStatic (waThdUsbStorage, sizeof(waThdUsbStorage),
NORMALPRIO, thdUsbStorage, NULL); NORMALPRIO, thdUsbStorage, NULL);
} }
@@ -283,6 +290,7 @@ static msg_t thdUsbStorage(void *arg)
(void) arg; // unused (void) arg; // unused
chRegSetThreadName("UsbStorage:polling"); chRegSetThreadName("UsbStorage:polling");
uint antiBounce=5; uint antiBounce=5;
EventListener connected;
// Should use EXTI interrupt instead of active polling, // Should use EXTI interrupt instead of active polling,
// but in the chibios_opencm3 implementation, since EXTI is // but in the chibios_opencm3 implementation, since EXTI is
@@ -301,8 +309,10 @@ static msg_t thdUsbStorage(void *arg)
isRunning = true; isRunning = true;
chRegSetThreadName("UsbStorage:connected"); chRegSetThreadName("UsbStorage:connected");
/* Stop the logs*/
chibios_logFinish (); chibios_logFinish ();
/* connect sdcard sdc interface sdio */ /* connect sdcard sdc interface sdio */
if (sdioConnect () == false) if (sdioConnect () == false)
chThdExit (RDY_TIMEOUT); chThdExit (RDY_TIMEOUT);
@@ -319,11 +329,24 @@ static msg_t thdUsbStorage(void *arg)
usbStart(&USBD1, &usbConfig); usbStart(&USBD1, &usbConfig);
usbConnectBus(&USBD1); 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)) { while (!chThdShouldTerminate() && palReadPad (GPIOA, GPIOA_OTG_FS_VBUS)) {
chThdSleepMilliseconds(10); chThdSleepMilliseconds(10);
} }
/* then close open descriptors and reboot autopilot */
usbDisconnectBus(&USBD1); usbDisconnectBus(&USBD1);
chThdSleepMilliseconds(500); chThdSleepMilliseconds(500);
msdStop(&UMSD1); msdStop(&UMSD1);
@@ -337,3 +360,25 @@ bool_t usbStorageIsItRunning (void)
{ {
return isRunning; 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<UniqProcessorIdLen; i++) {
*baseDdPtr++ = nibbleToHex(UniqProcessorId[i] >> 4);
*baseDdPtr++ = 0;
*baseDdPtr++ = nibbleToHex(UniqProcessorId[i]);
*baseDdPtr++ = 0;
}
}