1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-14 03:50:02 +03:00

MSC on-board flash: Persist RTC across reboot and use as file timestamp

Adds support to persist the RTC (if set) across the reboot if entering mass storage mode for on-board flash. The value is then used as the timestamp for the files exposed in the virtual FAT32 filesystem. The files will then have reasonable creation dates when copied to the host computer.

If the RTC is not set (or supported), then the default timestamp of 2018-01-01 will be used (unchanged from previous).

Included some improvements to the RTC functions and exposed the `tz_offsetMinutes` in the `timeConfig` PG. Support already existed for timezone offsets but the parameter wasn't exposed to the user and couldn't be set.

Move timezone offset up a layer as a parameter to systemResetToMsc()
Adds support for specifying a custom timezone offset from both the CLI and MSP calls to enter mass storage mode.

Added an option timezone offset minutes to the CLI `msc` command. If no parameter is specified then the default as specified by `timezone_offset_minutes` will be used. So to reboot into mass storage mode and force the file timestamps to be in UTC, use `msc 0`.

Added reboot message `MSP_REBOOT_MSC_UTC` to support rebooting into mass storage mode and forcing the timestamps to use UTC time (0 offset). The Configurator will need to be modified to use this message for operating systems that expect UTC times for FAT file systems (like Linux).
This commit is contained in:
Bruce Luckcuck 2018-12-01 10:07:22 -05:00
parent 1c50c317d6
commit b8085b170e
16 changed files with 138 additions and 14 deletions

View file

@ -30,6 +30,8 @@
#include "common/printf.h"
#include "common/time.h"
#include "drivers/persistent.h"
#include "pg/pg_ids.h"
#include "drivers/time.h"
@ -262,4 +264,31 @@ bool rtcSetDateTime(dateTime_t *dt)
return rtcSet(&t);
}
void rtcPersistWrite(int16_t offsetMinutes)
{
rtcTime_t workTime;
uint32_t highLongWord = 0;
uint32_t lowLongWord = 0;
if (rtcGet(&workTime)) {
workTime += (offsetMinutes * 60 * MILLIS_PER_SECOND);
highLongWord = (uint32_t)(workTime >> 32);
lowLongWord = (uint32_t)(workTime & 0xffffffff);
}
persistentObjectWrite(PERSISTENT_OBJECT_RTC_HIGH, highLongWord);
persistentObjectWrite(PERSISTENT_OBJECT_RTC_LOW, lowLongWord);
}
bool rtcPersistRead(rtcTime_t *t)
{
const uint32_t highLongWord = persistentObjectRead(PERSISTENT_OBJECT_RTC_HIGH);
const uint32_t lowLongWord = persistentObjectRead(PERSISTENT_OBJECT_RTC_LOW);
if ((highLongWord != 0) || (lowLongWord != 0)) {
*t = ((uint64_t)highLongWord << 32) + lowLongWord;
return true;
} else {
return false;
}
}
#endif

View file

@ -40,6 +40,9 @@ typedef uint32_t timeUs_t;
#define TIMEUS_MAX UINT32_MAX
#endif
#define TIMEZONE_OFFSET_MINUTES_MIN -780 // -13 hours
#define TIMEZONE_OFFSET_MINUTES_MAX 780 // +13 hours
static inline timeDelta_t cmpTimeUs(timeUs_t a, timeUs_t b) { return (timeDelta_t)(a - b); }
#define FORMATTED_DATE_TIME_BUFSIZE 30
@ -95,4 +98,6 @@ bool rtcSet(rtcTime_t *t);
bool rtcGetDateTime(dateTime_t *dt);
bool rtcSetDateTime(dateTime_t *dt);
void rtcPersistWrite(int16_t offsetMinutes);
bool rtcPersistRead(rtcTime_t *t);
#endif

View file

