Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
libscpi/src/fifo.c@0:be95bfb06686, 2022-01-17 (annotated)
- Committer:
- wuliqunyy
- Date:
- Mon Jan 17 13:20:09 2022 +0000
- Revision:
- 0:be95bfb06686
a working non_flat + adc_didt for ehp regulation version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wuliqunyy | 0:be95bfb06686 | 1 | /*- |
wuliqunyy | 0:be95bfb06686 | 2 | * BSD 2-Clause License |
wuliqunyy | 0:be95bfb06686 | 3 | * |
wuliqunyy | 0:be95bfb06686 | 4 | * Copyright (c) 2012-2018, Jan Breuer |
wuliqunyy | 0:be95bfb06686 | 5 | * All rights reserved. |
wuliqunyy | 0:be95bfb06686 | 6 | * |
wuliqunyy | 0:be95bfb06686 | 7 | * Redistribution and use in source and binary forms, with or without |
wuliqunyy | 0:be95bfb06686 | 8 | * modification, are permitted provided that the following conditions are met: |
wuliqunyy | 0:be95bfb06686 | 9 | * |
wuliqunyy | 0:be95bfb06686 | 10 | * * Redistributions of source code must retain the above copyright notice, this |
wuliqunyy | 0:be95bfb06686 | 11 | * list of conditions and the following disclaimer. |
wuliqunyy | 0:be95bfb06686 | 12 | * |
wuliqunyy | 0:be95bfb06686 | 13 | * * Redistributions in binary form must reproduce the above copyright notice, |
wuliqunyy | 0:be95bfb06686 | 14 | * this list of conditions and the following disclaimer in the documentation |
wuliqunyy | 0:be95bfb06686 | 15 | * and/or other materials provided with the distribution. |
wuliqunyy | 0:be95bfb06686 | 16 | * |
wuliqunyy | 0:be95bfb06686 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
wuliqunyy | 0:be95bfb06686 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
wuliqunyy | 0:be95bfb06686 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
wuliqunyy | 0:be95bfb06686 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
wuliqunyy | 0:be95bfb06686 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
wuliqunyy | 0:be95bfb06686 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
wuliqunyy | 0:be95bfb06686 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
wuliqunyy | 0:be95bfb06686 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
wuliqunyy | 0:be95bfb06686 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
wuliqunyy | 0:be95bfb06686 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
wuliqunyy | 0:be95bfb06686 | 27 | */ |
wuliqunyy | 0:be95bfb06686 | 28 | |
wuliqunyy | 0:be95bfb06686 | 29 | #include "fifo_private.h" |
wuliqunyy | 0:be95bfb06686 | 30 | |
wuliqunyy | 0:be95bfb06686 | 31 | /** |
wuliqunyy | 0:be95bfb06686 | 32 | * Initialize fifo |
wuliqunyy | 0:be95bfb06686 | 33 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 34 | */ |
wuliqunyy | 0:be95bfb06686 | 35 | void fifo_init(scpi_fifo_t * fifo, scpi_error_t * data, int16_t size) { |
wuliqunyy | 0:be95bfb06686 | 36 | fifo->wr = 0; |
wuliqunyy | 0:be95bfb06686 | 37 | fifo->rd = 0; |
wuliqunyy | 0:be95bfb06686 | 38 | fifo->count = 0; |
wuliqunyy | 0:be95bfb06686 | 39 | fifo->data = data; |
wuliqunyy | 0:be95bfb06686 | 40 | fifo->size = size; |
wuliqunyy | 0:be95bfb06686 | 41 | } |
wuliqunyy | 0:be95bfb06686 | 42 | |
wuliqunyy | 0:be95bfb06686 | 43 | /** |
wuliqunyy | 0:be95bfb06686 | 44 | * Empty fifo |
wuliqunyy | 0:be95bfb06686 | 45 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 46 | */ |
wuliqunyy | 0:be95bfb06686 | 47 | void fifo_clear(scpi_fifo_t * fifo) { |
wuliqunyy | 0:be95bfb06686 | 48 | fifo->wr = 0; |
wuliqunyy | 0:be95bfb06686 | 49 | fifo->rd = 0; |
wuliqunyy | 0:be95bfb06686 | 50 | fifo->count = 0; |
wuliqunyy | 0:be95bfb06686 | 51 | } |
wuliqunyy | 0:be95bfb06686 | 52 | |
wuliqunyy | 0:be95bfb06686 | 53 | /** |
wuliqunyy | 0:be95bfb06686 | 54 | * Test if fifo is empty |
wuliqunyy | 0:be95bfb06686 | 55 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 56 | * @return |
wuliqunyy | 0:be95bfb06686 | 57 | */ |
wuliqunyy | 0:be95bfb06686 | 58 | scpi_bool_t fifo_is_empty(scpi_fifo_t * fifo) { |
wuliqunyy | 0:be95bfb06686 | 59 | return fifo->count == 0; |
wuliqunyy | 0:be95bfb06686 | 60 | } |
wuliqunyy | 0:be95bfb06686 | 61 | |
wuliqunyy | 0:be95bfb06686 | 62 | /** |
wuliqunyy | 0:be95bfb06686 | 63 | * Test if fifo is full |
wuliqunyy | 0:be95bfb06686 | 64 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 65 | * @return |
wuliqunyy | 0:be95bfb06686 | 66 | */ |
wuliqunyy | 0:be95bfb06686 | 67 | scpi_bool_t fifo_is_full(scpi_fifo_t * fifo) { |
wuliqunyy | 0:be95bfb06686 | 68 | return fifo->count == fifo->size; |
wuliqunyy | 0:be95bfb06686 | 69 | } |
wuliqunyy | 0:be95bfb06686 | 70 | |
wuliqunyy | 0:be95bfb06686 | 71 | /** |
wuliqunyy | 0:be95bfb06686 | 72 | * Add element to fifo. If fifo is full, return FALSE. |
wuliqunyy | 0:be95bfb06686 | 73 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 74 | * @param err |
wuliqunyy | 0:be95bfb06686 | 75 | * @param info |
wuliqunyy | 0:be95bfb06686 | 76 | * @return |
wuliqunyy | 0:be95bfb06686 | 77 | */ |
wuliqunyy | 0:be95bfb06686 | 78 | scpi_bool_t fifo_add(scpi_fifo_t * fifo, const scpi_error_t * value) { |
wuliqunyy | 0:be95bfb06686 | 79 | /* FIFO full? */ |
wuliqunyy | 0:be95bfb06686 | 80 | if (fifo_is_full(fifo)) { |
wuliqunyy | 0:be95bfb06686 | 81 | return FALSE; |
wuliqunyy | 0:be95bfb06686 | 82 | } |
wuliqunyy | 0:be95bfb06686 | 83 | if (!value) { |
wuliqunyy | 0:be95bfb06686 | 84 | return FALSE; |
wuliqunyy | 0:be95bfb06686 | 85 | } |
wuliqunyy | 0:be95bfb06686 | 86 | |
wuliqunyy | 0:be95bfb06686 | 87 | fifo->data[fifo->wr] = *value; |
wuliqunyy | 0:be95bfb06686 | 88 | fifo->wr = (fifo->wr + 1) % (fifo->size); |
wuliqunyy | 0:be95bfb06686 | 89 | fifo->count += 1; |
wuliqunyy | 0:be95bfb06686 | 90 | return TRUE; |
wuliqunyy | 0:be95bfb06686 | 91 | } |
wuliqunyy | 0:be95bfb06686 | 92 | |
wuliqunyy | 0:be95bfb06686 | 93 | /** |
wuliqunyy | 0:be95bfb06686 | 94 | * Remove element form fifo |
wuliqunyy | 0:be95bfb06686 | 95 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 96 | * @param value |
wuliqunyy | 0:be95bfb06686 | 97 | * @return FALSE - fifo is empty |
wuliqunyy | 0:be95bfb06686 | 98 | */ |
wuliqunyy | 0:be95bfb06686 | 99 | scpi_bool_t fifo_remove(scpi_fifo_t * fifo, scpi_error_t * value) { |
wuliqunyy | 0:be95bfb06686 | 100 | /* FIFO empty? */ |
wuliqunyy | 0:be95bfb06686 | 101 | if (fifo_is_empty(fifo)) { |
wuliqunyy | 0:be95bfb06686 | 102 | return FALSE; |
wuliqunyy | 0:be95bfb06686 | 103 | } |
wuliqunyy | 0:be95bfb06686 | 104 | |
wuliqunyy | 0:be95bfb06686 | 105 | if (value) { |
wuliqunyy | 0:be95bfb06686 | 106 | *value = fifo->data[fifo->rd]; |
wuliqunyy | 0:be95bfb06686 | 107 | } |
wuliqunyy | 0:be95bfb06686 | 108 | |
wuliqunyy | 0:be95bfb06686 | 109 | fifo->rd = (fifo->rd + 1) % (fifo->size); |
wuliqunyy | 0:be95bfb06686 | 110 | fifo->count -= 1; |
wuliqunyy | 0:be95bfb06686 | 111 | |
wuliqunyy | 0:be95bfb06686 | 112 | return TRUE; |
wuliqunyy | 0:be95bfb06686 | 113 | } |
wuliqunyy | 0:be95bfb06686 | 114 | |
wuliqunyy | 0:be95bfb06686 | 115 | /** |
wuliqunyy | 0:be95bfb06686 | 116 | * Remove last element from fifo |
wuliqunyy | 0:be95bfb06686 | 117 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 118 | * @param value |
wuliqunyy | 0:be95bfb06686 | 119 | * @return FALSE - fifo is empty |
wuliqunyy | 0:be95bfb06686 | 120 | */ |
wuliqunyy | 0:be95bfb06686 | 121 | scpi_bool_t fifo_remove_last(scpi_fifo_t * fifo, scpi_error_t * value) { |
wuliqunyy | 0:be95bfb06686 | 122 | /* FIFO empty? */ |
wuliqunyy | 0:be95bfb06686 | 123 | if (fifo_is_empty(fifo)) { |
wuliqunyy | 0:be95bfb06686 | 124 | return FALSE; |
wuliqunyy | 0:be95bfb06686 | 125 | } |
wuliqunyy | 0:be95bfb06686 | 126 | |
wuliqunyy | 0:be95bfb06686 | 127 | fifo->wr = (fifo->wr + fifo->size - 1) % (fifo->size); |
wuliqunyy | 0:be95bfb06686 | 128 | |
wuliqunyy | 0:be95bfb06686 | 129 | if (value) { |
wuliqunyy | 0:be95bfb06686 | 130 | *value = fifo->data[fifo->wr]; |
wuliqunyy | 0:be95bfb06686 | 131 | } |
wuliqunyy | 0:be95bfb06686 | 132 | fifo->count -= 1; |
wuliqunyy | 0:be95bfb06686 | 133 | |
wuliqunyy | 0:be95bfb06686 | 134 | return TRUE; |
wuliqunyy | 0:be95bfb06686 | 135 | } |
wuliqunyy | 0:be95bfb06686 | 136 | |
wuliqunyy | 0:be95bfb06686 | 137 | /** |
wuliqunyy | 0:be95bfb06686 | 138 | * Retrive number of elements in fifo |
wuliqunyy | 0:be95bfb06686 | 139 | * @param fifo |
wuliqunyy | 0:be95bfb06686 | 140 | * @param value |
wuliqunyy | 0:be95bfb06686 | 141 | * @return |
wuliqunyy | 0:be95bfb06686 | 142 | */ |
wuliqunyy | 0:be95bfb06686 | 143 | scpi_bool_t fifo_count(scpi_fifo_t * fifo, int16_t * value) { |
wuliqunyy | 0:be95bfb06686 | 144 | *value = fifo->count; |
wuliqunyy | 0:be95bfb06686 | 145 | return TRUE; |
wuliqunyy | 0:be95bfb06686 | 146 | } |
wuliqunyy | 0:be95bfb06686 | 147 |