test
Fork of nrf51-sdk by
Embed:
(wiki syntax)
Show/hide line numbers
pm_buffer.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_buffer.h" 00035 00036 #include <stdbool.h> 00037 #include <string.h> 00038 #include "nrf_error.h" 00039 #include "pm_mutex.h" 00040 00041 00042 #define BUFFER_IS_VALID(p_buffer) ((p_buffer != NULL) \ 00043 && (p_buffer->p_memory != NULL) \ 00044 && (p_buffer->p_mutex != NULL)) 00045 00046 00047 00048 ret_code_t pm_buffer_init(pm_buffer_t * p_buffer, 00049 uint8_t * p_buffer_memory, 00050 uint32_t buffer_memory_size, 00051 uint8_t * p_mutex_memory, 00052 uint32_t mutex_memory_size, 00053 uint32_t n_blocks, 00054 uint32_t block_size) 00055 { 00056 if ( (p_buffer != NULL) 00057 && (p_buffer_memory != NULL) 00058 && (p_mutex_memory != NULL) 00059 && (buffer_memory_size >= (n_blocks*block_size)) 00060 && (mutex_memory_size >= MUTEX_STORAGE_SIZE(n_blocks)) 00061 && (n_blocks != 0) 00062 && (block_size != 0)) 00063 { 00064 p_buffer->p_memory = p_buffer_memory; 00065 p_buffer->p_mutex = p_mutex_memory; 00066 p_buffer->n_blocks = n_blocks; 00067 p_buffer->block_size = block_size; 00068 pm_mutex_init(p_buffer->p_mutex, n_blocks); 00069 00070 return NRF_SUCCESS; 00071 } 00072 else 00073 { 00074 return NRF_ERROR_INVALID_PARAM; 00075 } 00076 } 00077 00078 00079 uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks) 00080 { 00081 if (!BUFFER_IS_VALID(p_buffer)) 00082 { 00083 return ( BUFFER_INVALID_ID ); 00084 } 00085 00086 uint8_t first_locked_mutex = BUFFER_INVALID_ID; 00087 00088 for (uint8_t i = 0; i < p_buffer->n_blocks; i++) 00089 { 00090 if (pm_mutex_lock(p_buffer->p_mutex, i)) 00091 { 00092 if (first_locked_mutex == BUFFER_INVALID_ID) 00093 { 00094 first_locked_mutex = i; 00095 } 00096 if ((i - first_locked_mutex + 1) == n_blocks) 00097 { 00098 return first_locked_mutex; 00099 } 00100 } 00101 else if (first_locked_mutex != BUFFER_INVALID_ID) 00102 { 00103 for (uint8_t j = first_locked_mutex; j < i; j++) 00104 { 00105 pm_buffer_release(p_buffer, j); 00106 } 00107 first_locked_mutex = BUFFER_INVALID_ID; 00108 } 00109 } 00110 00111 return ( BUFFER_INVALID_ID ); 00112 } 00113 00114 00115 uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id) 00116 { 00117 if (!BUFFER_IS_VALID(p_buffer)) 00118 { 00119 return ( NULL ); 00120 } 00121 00122 if ( (id != BUFFER_INVALID_ID) 00123 && pm_mutex_lock_status_get(p_buffer->p_mutex, id) ) 00124 { 00125 return ( &p_buffer->p_memory[id*p_buffer->block_size] ); 00126 } 00127 else 00128 { 00129 return ( NULL ); 00130 } 00131 } 00132 00133 00134 void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id) 00135 { 00136 if ( BUFFER_IS_VALID(p_buffer) 00137 && (id != BUFFER_INVALID_ID) 00138 && pm_mutex_lock_status_get(p_buffer->p_mutex, id)) 00139 { 00140 pm_mutex_unlock(p_buffer->p_mutex, id); 00141 } 00142 }
Generated on Tue Jul 12 2022 15:51:28 by 1.7.2