Nordic stack and drivers for the mbed BLE API

Dependents:   BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate writable_gatt ... more

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 638:c90ae1400bf2 1 /*
Vincent Coubard 638:c90ae1400bf2 2 * Copyright (c) Nordic Semiconductor ASA
Vincent Coubard 638:c90ae1400bf2 3 * All rights reserved.
Vincent Coubard 638:c90ae1400bf2 4 *
Vincent Coubard 638:c90ae1400bf2 5 * Redistribution and use in source and binary forms, with or without modification,
Vincent Coubard 638:c90ae1400bf2 6 * are permitted provided that the following conditions are met:
Vincent Coubard 638:c90ae1400bf2 7 *
Vincent Coubard 638:c90ae1400bf2 8 * 1. Redistributions of source code must retain the above copyright notice, this
Vincent Coubard 638:c90ae1400bf2 9 * list of conditions and the following disclaimer.
Vincent Coubard 638:c90ae1400bf2 10 *
Vincent Coubard 638:c90ae1400bf2 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Vincent Coubard 638:c90ae1400bf2 12 * list of conditions and the following disclaimer in the documentation and/or
Vincent Coubard 638:c90ae1400bf2 13 * other materials provided with the distribution.
Vincent Coubard 638:c90ae1400bf2 14 *
Vincent Coubard 638:c90ae1400bf2 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Vincent Coubard 638:c90ae1400bf2 16 * contributors to this software may be used to endorse or promote products
Vincent Coubard 638:c90ae1400bf2 17 * derived from this software without specific prior written permission.
Vincent Coubard 638:c90ae1400bf2 18 *
Vincent Coubard 638:c90ae1400bf2 19 *
Vincent Coubard 638:c90ae1400bf2 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Vincent Coubard 638:c90ae1400bf2 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Vincent Coubard 638:c90ae1400bf2 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Vincent Coubard 638:c90ae1400bf2 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Vincent Coubard 638:c90ae1400bf2 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Vincent Coubard 638:c90ae1400bf2 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Vincent Coubard 638:c90ae1400bf2 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Vincent Coubard 638:c90ae1400bf2 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Vincent Coubard 638:c90ae1400bf2 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Vincent Coubard 638:c90ae1400bf2 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Vincent Coubard 638:c90ae1400bf2 30 *
Vincent Coubard 638:c90ae1400bf2 31 */
Vincent Coubard 638:c90ae1400bf2 32
Vincent Coubard 638:c90ae1400bf2 33
Vincent Coubard 638:c90ae1400bf2 34 #include "pm_mutex.h"
Vincent Coubard 638:c90ae1400bf2 35
Vincent Coubard 638:c90ae1400bf2 36 #include <stdbool.h>
Vincent Coubard 638:c90ae1400bf2 37 #include <string.h>
Vincent Coubard 638:c90ae1400bf2 38 #include "nrf_error.h"
Vincent Coubard 638:c90ae1400bf2 39 #include "app_util_platform.h"
Vincent Coubard 638:c90ae1400bf2 40
Vincent Coubard 638:c90ae1400bf2 41
Vincent Coubard 638:c90ae1400bf2 42
Vincent Coubard 638:c90ae1400bf2 43 /**@brief Locks the mutex defined by the mask.
Vincent Coubard 638:c90ae1400bf2 44 *
Vincent Coubard 638:c90ae1400bf2 45 * @param p_mutex pointer to the mutex storage.
Vincent Coubard 638:c90ae1400bf2 46 * @param mutex_mask the mask identifying the mutex position.
Vincent Coubard 638:c90ae1400bf2 47 *
Vincent Coubard 638:c90ae1400bf2 48 * @retval true if the mutex could be locked.
Vincent Coubard 638:c90ae1400bf2 49 * @retval false if the mutex was already locked.
Vincent Coubard 638:c90ae1400bf2 50 */
Vincent Coubard 638:c90ae1400bf2 51 static bool lock_by_mask(uint8_t * p_mutex, uint8_t mutex_mask)
Vincent Coubard 638:c90ae1400bf2 52 {
Vincent Coubard 638:c90ae1400bf2 53 bool success = false;
Vincent Coubard 638:c90ae1400bf2 54
Vincent Coubard 638:c90ae1400bf2 55 if ( (*p_mutex & mutex_mask) == 0 )
Vincent Coubard 638:c90ae1400bf2 56 {
Vincent Coubard 638:c90ae1400bf2 57 CRITICAL_REGION_ENTER();
Vincent Coubard 638:c90ae1400bf2 58 if ( (*p_mutex & mutex_mask) == 0 )
Vincent Coubard 638:c90ae1400bf2 59 {
Vincent Coubard 638:c90ae1400bf2 60 *p_mutex |= mutex_mask;
Vincent Coubard 638:c90ae1400bf2 61
Vincent Coubard 638:c90ae1400bf2 62 success = true;
Vincent Coubard 638:c90ae1400bf2 63 }
Vincent Coubard 638:c90ae1400bf2 64 CRITICAL_REGION_EXIT();
Vincent Coubard 638:c90ae1400bf2 65 }
Vincent Coubard 638:c90ae1400bf2 66
Vincent Coubard 638:c90ae1400bf2 67 return ( success );
Vincent Coubard 638:c90ae1400bf2 68 }
Vincent Coubard 638:c90ae1400bf2 69
Vincent Coubard 638:c90ae1400bf2 70
Vincent Coubard 638:c90ae1400bf2 71 void pm_mutex_init(uint8_t * p_mutex, uint16_t mutex_size)
Vincent Coubard 638:c90ae1400bf2 72 {
Vincent Coubard 638:c90ae1400bf2 73 if (p_mutex != NULL)
Vincent Coubard 638:c90ae1400bf2 74 {
Vincent Coubard 638:c90ae1400bf2 75 memset(&p_mutex[0], 0, MUTEX_STORAGE_SIZE(mutex_size));
Vincent Coubard 638:c90ae1400bf2 76 }
Vincent Coubard 638:c90ae1400bf2 77 }
Vincent Coubard 638:c90ae1400bf2 78
Vincent Coubard 638:c90ae1400bf2 79
Vincent Coubard 638:c90ae1400bf2 80 bool pm_mutex_lock(uint8_t * p_mutex, uint16_t mutex_id)
Vincent Coubard 638:c90ae1400bf2 81 {
Vincent Coubard 638:c90ae1400bf2 82 if (p_mutex != NULL)
Vincent Coubard 638:c90ae1400bf2 83 {
Vincent Coubard 638:c90ae1400bf2 84 return ( lock_by_mask(&(p_mutex[mutex_id >> 3]), (1 << (mutex_id & 0x07))) );
Vincent Coubard 638:c90ae1400bf2 85 }
Vincent Coubard 638:c90ae1400bf2 86 else
Vincent Coubard 638:c90ae1400bf2 87 {
Vincent Coubard 638:c90ae1400bf2 88 return false;
Vincent Coubard 638:c90ae1400bf2 89 }
Vincent Coubard 638:c90ae1400bf2 90 }
Vincent Coubard 638:c90ae1400bf2 91
Vincent Coubard 638:c90ae1400bf2 92
Vincent Coubard 638:c90ae1400bf2 93 void pm_mutex_unlock(uint8_t * p_mutex, uint16_t mutex_id)
Vincent Coubard 638:c90ae1400bf2 94 {
Vincent Coubard 638:c90ae1400bf2 95 uint8_t mutex_base = mutex_id >> 3;
Vincent Coubard 638:c90ae1400bf2 96 uint8_t mutex_mask = (1 << (mutex_id & 0x07));
Vincent Coubard 638:c90ae1400bf2 97
Vincent Coubard 638:c90ae1400bf2 98 if ((p_mutex != NULL)
Vincent Coubard 638:c90ae1400bf2 99 && (p_mutex[mutex_base] & mutex_mask))
Vincent Coubard 638:c90ae1400bf2 100 {
Vincent Coubard 638:c90ae1400bf2 101 CRITICAL_REGION_ENTER();
Vincent Coubard 638:c90ae1400bf2 102 p_mutex[mutex_base] &= ~mutex_mask;
Vincent Coubard 638:c90ae1400bf2 103 CRITICAL_REGION_EXIT();
Vincent Coubard 638:c90ae1400bf2 104 }
Vincent Coubard 638:c90ae1400bf2 105 }
Vincent Coubard 638:c90ae1400bf2 106
Vincent Coubard 638:c90ae1400bf2 107
Vincent Coubard 638:c90ae1400bf2 108 uint16_t pm_mutex_lock_first_available(uint8_t * p_mutex, uint16_t mutex_size)
Vincent Coubard 638:c90ae1400bf2 109 {
Vincent Coubard 638:c90ae1400bf2 110 if (p_mutex != NULL)
Vincent Coubard 638:c90ae1400bf2 111 {
Vincent Coubard 638:c90ae1400bf2 112 for ( uint16_t i = 0; i < mutex_size; i++ )
Vincent Coubard 638:c90ae1400bf2 113 {
Vincent Coubard 638:c90ae1400bf2 114 if ( lock_by_mask(&(p_mutex[i >> 3]), 1 << (i & 0x07)) )
Vincent Coubard 638:c90ae1400bf2 115 {
Vincent Coubard 638:c90ae1400bf2 116 return ( i );
Vincent Coubard 638:c90ae1400bf2 117 }
Vincent Coubard 638:c90ae1400bf2 118 }
Vincent Coubard 638:c90ae1400bf2 119 }
Vincent Coubard 638:c90ae1400bf2 120
Vincent Coubard 638:c90ae1400bf2 121 return ( mutex_size );
Vincent Coubard 638:c90ae1400bf2 122 }
Vincent Coubard 638:c90ae1400bf2 123
Vincent Coubard 638:c90ae1400bf2 124
Vincent Coubard 638:c90ae1400bf2 125 bool pm_mutex_lock_status_get(uint8_t * p_mutex, uint16_t mutex_id)
Vincent Coubard 638:c90ae1400bf2 126 {
Vincent Coubard 638:c90ae1400bf2 127 if (p_mutex != NULL)
Vincent Coubard 638:c90ae1400bf2 128 {
Vincent Coubard 638:c90ae1400bf2 129 return ( (p_mutex[mutex_id >> 3] & (1 << (mutex_id & 0x07))) );
Vincent Coubard 638:c90ae1400bf2 130 }
Vincent Coubard 638:c90ae1400bf2 131 else
Vincent Coubard 638:c90ae1400bf2 132 {
Vincent Coubard 638:c90ae1400bf2 133 return true;
Vincent Coubard 638:c90ae1400bf2 134 }
Vincent Coubard 638:c90ae1400bf2 135 }