PDK API Guide for J721E
enet_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Texas Instruments Incorporated 2020
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (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 
40 #ifndef ENET_UTILS_H_
41 #define ENET_UTILS_H_
42 
43 /* ========================================================================== */
44 /* Include Files */
45 /* ========================================================================== */
46 
47 #include <stdint.h>
48 #include <stdbool.h>
49 #include <stdarg.h>
50 #include <stddef.h>
51 #include <string.h>
52 #include <ti/drv/enet/enet_cfg.h>
57 
58 #if defined(__KLOCWORK__) || defined(__cplusplus)
59 #include <stdlib.h>
60 #endif
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 /* ========================================================================== */
67 /* Macros */
68 /* ========================================================================== */
69 
73 #define container_of(ptr, type, member) \
74  ({ \
75  const typeof(((type *)0)->member) * __mptr = (ptr); \
76  (type *)((char *)__mptr - offsetof(type, member)); \
77  })
78 
82 #define ENET_UNUSED(x) (x = x)
83 
87 #define ENET_MK_ONES(c_ebit, c_sbit) (((1U << (((c_ebit) - (c_sbit)) + 1U)) - 1U) << (c_sbit))
88 
90 #define ENET_UTILS_ALIGN(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
91 
93 #define ENET_UTILS_IS_ALIGNED(addr, alignSz) (((uintptr_t)addr & ((alignSz) - 1U)) == 0U)
94 
96 #define ENET_UTILS_ARRAY_COPY(dst, src) \
97  do \
98  { \
99  /* dst argument of macro should be array and not pointer.*/ \
100  ENET_UTILS_COMPILETIME_ASSERT(sizeof(dst) != sizeof(uintptr_t)); \
101  memcpy(dst, src, (ENET_ARRAYSIZE(dst) * sizeof(dst[0]))); \
102  } while (0)
103 
107 #if ENET_CFG_IS_ON(ASSERT)
108 #if defined(__KLOCWORK__) || defined(__cplusplus)
109 #define Enet_assert(cond, ...) do { if (!(cond)) abort(); } while (0)
110 #else /* !defined(__KLOCWORK__) && !defined(__cplusplus) */
111 #define Enet_assert(cond, ...) \
112  do { \
113  ENETTRACE_ERR_IF(!(bool)(cond), __VA_ARGS__); \
114  EnetUtils_assertLocal((bool)(cond), \
115  (const char *)# cond, \
116  (const char *)__FILE__, \
117  (int32_t)__LINE__); \
118  } while (0)
119 #endif /* defined(__KLOCWORK__) || defined(__cplusplus) */
120 #else /* !ENET_CFG_IS_ON(ASSERT) */
121 #define Enet_assert(cond, ...) (void)(cond)
122 #endif /* ENET_CFG_IS_ON(ASSERT) */
123 
127 #if ENET_CFG_IS_ON(ASSERT)
128 #if defined(__KLOCWORK__) || defined(__cplusplus)
129 #define Enet_devAssert(cond, ...) do { if (!(cond)) abort(); } while (0)
130 #else /* !defined(__KLOCWORK__) && !defined(__cplusplus) */
131 #define Enet_devAssert(cond, ...) \
132  do { \
133  ENETTRACE_ERR_IF(!(bool)(cond), __VA_ARGS__); \
134  EnetUtils_assertLocal((bool)(cond), \
135  (const char *)# cond, \
136  (const char *)__FILE__, \
137  (int32_t)__LINE__); \
138  } while (0)
139 #endif /* defined(__KLOCWORK__) || defined(__cplusplus) */
140 #else /* !ENET_CFG_IS_ON(ASSERT) || !ENET_CFG_IS_ON(DEV_ERROR) */
141 #define Enet_devAssert(cond, fmt, ...) (void)(cond)
142 #endif /* ENET_CFG_IS_ON(ASSERT) && ENET_CFG_IS_ON(DEV_ERROR) */
143 
147 #define ENET_UTILS_COMPILETIME_ASSERT(cond) \
148  do { \
149  typedef char ErrorCheck[((cond) == true) ? 1 : -1]; \
150  ErrorCheck a = {0}; \
151  a[0U] = a[0U]; \
152  } while (0)
153 
154 /* ========================================================================== */
155 /* Structures and Enums */
156 /* ========================================================================== */
157 
165 typedef void (*Enet_Print)(const char *fmt, ...);
166 
178 typedef uint64_t (*Enet_VirtToPhys)(const void *virtAddr, void *appData);
179 
191 typedef void *(*Enet_PhysToVirt)(uint64_t phyAddr, void *appData);
192 
196 typedef struct EnetUtils_Cfg_s
197 {
201 
206 
211 } EnetUtils_Cfg;
212 
213 /* ========================================================================== */
214 /* Global Variables Declarations */
215 /* ========================================================================== */
216 
217 /* None */
218 
219 /* ========================================================================== */
220 /* Function Declarations */
221 /* ========================================================================== */
222 
231 void EnetUtils_init(const EnetUtils_Cfg *cfg);
232 
236 void EnetUtils_deinit(void);
237 
245 void EnetUtils_printf(const char *fmt,
246  ...);
247 
256 void EnetUtils_vprintf(const char *fmt,
257  va_list args);
258 
267 uint32_t EnetUtils_min(uint32_t num1,
268  uint32_t num2);
269 
278 uint32_t EnetUtils_max(uint32_t num1,
279  uint32_t num2);
280 
287 void EnetUtils_delay(uint64_t delayInNsecs);
288 
297 uint64_t EnetUtils_virtToPhys(const void *virtAddr,
298  void *appData);
299 
308 void *EnetUtils_physToVirt(uint64_t physAddr,
309  void *appData);
310 
311 #if ENET_CFG_IS_ON(ASSERT) && !defined(__KLOCWORK__)
312 
320 static inline void EnetUtils_assertLocal(bool condition,
321  const char *str,
322  const char *fileName,
323  int32_t lineNum);
324 #endif
325 
337 
346 static inline void EnetUtils_copyMacAddr(uint8_t *dst,
347  const uint8_t *src);
348 
359 static inline bool EnetUtils_cmpMacAddr(const uint8_t *addr1,
360  const uint8_t *addr2);
361 
369 static inline void EnetUtils_clearMacAddr(uint8_t *addr);
370 
380 static inline bool EnetUtils_isMcastAddr(const uint8_t *addr);
381 
382 /* ========================================================================== */
383 /* Deprecated Function Declarations */
384 /* ========================================================================== */
385 
386 /* None */
387 
388 /* ========================================================================== */
389 /* Static Function Definitions */
390 /* ========================================================================== */
391 
392 #if ENET_CFG_IS_ON(ASSERT) && !defined(__KLOCWORK__)
393 static inline void EnetUtils_assertLocal(bool condition,
394  const char *str,
395  const char *fileName,
396  int32_t lineNum)
397 {
398  volatile static bool gEnetAssertWaitInLoop = true;
399 
400  if (!condition)
401  {
402  EnetUtils_printf("Assertion @ Line: %d in %s: %s\n", lineNum, fileName, str);
403  while (gEnetAssertWaitInLoop)
404  {
405  /* Do nothing */
406  }
407  }
408 }
409 #endif
410 
411 static inline void EnetUtils_copyMacAddr(uint8_t *dst,
412  const uint8_t *src)
413 {
414  memcpy(dst, src, ENET_MAC_ADDR_LEN);
415 }
416 
417 static inline bool EnetUtils_cmpMacAddr(const uint8_t *addr1,
418  const uint8_t *addr2)
419 {
420  return (memcmp(addr1, addr2, ENET_MAC_ADDR_LEN) == 0U);
421 }
422 
423 static inline void EnetUtils_clearMacAddr(uint8_t *addr)
424 {
425  memset(addr, 0, ENET_MAC_ADDR_LEN);
426 }
427 
428 static inline bool EnetUtils_isMcastAddr(const uint8_t *addr)
429 {
430  return ((addr[0U] & 1U) == 1U);
431 }
432 
433 #ifdef __cplusplus
434 }
435 #endif
436 
437 #endif /* ENET_UTILS_H_ */
uint64_t EnetUtils_virtToPhys(const void *virtAddr, void *appData)
Convert a virtual address to physical address.
MAC port interface.
Definition: enet_mod_macport.h:273
void EnetUtils_printf(const char *fmt,...)
Print function.
This file contains the basic types using across the Enet driver.
void EnetUtils_init(const EnetUtils_Cfg *cfg)
Initialize utils module.
Enet_VirtToPhys virtToPhys
Definition: enet_utils.h:205
void(* Enet_Print)(const char *fmt,...)
Info/debug print function prototype.
Definition: enet_utils.h:165
This file contains the type definitions and helper macros for the Enet MAC port module interface.
#define ENET_MAC_ADDR_LEN
MAC address length in bytes/octets.
Definition: enet_types.h:153
uint64_t(* Enet_VirtToPhys)(const void *virtAddr, void *appData)
Virtual-to-physical address translation callback function.
Definition: enet_utils.h:178
uint64_t addr
Definition: csl_udmap_tr.h:214
Enet utils parameters.
Definition: enet_utils.h:196
static void EnetUtils_copyMacAddr(uint8_t *dst, const uint8_t *src)
Copy MAC address.
Definition: enet_utils.h:411
void EnetUtils_vprintf(const char *fmt, va_list args)
Print function for va_list.
static void EnetUtils_clearMacAddr(uint8_t *addr)
Clear MAC address.
Definition: enet_utils.h:423
void * EnetUtils_physToVirt(uint64_t physAddr, void *appData)
Convert a physical address to virtual address.
uint32_t EnetUtils_min(uint32_t num1, uint32_t num2)
Returns minimum of two numbers.
void EnetUtils_delay(uint64_t delayInNsecs)
Busy loop for a given amount of delay in nanoseconds.
static bool EnetUtils_cmpMacAddr(const uint8_t *addr1, const uint8_t *addr2)
Compare two MAC address.
Definition: enet_utils.h:417
static bool EnetUtils_isMcastAddr(const uint8_t *addr)
Check if address is multicast.
Definition: enet_utils.h:428
EnetPhy_Mii EnetUtils_macToPhyMii(const EnetMacPort_Interface *macMii)
Convert MAC port MII to PHY MII types.
char str[32]
Definition: tisci_core.h:442
Enet_Print print
Definition: enet_utils.h:200
void *(* Enet_PhysToVirt)(uint64_t phyAddr, void *appData)
Physical-to-virtual address translation callback function.
Definition: enet_utils.h:191
Enet_PhysToVirt physToVirt
Definition: enet_utils.h:210
This file contains private type definitions and helper macros for the Enet trace interface.
void EnetUtils_deinit(void)
De-initialize utils module.
EnetPhy_Mii
MAC Media-Independent Interface (MII).
Definition: enetphy.h:206
This file contains the type definitions and helper macros for the Ethernet PHY interface.
uint32_t EnetUtils_max(uint32_t num1, uint32_t num2)
Returns minimum of two numbers.
This file contains the Enet configuration parameters.