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