From 95323e705e012492fbda97d1e7030b02205e1cc3 Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:28:14 -0600 Subject: [PATCH] [BACKPORT 1.18] feat(boards/ark_fpv): add the LSM6DSV32X revision (#29375) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(drivers/imu/st/lsm6dsv): LSM6DSV 80X and 320X (#27625) * drivers: LSM6DSV 80X and 320X * fix(drivers/lsm6dsv): flip y/z axes to match PX4 driver convention The ST sensor frame is right handed with z up. Flip y and z in the driver so it publishes x forward, y right, z down like the InvenSense drivers, and board rotations only describe physical mounting. Update FMU-v6c accordingly: -R 26 (PITCH_180_YAW_90) becomes -R 6 (YAW_270), now matching the ICM42688P sharing the same footprint. Signed-off-by: alexklimaj * fix(drivers/lsm6dsv): don't flush FIFO on high-g full-scale change Flushing discarded batched gyro and low-g samples on every range step, causing a data gap at exactly the moments the high-g channel matters. Only the high-g channel's scale changes, so instead skip publishing and clip-evaluating high-g samples for one read cycle while the words captured at the previous full-scale drain naturally. Signed-off-by: alexklimaj * fix(drivers/lsm6dsv): publish high-g samples captured before a full-scale change Instead of skipping the high-g channel for a cycle after a range step, normalize samples captured at the previous full-scale to the current one (counts scaled by the exact sensitivity ratio) and publish them. Samples are classified by reconstructed capture time since the batch drained after a change mixes old- and new-scale words. This keeps the high-g data flowing through escalations, which happen mid-impact when that data matters most. Signed-off-by: alexklimaj * fix(drivers/lsm6dsv): retry WHO_AM_I read in probe The first SPI transaction after power-up can return garbage (reads 0xFF) while the device's shared I2C/I3C/SPI interface latches onto SPI mode on the first CS falling edge. The single-shot probe read made startup fail until bus traffic preceded it; retry up to 3 times like the InvenSense drivers do. Signed-off-by: alexklimaj * fix(drivers/lsm6dsv): set full-scale before enabling the ODRs Both datasheets footnote CTRL6 that FS_G must be set while the gyro is in power-down, and the reset default FS_G=000 is reserved on the 80X, 320X and DSK320X. Configure() writes _register_cfg in array order, and that order put CTRL1/CTRL2 ahead of CTRL6/CTRL8 — so the gyro powered up at full ODR on a reserved full-scale, and FS_G was then changed while it was running. Move every full-scale, filter and FIFO register ahead of CTRL1/CTRL2, which set the ODRs and are what actually power the sensors up. * fix(drivers/lsm6dsv): drain the FIFO in a single burst read FIFORead() issued one 8-byte SPI transaction per 7-byte FIFO word. At the 7.68 kHz the high-g variants run, that is ~23k transactions/s, and each one pays a bus lock, a CS toggle and a full SPI reconfigure — roughly 0.5-1 ms of bus time per 2.5 ms drain. AN5763 / AN6119 section 9.8 document reading DIFF_FIFO words as a single (N * 7)-byte operation: with IF_INC set the address rounds from FIFO_DATA_OUT_Z_H back to FIFO_DATA_OUT_TAG at every word boundary. Read the whole drain into one buffer and dispatch by tag out of it, which is also the shape every InvenSense driver in the tree already uses. Also correct the FIFO::DEPTH comment: 512 is the ceiling of the DIFF_FIFO counter, not the buffer size (1.5 KB, ~219 uncompressed words). * fix(drivers/lsm6dsv): pin the high-g channel at its top full-scale The high-g full-scale escalated 32 -> 64 -> 80 g on high-g clipping and de-escalated after 2 s quiet, which in turn needed a rescale path to normalize samples still batched at the previous sensitivity. None of it earns its keep. The high-g channel is only ever published while the low-g channel clips, i.e. above 16 g. All the ladder buys there is resolution — 0.976 vs 3.904 mg/LSB, 0.0096 vs 0.038 m/s² — while the fallback engages with the high-g channel's ±1.5 g typ zero-g offset uncalibrated, a 15 m/s² bias step. And it climbs one step per FIFO drain (2.5 ms at the default IMU_GYRO_RATEMAX), so it needs 5-7.5 ms to reach ±80 g and lands after an impact's peak has passed — precisely the sample it exists to capture. Pin each variant at its top range instead: ±80 g on the 80X, ±320 g on the 320X. The first peak is caught unclipped, and ManageHighGFullScale(), ApplyHighGFullScale(), RescaleCount(), the stale-sample normalization and the high-g clip evaluation all go with it. Two latent bugs go away rather than needing fixes. SampleClips()'s int16 threshold (~23070 counts) could never fire at ±80 g, which saturates around 20492 (3.904 mg/LSB), so sustained >64 g would have oscillated 64<->80 g; it now only sees the low-g channel, whose ±16 g / 0.488 mg/LSB scaling does reach the rail. And escalation could drive an 80X started with -T 320 into FS codes 011/100, which are reserved on that part — nothing writes them now. * fix(drivers/lsm6dsv): drop unused ACCEL_ODR_HIGHG The high-g variants' accel ODR is never referenced by name — CTRL1, CTRL2 and the FIFO BDR all take the register code, and the sample timing is derived from GYRO_ODR_HIGHG. * fix(drivers/lsm6dsv): publish the high-g channel directly on the 80X / 320X The accelerometer channel was chosen per FIFO batch: publish low-g normally, switch to high-g for any batch where a low-g sample passed a clip threshold. A sensor_accel_fifo message carries a single scale factor, so that meant restating the scale of samples that had already been taken, and the decision had no hysteresis: sustained vibration near the threshold flips the scale batch to batch at the full publish rate, stepping the high-g channel's uncalibrated +/-1.5 g typ zero-g offset in and out of the estimator each time. On a part picked for its high-g range the low-g channel's finer resolution is not worth any of that, so publish the high-g channel and nothing else, with the scale fixed in Configure() for the lifetime of the driver — the same fixed-scale approach the ICM45686 moved to. The low-g channel stays enabled and batched; its FIFO words are simply not consumed. Dropping BDR_XL would save a third of the FIFO traffic but is only safe once XL_HG_BATCH_EN is confirmed to batch independently of it. Assisted-by: Claude:claude-opus-5 Signed-off-by: Jacob Dahl * fix(drivers/lsm6dsv): drain whole sample periods only The transfer buffer was sized to hold a partial trailing period on top of FIFO_MAX_SAMPLES full ones, so a maximal drain yielded 33 gyro and 33 accel words into 32-slot messages. The per-channel bound checks then dropped the newest sample of each without a trace, leaving timestamp_sample one dt optimistic for that batch. Hand FIFORead whole periods instead and leave any period still being batched in the FIFO for the next cycle. The message capacity is now a static_assert rather than a runtime check, and the remaining bound is a real error path: it can only trip if the tag stream stops matching the configured batching, which is corruption, not a backlog. Assisted-by: Claude:claude-opus-5 Signed-off-by: Jacob Dahl * refactor(drivers/lsm6dsv): drop the periodic register check Re-reading configuration registers in the run loop and resetting the sensor when one disagrees is nondeterministic behaviour guarding against something that does not happen: nothing in the FIFO read path writes to a register address, so a configured register does not spontaneously change. It only adds register traffic to the cycle and gives a transient SPI error a path to reset a healthy sensor. The 1 Hz temperature update was in the else arm of that check, so it only ran on cycles the check skipped; it now runs on its own timer. Configure() still verifies every register once after writing it, which is what makes the CONFIGURE retry work. Assisted-by: Claude:claude-opus-5 Signed-off-by: Jacob Dahl * refactor(drivers/lsm6dsv): hold the FIFO transfer buffer as a member The transfer buffer was a stack local, zero-initialised on every FIFO read in wq:SPIx. Nothing reads past transfer_size, so the clear was never protecting against stale data; only the command byte has to be restored, because transfer() overwrites it with the byte clocked in alongside. Also drop the FIFO temperature tag handling. FIFO_CTRL4 leaves ODR_T_BATCH at 0, so no temperature word is ever batched and the branch was unreachable; UpdateTemperature() is what actually reports temperature. Assisted-by: Claude:claude-opus-5 Signed-off-by: Jacob Dahl * perf(drivers/lsm6dsv): batch only the high-g channel on the 80X / 320X The low-g channel was still batched into the FIFO alongside the high-g one and its words simply discarded, so every sample period cost three FIFO words and three words of SPI traffic for two that were used. The high-g channel batches at its own ODR under XL_HG_BATCH_EN, independent of BDR_XL, so leave the low-g channel unbatched on those parts. One third less FIFO fill and SPI3 traffic at the same publish rate. * docs(drivers/lsm6dsv): correct the high-g clip detection comment The comment claimed PX4Accelerometer detects this channel's saturation from range/scale via UpdateClipLimit(). That limit only serves the non-FIFO update() path; updateFIFO() flags clipping off the int16 rail, which the ±80 g channel never reaches. State that the saturation goes unreported rather than the opposite. * fix(drivers/lsm6dsv): publish the low-g ±16 g channel on the 80X / 320X The high-g accelerometer is a sports-impact element: ±1.5 g typical zero-g offset, ±2 mg/°C tempco and 1000 µg/√Hz, against ±12 mg, ±0.07 mg/°C and 60 µg/√Hz for the low-g one in the same package. Pinning it at a narrower full-scale does not change the tempco. As the published accel it reads 0.4-0.6 m/s² low uncalibrated and drifts through a calibration with die temperature, which is the wrong property for a backup flight IMU. Batch the low-g channel at the same 7.68 kHz and leave the high-g accelerometer powered down: XL_HG_BATCH_EN clear, CTRL1_XL_HG at reset, no ACCEL_HG words. Scale is 0.488 mg/LSB for every variant; the gyro stays ±4000 dps. --------- Signed-off-by: alexklimaj Signed-off-by: Jacob Dahl Co-authored-by: Jacob Dahl (cherry picked from commit 29dea325db7b64cb57b29db8e699fbe255ee1e50) * feat(boards/ark_fpv): add FMUID 1 LSM6DSV32X (#28512) * feat(boards/ark_fpv): add FMUID 1 LSM6DSV32X Rev 1 increments FMUID and replaces the IIM-42653 with an LSM6DSV32X on the same SPI1 pads. Configure the 32X at ±32 g / ±4000 dps. Assisted-by: Grok:grok-4.6 Signed-off-by: alexklimaj * feat(drivers/lsm6dsv): run 32X at 7.68 kHz with PX4-owned filtering Match IIM-42653-style raw data: HAODR 7680 Hz, disable accel LPF2, 10 MHz SPI (device max), and exact FS/32768 accel plus 140 mdps/LSB gyro scales. Periodically re-check config registers like the TDK drivers. Assisted-by: Grok:grok-4.6 Signed-off-by: alexklimaj * fix(drivers/lsm6dsv): keep the accel LPF2 at ODR/10 PX4 has no anti-alias stage of its own: the FIFO is decimated to the publish rate with a plain mean and the software cutoffs run after that, so with LPF2 off the accel is flat to ODR/2 (3840 Hz on the 32X) and that content folds into the published stream. ODR/10 is the nearest match to the 585 Hz AAF the Invensense drivers use, and to what the rev 0 ARK FPV IIM-42653 flies with. The gyro is unchanged: its LPF2 is fixed by ODR at 537 Hz. * fix(drivers/lsm6dsv): validate complete register fields Runtime checks must detect changes to zero-valued rate, range, and filter bits. Include register faults in the published IMU error counts so health reporting retains the fault history after recovery. Assisted-by: Codex:gpt-6 Signed-off-by: Jacob Dahl --------- Signed-off-by: alexklimaj Signed-off-by: Jacob Dahl Co-authored-by: Jacob Dahl (cherry picked from commit d05e3a4a26636157962dfc68119c3217c34e0b32) * fix(boards/ark_fpv): drive SPIX_SYNC only on the IIM-42653 revision (#28825) SPIX_SYNC is the 32 kHz CLKIN on IMU pin 9. On FMUM 1 that pin is the LSM6DSV32X INT2 push-pull output, so the MCU drove against it: the IMU die ran about 10 degC hotter and accel read about 0.19 g off on one axis. Assisted-by: Claude:claude-opus-5-5 (cherry picked from commit bb4e77b00c54d05d91d5f30e3d5d2b5c6698185b) * fix(drivers/imu/st/lsm6dsv): reset before telling the 16X from the 32X (#28823) CTRL8 bit2 identifies the 32X only at its reset value, and probe() read it before any reset. After a warm restart from firmware that cleared the bit (drivers without 32X support write the 16X CTRL8 encoding), a 32X was detected as a 16X and its accel ran at ±32 g while scaled as ±16 g. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Jacob Dahl (cherry picked from commit a7646e2d6d4bc6dd0ff039702ad2a7d2b153d000) * fix(boards/ark/fpv): split FW/VTOL builds to free flash (#27906) * fix(boards/ark/fpv): split FW/VTOL into separate builds for flash Default ark_fpv is multicopter-only so listener and sd_bench fit in flash. Fixed-wing and VTOL stacks move to ark_fpv_fw and ark_fpv_vtol, which disable those two system commands to stay under the limit. Signed-off-by: alexklimaj * fix(boards/ark/fpv): trim optional modules from vtol build Drop gimbal, gyro_fft, autotune, camera, payload, and debug systemcmds from ark_fpv_vtol so the full VTOL stack fits with more flash margin. Signed-off-by: alexklimaj * fix(boards/ark/fpv): keep more modules on vtol; trim only debug extras Restore autotune, gimbal, gyro_fft, mag bias, payload, camera, esc battery, and actuator_test on ark_fpv_vtol. Keep flash margin by leaving sd_bench, listener, bsondump, i2cdetect, and pca9685 off. Signed-off-by: alexklimaj --------- Signed-off-by: alexklimaj (cherry picked from commit 40a05f07093971fc55f88495f594192c31e055df) --------- Signed-off-by: alexklimaj Signed-off-by: Jacob Dahl Co-authored-by: Alex Klimaj --- boards/ark/fpv/default.px4board | 16 +- boards/ark/fpv/fw.px4board | 17 + boards/ark/fpv/init/rc.board_defaults | 6 + boards/ark/fpv/init/rc.board_sensors | 8 +- boards/ark/fpv/src/board_config.h | 4 +- boards/ark/fpv/src/init.c | 9 +- boards/ark/fpv/src/spi.cpp | 4 +- boards/ark/fpv/vtol.px4board | 15 + boards/px4/fmu-v6c/init/rc.board_sensors | 2 +- src/drivers/drv_sensor.h | 2 + src/drivers/imu/st/lsm6dsv/LSM6DSV.cpp | 365 +++++++++++++----- src/drivers/imu/st/lsm6dsv/LSM6DSV.hpp | 87 ++++- .../imu/st/lsm6dsv/ST_LSM6DSV_Registers.hpp | 58 ++- src/drivers/imu/st/lsm6dsv/lsm6dsv_main.cpp | 9 +- 14 files changed, 470 insertions(+), 132 deletions(-) create mode 100644 boards/ark/fpv/fw.px4board create mode 100644 boards/ark/fpv/vtol.px4board diff --git a/boards/ark/fpv/default.px4board b/boards/ark/fpv/default.px4board index eac16b04dd4..520757cb0c6 100644 --- a/boards/ark/fpv/default.px4board +++ b/boards/ark/fpv/default.px4board @@ -17,6 +17,7 @@ CONFIG_DRIVERS_DSHOT=y CONFIG_DRIVERS_GPS=y CONFIG_DRIVERS_HEATER=y CONFIG_DRIVERS_IMU_INVENSENSE_IIM42653=y +CONFIG_DRIVERS_IMU_ST_LSM6DSV=y CONFIG_DRIVERS_IMU_MURATA_SCH16T=y CONFIG_DRIVERS_MAGNETOMETER_BOSCH_BMM150=y CONFIG_DRIVERS_MAGNETOMETER_HMC5883=y @@ -32,7 +33,6 @@ CONFIG_DRIVERS_PWM_OUT=y CONFIG_COMMON_RC=y CONFIG_DRIVERS_UAVCAN=y CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 -CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y CONFIG_MODULES_COMMANDER=y @@ -42,11 +42,14 @@ CONFIG_MODULES_EKF2=y CONFIG_MODULES_ESC_BATTERY=y CONFIG_MODULES_EVENTS=y CONFIG_MODULES_FLIGHT_MODE_MANAGER=y -CONFIG_MODULES_FW_ATT_CONTROL=y -CONFIG_MODULES_FW_AUTOTUNE_ATTITUDE_CONTROL=y -CONFIG_MODULES_FW_MODE_MANAGER=y -CONFIG_MODULES_FW_LATERAL_LONGITUDINAL_CONTROL=y -CONFIG_MODULES_FW_RATE_CONTROL=y +# Fixed-wing and VTOL stacks live in ark_fpv_fw / ark_fpv_vtol builds to free flash +CONFIG_MODULES_AIRSPEED_SELECTOR=n +CONFIG_MODULES_FW_ATT_CONTROL=n +CONFIG_MODULES_FW_AUTOTUNE_ATTITUDE_CONTROL=n +CONFIG_MODULES_FW_MODE_MANAGER=n +CONFIG_MODULES_FW_LATERAL_LONGITUDINAL_CONTROL=n +CONFIG_MODULES_FW_RATE_CONTROL=n +CONFIG_MODULES_VTOL_ATT_CONTROL=n CONFIG_MODULES_GIMBAL=y CONFIG_MODULES_GYRO_CALIBRATION=y CONFIG_MODULES_GYRO_FFT=y @@ -67,7 +70,6 @@ CONFIG_MODULES_PAYLOAD_DELIVERER=y CONFIG_MODULES_RC_UPDATE=y CONFIG_MODULES_SENSORS=y CONFIG_MODULES_UXRCE_DDS_CLIENT=y -CONFIG_MODULES_VTOL_ATT_CONTROL=y CONFIG_SYSTEMCMDS_ACTUATOR_TEST=y CONFIG_SYSTEMCMDS_BL_UPDATE=y CONFIG_SYSTEMCMDS_BSONDUMP=y diff --git a/boards/ark/fpv/fw.px4board b/boards/ark/fpv/fw.px4board new file mode 100644 index 00000000000..23a5b94b066 --- /dev/null +++ b/boards/ark/fpv/fw.px4board @@ -0,0 +1,17 @@ +# Fixed-wing variant: FW stack without multicopter / VTOL. +# SD bench and topic listener disabled to fit flash. +CONFIG_MODULES_AIRSPEED_SELECTOR=y +CONFIG_MODULES_FW_ATT_CONTROL=y +CONFIG_MODULES_FW_AUTOTUNE_ATTITUDE_CONTROL=y +CONFIG_MODULES_FW_MODE_MANAGER=y +CONFIG_MODULES_FW_LATERAL_LONGITUDINAL_CONTROL=y +CONFIG_MODULES_FW_RATE_CONTROL=y +CONFIG_MODULES_FLIGHT_MODE_MANAGER=n +CONFIG_MODULES_MC_ATT_CONTROL=n +CONFIG_MODULES_MC_AUTOTUNE_ATTITUDE_CONTROL=n +CONFIG_MODULES_MC_HOVER_THRUST_ESTIMATOR=n +CONFIG_MODULES_MC_POS_CONTROL=n +CONFIG_MODULES_MC_RATE_CONTROL=n +CONFIG_MODULES_VTOL_ATT_CONTROL=n +CONFIG_SYSTEMCMDS_SD_BENCH=n +CONFIG_SYSTEMCMDS_TOPIC_LISTENER=n diff --git a/boards/ark/fpv/init/rc.board_defaults b/boards/ark/fpv/init/rc.board_defaults index f1a916aa8f8..214323b7fcf 100644 --- a/boards/ark/fpv/init/rc.board_defaults +++ b/boards/ark/fpv/init/rc.board_defaults @@ -25,6 +25,12 @@ then param set-default HEATER1_SENS_ID 3014666 fi +if ver hwtypecmp ARKFPV001 +then + # LSM6DSV32X on SPI1 (device_id Type=0x13, SPI:1) + param set-default HEATER1_SENS_ID 1245194 +fi + param set-default BAT1_V_DIV 21.0 param set-default BAT1_V_FILT 0.075 param set-default BAT1_I_FILT 0.5 diff --git a/boards/ark/fpv/init/rc.board_sensors b/boards/ark/fpv/init/rc.board_sensors index 1c0b9cf185d..f5be77c4d88 100644 --- a/boards/ark/fpv/init/rc.board_sensors +++ b/boards/ark/fpv/init/rc.board_sensors @@ -7,10 +7,16 @@ board_adc start if ver hwtypecmp ARKFPV000 then - # Internal SPI bus IIM42653 + # Internal SPI bus IIM-42653 iim42653 -R 14 -s -b 1 start fi +if ver hwtypecmp ARKFPV001 +then + # Internal SPI bus LSM6DSV32X + lsm6dsv -R 14 -s -b 1 start +fi + # Internal magnetometer on I2C iis2mdc -R 0 -I -b 4 start diff --git a/boards/ark/fpv/src/board_config.h b/boards/ark/fpv/src/board_config.h index f75bec3e08a..f7606ac0e0e 100644 --- a/boards/ark/fpv/src/board_config.h +++ b/boards/ark/fpv/src/board_config.h @@ -197,8 +197,8 @@ #define BOARD_NUM_SPI_CFG_HW_VERSIONS 2 // Base/FMUM -#define ARKFPV_0 HW_FMUM_ID(0x0) // ARKFPV, Sensor Set Rev 0 -#define ARKFPV_1 HW_FMUM_ID(0x1) // ARKFPV, Sensor Set Rev 1 +#define ARKFPV_0 HW_FMUM_ID(0x0) // ARKFPV, IIM-42653 +#define ARKFPV_1 HW_FMUM_ID(0x1) // ARKFPV, LSM6DSV32X #define UAVCAN_NUM_IFACES_RUNTIME 1 diff --git a/boards/ark/fpv/src/init.c b/boards/ark/fpv/src/init.c index cbd9382238f..9bdd49f39ac 100644 --- a/boards/ark/fpv/src/init.c +++ b/boards/ark/fpv/src/init.c @@ -296,9 +296,12 @@ __EXPORT int board_app_initialize(uintptr_t arg) #endif /* CONFIG_MMCSD */ - /* Configure the SPIX_SYNC output */ - spix_sync_servo_init(BOARD_SPIX_SYNC_FREQ); - spix_sync_servo_set(0, 150); + /* SPIX_SYNC is the IIM-42653 CLKIN on IMU pin 9. On FMUM 1 that pin is the + * LSM6DSV32X INT2 push-pull output, so driving it fights the IMU. */ + if (GET_HW_FMUM_ID() == ARKFPV_0) { + spix_sync_servo_init(BOARD_SPIX_SYNC_FREQ); + spix_sync_servo_set(0, 150); + } return OK; } diff --git a/boards/ark/fpv/src/spi.cpp b/boards/ark/fpv/src/spi.cpp index fb922fc95d6..77a2f703ca2 100644 --- a/boards/ark/fpv/src/spi.cpp +++ b/boards/ark/fpv/src/spi.cpp @@ -44,9 +44,9 @@ constexpr px4_spi_bus_all_hw_t px4_spi_buses_all_hw[BOARD_NUM_SPI_CFG_HW_VERSION initSPIConfigExternal(SPI::CS{GPIO::PortI, GPIO::Pin10}, SPI::DRDY{GPIO::PortD, GPIO::Pin11}) }), }), - initSPIFmumID(ARKFPV_1, { // Placeholder + initSPIFmumID(ARKFPV_1, { // LSM6DSV32X initSPIBus(SPI::Bus::SPI1, { - initSPIDevice(DRV_IMU_DEVTYPE_IIM42653, SPI::CS{GPIO::PortI, GPIO::Pin9}, SPI::DRDY{GPIO::PortF, GPIO::Pin2}), + initSPIDevice(DRV_IMU_DEVTYPE_ST_LSM6DSV, SPI::CS{GPIO::PortI, GPIO::Pin9}, SPI::DRDY{GPIO::PortF, GPIO::Pin2}), }, {GPIO::PortI, GPIO::Pin11}), initSPIBusExternal(SPI::Bus::SPI6, { initSPIConfigExternal(SPI::CS{GPIO::PortI, GPIO::Pin10}, SPI::DRDY{GPIO::PortD, GPIO::Pin11}) diff --git a/boards/ark/fpv/vtol.px4board b/boards/ark/fpv/vtol.px4board new file mode 100644 index 00000000000..df2f6903938 --- /dev/null +++ b/boards/ark/fpv/vtol.px4board @@ -0,0 +1,15 @@ +# VTOL variant: multicopter + fixed-wing + VTOL stack. +# SD bench and topic listener disabled to fit flash (keep autotune, gimbal, +# gyro_fft, mag bias, payload, camera, esc battery, actuator_test). +CONFIG_MODULES_AIRSPEED_SELECTOR=y +CONFIG_MODULES_FW_ATT_CONTROL=y +CONFIG_MODULES_FW_AUTOTUNE_ATTITUDE_CONTROL=y +CONFIG_MODULES_FW_MODE_MANAGER=y +CONFIG_MODULES_FW_LATERAL_LONGITUDINAL_CONTROL=y +CONFIG_MODULES_FW_RATE_CONTROL=y +CONFIG_MODULES_VTOL_ATT_CONTROL=y +CONFIG_SYSTEMCMDS_SD_BENCH=n +CONFIG_SYSTEMCMDS_TOPIC_LISTENER=n +CONFIG_SYSTEMCMDS_BSONDUMP=n +CONFIG_SYSTEMCMDS_I2CDETECT=n +CONFIG_DRIVERS_PCA9685_PWM_OUT=n diff --git a/boards/px4/fmu-v6c/init/rc.board_sensors b/boards/px4/fmu-v6c/init/rc.board_sensors index 1382aa87c51..cf4705d32a2 100644 --- a/boards/px4/fmu-v6c/init/rc.board_sensors +++ b/boards/px4/fmu-v6c/init/rc.board_sensors @@ -18,7 +18,7 @@ fi # Internal SPI bus IMU (probe-based: LSM6DSV or ICM42688P on same CS) if ! icm42688p -R 6 -s -q start then - lsm6dsv -R 26 -s start + lsm6dsv -R 6 -s start fi # Internal barometer on I2C4 (The same bus is also exposed externally, and therefore marked as external) diff --git a/src/drivers/drv_sensor.h b/src/drivers/drv_sensor.h index 96126f60c49..c83d65f5a3c 100644 --- a/src/drivers/drv_sensor.h +++ b/src/drivers/drv_sensor.h @@ -78,6 +78,8 @@ #define DRV_FLOW_DEVTYPE_SIM 0x16 #define DRV_IMU_DEVTYPE_ST_LSM6DSK320X 0x17 +#define DRV_IMU_DEVTYPE_ST_LSM6DSV80X 0x18 +#define DRV_IMU_DEVTYPE_ST_LSM6DSV320X 0x19 #define DRV_IMU_DEVTYPE_MPU6000 0x21 #define DRV_GYR_DEVTYPE_L3GD20 0x22 diff --git a/src/drivers/imu/st/lsm6dsv/LSM6DSV.cpp b/src/drivers/imu/st/lsm6dsv/LSM6DSV.cpp index f180e98fadd..04c7b1e185c 100644 --- a/src/drivers/imu/st/lsm6dsv/LSM6DSV.cpp +++ b/src/drivers/imu/st/lsm6dsv/LSM6DSV.cpp @@ -42,7 +42,7 @@ static constexpr int16_t combine(uint8_t msb, uint8_t lsb) static constexpr bool IsSupportedWhoAmI(uint8_t whoami) { - return (whoami == WHO_AM_I_ID) || (whoami == WHO_AM_I_DSK320X); + return (whoami == WHO_AM_I_ID) || (whoami == WHO_AM_I_DSK320X) || (whoami == WHO_AM_I_HIGHG); } LSM6DSV::LSM6DSV(const I2CSPIDriverConfig &config) : @@ -50,7 +50,8 @@ LSM6DSV::LSM6DSV(const I2CSPIDriverConfig &config) : I2CSPIDriver(config), _drdy_gpio(config.drdy_gpio), _px4_accel(get_device_id(), config.rotation), - _px4_gyro(get_device_id(), config.rotation) + _px4_gyro(get_device_id(), config.rotation), + _highg_variant_arg(config.custom1) { if (config.drdy_gpio != 0) { _drdy_missed_perf = perf_alloc(PC_COUNT, MODULE_NAME": DRDY missed"); @@ -93,6 +94,16 @@ int LSM6DSV::init() _px4_accel.set_device_type(DRV_IMU_DEVTYPE_ST_LSM6DSK320X); _px4_gyro.set_device_type(DRV_IMU_DEVTYPE_ST_LSM6DSK320X); break; + + case DeviceVariant::LSM6DSV320X: + _px4_accel.set_device_type(DRV_IMU_DEVTYPE_ST_LSM6DSV320X); + _px4_gyro.set_device_type(DRV_IMU_DEVTYPE_ST_LSM6DSV320X); + break; + + case DeviceVariant::LSM6DSV80X: + _px4_accel.set_device_type(DRV_IMU_DEVTYPE_ST_LSM6DSV80X); + _px4_gyro.set_device_type(DRV_IMU_DEVTYPE_ST_LSM6DSV80X); + break; } return Reset() ? 0 : -1; @@ -125,13 +136,24 @@ void LSM6DSV::print_status() case DeviceVariant::LSM6DSV32X: variant_str = "LSM6DSV32X"; break; case DeviceVariant::LSM6DSK320X: variant_str = "LSM6DSK320X"; break; + + case DeviceVariant::LSM6DSV320X: variant_str = "LSM6DSV320X"; break; + + case DeviceVariant::LSM6DSV80X: variant_str = "LSM6DSV80X"; break; } PX4_INFO("Device variant: %s", variant_str); PX4_INFO("FIFO empty interval: %d us (%.1f Hz), %ld samples per cycle", _fifo_empty_interval_us, 1e6 / _fifo_empty_interval_us, (long)_fifo_gyro_samples); - PX4_INFO("Sensor ODR: %u Hz (HAODR mode1), FIFO sample dt: %.0f us", - (unsigned)GYRO_ODR, (double)FIFO_SAMPLE_DT); + PX4_INFO("Sensor ODR: %u Hz (HAODR), FIFO sample dt: %.1f us, %u words/period", + (unsigned)_sensor_odr, (double)_fifo_sample_dt, (unsigned)_fifo_words_per_period); + + if (_device_variant == DeviceVariant::LSM6DSV32X) { + PX4_INFO("Full-scale: accel +/-32 g, gyro +/-4000 dps"); + + } else if (_dsv80x_family) { + PX4_INFO("Accel channel: low-g, full-scale +/-16 g (high-g channel unused)"); + } perf_print_counter(_bad_register_perf); perf_print_counter(_bad_transfer_perf); @@ -143,7 +165,20 @@ void LSM6DSV::print_status() int LSM6DSV::probe() { - const uint8_t whoami = RegisterRead(Register::WHO_AM_I); + // The first SPI transaction after power-up can return garbage while the device's shared + // I2C/I3C/SPI interface latches onto SPI (selected by the first CS falling edge), so retry + // the WHO_AM_I read a few times before giving up. + uint8_t whoami = 0; + + for (int attempt = 0; attempt < 3; attempt++) { + whoami = RegisterRead(Register::WHO_AM_I); + + if (IsSupportedWhoAmI(whoami)) { + break; + } + + px4_usleep(1000); + } if (whoami == WHO_AM_I_DSK320X) { _device_variant = DeviceVariant::LSM6DSK320X; @@ -152,12 +187,53 @@ int LSM6DSV::probe() return PX4_OK; } + if (whoami == WHO_AM_I_HIGHG) { + // The LSM6DSV80X and LSM6DSV320X share this WHO_AM_I and register map and cannot be told + // apart over SPI, so the physically-installed part is selected explicitly at start with the + // -T argument (config.custom1): 320 -> LSM6DSV320X, otherwise (incl. 80 / unset) -> 80X. + if (_highg_variant_arg == 320) { + _device_variant = DeviceVariant::LSM6DSV320X; + PX4_INFO("LSM6DSV320X selected (WHO_AM_I 0x73)"); + + } else { + _device_variant = DeviceVariant::LSM6DSV80X; + + if (_highg_variant_arg != 80) { + PX4_WARN("WHO_AM_I 0x73 is ambiguous (80X/320X); defaulting to LSM6DSV80X. Pass -T 80 or -T 320."); + + } else { + PX4_INFO("LSM6DSV80X selected (WHO_AM_I 0x73)"); + } + } + + UpdateVariantRegisterConfig(); + return PX4_OK; + } + if (whoami != WHO_AM_I_ID) { DEVICE_DEBUG("unexpected WHO_AM_I 0x%02x", whoami); return PX4_ERROR; } - // 16X vs 32X: read CTRL8 bit2 (hardware-reserved, differs per variant) + // 16X vs 32X: CTRL8 bit2 is must-be-0 on the 16X and must-be-1 on the 32X, so it identifies the part + // only at its reset value. A warm restart keeps whatever the previous firmware wrote there (drivers + // without 32X support write the 16X encoding), so reset first, powered down per ST's reset procedure. + RegisterSetAndClearBits(Register::CTRL1, 0, CTRL1_BIT::ODR_XL_MASK); + RegisterSetAndClearBits(Register::CTRL2, 0, CTRL2_BIT::ODR_G_MASK); + RegisterWrite(Register::CTRL3, CTRL3_BIT::SW_RESET); + + bool reset_complete = false; + + for (int attempt = 0; attempt < 10 && !reset_complete; attempt++) { + px4_usleep(1000); + reset_complete = (RegisterRead(Register::CTRL3) & CTRL3_BIT::SW_RESET) == 0; + } + + if (!reset_complete) { + DEVICE_DEBUG("software reset timed out"); + return PX4_ERROR; + } + const uint8_t ctrl8 = RegisterRead(Register::CTRL8); _device_variant = (ctrl8 & Bit2) ? DeviceVariant::LSM6DSV32X : DeviceVariant::LSM6DSV16X; @@ -270,15 +346,17 @@ void LSM6DSV::RunImpl() } else { // FIFO unread word count: 9-bit field (FIFO_STATUS2 bit0 is bit8) - // Each sample period produces 2 words (1 gyro word + 1 accel word) + // Each sample period produces _fifo_words_per_period words (gyro + accel) uint16_t fifo_words = fifo_status.STATUS1; if (fifo_status.STATUS2 & static_cast(FIFO_STATUS2_BIT::DIFF_FIFO_8)) { fifo_words |= (1u << 8); } - // Convert word count to sample periods for comparisons against _fifo_gyro_samples / FIFO_MAX_SAMPLES - const uint16_t sample_periods = fifo_words / 2; + // Drain whole sample periods only. A period that was still being batched when the + // status was read stays in the FIFO for the next cycle, so a batch never carries a + // partial period and the per-channel sample counts always agree. + uint16_t sample_periods = fifo_words / _fifo_words_per_period; if (sample_periods == 0) { perf_count(_fifo_empty_perf); @@ -292,11 +370,11 @@ void LSM6DSV::RunImpl() // tolerate minor jitter, leave sample to next iteration if behind by only 1 if (sample_periods == static_cast(_fifo_gyro_samples) + 1) { - timestamp_sample -= static_cast(FIFO_SAMPLE_DT); - fifo_words -= 2; + timestamp_sample -= static_cast(_fifo_sample_dt); + sample_periods--; } - if (FIFORead(timestamp_sample, fifo_words)) { + if (FIFORead(timestamp_sample, sample_periods * _fifo_words_per_period)) { success = true; if (_failure_count > 0) { @@ -315,7 +393,6 @@ void LSM6DSV::RunImpl() } } - // periodically check configuration registers if (!success || hrt_elapsed_time(&_last_config_check_timestamp) > 100_ms) { if (RegisterCheck(_register_cfg[_checked_register])) { _last_config_check_timestamp = now; @@ -324,14 +401,14 @@ void LSM6DSV::RunImpl() } else { perf_count(_bad_register_perf); Reset(); + return; } + } - } else { - // periodically update temperature (~1 Hz) - if (hrt_elapsed_time(&_temperature_update_timestamp) >= 1_s) { - UpdateTemperature(); - _temperature_update_timestamp = now; - } + // periodically update temperature (~1 Hz) + if (hrt_elapsed_time(&_temperature_update_timestamp) >= 1_s) { + UpdateTemperature(); + _temperature_update_timestamp = now; } } @@ -341,30 +418,114 @@ void LSM6DSV::RunImpl() void LSM6DSV::ConfigureSampleRate(int sample_rate) { - const float min_interval = FIFO_SAMPLE_DT; + const float min_interval = _fifo_sample_dt; _fifo_empty_interval_us = math::max(roundf((1e6f / (float)sample_rate) / min_interval) * min_interval, min_interval); - _fifo_gyro_samples = roundf(math::min((float)_fifo_empty_interval_us / (1e6f / GYRO_RATE), (float)FIFO_MAX_SAMPLES)); + _fifo_gyro_samples = roundf(math::min((float)_fifo_empty_interval_us / _fifo_sample_dt, (float)FIFO_MAX_SAMPLES)); - _fifo_empty_interval_us = _fifo_gyro_samples * (1e6f / GYRO_RATE); + _fifo_empty_interval_us = _fifo_gyro_samples * _fifo_sample_dt; ConfigureFIFOWatermark(_fifo_gyro_samples); } void LSM6DSV::UpdateVariantRegisterConfig() { - for (auto &r : _register_cfg) { - if (r.reg == Register::CTRL6) { - r.set_bits = (_device_variant == DeviceVariant::LSM6DSK320X) - ? CTRL6_BIT::FS_G_2000DPS_DSK320X - : CTRL6_BIT::FS_G_2000DPS; + _dsv80x_family = (_device_variant == DeviceVariant::LSM6DSV80X) + || (_device_variant == DeviceVariant::LSM6DSV320X); - } else if (r.reg == Register::CTRL8) { - r.set_bits = (_device_variant == DeviceVariant::LSM6DSV32X) - ? static_cast(CTRL8_BIT::FS_XL_16G_DSV32X | CTRL8_BIT::LPF2_BW_ODR_DIV_10) - : static_cast(CTRL8_BIT::FS_XL_16G | CTRL8_BIT::LPF2_BW_ODR_DIV_10); + // 32X / 80X / 320X: HAODR_SEL=00 set, ODR code 0x0C = 7680 Hz + const bool odr_7680 = _dsv80x_family || (_device_variant == DeviceVariant::LSM6DSV32X); + _sensor_odr = odr_7680 ? ODR_DSV80X : GYRO_ODR; + + _fifo_words_per_period = 2; // gyro + low-g accel + + _fifo_sample_dt = 1e6f / (float)_sensor_odr; + + for (auto &r : _register_cfg) { + switch (r.reg) { + case Register::CTRL6: // gyroscope full-scale + switch (_device_variant) { + case DeviceVariant::LSM6DSV80X: + case DeviceVariant::LSM6DSV320X: + r.set_bits = CTRL6_BIT::FS_G_4000DPS_HIGHG; // ±4000 dps (CTRL6 bit3 = 1) + break; + + case DeviceVariant::LSM6DSV32X: + r.set_bits = CTRL6_BIT::FS_G_4000DPS_DSV32X; // ±4000 dps, FS_G_[3:0]=1100 + break; + + case DeviceVariant::LSM6DSK320X: + r.set_bits = CTRL6_BIT::FS_G_2000DPS_DSK320X; + break; + + default: + r.set_bits = CTRL6_BIT::FS_G_2000DPS; + break; + } + + r.clear_bits = CTRL6_BIT::FS_G_MASK & ~r.set_bits; + break; + + case Register::CTRL8: // accelerometer full-scale + LPF2 bandwidth + + // LPF2 is the only anti-alias stage: PX4 decimates the FIFO to the publish rate with a plain mean, + // so anything up to ODR/2 folds down without it. ODR/10 (768 Hz at 7.68 kHz, 200 Hz at 2 kHz) is + // the nearest match to the 585 Hz AAF the Invensense drivers run. The gyro needs nothing: its + // LPF2 is fixed by ODR (537 Hz at 7.68 kHz). + if (_device_variant == DeviceVariant::LSM6DSV32X) { + r.set_bits = static_cast(CTRL8_BIT::FS_XL_32G_DSV32X | CTRL8_BIT::LPF2_BW_ODR_DIV_10); + + } else { + r.set_bits = static_cast(CTRL8_BIT::FS_XL_16G | CTRL8_BIT::LPF2_BW_ODR_DIV_10); + } + + r.clear_bits = (CTRL8_BIT::FS_XL_MASK | CTRL8_BIT::LPF2_BW_MASK) & ~r.set_bits; + + if (_device_variant == DeviceVariant::LSM6DSV32X) { + r.clear_bits |= CTRL8_BIT::XL_DualC_EN; + } + + break; + + case Register::HAODR_CFG: // HAODR ODR set selection + if (odr_7680) { + r.set_bits = 0; // HAODR_SEL=00 (1920/3840/7680 Hz set) + + } else { + r.set_bits = HAODR_CFG_BIT::HAODR_MODE1; // HAODR_SEL=01 (2000 Hz set) + } + + r.clear_bits = HAODR_CFG_BIT::HAODR_SEL_MASK & ~r.set_bits; + break; + + case Register::CTRL1: // accelerometer ODR + high-accuracy ODR mode + r.set_bits = odr_7680 + ? static_cast(HAODR_SEL0_ODR_7680HZ | CTRL1_BIT::CTRL1_MODE_HAODR) + : static_cast(HAODR_MODE1_ODR_2000HZ | CTRL1_BIT::CTRL1_MODE_HAODR); + r.clear_bits = (CTRL1_BIT::ODR_XL_MASK | CTRL1_BIT::OP_MODE_XL_MASK) & ~r.set_bits; + break; + + case Register::CTRL2: // gyroscope ODR + high-accuracy ODR mode + r.set_bits = odr_7680 + ? static_cast(HAODR_SEL0_ODR_7680HZ | CTRL2_BIT::CTRL2_MODE_HAODR) + : static_cast(HAODR_MODE1_ODR_2000HZ | CTRL2_BIT::CTRL2_MODE_HAODR); + r.clear_bits = (CTRL2_BIT::ODR_G_MASK | CTRL2_BIT::OP_MODE_G_MASK) & ~r.set_bits; + break; + + case Register::FIFO_CTRL3: // FIFO batch data rate (gyro + low-g accel) + r.set_bits = odr_7680 + ? static_cast(FIFO_CTRL3_BIT::BDR_GY_7680 | FIFO_CTRL3_BIT::BDR_XL_7680) + : static_cast(FIFO_CTRL3_BIT::BDR_GY_HAODR | FIFO_CTRL3_BIT::BDR_XL_HAODR); + r.clear_bits = static_cast(~r.set_bits); + break; + + default: + break; } } + + // recompute FIFO timing / watermark for the (possibly changed) ODR and word count + ConfigureSampleRate(_px4_gyro.get_max_rate_hz()); } bool LSM6DSV::Configure() @@ -383,13 +544,28 @@ bool LSM6DSV::Configure() } } - // Gyroscope: ±2000 dps, 70 mdps/LSB (ST datasheet) - _px4_gyro.set_scale(math::radians(70.f / 1000.f)); - _px4_gyro.set_range(math::radians(2000.f)); + // Scale and range are set once here and never touched again, so every published batch carries + // the same scale factor. + // Accel LSB is FS / 32768 (datasheet 0.122/0.244/0.488/0.976 mg/LSB is that quotient rounded). + // Gyro LSB is not FS / 32768: ST specifies 70 mdps/LSB at ±2000 dps and 140 mdps/LSB at ±4000 dps. + if (_device_variant == DeviceVariant::LSM6DSV32X) { + _px4_gyro.set_scale(math::radians(0.140f)); + _px4_gyro.set_range(math::radians(4000.f)); + _px4_accel.set_scale(32.f * CONSTANTS_ONE_G / 32768.f); + _px4_accel.set_range(32.f * CONSTANTS_ONE_G); - // Accelerometer: ±16g, 0.488 mg/LSB (ST datasheet) - _px4_accel.set_scale(0.488f * (CONSTANTS_ONE_G / 1000.f)); - _px4_accel.set_range(16.f * CONSTANTS_ONE_G); + } else if (_dsv80x_family) { + _px4_gyro.set_scale(math::radians(0.140f)); + _px4_gyro.set_range(math::radians(4000.f)); + _px4_accel.set_scale(16.f * CONSTANTS_ONE_G / 32768.f); + _px4_accel.set_range(16.f * CONSTANTS_ONE_G); + + } else { + _px4_gyro.set_scale(math::radians(0.070f)); + _px4_gyro.set_range(math::radians(2000.f)); + _px4_accel.set_scale(16.f * CONSTANTS_ONE_G / 32768.f); + _px4_accel.set_range(16.f * CONSTANTS_ONE_G); + } return success; } @@ -438,87 +614,99 @@ void LSM6DSV::RegisterSetAndClearBits(Register reg, uint8_t setbits, uint8_t cle } } -bool LSM6DSV::FIFORead(const hrt_abstime ×tamp_sample, uint16_t samples) +bool LSM6DSV::FIFORead(const hrt_abstime ×tamp_sample, uint16_t words) { + // Drain the requested words in one burst. RunImpl() hands over whole sample periods, at most + // FIFO_MAX_SAMPLES of them; clamp defensively so transfer_size can never run past the buffer. + const uint16_t words_to_read = math::min(words, FIFO_MAX_SAMPLES * _fifo_words_per_period); + const size_t transfer_size = words_to_read * FIFO::WORD_SIZE + 1; + + // _fifo_buffer is reused across cycles. transfer() clobbers the command byte with the byte + // clocked in alongside it, so restore it; the rest needs no clearing because only the words + // covered by transfer_size are ever parsed. + _fifo_buffer.cmd = static_cast(Register::FIFO_DATA_OUT_TAG) | DIR_READ; + + if (transfer((uint8_t *)&_fifo_buffer, (uint8_t *)&_fifo_buffer, transfer_size) != PX4_OK) { + perf_count(_bad_transfer_perf); + return false; + } + sensor_gyro_fifo_s gyro{}; gyro.timestamp_sample = timestamp_sample; gyro.samples = 0; - gyro.dt = FIFO_SAMPLE_DT; + gyro.dt = _fifo_sample_dt; sensor_accel_fifo_s accel{}; accel.timestamp_sample = timestamp_sample; accel.samples = 0; - accel.dt = FIFO_SAMPLE_DT; + accel.dt = _fifo_sample_dt; - // Read FIFO word by word: each word is 7 bytes (tag + 6 data) - for (uint16_t i = 0; i < samples; i++) { - // Read tag + data in one transfer (1 cmd byte + 7 data bytes) - struct FIFOWordTransfer { - uint8_t cmd{static_cast(Register::FIFO_DATA_OUT_TAG) | DIR_READ}; - uint8_t TAG{0}; - uint8_t DATA_X_L{0}; - uint8_t DATA_X_H{0}; - uint8_t DATA_Y_L{0}; - uint8_t DATA_Y_H{0}; - uint8_t DATA_Z_L{0}; - uint8_t DATA_Z_H{0}; - } buffer{}; + // set if the tag stream carries more samples of a channel than the drain should have produced + bool tag_mismatch = false; - if (transfer((uint8_t *)&buffer, (uint8_t *)&buffer, sizeof(buffer)) != PX4_OK) { - perf_count(_bad_transfer_perf); - continue; - } + for (uint16_t i = 0; i < words_to_read; i++) { + const FIFOWord &word = _fifo_buffer.words[i]; // Decode tag from upper 5 bits - const uint8_t tag_id = buffer.TAG >> 3; + const uint8_t tag_id = word.TAG >> 3; - const int16_t data_x = combine(buffer.DATA_X_H, buffer.DATA_X_L); - const int16_t data_y = combine(buffer.DATA_Y_H, buffer.DATA_Y_L); - const int16_t data_z = combine(buffer.DATA_Z_H, buffer.DATA_Z_L); + // sensor's frame is +x forward, +y left, +z up + // flip y & z to publish right handed with z down (x forward, y right, z down) + const int16_t data_x = combine(word.DATA_X_H, word.DATA_X_L); + const int16_t y = combine(word.DATA_Y_H, word.DATA_Y_L); + const int16_t z = combine(word.DATA_Z_H, word.DATA_Z_L); + const int16_t data_y = (y == INT16_MIN) ? INT16_MAX : -y; + const int16_t data_z = (z == INT16_MIN) ? INT16_MAX : -z; if (tag_id == static_cast(FifoTag::GYRO_NC)) { - if (gyro.samples < (sizeof(gyro.x) / sizeof(gyro.x[0]))) { - gyro.x[gyro.samples] = data_x; - gyro.y[gyro.samples] = data_y; - gyro.z[gyro.samples] = data_z; - gyro.samples++; + if (gyro.samples >= FIFO_MAX_SAMPLES) { + tag_mismatch = true; + break; } + gyro.x[gyro.samples] = data_x; + gyro.y[gyro.samples] = data_y; + gyro.z[gyro.samples] = data_z; + gyro.samples++; + } else if (tag_id == static_cast(FifoTag::ACCEL_NC)) { - if (accel.samples < (sizeof(accel.x) / sizeof(accel.x[0]))) { - accel.x[accel.samples] = data_x; - accel.y[accel.samples] = data_y; - accel.z[accel.samples] = data_z; - accel.samples++; + if (accel.samples >= FIFO_MAX_SAMPLES) { + tag_mismatch = true; + break; } - } else if (tag_id == static_cast(FifoTag::TEMPERATURE)) { - const int16_t temp_raw = combine(buffer.DATA_X_H, buffer.DATA_X_L); - const float temperature = (temp_raw / 256.0f) + 25.0f; - - if (PX4_ISFINITE(temperature)) { - _px4_accel.set_temperature(temperature); - _px4_gyro.set_temperature(temperature); - } + accel.x[accel.samples] = data_x; + accel.y[accel.samples] = data_y; + accel.z[accel.samples] = data_z; + accel.samples++; } - // Other tags (TIMESTAMP, etc.) are silently ignored + // Other tags are ignored. FIFO_CTRL4 leaves ODR_T_BATCH and DEC_TS_BATCH at 0, so neither + // temperature nor timestamp is batched; temperature comes from UpdateTemperature(). } - // Publish + if (tag_mismatch) { + // the drain is bounded to whole sample periods, so a channel overrunning its share means + // the tag stream no longer matches the configured batching - resync rather than publish it + perf_count(_bad_transfer_perf); + FIFOReset(); + return false; + } + + const uint32_t error_count = perf_event_count(_bad_register_perf) + perf_event_count(_bad_transfer_perf) + + perf_event_count(_fifo_empty_perf) + perf_event_count(_fifo_overflow_perf); + if (gyro.samples > 0) { - _px4_gyro.set_error_count(perf_event_count(_bad_register_perf) + perf_event_count(_bad_transfer_perf) + - perf_event_count(_fifo_empty_perf) + perf_event_count(_fifo_overflow_perf)); + _px4_gyro.set_error_count(error_count); _px4_gyro.updateFIFO(gyro); } if (accel.samples > 0) { - _px4_accel.set_error_count(perf_event_count(_bad_register_perf) + perf_event_count(_bad_transfer_perf) + - perf_event_count(_fifo_empty_perf) + perf_event_count(_fifo_overflow_perf)); + _px4_accel.set_error_count(error_count); _px4_accel.updateFIFO(accel); } - return (accel.samples > 0) && (gyro.samples > 0); + return (gyro.samples > 0) && (accel.samples > 0); } void LSM6DSV::FIFOReset() @@ -590,8 +778,9 @@ bool LSM6DSV::DataReadyInterruptDisable() void LSM6DSV::ConfigureFIFOWatermark(uint8_t samples) { - // accel + gyro = 2 FIFO words per sample period - const uint8_t fifo_watermark = samples * 2; + // _fifo_words_per_period FIFO words per sample period (gyro + accel). + // WTM is the 8-bit FIFO_CTRL1 field; samples is capped at FIFO_MAX_SAMPLES (32) so this fits. + const uint8_t fifo_watermark = samples * _fifo_words_per_period; for (auto &r : _register_cfg) { if (r.reg == Register::FIFO_CTRL1) { diff --git a/src/drivers/imu/st/lsm6dsv/LSM6DSV.hpp b/src/drivers/imu/st/lsm6dsv/LSM6DSV.hpp index fb6414ac244..c331c666ffb 100644 --- a/src/drivers/imu/st/lsm6dsv/LSM6DSV.hpp +++ b/src/drivers/imu/st/lsm6dsv/LSM6DSV.hpp @@ -70,11 +70,46 @@ private: void exit_and_cleanup() override; // Sensor Configuration - static constexpr float FIFO_SAMPLE_DT{1e6f / GYRO_ODR}; - static constexpr float GYRO_RATE{static_cast(GYRO_ODR)}; - static constexpr float ACCEL_RATE{static_cast(ACCEL_ODR)}; - static constexpr int32_t FIFO_MAX_SAMPLES{static_cast(FIFO::MAX_DRAIN_SAMPLES)}; + static_assert(FIFO_MAX_SAMPLES <= (int32_t)(sizeof(sensor_gyro_fifo_s::x) / sizeof(sensor_gyro_fifo_s::x[0])), + "FIFO drain exceeds sensor_gyro_fifo capacity"); + static_assert(FIFO_MAX_SAMPLES <= (int32_t)(sizeof(sensor_accel_fifo_s::x) / sizeof(sensor_accel_fifo_s::x[0])), + "FIFO drain exceeds sensor_accel_fifo capacity"); + + // A FIFO word is a tag byte plus 6 data bytes. With IF_INC set the address rounds from + // FIFO_DATA_OUT_Z_H back to FIFO_DATA_OUT_TAG at every word boundary, so the whole FIFO drains + // as a single N*7 byte burst (AN5763 / AN6119 section 9.8). + struct FIFOWord { + uint8_t TAG; + uint8_t DATA_X_L; + uint8_t DATA_X_H; + uint8_t DATA_Y_L; + uint8_t DATA_Y_H; + uint8_t DATA_Z_L; + uint8_t DATA_Z_H; + }; + static_assert(sizeof(FIFOWord) == FIFO::WORD_SIZE, "FIFO word must be 7 bytes"); + + // RunImpl() drains whole sample periods only, at most FIFO_MAX_SAMPLES of them + static constexpr uint16_t FIFO_MAX_WORDS{static_cast(FIFO_MAX_SAMPLES * FIFO::MAX_WORDS_PER_PERIOD)}; + + struct FIFOTransferBuffer { + uint8_t cmd{static_cast(Register::FIFO_DATA_OUT_TAG) | DIR_READ}; + FIFOWord words[FIFO_MAX_WORDS] {}; + }; + static_assert(sizeof(FIFOTransferBuffer) == (1 + FIFO_MAX_WORDS * FIFO::WORD_SIZE), "Invalid transfer buffer size"); + + // held here rather than on the work queue stack: a wq:SPIx frame is not the place for a buffer + // this size, and reusing it avoids re-zeroing memory that transfer() overwrites anyway + FIFOTransferBuffer _fifo_buffer{}; + + // Sensor ODR is variant-dependent (set in UpdateVariantRegisterConfig()): + // default (16X / DSK320X): 2000 Hz (HAODR_SEL=01) + // LSM6DSV32X / 80X / 320X: 7680 Hz (HAODR_SEL=00) + // Every variant batches 2 FIFO words per period: gyro + low-g accel. + uint32_t _sensor_odr{GYRO_ODR}; + float _fifo_sample_dt{1e6f / GYRO_ODR}; + uint8_t _fifo_words_per_period{2}; struct register_config_t { Register reg; @@ -95,7 +130,7 @@ private: void RegisterWrite(Register reg, uint8_t value); void RegisterSetAndClearBits(Register reg, uint8_t setbits, uint8_t clearbits); - bool FIFORead(const hrt_abstime ×tamp_sample, uint16_t samples); + bool FIFORead(const hrt_abstime ×tamp_sample, uint16_t words); void FIFOReset(); void UpdateTemperature(); @@ -122,6 +157,7 @@ private: hrt_abstime _last_config_check_timestamp{0}; hrt_abstime _temperature_update_timestamp{0}; int _failure_count{0}; + uint8_t _checked_register{0}; px4::atomic _drdy_timestamp_sample{0}; bool _data_ready_interrupt_enabled{false}; @@ -138,30 +174,55 @@ private: LSM6DSV16X, LSM6DSV32X, LSM6DSK320X, + LSM6DSV80X, + LSM6DSV320X, }; DeviceVariant _device_variant{DeviceVariant::LSM6DSV16X}; + // The LSM6DSV80X and LSM6DSV320X share WHO_AM_I 0x73 and cannot be distinguished over SPI, so + // the physically-installed part is selected explicitly at start via the -T argument (config.custom1): + // 320 -> LSM6DSV320X, anything else (incl. 80 / unset) -> LSM6DSV80X. + const int _highg_variant_arg; + + // LSM6DSV80X / LSM6DSV320X: 7.68 kHz HAODR set and a ±4000 dps gyro. Their second, high-g + // accelerometer is left powered down: the ±16 g low-g channel is the published one. + // LSM6DSV32X publishes the UI channel at ±32 g / ±4000 dps / 7680 Hz (no separate high-g element). + // The 80X/320X high-g element is a sports-impact sensor (±1.5 g typ zero-g offset, ±2 mg/°C + // tempco against ±12 mg / ±0.07 mg/°C for low-g) and is no use as a flight accelerometer. + bool _dsv80x_family{false}; + uint16_t _fifo_empty_interval_us{500}; // default 500 us / 2000 Hz int32_t _fifo_gyro_samples{static_cast(_fifo_empty_interval_us / (1000000 / GYRO_ODR))}; - uint8_t _checked_register{0}; static constexpr uint8_t size_register_cfg{12}; + // Variant-dependent fields (HAODR_CFG, CTRL1/2/6/8, FIFO_CTRL3) are overwritten in + // UpdateVariantRegisterConfig(); initializers below are the default-variant (2000 Hz) values. + // + // Configure() writes these in array order, and the order is significant: FS_G (CTRL6) must be + // set while the gyro is in power-down, and the reset default FS_G=000 is reserved on the 80X / + // 320X / DSK320X. So every full-scale, filter and FIFO register comes first, and CTRL1/CTRL2 — + // which set the ODRs and thereby power the sensors up — come last. HAODR_CFG selects the ODR + // set that the CTRL1/CTRL2 codes index into, so it must also precede them. register_config_t _register_cfg[size_register_cfg] { // Register | Set bits | Clear bits { Register::CTRL3, CTRL3_BIT::BDU | CTRL3_BIT::IF_INC, CTRL3_BIT::SW_RESET }, { Register::HAODR_CFG, HAODR_CFG_BIT::HAODR_MODE1, 0 }, - { Register::CTRL1, HAODR_MODE1_ODR_2000HZ | CTRL1_BIT::CTRL1_MODE_HAODR, 0 }, - { Register::CTRL2, HAODR_MODE1_ODR_2000HZ | CTRL2_BIT::CTRL2_MODE_HAODR, 0 }, { Register::CTRL6, CTRL6_BIT::FS_G_2000DPS, 0 }, { Register::CTRL8, CTRL8_BIT::FS_XL_16G | CTRL8_BIT::LPF2_BW_ODR_DIV_10, 0 }, - { Register::CTRL9, CTRL9_BIT::LPF2_XL_EN, 0 }, + { Register::CTRL9, CTRL9_BIT::LPF2_XL_EN, CTRL9_BIT::HP_SLOPE_XL_EN }, + { Register::CTRL4, CTRL4_BIT::DRDY_PULSED, 0 }, + { Register::INT1_CTRL, INT1_CTRL_BIT::INT1_FIFO_TH, 0 }, + { Register::FIFO_CTRL1, 0, 0 }, // WTM[7:0] set at runtime by ConfigureFIFOWatermark() { Register::FIFO_CTRL3, static_cast(FIFO_CTRL3_BIT::BDR_GY_HAODR) | static_cast(FIFO_CTRL3_BIT::BDR_XL_HAODR), 0 }, - { Register::FIFO_CTRL4, FIFO_CTRL4_BIT::FIFO_MODE_CONTINUOUS, 0 }, - { Register::INT1_CTRL, INT1_CTRL_BIT::INT1_FIFO_TH, 0 }, - { Register::CTRL4, CTRL4_BIT::DRDY_PULSED, 0 }, - { Register::FIFO_CTRL1, 0, 0 }, // WTM[7:0] set at runtime by ConfigureFIFOWatermark() + { + Register::FIFO_CTRL4, FIFO_CTRL4_BIT::FIFO_MODE_CONTINUOUS, + (FIFO_CTRL4_BIT::FIFO_MODE_MASK & ~FIFO_CTRL4_BIT::FIFO_MODE_CONTINUOUS) | + FIFO_CTRL4_BIT::ODR_T_BATCH_MASK | FIFO_CTRL4_BIT::DEC_TS_BATCH_MASK + }, + { Register::CTRL1, HAODR_MODE1_ODR_2000HZ | CTRL1_BIT::CTRL1_MODE_HAODR, 0 }, + { Register::CTRL2, HAODR_MODE1_ODR_2000HZ | CTRL2_BIT::CTRL2_MODE_HAODR, 0 }, }; }; diff --git a/src/drivers/imu/st/lsm6dsv/ST_LSM6DSV_Registers.hpp b/src/drivers/imu/st/lsm6dsv/ST_LSM6DSV_Registers.hpp index a5051819613..15dc2ec2188 100644 --- a/src/drivers/imu/st/lsm6dsv/ST_LSM6DSV_Registers.hpp +++ b/src/drivers/imu/st/lsm6dsv/ST_LSM6DSV_Registers.hpp @@ -55,20 +55,29 @@ static constexpr uint8_t Bit7 = (1 << 7); namespace ST_LSM6DSV { -static constexpr uint32_t SPI_SPEED = 8 * 1000 * 1000; // 8 MHz SPI data clock +static constexpr uint32_t SPI_SPEED = 10 * 1000 * 1000; // 10 MHz SPI data clock (device max) static constexpr uint8_t DIR_READ = 0x80; static constexpr uint8_t WHO_AM_I_ID = 0x70; // LSM6DSV16X and LSM6DSV32X (same ID) static constexpr uint8_t WHO_AM_I_DSK320X = 0x75; // LSM6DSK320X (unique ID) +// The LSM6DSV80X and LSM6DSV320X share this ID and have an identical register map; they differ +// only in their high-g channel's full-scale table and cannot be distinguished over SPI (no part-ID +// register; only the MIPI I3C Provisioned ID differs, which is not SPI-accessible). The variant is +// therefore selected explicitly at driver start (-T 80 | -T 320) so the device type is right. +static constexpr uint8_t WHO_AM_I_HIGHG = 0x73; // LSM6DSV80X / LSM6DSV320X (shared ID) -// HAODR mode-1 ODR: 2000 Hz +// Default-variant (16X / DSK320X) ODR: HAODR_SEL=01, code 0x0A = 2000 Hz static constexpr uint32_t GYRO_ODR = 2000; static constexpr uint32_t ACCEL_ODR = 2000; -// HAODR mode-1 ODR codes (written to CTRL1/CTRL2 [3:0]) +// HAODR mode-1 (HAODR_SEL=01) ODR code (written to CTRL1/CTRL2 [3:0] and FIFO BDR [3:0]) static constexpr uint8_t HAODR_MODE1_ODR_2000HZ = 0x0A; +// HAODR_SEL=00, code 0x0C = 7680 Hz (32X / 80X / 320X) +static constexpr uint32_t ODR_DSV80X = 7680; +static constexpr uint8_t HAODR_SEL0_ODR_7680HZ = 0x0C; + enum class Register : uint8_t { IF_CFG = 0x03, // Interrupt polarity and output mode @@ -109,6 +118,8 @@ enum class Register : uint8_t { // CTRL1 — Accelerometer control (ODR + operating mode + HAODR flag) enum CTRL1_BIT : uint8_t { + ODR_XL_MASK = 0x0F, + OP_MODE_XL_MASK = 0x70, // ODR_XL [3:0] — set via HAODR mode-1 code 0x0A for 2000 Hz // OP_MODE_XL [2:0] in bits [6:4]: 0b000 = high-performance mode CTRL1_MODE_HAODR = Bit4, // bit 4 must be set for HAODR selection @@ -116,6 +127,8 @@ enum CTRL1_BIT : uint8_t { // CTRL2 — Gyroscope control (ODR + operating mode + HAODR flag) enum CTRL2_BIT : uint8_t { + ODR_G_MASK = 0x0F, + OP_MODE_G_MASK = 0x70, CTRL2_MODE_HAODR = Bit4, }; @@ -146,18 +159,24 @@ enum CTRL4_BIT : uint8_t { // CTRL6 — Gyroscope full-scale enum CTRL6_BIT : uint8_t { // FS_G [3:0] - FS_G_2000DPS = 0x04, // ±2000 dps (16X / 32X) + FS_G_MASK = 0x0F, + FS_G_2000DPS = 0x04, // ±2000 dps (16X / 32X), FS_G=0100 + FS_G_4000DPS_DSV32X = 0x0C, // ±4000 dps (16X / 32X), FS_G=1100; OIS gyro chain must stay off FS_G_2000DPS_DSK320X = 0x0C, // ±2000 dps with bit3=1 for DSK320X (0x04 | Bit3) + FS_G_4000DPS_HIGHG = 0x0D, // ±4000 dps for 80X/320X (FS_G=101 | bit3); CTRL6 bit3 must be 1 }; // CTRL8 — Accelerometer full-scale + LPF2 bandwidth enum CTRL8_BIT : uint8_t { - // bit2 is hardware-reserved and differs by variant: - // 16X / DSK320X = 0 (value OR'd with FS_XL) - // 32X = 1 (must be preserved) - // FS_XL [1:0] in bits [1:0] - FS_XL_16G = 0x03, // ±16 g for 16X / DSK320X (bit2=0) + FS_XL_MASK = 0x03, + LPF2_BW_MASK = 0xE0, + // LSM6DSV32X CTRL8: bit2 must be 1, bit4 must be 0, XL_DualC_EN (bit3) selects dual-channel. + // FS_XL [1:0] in bits [1:0]: 00=±4 g, 01=±8 g, 10=±16 g, 11=±32 g + // Other variants: bit2 is 0; FS_XL 11 = ±16 g + FS_XL_16G = 0x03, // ±16 g for 16X / DSK320X / 80X (bit2=0, FS_XL=11) FS_XL_16G_DSV32X = 0x06, // ±16 g for 32X (bit2=1, FS_XL=10) + FS_XL_32G_DSV32X = 0x07, // ±32 g for 32X (bit2=1, FS_XL=11) + XL_DualC_EN = Bit3, // 32X dual-channel (leave 0: UI chain at FS_XL) // HP_LPF2_XL_BW [2:0] in bits [7:5] — when LPF2 enabled via CTRL9 LPF2_BW_ODR_DIV_10 = Bit5, // 0x20 → ODR/10 @@ -165,6 +184,7 @@ enum CTRL8_BIT : uint8_t { // CTRL9 enum CTRL9_BIT : uint8_t { + HP_SLOPE_XL_EN = Bit4, // Select high-pass instead of low-pass filtering LPF2_XL_EN = Bit3, // Enable accelerometer LPF2 }; @@ -178,13 +198,19 @@ enum STATUS_REG_BIT : uint8_t { // FIFO_CTRL3 — Batch Data Rate for accel and gyro enum FIFO_CTRL3_BIT : uint8_t { // BDR_GY [3:0] in bits [7:4], BDR_XL [3:0] in bits [3:0] - // Set both to HAODR mode-1 code = 0x0A + // Default variants: HAODR mode-1 code 0x0A (2000 Hz) BDR_XL_HAODR = HAODR_MODE1_ODR_2000HZ, BDR_GY_HAODR = HAODR_MODE1_ODR_2000HZ << 4, + // LSM6DSV80X / 320X: code 0x0C (7680 Hz) + BDR_XL_7680 = HAODR_SEL0_ODR_7680HZ, + BDR_GY_7680 = HAODR_SEL0_ODR_7680HZ << 4, }; // FIFO_CTRL4 — FIFO mode enum FIFO_CTRL4_BIT : uint8_t { + FIFO_MODE_MASK = 0x07, + ODR_T_BATCH_MASK = 0x30, + DEC_TS_BATCH_MASK = 0xC0, FIFO_MODE_BYPASS = 0x00, FIFO_MODE_CONTINUOUS = 0x06, // Continuous mode }; @@ -199,13 +225,15 @@ enum FIFO_STATUS2_BIT : uint8_t { // HAODR_CFG enum HAODR_CFG_BIT : uint8_t { - HAODR_MODE1 = 0x01, // Enable HAODR mode-1 + HAODR_SEL_MASK = 0x03, // HAODR_SEL [1:0] + HAODR_MODE1 = 0x01, // HAODR_SEL=01 (2000 Hz ODR set) + // HAODR_SEL=00 (1920/3840/7680 Hz ODR set) is the default (0x00) }; // FIFO tag IDs (upper 5 bits of FIFO_DATA_OUT_TAG >> 3) enum class FifoTag : uint8_t { GYRO_NC = 0x01, - ACCEL_NC = 0x02, + ACCEL_NC = 0x02, // low-g accelerometer (the only one batched; the 80X / 320X high-g channel stays off) TEMPERATURE = 0x03, TIMESTAMP = 0x04, }; @@ -214,9 +242,11 @@ namespace FIFO { // FIFO word: 1-byte tag + 6-byte data = 7 bytes static constexpr size_t WORD_SIZE = 7; -// Max samples to drain per poll (avoid blocking scheduler) +// Words batched per sample period: gyro + low-g accel +static constexpr size_t MAX_WORDS_PER_PERIOD = 2; +// Max sample periods to drain per poll (avoid blocking scheduler) static constexpr size_t MAX_DRAIN_SAMPLES = 32; -// FIFO depth: 512 words max on LSM6DSV +// Ceiling of the DIFF_FIFO word counter. The buffer itself is 1.5 KB, i.e. ~219 uncompressed words. static constexpr size_t DEPTH = 512; } diff --git a/src/drivers/imu/st/lsm6dsv/lsm6dsv_main.cpp b/src/drivers/imu/st/lsm6dsv/lsm6dsv_main.cpp index f0b4cbe469a..273977a1321 100644 --- a/src/drivers/imu/st/lsm6dsv/lsm6dsv_main.cpp +++ b/src/drivers/imu/st/lsm6dsv/lsm6dsv_main.cpp @@ -43,6 +43,7 @@ void LSM6DSV::print_usage() PRINT_MODULE_USAGE_COMMAND("start"); PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true); PRINT_MODULE_USAGE_PARAM_INT('R', 0, 0, 35, "Rotation", true); + PRINT_MODULE_USAGE_PARAM_INT('T', 0, 0, 320, "High-g variant for WHO_AM_I 0x73 (80 = LSM6DSV80X, 320 = LSM6DSV320X)", true); PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); } @@ -53,11 +54,17 @@ extern "C" int lsm6dsv_main(int argc, char *argv[]) BusCLIArguments cli{false, true}; cli.default_spi_frequency = SPI_SPEED; - while ((ch = cli.getOpt(argc, argv, "R:")) != EOF) { + while ((ch = cli.getOpt(argc, argv, "R:T:")) != EOF) { switch (ch) { case 'R': cli.rotation = (enum Rotation)atoi(cli.optArg()); break; + + case 'T': + // High-g variant selector for the shared WHO_AM_I 0x73 (80 = LSM6DSV80X, 320 = LSM6DSV320X). + // Set per board in rc.sensors to match the physically-installed part. + cli.custom1 = atoi(cli.optArg()); + break; } }