1 /**
2 * @file xmc_common.h
3 * @date 2019-07-12
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-06-20:
40 * - Initial
41 * - Brief section updated
42 * - Added XMC_LIB_VERSION macro
43 *
44 * 2016-02-26:
45 * - Updated XMC_LIB_VERSION macro to v2.1.6
46 *
47 * 2016-05-30:
48 * - Updated XMC_LIB_VERSION macro to v2.1.8
49 *
50 * 2016-11-18:
51 * - Updated XMC_LIB_VERSION macro to v2.1.10
52 * - Changed type of size in XMC_PRIOARRAY_t to fix compilation warnings
53 *
54 * 2017-04-04:
55 * - Updated XMC_LIB_VERSION macro to v2.1.12
56 *
57 * 2017-06-24:
58 * - Updated XMC_LIB_VERSION macro to v2.1.14
59 *
60 * 2017-08-03:
61 * - Updated XMC_LIB_VERSION macro to v2.1.16
62 * - Added __RAM_FUNC macro
63 *
64 * 2018-06-29:
65 * - Updated XMC_LIB_VERSION macro to v2.1.20
66 *
67 * 2019-05-29:
68 * - Updated XMC_LIB_VERSION macro to v2.1.22
69 *
70 * 2019-07-12:
71 * - Updated XMC_LIB_VERSION macro to v2.1.24
72 *
73 * @endcond
74 *
75 */
76
77 #ifndef XMC_COMMON_H
78 #define XMC_COMMON_H
79
80 #include <stddef.h>
81 #include <stdint.h>
82 #include <stdbool.h>
83 #include <string.h>
84
85 #include "xmc_device.h"
86
87 /**
88 * @addtogroup XMClib XMC Peripheral Library
89 * @{
90 */
91
92 /**
93 * @addtogroup COMMON
94 * @brief Common APIs to all peripherals for XMC microcontroller family
95 * @{
96 */
97
98 /**********************************************************************************************************************
99 * MACROS
100 *********************************************************************************************************************/
101 #define XMC_LIB_MAJOR_VERSION (2U)
102 #define XMC_LIB_MINOR_VERSION (1U)
103 #define XMC_LIB_PATCH_VERSION (24U)
104
105 #define XMC_LIB_VERSION ((XMC_LIB_MAJOR_VERSION << 16U) + (XMC_LIB_MINOR_VERSION << 8U) + XMC_LIB_PATCH_VERSION)
106
107 /* Define WEAK attribute */
108 #if !defined(__WEAK)
109 #if defined ( __CC_ARM )
110 #define __WEAK __attribute__ ((weak))
111 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
112 #define __WEAK __attribute__ ((weak))
113 #elif defined ( __ICCARM__ )
114 #define __WEAK __weak
115 #elif defined ( __GNUC__ )
116 #define __WEAK __attribute__ ((weak))
117 #elif defined ( __TASKING__ )
118 #define __WEAK __attribute__ ((weak))
119 #endif
120 #endif
121
122 #if !defined(__RAM_FUNC)
123 #if defined ( __CC_ARM )
124 // http://www.keil.com/support/docs/3723.htm
125 #define __RAM_FUNC __attribute__((section("RAMCODESECTION")))
126 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
127 #define __RAM_FUNC __attribute__((section("RAMCODESECTION")))
128 #elif defined ( __ICCARM__ )
129 // https://www.iar.com/support/tech-notes/linker/controlling-placement-of-the-section-where-__ramfunc-functions-reside-ewarm-5.x--6.x/
130 #define __RAM_FUNC __ramfunc
131 #elif defined ( __GNUC__ )
132 #define __RAM_FUNC __attribute__((section(".ram_code"), long_call))
133 #elif defined ( __TASKING__ )
134 #define __RAM_FUNC __attribute__((section(".ram_code")))
135 #endif
136 #endif
137
138 #ifdef XMC_ASSERT_ENABLE
139 #define XMC_ASSERT(msg, exp) { if(!(exp)) {XMC_AssertHandler(msg, __FILE__, __LINE__);} }
140 #else
141 #define XMC_ASSERT(msg, exp) { ; }
142 #endif
143
144 #ifdef XMC_DEBUG_ENABLE
145 #include <stdio.h>
146 #define XMC_DEBUG(...) { printf(__VA_ARGS__); }
147 #else
148 #define XMC_DEBUG(...) { ; }
149 #endif
150
151 #define XMC_UNUSED_ARG(x) (void)x
152
153 #define XMC_STRUCT_INIT(m) memset(&m, 0, sizeof(m))
154
155 #define XMC_PRIOARRAY_DEF(name, size) \
156 XMC_PRIOARRAY_ITEM_t prioarray_m_##name[size + 2]; \
157 XMC_PRIOARRAY_t prioarray_def_##name = {(size), (prioarray_m_##name)};
158
159 #define XMC_PRIOARRAY(name) \
160 &prioarray_def_##name
161
162 /**********************************************************************************************************************
163 * DATA STRUCTURES
164 *********************************************************************************************************************/
165 /*
166 *
167 */
168 typedef struct XMC_DRIVER_VERSION
169 {
170 uint8_t major;
171 uint8_t minor;
172 uint8_t patch;
173 } XMC_DRIVER_VERSION_t;
174
175 /*
176 *
177 */
178 typedef void *XMC_LIST_t;
179
180 /*
181 *
182 */
183 typedef struct XMC_PRIOARRAY_ITEM
184 {
185 int32_t priority;
186 int32_t previous;
187 int32_t next;
188 } XMC_PRIOARRAY_ITEM_t;
189
190 /*
191 *
192 */
193 typedef struct XMC_PRIOARRAY
194 {
195 int32_t size;
196 XMC_PRIOARRAY_ITEM_t *items;
197 } XMC_PRIOARRAY_t;
198
199 /**********************************************************************************************************************
200 * API PROTOTYPES
201 *********************************************************************************************************************/
202
203 #ifdef __cplusplus
204 extern "C" {
205 #endif
206
207 /*
208 *
209 */
210 void XMC_AssertHandler(const char *const msg, const char *const file, uint32_t line);
211
212 /*
213 *
214 */
215 void XMC_LIST_Init(XMC_LIST_t *list);
216
217 /*
218 *
219 */
220 void XMC_LIST_Add(XMC_LIST_t *list, void *const item);
221
222 /*
223 *
224 */
225 void XMC_LIST_Remove(XMC_LIST_t *list, void *const item);
226
227 /*
228 *
229 */
230 uint32_t XMC_LIST_GetLength(XMC_LIST_t *list);
231
232 /*
233 *
234 */
235 void *XMC_LIST_GetHead(XMC_LIST_t *list);
236
237 /*
238 *
239 */
240 void *XMC_LIST_GetTail(XMC_LIST_t *list);
241
242 /*
243 *
244 */
245 void XMC_LIST_Insert(XMC_LIST_t *list, void *prev_item, void *new_item);
246
247 /*
248 *
249 */
250 void XMC_PRIOARRAY_Init(XMC_PRIOARRAY_t *prioarray);
251
252 /*
253 *
254 */
255 void XMC_PRIOARRAY_Add(XMC_PRIOARRAY_t *prioarray, int32_t item, int32_t priority);
256
257 /*
258 *
259 */
260 void XMC_PRIOARRAY_Remove(XMC_PRIOARRAY_t *prioarray, int32_t item);
261
262 /*
263 *
264 */
XMC_PRIOARRAY_GetHead(XMC_PRIOARRAY_t * prioarray)265 __STATIC_INLINE int32_t XMC_PRIOARRAY_GetHead(XMC_PRIOARRAY_t *prioarray)
266 {
267 XMC_ASSERT("XMC_PRIOARRAY_Init: NULL pointer", prioarray != NULL);
268 return prioarray->items[prioarray->size].next;
269 }
270
271 /*
272 *
273 */
XMC_PRIOARRAY_GetTail(XMC_PRIOARRAY_t * prioarray)274 __STATIC_INLINE int32_t XMC_PRIOARRAY_GetTail(XMC_PRIOARRAY_t *prioarray)
275 {
276 XMC_ASSERT("XMC_PRIOARRAY_Init: NULL pointer", prioarray != NULL);
277 return prioarray->items[prioarray->size + 1].previous;
278 }
279
280 /*
281 *
282 */
XMC_PRIOARRAY_GetItemPriority(XMC_PRIOARRAY_t * prioarray,int32_t item)283 __STATIC_INLINE int32_t XMC_PRIOARRAY_GetItemPriority(XMC_PRIOARRAY_t *prioarray, int32_t item)
284 {
285 XMC_ASSERT("XMC_PRIOARRAY_GetItemPriority: item out of range", (item >= 0) && (item < prioarray->size));
286 return prioarray->items[item].priority;
287 }
288
289 /*
290 *
291 */
XMC_PRIOARRAY_GetItemNext(XMC_PRIOARRAY_t * prioarray,int32_t item)292 __STATIC_INLINE int32_t XMC_PRIOARRAY_GetItemNext(XMC_PRIOARRAY_t *prioarray, int32_t item)
293 {
294 XMC_ASSERT("XMC_PRIOARRAY_GetItemNext: item out of range", (item >= 0) && (item < prioarray->size));
295 return prioarray->items[item].next;
296 }
297
298 /*
299 *
300 */
XMC_PRIOARRAY_GetItemPrevious(XMC_PRIOARRAY_t * prioarray,int32_t item)301 __STATIC_INLINE int32_t XMC_PRIOARRAY_GetItemPrevious(XMC_PRIOARRAY_t *prioarray, int32_t item)
302 {
303 XMC_ASSERT("XMC_PRIOARRAY_GetItemPrevious: item out of range", (item >= 0) && (item < prioarray->size));
304 return prioarray->items[item].previous;
305 }
306
307 #ifdef __cplusplus
308 }
309 #endif
310
311 /**
312 * @}
313 */
314
315 /**
316 * @}
317 */
318
319 #endif /* XMC_COMMON_H */
320