Skip to content

Conversation

@gmaz-phoesys
Copy link
Contributor

Description

These commits contain changes providing support for generic ms5xxx barometric sensors' series. New differential barometer type MS5525 is also added.

Motivation and Context

As new sensors from ms5xxx series appear in our projects it is worth to extract shared part into separate files.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (list targets here).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.


return 0;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[clang-format-pr] reported by reviewdog 🐶
suggested fix

Suggested change

/*
* Phoenix-RTOS
*
* Common part of TE Connectivity MS5xxx series barometric sensors.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[codespell] reported by reviewdog 🐶
TE ==> THE, BE, WE, TO

/*
* Phoenix-RTOS
*
* Common part of TE Connectivity MS5xxx series barometric sensors.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[codespell] reported by reviewdog 🐶
TE ==> THE, BE, WE, TO

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @gmaz-phoesys, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

Hello! Gemini here, providing a summary of this pull request to help everyone quickly understand the changes.

This PR aims to improve the sensor drivers for TE Connectivity MS5xxx series barometric sensors by extracting common functionality into shared files. This refactoring makes it easier to add support for new sensors in the series and reduces code duplication. Additionally, this PR adds a new driver specifically for the MS5525 differential barometric sensor.

Highlights

  • Common MS5xxx Code: Introduces new files (common.c and common.h) containing shared functions and definitions for MS5xxx series sensors, such as CRC calculation, measurement routines, and hardware setup (reset, PROM reading, validation).
  • New MS5525 Driver: Adds a new driver (ms5525.c) for the MS5525 differential barometric sensor, utilizing the newly created common MS5xxx functions for initialization and measurement.
  • MS5611 Driver Refactoring: Refactors the existing MS5611 driver (ms5611.c) to use the common MS5xxx functions, removing duplicate code for hardware setup and measurement.

Changelog

Click here to see the changelog
  • sensors/baro/ms5xxx/common.c
    • New file: Implements common functions for MS5xxx sensors, including PROM CRC calculation (ms5xxx_crc4), ADC measurement (ms5xxx_measure), and hardware initialization (ms5xxx_hwSetup).
  • sensors/baro/ms5xxx/common.h
    • New file: Defines common commands (reset, conversion, read ADC, read PROM), OSR delays, the ms5xxx_ctx_t context structure, and declares the common functions.
  • sensors/baro/ms5xxx/ms5525.c
    • New file: Adds the driver for the MS5525 differential barometric sensor.
    • Implements ms5525_publishthr to read pressure and temperature, perform calculations using sensor-specific coefficients, and publish SENSOR_TYPE_DIFFBARO events.
    • Implements ms5525_alloc and ms5525_start for sensor registration and initialization, using ms5xxx_hwSetup.
  • sensors/baro/ms5xxx/ms5611.c
    • Refactored to include common.h.
    • Updated ms5611_publishthr to use ms5xxx_measure for reading ADC values.
    • Updated ms5611_alloc to use ms5xxx_hwSetup for hardware initialization.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces common support for MS5xxx series barometric sensors and adds a new driver for the MS5525DSO differential pressure sensor. The refactoring into common files (common.c, common.h) is a good step towards modularity and reusability. The new MS5525 driver and updates to the MS5611 driver leverage this common code.

I've found a few issues, including a critical one related to data structure access in the MS5525 driver, and some high-severity concerns regarding SPI mode configuration and SPI function usage. There are also some medium-severity points about hardcoded values that might affect the generic applicability of the MS5525 driver.

Overall, the direction is good, but these issues should be addressed to ensure correctness and robustness.

Summary of Findings

  • Critical Data Structure Access: In ms5525.c, sensor_event_t members are accessed incorrectly (e.g., ctx->evtDiaro instead of ctx->evt.diffBaro), which will lead to undefined behavior or crashes.
  • SPI Mode Configuration: Both MS5525 and MS5611 drivers are configured for SPI_MODE3. Datasheets for these sensors specify SPI_MODE0. This mismatch can cause communication failures.
  • SPI Function Usage for Reset: In common.c, the spimsg_xfer function might be used incorrectly for the sensor reset command due to how spiCtx.oid is populated. Using sensorsspi_xfer is recommended for consistency.
  • Hardcoded Values in MS5525 Driver: The MS5525 driver uses a hardcoded static pressure and a pressure range check specific to a high-pressure variant, which may limit its generic applicability.
  • Copyright Year: The copyright year in new files is set to 2025. (Not commented due to review settings)
  • Comment Typo: In ms5xxx_crc4 (common.c), there's a typo: "crc reminder" should be "crc remainder". (Not commented due to review settings)
  • Type Usage for Microsecond Delays: The ms5xxx_measure function uses time_t for the usDelay parameter, which is intended to be microseconds. While this might work, using useconds_t or uint32_t would be more conventional and clearer. (Not commented due to review settings)

Merge Readiness

This pull request makes good progress in generalizing MS5xxx sensor support. However, due to the identified critical and high-severity issues, particularly the incorrect data structure access in ms5525.c and the SPI mode configurations, the code is not yet ready for merging. These issues must be addressed to ensure correct and reliable sensor operation. The medium-severity concerns regarding hardcoded values in the MS5525 driver should also be considered for improved reusability. I am unable to approve this pull request in its current state; please address the feedback and have the changes reviewed again.

Comment on lines 89 to 93
ctx->evtDiaro.timestamp = now;
ctx->evtDiffBaro.diffBaro.pressStatic = 101325;
ctx->evtDiffBaro.diffBaro.pressTotal = 101325 + press;

sensorspublish(info->id, &ctx->evtDiffBaro);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There seems to be a mix-up in accessing the sensor_event_t structure. The context ctx has a member evt of type sensor_event_t. When ctx->evt.type is SENSOR_TYPE_DIFFBARO, you should access its members like ctx->evt.timestamp and ctx->evt.diffBaro.pressStatic, etc.

The current usage of ctx->evtDiaro.timestamp and ctx->evtDiffBaro.diffBaro... will likely lead to incorrect memory access, data corruption, or crashes as evtDiaro and evtDiffBaro are not members of ms5xxx_ctx_t.

It should be:

ctx->evt.timestamp = now;
ctx->evt.diffBaro.pressStatic = 101325;
ctx->evt.diffBaro.pressTotal = 101325 + press;

sensors_publish(info->id, &ctx->evt);
			ctx->evt.timestamp = now;
			ctx->evt.diffBaro.pressStatic = 101325; /* Assuming standard atmospheric pressure for static component */
			ctx->evt.diffBaro.pressTotal = 101325 + press;

			sensors_publish(info->id, &ctx->evt);


