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