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