CAN.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @section License
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017, Thomas Barth, barth-dev.de
  7. * 2018, Michael Wagner, mw@iot-make.de
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without
  11. * restriction, including without limitation the rights to use, copy,
  12. * modify, merge, publish, distribute, sublicense, and/or sell copies
  13. * of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  23. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  24. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. */
  28. #ifndef __DRIVERS_CAN_H__
  29. #define __DRIVERS_CAN_H__
  30. #include <stdint.h>
  31. #include "CAN_config.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * \brief CAN frame type (standard/extended)
  37. */
  38. typedef enum {
  39. CAN_frame_std = 0, /**< Standard frame, using 11 bit identifer. */
  40. CAN_frame_ext = 1 /**< Extended frame, using 29 bit identifer. */
  41. } CAN_frame_format_t;
  42. /**
  43. * \brief CAN RTR
  44. */
  45. typedef enum {
  46. CAN_no_RTR = 0, /**< No RTR frame. */
  47. CAN_RTR = 1 /**< RTR frame. */
  48. } CAN_RTR_t;
  49. /** \brief Frame information record type */
  50. typedef union {
  51. uint32_t U; /**< \brief Unsigned access */
  52. struct {
  53. uint8_t DLC : 4; /**< \brief [3:0] DLC, Data length container */
  54. unsigned int unknown_2 : 2; /**< \brief \internal unknown */
  55. CAN_RTR_t RTR : 1; /**< \brief [6:6] RTR, Remote Transmission Request */
  56. CAN_frame_format_t FF : 1; /**< \brief [7:7] Frame Format, see# CAN_frame_format_t*/
  57. unsigned int reserved_24 : 24; /**< \brief \internal Reserved */
  58. } B;
  59. } CAN_FIR_t;
  60. /** \brief CAN Frame structure */
  61. typedef struct {
  62. CAN_FIR_t FIR; /**< \brief Frame information record*/
  63. uint32_t MsgID; /**< \brief Message ID */
  64. union {
  65. uint8_t u8[8]; /**< \brief Payload byte access*/
  66. uint32_t u32[2]; /**< \brief Payload u32 access*/
  67. uint64_t u64; /**< \brief Payload u64 access*/
  68. } data;
  69. } CAN_frame_t;
  70. typedef enum {
  71. Dual_Mode=0, /**< \brief The dual acceptance filter option is enabled (two filters, each with the length of 16 bit are active) */
  72. Single_Mode=1 /**< \brief The single acceptance filter option is enabled (one filter with the length of 32 bit is active) */
  73. } CAN_filter_mode_t;
  74. /** \brief CAN Filter structure */
  75. typedef struct {
  76. CAN_filter_mode_t FM:1; /**< \brief [0:0] Filter Mode */
  77. uint8_t ACR0; /**< \brief Acceptance Code Register ACR0 */
  78. uint8_t ACR1; /**< \brief Acceptance Code Register ACR1 */
  79. uint8_t ACR2; /**< \brief Acceptance Code Register ACR2 */
  80. uint8_t ACR3; /**< \brief Acceptance Code Register ACR3 */
  81. uint8_t AMR0; /**< \brief Acceptance Mask Register AMR0 */
  82. uint8_t AMR1; /**< \brief Acceptance Mask Register AMR1 */
  83. uint8_t AMR2; /**< \brief Acceptance Mask Register AMR2 */
  84. uint8_t AMR3; /**< \brief Acceptance Mask Register AMR3 */
  85. } CAN_filter_t;
  86. /**
  87. * \brief Initialize the CAN Module
  88. *
  89. * \return 0 CAN Module had been initialized
  90. */
  91. int CAN_init(void);
  92. /**
  93. * \brief Send a can frame
  94. *
  95. * \param p_frame Pointer to the frame to be send, see #CAN_frame_t
  96. * \return 0 Frame has been written to the module
  97. */
  98. int CAN_write_frame(const CAN_frame_t *p_frame);
  99. /**
  100. * \brief Stops the CAN Module
  101. *
  102. * \return 0 CAN Module was stopped
  103. */
  104. int CAN_stop(void);
  105. /**
  106. * \brief Config CAN Filter, must call before CANInit()
  107. *
  108. * \param p_filter Pointer to the filter, see #CAN_filter_t
  109. * \return 0 CAN Filter had been initialized
  110. */
  111. int CAN_config_filter(const CAN_filter_t* p_filter);
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. #endif