AM263x MCU+ SDK  09.02.00
enet_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Texas Instruments Incorporated 2022
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 #define ENET_NUM_NANOSECS_PER_SEC (1000000000ULL)
157 /* ========================================================================== */
158 /* Structures and Enums */
159 /* ========================================================================== */
160 
168 typedef void (*Enet_Print)(const char *fmt, ...);
169 
181 typedef uint64_t (*Enet_VirtToPhys)(const void *virtAddr, void *appData);
182 
194 typedef void *(*Enet_PhysToVirt)(uint64_t phyAddr, void *appData);
195 
199 typedef struct EnetUtils_Cfg_s
200 {
204 
209 
214 } EnetUtils_Cfg;
215 
216 /* ========================================================================== */
217 /* Global Variables Declarations */
218 /* ========================================================================== */
219 
220 /* None */
221 
222 /* ========================================================================== */
223 /* Function Declarations */
224 /* ========================================================================== */
225 
234 void EnetUtils_init(const EnetUtils_Cfg *cfg);
235 
239 void EnetUtils_deinit(void);
240 
248 void EnetUtils_printf(const char *fmt,
249  ...);
250 
259 void EnetUtils_vprintf(const char *fmt,
260  va_list args);
261 
270 uint32_t EnetUtils_min(uint32_t num1,
271  uint32_t num2);
272 
281 uint32_t EnetUtils_max(uint32_t num1,
282  uint32_t num2);
283 
290 void EnetUtils_delayTicks(const uint32_t delayTicks);
291 
298 void EnetUtils_delayNs(const uint32_t delayNs);
299 
308 uint64_t EnetUtils_virtToPhys(const void *virtAddr,
309  void *appData);
310 
319 void *EnetUtils_physToVirt(uint64_t physAddr,
320  void *appData);
321 
322 #if ENET_CFG_IS_ON(ASSERT) && !defined(__KLOCWORK__)
323 
331 static inline void EnetUtils_assertLocal(bool condition,
332  const char *str,
333  const char *fileName,
334  int32_t lineNum);
335 #endif
336 
348 
357 static inline void EnetUtils_copyMacAddr(uint8_t *dst,
358  const uint8_t *src);
359 
370 static inline bool EnetUtils_cmpMacAddr(const uint8_t *addr1,
371  const uint8_t *addr2);
372 
380 static inline void EnetUtils_clearMacAddr(uint8_t *addr);
381 
391 static inline bool EnetUtils_isMcastAddr(const uint8_t *addr);
392 
393 /* ========================================================================== */
394 /* Deprecated Function Declarations */
395 /* ========================================================================== */
396 
397 /* None */
398 
399 /* ========================================================================== */
400 /* Static Function Definitions */
401 /* ========================================================================== */
402 
403 #if ENET_CFG_IS_ON(ASSERT) && !defined(__KLOCWORK__)
404 static inline void EnetUtils_assertLocal(bool condition,
405  const char *str,
406  const char *fileName,
407  int32_t lineNum)
408 {
409  volatile static bool gEnetAssertWaitInLoop = true;
410 
411  if (!condition)
412  {
413  EnetUtils_printf("Assertion @ Line: %d in %s: %s\n", lineNum, fileName, str);
414  while (gEnetAssertWaitInLoop)
415  {
416  /* Do nothing */
417  }
418  }
419 }
420 #endif
421 
422 static inline void EnetUtils_copyMacAddr(uint8_t *dst,
423  const uint8_t *src)
424 {
425  memcpy(dst, src, ENET_MAC_ADDR_LEN);
426 }
427 
428 static inline bool EnetUtils_cmpMacAddr(const uint8_t *addr1,
429  const uint8_t *addr2)
430 {
431  return (memcmp(addr1, addr2, ENET_MAC_ADDR_LEN) == 0U);
432 }
433 
434 static inline void EnetUtils_clearMacAddr(uint8_t *addr)
435 {
436  memset(addr, 0, ENET_MAC_ADDR_LEN);
437 }
438 
439 static inline bool EnetUtils_isMcastAddr(const uint8_t *addr)
440 {
441  return ((addr[0U] & 1U) == 1U);
442 }
443 
444 #ifdef __cplusplus
445 }
446 #endif
447 
448 #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:194
EnetUtils_printf
void EnetUtils_printf(const char *fmt,...)
Print function.
args
void * args
Definition: hsmclient_msg.h:4
EnetUtils_Cfg::virtToPhys
Enet_VirtToPhys virtToPhys
Definition: enet_utils.h:208
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:212
EnetUtils_delayNs
void EnetUtils_delayNs(const uint32_t delayNs)
Busy loop for a given amount of delay in nano seconds.
Enet_Print
void(* Enet_Print)(const char *fmt,...)
Info/debug print function prototype.
Definition: enet_utils.h:168
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:200
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:439
EnetUtils_copyMacAddr
static void EnetUtils_copyMacAddr(uint8_t *dst, const uint8_t *src)
Copy MAC address.
Definition: enet_utils.h:422
EnetMacPort_Interface
MAC port interface.
Definition: enet_mod_macport.h:442
EnetUtils_clearMacAddr
static void EnetUtils_clearMacAddr(uint8_t *addr)
Clear MAC address.
Definition: enet_utils.h:434
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:203
Enet_VirtToPhys
uint64_t(* Enet_VirtToPhys)(const void *virtAddr, void *appData)
Virtual-to-physical address translation callback function.
Definition: enet_utils.h:181
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:428
EnetUtils_delayTicks
void EnetUtils_delayTicks(const uint32_t delayTicks)
Busy loop for a given amount of delay in CPU clock ticks.
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:213