drv_can.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-6-22 ChingI First version
  10. * 2022-1-8 Wayne Fix IE issue
  11. *
  12. ******************************************************************************/
  13. #include <rtconfig.h>
  14. #if defined(BSP_USING_CAN)
  15. #include <rtdevice.h>
  16. #include <rthw.h>
  17. #include "NuMicro.h"
  18. /* Private Define ---------------------------------------------------------------*/
  19. #define RX_MSG_ID_INDEX 16
  20. #define IS_CAN_STDID(STDID) ((STDID) <= 0x7FFU)
  21. #define IS_CAN_EXTID(EXTID) ((EXTID) <= 0x1FFFFFFFU)
  22. #define IS_CAN_DLC(DLC) ((DLC) <= 8U)
  23. /* Default config for serial_configure structure */
  24. #define NU_CAN_CONFIG_DEFAULT \
  25. { \
  26. CAN1MBaud, /* 1M bits/s */ \
  27. RT_CANMSG_BOX_SZ, /* message box max size */ \
  28. RT_CANSND_BOX_NUM, /* message box number */ \
  29. RT_CAN_MODE_NORMAL, /* Normal mode */ \
  30. 0, /* privmode */ \
  31. 0, /* reserved */ \
  32. 100, /* Timeout Tick */ \
  33. }
  34. enum
  35. {
  36. CAN_START = -1,
  37. #if defined(BSP_USING_CAN0)
  38. CAN0_IDX,
  39. #endif
  40. CAN_CNT
  41. };
  42. /* Private Typedef --------------------------------------------------------------*/
  43. struct nu_can
  44. {
  45. struct rt_can_device dev;
  46. char *name;
  47. CAN_T *base;
  48. IRQn_Type irqn;
  49. uint32_t rstidx;
  50. uint32_t int_flag;
  51. };
  52. typedef struct nu_can *nu_can_t;
  53. /* Private functions ------------------------------------------------------------*/
  54. static rt_err_t nu_can_configure(struct rt_can_device *can, struct can_configure *cfg);
  55. static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg);
  56. static int nu_can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
  57. static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
  58. static void nu_can_isr(nu_can_t can);
  59. static struct nu_can nu_can_arr[] =
  60. {
  61. #if defined(BSP_USING_CAN0)
  62. {
  63. .name = "can0",
  64. .base = CAN0,
  65. .rstidx = CAN0_RST,
  66. .irqn = CAN0_IRQn,
  67. },
  68. #endif
  69. }; /* struct nu_can */
  70. /* Public functions ------------------------------------------------------------*/
  71. /* Private variables ------------------------------------------------------------*/
  72. static const struct rt_can_ops nu_can_ops =
  73. {
  74. .configure = nu_can_configure,
  75. .control = nu_can_control,
  76. .sendmsg = nu_can_sendmsg,
  77. .recvmsg = nu_can_recvmsg,
  78. };
  79. static const struct can_configure nu_can_default_config = NU_CAN_CONFIG_DEFAULT;
  80. /* Interrupt Handle Function ----------------------------------------------------*/
  81. #if defined(BSP_USING_CAN0)
  82. /* CAN0 interrupt entry */
  83. void CAN0_IRQHandler(void)
  84. {
  85. /* enter interrupt */
  86. rt_interrupt_enter();
  87. nu_can_isr(&nu_can_arr[CAN0_IDX]);
  88. /* leave interrupt */
  89. rt_interrupt_leave();
  90. }
  91. #endif
  92. /* Private Variables ------------------------------------------------------------*/
  93. static void nu_can_isr(nu_can_t psNuCAN)
  94. {
  95. /* Get base address of CAN register */
  96. CAN_T *base = psNuCAN->base;
  97. /* Get interrupt event */
  98. uint32_t u32IIDRstatus = CAN_GET_INT_PENDING_STATUS(base) & CAN_IIDR_INTID_Msk;
  99. /* Check Status Interrupt Flag (Error status Int and Status change Int) */
  100. if (u32IIDRstatus == 0x00008000)
  101. {
  102. /**************************/
  103. /* Status Change interrupt*/
  104. /**************************/
  105. if (base->STATUS & CAN_STATUS_TXOK_Msk)
  106. {
  107. base->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear Tx Ok status*/
  108. #ifndef RT_CAN_USING_HDR
  109. if (psNuCAN->int_flag & RT_DEVICE_FLAG_INT_TX)
  110. {
  111. /*Using as Lisen,Loopback,Loopback+Lisen mode*/
  112. rt_hw_can_isr(&psNuCAN->dev, RT_CAN_EVENT_TX_DONE);
  113. }
  114. #endif
  115. }
  116. if (base->STATUS & CAN_STATUS_RXOK_Msk)
  117. {
  118. base->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear Rx Ok status*/
  119. #ifndef RT_CAN_USING_HDR
  120. if (psNuCAN->int_flag & RT_DEVICE_FLAG_INT_RX)
  121. {
  122. /*Using as Lisen,Loopback,Loopback+Lisen mode*/
  123. rt_hw_can_isr(&psNuCAN->dev, RT_CAN_EVENT_RX_IND);
  124. }
  125. #endif
  126. }
  127. /**************************/
  128. /* Error Status interrupt */
  129. /**************************/
  130. if (base->STATUS & CAN_STATUS_EWARN_Msk)
  131. {
  132. rt_kprintf("[%s]EWARN INT\n", psNuCAN->name) ;
  133. }
  134. if (base->STATUS & CAN_STATUS_BOFF_Msk)
  135. {
  136. rt_kprintf("[%s]BUSOFF INT\n", psNuCAN->name) ;
  137. /* To release busoff pin */
  138. CAN_EnterInitMode(base, CAN_CON_INIT_Msk | CAN_CON_CCE_Msk);
  139. CAN_LeaveInitMode(base);
  140. }
  141. if (base->STATUS & CAN_STATUS_LEC_Msk)
  142. {
  143. rt_kprintf("[%s] Last Error Code %03x\n", psNuCAN->name, base->STATUS & CAN_STATUS_LEC_Msk) ;
  144. }
  145. }
  146. #ifdef RT_CAN_USING_HDR
  147. /*IntId: 0x0001-0x0020, Number of Message Object which caused the interrupt.*/
  148. else if (u32IIDRstatus > 0 && u32IIDRstatus <= 32)
  149. {
  150. if ((psNuCAN->int_flag & RT_DEVICE_FLAG_INT_TX) &&
  151. (u32IIDRstatus <= RX_MSG_ID_INDEX))
  152. {
  153. /*Message RAM 0~RX_MSG_ID_INDEX for CAN Tx using*/
  154. rt_hw_can_isr(&psNuCAN->dev, RT_CAN_EVENT_TX_DONE);
  155. }
  156. else if (psNuCAN->int_flag & RT_DEVICE_FLAG_INT_RX)
  157. {
  158. /*Message RAM RX_MSG_ID_INDEX~31 for CAN Rx using*/
  159. rt_hw_can_isr(&psNuCAN->dev, (RT_CAN_EVENT_RX_IND | ((u32IIDRstatus - 1) << 8)));
  160. }
  161. CAN_CLR_INT_PENDING_BIT(base, (u32IIDRstatus - 1)); /* Clear Interrupt Pending */
  162. }
  163. #endif
  164. }
  165. static void nu_can_ie(nu_can_t psNuCAN)
  166. {
  167. uint32_t u32CanIE = CAN_CON_IE_Msk;
  168. if (psNuCAN->int_flag & (RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX))
  169. {
  170. u32CanIE |= CAN_CON_SIE_Msk;
  171. }
  172. else
  173. {
  174. u32CanIE &= ~CAN_CON_SIE_Msk;
  175. }
  176. if (psNuCAN->int_flag & RT_DEVICE_CAN_INT_ERR)
  177. {
  178. u32CanIE |= CAN_CON_EIE_Msk;
  179. }
  180. else
  181. {
  182. u32CanIE &= ~CAN_CON_EIE_Msk;
  183. }
  184. if (u32CanIE & (CAN_CON_SIE_Msk | CAN_CON_EIE_Msk))
  185. {
  186. CAN_EnableInt(psNuCAN->base, u32CanIE);
  187. /* Enable interrupt. */
  188. NVIC_EnableIRQ(psNuCAN->irqn);
  189. }
  190. else
  191. {
  192. u32CanIE |= (CAN_CON_IE_Msk | CAN_CON_SIE_Msk);
  193. CAN_DisableInt(psNuCAN->base, u32CanIE);
  194. /* Disable interrupt. */
  195. NVIC_DisableIRQ(psNuCAN->irqn);
  196. }
  197. }
  198. static rt_err_t nu_can_configure(struct rt_can_device *can, struct can_configure *cfg)
  199. {
  200. nu_can_t psNuCAN = (nu_can_t)can;
  201. uint32_t u32CANMode;
  202. RT_ASSERT(can);
  203. RT_ASSERT(cfg);
  204. /* Get base address of CAN register */
  205. CAN_T *base = psNuCAN->base;
  206. /* Reset this module */
  207. SYS_ResetModule(psNuCAN->rstidx);
  208. u32CANMode = (cfg->mode == RT_CAN_MODE_NORMAL) ? CAN_NORMAL_MODE : CAN_BASIC_MODE;
  209. /*Set the CAN Bit Rate and Operating mode*/
  210. if (CAN_Open(base, cfg->baud_rate, u32CANMode) != cfg->baud_rate)
  211. goto exit_nu_can_configure;
  212. switch (cfg->mode)
  213. {
  214. case RT_CAN_MODE_NORMAL:
  215. #ifdef RT_CAN_USING_HDR
  216. CAN_LeaveTestMode(base);
  217. #else
  218. CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk);
  219. #endif
  220. break;
  221. case RT_CAN_MODE_LISEN:
  222. CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk | CAN_TEST_SILENT_Msk);
  223. break;
  224. case RT_CAN_MODE_LOOPBACK:
  225. CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk | CAN_TEST_LBACK_Msk);
  226. break;
  227. case RT_CAN_MODE_LOOPBACKANLISEN:
  228. CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk | CAN_TEST_SILENT_Msk | CAN_TEST_LBACK_Msk);
  229. break;
  230. default:
  231. rt_kprintf("Unsupported Operating mode");
  232. goto exit_nu_can_configure;
  233. }
  234. nu_can_ie(psNuCAN);
  235. return RT_EOK;
  236. exit_nu_can_configure:
  237. CAN_Close(base);
  238. return -(RT_ERROR);
  239. }
  240. static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
  241. {
  242. rt_uint32_t argval = (rt_uint32_t)arg;
  243. nu_can_t psNuCAN = (nu_can_t)can;
  244. RT_ASSERT(can);
  245. switch (cmd)
  246. {
  247. case RT_DEVICE_CTRL_SET_INT:
  248. psNuCAN->int_flag |= argval;
  249. nu_can_ie(psNuCAN);
  250. break;
  251. case RT_DEVICE_CTRL_CLR_INT:
  252. psNuCAN->int_flag &= ~argval;
  253. nu_can_ie(psNuCAN);
  254. break;
  255. case RT_CAN_CMD_SET_FILTER:
  256. {
  257. struct rt_can_filter_config *filter_cfg = (struct rt_can_filter_config *)arg;
  258. for (int i = 0; i < filter_cfg->count; i++)
  259. {
  260. /*set the filter message object*/
  261. if (filter_cfg->items[i].mode == 1)
  262. {
  263. if (CAN_SetRxMsgObjAndMsk(psNuCAN->base, MSG(filter_cfg->items[i].hdr + RX_MSG_ID_INDEX), filter_cfg->items[i].ide, filter_cfg->items[i].id, filter_cfg->items[i].mask, FALSE) == FALSE)
  264. {
  265. return -(RT_ERROR);
  266. }
  267. }
  268. else
  269. {
  270. /*set the filter message object*/
  271. if (CAN_SetRxMsgAndMsk(psNuCAN->base, MSG(filter_cfg->items[i].hdr + RX_MSG_ID_INDEX), filter_cfg->items[i].ide, filter_cfg->items[i].id, filter_cfg->items[i].mask) == FALSE)
  272. {
  273. return -(RT_ERROR);
  274. }
  275. }
  276. }
  277. }
  278. break;
  279. case RT_CAN_CMD_SET_MODE:
  280. if ((argval == RT_CAN_MODE_NORMAL) ||
  281. (argval == RT_CAN_MODE_LISEN) ||
  282. (argval == RT_CAN_MODE_LOOPBACK) ||
  283. (argval == RT_CAN_MODE_LOOPBACKANLISEN))
  284. {
  285. if (argval != can->config.mode)
  286. {
  287. can->config.mode = argval;
  288. return nu_can_configure(can, &can->config);
  289. }
  290. }
  291. else
  292. {
  293. return -(RT_ERROR);
  294. }
  295. break;
  296. case RT_CAN_CMD_SET_BAUD:
  297. {
  298. if ((argval == CAN1MBaud) ||
  299. (argval == CAN800kBaud) ||
  300. (argval == CAN500kBaud) ||
  301. (argval == CAN250kBaud) ||
  302. (argval == CAN125kBaud) ||
  303. (argval == CAN100kBaud) ||
  304. (argval == CAN50kBaud) ||
  305. (argval == CAN20kBaud) ||
  306. (argval == CAN10kBaud))
  307. {
  308. if (argval != can->config.baud_rate)
  309. {
  310. can->config.baud_rate = argval;
  311. return nu_can_configure(can, &can->config);
  312. }
  313. }
  314. else
  315. {
  316. return -(RT_ERROR);
  317. }
  318. }
  319. break;
  320. case RT_CAN_CMD_SET_PRIV:
  321. if (argval != RT_CAN_MODE_PRIV &&
  322. argval != RT_CAN_MODE_NOPRIV)
  323. {
  324. return -(RT_ERROR);
  325. }
  326. if (argval != can->config.privmode)
  327. {
  328. can->config.privmode = argval;
  329. return nu_can_configure(can, &can->config);
  330. }
  331. break;
  332. case RT_CAN_CMD_GET_STATUS:
  333. {
  334. rt_uint32_t errtype = psNuCAN->base->ERR;
  335. RT_ASSERT(arg);
  336. /*Receive Error Counter, return value is with Receive Error Passive.*/
  337. can->status.rcverrcnt = (errtype >> 8);
  338. /*Transmit Error Counter*/
  339. can->status.snderrcnt = (errtype & 0xFF);
  340. /*Last Error Type*/
  341. can->status.lasterrtype = CAN_GET_INT_STATUS(psNuCAN->base) & 0x8000;
  342. /*Status error code*/
  343. can->status.errcode = CAN_GET_INT_STATUS(psNuCAN->base) & 0x07;
  344. rt_memcpy(arg, &can->status, sizeof(struct rt_can_status));
  345. }
  346. break;
  347. default:
  348. return -(RT_EINVAL);
  349. }
  350. return RT_EOK;
  351. }
  352. static int nu_can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno)
  353. {
  354. STR_CANMSG_T tMsg;
  355. struct rt_can_msg *pmsg;
  356. nu_can_t psNuCAN = (nu_can_t)can;
  357. RT_ASSERT(can);
  358. RT_ASSERT(buf);
  359. pmsg = (struct rt_can_msg *) buf;
  360. if (pmsg->ide == RT_CAN_STDID && IS_CAN_STDID(pmsg->id))
  361. {
  362. /* Standard ID (11 bits)*/
  363. tMsg.IdType = CAN_STD_ID;
  364. tMsg.Id = pmsg->id ;
  365. }
  366. else if (pmsg->ide == RT_CAN_EXTID && IS_CAN_EXTID(pmsg->id))
  367. {
  368. /* Extended ID (29 bits)*/
  369. tMsg.IdType = CAN_EXT_ID;
  370. tMsg.Id = pmsg->id ;
  371. }
  372. else
  373. {
  374. goto exit_nu_can_sendmsg;
  375. }
  376. if (pmsg->rtr == RT_CAN_DTR)
  377. {
  378. /* Data frame */
  379. tMsg.FrameType = CAN_DATA_FRAME;
  380. }
  381. else if (pmsg->rtr == RT_CAN_RTR)
  382. {
  383. /* Remote frame */
  384. tMsg.FrameType = CAN_REMOTE_FRAME;
  385. }
  386. else
  387. {
  388. goto exit_nu_can_sendmsg;
  389. }
  390. /* Check the parameters */
  391. if (IS_CAN_DLC(pmsg->len))
  392. {
  393. tMsg.DLC = pmsg->len;
  394. }
  395. else
  396. {
  397. goto exit_nu_can_sendmsg;
  398. }
  399. if (pmsg->data && pmsg->len)
  400. {
  401. rt_memcpy(&tMsg.Data[0], pmsg->data, pmsg->len);
  402. }
  403. else
  404. {
  405. goto exit_nu_can_sendmsg;
  406. }
  407. /* Configure Msg RAM and send the Msg in the RAM. */
  408. if (CAN_Transmit(psNuCAN->base, MSG(boxno), &tMsg) == FALSE)
  409. {
  410. goto exit_nu_can_sendmsg;
  411. }
  412. return RT_EOK;
  413. exit_nu_can_sendmsg:
  414. return -(RT_ERROR);
  415. }
  416. static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxno)
  417. {
  418. STR_CANMSG_T tMsg;
  419. struct rt_can_msg *pmsg;
  420. nu_can_t psNuCAN = (nu_can_t)can;
  421. RT_ASSERT(can);
  422. RT_ASSERT(buf);
  423. pmsg = (struct rt_can_msg *) buf;
  424. /* get data */
  425. if (CAN_Receive(psNuCAN->base, boxno, &tMsg) == FALSE)
  426. {
  427. rt_kprintf("No available RX Msg.\n");
  428. return -(RT_ERROR);
  429. }
  430. #ifdef RT_CAN_USING_HDR
  431. /* Hardware filter messages are valid */
  432. pmsg->hdr = boxno - RX_MSG_ID_INDEX;
  433. can->hdr[pmsg->hdr].connected = 1;
  434. #endif
  435. pmsg->ide = (tMsg.IdType == CAN_STD_ID) ? RT_CAN_STDID : RT_CAN_EXTID;
  436. pmsg->rtr = (tMsg.FrameType == CAN_DATA_FRAME) ? RT_CAN_DTR : RT_CAN_RTR;
  437. pmsg->id = tMsg.Id;
  438. pmsg->len = tMsg.DLC ;
  439. if (pmsg->data && pmsg->len)
  440. rt_memcpy(pmsg->data, &tMsg.Data[0], pmsg->len);
  441. return RT_EOK;
  442. }
  443. /**
  444. * Hardware CAN Initialization
  445. */
  446. static int rt_hw_can_init(void)
  447. {
  448. int i;
  449. rt_err_t ret = RT_EOK;
  450. for (i = (CAN_START + 1); i < CAN_CNT; i++)
  451. {
  452. nu_can_arr[i].dev.config = nu_can_default_config;
  453. #ifdef RT_CAN_USING_HDR
  454. nu_can_arr[i].dev.config.maxhdr = RT_CANMSG_BOX_SZ;
  455. #endif
  456. /* Register can device */
  457. ret = rt_hw_can_register(&nu_can_arr[i].dev, nu_can_arr[i].name, &nu_can_ops, NULL);
  458. RT_ASSERT(ret == RT_EOK);
  459. }
  460. return (int)ret;
  461. }
  462. INIT_DEVICE_EXPORT(rt_hw_can_init);
  463. #endif //#if defined(BSP_USING_CAN)