From 165319c3bd3f50a39b0b053b2ddf3f3aed5bbbab Mon Sep 17 00:00:00 2001 From: Matteo Golin Date: Wed, 15 Jan 2025 14:22:37 -0500 Subject: [PATCH] Fix CRC and test on Raspberry Pi Pico --- .../rp2040/common/src/rp2040_common_bringup.c | 16 +++++++++++ include/nuttx/sensors/msxxxx_crc4.h | 27 +++++++++---------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/boards/arm/rp2040/common/src/rp2040_common_bringup.c b/boards/arm/rp2040/common/src/rp2040_common_bringup.c index 136d4178d4810..e17dd8b9aa1a8 100644 --- a/boards/arm/rp2040/common/src/rp2040_common_bringup.c +++ b/boards/arm/rp2040/common/src/rp2040_common_bringup.c @@ -73,6 +73,11 @@ #include "rp2040_i2c.h" #endif +#ifdef CONFIG_SENSORS_MS56XX +#include +#include "rp2040_i2c.h" +#endif + #ifdef CONFIG_SENSORS_MAX6675 #include #include "rp2040_max6675.h" @@ -508,6 +513,17 @@ int rp2040_common_bringup(void) } #endif + +#ifdef CONFIG_SENSORS_MS56XX + /* Try to register MS56xx device at I2C0 */ + ret = ms56xx_register(rp2040_i2cbus_initialize(0), 0, MS56XX_ADDR0, + MS56XX_MODEL_MS5611); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: couldn't register MS5611: %d\n", ret); + } +#endif + #ifdef CONFIG_VIDEO_FB ret = fb_register(0, 0); if (ret < 0) diff --git a/include/nuttx/sensors/msxxxx_crc4.h b/include/nuttx/sensors/msxxxx_crc4.h index e1bf0b6cc723b..0101e34f969e3 100644 --- a/include/nuttx/sensors/msxxxx_crc4.h +++ b/include/nuttx/sensors/msxxxx_crc4.h @@ -43,42 +43,41 @@ static uint8_t msxxxx_crc4(FAR uint16_t *src, uint8_t crcndx, uint16_t crcmask) { - uint16_t cnt; - uint16_t n_rem; - uint16_t crc_read; + uint16_t cnt; /* simple counter */ + uint16_t n_rem; /* crc reminder */ + uint16_t crc_read; /* original value of the crc */ uint8_t n_bit; n_rem = 0x00; - crc_read = src[crcndx]; - src[crcndx] &= ~crcmask; + crc_read = src[crcndx]; /* save read CRC */ + src[crcndx] &= ~crcmask; /* CRC byte is replaced by 0 */ - for (cnt = 0; cnt < 16; cnt++) + for (cnt = 0; cnt < 16; cnt++) /* operation is performed on bytes */ { if (cnt % 2 == 1) { - n_rem ^= src[cnt >> 1] & 0x00ff; + n_rem ^= (uint16_t)((src[cnt >> 1]) & 0x00ff); } else { - n_rem ^= src[cnt >> 1] >> 8; + n_rem ^= (uint16_t)(src[cnt >> 1] >> 8); } for (n_bit = 8; n_bit > 0; n_bit--) { - if (n_rem & (0x8000) != 0) + if (n_rem & (0x8000)) { n_rem = (n_rem << 1) ^ 0x3000; } else { - n_rem <<= 1; + n_rem = (n_rem << 1); } } } - - n_rem = (n_rem >> 12) & 0x000f; - src[crcndx] = crc_read; - return n_rem ^ 0x00; + n_rem = (0x000f & (n_rem >> 12)); /* final 4-bit reminder is CRC code */ + src[crcndx] = crc_read; /* restore the crc_read to its original place */ + return (n_rem ^ 0x00); } #endif /* __INCLUDE_NUTTX_SENSORS_MSXXXX_CRC4_H */