Statistics
| Branch: | Tag: | Revision:

mongoose / examples / PIC32 / mqtt_client / firmware / src / system_config / mx795_CC3100_e16 / framework / driver / spi / static / src / drv_spi_static_sys_queue.c @ eaef5bd1

History | View | Annotate | Download (23.8 KB)

1
/* clang-format off */
2
/*******************************************************************************
3
  SPI Driver Queue related local interfaces
4

5
  Company:
6
    Microchip Technology Inc.
7

8
  File Name:
9
    drv_spi_static_sys_queue.h
10

11
  Summary:
12
    Queue related local interface declarations
13

14
  Description:
15
    Queue related local interface declarations
16
*******************************************************************************/
17

    
18
//DOM-IGNORE-BEGIN
19
/*******************************************************************************
20
Copyright (c) 2015 released Microchip Technology Inc.  All rights reserved.
21

22
Microchip licenses to you the right to use, modify, copy and distribute Software
23
only when embedded on a Microchip microcontroller or digital  signal  controller
24
that is integrated into your product or third party  product  (pursuant  to  the
25
sublicense terms in the accompanying license agreement).
26

27
You should refer to the license agreement accompanying this Software for
28
additional information regarding your rights and obligations.
29

30
SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS  WITHOUT  WARRANTY  OF  ANY  KIND,
31
EITHER EXPRESS  OR  IMPLIED,  INCLUDING  WITHOUT  LIMITATION,  ANY  WARRANTY  OF
32
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A  PARTICULAR  PURPOSE.
33
IN NO EVENT SHALL MICROCHIP OR  ITS  LICENSORS  BE  LIABLE  OR  OBLIGATED  UNDER
34
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,  BREACH  OF  WARRANTY,  OR
35
OTHER LEGAL  EQUITABLE  THEORY  ANY  DIRECT  OR  INDIRECT  DAMAGES  OR  EXPENSES
36
INCLUDING BUT NOT LIMITED TO ANY  INCIDENTAL,  SPECIAL,  INDIRECT,  PUNITIVE  OR
37
CONSEQUENTIAL DAMAGES, LOST  PROFITS  OR  LOST  DATA,  COST  OF  PROCUREMENT  OF
38
SUBSTITUTE  GOODS,  TECHNOLOGY,  SERVICES,  OR  ANY  CLAIMS  BY  THIRD   PARTIES
39
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE  THEREOF),  OR  OTHER  SIMILAR  COSTS.
40
*******************************************************************************/
41
//DOM-IGNORE-END
42

    
43
// *****************************************************************************
44
// *****************************************************************************
45
// Section: Included Files
46
// *****************************************************************************
47
// *****************************************************************************
48
#include <string.h>
49
#include "system_config.h"
50
#include "system_definitions.h"
51

    
52
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_LockQueue(DRV_SPI_SYS_QUEUE_HANDLE queue, bool freeList);
53
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_UnlockQueue(DRV_SPI_SYS_QUEUE_HANDLE queue, bool freeList);
54
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_LockQueueManager(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager);
55
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_UnlockQueueManager(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager);
56

    
57

    
58
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Initialize(DRV_SPI_SYS_QUEUE_MANAGER_SETUP * initParams, DRV_SPI_SYS_QUEUE_MANAGER_HANDLE * handle)
59
{
60
    if (initParams == NULL || initParams->pBuffer == NULL || handle == NULL)
61
    {
62
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
63
    }
64

    
65
    if (initParams->type != DRV_SPI_SYS_QUEUE_Fifo)
66
    {
67
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
68
    }
69

    
70
    size_t sizeNeeded = (sizeof(DRV_SPI_SYS_QUEUE_QUEUE_DATA) *
71
                        initParams->numQueues) +
72
                        sizeof(DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA) +
73
                        (sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA) +
74
                        initParams->elementSize);
75

    
76
    if (initParams->bufferLen < sizeNeeded)
77
    {
78
        return DRV_SPI_SYS_QUEUE_OUT_OF_MEMORY;
79
    }
80

    
81
    // Blank the memory area
82
    memset(initParams->pBuffer, 0, initParams->bufferLen);
