From 7a39f0398629a7ffd14d81234c1a673513d96756 Mon Sep 17 00:00:00 2001 From: Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de> Date: Fri, 5 Oct 2018 14:35:10 +0200 Subject: [PATCH] drivers/adxl345: Fix byte swap on reading function --- drivers/adxl345/adxl345.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/adxl345/adxl345.c b/drivers/adxl345/adxl345.c index b9a568671d..60dd4c5d05 100644 --- a/drivers/adxl345/adxl345.c +++ b/drivers/adxl345/adxl345.c @@ -23,6 +23,7 @@ #include "assert.h" #include "periph/i2c.h" +#include "byteorder.h" #include "adxl345.h" #include "adxl345_regs.h" #include "adxl345_params.h" @@ -83,9 +84,11 @@ void adxl345_read(const adxl345_t *dev, adxl345_data_t *data) i2c_read_regs(ADXL345_BUS, ADXL345_ADDR, ADXL345_DATA_X0, (void *)result, 6, 0); i2c_release(ADXL345_BUS); -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + /* ADXL345 returns value in little endian, so swap is needed on big endian + * platforms. See Analog Devices ADXL345 datasheet page 27. */ +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ for (int i = 0; i < 3; i++) { - result[i] = ((result[i] & 0xFF) << 8 | (result[i] >> 8)); + result[i] = byteorder_swaps((uint16_t)result[i]); } #endif -- GitLab