1 /** 2 * @file mecirc_buf.h 3 * 4 * @brief Meilhaus circular buffer implementation. 5 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) 6 * @author Guenter Gebhardt 7 * @author Krzysztof Gantzke (k.gantzke@meilhaus.de) 8 */ 9 10 /* 11 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) 12 * 13 * This file is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26 */ 27 28 #ifndef _MECIRC_BUF_H_ 29 #define _MECIRC_BUF_H_ 30 31 # ifdef __KERNEL__ 32 33 # ifdef BOSCH 34 35 typedef struct me_circ_buf { 36 unsigned int mask; 37 // unsigned int count; 38 uint32_t *buf; 39 int volatile head; 40 int volatile tail; 41 } me_circ_buf_t; 42 me_circ_buf_values(me_circ_buf_t * buf)43static int inline me_circ_buf_values(me_circ_buf_t * buf) 44 { 45 // return ((buf->head - buf->tail) & (buf->count - 1)); 46 return ((buf->head - buf->tail) & (buf->mask)); 47 } 48 me_circ_buf_space(me_circ_buf_t * buf)49static int inline me_circ_buf_space(me_circ_buf_t * buf) 50 { 51 // return ((buf->tail - (buf->head + 1)) & (buf->count - 1)); 52 return ((buf->tail - (buf->head + 1)) & (buf->mask)); 53 } 54 me_circ_buf_values_to_end(me_circ_buf_t * buf)55static int inline me_circ_buf_values_to_end(me_circ_buf_t * buf) 56 { 57 int end; 58 int n; 59 // end = buf->count - buf->tail; 60 // n = (buf->head + end) & (buf->count - 1); 61 end = buf->mask + 1 - buf->tail; 62 n = (buf->head + end) & (buf->mask); 63 return (n < end) ? n : end; 64 } 65 me_circ_buf_space_to_end(me_circ_buf_t * buf)66static int inline me_circ_buf_space_to_end(me_circ_buf_t * buf) 67 { 68 int end; 69 int n; 70 71 // end = buf->count - 1 - buf->head; 72 // n = (end + buf->tail) & (buf->count - 1); 73 end = buf->mask - buf->head; 74 n = (end + buf->tail) & (buf->mask); 75 return (n <= end) ? n : (end + 1); 76 } 77 78 #define _CBUFF_32b_t 79 80 # else //~BOSCH 81 /// @note buf->mask = buf->count-1 = ME4600_AI_CIRC_BUF_COUNT-1 82 83 # ifdef _CBUFF_32b_t 84 //32 bit 85 typedef struct me_circ_buf_32b { 86 int volatile head; 87 int volatile tail; 88 unsigned int mask; //buffor size-1 must be 2^n-1 to work 89 uint32_t *buf; 90 } me_circ_buf_t; 91 # else 92 //16 bit 93 typedef struct me_circ_buf_16b { 94 int volatile head; 95 int volatile tail; 96 unsigned int mask; //buffor size-1 must be 2^n-1 to work 97 uint16_t *buf; 98 } me_circ_buf_t; 99 # endif //_CBUFF_32b_t 100 101 /** How many values is in buffer */ me_circ_buf_values(me_circ_buf_t * buf)102static int inline me_circ_buf_values(me_circ_buf_t * buf) 103 { 104 return ((buf->head - buf->tail) & (buf->mask)); 105 } 106 107 /** How many space left */ me_circ_buf_space(me_circ_buf_t * buf)108static int inline me_circ_buf_space(me_circ_buf_t * buf) 109 { 110 return ((buf->tail - (buf->head + 1)) & (buf->mask)); 111 } 112 113 /** How many values can be read from buffor in one chunck. */ me_circ_buf_values_to_end(me_circ_buf_t * buf)114static int inline me_circ_buf_values_to_end(me_circ_buf_t * buf) 115 { 116 return (buf->tail <= 117 buf->head) ? (buf->head - buf->tail) : (buf->mask - buf->tail + 118 1); 119 } 120 121 /** How many values can be write to buffer in one chunck. */ me_circ_buf_space_to_end(me_circ_buf_t * buf)122static int inline me_circ_buf_space_to_end(me_circ_buf_t * buf) 123 { 124 return (buf->tail <= 125 buf->head) ? (buf->mask - buf->head + 1) : (buf->tail - 126 buf->head - 1); 127 } 128 129 # endif //BOSCH 130 # endif //__KERNEL__ 131 #endif //_MECIRC_BUF_H_ 132