1 /****************************************************************************** 2 * 3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. 4 * 5 ******************************************************************************/ 6 #ifndef __BASIC_TYPES_H__ 7 #define __BASIC_TYPES_H__ 8 9 //#define PLATFORM_FREERTOS 10 #include <stdint.h> 11 #include <stddef.h> /* for size_t */ 12 13 #define PLATFORM_LITTLE_ENDIAN 0 14 #define PLATFORM_BIG_ENDIAN 1 15 16 #define SYSTEM_ENDIAN PLATFORM_LITTLE_ENDIAN 17 18 #define SUCCESS 0 19 #define FAIL (-1) 20 21 #undef _SUCCESS 22 #define _SUCCESS 1 23 24 #undef _FAIL 25 #define _FAIL 0 26 27 #ifndef FALSE 28 #define FALSE 0 29 #endif 30 31 #ifndef TRUE 32 #define TRUE (!FALSE) 33 #endif 34 35 #define _TRUE TRUE 36 #define _FALSE FALSE 37 38 #ifndef NULL 39 #define NULL 0 40 #endif 41 42 #ifdef __GNUC__ 43 #define __weak __attribute__((weak)) 44 #ifndef likely 45 #define likely(x) __builtin_expect ((x), 1) 46 #endif 47 #ifndef unlikely 48 #define unlikely(x) __builtin_expect ((x), 0) 49 #endif 50 51 #elif defined(__ICCARM__) 52 #define likely(x) (x) 53 #define unlikely(x) (x) 54 55 #endif 56 57 typedef unsigned int uint; 58 typedef signed int sint; 59 60 #ifdef __ICCARM__ 61 typedef signed long long __int64_t; 62 typedef unsigned long long __uint64_t; 63 #endif 64 65 #define s8 int8_t 66 #define u8 uint8_t 67 #define s16 int16_t 68 #define u16 uint16_t 69 #define s32 int32_t 70 #define u32 uint32_t 71 #define s64 int64_t 72 #define u64 uint64_t 73 74 #ifdef CONFIG_MBED_ENABLED 75 #ifndef BOOL 76 typedef unsigned char BOOL; 77 #endif 78 #ifndef bool 79 typedef unsigned char bool; 80 #endif 81 #else 82 #ifndef BOOL 83 typedef unsigned char BOOL; 84 #endif 85 #ifdef __cplusplus 86 #else 87 #ifndef bool 88 typedef unsigned char bool; 89 #endif 90 #endif 91 #endif 92 93 #define UCHAR uint8_t 94 #define USHORT uint16_t 95 #define UINT uint32_t 96 #define ULONG uint32_t 97 98 //typedef struct { volatile int counter; } atomic_t; 99 100 typedef enum _RTK_STATUS_ { 101 _EXIT_SUCCESS = 0, 102 _EXIT_FAILURE = 1 103 }RTK_STATUS, *PRTK_STATUS; 104 105 #define IN 106 #define OUT 107 #define VOID void 108 #define INOUT 109 #define NDIS_OID uint 110 #define NDIS_STATUS uint 111 112 #ifndef PVOID 113 typedef void * PVOID; 114 #endif 115 116 typedef u32 dma_addr_t; 117 118 typedef void (*proc_t)(void*); 119 120 typedef unsigned int __kernel_size_t; 121 typedef int __kernel_ssize_t; 122 123 typedef __kernel_size_t SIZE_T; 124 typedef __kernel_ssize_t SSIZE_T; 125 #define FIELD_OFFSET(s,field) ((SSIZE_T)&((s*)(0))->field) 126 127 #define MEM_ALIGNMENT_OFFSET (sizeof (SIZE_T)) 128 #define MEM_ALIGNMENT_PADDING (sizeof(SIZE_T) - 1) 129 130 #define SIZE_PTR SIZE_T 131 #define SSIZE_PTR SSIZE_T 132 133 #ifndef ON 134 #define ON 1 135 #endif 136 137 #ifndef OFF 138 #define OFF 0 139 #endif 140 141 #ifndef ENABLE 142 #define ENABLE 1 143 #endif 144 145 #ifndef DISABLE 146 #define DISABLE 0 147 #endif 148 149 150 #define BIT0 0x0001 151 #define BIT1 0x0002 152 #define BIT2 0x0004 153 #define BIT3 0x0008 154 #define BIT4 0x0010 155 #define BIT5 0x0020 156 #define BIT6 0x0040 157 #define BIT7 0x0080 158 #define BIT8 0x0100 159 #define BIT9 0x0200 160 #define BIT10 0x0400 161 #define BIT11 0x0800 162 #define BIT12 0x1000 163 #define BIT13 0x2000 164 #define BIT14 0x4000 165 #define BIT15 0x8000 166 #define BIT16 0x00010000 167 #define BIT17 0x00020000 168 #define BIT18 0x00040000 169 #define BIT19 0x00080000 170 #define BIT20 0x00100000 171 #define BIT21 0x00200000 172 #define BIT22 0x00400000 173 #define BIT23 0x00800000 174 #define BIT24 0x01000000 175 #define BIT25 0x02000000 176 #define BIT26 0x04000000 177 #define BIT27 0x08000000 178 #define BIT28 0x10000000 179 #define BIT29 0x20000000 180 #define BIT30 0x40000000 181 #define BIT31 0x80000000 182 183 #define BIT_(__n) (1<<(__n)) 184 185 #ifndef BIT 186 #define BIT(__n) (1<<(__n)) 187 #endif 188 189 #if defined (__ICCARM__) 190 #define STRINGIFY(s) #s 191 #define SECTION(_name) _Pragma( STRINGIFY(location=_name)) 192 #define ALIGNMTO(_bound) _Pragma( STRINGIFY(data_alignment=_bound)) 193 #define _PACKED_ __packed 194 #define _LONG_CALL_ 195 #define _WEAK __weak 196 #define _OPTIMIZE_NONE_ _Pragma( STRINGIFY(optimize=none)) 197 #define UNUSED_WARN_DIS 198 #else 199 #define SECTION(_name) __attribute__ ((__section__(_name))) 200 #define ALIGNMTO(_bound) __attribute__ ((aligned (_bound))) 201 #define _PACKED_ __attribute__ ((packed)) 202 #define _LONG_CALL_ __attribute__ ((long_call)) 203 #define _WEAK __attribute__ ((weak)) 204 #define _OPTIMIZE_NONE_ __attribute__ ((optimize("O0"))) 205 #define UNUSED_WARN_DIS __attribute__((unused)) 206 #endif 207 208 209 210 //port from fw by thomas 211 // TODO: Belows are Sync from SD7-Driver. It is necessary to check correctness 212 213 #define SWAP32(x) ((u32)( \ 214 (((u32)(x) & (u32)0x000000ff) << 24) | \ 215 (((u32)(x) & (u32)0x0000ff00) << 8) | \ 216 (((u32)(x) & (u32)0x00ff0000) >> 8) | \ 217 (((u32)(x) & (u32)0xff000000) >> 24))) 218 219 #define WAP16(x) ((u16)( \ 220 (((u16)(x) & (u16)0x00ff) << 8) | \ 221 (((u16)(x) & (u16)0xff00) >> 8))) 222 223 #if SYSTEM_ENDIAN == PLATFORM_LITTLE_ENDIAN 224 #ifndef rtk_le16_to_cpu 225 #define rtk_cpu_to_le32(x) ((u32)(x)) 226 #define rtk_le32_to_cpu(x) ((u32)(x)) 227 #define rtk_cpu_to_le16(x) ((u16)(x)) 228 #define rtk_le16_to_cpu(x) ((u16)(x)) 229 #define rtk_cpu_to_be32(x) SWAP32((x)) 230 #define rtk_be32_to_cpu(x) SWAP32((x)) 231 #define rtk_cpu_to_be16(x) WAP16((x)) 232 #define rtk_be16_to_cpu(x) WAP16((x)) 233 #endif 234 235 #elif SYSTEM_ENDIAN == PLATFORM_BIG_ENDIAN 236 #ifndef rtk_le16_to_cpu 237 #define rtk_cpu_to_le32(x) SWAP32((x)) 238 #define rtk_le32_to_cpu(x) SWAP32((x)) 239 #define rtk_cpu_to_le16(x) WAP16((x)) 240 #define rtk_le16_to_cpu(x) WAP16((x)) 241 #define rtk_cpu_to_be32(x) ((__u32)(x)) 242 #define rtk_be32_to_cpu(x) ((__u32)(x)) 243 #define rtk_cpu_to_be16(x) ((__u16)(x)) 244 #define rtk_be16_to_cpu(x) ((__u16)(x)) 245 #endif 246 #endif 247 248 249 /* 250 * Call endian free function when 251 * 1. Read/write packet content. 252 * 2. Before write integer to IO. 253 * 3. After read integer from IO. 254 */ 255 256 // 257 // Byte Swapping routine. 258 // 259 #define EF1Byte (u8) 260 #define EF2Byte le16_to_cpu 261 #define EF4Byte le32_to_cpu 262 263 // 264 // Read LE format data from memory 265 // 266 #define ReadEF1Byte(_ptr) EF1Byte(*((u8 *)(_ptr))) 267 #define ReadEF2Byte(_ptr) EF2Byte(*((u16 *)(_ptr))) 268 #define ReadEF4Byte(_ptr) EF4Byte(*((u32 *)(_ptr))) 269 270 // 271 // Write LE data to memory 272 // 273 #define WriteEF1Byte(_ptr, _val) (*((u8 *)(_ptr)))=EF1Byte(_val) 274 #define WriteEF2Byte(_ptr, _val) (*((u16 *)(_ptr)))=EF2Byte(_val) 275 #define WriteEF4Byte(_ptr, _val) (*((u32 *)(_ptr)))=EF4Byte(_val) 276 277 // 278 // Example: 279 // BIT_LEN_MASK_32(0) => 0x00000000 280 // BIT_LEN_MASK_32(1) => 0x00000001 281 // BIT_LEN_MASK_32(2) => 0x00000003 282 // BIT_LEN_MASK_32(32) => 0xFFFFFFFF 283 // 284 #define BIT_LEN_MASK_32(__BitLen) \ 285 (0xFFFFFFFF >> (32 - (__BitLen))) 286 // 287 // Example: 288 // BIT_OFFSET_LEN_MASK_32(0, 2) => 0x00000003 289 // BIT_OFFSET_LEN_MASK_32(16, 2) => 0x00030000 290 // 291 #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) \ 292 (BIT_LEN_MASK_32(__BitLen) << (__BitOffset)) 293 294 // 295 // Description: 296 // Return 4-byte value in host byte ordering from 297 // 4-byte pointer in litten-endian system. 298 // 299 #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) \ 300 (EF4Byte(*((u32 *)(__pStart)))) 301 302 // 303 // Description: 304 // Translate subfield (continuous bits in little-endian) of 4-byte value in litten byte to 305 // 4-byte value in host byte ordering. 306 // 307 #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \ 308 ( \ 309 ( LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset) ) \ 310 & \ 311 BIT_LEN_MASK_32(__BitLen) \ 312 ) 313 314 // 315 // Description: 316 // Mask subfield (continuous bits in little-endian) of 4-byte value in litten byte oredering 317 // and return the result in 4-byte value in host byte ordering. 318 // 319 #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \ 320 ( \ 321 LE_P4BYTE_TO_HOST_4BYTE(__pStart) \ 322 & \ 323 ( ~ BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) ) \ 324 ) 325 326 // 327 // Description: 328 // Set subfield of little-endian 4-byte value to specified value. 329 // 330 #define SET_BITS_TO_LE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \ 331 *((u32 *)(__pStart)) = \ 332 EF4Byte( \ 333 LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \ 334 | \ 335 ( (((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset) ) \ 336 ); 337 338 339 #define BIT_LEN_MASK_16(__BitLen) \ 340 (0xFFFF >> (16 - (__BitLen))) 341 342 #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) \ 343 (BIT_LEN_MASK_16(__BitLen) << (__BitOffset)) 344 345 #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) \ 346 (EF2Byte(*((u16 *)(__pStart)))) 347 348 #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \ 349 ( \ 350 ( LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset) ) \ 351 & \ 352 BIT_LEN_MASK_16(__BitLen) \ 353 ) 354 355 #define LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \ 356 ( \ 357 LE_P2BYTE_TO_HOST_2BYTE(__pStart) \ 358 & \ 359 ( ~ BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) ) \ 360 ) 361 362 #define SET_BITS_TO_LE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \ 363 *((u16 *)(__pStart)) = \ 364 EF2Byte( \ 365 LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \ 366 | \ 367 ( (((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << (__BitOffset) ) \ 368 ); 369 370 #define BIT_LEN_MASK_8(__BitLen) \ 371 (0xFF >> (8 - (__BitLen))) 372 373 #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) \ 374 (BIT_LEN_MASK_8(__BitLen) << (__BitOffset)) 375 376 #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) \ 377 (EF1Byte(*((u8 *)(__pStart)))) 378 379 #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \ 380 ( \ 381 ( LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset) ) \ 382 & \ 383 BIT_LEN_MASK_8(__BitLen) \ 384 ) 385 386 #define LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \ 387 ( \ 388 LE_P1BYTE_TO_HOST_1BYTE(__pStart) \ 389 & \ 390 ( ~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) ) \ 391 ) 392 393 #define SET_BITS_TO_LE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \ 394 *((u8 *)(__pStart)) = \ 395 EF1Byte( \ 396 LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \ 397 | \ 398 ( (((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << (__BitOffset) ) \ 399 ); 400 401 //pclint 402 #define LE_BITS_CLEARED_TO_1BYTE_8BIT(__pStart, __BitOffset, __BitLen) \ 403 ( \ 404 LE_P1BYTE_TO_HOST_1BYTE(__pStart) \ 405 ) 406 407 //pclint 408 #define SET_BITS_TO_LE_1BYTE_8BIT(__pStart, __BitOffset, __BitLen, __Value) \ 409 { \ 410 *((pu1Byte)(__pStart)) = \ 411 EF1Byte( \ 412 LE_BITS_CLEARED_TO_1BYTE_8BIT(__pStart, __BitOffset, __BitLen) \ 413 | \ 414 ((u1Byte)__Value) \ 415 ); \ 416 } 417 418 // Get the N-bytes aligment offset from the current length 419 #define N_BYTE_ALIGMENT(__Value, __Aligment) ((__Aligment == 1) ? (__Value) : (((__Value + __Aligment - 1) / __Aligment) * __Aligment)) 420 421 typedef unsigned char BOOLEAN,*PBOOLEAN; 422 423 #define TEST_FLAG(__Flag,__testFlag) (((__Flag) & (__testFlag)) != 0) 424 #define SET_FLAG(__Flag, __setFlag) ((__Flag) |= __setFlag) 425 #define CLEAR_FLAG(__Flag, __clearFlag) ((__Flag) &= ~(__clearFlag)) 426 #define CLEAR_FLAGS(__Flag) ((__Flag) = 0) 427 #define TEST_FLAGS(__Flag, __testFlags) (((__Flag) & (__testFlags)) == (__testFlags)) 428 429 /* Define compilor specific symbol */ 430 // 431 // inline function 432 // 433 434 #if defined ( __ICCARM__ ) 435 #define __inline__ inline 436 #define __inline inline 437 #define __inline_definition //In dialect C99, inline means that a function's definition is provided 438 //only for inlining, and that there is another definition 439 //(without inline) somewhere else in the program. 440 //That means that this program is incomplete, because if 441 //add isn't inlined (for example, when compiling without optimization), 442 //then main will have an unresolved reference to that other definition. 443 444 // Do not inline function is the function body is defined .c file and this 445 // function will be called somewhere else, otherwise there is compile error 446 #ifndef __STATIC_INLINE 447 #define __STATIC_INLINE _Pragma("inline=forced") 448 #endif 449 #elif defined ( __CC_ARM ) 450 #define __inline__ __inline //__linine__ is not supported in keil compilor, use __inline instead 451 #define inline __inline 452 #define __inline_definition // for dialect C99 453 #elif defined ( __GNUC__ ) 454 #define __inline__ inline 455 #define __inline inline 456 #define __inline_definition inline 457 #ifndef __STATIC_INLINE 458 #define __STATIC_INLINE static inline __attribute__((always_inline)) 459 #endif 460 #endif 461 462 // 463 // pack 464 // 465 466 #if defined (__ICCARM__) 467 468 #define RTW_PACK_STRUCT_BEGIN _Pragma( STRINGIFY(pack(1))) 469 #define RTW_PACK_STRUCT_STRUCT 470 #define RTW_PACK_STRUCT_END _Pragma( STRINGIFY(pack())) 471 //#define RTW_PACK_STRUCT_USE_INCLUDES 472 473 #elif defined (__CC_ARM) 474 475 #define RTW_PACK_STRUCT_BEGIN __packed 476 #define RTW_PACK_STRUCT_STRUCT 477 #define RTW_PACK_STRUCT_END 478 479 #elif defined (__GNUC__) 480 481 #define RTW_PACK_STRUCT_BEGIN 482 #define RTW_PACK_STRUCT_STRUCT __attribute__ ((__packed__)) 483 #define RTW_PACK_STRUCT_END 484 485 #elif defined(PLATFORM_WINDOWS) 486 487 #define RTW_PACK_STRUCT_BEGIN 488 #define RTW_PACK_STRUCT_STRUCT 489 #define RTW_PACK_STRUCT_END 490 #define RTW_PACK_STRUCT_USE_INCLUDES 491 #endif 492 493 // for standard library 494 #ifdef __ICCARM__ 495 #define __extension__ /* Ignore */ 496 #define __restrict /* Ignore */ 497 #endif 498 499 #if defined (__ARM_FEATURE_CMSE) 500 #ifdef __ICCARM__ 501 #define NS_ENTRY __cmse_nonsecure_entry 502 #else 503 #define NS_ENTRY __attribute__((cmse_nonsecure_entry)) 504 #endif 505 #if defined (ARM_CORE_CM4) 506 #ifdef __ICCARM__ 507 typedef __cmse_nonsecure_call void nsfunc(void); 508 #else 509 typedef void __attribute__((cmse_nonsecure_call)) nsfunc(void); 510 #endif 511 #endif 512 #endif 513 514 typedef struct { 515 VOID (*RamStartFun) (VOID); 516 VOID (*RamWakeupFun) (VOID); 517 u32 VectorNS; 518 }RAM_START_FUNCTION, *PRAM_START_FUNCTION; 519 520 typedef struct _RAM_FUNCTION_START_TABLE_ { 521 VOID (*RamStartFun) (VOID); 522 VOID (*RamWakeupFun) (VOID); 523 VOID (*RamPatchFun0) (VOID); 524 VOID (*RamPatchFun1) (VOID); 525 VOID (*RamPatchFun2) (VOID); 526 VOID (*FlashStartFun) (VOID); 527 u32 Img1ValidCode; 528 u32 Img1Rsvd; 529 }RAM_FUNCTION_START_TABLE, *PRAM_FUNCTION_START_TABLE; 530 531 typedef struct _DSLP_RETENTION_FUNC_TABLE_ { 532 VOID (*DSLPPatchFun0) (VOID); 533 u32 PatchLen; 534 }DSLP_RETENTION_FUNC_TABLE, *PDSLP_RETENTION_FUNC_TABLE; 535 536 #endif// __BASIC_TYPES_H__ 537