AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 BLE Nanoで動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo

Dependencies:   BLE_API mbed

BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するBLE Nano用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
/media/uploads/lipoyang/blepropo_ui.png
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
/media/uploads/lipoyang/ministeer3.jpg
回路図
/media/uploads/lipoyang/ministeer3.pdf

Committer:
lipoyang
Date:
Sat Mar 14 12:02:48 2015 +0000
Revision:
5:7f89fca19a9e
-convert nRF51822 library to a folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lipoyang 5:7f89fca19a9e 1 /**************************************************************************/
lipoyang 5:7f89fca19a9e 2 /*!
lipoyang 5:7f89fca19a9e 3 @file common.h
lipoyang 5:7f89fca19a9e 4 @author hathach (tinyusb.org)
lipoyang 5:7f89fca19a9e 5
lipoyang 5:7f89fca19a9e 6 @section LICENSE
lipoyang 5:7f89fca19a9e 7
lipoyang 5:7f89fca19a9e 8 Software License Agreement (BSD License)
lipoyang 5:7f89fca19a9e 9
lipoyang 5:7f89fca19a9e 10 Copyright (c) 2013, K. Townsend (microBuilder.eu)
lipoyang 5:7f89fca19a9e 11 All rights reserved.
lipoyang 5:7f89fca19a9e 12
lipoyang 5:7f89fca19a9e 13 Redistribution and use in source and binary forms, with or without
lipoyang 5:7f89fca19a9e 14 modification, are permitted provided that the following conditions are met:
lipoyang 5:7f89fca19a9e 15 1. Redistributions of source code must retain the above copyright
lipoyang 5:7f89fca19a9e 16 notice, this list of conditions and the following disclaimer.
lipoyang 5:7f89fca19a9e 17 2. Redistributions in binary form must reproduce the above copyright
lipoyang 5:7f89fca19a9e 18 notice, this list of conditions and the following disclaimer in the
lipoyang 5:7f89fca19a9e 19 documentation and/or other materials provided with the distribution.
lipoyang 5:7f89fca19a9e 20 3. Neither the name of the copyright holders nor the
lipoyang 5:7f89fca19a9e 21 names of its contributors may be used to endorse or promote products
lipoyang 5:7f89fca19a9e 22 derived from this software without specific prior written permission.
lipoyang 5:7f89fca19a9e 23
lipoyang 5:7f89fca19a9e 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
lipoyang 5:7f89fca19a9e 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
lipoyang 5:7f89fca19a9e 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lipoyang 5:7f89fca19a9e 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
lipoyang 5:7f89fca19a9e 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
lipoyang 5:7f89fca19a9e 29 INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
lipoyang 5:7f89fca19a9e 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
lipoyang 5:7f89fca19a9e 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
lipoyang 5:7f89fca19a9e 32 INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
lipoyang 5:7f89fca19a9e 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lipoyang 5:7f89fca19a9e 34 */
lipoyang 5:7f89fca19a9e 35 /**************************************************************************/
lipoyang 5:7f89fca19a9e 36
lipoyang 5:7f89fca19a9e 37 /** \defgroup Group_Common Common Files
lipoyang 5:7f89fca19a9e 38 * @{
lipoyang 5:7f89fca19a9e 39 *
lipoyang 5:7f89fca19a9e 40 * \defgroup Group_CommonH common.h
lipoyang 5:7f89fca19a9e 41 *
lipoyang 5:7f89fca19a9e 42 * @{
lipoyang 5:7f89fca19a9e 43 */
lipoyang 5:7f89fca19a9e 44
lipoyang 5:7f89fca19a9e 45 #ifndef _COMMON_H_
lipoyang 5:7f89fca19a9e 46 #define _COMMON_H_
lipoyang 5:7f89fca19a9e 47
lipoyang 5:7f89fca19a9e 48 #ifdef __cplusplus
lipoyang 5:7f89fca19a9e 49 extern "C" {
lipoyang 5:7f89fca19a9e 50 #endif
lipoyang 5:7f89fca19a9e 51
lipoyang 5:7f89fca19a9e 52 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 53 // INCLUDES
lipoyang 5:7f89fca19a9e 54 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 55
lipoyang 5:7f89fca19a9e 56 //------------- Standard Header -------------//
lipoyang 5:7f89fca19a9e 57 #include <stdint.h>
lipoyang 5:7f89fca19a9e 58 #include <stdbool.h>
lipoyang 5:7f89fca19a9e 59 #include <stddef.h>
lipoyang 5:7f89fca19a9e 60 #include <string.h>
lipoyang 5:7f89fca19a9e 61 #include <stdio.h>
lipoyang 5:7f89fca19a9e 62
lipoyang 5:7f89fca19a9e 63 //------------- General Header -------------//
lipoyang 5:7f89fca19a9e 64 #include "projectconfig.h"
lipoyang 5:7f89fca19a9e 65 #include "compiler.h"
lipoyang 5:7f89fca19a9e 66 #include "assertion.h"
lipoyang 5:7f89fca19a9e 67 #include "binary.h"
lipoyang 5:7f89fca19a9e 68 #include "ble_error.h"
lipoyang 5:7f89fca19a9e 69
lipoyang 5:7f89fca19a9e 70 //------------- MCU header -------------//
lipoyang 5:7f89fca19a9e 71 //#include "nrf.h"
lipoyang 5:7f89fca19a9e 72
lipoyang 5:7f89fca19a9e 73 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 74 // TYPEDEFS
lipoyang 5:7f89fca19a9e 75 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 76 typedef unsigned char byte_t;
lipoyang 5:7f89fca19a9e 77 typedef float float32_t;
lipoyang 5:7f89fca19a9e 78 typedef double float64_t;
lipoyang 5:7f89fca19a9e 79
lipoyang 5:7f89fca19a9e 80 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 81 // MACROS
lipoyang 5:7f89fca19a9e 82 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 83 #define STRING_(x) #x // stringify without expand
lipoyang 5:7f89fca19a9e 84 #define XSTRING_(x) STRING_(x) // expand then stringify
lipoyang 5:7f89fca19a9e 85 #define STRING_CONCAT_(a, b) a##b // concat without expand
lipoyang 5:7f89fca19a9e 86 #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
lipoyang 5:7f89fca19a9e 87
lipoyang 5:7f89fca19a9e 88 #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
lipoyang 5:7f89fca19a9e 89 #define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
lipoyang 5:7f89fca19a9e 90 #define U16_TO_U8S_BE(u16) U16_HIGH_U8(u16), U16_LOW_U8(u16)
lipoyang 5:7f89fca19a9e 91 #define U16_TO_U8S_LE(u16) U16_LOW_U8(u16), U16_HIGH_U8(u16)
lipoyang 5:7f89fca19a9e 92
lipoyang 5:7f89fca19a9e 93 #define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
lipoyang 5:7f89fca19a9e 94 #define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
lipoyang 5:7f89fca19a9e 95 #define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
lipoyang 5:7f89fca19a9e 96 #define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
lipoyang 5:7f89fca19a9e 97
lipoyang 5:7f89fca19a9e 98 #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
lipoyang 5:7f89fca19a9e 99 #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
lipoyang 5:7f89fca19a9e 100
lipoyang 5:7f89fca19a9e 101 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 102 // INLINE FUNCTION
lipoyang 5:7f89fca19a9e 103 //--------------------------------------------------------------------+
lipoyang 5:7f89fca19a9e 104 #define memclr_(buffer, size) memset(buffer, 0, size)
lipoyang 5:7f89fca19a9e 105
lipoyang 5:7f89fca19a9e 106 //------------- Conversion -------------//
lipoyang 5:7f89fca19a9e 107 /// form an uint32_t from 4 x uint8_t
lipoyang 5:7f89fca19a9e 108 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 109 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
lipoyang 5:7f89fca19a9e 110 {
lipoyang 5:7f89fca19a9e 111 return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
lipoyang 5:7f89fca19a9e 112 }
lipoyang 5:7f89fca19a9e 113
lipoyang 5:7f89fca19a9e 114 static inline uint8_t u16_high_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
lipoyang 5:7f89fca19a9e 115 static inline uint8_t u16_high_u8(uint16_t u16)
lipoyang 5:7f89fca19a9e 116 {
lipoyang 5:7f89fca19a9e 117 return (uint8_t) ((u16 >> 8) & 0x00ff);
lipoyang 5:7f89fca19a9e 118 }
lipoyang 5:7f89fca19a9e 119
lipoyang 5:7f89fca19a9e 120 static inline uint8_t u16_low_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
lipoyang 5:7f89fca19a9e 121 static inline uint8_t u16_low_u8(uint16_t u16)
lipoyang 5:7f89fca19a9e 122 {
lipoyang 5:7f89fca19a9e 123 return (uint8_t) (u16 & 0x00ff);
lipoyang 5:7f89fca19a9e 124 }
lipoyang 5:7f89fca19a9e 125
lipoyang 5:7f89fca19a9e 126 //------------- Min -------------//
lipoyang 5:7f89fca19a9e 127 static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 128 static inline uint8_t min8_of(uint8_t x, uint8_t y)
lipoyang 5:7f89fca19a9e 129 {
lipoyang 5:7f89fca19a9e 130 return (x < y) ? x : y;
lipoyang 5:7f89fca19a9e 131 }
lipoyang 5:7f89fca19a9e 132
lipoyang 5:7f89fca19a9e 133 static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 134 static inline uint16_t min16_of(uint16_t x, uint16_t y)
lipoyang 5:7f89fca19a9e 135 {
lipoyang 5:7f89fca19a9e 136 return (x < y) ? x : y;
lipoyang 5:7f89fca19a9e 137 }
lipoyang 5:7f89fca19a9e 138
lipoyang 5:7f89fca19a9e 139 static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 140 static inline uint32_t min32_of(uint32_t x, uint32_t y)
lipoyang 5:7f89fca19a9e 141 {
lipoyang 5:7f89fca19a9e 142 return (x < y) ? x : y;
lipoyang 5:7f89fca19a9e 143 }
lipoyang 5:7f89fca19a9e 144
lipoyang 5:7f89fca19a9e 145 //------------- Max -------------//
lipoyang 5:7f89fca19a9e 146 static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 147 static inline uint32_t max32_of(uint32_t x, uint32_t y)
lipoyang 5:7f89fca19a9e 148 {
lipoyang 5:7f89fca19a9e 149 return (x > y) ? x : y;
lipoyang 5:7f89fca19a9e 150 }
lipoyang 5:7f89fca19a9e 151
lipoyang 5:7f89fca19a9e 152 //------------- Align -------------//
lipoyang 5:7f89fca19a9e 153 static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 154 static inline uint32_t align32 (uint32_t value)
lipoyang 5:7f89fca19a9e 155 {
lipoyang 5:7f89fca19a9e 156 return (value & 0xFFFFFFE0UL);
lipoyang 5:7f89fca19a9e 157 }
lipoyang 5:7f89fca19a9e 158
lipoyang 5:7f89fca19a9e 159 static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 160 static inline uint32_t align16 (uint32_t value)
lipoyang 5:7f89fca19a9e 161 {
lipoyang 5:7f89fca19a9e 162 return (value & 0xFFFFFFF0UL);
lipoyang 5:7f89fca19a9e 163 }
lipoyang 5:7f89fca19a9e 164
lipoyang 5:7f89fca19a9e 165 static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 166 static inline uint32_t align_n (uint32_t alignment, uint32_t value)
lipoyang 5:7f89fca19a9e 167 {
lipoyang 5:7f89fca19a9e 168 return value & (~(alignment-1));
lipoyang 5:7f89fca19a9e 169 }
lipoyang 5:7f89fca19a9e 170
lipoyang 5:7f89fca19a9e 171 static inline uint32_t align4k (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 172 static inline uint32_t align4k (uint32_t value)
lipoyang 5:7f89fca19a9e 173 {
lipoyang 5:7f89fca19a9e 174 return (value & 0xFFFFF000UL);
lipoyang 5:7f89fca19a9e 175 }
lipoyang 5:7f89fca19a9e 176
lipoyang 5:7f89fca19a9e 177 static inline uint32_t offset4k(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 178 static inline uint32_t offset4k(uint32_t value)
lipoyang 5:7f89fca19a9e 179 {
lipoyang 5:7f89fca19a9e 180 return (value & 0xFFFUL);
lipoyang 5:7f89fca19a9e 181 }
lipoyang 5:7f89fca19a9e 182
lipoyang 5:7f89fca19a9e 183 //------------- Mathematics -------------//
lipoyang 5:7f89fca19a9e 184 /// inclusive range checking
lipoyang 5:7f89fca19a9e 185 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 186 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper)
lipoyang 5:7f89fca19a9e 187 {
lipoyang 5:7f89fca19a9e 188 return (lower <= value) && (value <= upper);
lipoyang 5:7f89fca19a9e 189 }
lipoyang 5:7f89fca19a9e 190
lipoyang 5:7f89fca19a9e 191 /// exclusive range checking
lipoyang 5:7f89fca19a9e 192 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 193 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper)
lipoyang 5:7f89fca19a9e 194 {
lipoyang 5:7f89fca19a9e 195 return (lower < value) && (value < upper);
lipoyang 5:7f89fca19a9e 196 }
lipoyang 5:7f89fca19a9e 197
lipoyang 5:7f89fca19a9e 198 static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 199 static inline uint8_t log2_of(uint32_t value)
lipoyang 5:7f89fca19a9e 200 {
lipoyang 5:7f89fca19a9e 201 uint8_t result = 0; // log2 of a value is its MSB's position
lipoyang 5:7f89fca19a9e 202
lipoyang 5:7f89fca19a9e 203 while (value >>= 1)
lipoyang 5:7f89fca19a9e 204 {
lipoyang 5:7f89fca19a9e 205 result++;
lipoyang 5:7f89fca19a9e 206 }
lipoyang 5:7f89fca19a9e 207 return result;
lipoyang 5:7f89fca19a9e 208 }
lipoyang 5:7f89fca19a9e 209
lipoyang 5:7f89fca19a9e 210 // return the number of set bits in value
lipoyang 5:7f89fca19a9e 211 static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
lipoyang 5:7f89fca19a9e 212 static inline uint8_t cardinality_of(uint32_t value)
lipoyang 5:7f89fca19a9e 213 {
lipoyang 5:7f89fca19a9e 214 // Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only
lipoyang 5:7f89fca19a9e 215 // the high bit set, then it will only go once through the loop
lipoyang 5:7f89fca19a9e 216 // Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)
lipoyang 5:7f89fca19a9e 217 // mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method
lipoyang 5:7f89fca19a9e 218 // "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
lipoyang 5:7f89fca19a9e 219 // published in 1964 in a book edited by Beckenbach.)"
lipoyang 5:7f89fca19a9e 220 uint8_t count;
lipoyang 5:7f89fca19a9e 221 for (count = 0; value; count++)
lipoyang 5:7f89fca19a9e 222 {
lipoyang 5:7f89fca19a9e 223 value &= value - 1; // clear the least significant bit set
lipoyang 5:7f89fca19a9e 224 }
lipoyang 5:7f89fca19a9e 225
lipoyang 5:7f89fca19a9e 226 return count;
lipoyang 5:7f89fca19a9e 227 }
lipoyang 5:7f89fca19a9e 228
lipoyang 5:7f89fca19a9e 229 #ifdef __cplusplus
lipoyang 5:7f89fca19a9e 230 }
lipoyang 5:7f89fca19a9e 231 #endif
lipoyang 5:7f89fca19a9e 232
lipoyang 5:7f89fca19a9e 233 #endif /* _COMMON_H_ */
lipoyang 5:7f89fca19a9e 234
lipoyang 5:7f89fca19a9e 235 /** @} */
lipoyang 5:7f89fca19a9e 236 /** @} */