83

    
84
    // Set up the Queue Manager Area
85
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = (DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA *)initParams->pBuffer;
86
    pQueueManager->pQueueArea  = (DRV_SPI_SYS_QUEUE_QUEUE_DATA*)((uint32_t)pQueueManager + sizeof(DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA));
87
    pQueueManager->pElementArea  = (DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA*)((uint32_t)pQueueManager->pQueueArea + (sizeof(DRV_SPI_SYS_QUEUE_QUEUE_DATA) * initParams->numQueues) );
88

    
89
    //Set up the Queue Handles
90
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = pQueueManager->pQueueArea;
91
    pQueueManager->pFreeQueueHead = pQueue;
92
    pQueueManager->pFreeQueueTail = pQueue;
93
    pQueue->pQueueManager = pQueueManager;
94
    uint8_t counter;
95
    for (counter = 1; counter < initParams->numQueues; counter++)
96
    {
97
        pQueueManager->pFreeQueueTail->pNext = &(pQueue[counter]);
98
        pQueueManager->pFreeQueueTail = &(pQueue[counter]);
99
        pQueue[counter].pQueueManager = pQueueManager;
100
    }
101

    
102
    // Set up the elements
103
    size_t spaceRemaining = initParams->bufferLen -
104
            sizeof(DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA) -
105
            (sizeof(DRV_SPI_SYS_QUEUE_QUEUE_DATA) * initParams->numQueues);
106
    size_t numberOfElements = spaceRemaining /
107
            (sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA) + initParams->elementSize );
108

    
109
    DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA * pElement = pQueueManager->pElementArea;
110
    pQueueManager->pFreeElementHead = pElement;
111
    pQueueManager->pFreeElementTail = pElement;
112

    
113
    for (counter = 1; counter < numberOfElements; counter ++)
114
    {
115
        pElement = (DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA *)
116
              ((uint32_t)pQueueManager->pElementArea +
117
               (sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA) +
118
                initParams->elementSize) *
119
               counter);
120
        pQueueManager->pFreeElementTail->pNext = pElement;
121
        pQueueManager->pFreeElementTail = pElement;
122
    }
123
    pQueueManager->numFreeElements = numberOfElements;
124
#if _SPI_DRV_SYS_QUEUE_TRACKING
125
    pQueueManager->freeElementsLW = numberOfElements;
126
#endif
127
    *handle = (DRV_SPI_SYS_QUEUE_MANAGER_HANDLE)pQueueManager;
128
    return DRV_SPI_SYS_QUEUE_SUCCESS;
