| 1 | /****************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright (c) 2009 |
|---|
| 4 | Infineon Technologies AG |
|---|
| 5 | Am Campeon 1-12; 81726 Munich, Germany |
|---|
| 6 | |
|---|
| 7 | For licensing information, see the file 'LICENSE' in the root folder of |
|---|
| 8 | this software module. |
|---|
| 9 | |
|---|
| 10 | ******************************************************************************/ |
|---|
| 11 | |
|---|
| 12 | /****************************************************************************** |
|---|
| 13 | Module : sys_drv_fifo.c |
|---|
| 14 | Description : Fifo implementation |
|---|
| 15 | Remarks : |
|---|
| 16 | Initialize with Fifo_Init with previosly allocated memory. |
|---|
| 17 | The functions Fifo_writeElement and Fifo_readElement return a pointer |
|---|
| 18 | to the next element to be written or read respectively. |
|---|
| 19 | If empty Fifo_readElement returns IFX_NULL. If full Fifo_writeElement |
|---|
| 20 | returns IFX_NULL. |
|---|
| 21 | *****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | /* ============================= */ |
|---|
| 25 | /* Includes */ |
|---|
| 26 | /* ============================= */ |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | #define DSL_INTERN |
|---|
| 30 | |
|---|
| 31 | #include "drv_dsl_cpe_api.h" |
|---|
| 32 | #include "drv_dsl_cpe_fifo.h" |
|---|
| 33 | |
|---|
| 34 | /* ============================= */ |
|---|
| 35 | /* Local Macros Definitions */ |
|---|
| 36 | /* ============================= */ |
|---|
| 37 | |
|---|
| 38 | /* increment FIFO index */ |
|---|
| 39 | #define DSL_FIFO_INCREMENT_INDEX(p) \ |
|---|
| 40 | { \ |
|---|
| 41 | if (p == pFifo->pEnd) \ |
|---|
| 42 | p = pFifo->pStart; \ |
|---|
| 43 | else \ |
|---|
| 44 | p = (DSL_void_t*)(((DSL_uint_t)p) + pFifo->size); \ |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /* decrement FIFO index */ |
|---|
| 48 | #define DSL_FIFO_DECREMENT_INDEX(p) \ |
|---|
| 49 | { \ |
|---|
| 50 | if (p == pFifo->pStart) \ |
|---|
| 51 | p = pFifo->pEnd; \ |
|---|
| 52 | else \ |
|---|
| 53 | p = (DSL_void_t*)(((DSL_uint_t)p) - pFifo->size); \ |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* ============================= */ |
|---|
| 57 | /* Global variable definition */ |
|---|
| 58 | /* ============================= */ |
|---|
| 59 | |
|---|
| 60 | /* ============================= */ |
|---|
| 61 | /* Local function declaration */ |
|---|
| 62 | /* ============================= */ |
|---|
| 63 | |
|---|
| 64 | /* ============================= */ |
|---|
| 65 | /* Local variable definition */ |
|---|
| 66 | /* ============================= */ |
|---|
| 67 | |
|---|
| 68 | /* ============================= */ |
|---|
| 69 | /* Local function definition */ |
|---|
| 70 | /* ============================= */ |
|---|
| 71 | |
|---|
| 72 | /* ============================= */ |
|---|
| 73 | /* Global function definition */ |
|---|
| 74 | /* ============================= */ |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | /* |
|---|
| 78 | Initializes the fifo structure |
|---|
| 79 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 80 | \param *pStart - Pointer to the fifo start memory |
|---|
| 81 | \param *pEnd - Pointer to the fifo end memory |
|---|
| 82 | \param elSize - size of each element in bytes |
|---|
| 83 | \return |
|---|
| 84 | Always zero, otherwise error |
|---|
| 85 | */ |
|---|
| 86 | DSL_int8_t DSL_Fifo_Init (DSL_FIFO* pFifo, DSL_void_t* pStart, DSL_void_t* pEnd, DSL_uint32_t elSize) |
|---|
| 87 | { |
|---|
| 88 | pFifo->pEnd = pEnd; |
|---|
| 89 | pFifo->pStart = pStart; |
|---|
| 90 | pFifo->pRead = pStart; |
|---|
| 91 | pFifo->pWrite = pStart; |
|---|
| 92 | pFifo->size = elSize; |
|---|
| 93 | pFifo->count = 0; |
|---|
| 94 | |
|---|
| 95 | if (((DSL_uint32_t)pFifo->pEnd - (DSL_uint32_t)pFifo->pStart) % elSize != 0) |
|---|
| 96 | { |
|---|
| 97 | /* element size must be a multiple of fifo memory */ |
|---|
| 98 | return -1; |
|---|
| 99 | } |
|---|
| 100 | pFifo->nMaxSize = ((DSL_uint32_t)pFifo->pEnd - (DSL_uint32_t)pFifo->pStart) / elSize + 1; |
|---|
| 101 | |
|---|
| 102 | return 0; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /* |
|---|
| 106 | Clear the fifo |
|---|
| 107 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 108 | */ |
|---|
| 109 | DSL_void_t DSL_Fifo_Clear (DSL_FIFO *pFifo) |
|---|
| 110 | { |
|---|
| 111 | pFifo->pRead = pFifo->pStart; |
|---|
| 112 | pFifo->pWrite = pFifo->pStart; |
|---|
| 113 | pFifo->count = 0; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /* |
|---|
| 117 | Get the next element to read from |
|---|
| 118 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 119 | \return |
|---|
| 120 | Returns the element to read from, or IFX_NULL if no element available or an error occured |
|---|
| 121 | \remarks |
|---|
| 122 | Error occurs if fifo is empty |
|---|
| 123 | */ |
|---|
| 124 | DSL_void_t* DSL_Fifo_readElement (DSL_FIFO *pFifo) |
|---|
| 125 | { |
|---|
| 126 | DSL_void_t *ret = DSL_NULL; |
|---|
| 127 | |
|---|
| 128 | #ifdef CGC_CRC_DEBUG |
|---|
| 129 | /* temporary code, should be removed in final code */ |
|---|
| 130 | { |
|---|
| 131 | int cnt; |
|---|
| 132 | int bo; |
|---|
| 133 | |
|---|
| 134 | if ((IFX_uint32_t)pFifo->pWrite >= (IFX_uint32_t)pFifo->pRead) |
|---|
| 135 | { |
|---|
| 136 | cnt = (((IFX_uint32_t)pFifo->pWrite - (IFX_uint32_t)pFifo->pRead) / pFifo->size); |
|---|
| 137 | } |
|---|
| 138 | else |
|---|
| 139 | { |
|---|
| 140 | cnt = (((((IFX_uint32_t)pFifo->pEnd - (IFX_uint32_t)pFifo->pRead) + ((IFX_uint32_t)pFifo->pWrite - (IFX_uint32_t)pFifo->pStart)) / pFifo->size) + 1); |
|---|
| 141 | bo=1; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | if (cnt != pFifo->count) |
|---|
| 145 | { |
|---|
| 146 | printf ("%s: read fifo inconsistent"DSL_DRV_CRLF, taskName(taskIdSelf())); |
|---|
| 147 | printf (" pStart: 0x%08x"DSL_DRV_CRLF, (IFX_uint32_t)pFifo->pStart); |
|---|
| 148 | printf (" pEnd: 0x%08x"DSL_DRV_CRLF, (IFX_uint32_t)pFifo->pEnd); |
|---|
| 149 | printf (" pRead: 0x%08x"DSL_DRV_CRLF, (IFX_uint32_t)pFifo->pRead); |
|---|
| 150 | printf (" pWrite: 0x%08x"DSL_DRV_CRLF, (IFX_uint32_t)pFifo->pWrite); |
|---|
| 151 | printf (" Count: %d"DSL_DRV_CRLF, pFifo->count); |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | /* temporary code, should be removed in final code */ |
|---|
| 155 | #endif /* CGC_CRC_DEBUG */ |
|---|
| 156 | |
|---|
| 157 | if (pFifo->count == 0) |
|---|
| 158 | { |
|---|
| 159 | return DSL_NULL; |
|---|
| 160 | } |
|---|
| 161 | ret = pFifo->pRead; |
|---|
| 162 | |
|---|
| 163 | DSL_FIFO_INCREMENT_INDEX(pFifo->pRead); |
|---|
| 164 | pFifo->count--; |
|---|
| 165 | |
|---|
| 166 | return ret; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /* |
|---|
| 170 | Delivers empty status |
|---|
| 171 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 172 | \return |
|---|
| 173 | Returns TRUE if empty (no data available) |
|---|
| 174 | \remarks |
|---|
| 175 | No change on fifo! |
|---|
| 176 | */ |
|---|
| 177 | DSL_int8_t DSL_Fifo_isEmpty (DSL_FIFO *pFifo) |
|---|
| 178 | { |
|---|
| 179 | return (pFifo->count == 0); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /* |
|---|
| 183 | Get the next element to write to |
|---|
| 184 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 185 | \return |
|---|
| 186 | Returns the element to write to, or IFX_NULL in case of error |
|---|
| 187 | \remarks |
|---|
| 188 | Error occurs if the write pointer reaches the read pointer, meaning |
|---|
| 189 | the fifo if full and would otherwise overwrite the next element. |
|---|
| 190 | */ |
|---|
| 191 | DSL_void_t* DSL_Fifo_writeElement (DSL_FIFO *pFifo) |
|---|
| 192 | { |
|---|
| 193 | DSL_void_t* ret = DSL_NULL; |
|---|
| 194 | |
|---|
| 195 | if (DSL_Fifo_isFull(pFifo)) |
|---|
| 196 | return DSL_NULL; |
|---|
| 197 | else |
|---|
| 198 | { |
|---|
| 199 | /* get the next entry of the Fifo */ |
|---|
| 200 | ret = pFifo->pWrite; |
|---|
| 201 | DSL_FIFO_INCREMENT_INDEX(pFifo->pWrite); |
|---|
| 202 | pFifo->count++; |
|---|
| 203 | |
|---|
| 204 | return ret; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /* |
|---|
| 209 | Delivers full status |
|---|
| 210 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 211 | \return |
|---|
| 212 | TRUE if full (overflow on next write) |
|---|
| 213 | \remarks |
|---|
| 214 | No change on fifo! |
|---|
| 215 | */ |
|---|
| 216 | DSL_int8_t DSL_Fifo_isFull (DSL_FIFO *pFifo) |
|---|
| 217 | { |
|---|
| 218 | return (pFifo->count >= pFifo->nMaxSize); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /* |
|---|
| 222 | Returns the last element taken back to the fifo |
|---|
| 223 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 224 | \remarks |
|---|
| 225 | Makes an undo to a previosly Fifo_writeElement |
|---|
| 226 | */ |
|---|
| 227 | DSL_void_t DSL_Fifo_returnElement (DSL_FIFO *pFifo) |
|---|
| 228 | { |
|---|
| 229 | DSL_FIFO_DECREMENT_INDEX(pFifo->pWrite); |
|---|
| 230 | pFifo->count--; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /* |
|---|
| 234 | Get the number of stored elements |
|---|
| 235 | \param *pFifo - Pointer to the Fifo structure |
|---|
| 236 | \return |
|---|
| 237 | Number of containing elements |
|---|
| 238 | */ |
|---|
| 239 | DSL_uint32_t DSL_Fifo_getCount(DSL_FIFO *pFifo) |
|---|
| 240 | { |
|---|
| 241 | return pFifo->count; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | #ifdef INCLUDE_FIFO_TEST |
|---|
| 245 | /* |
|---|
| 246 | test routine |
|---|
| 247 | */ |
|---|
| 248 | DSL_void_t DSL_FifoTest(DSL_void_t) |
|---|
| 249 | { |
|---|
| 250 | DSL_uint16_t buf [3]; |
|---|
| 251 | DSL_uint16_t tst = 1; |
|---|
| 252 | DSL_uint16_t *entry = DSL_NULL; |
|---|
| 253 | DSL_FIFO fifo; |
|---|
| 254 | |
|---|
| 255 | DSL_Fifo_Init (&fifo, &buf[0], &buf[2], sizeof (DSL_uint16_t)); |
|---|
| 256 | |
|---|
| 257 | entry = DSL_Fifo_writeElement (&fifo); |
|---|
| 258 | if (entry != DSL_NULL) |
|---|
| 259 | *entry = 1; |
|---|
| 260 | entry = DSL_Fifo_writeElement (&fifo); |
|---|
| 261 | if (entry != DSL_NULL) |
|---|
| 262 | { |
|---|
| 263 | *entry = 2; |
|---|
| 264 | DSL_Fifo_returnElement (&fifo); |
|---|
| 265 | } |
|---|
| 266 | entry = DSL_Fifo_readElement (&fifo); |
|---|
| 267 | if (entry != DSL_NULL) |
|---|
| 268 | tst = *entry; |
|---|
| 269 | entry = DSL_Fifo_readElement (&fifo); |
|---|
| 270 | if (entry != DSL_NULL) |
|---|
| 271 | tst = *entry; |
|---|
| 272 | } |
|---|
| 273 | #endif |
|---|
| 274 | |
|---|