can.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**************************************************************************//**
  2. * @file can.h
  3. * @version V2.00
  4. * $Revision: 9 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 Series CAN Driver Header File
  7. *
  8. * @note
  9. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #ifndef __CAN_H__
  13. #define __CAN_H__
  14. #include "M451Series.h"
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. /** @addtogroup Standard_Driver Standard Driver
  20. @{
  21. */
  22. /** @addtogroup CAN_Driver CAN Driver
  23. @{
  24. */
  25. /** @addtogroup CAN_EXPORTED_CONSTANTS CAN Exported Constants
  26. @{
  27. */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* CAN Test Mode Constant Definitions */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. #define CAN_NORMAL_MODE 0
  32. #define CAN_BASIC_MODE 1
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. /* Message ID Type Constant Definitions */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. #define CAN_STD_ID 0
  37. #define CAN_EXT_ID 1
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. /* Message Frame Type Constant Definitions */
  40. /*---------------------------------------------------------------------------------------------------------*/
  41. #define CAN_REMOTE_FRAME 0
  42. #define CAN_DATA_FRAME 1
  43. /*---------------------------------------------------------------------------------------------------------*/
  44. /* CAN message structure */
  45. /*---------------------------------------------------------------------------------------------------------*/
  46. typedef struct
  47. {
  48. uint32_t IdType;
  49. uint32_t FrameType;
  50. uint32_t Id;
  51. uint8_t DLC;
  52. uint8_t Data[8];
  53. } STR_CANMSG_T;
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. /* CAN mask message structure */
  56. /*---------------------------------------------------------------------------------------------------------*/
  57. typedef struct
  58. {
  59. uint8_t u8Xtd;
  60. uint8_t u8Dir;
  61. uint32_t u32Id;
  62. uint8_t u8IdType;
  63. } STR_CANMASK_T;
  64. #define MSG(id) (id)
  65. /*@}*/ /* end of group CAN_EXPORTED_CONSTANTS */
  66. /** @addtogroup CAN_EXPORTED_FUNCTIONS CAN Exported Functions
  67. @{
  68. */
  69. /**
  70. * @brief Get interrupt status.
  71. *
  72. * @param[in] can The base address of can module.
  73. *
  74. * @return CAN module status register value.
  75. *
  76. * @details Status Interrupt is generated by bits BOff (CAN_STATUS[7]), EWarn (CAN_STATUS[6]),
  77. * EPass (CAN_STATUS[5]), RxOk (CAN_STATUS[4]), TxOk (CAN_STATUS[3]), and LEC (CAN_STATUS[2:0]).
  78. */
  79. #define CAN_GET_INT_STATUS(can) ((can)->STATUS)
  80. /**
  81. * @brief Get specified interrupt pending status.
  82. *
  83. * @param[in] can The base address of can module.
  84. *
  85. * @return The source of the interrupt.
  86. *
  87. * @details If several interrupts are pending, the CAN Interrupt Register will point to the pending interrupt
  88. * with the highest priority, disregarding their chronological order.
  89. */
  90. #define CAN_GET_INT_PENDING_STATUS(can) ((can)->IIDR)
  91. /**
  92. * @brief Disable wake-up function.
  93. *
  94. * @param[in] can The base address of can module.
  95. *
  96. * @return None
  97. *
  98. * @details The macro is used to disable wake-up function.
  99. */
  100. #define CAN_DISABLE_WAKEUP(can) ((can)->WU_EN = 0)
  101. /**
  102. * @brief Enable wake-up function.
  103. *
  104. * @param[in] can The base address of can module.
  105. *
  106. * @return None
  107. *
  108. * @details User can wake-up system when there is a falling edge in the CAN_Rx pin.
  109. */
  110. #define CAN_ENABLE_WAKEUP(can) ((can)->WU_EN = CAN_WUEN_WAKUP_EN_Msk)
  111. /**
  112. * @brief Get specified Message Object new data into bit value.
  113. *
  114. * @param[in] can The base address of can module.
  115. * @param[in] u32MsgNum Specified Message Object number, valid value are from 0 to 31.
  116. *
  117. * @return Specified Message Object new data into bit value.
  118. *
  119. * @details The NewDat bit (CAN_IFn_MCON[15]) of a specific Message Object can be set/reset by the software through the IFn Message Interface Registers
  120. * or by the Message Handler after reception of a Data Frame or after a successful transmission.
  121. */
  122. #define CAN_GET_NEW_DATA_IN_BIT(can, u32MsgNum) ((u32MsgNum) < 16 ? (can)->NDAT1 & (1 << (u32MsgNum)) : (can)->NDAT2 & (1 << ((u32MsgNum)-16)))
  123. /*---------------------------------------------------------------------------------------------------------*/
  124. /* Define CAN functions prototype */
  125. /*---------------------------------------------------------------------------------------------------------*/
  126. uint32_t CAN_SetBaudRate(CAN_T *tCAN, uint32_t u32BaudRate);
  127. uint32_t CAN_Open(CAN_T *tCAN, uint32_t u32BaudRate, uint32_t u32Mode);
  128. void CAN_Close(CAN_T *tCAN);
  129. void CAN_CLR_INT_PENDING_BIT(CAN_T *tCAN, uint8_t u32MsgNum);
  130. void CAN_EnableInt(CAN_T *tCAN, uint32_t u32Mask);
  131. void CAN_DisableInt(CAN_T *tCAN, uint32_t u32Mask);
  132. int32_t CAN_Transmit(CAN_T *tCAN, uint32_t u32MsgNum , STR_CANMSG_T* pCanMsg);
  133. int32_t CAN_Receive(CAN_T *tCAN, uint32_t u32MsgNum , STR_CANMSG_T* pCanMsg);
  134. int32_t CAN_SetMultiRxMsg(CAN_T *tCAN, uint32_t u32MsgNum , uint32_t u32MsgCount, uint32_t u32IDType, uint32_t u32ID);
  135. int32_t CAN_SetRxMsg(CAN_T *tCAN, uint32_t u32MsgNum , uint32_t u32IDType, uint32_t u32ID);
  136. int32_t CAN_SetRxMsgAndMsk(CAN_T *tCAN, uint32_t u32MsgNum , uint32_t u32IDType, uint32_t u32ID, uint32_t u32IDMask);
  137. int32_t CAN_SetTxMsg(CAN_T *tCAN, uint32_t u32MsgNum , STR_CANMSG_T* pCanMsg);
  138. int32_t CAN_TriggerTxMsg(CAN_T *tCAN, uint32_t u32MsgNum);
  139. /*@}*/ /* end of group CAN_EXPORTED_FUNCTIONS */
  140. /*@}*/ /* end of group CAN_Driver */
  141. /*@}*/ /* end of group Standard_Driver */
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif //__CAN_H__
  146. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/