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