129
}
130

    
131
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Deinitialize(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager)
132
{
133
    if ((queueManager <= 0) && (queueManager >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
134
    {
135
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
136
    }
137
    return DRV_SPI_SYS_QUEUE_SUCCESS;
138
}
139

    
140
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_CreateQueue(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager, DRV_SPI_SYS_QUEUE_SETUP * initParams, DRV_SPI_SYS_QUEUE_HANDLE * handle)
141
{
142
    if ((queueManager <= 0) && (queueManager >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
143
    {
144
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
145
    }
146
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = (DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA *)queueManager;
147
    if (initParams == NULL)
148
    {
149
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
150
    }
151
    if (pQueueManager->pFreeQueueHead == NULL)
152
    {
153
        return DRV_SPI_SYS_QUEUE_OUT_OF_QUEUES;
154
    }
155

    
156
    if ((pQueueManager->numReserveElements + initParams->reserveElements) > pQueueManager->numFreeElements)
157
    {
158
        return DRV_SPI_SYS_QUEUE_OUT_OF_MEMORY;
159
    }
160

    
161
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = pQueueManager->pFreeQueueHead;
162
    pQueueManager->pFreeQueueHead = pQueue->pNext;
163
    if (pQueueManager->pFreeQueueHead == NULL)
164
    {
165
        pQueueManager->pFreeQueueTail = NULL;
166
    }
167
    memset(pQueue, 0, sizeof(DRV_SPI_SYS_QUEUE_QUEUE_DATA));
168
    pQueue->pQueueManager = pQueueManager;
169

    
170
    pQueue->fptrIntChange = initParams->fptrIntChange;
171
    pQueue->numReserved = initParams->reserveElements;
172
    pQueue->maxElements = initParams->maxElements;
173

    
174
    pQueueManager->numReserveElements += initParams->reserveElements;
175

    
176
#if _SPI_DRV_SYS_QUEUE_TRACKING
177
    pQueueManager->numQueueCreateOps++;
178
    pQueueManager->numQueues++;
179
    pQueueManager->reserveElementsLW += initParams->reserveElements;
180
    if (pQueueManager->numQueues > pQueueManager->numQueuesHW)
181
    {
182
        pQueueManager->numQueuesHW = pQueueManager->numQueues;
183
    }
184
#endif
185

    
186
   *handle =  (DRV_SPI_SYS_QUEUE_HANDLE)pQueue;
187
   return DRV_SPI_SYS_QUEUE_SUCCESS;
188
}
189

    
190
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_DestroyQueue(DRV_SPI_SYS_QUEUE_HANDLE queue)
191
{
192
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
193
    {
194
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
195
    }
196

    
197
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
198
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = pQueue->pQueueManager;
199

    
200
    if (pQueue->pNext != NULL)
201
    {
202
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
203
    }
204

    
205

    
206
    void * pElement = NULL;
207
    DRV_SPI_SYS_QUEUE_Dequeue(queue, &pElement);
208
    while (pElement != NULL)
209
    {
210
        DRV_SPI_SYS_QUEUE_FreeElement(queue, pElement);
211
        DRV_SPI_SYS_QUEUE_Dequeue(queue, &pElement);
212
    }
213

    
214
    pQueueManager->numReserveElements -= pQueue->numReserved;
215
    if (pQueueManager->pFreeQueueTail == NULL)
216
    {
217
        pQueueManager->pFreeQueueTail = pQueue;
218
        pQueueManager->pFreeQueueHead = pQueue;
219
    }
220
    else
221
    {
222
        pQueue->pNext = pQueueManager->pFreeQueueHead;
223
        pQueueManager->pFreeQueueHead = pQueue;
224
    }
225

    
226
#if _SPI_DRV_SYS_QUEUE_TRACKING
227
    pQueueManager->numQueueDestroyOps++;
228
    pQueueManager->numQueues--;
229
    pQueueManager->reserveElementsLW -= pQueueManager->numReserveElements;
230
#endif
231

    
232
    return DRV_SPI_SYS_QUEUE_SUCCESS;
233
}
234

    
235
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_AllocElement(DRV_SPI_SYS_QUEUE_HANDLE queue, void ** element)
236
{
237
    if (element == NULL)
238
    {
239
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
240
    }
241
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
242
    {
243
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
244
    }
245

    
246
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
247
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = pQueue->pQueueManager;
248

    
249
    if (pQueue->pNext != NULL)
250
    {
251
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
252
    }
253

    
254
    if (pQueueManager->pFreeElementHead== NULL)
255
    {
256
#if _SPI_DRV_SYS_QUEUE_TRACKING
257
        pQueueManager->outOfMemoryErrors++;
258
        pQueue->outOfMemoryErrors++;
259
#endif
260
        return DRV_SPI_SYS_QUEUE_OUT_OF_MEMORY;
261
    }
262

    
263
    if (pQueue->numAlloc == pQueue->maxElements)
264
    {
265
#if _SPI_DRV_SYS_QUEUE_TRACKING
266
        pQueueManager->outOfMemoryErrors++;
267
        pQueue->outOfMemoryErrors++;
268
#endif
269
        return DRV_SPI_SYS_QUEUE_OUT_OF_MEMORY;
270
    }
271

    
272
    if (pQueue->numAlloc < pQueue->numReserved)
273
    {
274
        pQueueManager->numReserveElements--;
275
    }
276
    else if (pQueueManager->numFreeElements == pQueueManager->numReserveElements)
277
    {
278
#if _SPI_DRV_SYS_QUEUE_TRACKING
279
        pQueueManager->outOfMemoryErrors++;
280
        pQueue->outOfMemoryErrors++;
281
#endif
282
        return DRV_SPI_SYS_QUEUE_OUT_OF_MEMORY;
283
    }
284

    
285
    DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA * pEntry = pQueueManager->pFreeElementHead;
286
    pQueueManager->pFreeElementHead = pEntry->pNext;
287
    if (pQueueManager->pFreeElementHead == NULL)
288
    {
289
        pQueueManager->pFreeElementTail = NULL;
290
    }
291
    pEntry->pNext = NULL;
292
    pQueueManager->numFreeElements --;
293
    pQueue->numAlloc++;
294

    
295
#if _SPI_DRV_SYS_QUEUE_TRACKING
296
    pQueue->numAllocOps ++;
297
    pQueueManager->numAllocOps ++;
298
    if (pQueueManager->numFreeElements < pQueueManager->freeElementsLW)
299
    {
300
        pQueueManager->freeElementsLW = pQueueManager->numFreeElements;
301
    }
302
    if (pQueueManager->numReserveElements < pQueueManager->reserveElementsLW)
303
    {
304
        pQueueManager->reserveElementsLW = pQueueManager->numReserveElements;
305
    }
306
    if (pQueue->numAlloc > pQueue->numAllocHW)
307
    {
308
        pQueue->numAllocHW = pQueue->numAlloc;
309
    }
310
#endif
311
    *element = (void *)((uint32_t)pEntry + sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA));
312
    return DRV_SPI_SYS_QUEUE_SUCCESS;
313
}
314

    
315
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_FreeElement(DRV_SPI_SYS_QUEUE_HANDLE queue, void * element)
316
{
317
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
318
    {
319
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
320
    }
321

    
322
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
323
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = pQueue->pQueueManager;
324

    
325
    if (pQueue->pNext != NULL)
326
    {
327
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
328
    }
329

    
330
    DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA * pEntry = (DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA *)((uint32_t)element - sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA));
331

    
332
    if (pQueueManager->pFreeElementHead == NULL)
333
    {
334
        pQueueManager->pFreeElementHead = pEntry;
335
    }
336
    else
337
    {
338
        pQueueManager->pFreeElementTail->pNext = pEntry;
339
    }
340
    pQueueManager->pFreeElementTail = pEntry;
341

    
342
    pQueueManager->numFreeElements++;
343
    pQueue->numAlloc--;
344
    if (pQueue->numAlloc < pQueue->numReserved)
345
    {
346
        pQueueManager->numReserveElements++;
347
    }
348

    
349
#if _SPI_DRV_SYS_QUEUE_TRACKING
350
    pQueue->numFreeOps ++;
351
    pQueueManager->numFreeOps ++;
352
#endif
353
    return DRV_SPI_SYS_QUEUE_SUCCESS;
354
}
355

    
356
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Enqueue(DRV_SPI_SYS_QUEUE_HANDLE queue, void * element)
357
{
358
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
359
    {
360
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
361
    }
362

    
363
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
364

    
365
    if (pQueue->pNext != NULL)
366
    {
367
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
368
    }
369

    
370
    DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA * pEntry = (DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA *)((uint32_t)element - sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA));
371

    
372
    if (pQueue->pHead == NULL)
373
    {
374
        pQueue->pHead = pEntry;
375
        pQueue->pTail = pEntry;
376
    }
377
    else
378
    {
379
        pQueue->pTail->pNext = pEntry;
380
        pQueue->pTail = pEntry;
381
    }
382

    
383
#if _SPI_DRV_SYS_QUEUE_TRACKING
384
    pQueue->numEnqueued++;
385
    if (pQueue->numEnqueued > pQueue->numEnqueuedHW)
386
    {
387
        pQueue->numEnqueuedHW = pQueue->numEnqueued;
388
    }
389
    pQueue->numEnqueueOps++;
390
#endif
391
    return DRV_SPI_SYS_QUEUE_SUCCESS;
392
}
393

    
394
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Dequeue(DRV_SPI_SYS_QUEUE_HANDLE queue, void ** element)
395
{
396
    if (element == NULL)
397
    {
398
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
399
    }
400
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
401
    {
402
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
403
    }
404

    
405
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
406

    
407
    if (pQueue->pNext != NULL)
408
    {
409
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
410
    }
411
    if (pQueue->pHead == NULL)
412
    {
413
        *element = NULL;
414
        return DRV_SPI_SYS_QUEUE_SUCCESS;
415
    }
416
    DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA * pEntry = pQueue->pHead;
417
    pQueue->pHead = pEntry->pNext;
418
    if (pQueue->pHead == NULL)
419
    {
420
        pQueue->pTail = NULL;
421
    }
422

    
423
    pEntry->pNext = NULL;
424

    
425
#if _SPI_DRV_SYS_QUEUE_TRACKING
426
    pQueue->numEnqueued--;
427
    pQueue->numDequeueOps++;
428
#endif
429
    *element = (void *)((uint32_t)pEntry + sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA));
430
    return DRV_SPI_SYS_QUEUE_SUCCESS;
431
}
432

    
433
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Peek(DRV_SPI_SYS_QUEUE_HANDLE queue, void ** element)
434
{
435
    if (element == NULL)
436
    {
437
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
438
    }
439
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
440
    {
441
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
442
    }
443

    
444
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
445

    
446
    if (pQueue->pNext != NULL)
447
    {
448
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
449
    }
450
    if (pQueue->pHead == NULL)
451
    {
452
        *element = NULL;
453
    }
454
    DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA * pEntry = pQueue->pHead;
455
    *element = (void *)((uint32_t)pEntry + sizeof(DRV_SPI_SYS_QUEUE_FIFO_ELEMENT_DATA));
456
    return DRV_SPI_SYS_QUEUE_SUCCESS;
457
}
458

    
459
bool DRV_SPI_SYS_QUEUE_IsEmpty(DRV_SPI_SYS_QUEUE_HANDLE queue)
460
{
461
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
462
    return pQueue->pHead == NULL;
463
}
464

    
465
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Lock(DRV_SPI_SYS_QUEUE_HANDLE queue)
466
{
467

    
468
    return _DRV_SPI_SYS_QUEUE_LockQueue(queue, false);
469
}
470

    
471
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_Unlock(DRV_SPI_SYS_QUEUE_HANDLE queue)
472
{
473
    return _DRV_SPI_SYS_QUEUE_UnlockQueue(queue, false);
474
}
475

    
476
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_LockQueue(DRV_SPI_SYS_QUEUE_HANDLE queue, bool freeList)
477
{
478
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
479
    {
480
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
481
    }
482

    
483
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
484
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = pQueue->pQueueManager;
485

    
486
    if (pQueue->pNext != NULL)
487
    {
488
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
489
    }
490

    
491
    if (freeList)
492
    {
493
        return _DRV_SPI_SYS_QUEUE_LockQueueManager((DRV_SPI_SYS_QUEUE_MANAGER_HANDLE)pQueueManager);
494
    }
495
    else
496
    {
497
        if (pQueue->fptrIntChange != NULL)
498
        {
499
            (*pQueue->fptrIntChange)(queue, true);
500
        }
501
    }
502

    
503
    return DRV_SPI_SYS_QUEUE_SUCCESS;
504
}
505

    
506
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_UnlockQueue(DRV_SPI_SYS_QUEUE_HANDLE queue, bool freeList)
507
{
508
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
509
    {
510
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
511
    }
512

    
513
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
514
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = pQueue->pQueueManager;
515

    
516
    if (pQueue->pNext != NULL)
517
    {
518
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
519
    }
520

    
521
    if (freeList)
522
    {
523
        return _DRV_SPI_SYS_QUEUE_UnlockQueueManager((DRV_SPI_SYS_QUEUE_MANAGER_HANDLE)pQueueManager);
524
    }
525
    else
526
    {
527
        if (pQueue->fptrIntChange != NULL)
528
        {
529
            (*pQueue->fptrIntChange)(queue, false);
530
        }
531
    }
532

    
533
    return DRV_SPI_SYS_QUEUE_SUCCESS;
534
}
535

    
536
DRV_SPI_SYS_QUEUE_HANDLE DRV_SPI_SYS_QUEUE_CreateQueueLock(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager, DRV_SPI_SYS_QUEUE_SETUP * initParams, DRV_SPI_SYS_QUEUE_HANDLE * queue)
537
{
538
    DRV_SPI_SYS_QUEUE_RESULT ret;
539
    DRV_SPI_SYS_QUEUE_HANDLE  ret2;
540
    ret = _DRV_SPI_SYS_QUEUE_LockQueueManager(queueManager);
541
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
542
    {
543
        return ret;
544
    }
545

    
546
    ret2 = DRV_SPI_SYS_QUEUE_CreateQueue(queueManager, initParams, queue);
547

    
548
    ret = _DRV_SPI_SYS_QUEUE_UnlockQueueManager(queueManager);
549
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
550
    {
551
        return ret;
552
    }
553
    return ret2;
554
}
555

    
556
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_DestroyQueueLock(DRV_SPI_SYS_QUEUE_HANDLE queue)
557
{
558
    DRV_SPI_SYS_QUEUE_RESULT ret;
559
    DRV_SPI_SYS_QUEUE_RESULT  ret2;
560
    ret = _DRV_SPI_SYS_QUEUE_LockQueue(queue, true);// TODO!!! Fix this
561
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
562
    {
563
        return ret;
564
    }
565

    
566
    ret2 = DRV_SPI_SYS_QUEUE_DestroyQueue(queue);
567

    
568
    ret = _DRV_SPI_SYS_QUEUE_UnlockQueue(queue, true);
569
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
570
    {
571
        return ret;
572
    }
573
    return ret2;
574
}
575

    
576
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_AllocElementLock(DRV_SPI_SYS_QUEUE_HANDLE queue, void  ** element)
577
{
578
    DRV_SPI_SYS_QUEUE_RESULT ret;
579
    DRV_SPI_SYS_QUEUE_RESULT ret2;
580
    ret = _DRV_SPI_SYS_QUEUE_LockQueue(queue, true);
581
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
582
    {
583
        return ret;
584
    }
585

    
586
    ret2 = DRV_SPI_SYS_QUEUE_AllocElement(queue, element);
587

    
588
    ret = _DRV_SPI_SYS_QUEUE_UnlockQueue(queue, true);
589
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
590
    {
591
        return ret;
592
    }
593
    return ret2;
594
}
595

    
596

    
597
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_FreeElementLock(DRV_SPI_SYS_QUEUE_HANDLE queue, void * element)
598
{
599
    DRV_SPI_SYS_QUEUE_RESULT ret;
600
    DRV_SPI_SYS_QUEUE_RESULT  ret2;
601
    ret = _DRV_SPI_SYS_QUEUE_LockQueue(queue, true);
602
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
603
    {
604
        return ret;
605
    }
606

    
607
    ret2 = DRV_SPI_SYS_QUEUE_FreeElement(queue, element);
608

    
609
    ret = _DRV_SPI_SYS_QUEUE_UnlockQueue(queue, true);
610
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
611
    {
612
        return ret;
613
    }
614
    return ret2;
615
}
616

    
617
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_EnqueueLock(DRV_SPI_SYS_QUEUE_HANDLE queue, void * element)
618
{
619
    DRV_SPI_SYS_QUEUE_RESULT ret;
620
    DRV_SPI_SYS_QUEUE_RESULT  ret2;
621
    ret = _DRV_SPI_SYS_QUEUE_LockQueue(queue, false);
622
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
623
    {
624
        return ret;
625
    }
626

    
627
    ret2 = DRV_SPI_SYS_QUEUE_Enqueue(queue, element);
628

    
629
    ret = _DRV_SPI_SYS_QUEUE_UnlockQueue(queue, false);
630
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
631
    {
632
        return ret;
633
    }
634
    return ret2;
635
}
636

    
637
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_DequeueLock(DRV_SPI_SYS_QUEUE_HANDLE queue, void ** element)
638
{
639
    DRV_SPI_SYS_QUEUE_RESULT ret;
640
    DRV_SPI_SYS_QUEUE_RESULT  ret2;
641
    ret = _DRV_SPI_SYS_QUEUE_LockQueue(queue, false);
642
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
643
    {
644
        return ret;
645
    }
646

    
647
    ret2 = DRV_SPI_SYS_QUEUE_Dequeue(queue, element);
648

    
649
    ret = _DRV_SPI_SYS_QUEUE_UnlockQueue(queue, false);
650
    if (ret != DRV_SPI_SYS_QUEUE_SUCCESS)
651
    {
652
        return ret;
653
    }
654
    return ret2;
655
}
656

    
657
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_QueueManagerStatus(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager, DRV_SPI_SYS_QUEUE_MANAGER_STATUS * status)
658
{
659
#if _SPI_DRV_SYS_QUEUE_TRACKING
660
    if ((queueManager <= 0) && (queueManager >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
661
    {
662
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
663
    }
664
    DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA * pQueueManager = (DRV_SPI_SYS_QUEUE_QUEUE_MANAGER_DATA *)queueManager;
665

    
666
    status->numAllocOps = pQueueManager->numAllocOps;
667
    status->numFreeOps = pQueueManager->numFreeOps;
668
    status->numQueueCreateOps = pQueueManager->numQueueCreateOps;
669
    status->numQueueDestroyOps = pQueueManager->numQueueDestroyOps;
670
    status->numReserveElements = pQueueManager->numReserveElements;
671
    status->numFreeElements = pQueueManager->numFreeElements;
672
    status->freeElementsLW = pQueueManager->freeElementsLW;
673
    status->reserveElementsLW = pQueueManager->reserveElementsLW;
674
    status->outOfMemoryErrors = pQueueManager->outOfMemoryErrors;
675
    status->numQueues = pQueueManager->numQueues;
676
    status->numQueuesHW = pQueueManager->numQueuesHW;
677
    return DRV_SPI_SYS_QUEUE_SUCCESS;
678
#else
679
    return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
680
#endif
681
}
682

    
683
DRV_SPI_SYS_QUEUE_RESULT DRV_SPI_SYS_QUEUE_QueueStatus(DRV_SPI_SYS_QUEUE_HANDLE queue, DRV_SPI_SYS_QUEUE_STATUS * status)
684
{
685
#if _SPI_DRV_SYS_QUEUE_TRACKING
686
    if ((queue <= 0) && (queue >= DRV_SPI_SYS_QUEUE_MAX_ERROR))
687
    {
688
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
689
    }
690

    
691
    DRV_SPI_SYS_QUEUE_QUEUE_DATA * pQueue = (DRV_SPI_SYS_QUEUE_QUEUE_DATA *)queue;
692

    
693
    if (pQueue->pNext != NULL)
694
    {
695
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
696
    }
697
    if (status == NULL)
698
    {
699
        return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
700
    }
701

    
702
    status->numAllocOps = pQueue->numAllocOps;
703
    status->numFreeOps = pQueue->numFreeOps;
704
    status->numDequeueOps = pQueue->numDequeueOps;
705
    status->numEnqueueOps = pQueue->numEnqueueOps;
706
    status->numReserved = pQueue->numReserved;
707
    status->numAlloc = pQueue->numAlloc;
708
    status->numEnqueued = pQueue->numEnqueued;
709
    status->numReserveLW = pQueue->numReserveLW;
710
    status->numAllocHW = pQueue->numAllocHW;
711
    status->numEnqueuedHW = pQueue->numEnqueuedHW;
712
    return DRV_SPI_SYS_QUEUE_SUCCESS;
713
#else
714
    return DRV_SPI_SYS_QUEUE_INVALID_PARAMETER;
715
#endif
716

    
717
}
718

    
719
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_LockQueueManager(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager)
720
{
721
    return DRV_SPI_SYS_QUEUE_SUCCESS;
722
}
723
DRV_SPI_SYS_QUEUE_RESULT _DRV_SPI_SYS_QUEUE_UnlockQueueManager(DRV_SPI_SYS_QUEUE_MANAGER_HANDLE queueManager)
724
{
725
    return DRV_SPI_SYS_QUEUE_SUCCESS;
726
}