Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pm_mutex.c Source File

pm_mutex.c

00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 
00034 #include "pm_mutex.h"
00035 
00036 #include <stdbool.h>
00037 #include <string.h>
00038 #include "nrf_error.h"
00039 #include "app_util_platform.h "
00040 
00041 
00042 
00043 /**@brief Locks the mutex defined by the mask.
00044  *
00045  * @param p_mutex pointer to the mutex storage.
00046  * @param mutex_mask the mask identifying the mutex position.
00047  *
00048  * @retval true if the mutex could be locked.
00049  * @retval false if the mutex was already locked.
00050  */
00051 static bool lock_by_mask(uint8_t * p_mutex, uint8_t mutex_mask)
00052 {
00053     bool success = false;
00054 
00055     if ( (*p_mutex & mutex_mask) == 0 )
00056     {
00057         CRITICAL_REGION_ENTER();
00058         if ( (*p_mutex & mutex_mask) == 0 )
00059         {
00060             *p_mutex |= mutex_mask;
00061 
00062             success = true;
00063         }
00064         CRITICAL_REGION_EXIT();
00065     }
00066 
00067     return ( success );
00068 }
00069 
00070 
00071 void pm_mutex_init(uint8_t * p_mutex, uint16_t mutex_size)
00072 {
00073     if (p_mutex != NULL)
00074     {
00075         memset(&p_mutex[0], 0, MUTEX_STORAGE_SIZE(mutex_size));
00076     }
00077 }
00078 
00079 
00080 bool pm_mutex_lock(uint8_t * p_mutex, uint16_t mutex_id)
00081 {
00082     if (p_mutex != NULL)
00083     {
00084         return ( lock_by_mask(&(p_mutex[mutex_id >> 3]), (1 << (mutex_id & 0x07))) );
00085     }
00086     else
00087     {
00088         return false;
00089     }
00090 }
00091 
00092 
00093 void pm_mutex_unlock(uint8_t * p_mutex, uint16_t mutex_id)
00094 {
00095     uint8_t mutex_base = mutex_id >> 3;
00096     uint8_t mutex_mask = (1 << (mutex_id & 0x07));
00097 
00098     if   ((p_mutex != NULL)
00099        && (p_mutex[mutex_base] & mutex_mask))
00100     {
00101         CRITICAL_REGION_ENTER();
00102         p_mutex[mutex_base] &= ~mutex_mask;
00103         CRITICAL_REGION_EXIT();
00104     }
00105 }
00106 
00107 
00108 uint16_t pm_mutex_lock_first_available(uint8_t * p_mutex, uint16_t mutex_size)
00109 {
00110     if (p_mutex != NULL)
00111     {
00112         for ( uint16_t i = 0; i < mutex_size; i++ )
00113         {
00114             if ( lock_by_mask(&(p_mutex[i >> 3]), 1 << (i & 0x07)) )
00115             {
00116                 return ( i );
00117             }
00118         }
00119     }
00120 
00121     return ( mutex_size );
00122 }
00123 
00124 
00125 bool pm_mutex_lock_status_get(uint8_t * p_mutex, uint16_t mutex_id)
00126 {
00127     if (p_mutex != NULL)
00128     {
00129         return ( (p_mutex[mutex_id >> 3] & (1 << (mutex_id & 0x07))) );
00130     }
00131     else
00132     {
00133         return true;
00134     }
00135 }