1 /**
2  * @file xmc_common.c
3  * @date 2017-02-25
4  *
5  * @cond
6  *********************************************************************************************************************
7  * XMClib v2.1.24 - XMC Peripheral Driver Library
8  *
9  * Copyright (c) 2015-2019, Infineon Technologies AG
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
13  * following conditions are met:
14  *
15  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16  * disclaimer.
17  *
18  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided with the distribution.
20  *
21  * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
33  * Infineon Technologies AG dave@infineon.com).
34  *********************************************************************************************************************
35  *
36  * Change History
37  * --------------
38  *
39  * 2015-02-20:
40  *     - Initial <br>
41  *
42  * 2017-02-25:
43  *     - Remove the need to define XMC_USER_ASSERT_FUNCTION
44  *     - XMC_AssertHandler fixed compilation warnings
45  *
46  * @endcond
47  *
48  */
49 
50 #include "xmc_common.h"
51 
52 /*******************************************************************************
53  * DATA STRUCTURES
54  *******************************************************************************/
55 struct list
56 {
57   struct list *next;
58 };
59 
60 /*******************************************************************************
61  * API IMPLEMENTATION
62  *******************************************************************************/
63 #if defined(XMC_ASSERT_ENABLE)
XMC_AssertHandler(const char * const msg,const char * const file,uint32_t line)64 __WEAK void XMC_AssertHandler(const char *const msg, const char *const file, uint32_t line)
65 {
66   XMC_UNUSED_ARG(msg);
67   XMC_UNUSED_ARG(file);
68   XMC_UNUSED_ARG(line);
69 
70   while(1)
71   {
72     /* Endless loop */
73   }
74 }
75 #endif
76 
XMC_LIST_Init(XMC_LIST_t * list)77 void XMC_LIST_Init(XMC_LIST_t *list)
78 {
79   *list = NULL;
80 }
81 
XMC_LIST_GetHead(XMC_LIST_t * list)82 void *XMC_LIST_GetHead(XMC_LIST_t *list)
83 {
84   return *list;
85 }
86 
XMC_LIST_GetTail(XMC_LIST_t * list)87 void *XMC_LIST_GetTail(XMC_LIST_t *list)
88 {
89   struct list *tail;
90 
91   if (*list == NULL)
92   {
93     tail = NULL;
94   }
95   else
96   {
97     for (tail = (struct list *)*list; tail->next != NULL; tail = tail->next)
98     {
99       /* Loop through the list */
100     }
101   }
102 
103   return tail;
104 }
105 
XMC_LIST_Add(XMC_LIST_t * list,void * item)106 void XMC_LIST_Add(XMC_LIST_t *list, void *item)
107 {
108   struct list *tail;
109 
110   ((struct list *)item)->next = NULL;
111   tail = (struct list *)XMC_LIST_GetTail(list);
112 
113   if (tail == NULL)
114   {
115     *list = item;
116   }
117   else
118   {
119     tail->next = (struct list *)item;
120   }
121 }
122 
XMC_LIST_Remove(XMC_LIST_t * list,void * item)123 void XMC_LIST_Remove(XMC_LIST_t *list, void *item)
124 {
125   struct list *right, *left;
126 
127   if (*list != NULL)
128   {
129     left = NULL;
130     for(right = (struct list *)*list; right != NULL; right = right->next)
131     {
132       if(right == item)
133       {
134         if(left == NULL)
135         {
136           /* First on list */
137           *list = right->next;
138         }
139         else
140         {
141           /* Not first on list */
142           left->next = right->next;
143         }
144         right->next = NULL;
145         break;
146       }
147       left = right;
148     }
149   }
150 }
151 
XMC_LIST_Insert(XMC_LIST_t * list,void * prev_item,void * new_item)152 void XMC_LIST_Insert(XMC_LIST_t *list, void *prev_item, void *new_item)
153 {
154   if (prev_item == NULL)
155   {
156 	((struct list *)new_item)->next = (struct list *)*list;
157 	*list = new_item;
158   }
159   else
160   {
161     ((struct list *)new_item)->next = ((struct list *)prev_item)->next;
162     ((struct list *)prev_item)->next = (struct list *)new_item;
163   }
164 }
165 
XMC_PRIOARRAY_Init(XMC_PRIOARRAY_t * prioarray)166 void XMC_PRIOARRAY_Init(XMC_PRIOARRAY_t *prioarray)
167 {
168   XMC_ASSERT("XMC_PRIOARRAY_Init: NULL pointer", prioarray != NULL);
169 
170   /* Initialize head, next points to tail, previous to NULL and the priority is MININT */
171   prioarray->items[prioarray->size].next = prioarray->size + 1;
172   prioarray->items[prioarray->size].previous = -1;
173   prioarray->items[prioarray->size].priority = INT32_MAX;
174 
175   /* Initialize tail, next points to NULL, previous is the head and the priority is MAXINT */
176   prioarray->items[prioarray->size + 1].next = -1;
177   prioarray->items[prioarray->size + 1].previous = prioarray->size;
178   prioarray->items[prioarray->size + 1].priority = INT32_MIN;
179 
180 }
181 
XMC_PRIOARRAY_Add(XMC_PRIOARRAY_t * prioarray,int32_t item,int32_t priority)182 void XMC_PRIOARRAY_Add(XMC_PRIOARRAY_t *prioarray, int32_t item, int32_t priority)
183 {
184   int32_t next;
185   int32_t previous;
186 
187   XMC_ASSERT("XMC_PRIOARRAY_Add: item out of range", (item >= 0) && (item < prioarray->size));
188 
189   next = XMC_PRIOARRAY_GetHead(prioarray);
190   while (XMC_PRIOARRAY_GetItemPriority(prioarray, next) > priority)
191   {
192     next = XMC_PRIOARRAY_GetItemNext(prioarray, next);
193   }
194 
195   previous = prioarray->items[next].previous;
196 
197   prioarray->items[item].next = next;
198   prioarray->items[item].previous = previous;
199   prioarray->items[item].priority = priority;
200 
201   prioarray->items[previous].next = item;
202   prioarray->items[next].previous = item;
203 }
204 
XMC_PRIOARRAY_Remove(XMC_PRIOARRAY_t * prioarray,int32_t item)205 void XMC_PRIOARRAY_Remove(XMC_PRIOARRAY_t *prioarray, int32_t item)
206 {
207   int32_t next;
208   int32_t previous;
209 
210   XMC_ASSERT("XMC_PRIOARRAY_Add: item out of range", (item >= 0) && (item < prioarray->size));
211 
212   next = prioarray->items[item].next;
213   previous = prioarray->items[item].previous;
214 
215   prioarray->items[previous].next = next;
216   prioarray->items[next].previous = previous;
217 }
218 
219 
220