can.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**************************************************************************//**
  2. * @file can.h
  3. * @version V2.00
  4. * @brief M480 Series CAN Driver Header File
  5. *
  6. * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #ifndef __CAN_H__
  9. #define __CAN_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. /** @addtogroup Standard_Driver Standard Driver
  15. @{
  16. */
  17. /** @addtogroup CAN_Driver CAN Driver
  18. @{
  19. */
  20. /** @addtogroup CAN_EXPORTED_CONSTANTS CAN Exported Constants
  21. @{
  22. */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. /* CAN Test Mode Constant Definitions */
  25. /*---------------------------------------------------------------------------------------------------------*/
  26. #define CAN_NORMAL_MODE 0ul /*!< CAN select normal mode \hideinitializer */
  27. #define CAN_BASIC_MODE 1ul /*!< CAN select basic mode \hideinitializer */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* Message ID Type Constant Definitions */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. #define CAN_STD_ID 0ul /*!< CAN select standard ID \hideinitializer */
  32. #define CAN_EXT_ID 1ul /*!< CAN select extended ID \hideinitializer */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. /* Message Frame Type Constant Definitions */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. #define CAN_REMOTE_FRAME 0ul /*!< CAN frame select remote frame \hideinitializer */
  37. #define CAN_DATA_FRAME 1ul /*!< CAN frame select data frame \hideinitializer */
  38. /*@}*/ /* end of group CAN_EXPORTED_CONSTANTS */
  39. /** @addtogroup CAN_EXPORTED_STRUCTS CAN Exported Structs
  40. @{
  41. */
  42. /**
  43. * @details CAN message structure
  44. */
  45. typedef struct
  46. {
  47. uint32_t IdType; /*!< ID type */
  48. uint32_t FrameType; /*!< Frame type */
  49. uint32_t Id; /*!< Message ID */
  50. uint8_t DLC; /*!< Data length */
  51. uint8_t Data[8]; /*!< Data */
  52. } STR_CANMSG_T;
  53. /**
  54. * @details CAN mask message structure
  55. */
  56. typedef struct
  57. {
  58. uint8_t u8Xtd; /*!< Extended ID */
  59. uint8_t u8Dir; /*!< Direction */
  60. uint32_t u32Id; /*!< Message ID */
  61. uint8_t u8IdType; /*!< ID type*/
  62. } STR_CANMASK_T;
  63. /*@}*/ /* end of group CAN_EXPORTED_STRUCTS */
  64. /** @cond HIDDEN_SYMBOLS */
  65. #define MSG(id) (id)
  66. /** @endcond HIDDEN_SYMBOLS */
  67. /** @addtogroup CAN_EXPORTED_FUNCTIONS CAN Exported Functions
  68. @{
  69. */
  70. /**
  71. * @brief Get interrupt status.
  72. *
  73. * @param[in] can The base address of can module.
  74. *
  75. * @return CAN module status register value.
  76. *
  77. * @details Status Interrupt is generated by bits BOff (CAN_STATUS[7]), EWarn (CAN_STATUS[6]),
  78. * EPass (CAN_STATUS[5]), RxOk (CAN_STATUS[4]), TxOk (CAN_STATUS[3]), and LEC (CAN_STATUS[2:0]).
  79. * \hideinitializer
  80. */
  81. #define CAN_GET_INT_STATUS(can) ((can)->STATUS)
  82. /**
  83. * @brief Get specified interrupt pending status.
  84. *
  85. * @param[in] can The base address of can module.
  86. *
  87. * @return The source of the interrupt.
  88. *
  89. * @details If several interrupts are pending, the CAN Interrupt Register will point to the pending interrupt
  90. * with the highest priority, disregarding their chronological order.
  91. * \hideinitializer
  92. */
  93. #define CAN_GET_INT_PENDING_STATUS(can) ((can)->IIDR)
  94. /**
  95. * @brief Disable wake-up function.
  96. *
  97. * @param[in] can The base address of can module.
  98. *
  99. * @return None
  100. *
  101. * @details The macro is used to disable wake-up function.
  102. * \hideinitializer
  103. */
  104. #define CAN_DISABLE_WAKEUP(can) ((can)->WU_EN = 0ul)
  105. /**
  106. * @brief Enable wake-up function.
  107. *
  108. * @param[in] can The base address of can module.
  109. *
  110. * @return None
  111. *
  112. * @details User can wake-up system when there is a falling edge in the CAN_Rx pin.
  113. * \hideinitializer
  114. */
  115. #define CAN_ENABLE_WAKEUP(can) ((can)->WU_EN = CAN_WU_EN_WAKUP_EN_Msk)
  116. /**
  117. * @brief Get specified Message Object new data into bit value.
  118. *
  119. * @param[in] can The base address of can module.
  120. * @param[in] u32MsgNum Specified Message Object number, valid value are from 0 to 31.
  121. *
  122. * @return Specified Message Object new data into bit value.
  123. *
  124. * @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
  125. * or by the Message Handler after reception of a Data Frame or after a successful transmission.
  126. * \hideinitializer
  127. */
  128. #define CAN_GET_NEW_DATA_IN_BIT(can, u32MsgNum) ((u32MsgNum) < 16 ? (can)->NDAT1 & (1 << (u32MsgNum)) : (can)->NDAT2 & (1 << ((u32MsgNum)-16)))
  129. /*---------------------------------------------------------------------------------------------------------*/
  130. /* Define CAN functions prototype */
  131. /*---------------------------------------------------------------------------------------------------------*/
  132. uint32_t CAN_SetBaudRate(CAN_T *tCAN, uint32_t u32BaudRate);
  133. uint32_t CAN_Open(CAN_T *tCAN, uint32_t u32BaudRate, uint32_t u32Mode);
  134. void CAN_Close(CAN_T *tCAN);
  135. void CAN_CLR_INT_PENDING_BIT(CAN_T *tCAN, uint8_t u32MsgNum);
  136. void CAN_EnableInt(CAN_T *tCAN, uint32_t u32Mask);
  137. void CAN_DisableInt(CAN_T *tCAN, uint32_t u32Mask);
  138. int32_t CAN_Transmit(CAN_T *tCAN, uint32_t u32MsgNum, STR_CANMSG_T* pCanMsg);
  139. int32_t CAN_Receive(CAN_T *tCAN, uint32_t u32MsgNum, STR_CANMSG_T* pCanMsg);
  140. int32_t CAN_SetMultiRxMsg(CAN_T *tCAN, uint32_t u32MsgNum, uint32_t u32MsgCount, uint32_t u32IDType, uint32_t u32ID);
  141. int32_t CAN_SetRxMsg(CAN_T *tCAN, uint32_t u32MsgNum, uint32_t u32IDType, uint32_t u32ID);
  142. int32_t CAN_SetRxMsgAndMsk(CAN_T *tCAN, uint32_t u32MsgNum, uint32_t u32IDType, uint32_t u32ID, uint32_t u32IDMask);
  143. int32_t CAN_SetTxMsg(CAN_T *tCAN, uint32_t u32MsgNum, STR_CANMSG_T* pCanMsg);
  144. int32_t CAN_TriggerTxMsg(CAN_T *tCAN, uint32_t u32MsgNum);
  145. int32_t CAN_BasicSendMsg(CAN_T *tCAN, STR_CANMSG_T* pCanMsg);
  146. int32_t CAN_BasicReceiveMsg(CAN_T *tCAN, STR_CANMSG_T* pCanMsg);
  147. void CAN_EnterInitMode(CAN_T *tCAN, uint8_t u8Mask);
  148. void CAN_EnterTestMode(CAN_T *tCAN, uint8_t u8TestMask);
  149. void CAN_LeaveTestMode(CAN_T *tCAN);
  150. uint32_t CAN_GetCANBitRate(CAN_T *tCAN);
  151. uint32_t CAN_IsNewDataReceived(CAN_T *tCAN, uint8_t u8MsgObj);
  152. void CAN_LeaveInitMode(CAN_T *tCAN);
  153. int32_t CAN_SetRxMsgObjAndMsk(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8idType, uint32_t u32id, uint32_t u32idmask, uint8_t u8singleOrFifoLast);
  154. int32_t CAN_SetRxMsgObj(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8idType, uint32_t u32id, uint8_t u8singleOrFifoLast);
  155. void CAN_WaitMsg(CAN_T *tCAN);
  156. int32_t CAN_ReadMsgObj(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8Release, STR_CANMSG_T* pCanMsg);
  157. /*@}*/ /* end of group CAN_EXPORTED_FUNCTIONS */
  158. /*@}*/ /* end of group CAN_Driver */
  159. /*@}*/ /* end of group Standard_Driver */
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /*__CAN_H__ */
  164. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/