1 /** 2 ***************************************************************************************** 3 * 4 * @file ring_buffer.h 5 * 6 * @brief Header file - ring buffer APIs 7 * 8 ***************************************************************************************** 9 * @attention 10 #####Copyright (c) 2019 GOODIX 11 All rights reserved. 12 13 Redistribution and use in source and binary forms, with or without 14 modification, are permitted provided that the following conditions are met: 15 * Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 * Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in the 19 documentation and/or other materials provided with the distribution. 20 * Neither the name of GOODIX nor the names of its contributors may be used 21 to endorse or promote products derived from this software without 22 specific prior written permission. 23 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35 ***************************************************************************************** 36 */ 37 #ifndef __RING_BUFFER_H__ 38 #define __RING_BUFFER_H__ 39 40 /* 41 * INCLUDE FILES 42 ***************************************************************************************** 43 */ 44 #include <stdint.h> 45 #include <stdbool.h> 46 47 /** 48 * @defgroup RING_BUFFER_STRUCT Structures 49 * @{ 50 */ 51 typedef struct { 52 uint32_t buffer_size; /**< Size of ring buffer. */ 53 uint8_t *p_buffer; /**< Pointer to buffer saved data. */ 54 uint32_t write_index; /**< Index of write. */ 55 uint32_t read_index; /**< Index of read. */ 56 } ring_buffer_t; 57 /** @} */ 58 59 /** 60 * @defgroup RING_BUFFER_FUNCTION Functions 61 * @{ 62 */ 63 /* 64 * GLOBAL FUNCTION DECLARATION 65 ***************************************************************************************** 66 */ 67 /** 68 ***************************************************************************************** 69 * @brief Initialize one ring buffer. 70 * 71 * @param[in] p_ring_buff: Pointer to ring buffer structure. 72 * @param[in] p_buff: Pointer to where save data. 73 * @param[in] buff_size: Size of buffer save data. 74 * 75 * @return Result of initializing ring buffer. 76 ***************************************************************************************** 77 */ 78 bool ring_buffer_init(ring_buffer_t *p_ring_buff, uint8_t *p_buff, uint32_t buff_size); 79 80 /** 81 ***************************************************************************************** 82 * @brief Write data to one ring buffer. 83 * 84 * @param[in] p_ring_buff: Pointer to ring buffer. 85 * @param[in] p_wr_data: Pointer to data need be wrote. 86 * @param[in] length: Length of data need be wrote. 87 * 88 * @return Length of writen. 89 ***************************************************************************************** 90 */ 91 uint32_t ring_buffer_write(ring_buffer_t *p_ring_buff, uint8_t const *p_wr_data, uint32_t length); 92 93 /** 94 ***************************************************************************************** 95 * @brief Read data from one ring buffer. 96 * 97 * @param[in] p_ring_buff: Pointer to ring buffer. 98 * @param[in] p_rd_data: Pointer to where save read data. 99 * @param[in] length: Length of data want to read. 100 * 101 * @return Length of availdble read data. 102 ***************************************************************************************** 103 */ 104 uint32_t ring_buffer_read(ring_buffer_t *p_ring_buff, uint8_t *p_rd_data, uint32_t length); 105 106 /** 107 ***************************************************************************************** 108 * @brief Pick data from one ring buffer. 109 * 110 * @param[in] p_ring_buff: Pointer to ring buffer. 111 * @param[in] p_rd_data: Pointer to where save pick data. 112 * @param[in] length: Length of data want to read. 113 * 114 * @return Length of availdble pick data. 115 ***************************************************************************************** 116 */ 117 uint32_t ring_buffer_pick(ring_buffer_t *p_ring_buff, uint8_t *p_rd_data, uint32_t length); 118 /** 119 ***************************************************************************************** 120 * @brief Get surplus space of one ring buffer. 121 * 122 * @param[in] p_ring_buff: Pointer to ring buffer. 123 * 124 * @retval Size of surplus space. 125 ***************************************************************************************** 126 */ 127 uint32_t ring_buffer_surplus_space_get(ring_buffer_t *p_ring_buff); 128 129 /** 130 ***************************************************************************************** 131 * @brief Get availdble data from one ring buffer. 132 * 133 * @param[in] p_ring_buff: Pointer to ring buffer. 134 * 135 * @retval Length of availdble data. 136 ***************************************************************************************** 137 */ 138 uint32_t ring_buffer_items_count_get(ring_buffer_t *p_ring_buff); 139 140 /** 141 ***************************************************************************************** 142 * @brief Clean one ring buffer. 143 * 144 * @param[in] p_ring_buff: Pointer to ring buffer structure. 145 ***************************************************************************************** 146 */ 147 void ring_buffer_clean(ring_buffer_t *p_ring_buff); 148 149 /** 150 ***************************************************************************************** 151 * @brief Check if ring buffer is almost full. 152 * 153 * @param[in] p_ring_buff: Pointer to ring buffer. 154 * @param[in] left_threshold: Left room threshold. 155 * 156 * @return State of buffer almost full. 157 ***************************************************************************************** 158 */ 159 bool ring_buffer_is_reach_left_threshold(ring_buffer_t *p_ring_buff, uint32_t letf_threshold); 160 /** @} */ 161 162 #endif 163