From fdfec5ea3cf2da3233dc6102aca3d5208a6964d8 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Mon, 5 Jul 2021 01:03:17 +0300 Subject: [PATCH] Fix swap macro (#2904) * convert swap macroses to inline functions * clean-up workarounds --- firmware/console/binary/tunerstudio.cpp | 4 +--- firmware/controllers/can/can_dash.cpp | 4 +--- firmware/util/efilib.h | 11 +++++++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 395a116c31..f5abe889ed 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -326,9 +326,7 @@ static void handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_e mode const char* start = getWorkingPageAddr() + offset; - uint32_t crc = crc32(start, count); - // do not pass function as macro parameter that would cause same code to be invoked 4 times! - crc = SWAP_UINT32(crc); + uint32_t crc = SWAP_UINT32(crc32(start, count)); tsChannel->sendResponse(mode, (const uint8_t *) &crc, 4); } diff --git a/firmware/controllers/can/can_dash.cpp b/firmware/controllers/can/can_dash.cpp index 9857aa97cc..2d0a439acc 100644 --- a/firmware/controllers/can/can_dash.cpp +++ b/firmware/controllers/can/can_dash.cpp @@ -155,9 +155,7 @@ void canMazdaRX8(CanCycle cycle) { float kph = getVehicleSpeed(); - int rpm = GET_RPM(); - // do not pass function as macro parameter that would cause same code to be invoked 2 times! - msg.setShortValue(SWAP_UINT16(rpm * 4), 0); + msg.setShortValue(SWAP_UINT16(GET_RPM() * 4), 0); msg.setShortValue(0xFFFF, 2); msg.setShortValue(SWAP_UINT16((int )(100 * kph + 10000)), 4); msg.setShortValue(0, 6); diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index a6aa454421..6e6c378111 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -13,9 +13,16 @@ // http://en.wikipedia.org/wiki/Endianness -#define SWAP_UINT16(x) (((x) << 8) | ((x) >> 8)) +static inline uint16_t SWAP_UINT16(uint16_t x) +{ + return ((x << 8) | (x >> 8)); +} -#define SWAP_UINT32(x) ((((x) >> 24) & 0xff) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) | (((x) << 24) & 0xff000000)) +static inline uint32_t SWAP_UINT32(uint32_t x) +{ + return (((x >> 24) & 0x000000ff) | ((x << 8) & 0x00ff0000) | + ((x >> 8) & 0x0000ff00) | ((x << 24) & 0xff000000)); +} #define BIT(n) (UINT32_C(1) << (n))