@ -30,6 +30,8 @@ typedef enum {
PERSISTENT_OBJECT_HSE_VALUE,
PERSISTENT_OBJECT_OVERCLOCK_LEVEL,
PERSISTENT_OBJECT_BOOTLOADER_REQUEST,
PERSISTENT_OBJECT_RTC_HIGH, // high 32 bits of rtcTime_t
PERSISTENT_OBJECT_RTC_LOW, // low 32 bits of rtcTime_t
PERSISTENT_OBJECT_COUNT,
} persistentObjectId_e;

View file

@ -31,4 +31,4 @@ bool mscCheckBoot(void);
uint8_t mscStart(void);
bool mscCheckButton(void);
void mscWaitForButton(void);
void systemResetToMsc(void);
void systemResetToMsc(int timezoneOffsetMinutes);

View file

@ -159,11 +159,18 @@ void mscWaitForButton(void)
}
}
void systemResetToMsc(void)
void systemResetToMsc(int timezoneOffsetMinutes)
{
*((uint32_t *)0x2001FFF0) = MSC_MAGIC;
__disable_irq();
// Persist the RTC across the reboot to use as the file timestamp
#ifdef USE_PERSISTENT_MSC_RTC
rtcPersistWrite(timezoneOffsetMinutes);
#else
UNUSED(timezoneOffsetMinutes);
#endif
NVIC_SystemReset();
}
#endif

View file

@ -164,11 +164,18 @@ void mscWaitForButton(void)
}
}
void systemResetToMsc(void)
void systemResetToMsc(int timezoneOffsetMinutes)
{
*((__IO uint32_t*) BKPSRAM_BASE + 16) = MSC_MAGIC;
__disable_irq();
// Persist the RTC across the reboot to use as the file timestamp
#ifdef USE_PERSISTENT_MSC_RTC
rtcPersistWrite(timezoneOffsetMinutes);
#else
UNUSED(timezoneOffsetMinutes);
#endif
NVIC_SystemReset();
}
#endif

View file

@ -58,6 +58,7 @@
#include "drivers/light_led.h"
#include "drivers/mco.h"
#include "drivers/nvic.h"
#include "drivers/persistent.h"
#include "drivers/pwm_esc_detect.h"
#include "drivers/pwm_output.h"
#include "drivers/rx/rx_pwm.h"
@ -89,6 +90,10 @@
#include "interface/cli.h"
#include "interface/msp.h"
#ifdef USE_PERSISTENT_MSC_RTC
#include "msc/usbd_storage.h"
#endif
#include "msp/msp_serial.h"
#include "pg/adc.h"
@ -428,6 +433,12 @@ void init(void)
}
#endif
#ifdef USE_PERSISTENT_MSC_RTC
// if we didn't enter MSC mode then clear the persistent RTC value
persistentObjectWrite(PERSISTENT_OBJECT_RTC_HIGH, 0);
persistentObjectWrite(PERSISTENT_OBJECT_RTC_LOW, 0);
#endif
#ifdef USE_I2C
i2cHardwareConfigure(i2cConfig(0));

View file

