Paparazzi UAS  v5.17_devel-24-g2ae834f
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
usbStorage.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014-2015 Gautier Hattenberger, Alexandre Bustico
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * @file modules/loggers/sdlog_chibios/usbStorage.c
24  *
25  */
26 
27 #include <ch.h>
28 #include <hal.h>
29 #include "usb_msd.h"
30 #include "usbStorage.h"
32 #include <stdio.h>
33 #include <string.h>
34 #include "main_chibios.h"
35 #include "mcu.h"
36 #include "mcu_periph/sdio.h"
37 #include "led.h"
38 
39 static void thdUsbStorage(void *arg);
40 static thread_t *usbStorageThreadPtr = NULL;
41 /* USB mass storage driver */
42 static bool isRunning = false;
43 
44 
45 /* Turns on a LED when there is I/O activity on the USB port */
46 static void usbActivity(bool active __attribute__((unused)))
47 {
48 #ifdef SDLOG_USB_LED
49  if (active) {
51  } else {
53  }
54 #endif
55 }
56 
57 /* USB mass storage configuration */
59  &USBD1,
60  (BaseBlockDevice *) &SDCD1,
62  &usbActivity,
63  "Pprz_sd",
64  "AutoPilot",
65  "0.2"
66 };
67 
68 
69 static THD_WORKING_AREA(waThdUsbStorage, 1024);
71 {
72  usbStorageThreadPtr = chThdCreateStatic(waThdUsbStorage, sizeof(waThdUsbStorage),
73  NORMALPRIO + 2, thdUsbStorage, NULL);
74 
75 }
76 
77 
79 {
80  if (usbStorageThreadPtr != NULL) {
81  chThdWait(usbStorageThreadPtr);
82  }
83  usbStorageThreadPtr = NULL;
84 }
85 
86 void usbStorageStop(void)
87 {
88  if (usbStorageThreadPtr != NULL) {
89  chThdTerminate(usbStorageThreadPtr);
90  }
91 }
92 
93 
94 
95 
96 static void thdUsbStorage(void *arg)
97 {
98  (void) arg;
99 
100  chRegSetThreadName("UsbStorage:polling");
101  event_listener_t connected;
102 
103  palEnablePadEvent(SDLOG_USB_VBUS_PORT, SDLOG_USB_VBUS_PIN, PAL_EVENT_MODE_BOTH_EDGES);
104  // wait transition to HIGH with rebound management
105  do {
106  palWaitPadTimeout(SDLOG_USB_VBUS_PORT, SDLOG_USB_VBUS_PIN, TIME_INFINITE);
107  chThdSleepMilliseconds(10);
108  } while (palReadPad(SDLOG_USB_VBUS_PORT, SDLOG_USB_VBUS_PIN) == PAL_LOW);
109 
110  isRunning = true;
111  chRegSetThreadName("UsbStorage:connected");
112 
113  /* Stop the logs*/
114  // it's not a powerloss, wa have time to flush the ram buffer
115  sdlog_chibios_finish(false);
116 
117 
118  /* connect sdcard sdc interface sdio */
119  if (sdio_connect() == false) {
120  chThdExit(MSG_TIMEOUT);
121  }
122 
123  /* initialize the USB mass storage driver */
124  init_msd_driver(NULL, &msdConfig);
125 
126  /* wait for a real usb storage connexion before shutting down autopilot */
127  msd_register_evt_connected(&connected, EVENT_MASK(1));
128  chEvtWaitOne(EVENT_MASK(1));
129 
130  /* stop autopilot */
132 
133  /* wait until usb-storage is unmount and usb cable is unplugged*/
134  while (!chThdShouldTerminateX() && palReadPad(SDLOG_USB_VBUS_PORT, SDLOG_USB_VBUS_PIN)) {
135  chThdSleepMilliseconds(10);
136  }
137 
139 
140  chThdSleepMilliseconds(500);
141 
142  mcu_reset();
143 }
144 
146 {
147  return isRunning;
148 }
149 
static void usbActivity(bool active)
Definition: usbStorage.c:46
arch independent SDIO API
void sdlog_chibios_finish(const bool flush)
static bool isRunning
Definition: usbStorage.c:42
#define LED_OFF(i)
Definition: led_hw.h:52
#define SDLOG_USB_LED
Definition: board.h:577
#define USB_MS_DATA_EP
Definition: usb_msd.h:40
#define SDLOG_USB_VBUS_PIN
Definition: board.h:579
static thread_t * usbStorageThreadPtr
Definition: usbStorage.c:40
bool sdio_connect(void)
Connect a SD card on SDIO peripheral.
Definition: sdio_arch.c:48
void deinit_msd_driver(void)
Definition: usb_msd.c:1121
static void mcu_reset(void)
Request a software reset of the MCU.
Definition: mcu_arch.h:86
static void thdUsbStorage(void *arg)
Definition: usbStorage.c:96
Driver configuration structure.
Definition: usb_msd.h:123
void usbStorageStartPolling(void)
Definition: usbStorage.c:70
#define SDLOG_USB_VBUS_PORT
Definition: board.h:578
void pprz_terminate_autopilot_threads(void)
Terminate all autopilot threads Wait until proper stop.
Definition: main_chibios.c:178
void init_msd_driver(void *dbgThreadPtr, USBMassStorageConfig *msdConfig)
Definition: usb_msd.c:1126
static USBMassStorageConfig msdConfig
Definition: usbStorage.c:58
Arch independent mcu ( Micro Controller Unit ) utilities.
void usbStorageWaitForDeconnexion(void)
Definition: usbStorage.c:78
void msd_register_evt_connected(event_listener_t *elp, eventmask_t mask)
register connected event source in local event mask
Definition: usb_msd.c:1142
arch independent LED (Light Emitting Diodes) API
bool usbStorageIsItRunning(void)
Definition: usbStorage.c:145
#define LED_ON(i)
Definition: led_hw.h:51
void usbStorageStop(void)
Definition: usbStorage.c:86
static THD_WORKING_AREA(waThdUsbStorage, 1024)