ble_mesh: Add 24 bit variants [Zephyr]

This adds sys_*_*24 functions that operation on a 24 bits values.
This commit is contained in:
lly 2020-03-26 09:50:07 +08:00
parent e322c5950b
commit fd17ff2f0b

View file

@ -72,6 +72,12 @@ extern "C" {
#define __bswap_16(x) ((u16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) #define __bswap_16(x) ((u16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
#endif #endif
#ifndef __bswap_24
#define __bswap_24(x) ((u32_t) ((((x) >> 16) & 0xff) | \
(((x)) & 0xff00) | \
(((x) & 0xff) << 16)))
#endif
#ifndef __bswap_32 #ifndef __bswap_32
#define __bswap_32(x) ((u32_t) ((((x) >> 24) & 0xff) | \ #define __bswap_32(x) ((u32_t) ((((x) >> 24) & 0xff) | \
(((x) >> 8) & 0xff00) | \ (((x) >> 8) & 0xff00) | \
@ -90,14 +96,114 @@ extern "C" {
(((x) & 0xff) << 56))) (((x) & 0xff) << 56)))
#endif #endif
/** @def sys_le16_to_cpu
* @brief Convert 16-bit integer from little-endian to host endianness.
*
* @param val 16-bit integer in little-endian format.
*
* @return 16-bit integer in host endianness.
*/
/** @def sys_cpu_to_le16
* @brief Convert 16-bit integer from host endianness to little-endian.
*
* @param val 16-bit integer in host endianness.
*
* @return 16-bit integer in little-endian format.
*/
/** @def sys_le24_to_cpu
* @brief Convert 24-bit integer from little-endian to host endianness.
*
* @param val 24-bit integer in little-endian format.
*
* @return 24-bit integer in host endianness.
*/
/** @def sys_cpu_to_le24
* @brief Convert 24-bit integer from host endianness to little-endian.
*
* @param val 24-bit integer in host endianness.
*
* @return 24-bit integer in little-endian format.
*/
/** @def sys_le32_to_cpu
* @brief Convert 32-bit integer from little-endian to host endianness.
*
* @param val 32-bit integer in little-endian format.
*
* @return 32-bit integer in host endianness.
*/
/** @def sys_cpu_to_le32
* @brief Convert 32-bit integer from host endianness to little-endian.
*
* @param val 32-bit integer in host endianness.
*
* @return 32-bit integer in little-endian format.
*/
/** @def sys_be16_to_cpu
* @brief Convert 16-bit integer from big-endian to host endianness.
*
* @param val 16-bit integer in big-endian format.
*
* @return 16-bit integer in host endianness.
*/
/** @def sys_cpu_to_be16
* @brief Convert 16-bit integer from host endianness to big-endian.
*
* @param val 16-bit integer in host endianness.
*
* @return 16-bit integer in big-endian format.
*/
/** @def sys_be24_to_cpu
* @brief Convert 24-bit integer from big-endian to host endianness.
*
* @param val 24-bit integer in big-endian format.
*
* @return 24-bit integer in host endianness.
*/
/** @def sys_cpu_to_be24
* @brief Convert 24-bit integer from host endianness to big-endian.
*
* @param val 24-bit integer in host endianness.
*
* @return 24-bit integer in big-endian format.
*/
/** @def sys_be32_to_cpu
* @brief Convert 32-bit integer from big-endian to host endianness.
*
* @param val 32-bit integer in big-endian format.
*
* @return 32-bit integer in host endianness.
*/
/** @def sys_cpu_to_be32
* @brief Convert 32-bit integer from host endianness to big-endian.
*
* @param val 32-bit integer in host endianness.
*
* @return 32-bit integer in big-endian format.
*/
#define sys_le16_to_cpu(val) (val) #define sys_le16_to_cpu(val) (val)
#define sys_cpu_to_le16(val) (val) #define sys_cpu_to_le16(val) (val)
#define sys_be16_to_cpu(val) __bswap_16(val) #define sys_le24_to_cpu(val) (val)
#define sys_cpu_to_be16(val) __bswap_16(val) #define sys_cpu_to_le24(val) (val)
#define sys_le32_to_cpu(val) (val) #define sys_le32_to_cpu(val) (val)
#define sys_cpu_to_le32(val) (val) #define sys_cpu_to_le32(val) (val)
#define sys_le64_to_cpu(val) (val) #define sys_le64_to_cpu(val) (val)
#define sys_cpu_to_le64(val) (val) #define sys_cpu_to_le64(val) (val)
#define sys_be16_to_cpu(val) __bswap_16(val)
#define sys_cpu_to_be16(val) __bswap_16(val)
#define sys_be24_to_cpu(val) __bswap_24(val)
#define sys_cpu_to_be24(val) __bswap_24(val)
#define sys_be32_to_cpu(val) __bswap_32(val) #define sys_be32_to_cpu(val) __bswap_32(val)
#define sys_cpu_to_be32(val) __bswap_32(val) #define sys_cpu_to_be32(val) __bswap_32(val)
#define sys_be64_to_cpu(val) __bswap_64(val) #define sys_be64_to_cpu(val) __bswap_64(val)
@ -235,6 +341,21 @@ static inline void sys_put_be16(u16_t val, u8_t dst[2])
dst[1] = val; dst[1] = val;
} }
/**
* @brief Put a 24-bit integer as big-endian to arbitrary location.
*
* Put a 24-bit integer, originally in host endianness, to a
* potentially unaligned memory location in big-endian format.
*
* @param val 24-bit integer in host endianness.
* @param dst Destination memory address to store the result.
*/
static inline void sys_put_be24(u32_t val, u8_t dst[3])
{
dst[0] = val >> 16;
sys_put_be16(val, &dst[1]);
}
/** /**
* @brief Put a 32-bit integer as big-endian to arbitrary location. * @brief Put a 32-bit integer as big-endian to arbitrary location.
* *
@ -265,6 +386,21 @@ static inline void sys_put_le16(u16_t val, u8_t dst[2])
dst[1] = val >> 8; dst[1] = val >> 8;
} }
/**
* @brief Put a 24-bit integer as little-endian to arbitrary location.
*
* Put a 24-bit integer, originally in host endianness, to a
* potentially unaligned memory location in littel-endian format.
*
* @param val 24-bit integer in host endianness.
* @param dst Destination memory address to store the result.
*/
static inline void sys_put_le24(u32_t val, u8_t dst[3])
{
sys_put_le16(val, dst);
dst[2] = val >> 16;
}
/** /**
* @brief Put a 32-bit integer as little-endian to arbitrary location. * @brief Put a 32-bit integer as little-endian to arbitrary location.
* *
@ -310,6 +446,21 @@ static inline u16_t sys_get_be16(const u8_t src[2])
return ((u16_t)src[0] << 8) | src[1]; return ((u16_t)src[0] << 8) | src[1];
} }
/**
* @brief Get a 24-bit integer stored in big-endian format.
*
* Get a 24-bit integer, stored in big-endian format in a potentially
* unaligned memory location, and convert it to the host endianness.
*
* @param src Location of the big-endian 24-bit integer to get.
*
* @return 24-bit integer in host endianness.
*/
static inline u32_t sys_get_be24(const u8_t src[3])
{
return ((u32_t)src[0] << 16) | sys_get_be16(&src[1]);
}
/** /**
* @brief Get a 32-bit integer stored in big-endian format. * @brief Get a 32-bit integer stored in big-endian format.
* *
@ -340,6 +491,21 @@ static inline u16_t sys_get_le16(const u8_t src[2])
return ((u16_t)src[1] << 8) | src[0]; return ((u16_t)src[1] << 8) | src[0];
} }
/**
* @brief Get a 24-bit integer stored in big-endian format.
*
* Get a 24-bit integer, stored in big-endian format in a potentially
* unaligned memory location, and convert it to the host endianness.
*
* @param src Location of the big-endian 24-bit integer to get.
*
* @return 24-bit integer in host endianness.
*/
static inline u32_t sys_get_le24(const u8_t src[3])
{
return ((u32_t)src[2] << 16) | sys_get_le16(&src[0]);
}
/** /**
* @brief Get a 32-bit integer stored in little-endian format. * @brief Get a 32-bit integer stored in little-endian format.
* *