Skip to content

Commit c257c30

Browse files
committed
kakuteh7mini: add W25Q128 flash logging support
Enable onboard SPI NOR flash (W25Q128) for flight logging using LittleFS. Tested on Kakute H7 Mini v1.5. Signed-off-by: Lukanov Aleksei <[email protected]>
1 parent e72beb8 commit c257c30

9 files changed

Lines changed: 160 additions & 22 deletions

File tree

boards/holybro/kakuteh7mini/default.px4board

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CONFIG_BOARD_SERIAL_GPS1="/dev/ttyS3"
44
CONFIG_BOARD_SERIAL_TEL1="/dev/ttyS0"
55
CONFIG_BOARD_SERIAL_TEL2="/dev/ttyS1"
66
CONFIG_BOARD_SERIAL_RC="/dev/ttyS4"
7+
CONFIG_BOARD_ROOT_PATH="/fs/microsd"
78
CONFIG_DRIVERS_ADC_BOARD_ADC=y
89
CONFIG_COMMON_BAROMETERS=y
910
CONFIG_DRIVERS_BATT_SMBUS=y
@@ -91,3 +92,6 @@ CONFIG_SYSTEMCMDS_UORB=y
9192
CONFIG_SYSTEMCMDS_USB_CONNECTED=y
9293
CONFIG_SYSTEMCMDS_VER=y
9394
CONFIG_SYSTEMCMDS_WORK_QUEUE=y
95+
CONFIG_SYSTEMCMDS_MTD=y
96+
CONFIG_LOGGER_STACK_SIZE=4100
97+
CONFIG_LOGGER_WRITER_STACK_SIZE=4096

boards/holybro/kakuteh7mini/init/rc.board_extras

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,16 @@ fi
1010

1111
# DShot telemetry is always on UART7
1212
dshot telemetry -d /dev/ttyS5
13+
14+
# Create logger config for flash logging (W25Q128)
15+
if [ ! -f /fs/microsd/etc/logging/logger_topics.txt ]; then
16+
mkdir -p /fs/microsd/etc/logging
17+
echo "vehicle_attitude 500" > /fs/microsd/etc/logging/logger_topics.txt
18+
echo "vehicle_local_position 1000" >> /fs/microsd/etc/logging/logger_topics.txt
19+
echo "vehicle_global_position 1000" >> /fs/microsd/etc/logging/logger_topics.txt
20+
echo "battery_status 2000" >> /fs/microsd/etc/logging/logger_topics.txt
21+
echo "vehicle_status 1000" >> /fs/microsd/etc/logging/logger_topics.txt
22+
echo "failsafe_flags 1000" >> /fs/microsd/etc/logging/logger_topics.txt
23+
echo "vehicle_land_detected 1000" >> /fs/microsd/etc/logging/logger_topics.txt
24+
echo "actuator_outputs 500" >> /fs/microsd/etc/logging/logger_topics.txt
25+
fi

boards/holybro/kakuteh7mini/nuttx-config/nsh/defconfig

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,19 @@ CONFIG_DEV_FIFO_SIZE=0
9090
CONFIG_DEV_PIPE_MAXSIZE=1024
9191
CONFIG_DEV_PIPE_SIZE=70
9292
CONFIG_EXPERIMENTAL=y
93-
CONFIG_FAT_DMAMEMORY=y
94-
CONFIG_FAT_LCNAMES=y
95-
CONFIG_FAT_LFN=y
96-
CONFIG_FAT_LFN_ALIAS_HASH=y
9793
CONFIG_FDCLONE_STDIO=y
9894
CONFIG_FS_BINFS=y
9995
CONFIG_FS_CROMFS=y
100-
CONFIG_FS_FAT=y
101-
CONFIG_FS_FATTIME=y
10296
CONFIG_FS_PROCFS=y
10397
CONFIG_FS_PROCFS_INCLUDE_PROGMEM=y
10498
CONFIG_FS_PROCFS_MAX_TASKS=64
10599
CONFIG_FS_PROCFS_REGISTER=y
106100
CONFIG_FS_ROMFS=y
101+
CONFIG_FS_LITTLEFS=y
102+
CONFIG_FS_LITTLEFS_BLOCK_CYCLE=500
103+
CONFIG_FS_LITTLEFS_BLOCK_SIZE_FACTOR=16
104+
CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE=128
105+
CONFIG_FS_LITTLEFS_PROGRAM_SIZE_FACTOR=1
107106
CONFIG_GRAN=y
108107
CONFIG_GRAN_INTR=y
109108
CONFIG_HAVE_CXX=y
@@ -119,14 +118,12 @@ CONFIG_LIBC_MAX_EXITFUNS=1
119118
CONFIG_LIBC_STRERROR=y
120119
CONFIG_MEMSET_64BIT=y
121120
CONFIG_MEMSET_OPTSPEED=y
122-
CONFIG_MMCSD=y
123-
CONFIG_MMCSD_SDIO=y
124121
CONFIG_MM_REGIONS=4
125122
CONFIG_MTD=y
126123
CONFIG_MTD_BYTE_WRITE=y
127124
CONFIG_MTD_PARTITION=y
128125
CONFIG_MTD_PROGMEM=y
129-
CONFIG_MTD_RAMTRON=y
126+
CONFIG_MTD_W25=y
130127
CONFIG_NAME_MAX=40
131128
CONFIG_NSH_ARCHINIT=y
132129
CONFIG_NSH_ARGCAT=y
@@ -245,3 +242,4 @@ CONFIG_USBDEV_BUSPOWERED=y
245242
CONFIG_USBDEV_MAXPOWER=500
246243
CONFIG_USEC_PER_TICK=1000
247244
CONFIG_WATCHDOG=y
245+
CONFIG_BCH=y

