ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers common_functions.h Source File

common_functions.h

00001 /*
00002  * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef COMMON_FUNCTIONS_H_
00017 #define COMMON_FUNCTIONS_H_
00018 
00019 #include "ns_types.h"
00020 
00021 #ifdef __cplusplus
00022 extern "C" {
00023 #endif
00024 
00025 /*
00026  * Common write 64-bit variable to 8-bit pointer.
00027  *
00028  * Write 64 bits in big-endian (network) byte order.
00029  *
00030  * \param value 64-bit variable
00031  * \param ptr pointer where data to be written
00032  *
00033  * \return updated pointer
00034  */
00035 NS_INLINE uint8_t *common_write_64_bit(uint64_t value, uint8_t ptr[__static 8]);
00036 
00037 /*
00038  * Common read 64-bit variable from 8-bit pointer.
00039  *
00040  * Read 64 bits in big-endian (network) byte order.
00041  *
00042  * \param data_buf pointer where data to be read
00043  *
00044  * \return 64-bit variable
00045  */
00046 NS_INLINE uint64_t common_read_64_bit(const uint8_t data_buf[__static 8]);
00047 
00048 /*
00049  * Common write 32-bit variable to 8-bit pointer.
00050  *
00051  * Write 32 bits in big-endian (network) byte order.
00052  *
00053  * \param value 32-bit variable
00054  * \param ptr pointer where data to be written
00055  *
00056  * \return updated pointer
00057  */
00058 NS_INLINE uint8_t *common_write_32_bit(uint32_t value, uint8_t ptr[__static 4]);
00059 
00060 /*
00061  * Common read 32-bit variable from 8-bit pointer.
00062  *
00063  * Read 32 bits in big-endian (network) byte order.
00064  *
00065  * \param data_buf pointer where data to be read
00066  *
00067  * \return 32-bit variable
00068  */
00069 NS_INLINE uint32_t common_read_32_bit(const uint8_t data_buf[__static 4]);
00070 
00071 /*
00072  * Common write 32-bit variable to 8-bit pointer.
00073  *
00074  * Write 32 bits in little-endian byte order.
00075  *
00076  * \param value 32-bit variable
00077  * \param ptr pointer where data to be written
00078  *
00079  * \return updated pointer
00080  */
00081 NS_INLINE uint8_t *common_write_32_bit_inverse(uint32_t value, uint8_t ptr[__static 4]);
00082 
00083 /*
00084  * Common read 32-bit variable from 8-bit pointer.
00085  *
00086  * Read 32 bits in little-endian byte order.
00087  *
00088  * \param data_buf pointer where data to be read
00089  *
00090  * \return 32-bit variable
00091  */
00092 NS_INLINE uint32_t common_read_32_bit_inverse(const uint8_t data_buf[__static 4]);
00093 
00094 /*
00095  * Common write 24-bit variable to 8-bit pointer.
00096  *
00097  * Write 24 bits in big-endian (network) byte order.
00098  *
00099  * \param value 24-bit variable
00100  * \param ptr pointer where data to be written
00101  *
00102  * \return updated pointer
00103  */
00104 NS_INLINE uint8_t *common_write_24_bit(uint_fast24_t value, uint8_t ptr[__static 3]);
00105 
00106 /*
00107  * Common read 24-bit variable from 8-bit pointer.
00108  *
00109  * Read 24 bits in big-endian (network) byte order.
00110  *
00111  * \param data_buf pointer where data to be read
00112  *
00113  * \return 24-bit variable
00114  */
00115 NS_INLINE uint_fast24_t common_read_24_bit(const uint8_t data_buf[__static 3]);
00116 
00117 /*
00118  * Common write 16-bit variable to 8-bit pointer.
00119  *
00120  * Write 16 bits in big-endian (network) byte order.
00121  *
00122  * \param value 16-bit variable
00123  * \param ptr pointer where data to be written
00124  *
00125  * \return updated pointer
00126  */
00127 NS_INLINE uint8_t *common_write_16_bit(uint16_t value, uint8_t ptr[__static 2]);
00128 
00129 /*
00130  * Common read 16-bit variable from 8-bit pointer.
00131  *
00132  * Read 16 bits in big-endian (network) byte order.
00133  *
00134  * \param data_buf pointer where data to be read
00135  *
00136  * \return 16-bit variable
00137  */
00138 NS_INLINE uint16_t common_read_16_bit(const uint8_t data_buf[__static 2]);
00139 
00140 /*
00141  * Common write 16-bit variable to 8-bit pointer.
00142  *
00143  * Write 16 bits in little-endian byte order.
00144  *
00145  * \param value 16-bit variable
00146  * \param ptr pointer where data to be written
00147  *
00148  * \return updated pointer
00149  */
00150 NS_INLINE uint8_t *common_write_16_bit_inverse(uint16_t value, uint8_t ptr[__static 2]);
00151 
00152 /*
00153  * Common read 16-bit variable from 8-bit pointer.
00154  *
00155  * Read 16 bits in little-endian byte order.
00156  *
00157  * \param data_buf pointer where data to be read
00158  *
00159  * \return 16-bit variable
00160  */
00161 NS_INLINE uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 2]);
00162 
00163 /*
00164  * Count bits in a byte
00165  *
00166  * \param byte byte to inspect
00167  *
00168  * \return number of 1-bits in byte
00169  */
00170 NS_INLINE uint_fast8_t common_count_bits(uint8_t byte);
00171 
00172 /*
00173  * Count leading zeros in a byte
00174  *
00175  * \param byte byte to inspect
00176  *
00177  * \return number of leading zeros in byte (0-8)
00178  */
00179 NS_INLINE uint_fast8_t common_count_leading_zeros(uint8_t byte);
00180 
00181 /*
00182  * Compare 8-bit serial numbers
00183  *
00184  * Compare two 8-bit serial numbers, according to RFC 1982 Serial Number
00185  * Arithmetic.
00186  *
00187  * \param s1 first serial number
00188  * \param s2 second serial number
00189  *
00190  * \return true if s1 > s2
00191  * \return false if s1 <= s2, or the comparison is undefined
00192  */
00193 NS_INLINE bool common_serial_number_greater_8(uint8_t s1, uint8_t s2);
00194 
00195 /*
00196  * Compare 16-bit serial numbers
00197  *
00198  * Compare two 16-bit serial numbers, according to RFC 1982 Serial Number
00199  * Arithmetic.
00200  *
00201  * \param s1 first serial number
00202  * \param s2 second serial number
00203  *
00204  * \return true if s1 > s2
00205  * \return false if s1 <= s2, or the comparison is undefined
00206  */
00207 NS_INLINE bool common_serial_number_greater_16(uint16_t s1, uint16_t s2);
00208 
00209 /*
00210  * Compare 32-bit serial numbers
00211  *
00212  * Compare two 32-bit serial numbers, according to RFC 1982 Serial Number
00213  * Arithmetic.
00214  *
00215  * \param s1 first serial number
00216  * \param s2 second serial number
00217  *
00218  * \return true if s1 > s2
00219  * \return false if s1 <= s2, or the comparison is undefined
00220  */
00221 NS_INLINE bool common_serial_number_greater_32(uint32_t s1, uint32_t s2);
00222 
00223 /*
00224  * Test a bit in an bit array.
00225  *
00226  * Check whether a particular bit is set in a bit string. The bit array
00227  * is in big-endian (network) bit order.
00228  *
00229  * \param bitset pointer to bit array
00230  * \param bit index of bit - 0 is the most significant bit of the first byte
00231  *
00232  * \return true if the bit is set
00233  */
00234 NS_INLINE bool bit_test(const uint8_t *bitset, uint_fast8_t bit);
00235 
00236 /*
00237  * Set a bit in an bit array.
00238  *
00239  * Set a bit in a bit array. The array is in big-endian (network) bit order.
00240  *
00241  * \param bitset pointer to bit array
00242  * \param bit index of bit - 0 is the most significant bit of the first byte
00243  */
00244 NS_INLINE void bit_set(uint8_t *bitset, uint_fast8_t bit);
00245 
00246 /*
00247  * Clear a bit in an bit array.
00248  *
00249  * Clear a bit in a bit array. The bit array is in big-endian (network) bit order.
00250  *
00251  * \param bitset pointer to bit array
00252  * \param bit index of bit - 0 is the most significant bit of the first byte
00253  */
00254 NS_INLINE void bit_clear(uint8_t *bitset, uint_fast8_t bit);
00255 
00256 /*
00257  * Compare two bitstrings.
00258  *
00259  * Compare two bitstrings of specified length. The bit strings are in
00260  * big-endian (network) bit order.
00261  *
00262  * \param a pointer to first string
00263  * \param b pointer to second string
00264  * \param bits number of bits to compare
00265  *
00266  * \return true if the strings compare equal
00267  */
00268 bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits);
00269 
00270 /*
00271  * Copy a bitstring
00272  *
00273  * Copy a bitstring of specified length. The bit string is in big-endian
00274  * (network) bit order. Bits beyond the bitlength at the destination are not
00275  * modified.
00276  *
00277  * For example, copying 4 bits sets the first 4 bits of dst[0] from src[0],
00278  * the lower 4 bits of dst[0] are unmodified.
00279  *
00280  * \param dst destination pointer
00281  * \param src source pointer
00282  * \param bits number of bits to copy
00283  *
00284  * \return the value of dst
00285  */
00286 uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits);
00287 
00288 /*
00289  * Copy a bitstring and pad last byte with zeros
00290  *
00291  * Copy a bitstring of specified length. The bit string is in big-endian
00292  * (network) bit order. Bits beyond the bitlength in the last destination byte are
00293  * zeroed.
00294  *
00295  * For example, copying 4 bits sets the first 4 bits of dst[0] from src[0], and
00296  * the lower 4 bits of dst[0] are set to 0.
00297  *
00298  * \param dst destination pointer
00299  * \param src source pointer
00300  * \param bits number of bits to copy
00301  *
00302  * \return the value of dst
00303  */
00304 uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits);
00305 
00306 /* Provide definitions, either for inlining, or for common_functions.c */
00307 #if defined NS_ALLOW_INLINING || defined COMMON_FUNCTIONS_FN
00308 #ifndef COMMON_FUNCTIONS_FN
00309 #define COMMON_FUNCTIONS_FN NS_INLINE
00310 #endif
00311 
00312 COMMON_FUNCTIONS_FN uint8_t *common_write_64_bit(uint64_t value, uint8_t ptr[__static 8])
00313 {
00314     *ptr++ = value >> 56;
00315     *ptr++ = value >> 48;
00316     *ptr++ = value >> 40;
00317     *ptr++ = value >> 32;
00318     *ptr++ = value >> 24;
00319     *ptr++ = value >> 16;
00320     *ptr++ = value >> 8;
00321     *ptr++ = value;
00322     return ptr;
00323 }
00324 
00325 COMMON_FUNCTIONS_FN uint64_t common_read_64_bit(const uint8_t data_buf[__static 8])
00326 {
00327     uint64_t temp_64;
00328     temp_64 = (uint64_t)(*data_buf++) << 56;
00329     temp_64 += (uint64_t)(*data_buf++) << 48;
00330     temp_64 += (uint64_t)(*data_buf++) << 40;
00331     temp_64 += (uint64_t)(*data_buf++) << 32;
00332     temp_64 += (uint64_t)(*data_buf++) << 24;
00333     temp_64 += (uint64_t)(*data_buf++) << 16;
00334     temp_64 += (uint64_t)(*data_buf++) << 8;
00335     temp_64 += *data_buf++;
00336     return temp_64;
00337 }
00338 
00339 COMMON_FUNCTIONS_FN uint8_t *common_write_32_bit(uint32_t value, uint8_t ptr[__static 4])
00340 {
00341     *ptr++ = value >> 24;
00342     *ptr++ = value >> 16;
00343     *ptr++ = value >> 8;
00344     *ptr++ = value;
00345     return ptr;
00346 }
00347 
00348 COMMON_FUNCTIONS_FN uint32_t common_read_32_bit(const uint8_t data_buf[__static 4])
00349 {
00350     uint32_t temp_32;
00351     temp_32 = (uint32_t)(*data_buf++) << 24;
00352     temp_32 += (uint32_t)(*data_buf++) << 16;
00353     temp_32 += (uint32_t)(*data_buf++) << 8;
00354     temp_32 += *data_buf++;
00355     return temp_32;
00356 }
00357 
00358 COMMON_FUNCTIONS_FN uint8_t *common_write_32_bit_inverse(uint32_t value, uint8_t ptr[__static 4])
00359 {
00360     *ptr++ = value;
00361     *ptr++ = value >> 8;
00362     *ptr++ = value >> 16;
00363     *ptr++ = value >> 24;
00364     return ptr;
00365 }
00366 
00367 COMMON_FUNCTIONS_FN uint32_t common_read_32_bit_inverse(const uint8_t data_buf[__static 4])
00368 {
00369     uint32_t temp_32;
00370     temp_32 =  *data_buf++;
00371     temp_32 += (uint32_t)(*data_buf++) << 8;
00372     temp_32 += (uint32_t)(*data_buf++) << 16;
00373     temp_32 += (uint32_t)(*data_buf++) << 24;
00374     return temp_32;
00375 }
00376 
00377 COMMON_FUNCTIONS_FN uint8_t *common_write_24_bit(uint_fast24_t value, uint8_t ptr[__static 3])
00378 {
00379     *ptr++ = value >> 16;
00380     *ptr++ = value >> 8;
00381     *ptr++ = value;
00382     return ptr;
00383 }
00384 
00385 COMMON_FUNCTIONS_FN uint_fast24_t common_read_24_bit(const uint8_t data_buf[__static 3])
00386 {
00387     uint_fast24_t temp_24;
00388     temp_24 = (uint_fast24_t)(*data_buf++) << 16;
00389     temp_24 += (uint_fast24_t)(*data_buf++) << 8;
00390     temp_24 += *data_buf++;
00391     return temp_24;
00392 }
00393 
00394 COMMON_FUNCTIONS_FN uint8_t *common_write_16_bit(uint16_t value, uint8_t ptr[__static 2])
00395 {
00396     *ptr++ = value >> 8;
00397     *ptr++ = value;
00398     return ptr;
00399 }
00400 
00401 COMMON_FUNCTIONS_FN uint16_t common_read_16_bit(const uint8_t data_buf[__static 2])
00402 {
00403     uint16_t temp_16;
00404     temp_16 = (uint16_t)(*data_buf++) << 8;
00405     temp_16 += *data_buf++;
00406     return temp_16;
00407 }
00408 
00409 COMMON_FUNCTIONS_FN uint8_t *common_write_16_bit_inverse(uint16_t value, uint8_t ptr[__static 2])
00410 {
00411     *ptr++ = value;
00412     *ptr++ = value >> 8;
00413     return ptr;
00414 }
00415 
00416 COMMON_FUNCTIONS_FN uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 2])
00417 {
00418     uint16_t temp_16;
00419     temp_16 = *data_buf++;
00420     temp_16 += (uint16_t)(*data_buf++) << 8;
00421     return temp_16;
00422 }
00423 
00424 COMMON_FUNCTIONS_FN uint_fast8_t common_count_bits(uint8_t byte)
00425 {
00426     /* First step sets each bit pair to be count of bits (00,01,10) */
00427     /* [00-00 = 00, 01-00 = 01, 10-01 = 01, 11-01 = 10] */
00428     uint_fast8_t count = byte - ((byte >> 1) & 0x55);
00429     /* Add bit pairs to make each nibble contain count of bits (0-4) */
00430     count = (count & 0x33) + ((count >> 2) & 0x33);
00431     /* Final result is sum of nibbles (0-8) */
00432     count = (count >> 4) + (count & 0x0F);
00433     return count;
00434 }
00435 
00436 COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros(uint8_t byte)
00437 {
00438 #ifdef  __CC_ARM
00439     return byte ? __clz((unsigned int) byte << 24) : 8;
00440 #elif defined __GNUC__
00441     return byte ? __builtin_clz((unsigned int) byte << 24) : 8;
00442 #else
00443     uint_fast8_t cnt = 0;
00444     if (byte == 0) {
00445         return 8;
00446     }
00447     if ((byte & 0xF0) == 0) {
00448         byte <<= 4;
00449         cnt += 4;
00450     }
00451     if ((byte & 0xC0) == 0) {
00452         byte <<= 2;
00453         cnt += 2;
00454     }
00455     if ((byte & 0x80) == 0) {
00456         cnt += 1;
00457     }
00458 
00459     return cnt;
00460 #endif
00461 }
00462 
00463 COMMON_FUNCTIONS_FN bool common_serial_number_greater_8(uint8_t s1, uint8_t s2)
00464 {
00465     return (s1 > s2 && s1 - s2 < UINT8_C(0x80)) || (s1 < s2 && s2 - s1 > UINT8_C(0x80));
00466 }
00467 
00468 COMMON_FUNCTIONS_FN bool common_serial_number_greater_16(uint16_t s1, uint16_t s2)
00469 {
00470     return (s1 > s2 && s1 - s2 < UINT16_C(0x8000)) || (s1 < s2 && s2 - s1 > UINT16_C(0x8000));
00471 }
00472 
00473 COMMON_FUNCTIONS_FN bool common_serial_number_greater_32(uint32_t s1, uint32_t s2)
00474 {
00475     return (s1 > s2 && s1 - s2 < UINT32_C(0x80000000)) || (s1 < s2 && s2 - s1 > UINT32_C(0x80000000));
00476 }
00477 
00478 COMMON_FUNCTIONS_FN bool bit_test(const uint8_t *bitset, uint_fast8_t bit)
00479 {
00480     return bitset[bit >> 3] & (0x80 >> (bit & 7));
00481 }
00482 
00483 COMMON_FUNCTIONS_FN void bit_set(uint8_t *bitset, uint_fast8_t bit)
00484 {
00485     bitset[bit >> 3] |= (0x80 >> (bit & 7));
00486 }
00487 
00488 COMMON_FUNCTIONS_FN void bit_clear(uint8_t *bitset, uint_fast8_t bit)
00489 {
00490     bitset[bit >> 3] &= ~(0x80 >> (bit & 7));
00491 }
00492 
00493 #endif /* defined NS_ALLOW_INLINING || defined COMMON_FUNCTIONS_FN */
00494 
00495 #ifdef __cplusplus
00496 }
00497 #endif
00498 #endif /*__COMMON_FUNCTIONS_H_*/