mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-09 18:01:36 +08:00
boards: use littlefs for parameter storage
Co-authored-by: Igor Mišić <igy1000mb@gmail.com> Co-authored-by: Daniel Agar <daniel@agar.ca>
This commit is contained in:
co-authored by
Igor Mišić
parent
be56f74c1d
commit
7667b3a782
@@ -77,6 +77,24 @@ __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 transition to 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 transition to LittleFS.
|
||||
It will unmount MTD for parameters with Block Device and mount LittleFS with force formatting
|
||||
*/
|
||||
__EXPORT int px4_mtd_unmount_block_device_mount_littlefs(void);
|
||||
|
||||
/*
|
||||
Helper function for transition to LittleFS.
|
||||
It will force formatting to LittleFS
|
||||
*/
|
||||
__EXPORT int px4_mtd_forceformat_littlefs(void);
|
||||
|
||||
int px4_at24c_initialize(FAR struct i2c_master_s *dev,
|
||||
uint8_t address, FAR struct mtd_dev_s **mtd_dev);
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ static constexpr wq_config_t ttyS9{"wq:ttyS9", 1728, -30};
|
||||
static constexpr wq_config_t ttyACM0{"wq:ttyACM0", 1728, -31};
|
||||
static constexpr wq_config_t ttyUnknown{"wq:ttyUnknown", 1728, -32};
|
||||
|
||||
static constexpr wq_config_t lp_default{"wq:lp_default", 1920, -50};
|
||||
static constexpr wq_config_t lp_default{"wq:lp_default", 2048, -50};
|
||||
|
||||
static constexpr wq_config_t test1{"wq:test1", 2000, 0};
|
||||
static constexpr wq_config_t test2{"wq:test2", 2000, 0};
|
||||
|
||||
@@ -76,9 +76,12 @@ __EXPORT int px4_mft_configure(const px4_mft_s *mft)
|
||||
if (mft != nullptr) {
|
||||
for (uint32_t m = 0; m < mft->nmft; m++) {
|
||||
switch (mft->mfts[m]->type) {
|
||||
#if defined(CONFIG_MTD)
|
||||
|
||||
case MTD:
|
||||
px4_mtd_config(static_cast<const px4_mtd_manifest_t *>(mft->mfts[m]->pmft));
|
||||
break;
|
||||
#endif // CONFIG_MTD
|
||||
|
||||
case MFT:
|
||||
default:
|
||||
@@ -99,9 +102,12 @@ __EXPORT int px4_mft_query(const px4_mft_s *mft, px4_manifest_types_e type,
|
||||
for (uint32_t m = 0; m < mft->nmft; m++) {
|
||||
if (mft->mfts[m]->type == type)
|
||||
switch (type) {
|
||||
#if defined(CONFIG_MTD)
|
||||
|
||||
case MTD:
|
||||
return px4_mtd_query(sub, val, nullptr);
|
||||
break;
|
||||
#endif // CONFIG_MTD
|
||||
|
||||
case MFT:
|
||||
default:
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
* @author David Sidrane <david.sidrane@nscdg.com>
|
||||
*/
|
||||
|
||||
#if defined(CONFIG_MTD)
|
||||
|
||||
#ifndef MODULE_NAME
|
||||
#define MODULE_NAME "PX4_MTD"
|
||||
#endif
|
||||
@@ -57,6 +59,7 @@
|
||||
#include <nuttx/drivers/drivers.h>
|
||||
#include <nuttx/spi/spi.h>
|
||||
#include <nuttx/mtd/mtd.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
extern "C" {
|
||||
struct mtd_dev_s *ramtron_initialize(FAR struct spi_dev_s *dev);
|
||||
@@ -67,6 +70,9 @@ static int num_instances = 0;
|
||||
static int total_blocks = 0;
|
||||
static mtd_instance_s *instances[MAX_MTD_INSTANCES] = {};
|
||||
|
||||
static int8_t param_instance = -1;
|
||||
static int8_t param_part = -1;
|
||||
static int8_t param_block = -1;
|
||||
|
||||
static int ramtron_attach(mtd_instance_s &instance)
|
||||
{
|
||||
@@ -118,15 +124,6 @@ static int ramtron_attach(mtd_instance_s &instance)
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
int ret = instance.mtd_dev->ioctl(instance.mtd_dev, MTDIOC_SETSPEED, (unsigned long)spi_speed_mhz * 1000 * 1000);
|
||||
|
||||
if (ret != OK) {
|
||||
// FIXME: From the previous warning call, it looked like this should have been fatal error instead. Tried
|
||||
// that but setting the bus speed does fail all the time. Which was then exiting and the board would
|
||||
// not run correctly. So changed to PX4_WARN.
|
||||
PX4_WARN("failed to set bus speed");
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
@@ -395,27 +392,54 @@ memoryout:
|
||||
}
|
||||
|
||||
/* Initialize to provide an FTL block driver on the MTD FLASH interface */
|
||||
|
||||
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", total_blocks);
|
||||
|
||||
rv = ftl_initialize(total_blocks, instances[i]->part_dev[part]);
|
||||
printf("blockname: %s, type: %d, name: %s\n", blockname, instances[i]->partition_types[part],
|
||||
instances[i]->partition_names[part]);
|
||||
|
||||
if (rv < 0) {
|
||||
PX4_ERR("ftl_initialize %s failed: %d", blockname, rv);
|
||||
goto errout;
|
||||
if (instances[i]->partition_types[part] == MTD_PARAMETERS) {
|
||||
|
||||
param_instance = i;
|
||||
param_part = part;
|
||||
param_block = total_blocks;
|
||||
|
||||
rv = register_mtddriver(blockname, instances[i]->part_dev[part], 0755, nullptr);
|
||||
|
||||
if (rv < 0) {
|
||||
PX4_ERR("register_mtddriver %s failed: %d", blockname, rv);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
// Now create a character device on the block device
|
||||
//TODO: after the transition period return "autoformat"
|
||||
rv = nx_mount(blockname, instances[i]->partition_names[part], "littlefs", 0, "");
|
||||
|
||||
printf("nx_mount: blockname: %s partition: %s\n", blockname, instances[i]->partition_names[part]);
|
||||
|
||||
if (rv < 0) {
|
||||
PX4_ERR("nx_mount %s failed: %d", instances[i]->partition_names[part], rv);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
} else {
|
||||
rv = ftl_initialize(total_blocks, instances[i]->part_dev[part]);
|
||||
|
||||
if (rv < 0) {
|
||||
PX4_ERR("ftl_initialize %s failed: %d", blockname, rv);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Now create a character device on the block device */
|
||||
rv = bchdev_register(blockname, instances[i]->partition_names[part], false);
|
||||
|
||||
if (rv < 0) {
|
||||
PX4_ERR("bchdev_register %s failed: %d", instances[i]->partition_names[part], rv);
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
total_blocks++;
|
||||
|
||||
/* Now create a character device on the block device */
|
||||
|
||||
rv = bchdev_register(blockname, instances[i]->partition_names[part], false);
|
||||
|
||||
if (rv < 0) {
|
||||
PX4_ERR("bchdev_register %s failed: %d", instances[i]->partition_names[part], rv);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
instances[i]->n_partitions_current++;
|
||||
}
|
||||
|
||||
@@ -474,3 +498,97 @@ __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)
|
||||
{
|
||||
if ((param_instance == -1) || (param_part == -1) || (param_block == -1)) {
|
||||
PX4_ERR("MTD_PARAMETERS never initialized");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char blockname[32];
|
||||
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", param_block);
|
||||
|
||||
// in case LittleFS is mounted, unmount it
|
||||
nx_umount2(instances[param_instance]->partition_names[param_part], 0);
|
||||
unregister_mtddriver(blockname);
|
||||
|
||||
int ret = ftl_initialize(0, instances[param_instance]->part_dev[param_part]);
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("ftl_initialize failed with error %d, param_block %d, param_instance %d, param_part %d, block_counts %d",
|
||||
ret, param_block, param_instance, param_part, *instances[param_instance]->partition_block_counts);
|
||||
|
||||
} 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)
|
||||
{
|
||||
if ((param_instance == -1) || (param_part == -1) || (param_block == -1)) {
|
||||
PX4_ERR("MTD_PARAMETERS never initialized");
|
||||
return -1;
|
||||
}
|
||||
|
||||
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 = px4_mtd_forceformat_littlefs();
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int px4_mtd_forceformat_littlefs(void)
|
||||
{
|
||||
if ((param_instance == -1) || (param_part == -1) || (param_block == -1)) {
|
||||
PX4_ERR("MTD_PARAMETERS never initialized");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char blockname[32];
|
||||
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", param_block);
|
||||
|
||||
// in case bchdev is register
|
||||
bchdev_unregister(instances[param_instance]->partition_names[param_part]);
|
||||
unregister_blockdriver(blockname);
|
||||
|
||||
int 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, "forceformat");
|
||||
|
||||
if (ret < 0) {
|
||||
PX4_ERR("nx_mount %s failed: %d", instances[param_instance]->partition_names[param_part], ret);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif // CONFIG_MTD
|
||||
|
||||
Reference in New Issue
Block a user