nu_can.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /**************************************************************************//**
  2. * @file can.c
  3. * @version V2.00
  4. * @brief N9H30 series CAN driver source file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include "nu_can.h"
  10. #include "nu_sys.h"
  11. /** @addtogroup Standard_Driver Standard Driver
  12. @{
  13. */
  14. /** @addtogroup CAN_Driver CAN Driver
  15. @{
  16. */
  17. /** @addtogroup CAN_EXPORTED_FUNCTIONS CAN Exported Functions
  18. @{
  19. */
  20. /** @cond HIDDEN_SYMBOLS */
  21. static uint8_t gu8LockCanIf[4ul][2ul] = {0ul}; /* The chip have 4 CANs. */
  22. #define RETRY_COUNTS (0x10000000ul)
  23. #define TSEG1_MIN 2ul
  24. #define TSEG1_MAX 16ul
  25. #define TSEG2_MIN 1ul
  26. #define TSEG2_MAX 8ul
  27. #define BRP_MIN 1ul
  28. #define BRP_MAX 1024ul /* 6-bit BRP field + 4-bit BRPE field*/
  29. #define SJW_MAX 4ul
  30. #define BRP_INC 1ul
  31. /* #define DEBUG_PRINTF printf */
  32. #define DEBUG_PRINTF(...)
  33. static uint32_t CAN_Clock = 75000000ul;
  34. static uint32_t LockIF(CAN_T *tCAN);
  35. static uint32_t LockIF_TL(CAN_T *tCAN);
  36. static void ReleaseIF(CAN_T *tCAN, uint32_t u32IfNo);
  37. static int can_update_spt(int sampl_pt, int tseg, int *tseg1, int *tseg2);
  38. /**
  39. * @brief Check if any interface is available then lock it for usage.
  40. * @param[in] tCAN The pointer to CAN module base address.
  41. * @retval 0 IF0 is free
  42. * @retval 1 IF1 is free
  43. * @retval 2 No IF is free
  44. * @details Search the first free message interface, starting from 0. If a interface is
  45. * available, set a flag to lock the interface.
  46. */
  47. static uint32_t LockIF(CAN_T *tCAN)
  48. {
  49. uint32_t u32CanNo;
  50. uint32_t u32FreeIfNo = 2ul;
  51. uint32_t u32IntMask;
  52. if (tCAN == CAN0)
  53. u32CanNo = 0ul;
  54. #if defined(CAN1)
  55. else if (tCAN == CAN1)
  56. u32CanNo = 1ul;
  57. #endif
  58. #if defined(CAN2)
  59. else if (tCAN == CAN2)
  60. u32CanNo = 2ul;
  61. #endif
  62. #if defined(CAN3)
  63. else if (tCAN == CAN3)
  64. u32CanNo = 3ul;
  65. #endif
  66. else
  67. return u32FreeIfNo;
  68. /* Disable CAN interrupt */
  69. u32IntMask = tCAN->CON & (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk);
  70. tCAN->CON = tCAN->CON & ~(CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk);
  71. /* Check interface 1 is available or not */
  72. if ((tCAN->IF[0ul].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0ul)
  73. {
  74. if (gu8LockCanIf[u32CanNo][0ul] == 0ul)
  75. {
  76. gu8LockCanIf[u32CanNo][0ul] = 1u;
  77. u32FreeIfNo = 0ul;
  78. }
  79. else
  80. {
  81. }
  82. }
  83. else
  84. {
  85. }
  86. /* Or check interface 2 is available or not */
  87. if (u32FreeIfNo == 2ul)
  88. {
  89. if ((tCAN->IF[1ul].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0ul)
  90. {
  91. if (gu8LockCanIf[u32CanNo][1ul] == 0ul)
  92. {
  93. gu8LockCanIf[u32CanNo][1ul] = 1u;
  94. u32FreeIfNo = 1ul;
  95. }
  96. else
  97. {
  98. }
  99. }
  100. else
  101. {
  102. }
  103. }
  104. else
  105. {
  106. }
  107. /* Enable CAN interrupt */
  108. tCAN->CON |= u32IntMask;
  109. return u32FreeIfNo;
  110. }
  111. /**
  112. * @brief Check if any interface is available in a time limitation then lock it for usage.
  113. * @param[in] tCAN The pointer to CAN module base address.
  114. * @retval 0 IF0 is free
  115. * @retval 1 IF1 is free
  116. * @retval 2 No IF is free
  117. * @details Search the first free message interface, starting from 0. If no interface is
  118. * it will try again until time out. If a interface is available, set a flag to
  119. * lock the interface.
  120. */
  121. static uint32_t LockIF_TL(CAN_T *tCAN)
  122. {
  123. uint32_t u32Count;
  124. uint32_t u32FreeIfNo;
  125. for (u32Count = 0ul; u32Count < RETRY_COUNTS; u32Count++)
  126. {
  127. if ((u32FreeIfNo = LockIF(tCAN)) != 2ul)
  128. {
  129. break;
  130. }
  131. else
  132. {
  133. }
  134. }
  135. return u32FreeIfNo;
  136. }
  137. /**
  138. * @brief Release locked interface.
  139. * @param[in] tCAN The pointer to CAN module base address.
  140. * @param[in] u32Info The interface number, 0 or 1.
  141. * @return none
  142. * @details Release the locked interface.
  143. */
  144. static void ReleaseIF(CAN_T *tCAN, uint32_t u32IfNo)
  145. {
  146. uint32_t u32IntMask;
  147. uint32_t u32CanNo;
  148. if (u32IfNo >= 2ul)
  149. {
  150. }
  151. else
  152. {
  153. if (tCAN == CAN0)
  154. u32CanNo = 0ul;
  155. #if defined(CAN1)
  156. else if (tCAN == CAN1)
  157. u32CanNo = 1ul;
  158. #endif
  159. #if defined(CAN2)
  160. else if (tCAN == CAN2)
  161. u32CanNo = 2ul;
  162. #endif
  163. #if defined(CAN3)
  164. else if (tCAN == CAN3)
  165. u32CanNo = 3ul;
  166. #endif
  167. else
  168. return ;
  169. /* Disable CAN interrupt */
  170. u32IntMask = tCAN->CON & (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk);
  171. tCAN->CON = tCAN->CON & ~(CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk);
  172. gu8LockCanIf[u32CanNo][u32IfNo] = 0u;
  173. /* Enable CAN interrupt */
  174. tCAN->CON |= u32IntMask;
  175. }
  176. }
  177. static int can_update_spt(int sampl_pt, int tseg, int *tseg1, int *tseg2)
  178. {
  179. *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
  180. if (*tseg2 < TSEG2_MIN)
  181. {
  182. *tseg2 = TSEG2_MIN;
  183. }
  184. else
  185. {
  186. }
  187. if (*tseg2 > TSEG2_MAX)
  188. {
  189. *tseg2 = TSEG2_MAX;
  190. }
  191. else
  192. {
  193. }
  194. *tseg1 = tseg - *tseg2;
  195. if (*tseg1 > TSEG1_MAX)
  196. {
  197. *tseg1 = TSEG1_MAX;
  198. *tseg2 = tseg - *tseg1;
  199. }
  200. else
  201. {
  202. }
  203. return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
  204. }
  205. /** @endcond HIDDEN_SYMBOLS */
  206. /**
  207. * @brief Enter initialization mode
  208. * @param[in] tCAN The pointer to CAN module base address.
  209. * @param[in] u8Mask Following values can be used.
  210. * \ref CAN_CON_DAR_Msk Disable automatic retransmission.
  211. * \ref CAN_CON_EIE_Msk Enable error interrupt.
  212. * \ref CAN_CON_SIE_Msk Enable status interrupt.
  213. * \ref CAN_CON_IE_Msk CAN interrupt.
  214. * @return None
  215. * @details This function is used to set CAN to enter initialization mode and enable access bit timing
  216. * register. After bit timing configuration ready, user must call CAN_LeaveInitMode()
  217. * to leave initialization mode and lock bit timing register to let new configuration
  218. * take effect.
  219. */
  220. void CAN_EnterInitMode(CAN_T *tCAN, uint8_t u8Mask)
  221. {
  222. tCAN->CON = u8Mask | (CAN_CON_INIT_Msk | CAN_CON_CCE_Msk);
  223. }
  224. /**
  225. * @brief Leave initialization mode
  226. * @param[in] tCAN The pointer to CAN module base address.
  227. * @return None
  228. * @details This function is used to set CAN to leave initialization mode to let
  229. * bit timing configuration take effect after configuration ready.
  230. */
  231. void CAN_LeaveInitMode(CAN_T *tCAN)
  232. {
  233. tCAN->CON &= (~(CAN_CON_INIT_Msk | CAN_CON_CCE_Msk));
  234. while (tCAN->CON & CAN_CON_INIT_Msk)
  235. {
  236. /* Check INIT bit is released */
  237. }
  238. }
  239. /**
  240. * @brief Wait message into message buffer in basic mode.
  241. * @param[in] tCAN The pointer to CAN module base address.
  242. * @return None
  243. * @details This function is used to wait message into message buffer in basic mode. Please notice the
  244. * function is polling NEWDAT bit of MCON register by while loop and it is used in basic mode.
  245. */
  246. void CAN_WaitMsg(CAN_T *tCAN)
  247. {
  248. tCAN->STATUS = 0x0ul; /* clr status */
  249. while (1)
  250. {
  251. if (tCAN->IF[1].MCON & CAN_IF_MCON_NEWDAT_Msk) /* check new data */
  252. {
  253. /* New Data IN */
  254. break;
  255. }
  256. else
  257. {
  258. }
  259. if (tCAN->STATUS & CAN_STATUS_RXOK_Msk)
  260. {
  261. /* Rx OK */
  262. }
  263. else
  264. {
  265. }
  266. if (tCAN->STATUS & CAN_STATUS_LEC_Msk)
  267. {
  268. /* Error */
  269. }
  270. else
  271. {
  272. }
  273. }
  274. }
  275. /**
  276. * @brief Get current bit rate
  277. * @param[in] tCAN The pointer to CAN module base address.
  278. * @return Current Bit-Rate (kilo bit per second)
  279. * @details Return current CAN bit rate according to the user bit-timing parameter settings
  280. */
  281. uint32_t CAN_GetCANBitRate(CAN_T *tCAN)
  282. {
  283. uint32_t u32Tseg1, u32Tseg2;
  284. uint32_t u32Bpr;
  285. u32Tseg1 = (tCAN->BTIME & CAN_BTIME_TSEG1_Msk) >> CAN_BTIME_TSEG1_Pos;
  286. u32Tseg2 = (tCAN->BTIME & CAN_BTIME_TSEG2_Msk) >> CAN_BTIME_TSEG2_Pos;
  287. u32Bpr = (tCAN->BTIME & CAN_BTIME_BRP_Msk) | (tCAN->BRPE << 6ul);
  288. return (CAN_Clock / (u32Bpr + 1ul) / (u32Tseg1 + u32Tseg2 + 3ul));
  289. }
  290. /**
  291. * @brief Switch the CAN into test mode.
  292. * @param[in] tCAN The pointer to CAN module base address.
  293. * @param[in] u8TestMask Specifies the configuration in test modes
  294. * \ref CAN_TEST_BASIC_Msk Enable basic mode of test mode
  295. * \ref CAN_TEST_SILENT_Msk Enable silent mode of test mode
  296. * \ref CAN_TEST_LBACK_Msk Enable Loop Back Mode of test mode
  297. * \ref CAN_TEST_Tx_Msk Control CAN_TX pin bit field
  298. * @return None
  299. * @details Switch the CAN into test mode. There are four test mode (BASIC/SILENT/LOOPBACK/
  300. * LOOPBACK combined SILENT/CONTROL_TX_PIN)could be selected. After setting test mode,user
  301. * must call CAN_LeaveInitMode() to let the setting take effect.
  302. */
  303. void CAN_EnterTestMode(CAN_T *tCAN, uint8_t u8TestMask)
  304. {
  305. tCAN->CON |= CAN_CON_TEST_Msk;
  306. tCAN->TEST = u8TestMask;
  307. }
  308. /**
  309. * @brief Leave the test mode
  310. * @param[in] tCAN The pointer to CAN module base address.
  311. * @return None
  312. * @details This function is used to Leave the test mode (switch into normal mode).
  313. */
  314. void CAN_LeaveTestMode(CAN_T *tCAN)
  315. {
  316. tCAN->CON |= CAN_CON_TEST_Msk;
  317. tCAN->TEST &= ~(CAN_TEST_LBACK_Msk | CAN_TEST_SILENT_Msk | CAN_TEST_BASIC_Msk);
  318. tCAN->CON &= (~CAN_CON_TEST_Msk);
  319. }
  320. /**
  321. * @brief Get the waiting status of a received message.
  322. * @param[in] tCAN The pointer to CAN module base address.
  323. * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31.
  324. * @retval non-zero The corresponding message object has a new data bit is set.
  325. * @retval 0 No message object has new data.
  326. * @details This function is used to get the waiting status of a received message.
  327. */
  328. uint32_t CAN_IsNewDataReceived(CAN_T *tCAN, uint8_t u8MsgObj)
  329. {
  330. return (u8MsgObj < 16ul ? tCAN->NDAT1 & (1ul << u8MsgObj) : tCAN->NDAT2 & (1ul << (u8MsgObj - 16ul)));
  331. }
  332. /**
  333. * @brief Send CAN message in BASIC mode of test mode
  334. * @param[in] tCAN The pointer to CAN module base address.
  335. * @param[in] pCanMsg Pointer to the message structure containing data to transmit.
  336. * @return TRUE: Transmission OK
  337. * FALSE: Check busy flag of interface 0 is timeout
  338. * @details The function is used to send CAN message in BASIC mode of test mode. Before call the API,
  339. * the user should be call CAN_EnterTestMode(CAN_TEST_BASIC) and let CAN controller enter
  340. * basic mode of test mode. Please notice IF1 Registers used as Tx Buffer in basic mode.
  341. */
  342. int32_t CAN_BasicSendMsg(CAN_T *tCAN, STR_CANMSG_T *pCanMsg)
  343. {
  344. uint32_t i = 0ul;
  345. int32_t rev = 1l;
  346. while (tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk)
  347. {
  348. }
  349. tCAN->STATUS &= (~CAN_STATUS_TXOK_Msk);
  350. if (pCanMsg->IdType == CAN_STD_ID)
  351. {
  352. /* standard ID*/
  353. tCAN->IF[0].ARB1 = 0ul;
  354. tCAN->IF[0].ARB2 = (((pCanMsg->Id) & 0x7FFul) << 2ul) ;
  355. }
  356. else
  357. {
  358. /* extended ID*/
  359. tCAN->IF[0].ARB1 = (pCanMsg->Id) & 0xFFFFul;
  360. tCAN->IF[0].ARB2 = ((pCanMsg->Id) & 0x1FFF0000ul) >> 16ul | CAN_IF_ARB2_XTD_Msk;
  361. }
  362. if (pCanMsg->FrameType)
  363. {
  364. tCAN->IF[0].ARB2 |= CAN_IF_ARB2_DIR_Msk;
  365. }
  366. else
  367. {
  368. tCAN->IF[0].ARB2 &= (~CAN_IF_ARB2_DIR_Msk);
  369. }
  370. tCAN->IF[0].MCON = (tCAN->IF[0].MCON & (~CAN_IF_MCON_DLC_Msk)) | pCanMsg->DLC;
  371. tCAN->IF[0].DAT_A1 = (uint16_t)((uint16_t)((uint16_t)pCanMsg->Data[1] << 8) | pCanMsg->Data[0]);
  372. tCAN->IF[0].DAT_A2 = (uint16_t)((uint16_t)((uint16_t)pCanMsg->Data[3] << 8) | pCanMsg->Data[2]);
  373. tCAN->IF[0].DAT_B1 = (uint16_t)((uint16_t)((uint16_t)pCanMsg->Data[5] << 8) | pCanMsg->Data[4]);
  374. tCAN->IF[0].DAT_B2 = (uint16_t)((uint16_t)((uint16_t)pCanMsg->Data[7] << 8) | pCanMsg->Data[6]);
  375. /* request transmission*/
  376. tCAN->IF[0].CREQ &= (~CAN_IF_CREQ_BUSY_Msk);
  377. if (tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk)
  378. {
  379. /* Cannot clear busy for sending ...*/
  380. rev = 0l; /* return FALSE */
  381. }
  382. else
  383. {
  384. tCAN->IF[0].CREQ |= CAN_IF_CREQ_BUSY_Msk; /* sending */
  385. for (i = 0ul; i < 0xFFFFFul; i++)
  386. {
  387. if ((tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0ul)
  388. {
  389. break;
  390. }
  391. else
  392. {
  393. }
  394. }
  395. if (i >= 0xFFFFFul)
  396. {
  397. /* Cannot send out... */
  398. rev = 0l; /* return FALSE */
  399. }
  400. else
  401. {
  402. }
  403. }
  404. return rev;
  405. }
  406. /**
  407. * @brief Get a message information in BASIC mode.
  408. *
  409. * @param[in] tCAN The pointer to CAN module base address.
  410. * @param[in] pCanMsg Pointer to the message structure where received data is copied.
  411. *
  412. * @return FALSE No any message received.
  413. * TRUE Receive a message success.
  414. *
  415. */
  416. int32_t CAN_BasicReceiveMsg(CAN_T *tCAN, STR_CANMSG_T *pCanMsg)
  417. {
  418. int32_t rev = 1l;
  419. if ((tCAN->IF[1].MCON & CAN_IF_MCON_NEWDAT_Msk) == 0ul)
  420. {
  421. /* In basic mode, receive data always save in IF2 */
  422. rev = 0; /* return FALSE */
  423. }
  424. else
  425. {
  426. tCAN->STATUS &= (~CAN_STATUS_RXOK_Msk);
  427. tCAN->IF[1].CMASK = CAN_IF_CMASK_ARB_Msk
  428. | CAN_IF_CMASK_CONTROL_Msk
  429. | CAN_IF_CMASK_DATAA_Msk
  430. | CAN_IF_CMASK_DATAB_Msk;
  431. if ((tCAN->IF[1].ARB2 & CAN_IF_ARB2_XTD_Msk) == 0ul)
  432. {
  433. /* standard ID*/
  434. pCanMsg->IdType = CAN_STD_ID;
  435. pCanMsg->Id = (tCAN->IF[1].ARB2 >> 2) & 0x07FFul;
  436. }
  437. else
  438. {
  439. /* extended ID*/
  440. pCanMsg->IdType = CAN_EXT_ID;
  441. pCanMsg->Id = (tCAN->IF[1].ARB2 & 0x1FFFul) << 16;
  442. pCanMsg->Id |= (uint32_t)tCAN->IF[1].ARB1;
  443. }
  444. pCanMsg->FrameType = (((tCAN->IF[1].ARB2 & CAN_IF_ARB2_DIR_Msk) >> CAN_IF_ARB2_DIR_Pos)) ? 0ul : 1ul;
  445. pCanMsg->DLC = (uint8_t)(tCAN->IF[1].MCON & CAN_IF_MCON_DLC_Msk);
  446. pCanMsg->Data[0] = (uint8_t)(tCAN->IF[1].DAT_A1 & CAN_IF_DAT_A1_DATA0_Msk);
  447. pCanMsg->Data[1] = (uint8_t)((tCAN->IF[1].DAT_A1 & CAN_IF_DAT_A1_DATA1_Msk) >> CAN_IF_DAT_A1_DATA1_Pos);
  448. pCanMsg->Data[2] = (uint8_t)(tCAN->IF[1].DAT_A2 & CAN_IF_DAT_A2_DATA2_Msk);
  449. pCanMsg->Data[3] = (uint8_t)((tCAN->IF[1].DAT_A2 & CAN_IF_DAT_A2_DATA3_Msk) >> CAN_IF_DAT_A2_DATA3_Pos);
  450. pCanMsg->Data[4] = (uint8_t)(tCAN->IF[1].DAT_B1 & CAN_IF_DAT_B1_DATA4_Msk);
  451. pCanMsg->Data[5] = (uint8_t)((tCAN->IF[1].DAT_B1 & CAN_IF_DAT_B1_DATA5_Msk) >> CAN_IF_DAT_B1_DATA5_Pos);
  452. pCanMsg->Data[6] = (uint8_t)(tCAN->IF[1].DAT_B2 & CAN_IF_DAT_B2_DATA6_Msk);
  453. pCanMsg->Data[7] = (uint8_t)((tCAN->IF[1].DAT_B2 & CAN_IF_DAT_B2_DATA7_Msk) >> CAN_IF_DAT_B2_DATA7_Pos);
  454. }
  455. return rev;
  456. }
  457. /**
  458. * @brief Set Rx message object, include ID mask.
  459. * @param[in] tCAN The pointer to CAN module base address.
  460. * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31.
  461. * @param[in] u8idType Specifies the identifier type of the frames that will be transmitted
  462. * This parameter can be one of the following values:
  463. * \ref CAN_STD_ID (standard ID, 11-bit)
  464. * \ref CAN_EXT_ID (extended ID, 29-bit)
  465. * @param[in] u32id Specifies the identifier used for acceptance filtering.
  466. * @param[in] u32idmask Specifies the identifier mask used for acceptance filtering.
  467. * @param[in] u8singleOrFifoLast Specifies the end-of-buffer indicator.
  468. * This parameter can be one of the following values:
  469. * TRUE: for a single receive object or a FIFO receive object that is the last one of the FIFO.
  470. * FALSE: for a FIFO receive object that is not the last one.
  471. * @retval TRUE SUCCESS
  472. * @retval FALSE No useful interface
  473. * @details The function is used to configure a receive message object.
  474. */
  475. int32_t CAN_SetRxMsgObjAndMsk(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8idType, uint32_t u32id, uint32_t u32idmask, uint8_t u8singleOrFifoLast)
  476. {
  477. int32_t rev = 1l;
  478. uint32_t u32MsgIfNum;
  479. /* Get and lock a free interface */
  480. if ((u32MsgIfNum = LockIF_TL(tCAN)) == 2ul)
  481. {
  482. rev = 0; /* return FALSE */
  483. }
  484. else
  485. {
  486. /* Command Setting */
  487. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_WRRD_Msk | CAN_IF_CMASK_MASK_Msk | CAN_IF_CMASK_ARB_Msk |
  488. CAN_IF_CMASK_CONTROL_Msk | CAN_IF_CMASK_DATAA_Msk | CAN_IF_CMASK_DATAB_Msk;
  489. if (u8idType == CAN_STD_ID) /* According STD/EXT ID format,Configure Mask and Arbitration register */
  490. {
  491. tCAN->IF[u32MsgIfNum].ARB1 = 0ul;
  492. tCAN->IF[u32MsgIfNum].ARB2 = CAN_IF_ARB2_MSGVAL_Msk | (u32id & 0x7FFul) << 2;
  493. }
  494. else
  495. {
  496. tCAN->IF[u32MsgIfNum].ARB1 = u32id & 0xFFFFul;
  497. tCAN->IF[u32MsgIfNum].ARB2 = CAN_IF_ARB2_MSGVAL_Msk | CAN_IF_ARB2_XTD_Msk | (u32id & 0x1FFF0000ul) >> 16;
  498. }
  499. tCAN->IF[u32MsgIfNum].MASK1 = (u32idmask & 0xFFFFul);
  500. tCAN->IF[u32MsgIfNum].MASK2 = (u32idmask >> 16) & 0xFFFFul;
  501. /* tCAN->IF[u32MsgIfNum].MCON |= CAN_IF_MCON_UMASK_Msk | CAN_IF_MCON_RXIE_Msk; */
  502. tCAN->IF[u32MsgIfNum].MCON = CAN_IF_MCON_UMASK_Msk | CAN_IF_MCON_RXIE_Msk;
  503. if (u8singleOrFifoLast)
  504. {
  505. tCAN->IF[u32MsgIfNum].MCON |= CAN_IF_MCON_EOB_Msk;
  506. }
  507. else
  508. {
  509. tCAN->IF[u32MsgIfNum].MCON &= (~CAN_IF_MCON_EOB_Msk);
  510. }
  511. tCAN->IF[u32MsgIfNum].DAT_A1 = 0ul;
  512. tCAN->IF[u32MsgIfNum].DAT_A2 = 0ul;
  513. tCAN->IF[u32MsgIfNum].DAT_B1 = 0ul;
  514. tCAN->IF[u32MsgIfNum].DAT_B2 = 0ul;
  515. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u8MsgObj;
  516. ReleaseIF(tCAN, u32MsgIfNum);
  517. }
  518. return rev;
  519. }
  520. /**
  521. * @brief Set Rx message object
  522. * @param[in] tCAN The pointer to CAN module base address.
  523. * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31.
  524. * @param[in] u8idType Specifies the identifier type of the frames that will be transmitted
  525. * This parameter can be one of the following values:
  526. * \ref CAN_STD_ID (standard ID, 11-bit)
  527. * \ref CAN_EXT_ID (extended ID, 29-bit)
  528. * @param[in] u32id Specifies the identifier used for acceptance filtering.
  529. * @param[in] u8singleOrFifoLast Specifies the end-of-buffer indicator.
  530. * This parameter can be one of the following values:
  531. * TRUE: for a single receive object or a FIFO receive object that is the last one of the FIFO.
  532. * FALSE: for a FIFO receive object that is not the last one.
  533. * @retval TRUE SUCCESS
  534. * @retval FALSE No useful interface
  535. * @details The function is used to configure a receive message object.
  536. */
  537. int32_t CAN_SetRxMsgObj(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8idType, uint32_t u32id, uint8_t u8singleOrFifoLast)
  538. {
  539. int32_t rev = 1l;
  540. uint32_t u32MsgIfNum;
  541. /* Get and lock a free interface */
  542. if ((u32MsgIfNum = LockIF_TL(tCAN)) == 2ul)
  543. {
  544. rev = 0; /* return FALSE */
  545. }
  546. else
  547. {
  548. /* Command Setting */
  549. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_WRRD_Msk | CAN_IF_CMASK_MASK_Msk | CAN_IF_CMASK_ARB_Msk |
  550. CAN_IF_CMASK_CONTROL_Msk | CAN_IF_CMASK_DATAA_Msk | CAN_IF_CMASK_DATAB_Msk;
  551. if (u8idType == CAN_STD_ID) /* According STD/EXT ID format,Configure Mask and Arbitration register */
  552. {
  553. tCAN->IF[u32MsgIfNum].ARB1 = 0ul;
  554. tCAN->IF[u32MsgIfNum].ARB2 = CAN_IF_ARB2_MSGVAL_Msk | (u32id & 0x7FFul) << 2;
  555. }
  556. else
  557. {
  558. tCAN->IF[u32MsgIfNum].ARB1 = u32id & 0xFFFFul;
  559. tCAN->IF[u32MsgIfNum].ARB2 = CAN_IF_ARB2_MSGVAL_Msk | CAN_IF_ARB2_XTD_Msk | (u32id & 0x1FFF0000ul) >> 16;
  560. }
  561. /* tCAN->IF[u8MsgIfNum].MCON |= CAN_IF_MCON_UMASK_Msk | CAN_IF_MCON_RXIE_Msk; */
  562. tCAN->IF[u32MsgIfNum].MCON = CAN_IF_MCON_UMASK_Msk | CAN_IF_MCON_RXIE_Msk;
  563. if (u8singleOrFifoLast)
  564. {
  565. tCAN->IF[u32MsgIfNum].MCON |= CAN_IF_MCON_EOB_Msk;
  566. }
  567. else
  568. {
  569. tCAN->IF[u32MsgIfNum].MCON &= (~CAN_IF_MCON_EOB_Msk);
  570. }
  571. tCAN->IF[u32MsgIfNum].DAT_A1 = 0ul;
  572. tCAN->IF[u32MsgIfNum].DAT_A2 = 0ul;
  573. tCAN->IF[u32MsgIfNum].DAT_B1 = 0ul;
  574. tCAN->IF[u32MsgIfNum].DAT_B2 = 0ul;
  575. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u8MsgObj;
  576. ReleaseIF(tCAN, u32MsgIfNum);
  577. }
  578. return rev;
  579. }
  580. /**
  581. * @brief Gets the message
  582. * @param[in] tCAN The pointer to CAN module base address.
  583. * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31.
  584. * @param[in] u8Release Specifies the message release indicator.
  585. * This parameter can be one of the following values:
  586. * TRUE: the message object is released when getting the data.
  587. * FALSE:the message object is not released.
  588. * @param[in] pCanMsg Pointer to the message structure where received data is copied.
  589. * @retval TRUE Success
  590. * @retval FALSE No any message received
  591. * @details Gets the message, if received.
  592. */
  593. int32_t CAN_ReadMsgObj(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8Release, STR_CANMSG_T *pCanMsg)
  594. {
  595. int32_t rev = 1l;
  596. uint32_t u32MsgIfNum;
  597. if (!CAN_IsNewDataReceived(tCAN, u8MsgObj))
  598. {
  599. rev = 0; /* return FALSE */
  600. }
  601. else
  602. {
  603. /* Get and lock a free interface */
  604. if ((u32MsgIfNum = LockIF_TL(tCAN)) == 2ul)
  605. {
  606. rev = 0; /* return FALSE */
  607. }
  608. else
  609. {
  610. tCAN->STATUS &= (~CAN_STATUS_RXOK_Msk);
  611. /* read the message contents*/
  612. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_MASK_Msk
  613. | CAN_IF_CMASK_ARB_Msk
  614. | CAN_IF_CMASK_CONTROL_Msk
  615. | CAN_IF_CMASK_CLRINTPND_Msk
  616. | (u8Release ? CAN_IF_CMASK_TXRQSTNEWDAT_Msk : 0ul)
  617. | CAN_IF_CMASK_DATAA_Msk
  618. | CAN_IF_CMASK_DATAB_Msk;
  619. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u8MsgObj;
  620. while (tCAN->IF[u32MsgIfNum].CREQ & CAN_IF_CREQ_BUSY_Msk)
  621. {
  622. /*Wait*/
  623. }
  624. if ((tCAN->IF[u32MsgIfNum].ARB2 & CAN_IF_ARB2_XTD_Msk) == 0ul)
  625. {
  626. /* standard ID*/
  627. pCanMsg->IdType = CAN_STD_ID;
  628. pCanMsg->Id = (tCAN->IF[u32MsgIfNum].ARB2 & CAN_IF_ARB2_ID_Msk) >> 2ul;
  629. }
  630. else
  631. {
  632. /* extended ID*/
  633. pCanMsg->IdType = CAN_EXT_ID;
  634. pCanMsg->Id = (((tCAN->IF[u32MsgIfNum].ARB2) & 0x1FFFul) << 16) | tCAN->IF[u32MsgIfNum].ARB1;
  635. }
  636. pCanMsg->DLC = (uint8_t)(tCAN->IF[u32MsgIfNum].MCON & CAN_IF_MCON_DLC_Msk);
  637. pCanMsg->Data[0] = (uint8_t)(tCAN->IF[u32MsgIfNum].DAT_A1 & CAN_IF_DAT_A1_DATA0_Msk);
  638. pCanMsg->Data[1] = (uint8_t)((tCAN->IF[u32MsgIfNum].DAT_A1 & CAN_IF_DAT_A1_DATA1_Msk) >> CAN_IF_DAT_A1_DATA1_Pos);
  639. pCanMsg->Data[2] = (uint8_t)(tCAN->IF[u32MsgIfNum].DAT_A2 & CAN_IF_DAT_A2_DATA2_Msk);
  640. pCanMsg->Data[3] = (uint8_t)((tCAN->IF[u32MsgIfNum].DAT_A2 & CAN_IF_DAT_A2_DATA3_Msk) >> CAN_IF_DAT_A2_DATA3_Pos);
  641. pCanMsg->Data[4] = (uint8_t)(tCAN->IF[u32MsgIfNum].DAT_B1 & CAN_IF_DAT_B1_DATA4_Msk);
  642. pCanMsg->Data[5] = (uint8_t)((tCAN->IF[u32MsgIfNum].DAT_B1 & CAN_IF_DAT_B1_DATA5_Msk) >> CAN_IF_DAT_B1_DATA5_Pos);
  643. pCanMsg->Data[6] = (uint8_t)(tCAN->IF[u32MsgIfNum].DAT_B2 & CAN_IF_DAT_B2_DATA6_Msk);
  644. pCanMsg->Data[7] = (uint8_t)((tCAN->IF[u32MsgIfNum].DAT_B2 & CAN_IF_DAT_B2_DATA7_Msk) >> CAN_IF_DAT_B2_DATA7_Pos);
  645. ReleaseIF(tCAN, u32MsgIfNum);
  646. }
  647. }
  648. return rev;
  649. }
  650. /**
  651. * @brief Set bus baud-rate.
  652. *
  653. * @param[in] tCAN The pointer to CAN module base address.
  654. * @param[in] u32BaudRate The target CAN baud-rate. The range of u32BaudRate is 1~1000KHz.
  655. *
  656. * @return u32CurrentBitRate Real baud-rate value.
  657. *
  658. * @details The function is used to set bus timing parameter according current clock and target baud-rate.
  659. */
  660. uint32_t CAN_SetBaudRate(CAN_T *tCAN, uint32_t u32BaudRate)
  661. {
  662. long rate;
  663. long best_error = 1000000000, error = 0;
  664. int best_tseg = 0, best_brp = 0, brp = 0;
  665. int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
  666. int spt_error = 1000, spt = 0, sampl_pt;
  667. uint64_t clock_freq = (uint64_t)0, u64PCLK_DIV = (uint64_t)1;
  668. uint32_t sjw = (uint32_t)1;
  669. CAN_EnterInitMode(tCAN, (uint8_t)0);
  670. CAN_Clock = sysGetClock(SYS_PCLK) * 1000000;
  671. clock_freq = CAN_Clock / u64PCLK_DIV;
  672. if (u32BaudRate >= (uint32_t)1000000)
  673. {
  674. u32BaudRate = (uint32_t)1000000;
  675. }
  676. /* Use CIA recommended sample points */
  677. if (u32BaudRate > (uint32_t)800000)
  678. {
  679. sampl_pt = (int)750;
  680. }
  681. else if (u32BaudRate > (uint32_t)500000)
  682. {
  683. sampl_pt = (int)800;
  684. }
  685. else
  686. {
  687. sampl_pt = (int)875;
  688. }
  689. /* tseg even = round down, odd = round up */
  690. for (tseg = (TSEG1_MAX + TSEG2_MAX) * 2ul + 1ul; tseg >= (TSEG1_MIN + TSEG2_MIN) * 2ul; tseg--)
  691. {
  692. tsegall = 1ul + tseg / 2ul;
  693. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  694. brp = clock_freq / (tsegall * u32BaudRate) + tseg % 2;
  695. /* chose brp step which is possible in system */
  696. brp = (brp / BRP_INC) * BRP_INC;
  697. if ((brp < BRP_MIN) || (brp > BRP_MAX))
  698. {
  699. continue;
  700. }
  701. rate = clock_freq / (brp * tsegall);
  702. error = u32BaudRate - rate;
  703. /* tseg brp biterror */
  704. if (error < 0)
  705. {
  706. error = -error;
  707. }
  708. if (error > best_error)
  709. {
  710. continue;
  711. }
  712. best_error = error;
  713. if (error == 0)
  714. {
  715. spt = can_update_spt(sampl_pt, tseg / 2, &tseg1, &tseg2);
  716. error = sampl_pt - spt;
  717. if (error < 0)
  718. {
  719. error = -error;
  720. }
  721. if (error > spt_error)
  722. {
  723. continue;
  724. }
  725. spt_error = error;
  726. }
  727. best_tseg = tseg / 2;
  728. best_brp = brp;
  729. if (error == 0)
  730. {
  731. break;
  732. }
  733. }
  734. spt = can_update_spt(sampl_pt, best_tseg, &tseg1, &tseg2);
  735. /* check for sjw user settings */
  736. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  737. if (sjw > SJW_MAX)
  738. {
  739. sjw = SJW_MAX;
  740. }
  741. /* bt->sjw must not be higher than tseg2 */
  742. if (tseg2 < sjw)
  743. {
  744. sjw = tseg2;
  745. }
  746. /* real bit-rate */
  747. u32BaudRate = clock_freq / (best_brp * (tseg1 + tseg2 + 1));
  748. tCAN->BTIME = ((uint32_t)(tseg2 - 1ul) << CAN_BTIME_TSEG2_Pos) | ((uint32_t)(tseg1 - 1ul) << CAN_BTIME_TSEG1_Pos) |
  749. ((uint32_t)(best_brp - 1ul) & CAN_BTIME_BRP_Msk) | (sjw << CAN_BTIME_SJW_Pos);
  750. tCAN->BRPE = ((uint32_t)(best_brp - 1ul) >> 6) & 0x0Ful;
  751. /* printf("\n bitrate = %d \n", CAN_GetCANBitRate(tCAN)); */
  752. CAN_LeaveInitMode(tCAN);
  753. return u32BaudRate;
  754. }
  755. /**
  756. * @brief The function is used to disable all CAN interrupt.
  757. *
  758. * @param[in] tCAN The pointer to CAN module base address.
  759. *
  760. * @return None
  761. *
  762. * @details No Status Change Interrupt and Error Status Interrupt will be generated.
  763. */
  764. void CAN_Close(CAN_T *tCAN)
  765. {
  766. CAN_DisableInt(tCAN, (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk));
  767. }
  768. /**
  769. * @brief Set CAN operation mode and target baud-rate.
  770. *
  771. * @param[in] tCAN The pointer to CAN module base address.
  772. * @param[in] u32BaudRate The target CAN baud-rate. The range of u32BaudRate is 1~1000KHz.
  773. * @param[in] u32Mode The CAN operation mode. Valid values are:
  774. * - \ref CAN_NORMAL_MODE Normal operation.
  775. * - \ref CAN_BASIC_MODE Basic mode.
  776. * @return u32CurrentBitRate Real baud-rate value.
  777. *
  778. * @details Set bus timing parameter according current clock and target baud-rate.
  779. * In Basic mode, IF1 Registers used as Tx Buffer, IF2 Registers used as Rx Buffer.
  780. */
  781. uint32_t CAN_Open(CAN_T *tCAN, uint32_t u32BaudRate, uint32_t u32Mode)
  782. {
  783. uint32_t u32CurrentBitRate;
  784. u32CurrentBitRate = CAN_SetBaudRate(tCAN, u32BaudRate);
  785. if (u32Mode == CAN_BASIC_MODE)
  786. {
  787. CAN_EnterTestMode(tCAN, (uint8_t)CAN_TEST_BASIC_Msk);
  788. }
  789. else
  790. {
  791. }
  792. return u32CurrentBitRate;
  793. }
  794. /**
  795. * @brief The function is used to configure a transmit object.
  796. *
  797. * @param[in] tCAN The pointer to CAN module base address.
  798. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  799. * @param[in] pCanMsg Pointer to the message structure where received data is copied.
  800. *
  801. * @retval FALSE No useful interface.
  802. * @retval TRUE Config message object success.
  803. *
  804. * @details The two sets of interface registers (IF1 and IF2) control the software access to the Message RAM.
  805. * They buffer the data to be transferred to and from the RAM, avoiding conflicts between software accesses and message reception/transmission.
  806. */
  807. int32_t CAN_SetTxMsg(CAN_T *tCAN, uint32_t u32MsgNum, STR_CANMSG_T *pCanMsg)
  808. {
  809. int32_t rev = 1l;
  810. uint32_t u32MsgIfNum;
  811. if ((u32MsgIfNum = LockIF_TL(tCAN)) == 2ul)
  812. {
  813. rev = 0; /* return FALSE */
  814. }
  815. else
  816. {
  817. /* update the contents needed for transmission*/
  818. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_WRRD_Msk | CAN_IF_CMASK_MASK_Msk | CAN_IF_CMASK_ARB_Msk |
  819. CAN_IF_CMASK_CONTROL_Msk | CAN_IF_CMASK_DATAA_Msk | CAN_IF_CMASK_DATAB_Msk;
  820. if (pCanMsg->IdType == CAN_STD_ID)
  821. {
  822. /* standard ID*/
  823. tCAN->IF[u32MsgIfNum].ARB1 = 0ul;
  824. tCAN->IF[u32MsgIfNum].ARB2 = (((pCanMsg->Id) & 0x7FFul) << 2) | CAN_IF_ARB2_DIR_Msk | CAN_IF_ARB2_MSGVAL_Msk;
  825. }
  826. else
  827. {
  828. /* extended ID*/
  829. tCAN->IF[u32MsgIfNum].ARB1 = (pCanMsg->Id) & 0xFFFFul;
  830. tCAN->IF[u32MsgIfNum].ARB2 = ((pCanMsg->Id) & 0x1FFF0000ul) >> 16 |
  831. CAN_IF_ARB2_DIR_Msk | CAN_IF_ARB2_XTD_Msk | CAN_IF_ARB2_MSGVAL_Msk;
  832. }
  833. if (pCanMsg->FrameType)
  834. {
  835. tCAN->IF[u32MsgIfNum].ARB2 |= CAN_IF_ARB2_DIR_Msk;
  836. }
  837. else
  838. {
  839. tCAN->IF[u32MsgIfNum].ARB2 &= (~CAN_IF_ARB2_DIR_Msk);
  840. }
  841. tCAN->IF[u32MsgIfNum].DAT_A1 = (uint16_t)((uint16_t)(((uint16_t)pCanMsg->Data[1] << 8)) | pCanMsg->Data[0]);
  842. tCAN->IF[u32MsgIfNum].DAT_A2 = (uint16_t)((uint16_t)(((uint16_t)pCanMsg->Data[3] << 8)) | pCanMsg->Data[2]);
  843. tCAN->IF[u32MsgIfNum].DAT_B1 = (uint16_t)((uint16_t)(((uint16_t)pCanMsg->Data[5] << 8)) | pCanMsg->Data[4]);
  844. tCAN->IF[u32MsgIfNum].DAT_B2 = (uint16_t)((uint16_t)(((uint16_t)pCanMsg->Data[7] << 8)) | pCanMsg->Data[6]);
  845. tCAN->IF[u32MsgIfNum].MCON = CAN_IF_MCON_NEWDAT_Msk | pCanMsg->DLC | CAN_IF_MCON_TXIE_Msk | CAN_IF_MCON_EOB_Msk;
  846. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u32MsgNum;
  847. ReleaseIF(tCAN, u32MsgIfNum);
  848. }
  849. return rev;
  850. }
  851. /**
  852. * @brief Set transmit request bit.
  853. *
  854. * @param[in] tCAN The pointer to CAN module base address.
  855. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  856. *
  857. * @return TRUE: Start transmit message.
  858. *
  859. * @details If a transmission is requested by programming bit TxRqst/NewDat (IFn_CMASK[2]), the TxRqst (IFn_MCON[8]) will be ignored.
  860. */
  861. int32_t CAN_TriggerTxMsg(CAN_T *tCAN, uint32_t u32MsgNum)
  862. {
  863. int32_t rev = 1l;
  864. uint32_t u32MsgIfNum;
  865. if ((u32MsgIfNum = LockIF_TL(tCAN)) == 2ul)
  866. {
  867. rev = 0; /* return FALSE */
  868. }
  869. else
  870. {
  871. tCAN->STATUS &= (~CAN_STATUS_TXOK_Msk);
  872. /* read the message contents*/
  873. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_CLRINTPND_Msk
  874. | CAN_IF_CMASK_TXRQSTNEWDAT_Msk;
  875. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u32MsgNum;
  876. while (tCAN->IF[u32MsgIfNum].CREQ & CAN_IF_CREQ_BUSY_Msk)
  877. {
  878. /*Wait*/
  879. }
  880. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_WRRD_Msk | CAN_IF_CMASK_TXRQSTNEWDAT_Msk;
  881. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u32MsgNum;
  882. ReleaseIF(tCAN, u32MsgIfNum);
  883. }
  884. return rev;
  885. }
  886. /**
  887. * @brief Enable CAN interrupt.
  888. *
  889. * @param[in] tCAN The pointer to CAN module base address.
  890. * @param[in] u32Mask Interrupt Mask. Valid values are:
  891. * - \ref CAN_CON_IE_Msk Module interrupt enable.
  892. * - \ref CAN_CON_SIE_Msk Status change interrupt enable.
  893. * - \ref CAN_CON_EIE_Msk Error interrupt enable.
  894. *
  895. * @return None
  896. *
  897. * @details The application software has two possibilities to follow the source of a message interrupt.
  898. * First, it can follow the IntId in the Interrupt Register and second it can poll the Interrupt Pending Register.
  899. */
  900. void CAN_EnableInt(CAN_T *tCAN, uint32_t u32Mask)
  901. {
  902. tCAN->CON = (tCAN->CON & ~(CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk)) |
  903. (u32Mask & (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk));
  904. }
  905. /**
  906. * @brief Disable CAN interrupt.
  907. *
  908. * @param[in] tCAN The pointer to CAN module base address.
  909. * @param[in] u32Mask Interrupt Mask. (CAN_CON_IE_Msk / CAN_CON_SIE_Msk / CAN_CON_EIE_Msk).
  910. *
  911. * @return None
  912. *
  913. * @details The interrupt remains active until the Interrupt Register is back to value zero (the cause of the interrupt is reset) or until IE is reset.
  914. */
  915. void CAN_DisableInt(CAN_T *tCAN, uint32_t u32Mask)
  916. {
  917. tCAN->CON = tCAN->CON & ~((u32Mask & (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk)));
  918. }
  919. /**
  920. * @brief The function is used to configure a receive message object.
  921. *
  922. * @param[in] tCAN The pointer to CAN module base address.
  923. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  924. * @param[in] u32IDType Specifies the identifier type of the frames that will be transmitted. Valid values are:
  925. * - \ref CAN_STD_ID The 11-bit identifier.
  926. * - \ref CAN_EXT_ID The 29-bit identifier.
  927. * @param[in] u32ID Specifies the identifier used for acceptance filtering.
  928. *
  929. * @retval FALSE No useful interface.
  930. * @retval TRUE Configure a receive message object success.
  931. *
  932. * @details If the RxIE bit (CAN_IFn_MCON[10]) is set, the IntPnd bit (CAN_IFn_MCON[13])
  933. * will be set when a received Data Frame is accepted and stored in the Message Object.
  934. */
  935. int32_t CAN_SetRxMsg(CAN_T *tCAN, uint32_t u32MsgNum, uint32_t u32IDType, uint32_t u32ID)
  936. {
  937. int32_t rev = (int32_t)TRUE;
  938. uint32_t u32TimeOutCount = 0ul;
  939. while (CAN_SetRxMsgObj(tCAN, (uint8_t)u32MsgNum, (uint8_t)u32IDType, u32ID, (uint8_t)TRUE) == (int32_t)FALSE)
  940. {
  941. if (++u32TimeOutCount >= RETRY_COUNTS)
  942. {
  943. rev = (int32_t)(FALSE); /* return FALSE */
  944. break;
  945. }
  946. else
  947. {
  948. }
  949. }
  950. return rev;
  951. }
  952. /**
  953. * @brief The function is used to configure a receive message object.
  954. *
  955. * @param[in] tCAN The pointer to CAN module base address.
  956. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  957. * @param[in] u32IDType Specifies the identifier type of the frames that will be transmitted. Valid values are:
  958. * - \ref CAN_STD_ID The 11-bit identifier.
  959. * - \ref CAN_EXT_ID The 29-bit identifier.
  960. * @param[in] u32ID Specifies the identifier used for acceptance filtering.
  961. * @param[in] u32IDMask Specifies the identifier mask used for acceptance filtering.
  962. *
  963. * @retval FALSE No useful interface.
  964. * @retval TRUE Configure a receive message object success.
  965. *
  966. * @details If the RxIE bit (CAN_IFn_MCON[10]) is set, the IntPnd bit (CAN_IFn_MCON[13])
  967. * will be set when a received Data Frame is accepted and stored in the Message Object.
  968. */
  969. int32_t CAN_SetRxMsgAndMsk(CAN_T *tCAN, uint32_t u32MsgNum, uint32_t u32IDType, uint32_t u32ID, uint32_t u32IDMask)
  970. {
  971. int32_t rev = (int32_t)TRUE;
  972. uint32_t u32TimeOutCount = 0ul;
  973. while (CAN_SetRxMsgObjAndMsk(tCAN, (uint8_t)u32MsgNum, (uint8_t)u32IDType, u32ID, u32IDMask, (uint8_t)TRUE) == (int32_t)FALSE)
  974. {
  975. if (++u32TimeOutCount >= RETRY_COUNTS)
  976. {
  977. rev = (int32_t)FALSE;
  978. break;
  979. }
  980. else
  981. {
  982. }
  983. }
  984. return rev;
  985. }
  986. /**
  987. * @brief The function is used to configure several receive message objects.
  988. *
  989. * @param[in] tCAN The pointer to CAN module base address.
  990. * @param[in] u32MsgNum The starting MSG RAM number(0 ~ 31).
  991. * @param[in] u32MsgCount the number of MSG RAM of the FIFO.
  992. * @param[in] u32IDType Specifies the identifier type of the frames that will be transmitted. Valid values are:
  993. * - \ref CAN_STD_ID The 11-bit identifier.
  994. * - \ref CAN_EXT_ID The 29-bit identifier.
  995. * @param[in] u32ID Specifies the identifier used for acceptance filtering.
  996. *
  997. * @retval FALSE No useful interface.
  998. * @retval TRUE Configure receive message objects success.
  999. *
  1000. * @details The Interface Registers avoid conflict between the CPU accesses to the Message RAM and CAN message reception
  1001. * and transmission by buffering the data to be transferred.
  1002. */
  1003. int32_t CAN_SetMultiRxMsg(CAN_T *tCAN, uint32_t u32MsgNum, uint32_t u32MsgCount, uint32_t u32IDType, uint32_t u32ID)
  1004. {
  1005. int32_t rev = (int32_t)TRUE;
  1006. uint32_t i;
  1007. uint32_t u32TimeOutCount;
  1008. uint32_t u32EOB_Flag = 0ul;
  1009. for (i = 1ul; i <= u32MsgCount; i++)
  1010. {
  1011. u32TimeOutCount = 0ul;
  1012. u32MsgNum += (i - 1ul);
  1013. if (i == u32MsgCount)
  1014. {
  1015. u32EOB_Flag = 1ul;
  1016. }
  1017. else
  1018. {
  1019. }
  1020. while (CAN_SetRxMsgObj(tCAN, (uint8_t)u32MsgNum, (uint8_t)u32IDType, u32ID, (uint8_t)u32EOB_Flag) == (int32_t)FALSE)
  1021. {
  1022. if (++u32TimeOutCount >= RETRY_COUNTS)
  1023. {
  1024. rev = (int32_t)FALSE;
  1025. break;
  1026. }
  1027. else
  1028. {
  1029. }
  1030. }
  1031. }
  1032. return rev;
  1033. }
  1034. /**
  1035. * @brief Send CAN message.
  1036. * @param[in] tCAN The pointer to CAN module base address.
  1037. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  1038. * @param[in] pCanMsg Pointer to the message structure where received data is copied.
  1039. *
  1040. * @retval FALSE 1. When operation in basic mode: Transmit message time out. \n
  1041. * 2. When operation in normal mode: No useful interface. \n
  1042. * @retval TRUE Transmit Message success.
  1043. *
  1044. * @details The receive/transmit priority for the Message Objects is attached to the message number.
  1045. * Message Object 1 has the highest priority, while Message Object 32 has the lowest priority.
  1046. */
  1047. int32_t CAN_Transmit(CAN_T *tCAN, uint32_t u32MsgNum, STR_CANMSG_T *pCanMsg)
  1048. {
  1049. int32_t rev = (int32_t)TRUE;
  1050. uint32_t u32Tmp;
  1051. u32Tmp = (tCAN->TEST & CAN_TEST_BASIC_Msk);
  1052. if ((tCAN->CON & CAN_CON_TEST_Msk) && u32Tmp)
  1053. {
  1054. rev = CAN_BasicSendMsg(tCAN, pCanMsg);
  1055. }
  1056. else
  1057. {
  1058. if (CAN_SetTxMsg(tCAN, u32MsgNum, pCanMsg) == FALSE)
  1059. {
  1060. rev = (int32_t)FALSE;
  1061. }
  1062. else
  1063. {
  1064. CAN_TriggerTxMsg(tCAN, u32MsgNum);
  1065. }
  1066. }
  1067. return rev;
  1068. }
  1069. /**
  1070. * @brief Gets the message, if received.
  1071. * @param[in] tCAN The pointer to CAN module base address.
  1072. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  1073. * @param[in] pCanMsg Pointer to the message structure where received data is copied.
  1074. *
  1075. * @retval FALSE No any message received.
  1076. * @retval TRUE Receive Message success.
  1077. *
  1078. * @details The Interface Registers avoid conflict between the CPU accesses to the Message RAM and CAN message reception
  1079. * and transmission by buffering the data to be transferred.
  1080. */
  1081. int32_t CAN_Receive(CAN_T *tCAN, uint32_t u32MsgNum, STR_CANMSG_T *pCanMsg)
  1082. {
  1083. int32_t rev = (int32_t)TRUE;
  1084. uint32_t u32Tmp;
  1085. u32Tmp = (tCAN->TEST & CAN_TEST_BASIC_Msk);
  1086. if ((tCAN->CON & CAN_CON_TEST_Msk) && u32Tmp)
  1087. {
  1088. rev = CAN_BasicReceiveMsg(tCAN, pCanMsg);
  1089. }
  1090. else
  1091. {
  1092. rev = CAN_ReadMsgObj(tCAN, (uint8_t)u32MsgNum, (uint8_t)TRUE, pCanMsg);
  1093. }
  1094. return rev;
  1095. }
  1096. /**
  1097. * @brief Clear interrupt pending bit.
  1098. * @param[in] tCAN The pointer to CAN module base address.
  1099. * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31.
  1100. *
  1101. * @return None
  1102. *
  1103. * @details An interrupt remains pending until the application software has cleared it.
  1104. */
  1105. void CAN_CLR_INT_PENDING_BIT(CAN_T *tCAN, uint8_t u32MsgNum)
  1106. {
  1107. uint32_t u32MsgIfNum;
  1108. if ((u32MsgIfNum = LockIF_TL(tCAN)) == 2ul)
  1109. {
  1110. u32MsgIfNum = 0ul;
  1111. }
  1112. else
  1113. {
  1114. }
  1115. tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_CLRINTPND_Msk | CAN_IF_CMASK_TXRQSTNEWDAT_Msk;
  1116. tCAN->IF[u32MsgIfNum].CREQ = 1ul + u32MsgNum;
  1117. ReleaseIF(tCAN, u32MsgIfNum);
  1118. }
  1119. /*@}*/ /* end of group CAN_EXPORTED_FUNCTIONS */
  1120. /*@}*/ /* end of group CAN_Driver */
  1121. /*@}*/ /* end of group Standard_Driver */
  1122. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/