AM243x MCU+ SDK  08.04.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  bool assertCond = (bool)(cond); \
114  ENETTRACE_ERR_IF(!assertCond, __VA_ARGS__); \
115  EnetUtils_assertLocal(assertCond, \
116  (const char *)# cond, \
117  (const char *)__FILE__, \
118  (int32_t)__LINE__); \
119  } while (0)
120 #endif /* defined(__KLOCWORK__) || defined(__cplusplus) */
121 #else /* !ENET_CFG_IS_ON(ASSERT) */
122 #define Enet_assert(cond, ...) (void)(cond)
123 #endif /* ENET_CFG_IS_ON(ASSERT) */
124 
128 #if ENET_CFG_IS_ON(ASSERT)
129 #if defined(__KLOCWORK__) || defined(__cplusplus)
130 #define Enet_devAssert(cond, ...) do { if (!(cond)) abort(); } while (0)
131 #else /* !defined(__KLOCWORK__) && !defined(__cplusplus) */
132 #define Enet_devAssert(cond, ...) \
133  do { \
134  ENETTRACE_ERR_IF(!(bool)(cond), __VA_ARGS__); \
135  EnetUtils_assertLocal((bool)(cond), \
136  (const char *)# cond, \
137  (const char *)__FILE__, \
138  (int32_t)__LINE__); \
139  } while (0)
140 #endif /* defined(__KLOCWORK__) || defined(__cplusplus) */
141 #else /* !ENET_CFG_IS_ON(ASSERT) || !ENET_CFG_IS_ON(DEV_ERROR) */
142 #define Enet_devAssert(cond, ...) (void)(cond)
143 #endif /* ENET_CFG_IS_ON(ASSERT) && ENET_CFG_IS_ON(DEV_ERROR) */
144 
148 #define ENET_UTILS_COMPILETIME_ASSERT(cond) \
149  do { \
150  typedef char ErrorCheck[((cond) == true) ? 1 : -1]; \
151  ErrorCheck a = {0}; \
152  a[0U] = a[0U]; \
153  } while (0)
154 
155 /* ========================================================================== */
156 /* Structures and Enums */
157 /* ========================================================================== */
158 
166 typedef void (*Enet_Print)(const char *fmt, ...);
167 
179 typedef uint64_t (*Enet_VirtToPhys)(const void *virtAddr, void *appData);
180 
192 typedef void *(*Enet_PhysToVirt)(uint64_t phyAddr, void *appData);
193 
197 typedef struct EnetUtils_Cfg_s
198 {
202 
207 
212 } EnetUtils_Cfg;
213 
214 /* ========================================================================== */
215 /* Global Variables Declarations */
216 /* ========================================================================== */
217 
218 /* None */
219 
220 /* ========================================================================== */
221 /* Function Declarations */
222 /* ========================================================================== */
223 
232 void EnetUtils_init(const EnetUtils_Cfg *cfg);
233 
237 void EnetUtils_deinit(void);
238 
246 void EnetUtils_printf(const char *fmt,
247  ...);
248 
257 void EnetUtils_vprintf(const char *fmt,
258  va_list args);
259 
268 uint32_t EnetUtils_min(uint32_t num1,
269  uint32_t num2);
270 
279 uint32_t EnetUtils_max(uint32_t num1,
280  uint32_t num2);
281 
287 void EnetUtils_delay(uint32_t delayVal);
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_ */
Enet_PhysToVirt
void *(* Enet_PhysToVirt)(uint64_t phyAddr, void *appData)
Physical-to-virtual address translation callback function.
Definition: enet_utils.h:192
EnetUtils_printf
void EnetUtils_printf(const char *fmt,...)
Print function.
EnetUtils_Cfg::virtToPhys
Enet_VirtToPhys virtToPhys
Definition: enet_utils.h:206
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:166
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:198
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:428
EnetUtils_copyMacAddr
static void EnetUtils_copyMacAddr(uint8_t *dst, const uint8_t *src)
Copy MAC address.
Definition: enet_utils.h:411
EnetMacPort_Interface
MAC port interface.
Definition: enet_mod_macport.h:288
EnetUtils_clearMacAddr
static void EnetUtils_clearMacAddr(uint8_t *addr)
Clear MAC address.
Definition: enet_utils.h:423
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:201
Enet_VirtToPhys
uint64_t(* Enet_VirtToPhys)(const void *virtAddr, void *appData)
Virtual-to-physical address translation callback function.
Definition: enet_utils.h:179
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:417
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:211