boards/holybro/kakuteh7mini/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ else()
4848
i2c.cpp
4949
init.c
5050
led.c
51+
mtd.cpp
5152
spi.cpp
5253
timer_config.cpp
5354
usb.c

boards/holybro/kakuteh7mini/src/init.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <string.h>
5353
#include <debug.h>
5454
#include <errno.h>
55+
#include <sys/stat.h>
5556
#include <syslog.h>
5657

5758
#include <nuttx/config.h>
@@ -231,16 +232,7 @@ __EXPORT int board_app_initialize(uintptr_t arg)
231232
led_on(LED_RED);
232233
}
233234

234-
// MARK: this will *not* work as the minis have a W25N NAND flash chip
235-
/* Get the SPI port for the microSD slot */
236-
struct spi_dev_s *spi_dev = stm32_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO);
237235

238-
if (!spi_dev) {
239-
syslog(LOG_ERR, "[boot] FAILED to initialize SPI port %d\n", CONFIG_NSH_MMCSDSPIPORTNO);
240-
led_on(LED_BLUE);
241-
}
242-
243-
up_udelay(20);
244236

245237
#if defined(FLASH_BASED_PARAMS)
246238
static sector_descriptor_t params_sector_map[] = {
@@ -259,8 +251,18 @@ __EXPORT int board_app_initialize(uintptr_t arg)
259251
#endif
260252

261253
/* Configure the HW based on the manifest */
262-
263254
px4_platform_configure();
264255

256+
/* Mount LittleFS on W25Q128 flash for logging */
257+
mkdir("/fs/microsd", 0777);
258+
259+
if (nx_mount("/dev/mtdblock0", "/fs/microsd", "littlefs", 0, "autoformat") != 0) {
260+
syslog(LOG_ERR, "[boot] FAILED to mount littlefs on mtdblock0\n");
261+
led_on(LED_BLUE);
262+
263+
} else {
264+
syslog(LOG_INFO, "[boot] LittleFS mounted on /fs/microsd\n");
265+
}
266+
265267
return OK;
266268
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (C) 2020 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
#include <nuttx/spi/spi.h>
35+
#include <px4_platform_common/px4_manifest.h>
36+
37+
// W25Q128 on SPI1: 16MB (128Mbit), 4KB sectors, 64KB blocks
38+
// For parameters we use a small portion - 128KB (32 x 4KB blocks)
39+
static const px4_mft_device_t spi1 = {
40+
.bus_type = px4_mft_device_t::SPI,
41+
.devid = SPIDEV_FLASH(0)
42+
};
43+
44+
static const px4_mtd_entry_t w25q128_flash = {
45+
.device = &spi1,
46+
.npart = 1,
47+
.partd = {
48+
{
49+
.type = MTD_WAYPOINTS, // Use for data storage
50+
.path = "/fs/mtd_data",
51+
.nblocks = 65536 // 65536 x 256 bytes = 16MB (full flash)
52+
}
53+
},
54+
};
55+
56+
static const px4_mtd_manifest_t board_mtd_config = {
57+
.nconfigs = 1,
58+
.entries = {
59+
&w25q128_flash
60+
}
61+
};
62+
63+
static const px4_mft_entry_s mtd_mft = {
64+
.type = MTD,
65+
.pmft = (void *) &board_mtd_config,
66+
};
67+
68+
static const px4_mft_s mft = {
69+
.nmft = 1,
70+
.mfts = {
71+
&mtd_mft
72+
}
73+
};
74+
75+
const px4_mft_s *board_get_manifest(void)
76+
{
77+
return &mft;
78+
}

boards/holybro/kakuteh7mini/src/spi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
constexpr px4_spi_bus_t px4_spi_buses[SPI_BUS_MAX_BUS_ITEMS] = {
3939
initSPIBus(SPI::Bus::SPI1, {
40-
initSPIDevice(SPIDEV_MMCSD(0), SPI::CS{GPIO::PortA, GPIO::Pin4})
40+
initSPIDevice(SPIDEV_FLASH(0), SPI::CS{GPIO::PortA, GPIO::Pin4})
4141
}),
4242
initSPIBus(SPI::Bus::SPI2, {
4343
initSPIDevice(DRV_OSD_DEVTYPE_ATXXXX, SPI::CS{GPIO::PortB, GPIO::Pin12}),

platforms/nuttx/src/px4/common/px4_mtd.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
extern "C" {
6262
struct mtd_dev_s *ramtron_initialize(FAR struct spi_dev_s *dev);
63+
struct mtd_dev_s *w25_initialize(FAR struct spi_dev_s *dev);
6364
struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s *mtd,
6465
off_t firstblock, off_t nblocks);
6566
}
@@ -132,6 +133,41 @@ static int ramtron_attach(mtd_instance_s &instance)
132133
}
133134

134135

136+
static int w25_attach(mtd_instance_s &instance)
137+
{
138+
#if !defined(CONFIG_MTD_W25)
139+
PX4_ERR("Misconfiguration CONFIG_MTD_W25 not set");
140+
return -ENXIO;
141+
#else
142+
/* W25 supports up to 50MHz, start at 20MHz */
143+
unsigned long spi_speed_hz = 20'000'000;
144+
145+
struct spi_dev_s *spi = px4_spibus_initialize(px4_find_spi_bus(instance.devid));
146+
147+
if (spi == nullptr) {
148+
PX4_ERR("failed to locate spi bus");
149+
return -ENXIO;
150+
}
151+
152+
SPI_LOCK(spi, true);
153+
SPI_SETFREQUENCY(spi, spi_speed_hz);
154+
SPI_SETBITS(spi, 8);
155+
SPI_SETMODE(spi, SPIDEV_MODE0); /* W25 uses MODE0 */
156+
SPI_SELECT(spi, instance.devid, false);
157+
SPI_LOCK(spi, false);
158+
159+
instance.mtd_dev = w25_initialize(spi);
160+
161+
if (instance.mtd_dev == nullptr) {
162+
PX4_ERR("failed to initialize W25 mtd driver");
163+
return -EIO;
164+
}
165+
166+
return 0;
167+
#endif
168+
}
169+
170+
135171
static int at24xxx_attach(mtd_instance_s &instance)
136172
{
137173
#if !defined(PX4_I2C_BUS_MTD)
@@ -350,7 +386,13 @@ int px4_mtd_config(const px4_mtd_manifest_t *mft_mtd)
350386
rv = at24xxx_attach(*instances[i]);
351387

352388
} else if (mtd_list->entries[num_entry]->device->bus_type == px4_mft_device_t::SPI) {
353-
rv = ramtron_attach(*instances[i]);
389+
/* Try W25 first, then RAMTRON */
390+
rv = w25_attach(*instances[i]);
391+
392+
if (rv != 0) {
393+
rv = ramtron_attach(*instances[i]);
394+
}
395+
354396
#if defined(HAS_FLEXSPI)
355397

356398
} else if (mtd_list->entries[num_entry]->device->bus_type == px4_mft_device_t::FLEXSPI) {

src/modules/logger/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ int check_free_space(const char *log_root_dir, int32_t max_log_dirs_to_keep, orb
233233

234234

235235
/* use a threshold of 50 MiB: if below, do not start logging */
236-
if (statfs_buf.f_bavail < (px4_statfs_buf_f_bavail_t)(50 * 1024 * 1024 / statfs_buf.f_bsize)) {
236+
if (statfs_buf.f_bavail < (px4_statfs_buf_f_bavail_t)(10 * 1024 * 1024 / statfs_buf.f_bsize)) {
237237
mavlink_log_critical(&mavlink_log_pub,
238238
"[logger] Not logging; SD almost full: %u MiB\t",
239239
(unsigned int)(statfs_buf.f_bavail * statfs_buf.f_bsize / 1024U / 1024U));

0 commit comments

Comments
 (0)