mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-14 11:27:34 +08:00
Parameter reverse transition from LittleFS to block device
For the smooth backward transition from LittleFS to preserve parameters
This commit is contained in:
@@ -141,6 +141,18 @@ else
|
||||
param select $PARAM_FILE
|
||||
if ! param import
|
||||
then
|
||||
|
||||
echo "Try parameter reverse transition from LittleFS"
|
||||
if ! param transition
|
||||
then
|
||||
echo "ERROR [init] param transition"
|
||||
else
|
||||
echo "Successful param reverse transition. The system will reboot!"
|
||||
# Wait for UART to send messages
|
||||
usleep 200000
|
||||
reboot
|
||||
fi
|
||||
|
||||
echo "ERROR [init] param import failed"
|
||||
set STARTUP_TUNE 2 # tune 2 = ERROR_TUNE
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ CONFIG_FS_BINFS=y
|
||||
CONFIG_FS_CROMFS=y
|
||||
CONFIG_FS_FAT=y
|
||||
CONFIG_FS_FATTIME=y
|
||||
CONFIG_FS_LITTLEFS=y
|
||||
CONFIG_FS_PROCFS=y
|
||||
CONFIG_FS_PROCFS_INCLUDE_PROGMEM=y
|
||||
CONFIG_FS_PROCFS_MAX_TASKS=64
|
||||
|
||||
@@ -75,6 +75,25 @@ __EXPORT int px4_mtd_get_geometry(const mtd_instance_s *instance, unsigned long
|
||||
*/
|
||||
__EXPORT ssize_t px4_mtd_get_partition_size(const mtd_instance_s *instance, const char *partname);
|
||||
|
||||
/*
|
||||
Helper function for back transition from LittleFS.
|
||||
It will unmount MTD for parameters with LittleFS and mount Block Device
|
||||
*/
|
||||
__EXPORT int px4_mtd_unmount_littlefs_mount_block_device(void);
|
||||
|
||||
/*
|
||||
Helper function for back transition from LittleFS.
|
||||
It will unmount MTD for parameters with Block Device and mount LittleFS with auto formatting
|
||||
*/
|
||||
__EXPORT int px4_mtd_unmount_block_device_mount_littlefs(void);
|
||||
|
||||
/*
|
||||
Helper function for back transition from LittleFS.
|
||||
It will write 0xFF to all RAMTRON to remove LittleFS records
|
||||
*/
|
||||
__EXPORT int px4_mtd_erase_littlefs_records(void);
|
||||
|
||||
|
||||
FAR struct mtd_dev_s *px4_at24c_initialize(FAR struct i2c_master_s *dev,
|
||||
uint8_t address);
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
#include <nuttx/drivers/drivers.h>
|
||||
#include <nuttx/spi/spi.h>
|
||||
#include <nuttx/mtd/mtd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
extern "C" {
|
||||
struct mtd_dev_s *ramtron_initialize(FAR struct spi_dev_s *dev);
|
||||
@@ -66,6 +67,9 @@ extern "C" {
|
||||
static int num_instances = 0;
|
||||
static mtd_instance_s *instances = nullptr;
|
||||
|
||||
static uint8_t param_instance = 0;
|
||||
static uint8_t param_part = 0;
|
||||
static uint8_t param_block = 0;
|
||||
|
||||
static int ramtron_attach(mtd_instance_s &instance)
|
||||
{
|
||||
@@ -399,6 +403,12 @@ memoryout:
|
||||
goto errout;
|
||||
}
|
||||
|
||||
if (instances[i].partition_types[part] == MTD_PARAMETERS) {
|
||||
param_instance = i;
|
||||
param_part = part;
|
||||
param_block = total_blocks;
|
||||
}
|
||||
|
||||
total_blocks++;
|
||||
|
||||
/* Now create a character device on the block device */
|
||||
@@ -470,3 +480,96 @@ __EXPORT int px4_mtd_query(const char *sub, const char *val, const char **get)
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
int px4_mtd_unmount_littlefs_mount_block_device(void)
|
||||
{
|
||||
char blockname[32];
|
||||
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", param_block);
|
||||
|
||||
nx_umount2(instances[param_instance].partition_names[param_part], 0);
|
||||
|
||||
int ret = unregister_mtddriver(blockname);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("unregister_mtddriver fail: %d", ret);
|
||||
|
||||
} else {
|
||||
ret = ftl_initialize(0, instances[0].part_dev[0]);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("ftl_initialize failed: %d", ret);
|
||||
|
||||
} else {
|
||||
ret = bchdev_register(blockname, instances[param_instance].partition_names[param_part], false);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("bchdev_register failed: %d", ret);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int px4_mtd_unmount_block_device_mount_littlefs(void)
|
||||
{
|
||||
char blockname[32];
|
||||
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", param_block);
|
||||
|
||||
int ret = bchdev_unregister(instances[param_instance].partition_names[param_part]);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("bchdev_unregister %s failed: %d", instances[param_instance].partition_names[param_part], ret);
|
||||
|
||||
} else {
|
||||
ret = unregister_blockdriver(blockname);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("unregister_blockdriver %s failed: %d", blockname, ret);
|
||||
|
||||
} else {
|
||||
ret = register_mtddriver(blockname, instances[param_instance].part_dev[param_part], 0755, nullptr);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("register_mtddriver %s failed: %d", blockname, ret);
|
||||
|
||||
} else {
|
||||
ret = nx_mount(blockname, instances[param_instance].partition_names[param_part], "littlefs", 0, "");
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("nx_mount %s failed: %d", instances[param_instance].partition_names[param_part], ret);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int px4_mtd_erase_littlefs_records(void)
|
||||
{
|
||||
uint8_t v[64];
|
||||
memset(v, 0xFF, sizeof(v));
|
||||
|
||||
for (uint8_t i = 0; i < instances[param_instance].n_partitions_current; i++) {
|
||||
|
||||
uint32_t count = 0;
|
||||
printf("Erasing %s\n", instances[param_instance].partition_names[i]);
|
||||
int fd = open(instances[param_instance].partition_names[i], O_WRONLY);
|
||||
|
||||
if (fd == -1) {
|
||||
PX4_ERR("Failed to open partition");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (write(fd, v, sizeof(v)) == sizeof(v)) {
|
||||
count += sizeof(v);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <px4_platform_common/posix.h>
|
||||
#include <px4_platform_common/px4_mtd.h>
|
||||
|
||||
#include <float.h>
|
||||
#include <errno.h>
|
||||
@@ -85,6 +86,7 @@ static int do_save(const char *param_file_name);
|
||||
static int do_save_default();
|
||||
static int do_dump(const char *param_file_name);
|
||||
static int do_load(const char *param_file_name);
|
||||
static int do_reverse_transition();
|
||||
static int do_import(const char *param_file_name = nullptr);
|
||||
static int do_show(const char *search_string, bool only_changed);
|
||||
static int do_show_for_airframe();
|
||||
@@ -230,6 +232,10 @@ param_main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "transition")) {
|
||||
return do_reverse_transition();
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "import")) {
|
||||
if (argc >= 3) {
|
||||
return do_import(argv[2]);
|
||||
@@ -514,6 +520,46 @@ do_load(const char *param_file_name)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
do_reverse_transition()
|
||||
{
|
||||
#ifdef __PX4_NUTTX
|
||||
|
||||
int ret_val_1 = px4_mtd_unmount_block_device_mount_littlefs();
|
||||
|
||||
if (ret_val_1 < 0) {
|
||||
PX4_ERR("Transition from Blockdriver to LittleFS failed");
|
||||
} else {
|
||||
ret_val_1 = do_import("/fs/mtd_params/parameters.bson");
|
||||
|
||||
if (ret_val_1 < 0) {
|
||||
PX4_ERR("Import from blockdriver");
|
||||
}
|
||||
}
|
||||
|
||||
int ret_val_2 = px4_mtd_unmount_littlefs_mount_block_device();
|
||||
|
||||
if (ret_val_2 < 0) {
|
||||
PX4_ERR("Transition from LittleFS to Blockdriver");
|
||||
} else {
|
||||
|
||||
if(ret_val_1 >= 0) {
|
||||
if (px4_mtd_erase_littlefs_records() >= 0) {
|
||||
ret_val_2 = param_export(param_get_default_file(), nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ret_val_1 < 0 || ret_val_2 < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
do_import(const char *param_file_name)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user