Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
TARGET_NRF5/source/common/binary.h@640:819c58e5bd42, 2017-01-24 (annotated)
- Committer:
- kataokashunpei
- Date:
- Tue Jan 24 23:44:29 2017 +0000
- Revision:
- 640:819c58e5bd42
- Parent:
- 638:c90ae1400bf2
update final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
638:c90ae1400bf2 | 1 | /**************************************************************************/ |
Vincent Coubard |
638:c90ae1400bf2 | 2 | /*! |
Vincent Coubard |
638:c90ae1400bf2 | 3 | @file binary.h |
Vincent Coubard |
638:c90ae1400bf2 | 4 | @author hathach (tinyusb.org) |
Vincent Coubard |
638:c90ae1400bf2 | 5 | |
Vincent Coubard |
638:c90ae1400bf2 | 6 | @section LICENSE |
Vincent Coubard |
638:c90ae1400bf2 | 7 | |
Vincent Coubard |
638:c90ae1400bf2 | 8 | Software License Agreement (BSD License) |
Vincent Coubard |
638:c90ae1400bf2 | 9 | |
Vincent Coubard |
638:c90ae1400bf2 | 10 | Copyright (c) 2013, K. Townsend (microBuilder.eu) |
Vincent Coubard |
638:c90ae1400bf2 | 11 | All rights reserved. |
Vincent Coubard |
638:c90ae1400bf2 | 12 | |
Vincent Coubard |
638:c90ae1400bf2 | 13 | Redistribution and use in source and binary forms, with or without |
Vincent Coubard |
638:c90ae1400bf2 | 14 | modification, are permitted provided that the following conditions are met: |
Vincent Coubard |
638:c90ae1400bf2 | 15 | 1. Redistributions of source code must retain the above copyright |
Vincent Coubard |
638:c90ae1400bf2 | 16 | notice, this list of conditions and the following disclaimer. |
Vincent Coubard |
638:c90ae1400bf2 | 17 | 2. Redistributions in binary form must reproduce the above copyright |
Vincent Coubard |
638:c90ae1400bf2 | 18 | notice, this list of conditions and the following disclaimer in the |
Vincent Coubard |
638:c90ae1400bf2 | 19 | documentation and/or other materials provided with the distribution. |
Vincent Coubard |
638:c90ae1400bf2 | 20 | 3. Neither the name of the copyright holders nor the |
Vincent Coubard |
638:c90ae1400bf2 | 21 | names of its contributors may be used to endorse or promote products |
Vincent Coubard |
638:c90ae1400bf2 | 22 | derived from this software without specific prior written permission. |
Vincent Coubard |
638:c90ae1400bf2 | 23 | |
Vincent Coubard |
638:c90ae1400bf2 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
Vincent Coubard |
638:c90ae1400bf2 | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Vincent Coubard |
638:c90ae1400bf2 | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Vincent Coubard |
638:c90ae1400bf2 | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
Vincent Coubard |
638:c90ae1400bf2 | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Vincent Coubard |
638:c90ae1400bf2 | 29 | INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Vincent Coubard |
638:c90ae1400bf2 | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND |
Vincent Coubard |
638:c90ae1400bf2 | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Vincent Coubard |
638:c90ae1400bf2 | 32 | INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS |
Vincent Coubard |
638:c90ae1400bf2 | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Vincent Coubard |
638:c90ae1400bf2 | 34 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 35 | /**************************************************************************/ |
Vincent Coubard |
638:c90ae1400bf2 | 36 | |
Vincent Coubard |
638:c90ae1400bf2 | 37 | /** \ingroup TBD |
Vincent Coubard |
638:c90ae1400bf2 | 38 | * \defgroup TBD |
Vincent Coubard |
638:c90ae1400bf2 | 39 | * \brief TBD |
Vincent Coubard |
638:c90ae1400bf2 | 40 | * |
Vincent Coubard |
638:c90ae1400bf2 | 41 | * @{ |
Vincent Coubard |
638:c90ae1400bf2 | 42 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 43 | |
Vincent Coubard |
638:c90ae1400bf2 | 44 | #ifndef _BINARY_H_ |
Vincent Coubard |
638:c90ae1400bf2 | 45 | #define _BINARY_H_ |
Vincent Coubard |
638:c90ae1400bf2 | 46 | |
Vincent Coubard |
638:c90ae1400bf2 | 47 | #ifdef __cplusplus |
Vincent Coubard |
638:c90ae1400bf2 | 48 | extern "C" { |
Vincent Coubard |
638:c90ae1400bf2 | 49 | #endif |
Vincent Coubard |
638:c90ae1400bf2 | 50 | |
Vincent Coubard |
638:c90ae1400bf2 | 51 | /// n-th Bit |
Vincent Coubard |
638:c90ae1400bf2 | 52 | #define BIT(n) (1 << (n)) |
Vincent Coubard |
638:c90ae1400bf2 | 53 | |
Vincent Coubard |
638:c90ae1400bf2 | 54 | /// set n-th bit of x to 1 |
Vincent Coubard |
638:c90ae1400bf2 | 55 | #define BIT_SET(x, n) ( (x) | BIT(n) ) |
Vincent Coubard |
638:c90ae1400bf2 | 56 | |
Vincent Coubard |
638:c90ae1400bf2 | 57 | /// clear n-th bit of x |
Vincent Coubard |
638:c90ae1400bf2 | 58 | #define BIT_CLR(x, n) ( (x) & (~BIT(n)) ) |
Vincent Coubard |
638:c90ae1400bf2 | 59 | |
Vincent Coubard |
638:c90ae1400bf2 | 60 | /// test n-th bit of x |
Vincent Coubard |
638:c90ae1400bf2 | 61 | #define BIT_TEST(x, n) ( (x) & BIT(n) ) |
Vincent Coubard |
638:c90ae1400bf2 | 62 | |
Vincent Coubard |
638:c90ae1400bf2 | 63 | #if defined(__GNUC__) && !defined(__CC_ARM) // keil does not support binary format |
Vincent Coubard |
638:c90ae1400bf2 | 64 | |
Vincent Coubard |
638:c90ae1400bf2 | 65 | #define BIN8(x) ((uint8_t) (0b##x)) |
Vincent Coubard |
638:c90ae1400bf2 | 66 | #define BIN16(b1, b2) ((uint16_t) (0b##b1##b2)) |
Vincent Coubard |
638:c90ae1400bf2 | 67 | #define BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4)) |
Vincent Coubard |
638:c90ae1400bf2 | 68 | |
Vincent Coubard |
638:c90ae1400bf2 | 69 | #else |
Vincent Coubard |
638:c90ae1400bf2 | 70 | |
Vincent Coubard |
638:c90ae1400bf2 | 71 | // internal macro of B8, B16, B32 |
Vincent Coubard |
638:c90ae1400bf2 | 72 | #define _B8__(x) (((x&0x0000000FUL)?1:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 73 | +((x&0x000000F0UL)?2:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 74 | +((x&0x00000F00UL)?4:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 75 | +((x&0x0000F000UL)?8:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 76 | +((x&0x000F0000UL)?16:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 77 | +((x&0x00F00000UL)?32:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 78 | +((x&0x0F000000UL)?64:0) \ |
Vincent Coubard |
638:c90ae1400bf2 | 79 | +((x&0xF0000000UL)?128:0)) |
Vincent Coubard |
638:c90ae1400bf2 | 80 | |
Vincent Coubard |
638:c90ae1400bf2 | 81 | #define BIN8(d) ((uint8_t) _B8__(0x##d##UL)) |
Vincent Coubard |
638:c90ae1400bf2 | 82 | #define BIN16(dmsb,dlsb) (((uint16_t)BIN8(dmsb)<<8) + BIN8(dlsb)) |
Vincent Coubard |
638:c90ae1400bf2 | 83 | #define BIN32(dmsb,db2,db3,dlsb) \ |
Vincent Coubard |
638:c90ae1400bf2 | 84 | (((uint32_t)BIN8(dmsb)<<24) \ |
Vincent Coubard |
638:c90ae1400bf2 | 85 | + ((uint32_t)BIN8(db2)<<16) \ |
Vincent Coubard |
638:c90ae1400bf2 | 86 | + ((uint32_t)BIN8(db3)<<8) \ |
Vincent Coubard |
638:c90ae1400bf2 | 87 | + BIN8(dlsb)) |
Vincent Coubard |
638:c90ae1400bf2 | 88 | #endif |
Vincent Coubard |
638:c90ae1400bf2 | 89 | |
Vincent Coubard |
638:c90ae1400bf2 | 90 | #ifdef __cplusplus |
Vincent Coubard |
638:c90ae1400bf2 | 91 | } |
Vincent Coubard |
638:c90ae1400bf2 | 92 | #endif |
Vincent Coubard |
638:c90ae1400bf2 | 93 | |
Vincent Coubard |
638:c90ae1400bf2 | 94 | #endif /* _BINARY_H_ */ |
Vincent Coubard |
638:c90ae1400bf2 | 95 | |
Vincent Coubard |
638:c90ae1400bf2 | 96 | /** @} */ |