mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
targets/TARGET_NUVOTON/nu_bitutil.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2015-2016 Nuvoton |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | #ifndef NU_BIT_UTIL_H |
elessair | 0:f269e3021894 | 18 | #define NU_BIT_UTIL_H |
elessair | 0:f269e3021894 | 19 | |
elessair | 0:f269e3021894 | 20 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | #ifdef __cplusplus |
elessair | 0:f269e3021894 | 23 | extern "C" { |
elessair | 0:f269e3021894 | 24 | #endif |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | __STATIC_INLINE int nu_clz(uint32_t x) |
elessair | 0:f269e3021894 | 27 | { |
elessair | 0:f269e3021894 | 28 | return __CLZ(x); |
elessair | 0:f269e3021894 | 29 | } |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | __STATIC_INLINE int nu_clo(uint32_t x) |
elessair | 0:f269e3021894 | 32 | { |
elessair | 0:f269e3021894 | 33 | return nu_clz(~x); |
elessair | 0:f269e3021894 | 34 | } |
elessair | 0:f269e3021894 | 35 | |
elessair | 0:f269e3021894 | 36 | __STATIC_INLINE int nu_ctz(uint32_t x) |
elessair | 0:f269e3021894 | 37 | { |
elessair | 0:f269e3021894 | 38 | int c = __CLZ(x & -x); |
elessair | 0:f269e3021894 | 39 | return x ? 31 - c : c; |
elessair | 0:f269e3021894 | 40 | } |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | __STATIC_INLINE int nu_cto(uint32_t x) |
elessair | 0:f269e3021894 | 43 | { |
elessair | 0:f269e3021894 | 44 | return nu_ctz(~x); |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | |
elessair | 0:f269e3021894 | 48 | __STATIC_INLINE uint16_t nu_get16_le(const uint8_t *pos) |
elessair | 0:f269e3021894 | 49 | { |
elessair | 0:f269e3021894 | 50 | uint16_t val; |
elessair | 0:f269e3021894 | 51 | |
elessair | 0:f269e3021894 | 52 | val = *pos ++; |
elessair | 0:f269e3021894 | 53 | val += (*pos << 8); |
elessair | 0:f269e3021894 | 54 | |
elessair | 0:f269e3021894 | 55 | return val; |
elessair | 0:f269e3021894 | 56 | } |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | __STATIC_INLINE void nu_set16_le(uint8_t *pos, uint16_t val) |
elessair | 0:f269e3021894 | 59 | { |
elessair | 0:f269e3021894 | 60 | *pos ++ = val & 0xFF; |
elessair | 0:f269e3021894 | 61 | *pos = val >> 8; |
elessair | 0:f269e3021894 | 62 | } |
elessair | 0:f269e3021894 | 63 | |
elessair | 0:f269e3021894 | 64 | __STATIC_INLINE uint32_t nu_get32_le(const uint8_t *pos) |
elessair | 0:f269e3021894 | 65 | { |
elessair | 0:f269e3021894 | 66 | uint32_t val; |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | val = *pos ++; |
elessair | 0:f269e3021894 | 69 | val += (*pos ++ << 8); |
elessair | 0:f269e3021894 | 70 | val += (*pos ++ << 16); |
elessair | 0:f269e3021894 | 71 | val += (*pos ++ << 24); |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | return val; |
elessair | 0:f269e3021894 | 74 | } |
elessair | 0:f269e3021894 | 75 | |
elessair | 0:f269e3021894 | 76 | __STATIC_INLINE void nu_set32_le(uint8_t *pos, uint32_t val) |
elessair | 0:f269e3021894 | 77 | { |
elessair | 0:f269e3021894 | 78 | *pos ++ = val & 0xFF; |
elessair | 0:f269e3021894 | 79 | *pos ++ = (val >> 8) & 0xFF; |
elessair | 0:f269e3021894 | 80 | *pos ++ = (val >> 16) & 0xFF; |
elessair | 0:f269e3021894 | 81 | *pos = (val >> 24) & 0xFF; |
elessair | 0:f269e3021894 | 82 | } |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | __STATIC_INLINE uint16_t nu_get16_be(const uint8_t *pos) |
elessair | 0:f269e3021894 | 85 | { |
elessair | 0:f269e3021894 | 86 | uint16_t val; |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | val = *pos ++; |
elessair | 0:f269e3021894 | 89 | val <<= 8; |
elessair | 0:f269e3021894 | 90 | val += *pos; |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | return val; |
elessair | 0:f269e3021894 | 93 | } |
elessair | 0:f269e3021894 | 94 | |
elessair | 0:f269e3021894 | 95 | __STATIC_INLINE void nu_set16_be(uint8_t *pos, uint16_t val) |
elessair | 0:f269e3021894 | 96 | { |
elessair | 0:f269e3021894 | 97 | *pos ++ = val >> 8; |
elessair | 0:f269e3021894 | 98 | *pos = (val & 0xFF); |
elessair | 0:f269e3021894 | 99 | } |
elessair | 0:f269e3021894 | 100 | |
elessair | 0:f269e3021894 | 101 | __STATIC_INLINE uint32_t nu_get32_be(const uint8_t *pos) |
elessair | 0:f269e3021894 | 102 | { |
elessair | 0:f269e3021894 | 103 | uint32_t val; |
elessair | 0:f269e3021894 | 104 | |
elessair | 0:f269e3021894 | 105 | val = *pos ++; |
elessair | 0:f269e3021894 | 106 | val <<= 8; |
elessair | 0:f269e3021894 | 107 | val += *pos ++; |
elessair | 0:f269e3021894 | 108 | val <<= 8; |
elessair | 0:f269e3021894 | 109 | val += *pos ++; |
elessair | 0:f269e3021894 | 110 | val <<= 8; |
elessair | 0:f269e3021894 | 111 | val += *pos; |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | return val; |
elessair | 0:f269e3021894 | 114 | } |
elessair | 0:f269e3021894 | 115 | |
elessair | 0:f269e3021894 | 116 | __STATIC_INLINE void nu_set32_be(uint8_t *pos, uint32_t val) |
elessair | 0:f269e3021894 | 117 | { |
elessair | 0:f269e3021894 | 118 | *pos ++ = val >> 24; |
elessair | 0:f269e3021894 | 119 | *pos ++ = val >> 16; |
elessair | 0:f269e3021894 | 120 | *pos ++ = val >> 8; |
elessair | 0:f269e3021894 | 121 | *pos ++ = (val & 0xFF); |
elessair | 0:f269e3021894 | 122 | } |
elessair | 0:f269e3021894 | 123 | |
elessair | 0:f269e3021894 | 124 | #ifdef __cplusplus |
elessair | 0:f269e3021894 | 125 | } |
elessair | 0:f269e3021894 | 126 | #endif |
elessair | 0:f269e3021894 | 127 | |
elessair | 0:f269e3021894 | 128 | #endif |