2020-12-21 14:34:01 +08:00
|
|
|
/**************************************************************************//**
|
|
|
|
*
|
|
|
|
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-12-12 Wayne First version
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <rtconfig.h>
|
|
|
|
|
|
|
|
#if defined(BSP_USING_CAN)
|
|
|
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <rthw.h>
|
2021-03-15 15:41:41 +08:00
|
|
|
#include "NuMicro.h"
|
2020-12-21 14:34:01 +08:00
|
|
|
#include <drv_sys.h>
|
|
|
|
|
|
|
|
/* Private Define ---------------------------------------------------------------*/
|
|
|
|
#define RX_MSG_ID_INDEX 16
|
|
|
|
#define IS_CAN_STDID(STDID) ((STDID) <= 0x7FFU)
|
|
|
|
#define IS_CAN_EXTID(EXTID) ((EXTID) <= 0x1FFFFFFFU)
|
|
|
|
#define IS_CAN_DLC(DLC) ((DLC) <= 8U)
|
|
|
|
|
|
|
|
/* Default config for serial_configure structure */
|
|
|
|
#define NU_CAN_CONFIG_DEFAULT \
|
|
|
|
{ \
|
|
|
|
CAN1MBaud, /* 1M bits/s */ \
|
|
|
|
RT_CANMSG_BOX_SZ, /* message box max size */ \
|
|
|
|
RT_CANSND_BOX_NUM, /* message box number */ \
|
|
|
|
RT_CAN_MODE_NORMAL, /* Normal mode */ \
|
|
|
|
0, /* privmode */ \
|
|
|
|
0, /* reserved */ \
|
|
|
|
100, /* Timeout Tick */ \
|
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
CAN_START = -1,
|
|
|
|
#if defined(BSP_USING_CAN0)
|
|
|
|
CAN0_IDX,
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN1)
|
|
|
|
CAN1_IDX,
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN2)
|
|
|
|
CAN2_IDX,
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN3)
|
|
|
|
CAN3_IDX,
|
|
|
|
#endif
|
|
|
|
CAN_CNT,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Private Typedef --------------------------------------------------------------*/
|
|
|
|
struct nu_can
|
|
|
|
{
|
|
|
|
struct rt_can_device dev;
|
|
|
|
char *name;
|
|
|
|
CAN_T *base;
|
|
|
|
IRQn_Type irqn;
|
|
|
|
E_SYS_IPRST rstidx;
|
|
|
|
E_SYS_IPCLK clkidx;
|
2022-01-16 18:43:27 +08:00
|
|
|
uint32_t int_flag;
|
2020-12-21 14:34:01 +08:00
|
|
|
};
|
|
|
|
typedef struct nu_can *nu_can_t;
|
|
|
|
|
|
|
|
/* Private functions ------------------------------------------------------------*/
|
|
|
|
static rt_err_t nu_can_configure(struct rt_can_device *can, struct can_configure *cfg);
|
|
|
|
static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg);
|
|
|
|
static int nu_can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
|
|
|
|
static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
|
|
|
|
static void nu_can_isr(int vector, void *param);
|
|
|
|
|
|
|
|
static struct nu_can nu_can_arr[] =
|
|
|
|
{
|
|
|
|
#if defined(BSP_USING_CAN0)
|
|
|
|
{
|
|
|
|
.name = "can0",
|
|
|
|
.base = CAN0,
|
|
|
|
.irqn = IRQ_CAN0,
|
|
|
|
.rstidx = CAN0RST,
|
|
|
|
.clkidx = CAN0CKEN,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN1)
|
|
|
|
{
|
|
|
|
.name = "can1",
|
|
|
|
.base = CAN1,
|
|
|
|
.irqn = IRQ_CAN1,
|
|
|
|
.rstidx = CAN1RST,
|
|
|
|
.clkidx = CAN1CKEN,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN2)
|
|
|
|
{
|
|
|
|
.name = "can2",
|
|
|
|
.base = CAN2,
|
|
|
|
.irqn = IRQ_CAN2,
|
|
|
|
.rstidx = CAN2RST,
|
|
|
|
.clkidx = CAN2CKEN,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN3)
|
|
|
|
{
|
|
|
|
.name = "can3",
|
|
|
|
.base = CAN3,
|
|
|
|
.irqn = IRQ_CAN3,
|
|
|
|
.rstidx = CAN3RST,
|
|
|
|
.clkidx = CAN3CKEN,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
}; /* struct nu_can */
|
|
|
|
|
|
|
|
/* Public functions ------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* Private variables ------------------------------------------------------------*/
|
|
|
|
static const struct rt_can_ops nu_can_ops =
|
|
|
|
{
|
|
|
|
.configure = nu_can_configure,
|
|
|
|
.control = nu_can_control,
|
|
|
|
.sendmsg = nu_can_sendmsg,
|
|
|
|
.recvmsg = nu_can_recvmsg,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct can_configure nu_can_default_config = NU_CAN_CONFIG_DEFAULT;
|
|
|
|
|
|
|
|
/* Interrupt Handle Function ----------------------------------------------------*/
|
|
|
|
static void nu_can_isr(int vector, void *param)
|
|
|
|
{
|
|
|
|
nu_can_t psNuCAN = (nu_can_t)param;
|
|
|
|
|
|
|
|
/* Get base address of CAN register */
|
|
|
|
CAN_T *base = psNuCAN->base;
|
|
|
|
|
|
|
|
/* Get interrupt event */
|
2022-01-16 18:43:27 +08:00
|
|
|
uint32_t u32IIDRstatus = CAN_GET_INT_PENDING_STATUS(base) & CAN_IIDR_INTID_Msk;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
/* Check Status Interrupt Flag (Error status Int and Status change Int) */
|
|
|
|
if (u32IIDRstatus == 0x00008000)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
|
|
|
/**************************/
|
|
|
|
/* Status Change interrupt*/
|
|
|
|
/**************************/
|
2020-12-25 18:39:12 +08:00
|
|
|
if (base->STATUS & CAN_STATUS_TXOK_Msk)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2020-12-25 18:39:12 +08:00
|
|
|
base->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear Tx Ok status*/
|
2020-12-21 14:34:01 +08:00
|
|
|
#ifndef RT_CAN_USING_HDR
|
2022-01-16 18:43:27 +08:00
|
|
|
if (psNuCAN->int_flag & RT_DEVICE_FLAG_INT_TX)
|
|
|
|
{
|
|
|
|
/*Using as Lisen,Loopback,Loopback+Lisen mode*/
|
|
|
|
rt_hw_can_isr(&psNuCAN->dev, RT_CAN_EVENT_TX_DONE);
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-25 18:39:12 +08:00
|
|
|
if (base->STATUS & CAN_STATUS_RXOK_Msk)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2020-12-25 18:39:12 +08:00
|
|
|
base->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear Rx Ok status*/
|
2020-12-21 14:34:01 +08:00
|
|
|
#ifndef RT_CAN_USING_HDR
|
2022-01-16 18:43:27 +08:00
|
|
|
if (psNuCAN->int_flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
|
|
|
/*Using as Lisen,Loopback,Loopback+Lisen mode*/
|
|
|
|
rt_hw_can_isr(&psNuCAN->dev, RT_CAN_EVENT_RX_IND);
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************/
|
|
|
|
/* Error Status interrupt */
|
|
|
|
/**************************/
|
|
|
|
if (base->STATUS & CAN_STATUS_EWARN_Msk)
|
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
rt_kprintf("[%s]EWARN INT\n", psNuCAN->name) ;
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (base->STATUS & CAN_STATUS_BOFF_Msk)
|
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
rt_kprintf("[%s]BUSOFF INT\n", psNuCAN->name) ;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
/* To release busoff pin */
|
|
|
|
CAN_EnterInitMode(base, CAN_CON_INIT_Msk | CAN_CON_CCE_Msk);
|
|
|
|
CAN_LeaveInitMode(base);
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
2020-12-25 18:39:12 +08:00
|
|
|
|
|
|
|
if (base->STATUS & CAN_STATUS_LEC_Msk)
|
|
|
|
{
|
|
|
|
rt_kprintf("[%s] Last Error Code %03x\n", psNuCAN->name, base->STATUS & CAN_STATUS_LEC_Msk) ;
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
|
|
|
#ifdef RT_CAN_USING_HDR
|
2020-12-25 18:39:12 +08:00
|
|
|
/*IntId: 0x0001-0x0020, Number of Message Object which caused the interrupt.*/
|
|
|
|
else if (u32IIDRstatus > 0 && u32IIDRstatus <= 32)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
if ((psNuCAN->int_flag & RT_DEVICE_FLAG_INT_TX) &&
|
|
|
|
(u32IIDRstatus <= RX_MSG_ID_INDEX))
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
/*Message RAM 0~RX_MSG_ID_INDEX for CAN Tx using*/
|
2020-12-21 14:34:01 +08:00
|
|
|
rt_hw_can_isr(&psNuCAN->dev, RT_CAN_EVENT_TX_DONE);
|
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else if (psNuCAN->int_flag & RT_DEVICE_FLAG_INT_RX)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
/*Message RAM RX_MSG_ID_INDEX~31 for CAN Rx using*/
|
2020-12-25 18:39:12 +08:00
|
|
|
rt_hw_can_isr(&psNuCAN->dev, (RT_CAN_EVENT_RX_IND | ((u32IIDRstatus - 1) << 8)));
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
2020-12-25 18:39:12 +08:00
|
|
|
CAN_CLR_INT_PENDING_BIT(base, (u32IIDRstatus - 1)); /* Clear Interrupt Pending */
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
static void nu_can_ie(nu_can_t psNuCAN)
|
|
|
|
{
|
|
|
|
uint32_t u32CanIE = CAN_CON_IE_Msk;
|
|
|
|
|
|
|
|
if (psNuCAN->int_flag & (RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX))
|
|
|
|
{
|
|
|
|
u32CanIE |= CAN_CON_SIE_Msk;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32CanIE &= ~CAN_CON_SIE_Msk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (psNuCAN->int_flag & RT_DEVICE_CAN_INT_ERR)
|
|
|
|
{
|
|
|
|
u32CanIE |= CAN_CON_EIE_Msk;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32CanIE &= ~CAN_CON_EIE_Msk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u32CanIE & (CAN_CON_SIE_Msk | CAN_CON_EIE_Msk))
|
|
|
|
{
|
|
|
|
CAN_EnableInt(psNuCAN->base, u32CanIE);
|
|
|
|
|
|
|
|
/* Enable interrupt. */
|
|
|
|
rt_hw_interrupt_umask(psNuCAN->irqn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32CanIE |= (CAN_CON_IE_Msk | CAN_CON_SIE_Msk);
|
|
|
|
CAN_DisableInt(psNuCAN->base, u32CanIE);
|
|
|
|
|
|
|
|
/* Disable interrupt. */
|
|
|
|
rt_hw_interrupt_mask(psNuCAN->irqn);
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
static rt_err_t nu_can_configure(struct rt_can_device *can, struct can_configure *cfg)
|
|
|
|
{
|
|
|
|
nu_can_t psNuCAN = (nu_can_t)can;
|
2022-01-16 18:43:27 +08:00
|
|
|
uint32_t u32CANMode;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
RT_ASSERT(can);
|
|
|
|
RT_ASSERT(cfg);
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
/* Get base address of CAN register */
|
|
|
|
CAN_T *base = psNuCAN->base;
|
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
/* Reset this module */
|
2020-12-21 14:34:01 +08:00
|
|
|
nu_sys_ip_reset(psNuCAN->rstidx);
|
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
u32CANMode = (cfg->mode == RT_CAN_MODE_NORMAL) ? CAN_NORMAL_MODE : CAN_BASIC_MODE;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
/*Set the CAN Bit Rate and Operating mode*/
|
|
|
|
if (CAN_Open(base, cfg->baud_rate, u32CANMode) != cfg->baud_rate)
|
|
|
|
goto exit_nu_can_configure;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
switch (cfg->mode)
|
|
|
|
{
|
|
|
|
case RT_CAN_MODE_NORMAL:
|
|
|
|
#ifdef RT_CAN_USING_HDR
|
|
|
|
CAN_LeaveTestMode(base);
|
|
|
|
#else
|
|
|
|
CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk);
|
|
|
|
#endif
|
|
|
|
break;
|
2022-08-14 10:59:28 +08:00
|
|
|
case RT_CAN_MODE_LISTEN:
|
2020-12-21 14:34:01 +08:00
|
|
|
CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk | CAN_TEST_SILENT_Msk);
|
|
|
|
break;
|
|
|
|
case RT_CAN_MODE_LOOPBACK:
|
|
|
|
CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk | CAN_TEST_LBACK_Msk);
|
|
|
|
break;
|
2022-08-14 10:59:28 +08:00
|
|
|
case RT_CAN_MODE_LOOPBACKANLISTEN:
|
2020-12-21 14:34:01 +08:00
|
|
|
CAN_EnterTestMode(base, CAN_TEST_BASIC_Msk | CAN_TEST_SILENT_Msk | CAN_TEST_LBACK_Msk);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_kprintf("Unsupported Operating mode");
|
|
|
|
goto exit_nu_can_configure;
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
nu_can_ie(psNuCAN);
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
|
|
|
|
exit_nu_can_configure:
|
|
|
|
|
|
|
|
CAN_Close(base);
|
|
|
|
|
|
|
|
return -(RT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
|
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
rt_uint32_t argval = (rt_uint32_t)arg;
|
|
|
|
nu_can_t psNuCAN = (nu_can_t)can;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
RT_ASSERT(can);
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
2022-01-16 18:43:27 +08:00
|
|
|
psNuCAN->int_flag |= argval;
|
|
|
|
nu_can_ie(psNuCAN);
|
|
|
|
break;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
psNuCAN->int_flag &= ~argval;
|
|
|
|
nu_can_ie(psNuCAN);
|
2020-12-21 14:34:01 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_CAN_CMD_SET_FILTER:
|
2022-01-16 18:43:27 +08:00
|
|
|
{
|
|
|
|
struct rt_can_filter_config *filter_cfg = (struct rt_can_filter_config *)arg;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < filter_cfg->count; i++)
|
|
|
|
{
|
|
|
|
/*set the filter message object*/
|
|
|
|
if (filter_cfg->items[i].mode == 1)
|
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
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)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
|
|
|
return -(RT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*set the filter message object*/
|
2022-01-16 18:43:27 +08:00
|
|
|
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)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
|
|
|
return -(RT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
}
|
|
|
|
break;
|
2020-12-25 18:39:12 +08:00
|
|
|
|
2020-12-21 14:34:01 +08:00
|
|
|
case RT_CAN_CMD_SET_MODE:
|
2022-01-16 18:43:27 +08:00
|
|
|
if ((argval == RT_CAN_MODE_NORMAL) ||
|
2022-08-14 10:59:28 +08:00
|
|
|
(argval == RT_CAN_MODE_LISTEN) ||
|
2022-01-16 18:43:27 +08:00
|
|
|
(argval == RT_CAN_MODE_LOOPBACK) ||
|
2022-08-14 10:59:28 +08:00
|
|
|
(argval == RT_CAN_MODE_LOOPBACKANLISTEN))
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
if (argval != can->config.mode)
|
|
|
|
{
|
|
|
|
can->config.mode = argval;
|
|
|
|
return nu_can_configure(can, &can->config);
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
return -(RT_ERROR);
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_CAN_CMD_SET_BAUD:
|
2022-01-16 18:43:27 +08:00
|
|
|
{
|
|
|
|
if ((argval == CAN1MBaud) ||
|
|
|
|
(argval == CAN800kBaud) ||
|
|
|
|
(argval == CAN500kBaud) ||
|
|
|
|
(argval == CAN250kBaud) ||
|
|
|
|
(argval == CAN125kBaud) ||
|
|
|
|
(argval == CAN100kBaud) ||
|
|
|
|
(argval == CAN50kBaud) ||
|
|
|
|
(argval == CAN20kBaud) ||
|
|
|
|
(argval == CAN10kBaud))
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
if (argval != can->config.baud_rate)
|
|
|
|
{
|
|
|
|
can->config.baud_rate = argval;
|
|
|
|
return nu_can_configure(can, &can->config);
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
return -(RT_ERROR);
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
}
|
|
|
|
break;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
case RT_CAN_CMD_SET_PRIV:
|
2022-01-16 18:43:27 +08:00
|
|
|
if (argval != RT_CAN_MODE_PRIV &&
|
|
|
|
argval != RT_CAN_MODE_NOPRIV)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
|
|
|
return -(RT_ERROR);
|
|
|
|
}
|
|
|
|
if (argval != can->config.privmode)
|
|
|
|
{
|
|
|
|
can->config.privmode = argval;
|
|
|
|
return nu_can_configure(can, &can->config);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_CAN_CMD_GET_STATUS:
|
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
rt_uint32_t errtype = psNuCAN->base->ERR;
|
|
|
|
|
|
|
|
RT_ASSERT(arg);
|
|
|
|
|
|
|
|
/*Receive Error Counter, return value is with Receive Error Passive.*/
|
2020-12-21 14:34:01 +08:00
|
|
|
can->status.rcverrcnt = (errtype >> 8);
|
2022-01-16 18:43:27 +08:00
|
|
|
|
2020-12-21 14:34:01 +08:00
|
|
|
/*Transmit Error Counter*/
|
2022-01-16 18:43:27 +08:00
|
|
|
can->status.snderrcnt = (errtype & 0xFF);
|
|
|
|
|
|
|
|
/*Last Error Type*/
|
|
|
|
can->status.lasterrtype = CAN_GET_INT_STATUS(psNuCAN->base) & 0x8000;
|
|
|
|
|
|
|
|
/*Status error code*/
|
|
|
|
can->status.errcode = CAN_GET_INT_STATUS(psNuCAN->base) & 0x07;
|
|
|
|
|
|
|
|
rt_memcpy(arg, &can->status, sizeof(struct rt_can_status));
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -(RT_EINVAL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nu_can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno)
|
|
|
|
{
|
|
|
|
STR_CANMSG_T tMsg;
|
2022-01-16 18:43:27 +08:00
|
|
|
struct rt_can_msg *pmsg;
|
|
|
|
nu_can_t psNuCAN = (nu_can_t)can;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
RT_ASSERT(can);
|
|
|
|
RT_ASSERT(buf);
|
2020-12-25 18:39:12 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
pmsg = (struct rt_can_msg *) buf;
|
2020-12-25 18:39:12 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
if (pmsg->ide == RT_CAN_STDID && IS_CAN_STDID(pmsg->id))
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
/* Standard ID (11 bits)*/
|
2020-12-21 14:34:01 +08:00
|
|
|
tMsg.IdType = CAN_STD_ID;
|
|
|
|
tMsg.Id = pmsg->id ;
|
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else if (pmsg->ide == RT_CAN_EXTID && IS_CAN_EXTID(pmsg->id))
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
|
|
|
/* Extended ID (29 bits)*/
|
|
|
|
tMsg.IdType = CAN_EXT_ID;
|
|
|
|
tMsg.Id = pmsg->id ;
|
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
goto exit_nu_can_sendmsg;
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
if (pmsg->rtr == RT_CAN_DTR)
|
|
|
|
{
|
|
|
|
/* Data frame */
|
|
|
|
tMsg.FrameType = CAN_DATA_FRAME;
|
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else if (pmsg->rtr == RT_CAN_RTR)
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
|
|
|
/* Remote frame */
|
|
|
|
tMsg.FrameType = CAN_REMOTE_FRAME;
|
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
goto exit_nu_can_sendmsg;
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
/* Check the parameters */
|
|
|
|
if (IS_CAN_DLC(pmsg->len))
|
2020-12-21 14:34:01 +08:00
|
|
|
{
|
2022-01-16 18:43:27 +08:00
|
|
|
tMsg.DLC = pmsg->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto exit_nu_can_sendmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pmsg->data && pmsg->len)
|
|
|
|
{
|
|
|
|
rt_memcpy(&tMsg.Data[0], pmsg->data, pmsg->len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto exit_nu_can_sendmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure Msg RAM and send the Msg in the RAM. */
|
|
|
|
if (CAN_Transmit(psNuCAN->base, MSG(boxno), &tMsg) == FALSE)
|
|
|
|
{
|
|
|
|
goto exit_nu_can_sendmsg;
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2022-01-16 18:43:27 +08:00
|
|
|
|
|
|
|
exit_nu_can_sendmsg:
|
|
|
|
|
|
|
|
return -(RT_ERROR);
|
2020-12-21 14:34:01 +08:00
|
|
|
}
|
2022-01-16 18:43:27 +08:00
|
|
|
|
2020-12-21 14:34:01 +08:00
|
|
|
static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxno)
|
|
|
|
{
|
|
|
|
STR_CANMSG_T tMsg;
|
2022-01-16 18:43:27 +08:00
|
|
|
struct rt_can_msg *pmsg;
|
|
|
|
nu_can_t psNuCAN = (nu_can_t)can;
|
|
|
|
|
|
|
|
RT_ASSERT(can);
|
|
|
|
RT_ASSERT(buf);
|
2020-12-21 14:34:01 +08:00
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
pmsg = (struct rt_can_msg *) buf;
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
/* get data */
|
2022-01-16 18:43:27 +08:00
|
|
|
if (CAN_Receive(psNuCAN->base, boxno, &tMsg) == FALSE)
|
2020-12-25 18:39:12 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("No available RX Msg.\n");
|
|
|
|
return -(RT_ERROR);
|
|
|
|
}
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
#ifdef RT_CAN_USING_HDR
|
|
|
|
/* Hardware filter messages are valid */
|
|
|
|
pmsg->hdr = boxno - RX_MSG_ID_INDEX;
|
|
|
|
can->hdr[pmsg->hdr].connected = 1;
|
|
|
|
#endif
|
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
pmsg->ide = (tMsg.IdType == CAN_STD_ID) ? RT_CAN_STDID : RT_CAN_EXTID;
|
|
|
|
pmsg->rtr = (tMsg.FrameType == CAN_DATA_FRAME) ? RT_CAN_DTR : RT_CAN_RTR;
|
|
|
|
pmsg->id = tMsg.Id;
|
2020-12-21 14:34:01 +08:00
|
|
|
pmsg->len = tMsg.DLC ;
|
|
|
|
|
2022-01-16 18:43:27 +08:00
|
|
|
if (pmsg->data && pmsg->len)
|
|
|
|
rt_memcpy(pmsg->data, &tMsg.Data[0], pmsg->len);
|
2020-12-21 14:34:01 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hardware CAN Initialization
|
|
|
|
*/
|
|
|
|
static int rt_hw_can_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
|
|
|
|
|
for (i = (CAN_START + 1); i < CAN_CNT; i++)
|
|
|
|
{
|
|
|
|
nu_can_arr[i].dev.config = nu_can_default_config;
|
|
|
|
|
|
|
|
#ifdef RT_CAN_USING_HDR
|
|
|
|
nu_can_arr[i].dev.config.maxhdr = RT_CANMSG_BOX_SZ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Register CAN ISR */
|
|
|
|
rt_hw_interrupt_install(nu_can_arr[i].irqn, nu_can_isr, &nu_can_arr[i], nu_can_arr[i].name);
|
|
|
|
|
|
|
|
/* Enable IP engine clock */
|
|
|
|
nu_sys_ipclk_enable(nu_can_arr[i].clkidx);
|
|
|
|
|
|
|
|
/* Register can device */
|
|
|
|
ret = rt_hw_can_register(&nu_can_arr[i].dev, nu_can_arr[i].name, &nu_can_ops, NULL);
|
|
|
|
RT_ASSERT(ret == RT_EOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)ret;
|
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_can_init);
|
|
|
|
#endif //#if defined(BSP_USING_CAN)
|