@ -4483,16 +4483,27 @@ static void cliDiff(char *cmdline)
#if defined(USE_USB_MSC)
static void cliMsc(char *cmdline)
{
UNUSED(cmdline);
if (mscCheckFilesystemReady()) {
#ifdef USE_RTC_TIME
int timezoneOffsetMinutes = timeConfig()->tz_offsetMinutes;
if (!isEmpty(cmdline)) {
timezoneOffsetMinutes = atoi(cmdline);
if ((timezoneOffsetMinutes < TIMEZONE_OFFSET_MINUTES_MIN) || (timezoneOffsetMinutes > TIMEZONE_OFFSET_MINUTES_MAX)) {
cliPrintErrorLinef("INVALID TIMEZONE OFFSET");
return;
}
}
#else
int timezoneOffsetMinutes = 0;
UNUSED(cmdline);
#endif
cliPrintHashLine("Restarting in mass storage mode");
cliPrint("\r\nRebooting");
bufWriterFlush(cliWriter);
waitForSerialPortToFinishTransmitting(cliPort);
stopPwmAllMotors();
systemResetToMsc();
systemResetToMsc(timezoneOffsetMinutes);
} else {
cliPrintHashLine("Storage not present or failed to initialize!");
}
@ -4598,7 +4609,11 @@ const clicmd_t cmdTable[] = {
#endif
CLI_COMMAND_DEF("motor", "get/set motor", "<index> [<value>]", cliMotor),
#ifdef USE_USB_MSC
#ifdef USE_RTC_TIME
CLI_COMMAND_DEF("msc", "switch into msc mode", "[<timezone offset minutes>]", cliMsc),
#else
CLI_COMMAND_DEF("msc", "switch into msc mode", NULL, cliMsc),
#endif
#endif
CLI_COMMAND_DEF("name", "name of craft", NULL, cliName),
#ifndef MINIMAL_CLI

View file

@ -138,6 +138,7 @@ enum {
MSP_REBOOT_FIRMWARE = 0,
MSP_REBOOT_BOOTLOADER,
MSP_REBOOT_MSC,
MSP_REBOOT_MSC_UTC,
MSP_REBOOT_COUNT,
};
@ -253,8 +254,14 @@ static void mspRebootFn(serialPort_t *serialPort)
break;
#if defined(USE_USB_MSC)
case MSP_REBOOT_MSC:
systemResetToMsc();
case MSP_REBOOT_MSC_UTC: {
#ifdef USE_RTC_TIME
const int16_t timezoneOffsetMinutes = (rebootMode == MSP_REBOOT_MSC) ? timeConfig()->tz_offsetMinutes : 0;
systemResetToMsc(timezoneOffsetMinutes);
#else
systemResetToMsc(0);
#endif
}
break;
#endif
default:

View file

@ -30,6 +30,7 @@
#include "cms/cms.h"
#include "common/utils.h"
#include "common/time.h"
#include "drivers/adc.h"
#include "drivers/bus_i2c.h"
@ -1266,6 +1267,11 @@ const clivalue_t valueTable[] = {
{ "spektrum_spi_mfg_id", VAR_UINT8 | MASTER_VALUE | MODE_ARRAY, .config.array.length = 4, PG_RX_SPEKTRUM_SPI_CONFIG, offsetof(spektrumConfig_t, mfgId) },
{ "spektrum_spi_num_channels", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, DSM_MAX_CHANNEL_COUNT }, PG_RX_SPEKTRUM_SPI_CONFIG, offsetof(spektrumConfig_t, numChannels) },
#endif
// PG_TIMECONFIG
#ifdef USE_RTC_TIME
{ "timezone_offset_minutes", VAR_INT16 | MASTER_VALUE, .config.minmax = { TIMEZONE_OFFSET_MINUTES_MIN, TIMEZONE_OFFSET_MINUTES_MAX }, PG_TIME_CONFIG, offsetof(timeConfig_t, tz_offsetMinutes) },
#endif
};
const uint16_t valueTableEntryCount = ARRAYLEN(valueTable);

View file

@ -22,14 +22,18 @@
* Author: jflyper@github.com
*/
#include "common/utils.h"
#include "common/printf.h"
#include "platform.h"
#include "emfat.h"
#include "emfat_file.h"
#include "common/printf.h"
#include "common/time.h"
#include "common/utils.h"
#include "io/flashfs.h"
#include "msc/usbd_storage.h"
#define FILESYSTEM_SIZE_MB 256
#define USE_EMFAT_AUTORUN
@ -269,6 +273,14 @@ static emfat_entry_t entries[EMFAT_MAX_ENTRY];
static char logNames[EMFAT_MAX_LOG_ENTRY][8+3];
emfat_t emfat;
static uint32_t cmaTime = CMA_TIME;
static void emfat_set_entry_cma(emfat_entry_t *entry)
{
entry->cma_time[0] = cmaTime;
entry->cma_time[1] = cmaTime;
entry->cma_time[2] = cmaTime;
}
static void emfat_add_log(emfat_entry_t *entry, int number, uint32_t offset, uint32_t size)
{
@ -278,10 +290,8 @@ static void emfat_add_log(emfat_entry_t *entry, int number, uint32_t offset, uin
entry->offset = offset;
entry->curr_size = size;
entry->max_size = entry->curr_size;
entry->cma_time[0] = CMA_TIME;
entry->cma_time[1] = CMA_TIME;
entry->cma_time[2] = CMA_TIME;
entry->readcb = bblog_read_proc;
emfat_set_entry_cma(entry);
}
static int emfat_find_log(emfat_entry_t *entry, int maxCount)
@ -327,8 +337,17 @@ void emfat_init_files(void)
emfat_entry_t *entry;
memset(entries, 0, sizeof(entries));
#ifdef USE_PERSISTENT_MSC_RTC
rtcTime_t mscRebootRtc;
if (rtcPersistRead(&mscRebootRtc)) {
const int32_t rtcSeconds = rtcTimeGetSeconds(&mscRebootRtc);
cmaTime = emfat_cma_time_from_unix((uint32_t)rtcSeconds);
}
#endif
for (size_t i = 0 ; i < PREDEFINED_ENTRY_COUNT ; i++) {
entries[i] = entriesPredefined[i];
emfat_set_entry_cma(&entries[i]);
}
// Detect and create entries for each individual log
@ -343,6 +362,7 @@ void emfat_init_files(void)
entry = &entries[entryIndex];
entry->curr_size = flashfsIdentifyStartOfFreeSpace();
entry->max_size = entry->curr_size;
emfat_set_entry_cma(entry);
++entryIndex;
}
@ -352,6 +372,7 @@ void emfat_init_files(void)
// used space is doubled because of the individual files plus the single complete file
entry->curr_size = (FILESYSTEM_SIZE_MB * 1024 * 1024) - (flashfsIdentifyStartOfFreeSpace() * 2);
entry->max_size = entry->curr_size;
emfat_set_entry_cma(entry);
emfat_init(&emfat, "BETAFLT", entries);
}

