Developer World
Spresense SDK Library v3.2.0-ebc0364
common_types.h
1/****************************************************************************
2 * modules/include/memutils/common_utils/common_types.h
3 *
4 * Copyright 2018 Sony Semiconductor Solutions Corporation
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17 * the names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36#ifndef COMMON_TYPES_H
37#define COMMON_TYPES_H
38
39/***********************************************************************
40 *
41 * Common Environment
42 *
43 ***********************************************************************
44 */
45
46/*-------------------------------------------------------------------
47 * Standard Libraries
48 *-------------------------------------------------------------------*/
49#if (defined(__CC_ARM) && (__ARMCC_VERSION >= 5050041))
50#include <stddef.h> /* for NULL */
51#else
52#include <sys/types.h>
53#endif
54
55/* ushort, uint is defined in <sys/types.h> */
56
57typedef unsigned char uchar;
58typedef unsigned long ulong;
59
60/* Since ARMCC 5.05 doesn't have sys/types.h and ushort/uint isn't defined */
61
62#if defined(_WIN32) || (defined(__CC_ARM) && (__ARMCC_VERSION >= 5050041))
63typedef unsigned short ushort;
64typedef unsigned int uint;
65#endif
66
67/*
68 * fixed size types
69 */
70
71#include <stdint.h>
72
73typedef uint32_t drm_t; /* DRM address. 0 origin byte addressing */
74static const uint32_t INVALID_DRM = 0xffffffffU; /* Invalid DRM address */
75
76#if (!defined(_ITRON_H_) && !defined(_POSIX))
77# ifndef TRUE
78# define TRUE 1
79# else
80S_ASSERT(TRUE == 1); /* ERROR: TRUE is not 1 */
81# endif
82
83
84# ifndef FALSE
85# define FALSE 0
86# else
87S_ASSERT(FALSE == 0); /* ERROR: FALSE is not 0 */
88# endif
89#endif /* _ITRON_H_ && _POSIX */
90
91#define ON (1)
92#define OFF (0)
93
94#ifndef MIN
95#define MIN(a,b) (((a)<(b))?(a):(b))
96#endif
97#ifndef MAX
98#define MAX(a,b) (((a)>(b))?(a):(b))
99#endif
100#define ABS(x) (((x) < 0) ? ((~(x))+1) : (x))
101#define AVE(x, y) (((x)>>1)+((y)>>1))
102#define LOW16(x) (0xffff & (x))
103#define HIGH16(x) (0xffff & ((x)>>16))
104#define CLIPPING(val, min, max) {\
105 (val) = MAX((val), (min)); \
106 (val) = MIN((val), (max)); \
107}
108
109#define WORD_BIT 32
110#define LONG_BIT WORD_BIT
111#define HWORD_BIT 16
112#define SHORT_BIT HWORD_BIT
113#define BYTE_BIT 8
114
115#define bit_sizeof(x) (sizeof(x)*CHAR_BIT)
116
117/*** BITS Macro ***/
118/* width : 0~31
119 offset : 0~31
120 val : uint(type)
121 spec : Take out bits from a "val".
122*/
123#define BITS(width,offset,val) ((((uint)(1 << (width))-1) << (offset)) & (val))
124
125
126#define BIT(n) ( (uint) 1 << (n) )
127#define BIT0 BIT(0)
128#define BIT1 BIT(1)
129#define BIT2 BIT(2)
130#define BIT3 BIT(3)
131#define BIT4 BIT(4)
132#define BIT5 BIT(5)
133#define BIT6 BIT(6)
134#define BIT7 BIT(7)
135#define BIT8 BIT(8)
136#define BIT9 BIT(9)
137#define BIT10 BIT(10)
138#define BIT11 BIT(11)
139#define BIT12 BIT(12)
140#define BIT13 BIT(13)
141#define BIT14 BIT(14)
142#define BIT15 BIT(15)
143#define BIT16 BIT(16)
144#define BIT17 BIT(17)
145#define BIT18 BIT(18)
146#define BIT19 BIT(19)
147#define BIT20 BIT(20)
148#define BIT21 BIT(21)
149#define BIT22 BIT(22)
150#define BIT23 BIT(23)
151#define BIT24 BIT(24)
152#define BIT25 BIT(25)
153#define BIT26 BIT(26)
154#define BIT27 BIT(27)
155#define BIT28 BIT(28)
156#define BIT29 BIT(29)
157#define BIT30 BIT(30)
158#define BIT31 BIT(31)
159
160#define MASK01 0x00000001
161#define MASK02 0x00000003
162#define MASK03 0x00000007
163#define MASK04 0x0000000f
164#define MASK05 0x0000001f
165#define MASK06 0x0000003f
166#define MASK07 0x0000007f
167#define MASK08 0x000000ff
168#define MASK09 0x000001ff
169#define MASK10 0x000003ff
170#define MASK11 0x000007ff
171#define MASK12 0x00000fff
172#define MASK14 0x00003fff
173#define MASK16 0x0000ffff
174#define MASK32 0xffffffff
175
176#define ALL32_0 0x00000000
177#define ALL32_1 0xffffffff
178
179#define RSHIFT(x, y) ( (x) >> (y) )
180#define LSHIFT(x, y) ( (x) << (y) )
181
182/***********************************************************************
183 * Add Static Assert
184 ***********************************************************************
185 */
186
187#define STATIC_ASSERT(e) \
188 do { char static_assert[(e)? 1:-1]; (void)static_assert[0]; } while (0)
189
190/***********************************************************************
191 *
192 * I/O Read, Write Macros
193 *
194 ***********************************************************************
195 */
196#ifdef ON_UNIX
197
198#include "dummy_io.h"
199#define IO_OUT(x,y) dummy_io_out((uint)&(x), (uint)(y))
200#define IO_IN(x,y) dummy_io_in((uint)&(x),(uint*)&(y))
201#define WAIT_READY(x)
202#define TWAIT_READY(x,t,r)
203#define IO_REG_OUT(x, y) dummy_io_out((uint)(x), (uint)(y))
204#define IO_REG_IN(x, y) dummy_io_in((uint)(x),(uint*)&(y))
205#define WAIT_REG_READY(x,y)
206#define TWAIT_REG_READY(x,y,t,r)
207#define TWAIT_REG_BUSY_END(x,y,t,r)
208#define IS_REG_READY(x,y) (1)
209#define IO_REG_SWAP_IN(x, y) { dummy_io_in((uint)(x), (uint*)&(y)); \
210 (y) = ((((uint)(y) & 0xFF000000) >> 24) | \
211 (((uint)(y) & 0x00FF0000) >> 8) | \
212 (((uint)(y) & 0x0000FF00) << 8) | \
213 (((uint)(y) & 0x000000FF) << 24)); }
214#define IO_REG_SWAP_OUT(x, y) { dummy_io_out((uint)(x), ((((uint)(y) & 0xFF000000) >> 24) | \
215 (((uint)(y) & 0x00FF0000) >> 8) | \
216 (((uint)(y) & 0x0000FF00) << 8) | \
217 (((uint)(y) & 0x000000FF) << 24))); }
218#define IO_REG16_OUT(x, y) dummy_io16_out((uint)(x), (ushort)(y))
219#define IO_REG16_IN(x, y) dummy_io16_in((uint)(x),(ushort*)&(y))
220#define DUMMY_IO_INIT_MAP(x, y) dummy_io_init_map((const io_value*)(x), (uint)(y))
221#define DUMMY_IO_MODIFY_MAP(x, y) dummy_io_modify_map((uint)(x), (uint)(y))
222#define DUMMY_IO_READ_MAP(x, y) dummy_io_read_map((uint)(x), (uint*)&(y));
223
224#elif defined (ISS_ALLEGRO)
225
226#include "dummy_io.h"
227#define IO_OUT(x,y) dummy_io_out((uint)&(x), (uint)(y))
228#define IO_IN(x,y) dummy_io_in((uint)&(x),(uint*)&(y))
229#define WAIT_READY(x)
230#define TWAIT_READY(x,t,r)
231#define IO_REG_OUT(x, y) dummy_io_out((uint)(x), (uint)(y))
232#define IO_REG_IN(x, y) dummy_io_in((uint)(x),(uint*)&(y))
233#define WAIT_REG_READY(x,y)
234#define TWAIT_REG_READY(x,y,t,r)
235#define TWAIT_REG_BUSY_END(x,y,t,r)
236#define IS_REG_READY(x,y) (1)
237#define IO_REG_SWAP_IN(x, y) { dummy_io_in((uint)(x), (uint*)&(y)); \
238 (y) = ((((uint)(y) & 0xFF000000) >> 24) | \
239 (((uint)(y) & 0x00FF0000) >> 8) | \
240 (((uint)(y) & 0x0000FF00) << 8) | \
241 (((uint)(y) & 0x000000FF) << 24)); }
242#define IO_REG_SWAP_OUT(x, y) { dummy_io_out((uint)(x), ((((uint)(y) & 0xFF000000) >> 24) | \
243 (((uint)(y) & 0x00FF0000) >> 8) | \
244 (((uint)(y) & 0x0000FF00) << 8) | \
245 (((uint)(y) & 0x000000FF) << 24))); }
246#define IO_REG16_OUT(x, y) dummy_io16_out((uint)(x), (ushort)(y))
247#define IO_REG16_IN(x, y) dummy_io16_in((uint)(x),(ushort*)&(y))
248#define DUMMY_IO_INIT_MAP(x, y) dummy_io_init_map((const io_value*)(x), (uint)(y))
249#define DUMMY_IO_MODIFY_MAP(x, y) dummy_io_modify_map((uint)(x), (uint)(y))
250#define DUMMY_IO_READ_MAP(x, y) dummy_io_read_map((uint)(x), (uint*)&(y));
251
252#else
253#ifdef allegro
254#include "allegro_regs.h"
255#endif /* allegro */
256
257#define IO_OUT(x,y) ( (x) = (y) ) /* OUT = write */
258#define IO_IN( x,y) ( (y) = (x) ) /* IN = read */
259#define WAIT_READY(x) { while(!(x)); }
260#define TWAIT_READY(x,t,r) { int _i_; (r) = FALSE; STATIC_ASSERT((t) < 20); \
261 for(_i_ = 0; _i_ < (t); _i_++){ if(x){(r) = TRUE; break;}};}
262#define IO_REG_OUT(x, y) ( *((volatile uint *)(x)) = (y) )
263#define IO_REG_IN(x, y) ( (y) = *((volatile uint *)(x)) )
264#define WAIT_REG_READY(x,y) { while(!(*((volatile uint *)(x))&(y))); }
265#define TWAIT_REG_READY(x,y,t,r) { int _i_; (r) = FALSE; STATIC_ASSERT((t) < 20); \
266 for(_i_ = 0; _i_ < (t); _i_++){ if(*((volatile uint *)(x))&(y)){(r) = TRUE; break;}};}
267#define TWAIT_REG_BUSY_END(x,y,t,r) { int _i_; (r) = FALSE; STATIC_ASSERT((t) < 20); \
268 for(_i_ = 0; _i_ < (t); _i_++){ if(!(*((volatile uint *)(x))&(y))){(r) = TRUE; break;}};}
269#define IS_REG_READY(x,y) (*((volatile uint *)(x))&(y))
270#define IO_REG_SWAP_IN(x, y) { __wsbw__(y,*((const volatile uint *)(x))); }
271#define IO_REG_SWAP_OUT(x, y) { __wsbw__(*((volatile uint *)(x)),(y)); }
272#define IO_REG16_OUT(x, y) ( *((volatile ushort *)(x)) = (y) )
273#define IO_REG16_IN(x, y) ( (y) = *((volatile ushort *)(x)) )
274#define DUMMY_IO_INIT_MAP(x, y)
275#define DUMMY_IO_MODIFY_MAP(x, y)
276#define DUMMY_IO_READ_MAP(x, y)
277
278#endif /* ON_UNIX */
279
280/***********************************************************************
281 *
282 * Address conversion
283 *
284 ***********************************************************************
285 */
286#define ADL2BYTE(x) ((x)<<3)
287#define BYTE2ADL(x) ((x)>>3)
288
289#endif /* COMMON_TYPES_H */
290