nu_can.h 7.3 KB

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