1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef PLATFORM_H 16 #define PLATFORM_H 17 18 #include <errno.h> 19 #include <stdarg.h> 20 #include <stdbool.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #define P_ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0])) 32 #define P_MIN(a, b) ((a) < (b) ? (a) : (b)) 33 #define P_MAX(a, b) ((a) > (b) ? (a) : (b)) 34 35 #ifndef offsetof 36 #define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) 37 #endif 38 39 #ifndef container_of 40 #define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member) *__mptr = (ptr); \ 41 (type *)( (char *)__mptr - offsetof(type,member) ); }) 42 #endif 43 44 typedef void *DevHandle; 45 46 uint32_t GetSysTime(void); 47 48 uint32_t SysTimeDiff(uint32_t start); 49 50 void DelayMs(uint32_t ms); 51 52 void DelayUs(uint32_t us); 53 54 /* platform log module */ 55 #define LOG_LEVEL_ERROR 0 56 #define LOG_LEVEL_WARN 1 57 #define LOG_LEVEL_INFO 2 58 #define LOG_LEVEL_DEBUG 3 59 60 /* log low level interfaces */ 61 #define LOG_LEVEL LOG_LEVEL_INFO 62 63 #if (LOG_LEVEL >= LOG_LEVEL_DEBUG) 64 #define LOG_D(fmt, ...) print_string("D %s: " fmt "\r\n", __func__, ##__VA_ARGS__) 65 #define LOG_DA(n, a, l) print_array(n, a, l) 66 #else 67 #define LOG_D(fmt, ...) 68 #define LOG_DA(n, a, l) 69 #endif 70 71 #if (LOG_LEVEL >= LOG_LEVEL_INFO) 72 #define LOG_I(fmt, ...) print_string("I %s: " fmt "\r\n", __func__, ##__VA_ARGS__) 73 #define LOG_IA(n, a, l) print_array(n, a, l) 74 #else 75 #define LOG_I(fmt, ...) 76 #define LOG_IA(n, a, l) 77 #endif 78 79 #if (LOG_LEVEL >= LOG_LEVEL_WARN) 80 #define LOG_W(fmt, ...) print_string("W %s: " fmt "\r\n", __func__, ##__VA_ARGS__) 81 #define LOG_WA(n, a, l) print_array(n, a, l) 82 #else 83 #define LOG_W(fmt, ...) 84 #define LOG_WA(n, a, l) 85 #endif 86 87 #if (LOG_LEVEL >= LOG_LEVEL_ERROR) 88 #define LOG_E(fmt, ...) print_string("E %s: " fmt "\r\n", __func__, ##__VA_ARGS__) 89 #define LOG_EA(n, a, l) print_array(n, a, l) 90 #else 91 #define LOG_E(fmt, ...) 92 #define LOG_EA(n, a, l) 93 #endif 94 95 #if (LOG_LEVEL >= LOG_LEVEL_DEBUG) 96 #define LOG_ENTER() print_string("D %s:%d %s enter\r\n", __FILE__, __LINE__, __func__) 97 #define LOG_LEAVE() print_string("D %s:%d %s leave\r\n", __FILE__, __LINE__, __func__) 98 #else 99 #define LOG_ENTER() 100 #define LOG_LEAVE() 101 #endif 102 103 #define RETURN_VAL_IF_FAIL(cond, val) \ 104 do { \ 105 if (!(cond)) { \ 106 LOG_E("'%s' failed.", #cond); \ 107 return val; \ 108 } \ 109 } while (0) 110 111 #define RETURN_IF_FAIL(cond) \ 112 do { \ 113 if (!(cond)) { \ 114 LOG_E("'%s' failed.", #cond); \ 115 return; \ 116 } \ 117 } while (0) 118 119 #define DBG_ASSERT(cond) \ 120 do { \ 121 if (!(cond)) { \ 122 print_string("E/%s:%d '%s' assert failed.\r\n", __FILE__, __LINE__, #cond); \ 123 while (1) \ 124 ; \ 125 } \ 126 } while (0) 127 128 void print_string(const char *fmt, ...); 129 130 void print_array(const char *name, const void *array, uint16_t len); 131 132 #ifdef __cplusplus 133 } 134 #endif 135 136 #endif 137