sensors/msxxxx-crc4: Modify CRC calculation so that it passes with MS5611.

This commit is contained in:
Matteo Golin 2025-01-15 14:22:37 -05:00 committed by Xiang Xiao
parent 67582acc14
commit 5de7e240af
2 changed files with 28 additions and 13 deletions

View file

@ -80,6 +80,11 @@
#include "rp2040_i2c.h"
#endif
#ifdef CONFIG_SENSORS_MS56XX
#include <nuttx/sensors/ms56xx.h>
#include "rp2040_i2c.h"
#endif
#ifdef CONFIG_SENSORS_MAX6675
#include <nuttx/sensors/max6675.h>
#include "rp2040_max6675.h"
@ -553,6 +558,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)

View file

@ -44,41 +44,40 @@ static uint8_t msxxxx_crc4(FAR uint16_t *src,
uint16_t crcmask)
{
uint16_t cnt;
uint16_t n_rem;
uint16_t crc_read;
uint16_t n_rem; /* CRC remainder */
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 */