mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1
elessair 0:f269e3021894 2 /** \addtogroup platform */
elessair 0:f269e3021894 3 /** @{*/
elessair 0:f269e3021894 4 /* mbed Microcontroller Library
elessair 0:f269e3021894 5 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 6 *
elessair 0:f269e3021894 7 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 8 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 9 * You may obtain a copy of the License at
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 12 *
elessair 0:f269e3021894 13 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 14 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 16 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 17 * limitations under the License.
elessair 0:f269e3021894 18 */
elessair 0:f269e3021894 19 #ifndef SINGLETONPTR_H
elessair 0:f269e3021894 20 #define SINGLETONPTR_H
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #include <stdint.h>
elessair 0:f269e3021894 23 #include <new>
elessair 0:f269e3021894 24 #include "platform/mbed_assert.h"
elessair 0:f269e3021894 25 #ifdef MBED_CONF_RTOS_PRESENT
elessair 0:f269e3021894 26 #include "cmsis_os.h"
elessair 0:f269e3021894 27 #endif
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 #ifdef MBED_CONF_RTOS_PRESENT
elessair 0:f269e3021894 30 extern osMutexId singleton_mutex_id;
elessair 0:f269e3021894 31 #endif
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 /** Lock the singleton mutex
elessair 0:f269e3021894 34 *
elessair 0:f269e3021894 35 * This function is typically used to provide
elessair 0:f269e3021894 36 * exclusive access when initializing a
elessair 0:f269e3021894 37 * global object.
elessair 0:f269e3021894 38 */
elessair 0:f269e3021894 39 inline static void singleton_lock(void)
elessair 0:f269e3021894 40 {
elessair 0:f269e3021894 41 #ifdef MBED_CONF_RTOS_PRESENT
elessair 0:f269e3021894 42 osMutexWait(singleton_mutex_id, osWaitForever);
elessair 0:f269e3021894 43 #endif
elessair 0:f269e3021894 44 }
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 /** Unlock the singleton mutex
elessair 0:f269e3021894 47 *
elessair 0:f269e3021894 48 * This function is typically used to provide
elessair 0:f269e3021894 49 * exclusive access when initializing a
elessair 0:f269e3021894 50 * global object.
elessair 0:f269e3021894 51 */
elessair 0:f269e3021894 52 inline static void singleton_unlock(void)
elessair 0:f269e3021894 53 {
elessair 0:f269e3021894 54 #ifdef MBED_CONF_RTOS_PRESENT
elessair 0:f269e3021894 55 osMutexRelease (singleton_mutex_id);
elessair 0:f269e3021894 56 #endif
elessair 0:f269e3021894 57 }
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 /** Utility class for creating an using a singleton
elessair 0:f269e3021894 60 *
elessair 0:f269e3021894 61 * @Note Synchronization level: Thread safe
elessair 0:f269e3021894 62 *
elessair 0:f269e3021894 63 * @Note: This class must only be used in a static context -
elessair 0:f269e3021894 64 * this class must never be allocated or created on the
elessair 0:f269e3021894 65 * stack.
elessair 0:f269e3021894 66 *
elessair 0:f269e3021894 67 * @Note: This class is lazily initialized on first use.
elessair 0:f269e3021894 68 * This class is a POD type so if it is not used it will
elessair 0:f269e3021894 69 * be garbage collected.
elessair 0:f269e3021894 70 */
elessair 0:f269e3021894 71 template <class T>
elessair 0:f269e3021894 72 struct SingletonPtr {
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 /** Get a pointer to the underlying singleton
elessair 0:f269e3021894 75 *
elessair 0:f269e3021894 76 * @returns
elessair 0:f269e3021894 77 * A pointer to the singleton
elessair 0:f269e3021894 78 */
elessair 0:f269e3021894 79 T* get() {
elessair 0:f269e3021894 80 if (NULL == _ptr) {
elessair 0:f269e3021894 81 singleton_lock();
elessair 0:f269e3021894 82 if (NULL == _ptr) {
elessair 0:f269e3021894 83 _ptr = new (_data) T();
elessair 0:f269e3021894 84 }
elessair 0:f269e3021894 85 singleton_unlock();
elessair 0:f269e3021894 86 }
elessair 0:f269e3021894 87 // _ptr was not zero initialized or was
elessair 0:f269e3021894 88 // corrupted if this assert is hit
elessair 0:f269e3021894 89 MBED_ASSERT(_ptr == (T *)&_data);
elessair 0:f269e3021894 90 return _ptr;
elessair 0:f269e3021894 91 }
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 /** Get a pointer to the underlying singleton
elessair 0:f269e3021894 94 *
elessair 0:f269e3021894 95 * @returns
elessair 0:f269e3021894 96 * A pointer to the singleton
elessair 0:f269e3021894 97 */
elessair 0:f269e3021894 98 T* operator->() {
elessair 0:f269e3021894 99 return get();
elessair 0:f269e3021894 100 }
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 // This is zero initialized when in global scope
elessair 0:f269e3021894 103 T *_ptr;
elessair 0:f269e3021894 104 // Force data to be 4 byte aligned
elessair 0:f269e3021894 105 uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
elessair 0:f269e3021894 106 };
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 #endif
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 /** @}*/