| 1 | #ifndef CSR_MACRO_H__ |
| 2 | #define CSR_MACRO_H__ |
| 3 | /***************************************************************************** |
| 4 | |
| 5 | (c) Cambridge Silicon Radio Limited 2010 |
| 6 | All rights reserved and confidential information of CSR |
| 7 | |
| 8 | Refer to LICENSE.txt included with this source for details |
| 9 | on the license terms. |
| 10 | |
| 11 | *****************************************************************************/ |
| 12 | |
| 13 | #include "csr_types.h" |
| 14 | |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | /*------------------------------------------------------------------*/ |
| 20 | /* Bits - intended to operate on CsrUint32 values */ |
| 21 | /*------------------------------------------------------------------*/ |
| 22 | #define CSR_MASK_IS_SET(val, mask) (((val) & (mask)) == (mask)) |
| 23 | #define CSR_MASK_IS_UNSET(val, mask) ((((val) & (mask)) ^ mask) == (mask)) |
| 24 | #define CSR_MASK_SET(val, mask) ((val) |= (mask)) |
| 25 | #define CSR_MASK_UNSET(val, mask) ((val) = ((val) ^ (mask)) & (val)) /* Unsets the bits in val that are set in mask */ |
| 26 | #define CSR_BIT_IS_SET(val, bit) ((CsrBool) ((((val) & (1UL << (bit))) != 0))) |
| 27 | #define CSR_BIT_SET(val, bit) ((val) |= (1UL << (bit))) |
| 28 | #define CSR_BIT_UNSET(val, bit) ((val) &= ~(1UL << (bit))) |
| 29 | #define CSR_BIT_TOGGLE(val, bit) ((val) ^= (1UL << (bit))) |
| 30 | |
| 31 | /*------------------------------------------------------------------*/ |
| 32 | /* Endian conversion */ |
| 33 | /*------------------------------------------------------------------*/ |
| 34 | #define CSR_GET_UINT16_FROM_LITTLE_ENDIAN(ptr) (((CsrUint16) ((CsrUint8 *) (ptr))[0]) | ((CsrUint16) ((CsrUint8 *) (ptr))[1]) << 8) |
| 35 | #define CSR_GET_UINT32_FROM_LITTLE_ENDIAN(ptr) (((CsrUint32) ((CsrUint8 *) (ptr))[0]) | ((CsrUint32) ((CsrUint8 *) (ptr))[1]) << 8 | \ |
| 36 | ((CsrUint32) ((CsrUint8 *) (ptr))[2]) << 16 | ((CsrUint32) ((CsrUint8 *) (ptr))[3]) << 24) |
| 37 | #define CSR_COPY_UINT16_TO_LITTLE_ENDIAN(uint, ptr) ((CsrUint8 *) (ptr))[0] = ((CsrUint8) ((uint) & 0x00FF)); \ |
| 38 | ((CsrUint8 *) (ptr))[1] = ((CsrUint8) ((uint) >> 8)) |
| 39 | #define CSR_COPY_UINT32_TO_LITTLE_ENDIAN(uint, ptr) ((CsrUint8 *) (ptr))[0] = ((CsrUint8) ((uint) & 0x000000FF)); \ |
| 40 | ((CsrUint8 *) (ptr))[1] = ((CsrUint8) (((uint) >> 8) & 0x000000FF)); \ |
| 41 | ((CsrUint8 *) (ptr))[2] = ((CsrUint8) (((uint) >> 16) & 0x000000FF)); \ |
| 42 | ((CsrUint8 *) (ptr))[3] = ((CsrUint8) (((uint) >> 24) & 0x000000FF)) |
| 43 | #define CSR_GET_UINT16_FROM_BIG_ENDIAN(ptr) (((CsrUint16) ((CsrUint8 *) (ptr))[1]) | ((CsrUint16) ((CsrUint8 *) (ptr))[0]) << 8) |
| 44 | #define CSR_GET_UINT24_FROM_BIG_ENDIAN(ptr) (((CsrUint24) ((CsrUint8 *) (ptr))[2]) | \ |
| 45 | ((CsrUint24) ((CsrUint8 *) (ptr))[1]) << 8 | ((CsrUint24) ((CsrUint8 *) (ptr))[0]) << 16) |
| 46 | #define CSR_GET_UINT32_FROM_BIG_ENDIAN(ptr) (((CsrUint32) ((CsrUint8 *) (ptr))[3]) | ((CsrUint32) ((CsrUint8 *) (ptr))[2]) << 8 | \ |
| 47 | ((CsrUint32) ((CsrUint8 *) (ptr))[1]) << 16 | ((CsrUint32) ((CsrUint8 *) (ptr))[0]) << 24) |
| 48 | #define CSR_COPY_UINT16_TO_BIG_ENDIAN(uint, ptr) ((CsrUint8 *) (ptr))[1] = ((CsrUint8) ((uint) & 0x00FF)); \ |
| 49 | ((CsrUint8 *) (ptr))[0] = ((CsrUint8) ((uint) >> 8)) |
| 50 | #define CSR_COPY_UINT24_TO_BIG_ENDIAN(uint, ptr) ((CsrUint8 *) (ptr))[2] = ((CsrUint8) ((uint) & 0x000000FF)); \ |
| 51 | ((CsrUint8 *) (ptr))[1] = ((CsrUint8) (((uint) >> 8) & 0x000000FF)); \ |
| 52 | ((CsrUint8 *) (ptr))[0] = ((CsrUint8) (((uint) >> 16) & 0x000000FF)) |
| 53 | #define CSR_COPY_UINT32_TO_BIG_ENDIAN(uint, ptr) ((CsrUint8 *) (ptr))[3] = ((CsrUint8) ((uint) & 0x000000FF)); \ |
| 54 | ((CsrUint8 *) (ptr))[2] = ((CsrUint8) (((uint) >> 8) & 0x000000FF)); \ |
| 55 | ((CsrUint8 *) (ptr))[1] = ((CsrUint8) (((uint) >> 16) & 0x000000FF)); \ |
| 56 | ((CsrUint8 *) (ptr))[0] = ((CsrUint8) (((uint) >> 24) & 0x000000FF)) |
| 57 | |
| 58 | /*------------------------------------------------------------------*/ |
| 59 | /* XAP conversion macros */ |
| 60 | /*------------------------------------------------------------------*/ |
| 61 | |
| 62 | #define CSR_LSB16(a) ((CsrUint8) ((a) & 0x00ff)) |
| 63 | #define CSR_MSB16(b) ((CsrUint8) ((b) >> 8)) |
| 64 | |
| 65 | #define CSR_CONVERT_8_FROM_XAP(output, input) \ |
| 66 | (output) = ((CsrUint8) (input));(input) += 2 |
| 67 | |
| 68 | #define CSR_CONVERT_16_FROM_XAP(output, input) \ |
| 69 | (output) = (CsrUint16) ((((CsrUint16) (input)[1]) << 8) | \ |
| 70 | ((CsrUint16) (input)[0]));(input) += 2 |
| 71 | |
| 72 | #define CSR_CONVERT_32_FROM_XAP(output, input) \ |
| 73 | (output) = (((CsrUint32) (input)[1]) << 24) | \ |
| 74 | (((CsrUint32) (input)[0]) << 16) | \ |
| 75 | (((CsrUint32) (input)[3]) << 8) | \ |
| 76 | ((CsrUint32) (input)[2]);input += 4 |
| 77 | |
| 78 | #define CSR_ADD_UINT8_TO_XAP(output, input) \ |
| 79 | (output)[0] = (input); \ |
| 80 | (output)[1] = 0;(output) += 2 |
| 81 | |
| 82 | #define CSR_ADD_UINT16_TO_XAP(output, input) \ |
| 83 | (output)[0] = ((CsrUint8) ((input) & 0x00FF)); \ |
| 84 | (output)[1] = ((CsrUint8) ((input) >> 8));(output) += 2 |
| 85 | |
| 86 | #define CSR_ADD_UINT32_TO_XAP(output, input) \ |
| 87 | (output)[0] = ((CsrUint8) (((input) >> 16) & 0x00FF)); \ |
| 88 | (output)[1] = ((CsrUint8) ((input) >> 24)); \ |
| 89 | (output)[2] = ((CsrUint8) ((input) & 0x00FF)); \ |
| 90 | (output)[3] = ((CsrUint8) (((input) >> 8) & 0x00FF));(output) += 4 |
| 91 | |
| 92 | /*------------------------------------------------------------------*/ |
| 93 | /* Misc */ |
| 94 | /*------------------------------------------------------------------*/ |
| 95 | #define CSRMAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 96 | #define CSRMIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 97 | |
| 98 | /* Use this macro on unused local variables that cannot be removed (such as |
| 99 | unused function parameters). This will quell warnings from certain compilers |
| 100 | and static code analysis tools like Lint and Valgrind. */ |
| 101 | #define CSR_UNUSED(x) ((void) (x)) |
| 102 | |
| 103 | #define CSR_TOUPPER(character) (((character) >= 'a') && ((character) <= 'z') ? ((character) - 0x20) : (character)) |
| 104 | #define CSR_TOLOWER(character) (((character) >= 'A') && ((character) <= 'Z') ? ((character) + 0x20) : (character)) |
| 105 | #define CSR_ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
| 106 | |
| 107 | #ifdef __cplusplus |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #endif |