/* Reset ms5525 sequence */
cmd = CMD_RESET;
if (spimsg_xfer(&ctx->spiCtx, &cmd, sizeof(cmd), NULL, 0, 0) < 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The spimsg_xfer function typically expects ctx->oid within spimsg_ctx_t to be the specific slave device OID. However, ctx->spiCtx.oid is populated by sensorsspi_open with the SPI bus controller OID. This might lead to the reset command not being correctly addressed to the sensor slave.

Consider using sensorsspi_xfer for consistency and clarity, as it's designed to work with spiCtx (containing bus OID, mode, speed) and a separate spiSS (slave OID). It would look like:
sensorsspi_xfer(&ctx->spiCtx, &ctx->spiSS, &cmd, sizeof(cmd), NULL, 0, 0)

Could you clarify if spimsg_xfer is intended to be used this way, or if sensorsspi_xfer would be more appropriate here?

Suggested change
if (spimsg_xfer(&ctx->spiCtx, &cmd, sizeof(cmd), NULL, 0, 0) < 0) {
if (sensorsspi_xfer(&ctx->spiCtx, &ctx->spiSS, &cmd, sizeof(cmd), NULL, 0, 0) < 0) {

info->ctx = ctx;
info->types = SENSOR_TYPE_DIFFBARO;

ctx->spiCtx.mode = SPI_MODE3;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

MS5xxx series sensors, including the MS5525, typically use SPI Mode 0 (CPOL=0, CPHA=0). Using SPI_MODE3 (CPOL=1, CPHA=1) might lead to communication errors or unreliable behavior depending on the SPI controller and sensor timing tolerances.

Could you verify the required SPI mode for MS5525 from its datasheet and adjust this accordingly? It's highly recommended to use the mode specified by the manufacturer.

	ctx->spiCtx.mode = SPI_MODE0;

info->ctx = ctx;
info->types = SENSOR_TYPE_BARO;

ctx->spiCtx.mode = SPI_MODE3;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the MS5525, the MS5611 sensor typically uses SPI Mode 0 (CPOL=0, CPHA=0) as per its datasheet. Configuring SPI_MODE3 (CPOL=1, CPHA=1) might cause communication issues.

Please confirm the correct SPI mode for MS5611 and update if necessary. Adhering to the datasheet specification is crucial for reliable operation.

	ctx->spiCtx.mode = SPI_MODE0;

press = ((((int64_t)D1 * sens) >> 21) - off) >> 15; /* P = ((D1 * SENS) / 2^21 - OFF) / 2^15 */

/* MS5525DSO operating ranges: temperature from -40C to +80C, and pressure: 45000Pa, 110000 Pa */
if (press > -206842 && press < 206842 && temp > -4000 && temp < 8500) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pressure range check press > -206842 && press < 206842 (approx +/- 30 PSI) is specific to certain MS5525DSO variants (e.g., MS5525DSO-DS030DS-N). If this driver is intended for generic MS5525DSO support, this hardcoded range might be too restrictive for lower-pressure variants or too permissive if a lower-pressure variant is mistakenly used.

Could this range be made configurable, or perhaps derived from PROM coefficients if possible (though unlikely for range itself)? If this driver targets a specific variant, a comment clarifying this would be helpful.

/* MS5525DSO operating ranges: temperature from -40C to +80C, and pressure: 45000Pa, 110000 Pa */
if (press > -206842 && press < 206842 && temp > -4000 && temp < 8500) {
ctx->evtDiaro.timestamp = now;
ctx->evtDiffBaro.diffBaro.pressStatic = 101325;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The static pressure pressStatic is hardcoded to 101325 Pa (standard atmospheric pressure). For a differential pressure sensor, the meaning of 'static' and 'total' pressure depends on the application. If this driver is for general use, this hardcoding might not always be appropriate.

Is this value always applicable for the intended use cases, or should it be configurable or obtained from another source?

@gmaz-phoesys gmaz-phoesys marked this pull request as draft June 9, 2025 13:39
@github-actions
Copy link

github-actions bot commented Jun 9, 2025

Unit Test Results

8 030 tests  ±0   7 506 ✅ ±0   40m 27s ⏱️ -1s
  479 suites ±0     524 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit b66ea35. ± Comparison against base commit 1effaf1.

♻️ This comment has been updated with latest results.

@gmaz-phoesys gmaz-phoesys force-pushed the gmaz_phoesys/generic_ms5xxx branch from 6dcd103 to b66ea35 Compare June 10, 2025 09:04
@gmaz-phoesys gmaz-phoesys requested a review from niewim19 June 10, 2025 09:05
@gmaz-phoesys gmaz-phoesys marked this pull request as ready for review June 10, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants