nu_miscutil.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-2-7 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #ifndef NU_MISC_UTIL_H
  13. #define NU_MISC_UTIL_H
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define NU_MAX(a,b) ((a)>(b)?(a):(b))
  18. #define NU_MIN(a,b) ((a)<(b)?(a):(b))
  19. #define NU_CLAMP(x, min, max) NU_MIN(NU_MAX((x), (min)), (max))
  20. #define NU_ALIGN_DOWN(X, ALIGN) ((X) & ~((ALIGN) - 1))
  21. #define NU_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1))
  22. #define NU_ISALIGNED(a, b) (((a) & (b - 1)) == 0)
  23. #define SET_BIT(REG, BIT) ((REG) |= (BIT))
  24. #define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))
  25. #define READ_BIT(REG, BIT) ((REG) & (BIT))
  26. #define CLEAR_REG(REG) ((REG) = (0x0))
  27. #define WRITE_REG(REG, VAL) ((REG) = (VAL))
  28. #define READ_REG(REG) ((REG))
  29. #define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
  30. #ifdef __cplusplus
  31. }
  32. #endif
  33. #endif