From 32c669a38d7f7d50836ee5ca8ba9d5d574e19eee Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 11 Dec 2021 17:23:12 +0100 Subject: mpu9250: fix casting / sign extension on 32bit systems --- src/driver/mpu9250.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/driver') diff --git a/src/driver/mpu9250.cc b/src/driver/mpu9250.cc index bef086f..de47597 100644 --- a/src/driver/mpu9250.cc +++ b/src/driver/mpu9250.cc @@ -5,6 +5,7 @@ #else #include "driver/soft_i2c.h" #endif +#include /* Copyright (c) 2016 SparkFun Electronics, Inc. @@ -143,7 +144,7 @@ signed int MPU9250::getWordReg(unsigned char const regBase) txbuf[0] = regBase; i2c.xmit(address, 1, txbuf, 2, rxbuf); - return ((signed int)rxbuf[0] << 8) + rxbuf[1]; + return (int16_t)((rxbuf[0] << 8) + rxbuf[1]); } float MPU9250::getTemperature() @@ -156,9 +157,9 @@ void MPU9250::getRawGyro(int *x, int *y, int *z) txbuf[0] = 67; i2c.xmit(address, 1, txbuf, 6, rxbuf); - *x = ((signed int) rxbuf[0] << 8) + rxbuf[1]; - *y = ((signed int) rxbuf[2] << 8) + rxbuf[3]; - *z = ((signed int) rxbuf[4] << 8) + rxbuf[5]; + *x = (int16_t)(( rxbuf[0] << 8) + rxbuf[1]); + *y = (int16_t)(( rxbuf[2] << 8) + rxbuf[3]); + *z = (int16_t)(( rxbuf[4] << 8) + rxbuf[5]); } void MPU9250::getRawAccel(int *x, int *y, int *z) @@ -166,9 +167,9 @@ void MPU9250::getRawAccel(int *x, int *y, int *z) txbuf[0] = 59; i2c.xmit(address, 1, txbuf, 6, rxbuf); - *x = ((signed int) rxbuf[0] << 8) + rxbuf[1]; - *y = ((signed int) rxbuf[2] << 8) + rxbuf[3]; - *z = ((signed int) rxbuf[4] << 8) + rxbuf[5]; + *x = (int16_t)(( rxbuf[0] << 8) + rxbuf[1]); + *y = (int16_t)(( rxbuf[2] << 8) + rxbuf[3]); + *z = (int16_t)(( rxbuf[4] << 8) + rxbuf[5]); } void MPU9250::getAccel(float *g_x, float *g_y, float *g_z) @@ -211,9 +212,9 @@ bool MPU9250::getRawMagnet(int *x, int *y, int *z) i2c.xmit(0x0c, 1, txbuf, 8, rxbuf); if ((rxbuf[0] & 0x01) && !(rxbuf[7] & 0x08)) { - *x = ((signed int)rxbuf[2] << 8) + rxbuf[1]; - *y = ((signed int)rxbuf[4] << 8) + rxbuf[3]; - *z = ((signed int)rxbuf[6] << 8) + rxbuf[5]; + *x = (int16_t)((rxbuf[2] << 8) + rxbuf[1]); + *y = (int16_t)((rxbuf[4] << 8) + rxbuf[3]); + *z = (int16_t)((rxbuf[6] << 8) + rxbuf[5]); return true; } else { -- cgit v1.2.3