View file

@ -21,6 +21,9 @@
/*
* Author: jflyper (https://github.com/jflyper)
*/
#include "platform.h"
#include "common/time.h"
#ifdef USE_HAL_DRIVER
#include "usbd_msc.h"

View file

@ -27,6 +27,8 @@
#include "usbd_msc_core.h"
#endif
#include "common/time.h"
#ifdef USE_HAL_DRIVER
extern USBD_StorageTypeDef *USBD_STORAGE_fops;
#ifdef USE_SDCARD_SDIO

View file

@ -223,3 +223,7 @@
#define SPI_PREINIT_COUNT 16 // 2 x 8 (GYROx2, BARO, MAG, MAX, FLASHx2, RX)
#endif
#endif
#if (!defined(USE_FLASHFS) || !defined(USE_RTC_TIME) || !defined(USE_USB_MSC))
#undef USE_PERSISTENT_MSC_RTC
#endif

View file

@ -59,6 +59,7 @@
#define USE_ADC_INTERNAL
#define USE_USB_CDC_HID
#define USE_USB_MSC
#define USE_PERSISTENT_MSC_RTC
#if defined(STM32F40_41xxx) || defined(STM32F411xE)
#define USE_OVERCLOCK
@ -78,6 +79,7 @@
#define USE_ADC_INTERNAL
#define USE_USB_CDC_HID
#define USE_USB_MSC
#define USE_PERSISTENT_MSC_RTC
#define USE_MCO
#endif

View file

@ -37,6 +37,7 @@ extern "C" {
#include "common/time.h"
#include "drivers/max7456_symbols.h"
#include "drivers/persistent.h"
#include "drivers/serial.h"
#include "fc/config.h"
@ -1080,4 +1081,6 @@ extern "C" {
bool failsafeIsActive(void) { return false; }
bool gpsRescueIsConfigured(void) { return false; }
int8_t calculateThrottlePercent(void) { return 0; }
uint32_t persistentObjectRead(persistentObjectId_e) { return 0; }
void persistentObjectWrite(persistentObjectId_e, uint32_t) {}
}