Pratyush Mallick
/
nano_dac
this is testing
noos_mbed/util/fifo.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 fifo.c |
pmallick | 0:e8a1ba50c46b | 3 | * @brief Implementation of fifo. |
pmallick | 0:e8a1ba50c46b | 4 | * @author Cristian Pop (cristian.pop@analog.com) |
pmallick | 0:e8a1ba50c46b | 5 | ******************************************************************************** |
pmallick | 0:e8a1ba50c46b | 6 | * Copyright 2019(c) Analog Devices, Inc. |
pmallick | 0:e8a1ba50c46b | 7 | * |
pmallick | 0:e8a1ba50c46b | 8 | * All rights reserved. |
pmallick | 0:e8a1ba50c46b | 9 | * |
pmallick | 0:e8a1ba50c46b | 10 | * Redistribution and use in source and binary forms, with or without |
pmallick | 0:e8a1ba50c46b | 11 | * modification, are permitted provided that the following conditions are met: |
pmallick | 0:e8a1ba50c46b | 12 | * - Redistributions of source code must retain the above copyright |
pmallick | 0:e8a1ba50c46b | 13 | * notice, this list of conditions and the following disclaimer. |
pmallick | 0:e8a1ba50c46b | 14 | * - Redistributions in binary form must reproduce the above copyright |
pmallick | 0:e8a1ba50c46b | 15 | * notice, this list of conditions and the following disclaimer in |
pmallick | 0:e8a1ba50c46b | 16 | * the documentation and/or other materials provided with the |
pmallick | 0:e8a1ba50c46b | 17 | * distribution. |
pmallick | 0:e8a1ba50c46b | 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
pmallick | 0:e8a1ba50c46b | 19 | * contributors may be used to endorse or promote products derived |
pmallick | 0:e8a1ba50c46b | 20 | * from this software without specific prior written permission. |
pmallick | 0:e8a1ba50c46b | 21 | * - The use of this software may or may not infringe the patent rights |
pmallick | 0:e8a1ba50c46b | 22 | * of one or more patent holders. This license does not release you |
pmallick | 0:e8a1ba50c46b | 23 | * from the requirement that you obtain separate licenses from these |
pmallick | 0:e8a1ba50c46b | 24 | * patent holders to use this software. |
pmallick | 0:e8a1ba50c46b | 25 | * - Use of the software either in source or binary form, must be run |
pmallick | 0:e8a1ba50c46b | 26 | * on or directly connected to an Analog Devices Inc. component. |
pmallick | 0:e8a1ba50c46b | 27 | * |
pmallick | 0:e8a1ba50c46b | 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
pmallick | 0:e8a1ba50c46b | 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
pmallick | 0:e8a1ba50c46b | 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
pmallick | 0:e8a1ba50c46b | 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
pmallick | 0:e8a1ba50c46b | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
pmallick | 0:e8a1ba50c46b | 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
pmallick | 0:e8a1ba50c46b | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
pmallick | 0:e8a1ba50c46b | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
pmallick | 0:e8a1ba50c46b | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
pmallick | 0:e8a1ba50c46b | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
pmallick | 0:e8a1ba50c46b | 38 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 39 | |
pmallick | 0:e8a1ba50c46b | 40 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 41 | /***************************** Include Files **********************************/ |
pmallick | 0:e8a1ba50c46b | 42 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 43 | |
pmallick | 0:e8a1ba50c46b | 44 | #include <string.h> |
pmallick | 0:e8a1ba50c46b | 45 | #include <stdlib.h> |
pmallick | 0:e8a1ba50c46b | 46 | #include "fifo.h" |
pmallick | 0:e8a1ba50c46b | 47 | #include "error.h" |
pmallick | 0:e8a1ba50c46b | 48 | |
pmallick | 0:e8a1ba50c46b | 49 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 50 | /************************ Functions Definitions *******************************/ |
pmallick | 0:e8a1ba50c46b | 51 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 52 | |
pmallick | 0:e8a1ba50c46b | 53 | /** |
pmallick | 0:e8a1ba50c46b | 54 | * @brief Create new fifo element |
pmallick | 0:e8a1ba50c46b | 55 | * @param buff - Data to be saved in fifo. |
pmallick | 0:e8a1ba50c46b | 56 | * @param len - Length of the data. |
pmallick | 0:e8a1ba50c46b | 57 | * @return fifo element in case of success, NULL otherwise |
pmallick | 0:e8a1ba50c46b | 58 | */ |
pmallick | 0:e8a1ba50c46b | 59 | static struct fifo_element * fifo_new_element(char *buff, uint32_t len) |
pmallick | 0:e8a1ba50c46b | 60 | { |
pmallick | 0:e8a1ba50c46b | 61 | struct fifo_element *q = calloc(1, sizeof(struct fifo_element)); |
pmallick | 0:e8a1ba50c46b | 62 | if (!q) |
pmallick | 0:e8a1ba50c46b | 63 | return NULL; |
pmallick | 0:e8a1ba50c46b | 64 | |
pmallick | 0:e8a1ba50c46b | 65 | q->len = len; |
pmallick | 0:e8a1ba50c46b | 66 | q->data = calloc(1, len); |
pmallick | 0:e8a1ba50c46b | 67 | if (!(q->data)) { |
pmallick | 0:e8a1ba50c46b | 68 | free(q); |
pmallick | 0:e8a1ba50c46b | 69 | return NULL; |
pmallick | 0:e8a1ba50c46b | 70 | } |
pmallick | 0:e8a1ba50c46b | 71 | memcpy(q->data, buff, len); |
pmallick | 0:e8a1ba50c46b | 72 | |
pmallick | 0:e8a1ba50c46b | 73 | return q; |
pmallick | 0:e8a1ba50c46b | 74 | } |
pmallick | 0:e8a1ba50c46b | 75 | |
pmallick | 0:e8a1ba50c46b | 76 | /** |
pmallick | 0:e8a1ba50c46b | 77 | * @brief Get last element in fifo |
pmallick | 0:e8a1ba50c46b | 78 | * @param p_fifo - pointer to fifo |
pmallick | 0:e8a1ba50c46b | 79 | * @return fifo last element if exists, NULL otherwise |
pmallick | 0:e8a1ba50c46b | 80 | */ |
pmallick | 0:e8a1ba50c46b | 81 | static struct fifo_element *fifo_get_last(struct fifo_element *p_fifo) |
pmallick | 0:e8a1ba50c46b | 82 | { |
pmallick | 0:e8a1ba50c46b | 83 | if(p_fifo == NULL) |
pmallick | 0:e8a1ba50c46b | 84 | return NULL; |
pmallick | 0:e8a1ba50c46b | 85 | while (p_fifo->next) { |
pmallick | 0:e8a1ba50c46b | 86 | p_fifo = p_fifo->next; |
pmallick | 0:e8a1ba50c46b | 87 | } |
pmallick | 0:e8a1ba50c46b | 88 | |
pmallick | 0:e8a1ba50c46b | 89 | return p_fifo; |
pmallick | 0:e8a1ba50c46b | 90 | } |
pmallick | 0:e8a1ba50c46b | 91 | |
pmallick | 0:e8a1ba50c46b | 92 | /** |
pmallick | 0:e8a1ba50c46b | 93 | * @brief Insert element to fifo, in the last position. |
pmallick | 0:e8a1ba50c46b | 94 | * @param p_fifo - Pointer to fifo. |
pmallick | 0:e8a1ba50c46b | 95 | * @param buff - Data to be saved in fifo. |
pmallick | 0:e8a1ba50c46b | 96 | * @param len - Length of the data. |
pmallick | 0:e8a1ba50c46b | 97 | * @return SUCCESS in case of success, FAILURE otherwise |
pmallick | 0:e8a1ba50c46b | 98 | */ |
pmallick | 0:e8a1ba50c46b | 99 | int32_t fifo_insert(struct fifo_element **p_fifo, char *buff, uint32_t len) |
pmallick | 0:e8a1ba50c46b | 100 | { |
pmallick | 0:e8a1ba50c46b | 101 | struct fifo_element *p, *q; |
pmallick | 0:e8a1ba50c46b | 102 | |
pmallick | 0:e8a1ba50c46b | 103 | if (len <= 0) |
pmallick | 0:e8a1ba50c46b | 104 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 105 | |
pmallick | 0:e8a1ba50c46b | 106 | q = fifo_new_element(buff, len); |
pmallick | 0:e8a1ba50c46b | 107 | if (!q) |
pmallick | 0:e8a1ba50c46b | 108 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 109 | |
pmallick | 0:e8a1ba50c46b | 110 | if (!(*p_fifo)) { |
pmallick | 0:e8a1ba50c46b | 111 | *p_fifo = q; |
pmallick | 0:e8a1ba50c46b | 112 | } else { |
pmallick | 0:e8a1ba50c46b | 113 | p = fifo_get_last(*p_fifo); |
pmallick | 0:e8a1ba50c46b | 114 | p->next = q; |
pmallick | 0:e8a1ba50c46b | 115 | } |
pmallick | 0:e8a1ba50c46b | 116 | |
pmallick | 0:e8a1ba50c46b | 117 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 118 | } |
pmallick | 0:e8a1ba50c46b | 119 | |
pmallick | 0:e8a1ba50c46b | 120 | /** |
pmallick | 0:e8a1ba50c46b | 121 | * @brief Remove fifo head |
pmallick | 0:e8a1ba50c46b | 122 | * @param p_fifo - Pointer to fifo. |
pmallick | 0:e8a1ba50c46b | 123 | * @return next element in fifo if exists, NULL otherwise. |
pmallick | 0:e8a1ba50c46b | 124 | */ |
pmallick | 0:e8a1ba50c46b | 125 | struct fifo_element * fifo_remove(struct fifo_element *p_fifo) |
pmallick | 0:e8a1ba50c46b | 126 | { |
pmallick | 0:e8a1ba50c46b | 127 | struct fifo_element *p = p_fifo; |
pmallick | 0:e8a1ba50c46b | 128 | |
pmallick | 0:e8a1ba50c46b | 129 | if (p_fifo != NULL) { |
pmallick | 0:e8a1ba50c46b | 130 | p_fifo = p_fifo->next; |
pmallick | 0:e8a1ba50c46b | 131 | free(p->data); |
pmallick | 0:e8a1ba50c46b | 132 | free(p); |
pmallick | 0:e8a1ba50c46b | 133 | } |
pmallick | 0:e8a1ba50c46b | 134 | |
pmallick | 0:e8a1ba50c46b | 135 | return p_fifo; |
pmallick | 0:e8a1ba50c46b | 136 | } |