1 /* 2 * Copyright (c) 2015-2019, Texas Instruments Incorporated 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of Texas Instruments Incorporated nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef ti_drivers_uart_RingBuf__include 34 #define ti_drivers_uart_RingBuf__include 35 36 #include <stdint.h> 37 #include <stddef.h> 38 #include <stdbool.h> 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 typedef struct { 45 unsigned char *buffer; 46 size_t length; 47 size_t count; 48 size_t head; 49 size_t tail; 50 size_t maxCount; 51 } RingBuf_Object, *RingBuf_Handle; 52 53 /*! 54 * @brief Initialize circular buffer 55 * 56 * @param object Pointer to a RingBuf Object that contains the member 57 * variables to operate a circular buffer. 58 * 59 * @param bufPtr Pointer to data buffer to be used for the circular buffer. 60 * The buffer is NOT stored in RingBuf_Object. 61 * 62 * @param bufSize The size of bufPtr in number of unsigned chars. 63 */ 64 void RingBuf_construct(RingBuf_Handle object, unsigned char *bufPtr, 65 size_t bufSize); 66 67 /*! 68 * @brief Get an unsigned char from the end of the circular buffer and remove 69 * it. 70 * 71 * @param object Pointer to a RingBuf Object that contains the member 72 * variables to operate a circular buffer. 73 * 74 * @param data Pointer to an unsigned char to be filled with the data from 75 * the front of the circular buffer. 76 * 77 * @return Number of unsigned chars on the buffer after taking it out 78 * of the circular buffer. If it returns -1, the circular 79 * buffer was already empty and data is invalid. 80 */ 81 int RingBuf_get(RingBuf_Handle object, unsigned char *data); 82 83 /*! 84 * @brief Get the number of unsigned chars currently stored on the circular 85 * buffer. 86 * 87 * @param object Pointer to a RingBuf Object that contains the member 88 * variables to operate a circular buffer. 89 * 90 * @return Number of unsigned chars on the circular buffer. 91 */ 92 int RingBuf_getCount(RingBuf_Handle object); 93 94 /*! 95 * @brief Function to determine if the circular buffer is full or not. 96 * 97 * @param object Pointer to a RingBuf Object that contains the member 98 * variables to operate a circular buffer. 99 * 100 * @return true if circular buffer is full, else false. 101 */ 102 bool RingBuf_isFull(RingBuf_Handle object); 103 104 /*! 105 * @brief A high-water mark indicating the largest number of unsigned chars 106 * stored on the circular buffer since it was constructed. 107 * 108 * @return Get the largest number of unsigned chars that were at one 109 * point in the circular buffer. 110 */ 111 int RingBuf_getMaxCount(RingBuf_Handle object); 112 113 /*! 114 * @brief Get an unsigned char from the end of the circular buffer without 115 * removing it. 116 * 117 * @param object Pointer to a RingBuf Object that contains the member 118 * variables to operate a circular buffer. 119 * 120 * @param data Pointer to an unsigned char to be filled with the data from 121 * the front of the circular buffer. This function does not 122 * remove the data from the circular buffer. Do not evaluate 123 * data if the count returned is equal to 0. 124 * 125 * @return Number of unsigned chars on the circular buffer. If the 126 * number != 0, then data will contain the unsigned char at the 127 * end of the circular buffer. 128 */ 129 int RingBuf_peek(RingBuf_Handle object, unsigned char *data); 130 131 /*! 132 * @brief Put an unsigned char into the end of the circular buffer. 133 * 134 * @param object Pointer to a RingBuf Object that contains the member 135 * variables to operate a circular buffer. 136 * 137 * @param data unsigned char to be placed at the end of the circular 138 * buffer. 139 * 140 * @return Number of unsigned chars on the buffer after it was added, 141 * or -1 if it's already full. 142 */ 143 int RingBuf_put(RingBuf_Handle object, unsigned char data); 144 145 #ifdef __cplusplus 146 } 147 #endif 148 149 #endif /* ti_drivers_uart_RingBuf__include */ 150