AM64x MCU+ SDK  08.02.00
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 <enet_cfg.h>
55 #include <priv/core/enet_trace_priv.h>
56 #include <include/phy/enetphy.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, ...) (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 
286 void EnetUtils_delay(uint32_t delayVal);
287 
296 uint64_t EnetUtils_virtToPhys(const void *virtAddr,
297  void *appData);
298 
307 void *EnetUtils_physToVirt(uint64_t physAddr,
308  void *appData);
309 
310 #if ENET_CFG_IS_ON(ASSERT) && !defined(__KLOCWORK__)
311 
319 static inline void EnetUtils_assertLocal(bool condition,
320  const char *str,
321  const char *fileName,
322  int32_t lineNum);
323 #endif
324 
336 
345 static inline void EnetUtils_copyMacAddr(uint8_t *dst,
346  const uint8_t *src);
347 
358 static inline bool EnetUtils_cmpMacAddr(const uint8_t *addr1,
359  const uint8_t *addr2);
360 
368 static inline void EnetUtils_clearMacAddr(uint8_t *addr);
369 
379 static inline bool EnetUtils_isMcastAddr(const uint8_t *addr);
380 
381 /* ========================================================================== */
382 /* Deprecated Function Declarations */
383 /* ========================================================================== */
384 
385 /* None */
386 
387 /* ========================================================================== */
388 /* Static Function Definitions */
389 /* ========================================================================== */
390 
391 #if ENET_CFG_IS_ON(ASSERT) && !defined(__KLOCWORK__)
392 static inline void EnetUtils_assertLocal(bool condition,
393  const char *str,
394  const char *fileName,
395  int32_t lineNum)
396 {
397  volatile static bool gEnetAssertWaitInLoop = true;
398 
399  if (!condition)
400  {
401  EnetUtils_printf("Assertion @ Line: %d in %s: %s\n", lineNum, fileName, str);
402  while (gEnetAssertWaitInLoop)
403  {
404  /* Do nothing */
405  }
406  }
407 }
408 #endif
409 
410 static inline void EnetUtils_copyMacAddr(uint8_t *dst,
411  const uint8_t *src)
412 {
413  memcpy(dst, src, ENET_MAC_ADDR_LEN);
414 }
415 
416 static inline bool EnetUtils_cmpMacAddr(const uint8_t *addr1,
417  const uint8_t *addr2)
418 {
419  return (memcmp(addr1, addr2, ENET_MAC_ADDR_LEN) == 0U);
420 }
421 
422 static inline void EnetUtils_clearMacAddr(uint8_t *addr)
423 {
424  memset(addr, 0, ENET_MAC_ADDR_LEN);
425 }
426 
427 static inline bool EnetUtils_isMcastAddr(const uint8_t *addr)
428 {
429  return ((addr[0U] & 1U) == 1U);
430 }
431 
432 #ifdef __cplusplus
433 }
434 #endif
435 
436 #endif /* ENET_UTILS_H_ */
Enet_PhysToVirt
void *(* Enet_PhysToVirt)(uint64_t phyAddr, void *appData)
Physical-to-virtual address translation callback function.
Definition: enet_utils.h:191
EnetUtils_printf
void EnetUtils_printf(const char *fmt,...)
Print function.
EnetUtils_Cfg::virtToPhys
Enet_VirtToPhys virtToPhys
Definition: enet_utils.h:205
enet_mod_macport.h
This file contains the type definitions and helper macros for the Enet MAC port module interface.
EnetUtils_max
uint32_t EnetUtils_max(uint32_t num1, uint32_t num2)
Returns minimum of two numbers.
enet_types.h
This file contains the basic types using across the Enet driver.
EnetUtils_deinit
void EnetUtils_deinit(void)
De-initialize utils module.
EnetPhy_Mii
EnetPhy_Mii
MAC Media-Independent Interface (MII).
Definition: enetphy.h:206
EnetUtils_delay
void EnetUtils_delay(uint32_t delayVal)
Busy loop for a given amount of cycles.
addr
uint64_t addr
Definition: csl_udmap_tr.h:3
Enet_Print
void(* Enet_Print)(const char *fmt,...)
Info/debug print function prototype.
Definition: enet_utils.h:165
str
char str[32]
Definition: tisci_core.h:1
EnetUtils_physToVirt
void * EnetUtils_physToVirt(uint64_t physAddr, void *appData)
Convert a physical address to virtual address.
EnetUtils_Cfg
Enet utils parameters.
Definition: enet_utils.h:197
enetphy.h
This file contains the type definitions and helper macros for the Ethernet PHY interface.
EnetUtils_isMcastAddr
static bool EnetUtils_isMcastAddr(const uint8_t *addr)
Check if address is multicast.
Definition: enet_utils.h:427
EnetUtils_copyMacAddr
static void EnetUtils_copyMacAddr(uint8_t *dst, const uint8_t *src)
Copy MAC address.
Definition: enet_utils.h:410
EnetMacPort_Interface
MAC port interface.
Definition: enet_mod_macport.h:274
EnetUtils_clearMacAddr
static void EnetUtils_clearMacAddr(uint8_t *addr)
Clear MAC address.
Definition: enet_utils.h:422
EnetUtils_macToPhyMii
EnetPhy_Mii EnetUtils_macToPhyMii(const EnetMacPort_Interface *macMii)
Convert MAC port MII to PHY MII types.
EnetUtils_Cfg::print
Enet_Print print
Definition: enet_utils.h:200
Enet_VirtToPhys
uint64_t(* Enet_VirtToPhys)(const void *virtAddr, void *appData)
Virtual-to-physical address translation callback function.
Definition: enet_utils.h:178
enet_cfg.h
This file contains the Enet configuration parameters.
EnetUtils_min
uint32_t EnetUtils_min(uint32_t num1, uint32_t num2)
Returns minimum of two numbers.
ENET_MAC_ADDR_LEN
#define ENET_MAC_ADDR_LEN
MAC address length in bytes/octets.
Definition: enet_types.h:153
EnetUtils_init
void EnetUtils_init(const EnetUtils_Cfg *cfg)
Initialize utils module.
EnetUtils_cmpMacAddr
static bool EnetUtils_cmpMacAddr(const uint8_t *addr1, const uint8_t *addr2)
Compare two MAC address.
Definition: enet_utils.h:416
EnetUtils_virtToPhys
uint64_t EnetUtils_virtToPhys(const void *virtAddr, void *appData)
Convert a virtual address to physical address.
EnetUtils_vprintf
void EnetUtils_vprintf(const char *fmt, va_list args)
Print function for va_list.
EnetUtils_Cfg::physToVirt
Enet_PhysToVirt physToVirt
Definition: enet_utils.h:210