Pratyush Mallick
/
nano_dac
this is testing
noos_mbed/util/circular_buffer.c@0:e8a1ba50c46b, 2021-01-14 (annotated)
- Committer:
- pmallick
- Date:
- Thu Jan 14 19:12:57 2021 +0530
- Revision:
- 0:e8a1ba50c46b
this is testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmallick | 0:e8a1ba50c46b | 1 | /***************************************************************************//** |
pmallick | 0:e8a1ba50c46b | 2 | * @file circular_buffer.c |
pmallick | 0:e8a1ba50c46b | 3 | * @brief Circular buffer implementation |
pmallick | 0:e8a1ba50c46b | 4 | * @author Mihail Chindris (mihail.chindris@analog.com) |
pmallick | 0:e8a1ba50c46b | 5 | ******************************************************************************** |
pmallick | 0:e8a1ba50c46b | 6 | * @copyright |
pmallick | 0:e8a1ba50c46b | 7 | * Copyright 2020(c) Analog Devices, Inc. |
pmallick | 0:e8a1ba50c46b | 8 | * |
pmallick | 0:e8a1ba50c46b | 9 | * All rights reserved. |
pmallick | 0:e8a1ba50c46b | 10 | * |
pmallick | 0:e8a1ba50c46b | 11 | * Redistribution and use in source and binary forms, with or without |
pmallick | 0:e8a1ba50c46b | 12 | * modification, are permitted provided that the following conditions are met: |
pmallick | 0:e8a1ba50c46b | 13 | * - Redistributions of source code must retain the above copyright |
pmallick | 0:e8a1ba50c46b | 14 | * notice, this list of conditions and the following disclaimer. |
pmallick | 0:e8a1ba50c46b | 15 | * - Redistributions in binary form must reproduce the above copyright |
pmallick | 0:e8a1ba50c46b | 16 | * notice, this list of conditions and the following disclaimer in |
pmallick | 0:e8a1ba50c46b | 17 | * the documentation and/or other materials provided with the |
pmallick | 0:e8a1ba50c46b | 18 | * distribution. |
pmallick | 0:e8a1ba50c46b | 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
pmallick | 0:e8a1ba50c46b | 20 | * contributors may be used to endorse or promote products derived |
pmallick | 0:e8a1ba50c46b | 21 | * from this software without specific prior written permission. |
pmallick | 0:e8a1ba50c46b | 22 | * - The use of this software may or may not infringe the patent rights |
pmallick | 0:e8a1ba50c46b | 23 | * of one or more patent holders. This license does not release you |
pmallick | 0:e8a1ba50c46b | 24 | * from the requirement that you obtain separate licenses from these |
pmallick | 0:e8a1ba50c46b | 25 | * patent holders to use this software. |
pmallick | 0:e8a1ba50c46b | 26 | * - Use of the software either in source or binary form, must be run |
pmallick | 0:e8a1ba50c46b | 27 | * on or directly connected to an Analog Devices Inc. component. |
pmallick | 0:e8a1ba50c46b | 28 | * |
pmallick | 0:e8a1ba50c46b | 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
pmallick | 0:e8a1ba50c46b | 30 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
pmallick | 0:e8a1ba50c46b | 31 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
pmallick | 0:e8a1ba50c46b | 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
pmallick | 0:e8a1ba50c46b | 33 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
pmallick | 0:e8a1ba50c46b | 34 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
pmallick | 0:e8a1ba50c46b | 35 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
pmallick | 0:e8a1ba50c46b | 36 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
pmallick | 0:e8a1ba50c46b | 37 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
pmallick | 0:e8a1ba50c46b | 38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
pmallick | 0:e8a1ba50c46b | 39 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 40 | |
pmallick | 0:e8a1ba50c46b | 41 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 42 | /***************************** Include Files **********************************/ |
pmallick | 0:e8a1ba50c46b | 43 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 44 | |
pmallick | 0:e8a1ba50c46b | 45 | #include <string.h> |
pmallick | 0:e8a1ba50c46b | 46 | #include <stdlib.h> |
pmallick | 0:e8a1ba50c46b | 47 | #include <stdbool.h> |
pmallick | 0:e8a1ba50c46b | 48 | #include "circular_buffer.h" |
pmallick | 0:e8a1ba50c46b | 49 | #include "error.h" |
pmallick | 0:e8a1ba50c46b | 50 | #include "util.h" |
pmallick | 0:e8a1ba50c46b | 51 | |
pmallick | 0:e8a1ba50c46b | 52 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 53 | /*************************** Types Declarations *******************************/ |
pmallick | 0:e8a1ba50c46b | 54 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 55 | |
pmallick | 0:e8a1ba50c46b | 56 | /** |
pmallick | 0:e8a1ba50c46b | 57 | * @struct cb_ptr |
pmallick | 0:e8a1ba50c46b | 58 | * @brief Circular buffer pointer |
pmallick | 0:e8a1ba50c46b | 59 | */ |
pmallick | 0:e8a1ba50c46b | 60 | struct cb_ptr { |
pmallick | 0:e8a1ba50c46b | 61 | /** Index of data in the buffer */ |
pmallick | 0:e8a1ba50c46b | 62 | uint32_t idx; |
pmallick | 0:e8a1ba50c46b | 63 | /** Counts the number of times idx exceeds the liniar buffer */ |
pmallick | 0:e8a1ba50c46b | 64 | uint32_t spin_count; |
pmallick | 0:e8a1ba50c46b | 65 | /** Set if async transaction is active */ |
pmallick | 0:e8a1ba50c46b | 66 | bool async_started; |
pmallick | 0:e8a1ba50c46b | 67 | /** Number of bytes to update after an async transaction is finished */ |
pmallick | 0:e8a1ba50c46b | 68 | uint32_t async_size; |
pmallick | 0:e8a1ba50c46b | 69 | }; |
pmallick | 0:e8a1ba50c46b | 70 | |
pmallick | 0:e8a1ba50c46b | 71 | /** |
pmallick | 0:e8a1ba50c46b | 72 | * @struct circular_buffer |
pmallick | 0:e8a1ba50c46b | 73 | * @brief Circular buffer descriptor |
pmallick | 0:e8a1ba50c46b | 74 | */ |
pmallick | 0:e8a1ba50c46b | 75 | struct circular_buffer { |
pmallick | 0:e8a1ba50c46b | 76 | /** Size of the buffer in bytes */ |
pmallick | 0:e8a1ba50c46b | 77 | uint32_t size; |
pmallick | 0:e8a1ba50c46b | 78 | /** Address of the buffer */ |
pmallick | 0:e8a1ba50c46b | 79 | int8_t *buff; |
pmallick | 0:e8a1ba50c46b | 80 | /** Write pointer */ |
pmallick | 0:e8a1ba50c46b | 81 | struct cb_ptr write; |
pmallick | 0:e8a1ba50c46b | 82 | /** Read pointer */ |
pmallick | 0:e8a1ba50c46b | 83 | struct cb_ptr read; |
pmallick | 0:e8a1ba50c46b | 84 | }; |
pmallick | 0:e8a1ba50c46b | 85 | |
pmallick | 0:e8a1ba50c46b | 86 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 87 | /************************ Functions Definitions *******************************/ |
pmallick | 0:e8a1ba50c46b | 88 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 89 | |
pmallick | 0:e8a1ba50c46b | 90 | /** |
pmallick | 0:e8a1ba50c46b | 91 | * @brief Create circular buffer structure |
pmallick | 0:e8a1ba50c46b | 92 | * |
pmallick | 0:e8a1ba50c46b | 93 | * @note Circular buffer implementation is thread safe for one write |
pmallick | 0:e8a1ba50c46b | 94 | * and one reader. |
pmallick | 0:e8a1ba50c46b | 95 | * If multiple writer or multiple readers access the circular buffer then |
pmallick | 0:e8a1ba50c46b | 96 | * function that updates the structure should be called inside a critical |
pmallick | 0:e8a1ba50c46b | 97 | * critical section. |
pmallick | 0:e8a1ba50c46b | 98 | * |
pmallick | 0:e8a1ba50c46b | 99 | * @param desc - Where to store the circular buffer reference |
pmallick | 0:e8a1ba50c46b | 100 | * @param buff_size - Buffer size |
pmallick | 0:e8a1ba50c46b | 101 | * @return |
pmallick | 0:e8a1ba50c46b | 102 | * - \ref SUCCESS : On success |
pmallick | 0:e8a1ba50c46b | 103 | * - \ref FAILURE : Otherwise |
pmallick | 0:e8a1ba50c46b | 104 | */ |
pmallick | 0:e8a1ba50c46b | 105 | int32_t cb_init(struct circular_buffer **desc, uint32_t buff_size) |
pmallick | 0:e8a1ba50c46b | 106 | { |
pmallick | 0:e8a1ba50c46b | 107 | struct circular_buffer *ldesc; |
pmallick | 0:e8a1ba50c46b | 108 | |
pmallick | 0:e8a1ba50c46b | 109 | if (!desc || !buff_size) |
pmallick | 0:e8a1ba50c46b | 110 | return -EINVAL; |
pmallick | 0:e8a1ba50c46b | 111 | |
pmallick | 0:e8a1ba50c46b | 112 | ldesc = (struct circular_buffer*)calloc(1, sizeof(*ldesc)); |
pmallick | 0:e8a1ba50c46b | 113 | if (!ldesc) |
pmallick | 0:e8a1ba50c46b | 114 | return -ENOMEM; |
pmallick | 0:e8a1ba50c46b | 115 | |
pmallick | 0:e8a1ba50c46b | 116 | *desc = ldesc; |
pmallick | 0:e8a1ba50c46b | 117 | |
pmallick | 0:e8a1ba50c46b | 118 | ldesc->size = buff_size; |
pmallick | 0:e8a1ba50c46b | 119 | ldesc->buff = calloc(1, buff_size); |
pmallick | 0:e8a1ba50c46b | 120 | if (!ldesc->buff) { |
pmallick | 0:e8a1ba50c46b | 121 | free(ldesc); |
pmallick | 0:e8a1ba50c46b | 122 | return -ENOMEM; |
pmallick | 0:e8a1ba50c46b | 123 | } |
pmallick | 0:e8a1ba50c46b | 124 | |
pmallick | 0:e8a1ba50c46b | 125 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 126 | } |
pmallick | 0:e8a1ba50c46b | 127 | |
pmallick | 0:e8a1ba50c46b | 128 | /** |
pmallick | 0:e8a1ba50c46b | 129 | * @brief Free the resources allocated for the circular buffer structure |
pmallick | 0:e8a1ba50c46b | 130 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 131 | * @return |
pmallick | 0:e8a1ba50c46b | 132 | * - \ref SUCCESS : On success |
pmallick | 0:e8a1ba50c46b | 133 | * - \ref FAILURE : Otherwise |
pmallick | 0:e8a1ba50c46b | 134 | */ |
pmallick | 0:e8a1ba50c46b | 135 | int32_t cb_remove(struct circular_buffer *desc) |
pmallick | 0:e8a1ba50c46b | 136 | { |
pmallick | 0:e8a1ba50c46b | 137 | if (!desc) |
pmallick | 0:e8a1ba50c46b | 138 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 139 | |
pmallick | 0:e8a1ba50c46b | 140 | if (desc->buff) |
pmallick | 0:e8a1ba50c46b | 141 | free(desc->buff); |
pmallick | 0:e8a1ba50c46b | 142 | free(desc); |
pmallick | 0:e8a1ba50c46b | 143 | |
pmallick | 0:e8a1ba50c46b | 144 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 145 | } |
pmallick | 0:e8a1ba50c46b | 146 | |
pmallick | 0:e8a1ba50c46b | 147 | /** |
pmallick | 0:e8a1ba50c46b | 148 | * @brief Get the number of elements in the buffer |
pmallick | 0:e8a1ba50c46b | 149 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 150 | * @param size - Where to store size of data available to read |
pmallick | 0:e8a1ba50c46b | 151 | * @return |
pmallick | 0:e8a1ba50c46b | 152 | * - \ref SUCCESS - No errors |
pmallick | 0:e8a1ba50c46b | 153 | * - -EINVAL - Wrong parameters used |
pmallick | 0:e8a1ba50c46b | 154 | * - -EOVERRUN - A buffer overrun occurred |
pmallick | 0:e8a1ba50c46b | 155 | */ |
pmallick | 0:e8a1ba50c46b | 156 | int32_t cb_size(struct circular_buffer *desc, uint32_t *size) |
pmallick | 0:e8a1ba50c46b | 157 | { |
pmallick | 0:e8a1ba50c46b | 158 | uint32_t nb_spins; |
pmallick | 0:e8a1ba50c46b | 159 | |
pmallick | 0:e8a1ba50c46b | 160 | if (!desc || !size) |
pmallick | 0:e8a1ba50c46b | 161 | return -EINVAL; |
pmallick | 0:e8a1ba50c46b | 162 | |
pmallick | 0:e8a1ba50c46b | 163 | if (desc->write.spin_count > desc->read.spin_count) |
pmallick | 0:e8a1ba50c46b | 164 | nb_spins = desc->write.spin_count - desc->read.spin_count; |
pmallick | 0:e8a1ba50c46b | 165 | else |
pmallick | 0:e8a1ba50c46b | 166 | /* Integer overflow on desc->write.spin_count */ |
pmallick | 0:e8a1ba50c46b | 167 | nb_spins = UINT32_MAX - desc->read.spin_count + |
pmallick | 0:e8a1ba50c46b | 168 | desc->write.spin_count + 1; |
pmallick | 0:e8a1ba50c46b | 169 | |
pmallick | 0:e8a1ba50c46b | 170 | if (nb_spins > 0) |
pmallick | 0:e8a1ba50c46b | 171 | *size = desc->size + desc->write.idx - desc->read.idx; |
pmallick | 0:e8a1ba50c46b | 172 | else |
pmallick | 0:e8a1ba50c46b | 173 | *size = desc->write.idx - desc->read.idx; |
pmallick | 0:e8a1ba50c46b | 174 | |
pmallick | 0:e8a1ba50c46b | 175 | if (*size > desc->size) { |
pmallick | 0:e8a1ba50c46b | 176 | *size = desc->size; |
pmallick | 0:e8a1ba50c46b | 177 | return -EOVERRUN; |
pmallick | 0:e8a1ba50c46b | 178 | } |
pmallick | 0:e8a1ba50c46b | 179 | |
pmallick | 0:e8a1ba50c46b | 180 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 181 | } |
pmallick | 0:e8a1ba50c46b | 182 | |
pmallick | 0:e8a1ba50c46b | 183 | /* |
pmallick | 0:e8a1ba50c46b | 184 | * Functionality described at cb_prepare_async_write/read having the is_read |
pmallick | 0:e8a1ba50c46b | 185 | * parameter to specifiy if it is a read or write operation |
pmallick | 0:e8a1ba50c46b | 186 | */ |
pmallick | 0:e8a1ba50c46b | 187 | static int32_t cb_prepare_async_operation(struct circular_buffer *desc, |
pmallick | 0:e8a1ba50c46b | 188 | uint32_t requested_size, |
pmallick | 0:e8a1ba50c46b | 189 | void **buff, |
pmallick | 0:e8a1ba50c46b | 190 | uint32_t *raw_size_available, |
pmallick | 0:e8a1ba50c46b | 191 | bool is_read) |
pmallick | 0:e8a1ba50c46b | 192 | { |
pmallick | 0:e8a1ba50c46b | 193 | struct cb_ptr *ptr; |
pmallick | 0:e8a1ba50c46b | 194 | uint32_t available_size; |
pmallick | 0:e8a1ba50c46b | 195 | uint32_t ret; |
pmallick | 0:e8a1ba50c46b | 196 | |
pmallick | 0:e8a1ba50c46b | 197 | if (!desc || !buff || !raw_size_available) |
pmallick | 0:e8a1ba50c46b | 198 | return -EINVAL; |
pmallick | 0:e8a1ba50c46b | 199 | |
pmallick | 0:e8a1ba50c46b | 200 | ret = SUCCESS; |
pmallick | 0:e8a1ba50c46b | 201 | /* Select if read or write index will be updated */ |
pmallick | 0:e8a1ba50c46b | 202 | ptr = is_read ? &desc->read : &desc->write; |
pmallick | 0:e8a1ba50c46b | 203 | |
pmallick | 0:e8a1ba50c46b | 204 | /* Only one transaction type possible at a single time */ |
pmallick | 0:e8a1ba50c46b | 205 | if (ptr->async_started) |
pmallick | 0:e8a1ba50c46b | 206 | return -EBUSY; |
pmallick | 0:e8a1ba50c46b | 207 | |
pmallick | 0:e8a1ba50c46b | 208 | if (is_read) { |
pmallick | 0:e8a1ba50c46b | 209 | ret = cb_size(desc, &available_size); |
pmallick | 0:e8a1ba50c46b | 210 | if (ret == -EOVERRUN) { |
pmallick | 0:e8a1ba50c46b | 211 | /* Update read index */ |
pmallick | 0:e8a1ba50c46b | 212 | desc->read.spin_count = desc->write.spin_count - 1; |
pmallick | 0:e8a1ba50c46b | 213 | desc->read.idx = desc->write.idx; |
pmallick | 0:e8a1ba50c46b | 214 | } |
pmallick | 0:e8a1ba50c46b | 215 | |
pmallick | 0:e8a1ba50c46b | 216 | /* We can only read available data */ |
pmallick | 0:e8a1ba50c46b | 217 | requested_size = min(requested_size, available_size); |
pmallick | 0:e8a1ba50c46b | 218 | if (!requested_size) |
pmallick | 0:e8a1ba50c46b | 219 | return -EAGAIN; |
pmallick | 0:e8a1ba50c46b | 220 | } |
pmallick | 0:e8a1ba50c46b | 221 | |
pmallick | 0:e8a1ba50c46b | 222 | /* Size to end of buffer */ |
pmallick | 0:e8a1ba50c46b | 223 | ptr->async_size = min(requested_size, desc->size - ptr->idx); |
pmallick | 0:e8a1ba50c46b | 224 | |
pmallick | 0:e8a1ba50c46b | 225 | *raw_size_available = ptr->async_size; |
pmallick | 0:e8a1ba50c46b | 226 | |
pmallick | 0:e8a1ba50c46b | 227 | /* Convert index to address in the buffer */ |
pmallick | 0:e8a1ba50c46b | 228 | *buff = (void *)(desc->buff + ptr->idx); |
pmallick | 0:e8a1ba50c46b | 229 | |
pmallick | 0:e8a1ba50c46b | 230 | ptr->async_started = true; |
pmallick | 0:e8a1ba50c46b | 231 | |
pmallick | 0:e8a1ba50c46b | 232 | return ret; |
pmallick | 0:e8a1ba50c46b | 233 | } |
pmallick | 0:e8a1ba50c46b | 234 | |
pmallick | 0:e8a1ba50c46b | 235 | /* |
pmallick | 0:e8a1ba50c46b | 236 | * Functionality described at cb_end_async_write/read having the is_read |
pmallick | 0:e8a1ba50c46b | 237 | * parameter to specifiy if it is a read or write operation |
pmallick | 0:e8a1ba50c46b | 238 | */ |
pmallick | 0:e8a1ba50c46b | 239 | static int32_t cb_end_async_operation(struct circular_buffer *desc, |
pmallick | 0:e8a1ba50c46b | 240 | bool is_read) |
pmallick | 0:e8a1ba50c46b | 241 | { |
pmallick | 0:e8a1ba50c46b | 242 | struct cb_ptr *ptr; |
pmallick | 0:e8a1ba50c46b | 243 | uint32_t new_val; |
pmallick | 0:e8a1ba50c46b | 244 | |
pmallick | 0:e8a1ba50c46b | 245 | |
pmallick | 0:e8a1ba50c46b | 246 | if (!desc) |
pmallick | 0:e8a1ba50c46b | 247 | return -EINVAL; |
pmallick | 0:e8a1ba50c46b | 248 | |
pmallick | 0:e8a1ba50c46b | 249 | /* Select if read or write index will be updated */ |
pmallick | 0:e8a1ba50c46b | 250 | ptr = is_read ? &desc->read : &desc->write; |
pmallick | 0:e8a1ba50c46b | 251 | |
pmallick | 0:e8a1ba50c46b | 252 | /* Transaction not started */ |
pmallick | 0:e8a1ba50c46b | 253 | if (!ptr->async_started) |
pmallick | 0:e8a1ba50c46b | 254 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 255 | |
pmallick | 0:e8a1ba50c46b | 256 | /* Update pointer value */ |
pmallick | 0:e8a1ba50c46b | 257 | new_val = ptr->idx + ptr->async_size; |
pmallick | 0:e8a1ba50c46b | 258 | if (new_val >= desc->size) { |
pmallick | 0:e8a1ba50c46b | 259 | ptr->spin_count++; |
pmallick | 0:e8a1ba50c46b | 260 | new_val %= desc->size; |
pmallick | 0:e8a1ba50c46b | 261 | } |
pmallick | 0:e8a1ba50c46b | 262 | ptr->idx = new_val; |
pmallick | 0:e8a1ba50c46b | 263 | ptr->async_size = 0; |
pmallick | 0:e8a1ba50c46b | 264 | ptr->async_started = false; |
pmallick | 0:e8a1ba50c46b | 265 | |
pmallick | 0:e8a1ba50c46b | 266 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 267 | } |
pmallick | 0:e8a1ba50c46b | 268 | |
pmallick | 0:e8a1ba50c46b | 269 | /* |
pmallick | 0:e8a1ba50c46b | 270 | * Functionality described at cb_write/read having the is_read |
pmallick | 0:e8a1ba50c46b | 271 | * parameter to specifiy if it is a read or write operation |
pmallick | 0:e8a1ba50c46b | 272 | */ |
pmallick | 0:e8a1ba50c46b | 273 | static int32_t cb_operation(struct circular_buffer *desc, |
pmallick | 0:e8a1ba50c46b | 274 | void *data, uint32_t size, |
pmallick | 0:e8a1ba50c46b | 275 | bool is_read) |
pmallick | 0:e8a1ba50c46b | 276 | { |
pmallick | 0:e8a1ba50c46b | 277 | uint8_t *buff; |
pmallick | 0:e8a1ba50c46b | 278 | uint32_t available_size; |
pmallick | 0:e8a1ba50c46b | 279 | int32_t ret; |
pmallick | 0:e8a1ba50c46b | 280 | uint32_t i; |
pmallick | 0:e8a1ba50c46b | 281 | bool sticky_overrun; |
pmallick | 0:e8a1ba50c46b | 282 | |
pmallick | 0:e8a1ba50c46b | 283 | if (!desc || !data || !size) |
pmallick | 0:e8a1ba50c46b | 284 | return -EINVAL; |
pmallick | 0:e8a1ba50c46b | 285 | |
pmallick | 0:e8a1ba50c46b | 286 | sticky_overrun = 0; |
pmallick | 0:e8a1ba50c46b | 287 | i = 0; |
pmallick | 0:e8a1ba50c46b | 288 | while (i < size) { |
pmallick | 0:e8a1ba50c46b | 289 | do { |
pmallick | 0:e8a1ba50c46b | 290 | ret = cb_prepare_async_operation(desc, size - i, |
pmallick | 0:e8a1ba50c46b | 291 | (void **)&buff, |
pmallick | 0:e8a1ba50c46b | 292 | &available_size, |
pmallick | 0:e8a1ba50c46b | 293 | is_read); |
pmallick | 0:e8a1ba50c46b | 294 | } while (ret == -EBUSY || ret == -EAGAIN); |
pmallick | 0:e8a1ba50c46b | 295 | if (ret == -EOVERRUN) |
pmallick | 0:e8a1ba50c46b | 296 | sticky_overrun = true; |
pmallick | 0:e8a1ba50c46b | 297 | |
pmallick | 0:e8a1ba50c46b | 298 | if (is_read) |
pmallick | 0:e8a1ba50c46b | 299 | memcpy((uint8_t *)data + i, buff, available_size); |
pmallick | 0:e8a1ba50c46b | 300 | else |
pmallick | 0:e8a1ba50c46b | 301 | memcpy(buff, (uint8_t *)data + i, available_size); |
pmallick | 0:e8a1ba50c46b | 302 | |
pmallick | 0:e8a1ba50c46b | 303 | cb_end_async_operation(desc, is_read); |
pmallick | 0:e8a1ba50c46b | 304 | |
pmallick | 0:e8a1ba50c46b | 305 | i += available_size; |
pmallick | 0:e8a1ba50c46b | 306 | } |
pmallick | 0:e8a1ba50c46b | 307 | |
pmallick | 0:e8a1ba50c46b | 308 | if (sticky_overrun) |
pmallick | 0:e8a1ba50c46b | 309 | return -EOVERRUN; |
pmallick | 0:e8a1ba50c46b | 310 | |
pmallick | 0:e8a1ba50c46b | 311 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 312 | } |
pmallick | 0:e8a1ba50c46b | 313 | |
pmallick | 0:e8a1ba50c46b | 314 | /** |
pmallick | 0:e8a1ba50c46b | 315 | * @brief Prepare asynchronous write |
pmallick | 0:e8a1ba50c46b | 316 | * |
pmallick | 0:e8a1ba50c46b | 317 | * Get the inside raw buffer to be used in DMA transactions. |
pmallick | 0:e8a1ba50c46b | 318 | * |
pmallick | 0:e8a1ba50c46b | 319 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 320 | * @param size_to_write - Number of bytes needed to write to the buffer. |
pmallick | 0:e8a1ba50c46b | 321 | * @param write_buff - Address where to store the buffer where to write to. |
pmallick | 0:e8a1ba50c46b | 322 | * @param size_avilable - min(size_to_write, size until end of allocated buffer) |
pmallick | 0:e8a1ba50c46b | 323 | * @return |
pmallick | 0:e8a1ba50c46b | 324 | * - \ref SUCCESS - No errors |
pmallick | 0:e8a1ba50c46b | 325 | * - -EINVAL - Wrong parameters used |
pmallick | 0:e8a1ba50c46b | 326 | * - -EBUSY - Asynchronous transaction already started |
pmallick | 0:e8a1ba50c46b | 327 | */ |
pmallick | 0:e8a1ba50c46b | 328 | int32_t cb_prepare_async_write(struct circular_buffer *desc, |
pmallick | 0:e8a1ba50c46b | 329 | uint32_t size_to_write, |
pmallick | 0:e8a1ba50c46b | 330 | void **write_buff, |
pmallick | 0:e8a1ba50c46b | 331 | uint32_t *size_avilable) |
pmallick | 0:e8a1ba50c46b | 332 | { |
pmallick | 0:e8a1ba50c46b | 333 | return cb_prepare_async_operation(desc, size_to_write, write_buff, |
pmallick | 0:e8a1ba50c46b | 334 | size_avilable, 0); |
pmallick | 0:e8a1ba50c46b | 335 | } |
pmallick | 0:e8a1ba50c46b | 336 | |
pmallick | 0:e8a1ba50c46b | 337 | /** |
pmallick | 0:e8a1ba50c46b | 338 | * @brief Prepare asynchronous read |
pmallick | 0:e8a1ba50c46b | 339 | * |
pmallick | 0:e8a1ba50c46b | 340 | * Get the inside raw buffer to be used in DMA transactions. |
pmallick | 0:e8a1ba50c46b | 341 | * |
pmallick | 0:e8a1ba50c46b | 342 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 343 | * @param size_to_read - Number of bytes needed to write to the buffer. |
pmallick | 0:e8a1ba50c46b | 344 | * @param read_buff - Address where to store the buffer where data will be read. |
pmallick | 0:e8a1ba50c46b | 345 | * @param size_avilable - min(size_to_read, size until end of allocated buffer) |
pmallick | 0:e8a1ba50c46b | 346 | * @return |
pmallick | 0:e8a1ba50c46b | 347 | * - \ref SUCCESS - No errors |
pmallick | 0:e8a1ba50c46b | 348 | * - -EAGAIN - No data available at this moment |
pmallick | 0:e8a1ba50c46b | 349 | * - -EINVAL - Wrong parameters used |
pmallick | 0:e8a1ba50c46b | 350 | * - -EBUSY - Asynchronous transaction already started |
pmallick | 0:e8a1ba50c46b | 351 | * - -EOVERRUN - An overrun occurred and some data have been overwritten |
pmallick | 0:e8a1ba50c46b | 352 | */ |
pmallick | 0:e8a1ba50c46b | 353 | int32_t cb_prepare_async_read(struct circular_buffer *desc, |
pmallick | 0:e8a1ba50c46b | 354 | uint32_t size_to_read, |
pmallick | 0:e8a1ba50c46b | 355 | void **read_buff, |
pmallick | 0:e8a1ba50c46b | 356 | uint32_t *size_avilable) |
pmallick | 0:e8a1ba50c46b | 357 | { |
pmallick | 0:e8a1ba50c46b | 358 | return cb_prepare_async_operation(desc, size_to_read, read_buff, |
pmallick | 0:e8a1ba50c46b | 359 | size_avilable, 1); |
pmallick | 0:e8a1ba50c46b | 360 | } |
pmallick | 0:e8a1ba50c46b | 361 | |
pmallick | 0:e8a1ba50c46b | 362 | /** |
pmallick | 0:e8a1ba50c46b | 363 | * \defgroup end_async_group End Ashyncronous functions |
pmallick | 0:e8a1ba50c46b | 364 | * @brief End asynchronous transaction |
pmallick | 0:e8a1ba50c46b | 365 | * |
pmallick | 0:e8a1ba50c46b | 366 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 367 | * @return |
pmallick | 0:e8a1ba50c46b | 368 | * - \ref SUCCESS - No errors |
pmallick | 0:e8a1ba50c46b | 369 | * - \ref FAILURE - Asynchronous transaction not started |
pmallick | 0:e8a1ba50c46b | 370 | * - -EINVAL - Wrong parameters used |
pmallick | 0:e8a1ba50c46b | 371 | * @{ |
pmallick | 0:e8a1ba50c46b | 372 | */ |
pmallick | 0:e8a1ba50c46b | 373 | int32_t cb_end_async_write(struct circular_buffer *desc) |
pmallick | 0:e8a1ba50c46b | 374 | { |
pmallick | 0:e8a1ba50c46b | 375 | return cb_end_async_operation(desc, 0); |
pmallick | 0:e8a1ba50c46b | 376 | } |
pmallick | 0:e8a1ba50c46b | 377 | |
pmallick | 0:e8a1ba50c46b | 378 | int32_t cb_end_async_read(struct circular_buffer *desc) |
pmallick | 0:e8a1ba50c46b | 379 | { |
pmallick | 0:e8a1ba50c46b | 380 | |
pmallick | 0:e8a1ba50c46b | 381 | return cb_end_async_operation(desc, 1); |
pmallick | 0:e8a1ba50c46b | 382 | } |
pmallick | 0:e8a1ba50c46b | 383 | /** @} */ |
pmallick | 0:e8a1ba50c46b | 384 | |
pmallick | 0:e8a1ba50c46b | 385 | /** |
pmallick | 0:e8a1ba50c46b | 386 | * @brief Write data to the buffer (Blocking) |
pmallick | 0:e8a1ba50c46b | 387 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 388 | * @param data - Buffer from where data is copied to the circular buffer |
pmallick | 0:e8a1ba50c46b | 389 | * @param size - Size to write |
pmallick | 0:e8a1ba50c46b | 390 | * @return |
pmallick | 0:e8a1ba50c46b | 391 | * - \ref SUCCESS - No errors |
pmallick | 0:e8a1ba50c46b | 392 | * - -EINVAL - Wrong parameters used |
pmallick | 0:e8a1ba50c46b | 393 | */ |
pmallick | 0:e8a1ba50c46b | 394 | int32_t cb_write(struct circular_buffer *desc, const void *data, uint32_t size) |
pmallick | 0:e8a1ba50c46b | 395 | { |
pmallick | 0:e8a1ba50c46b | 396 | return cb_operation(desc, (void *)data, size, 0); |
pmallick | 0:e8a1ba50c46b | 397 | } |
pmallick | 0:e8a1ba50c46b | 398 | |
pmallick | 0:e8a1ba50c46b | 399 | /** |
pmallick | 0:e8a1ba50c46b | 400 | * @brief Read data from the buffer (Blocking) |
pmallick | 0:e8a1ba50c46b | 401 | * @param desc - Circular buffer reference |
pmallick | 0:e8a1ba50c46b | 402 | * @param data - Buffer where to data is copied from the circular buffer |
pmallick | 0:e8a1ba50c46b | 403 | * @param size - Size to read |
pmallick | 0:e8a1ba50c46b | 404 | * @return |
pmallick | 0:e8a1ba50c46b | 405 | * - \ref SUCCESS - No errors |
pmallick | 0:e8a1ba50c46b | 406 | * - -EINVAL - Wrong parameters used |
pmallick | 0:e8a1ba50c46b | 407 | * - -EOVERRUN - An overrun occurred and some data have been overwritten |
pmallick | 0:e8a1ba50c46b | 408 | */ |
pmallick | 0:e8a1ba50c46b | 409 | int32_t cb_read(struct circular_buffer *desc, void *data, uint32_t size) |
pmallick | 0:e8a1ba50c46b | 410 | { |
pmallick | 0:e8a1ba50c46b | 411 | return cb_operation(desc, data, size, 1); |
pmallick | 0:e8a1ba50c46b | 412 | } |