cvb

Fork of nrf51-sdk by Lancaster University

Committer:
Jonathan Austin
Date:
Wed Apr 06 23:55:04 2016 +0100
Revision:
0:bc2961fa1ef0
Synchronized with git rev 90647e3

Who changed what in which revision?

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