can_dm.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #ifndef __CAN_DM_H__
  11. #define __CAN_DM_H__
  12. #include <drivers/misc.h>
  13. /* Special address description flags for the CAN_ID */
  14. #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
  15. #define CAN_RTR_FLAG 0x40000000U /* Remote transmission request */
  16. #define CAN_ERR_FLAG 0x20000000U /* Error message frame */
  17. /* Valid bits in CAN ID for frame formats */
  18. #define CAN_SFF_MASK 0x000007ffU /* Standard frame format (SFF) */
  19. #define CAN_EFF_MASK 0x1fffffffU /* Extended frame format (EFF) */
  20. #define CAN_ERR_MASK 0x1fffffffU /* Omit EFF, RTR, ERR flags */
  21. #define CANXL_PRIO_MASK CAN_SFF_MASK /* 11 bit priority mask */
  22. /* CAN payload length and DLC definitions according to ISO 11898-1 */
  23. #define CAN_MAX_DLC 8
  24. #define CAN_MAX_RAW_DLC 15
  25. #define CAN_MAX_DLEN 8
  26. /* CAN FD payload length and DLC definitions according to ISO 11898-7 */
  27. #define CANFD_MAX_DLC 15
  28. #define CANFD_MAX_DLEN 64
  29. /*
  30. * To be used in the CAN netdriver receive path to ensure conformance with
  31. * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
  32. */
  33. #define can_get_dlc(v) (rt_min_t(rt_uint8_t, (v), CAN_MAX_DLC))
  34. #define canfd_get_dlc(v) (rt_min_t(rt_uint8_t, (v), CANFD_MAX_DLC))
  35. /**
  36. * @brief Convert CAN DLC value to actual data length
  37. *
  38. * Converts a CAN Data Length Code (DLC) to the actual number of data bytes
  39. * according to ISO 11898-1 and ISO 11898-7 (CAN FD) specifications.
  40. *
  41. * @param can_dlc The DLC value (0-15)
  42. * @return The actual data length in bytes
  43. */
  44. rt_uint8_t can_dlc2len(rt_uint8_t can_dlc);
  45. /**
  46. * @brief Convert data length to CAN DLC value
  47. *
  48. * Converts a data length in bytes to the appropriate CAN Data Length Code (DLC)
  49. * according to ISO 11898-1 and ISO 11898-7 (CAN FD) specifications.
  50. *
  51. * @param len The data length in bytes (0-64)
  52. * @return The corresponding DLC value (0-15)
  53. */
  54. rt_uint8_t can_len2dlc(rt_uint8_t len);
  55. #endif /* __CAN_DM_H__ */