2022-05-06 09:28:21 +08:00
|
|
|
/*
|
2022-05-31 11:53:56 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
|
* Copyright (c) 2022, Xiaohua Semiconductor Co., Ltd.
|
2022-05-06 09:28:21 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
2022-06-08 12:10:58 +08:00
|
|
|
* Date Author Notes
|
|
|
|
* 2022-04-28 CDT first version
|
|
|
|
* 2022-06-07 xiaoxiaolisunny add hc32f460 series
|
2022-06-15 10:37:27 +08:00
|
|
|
* 2022-06-08 CDT fix a bug of RT_CAN_CMD_SET_FILTER
|
2022-05-06 09:28:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "drv_can.h"
|
|
|
|
#include <drv_config.h>
|
|
|
|
#include <board_config.h>
|
|
|
|
|
|
|
|
#ifdef BSP_USING_CAN
|
|
|
|
#define LOG_TAG "drv_can"
|
|
|
|
|
|
|
|
#if !defined(BSP_USING_CAN1) && !defined(BSP_USING_CAN2)
|
|
|
|
#error "Please define at least one BSP_USING_CANx"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (HC32F4A0)
|
2022-05-15 20:57:35 +08:00
|
|
|
#define FILTER_COUNT (16)
|
|
|
|
#define CAN1_INT_SRC (INT_SRC_CAN1_HOST)
|
|
|
|
#define CAN2_INT_SRC (INT_SRC_CAN2_HOST)
|
2022-05-06 09:28:21 +08:00
|
|
|
#endif
|
|
|
|
|
2022-06-08 12:10:58 +08:00
|
|
|
#if defined (HC32F460)
|
|
|
|
#define FILTER_COUNT (16)
|
|
|
|
#define CAN1_INT_SRC (INT_SRC_CAN_INT)
|
|
|
|
#endif
|
|
|
|
|
2022-05-06 09:28:21 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_CAN1
|
|
|
|
CAN1_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_CAN2
|
|
|
|
CAN2_INDEX,
|
|
|
|
#endif
|
|
|
|
CAN_INDEX_MAX,
|
|
|
|
};
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
struct can_baud_rate_tab
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
rt_uint32_t baud_rate;
|
|
|
|
stc_can_bit_time_config_t ll_sbt;
|
|
|
|
};
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static const struct can_baud_rate_tab g_baudrate_tab[] =
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
{CAN1MBaud, CAN_BIT_TIME_CONFIG_1M_BAUD},
|
|
|
|
{CAN800kBaud, CAN_BIT_TIME_CONFIG_800K_BAUD},
|
|
|
|
{CAN500kBaud, CAN_BIT_TIME_CONFIG_500K_BAUD},
|
|
|
|
{CAN250kBaud, CAN_BIT_TIME_CONFIG_250K_BAUD},
|
|
|
|
{CAN125kBaud, CAN_BIT_TIME_CONFIG_125K_BAUD},
|
|
|
|
{CAN100kBaud, CAN_BIT_TIME_CONFIG_100K_BAUD},
|
|
|
|
{CAN50kBaud, CAN_BIT_TIME_CONFIG_50K_BAUD},
|
|
|
|
{CAN20kBaud, CAN_BIT_TIME_CONFIG_20K_BAUD},
|
|
|
|
{CAN10kBaud, CAN_BIT_TIME_CONFIG_10K_BAUD},
|
2022-05-06 09:28:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct rt_can_device rt_can;
|
2022-05-15 20:57:35 +08:00
|
|
|
struct can_dev_init_params init;
|
2022-05-06 09:28:21 +08:00
|
|
|
CM_CAN_TypeDef *instance;
|
|
|
|
stc_can_init_t ll_init;
|
2022-05-15 20:57:35 +08:00
|
|
|
} can_device;
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static can_device g_can_dev_array[] =
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-06-08 12:10:58 +08:00
|
|
|
#if defined (HC32F4A0)
|
2022-05-06 09:28:21 +08:00
|
|
|
#ifdef BSP_USING_CAN1
|
|
|
|
{
|
|
|
|
{0},
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN1_INIT_PARAMS,
|
2022-05-06 09:28:21 +08:00
|
|
|
.instance = CM_CAN1,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_CAN2
|
|
|
|
{
|
|
|
|
{0},
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN2_INIT_PARAMS,
|
2022-05-06 09:28:21 +08:00
|
|
|
.instance = CM_CAN2,
|
|
|
|
},
|
|
|
|
#endif
|
2022-06-08 12:10:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (HC32F460)
|
|
|
|
#ifdef BSP_USING_CAN1
|
|
|
|
{
|
|
|
|
{0},
|
|
|
|
CAN1_INIT_PARAMS,
|
|
|
|
.instance = CM_CAN,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-05-06 09:28:21 +08:00
|
|
|
};
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static rt_uint32_t _get_can_baud_index(rt_uint32_t baud)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
rt_uint32_t len, index;
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
len = sizeof(g_baudrate_tab) / sizeof(g_baudrate_tab[0]);
|
2022-05-06 09:28:21 +08:00
|
|
|
for (index = 0; index < len; index++)
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
if (g_baudrate_tab[index].baud_rate == baud)
|
2022-05-06 09:28:21 +08:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* default baud is CAN1MBaud */
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static rt_uint32_t _get_can_work_mode(rt_uint32_t mode)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
rt_uint32_t work_mode;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case RT_CAN_MODE_NORMAL:
|
|
|
|
work_mode = CAN_WORK_MD_NORMAL;
|
|
|
|
break;
|
|
|
|
case RT_CAN_MODE_LISEN:
|
|
|
|
work_mode = CAN_WORK_MD_SILENT;
|
|
|
|
break;
|
|
|
|
case RT_CAN_MODE_LOOPBACK:
|
|
|
|
work_mode = CAN_WORK_MD_ELB;
|
|
|
|
break;
|
|
|
|
case RT_CAN_MODE_LOOPBACKANLISEN:
|
|
|
|
work_mode = CAN_WORK_MD_ELB_SILENT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
work_mode = CAN_WORK_MD_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return work_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg)
|
|
|
|
{
|
|
|
|
rt_uint32_t baud_index;
|
2022-05-15 20:57:35 +08:00
|
|
|
can_device *p_can_dev;
|
2022-05-06 09:28:21 +08:00
|
|
|
rt_err_t rt_ret = RT_EOK;
|
|
|
|
|
|
|
|
RT_ASSERT(can);
|
|
|
|
RT_ASSERT(cfg);
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev = (can_device *)rt_container_of(can, can_device, rt_can);
|
|
|
|
RT_ASSERT(p_can_dev);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev->ll_init.u8WorkMode = _get_can_work_mode(cfg->mode);
|
|
|
|
baud_index = _get_can_baud_index(cfg->baud_rate);
|
|
|
|
p_can_dev->ll_init.stcBitCfg = g_baudrate_tab[baud_index].ll_sbt;
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
/* init can */
|
2022-05-15 20:57:35 +08:00
|
|
|
int32_t ret = CAN_Init(p_can_dev->instance, &p_can_dev->ll_init);
|
2022-05-06 09:28:21 +08:00
|
|
|
if (ret != LL_OK)
|
|
|
|
{
|
|
|
|
rt_ret = RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt_ret;
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static uint16_t _get_filter_idx(struct rt_can_filter_config *filter_cfg)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
uint16_t u16FilterSelected = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < filter_cfg->count; i++)
|
|
|
|
{
|
|
|
|
if (filter_cfg->items[i].hdr != -1)
|
|
|
|
{
|
|
|
|
u16FilterSelected |= 1 << filter_cfg->items[i].hdr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < filter_cfg->count; i++)
|
|
|
|
{
|
|
|
|
if (filter_cfg->items[i].hdr == -1)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < FILTER_COUNT; j++)
|
|
|
|
{
|
|
|
|
if ((u16FilterSelected & 1 << j) == 0)
|
|
|
|
{
|
|
|
|
filter_cfg->items[i].hdr = j;
|
|
|
|
u16FilterSelected |= 1 << filter_cfg->items[i].hdr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return u16FilterSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
can_device *p_can_dev;
|
2022-05-06 09:28:21 +08:00
|
|
|
rt_uint32_t argval;
|
|
|
|
struct rt_can_filter_config *filter_cfg;
|
|
|
|
|
|
|
|
RT_ASSERT(can != RT_NULL);
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev = (can_device *)rt_container_of(can, can_device, rt_can);
|
|
|
|
RT_ASSERT(p_can_dev);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
argval = (rt_uint32_t) arg;
|
|
|
|
switch (argval)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_FLAG_INT_RX:
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX_BUF_WARN, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX_BUF_FULL, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX_OVERRUN, DISABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_FLAG_INT_TX:
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_STB_TX, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_PTB_TX, DISABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_CAN_INT_ERR:
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_ERR_INT, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_ARBITR_LOST, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_ERR_PASSIVE, DISABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_BUS_ERR, DISABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
argval = (rt_uint32_t) arg;
|
|
|
|
switch (argval)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_FLAG_INT_RX:
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX_BUF_WARN, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX_BUF_FULL, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_RX_OVERRUN, ENABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_FLAG_INT_TX:
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_STB_TX, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_FLAG_PTB_TX, ENABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_CAN_INT_ERR:
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_ERR_INT, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_ARBITR_LOST, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_ERR_PASSIVE, ENABLE);
|
|
|
|
CAN_IntCmd(p_can_dev->instance, CAN_INT_BUS_ERR, ENABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RT_CAN_CMD_SET_FILTER:
|
|
|
|
if (RT_NULL != arg)
|
|
|
|
{
|
|
|
|
filter_cfg = (struct rt_can_filter_config *)arg;
|
|
|
|
|
|
|
|
if (filter_cfg->count == 0)
|
|
|
|
{
|
|
|
|
return -RT_EINVAL;
|
|
|
|
}
|
|
|
|
RT_ASSERT(filter_cfg->count <= FILTER_COUNT);
|
|
|
|
|
|
|
|
/* get default filter */
|
2022-05-15 20:57:35 +08:00
|
|
|
if (p_can_dev->ll_init.pstcFilter)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev->ll_init.u16FilterSelect = _get_filter_idx(filter_cfg);
|
2022-05-06 09:28:21 +08:00
|
|
|
for (int i = 0; i < filter_cfg->count; i++)
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev->ll_init.pstcFilter[i].u32ID = filter_cfg->items[i].id;
|
|
|
|
p_can_dev->ll_init.pstcFilter[i].u32IDMask = filter_cfg->items[i].mask;
|
2022-06-15 10:37:27 +08:00
|
|
|
if (filter_cfg->items[i].ide == RT_CAN_STDID)
|
|
|
|
{
|
|
|
|
p_can_dev->ll_init.pstcFilter[i].u32IDType = CAN_ID_STD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p_can_dev->ll_init.pstcFilter[i].u32IDType = CAN_ID_EXT;
|
|
|
|
}
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
(void)CAN_Init(p_can_dev->instance, &p_can_dev->ll_init);
|
2022-05-06 09:28:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RT_CAN_CMD_SET_MODE:
|
|
|
|
argval = (rt_uint32_t) arg;
|
|
|
|
if (argval != RT_CAN_MODE_NORMAL &&
|
|
|
|
argval != RT_CAN_MODE_LISEN &&
|
|
|
|
argval != RT_CAN_MODE_LOOPBACK &&
|
|
|
|
argval != RT_CAN_MODE_LOOPBACKANLISEN)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
if (argval != p_can_dev->rt_can.config.mode)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev->rt_can.config.mode = argval;
|
|
|
|
_can_config(can, &p_can_dev->rt_can.config);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RT_CAN_CMD_SET_BAUD:
|
|
|
|
argval = (rt_uint32_t) arg;
|
|
|
|
if (argval != CAN1MBaud &&
|
|
|
|
argval != CAN800kBaud &&
|
|
|
|
argval != CAN500kBaud &&
|
|
|
|
argval != CAN250kBaud &&
|
|
|
|
argval != CAN125kBaud &&
|
|
|
|
argval != CAN100kBaud &&
|
|
|
|
argval != CAN50kBaud &&
|
|
|
|
argval != CAN20kBaud &&
|
|
|
|
argval != CAN10kBaud)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
if (argval != p_can_dev->rt_can.config.baud_rate)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev->rt_can.config.baud_rate = argval;
|
|
|
|
_can_config(can, &p_can_dev->rt_can.config);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RT_CAN_CMD_SET_PRIV:
|
|
|
|
argval = (rt_uint32_t) arg;
|
|
|
|
if (argval != RT_CAN_MODE_PRIV &&
|
|
|
|
argval != RT_CAN_MODE_NOPRIV)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
if (argval != p_can_dev->rt_can.config.privmode)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
p_can_dev->rt_can.config.privmode = argval;
|
|
|
|
return _can_config(can, &p_can_dev->rt_can.config);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RT_CAN_CMD_GET_STATUS:
|
|
|
|
{
|
|
|
|
struct rt_can_status *rt_can_stat = (struct rt_can_status *)arg;
|
|
|
|
stc_can_error_info_t stcErr = {0};
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_GetErrorInfo(p_can_dev->instance, &stcErr);
|
2022-05-06 09:28:21 +08:00
|
|
|
rt_can_stat->rcverrcnt = stcErr.u8RxErrorCount;
|
|
|
|
rt_can_stat->snderrcnt = stcErr.u8TxErrorCount;
|
|
|
|
rt_can_stat->lasterrtype = stcErr.u8ErrorType;
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_can_stat->errcode = CAN_GetStatusValue(p_can_dev->instance);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num)
|
|
|
|
{
|
|
|
|
struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
|
|
|
|
stc_can_tx_frame_t stc_tx_frame = {0};
|
|
|
|
int32_t ll_ret;
|
|
|
|
|
|
|
|
RT_ASSERT(can != RT_NULL);
|
2022-05-15 20:57:35 +08:00
|
|
|
can_device *p_can_dev = (can_device *)rt_container_of(can, can_device, rt_can);
|
|
|
|
RT_ASSERT(p_can_dev);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
stc_tx_frame.u32ID = pmsg->id;
|
|
|
|
if (RT_CAN_DTR == pmsg->rtr)
|
|
|
|
{
|
|
|
|
stc_tx_frame.RTR = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stc_tx_frame.RTR = 1;
|
|
|
|
}
|
|
|
|
/* Set up the DLC */
|
|
|
|
stc_tx_frame.DLC = pmsg->len & 0x0FU;
|
2022-06-08 12:10:58 +08:00
|
|
|
/* Set up the IDE */
|
|
|
|
stc_tx_frame.IDE = pmsg->ide;
|
2022-05-06 09:28:21 +08:00
|
|
|
/* Set up the data field */
|
|
|
|
rt_memcpy(&stc_tx_frame.au8Data, pmsg->data, sizeof(stc_tx_frame.au8Data));
|
2022-05-15 20:57:35 +08:00
|
|
|
ll_ret = CAN_FillTxFrame(p_can_dev->instance, CAN_TX_BUF_PTB, &stc_tx_frame);
|
2022-05-06 09:28:21 +08:00
|
|
|
if (ll_ret != LL_OK)
|
|
|
|
{
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
/* Request transmission */
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_StartTx(p_can_dev->instance, CAN_TX_REQ_PTB);
|
|
|
|
|
2022-05-06 09:28:21 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo)
|
|
|
|
{
|
|
|
|
int32_t ll_ret;
|
|
|
|
struct rt_can_msg *pmsg;
|
|
|
|
stc_can_rx_frame_t ll_rx_frame;
|
|
|
|
|
|
|
|
RT_ASSERT(can != RT_NULL);
|
2022-05-15 20:57:35 +08:00
|
|
|
can_device *p_can_dev = (can_device *)rt_container_of(can, can_device, rt_can);
|
|
|
|
RT_ASSERT(p_can_dev);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
pmsg = (struct rt_can_msg *) buf;
|
|
|
|
/* get data */
|
2022-05-15 20:57:35 +08:00
|
|
|
ll_ret = CAN_GetRxFrame(p_can_dev->instance, &ll_rx_frame);
|
2022-05-06 09:28:21 +08:00
|
|
|
if (ll_ret != LL_OK)
|
|
|
|
return -RT_ERROR;
|
2022-05-15 20:57:35 +08:00
|
|
|
|
2022-05-06 09:28:21 +08:00
|
|
|
/* get id */
|
2022-06-08 12:10:58 +08:00
|
|
|
if (0 == ll_rx_frame.IDE)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
pmsg->ide = RT_CAN_STDID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pmsg->ide = RT_CAN_EXTID;
|
|
|
|
}
|
|
|
|
pmsg->id = ll_rx_frame.u32ID;
|
|
|
|
/* get type */
|
|
|
|
if (0 == ll_rx_frame.RTR)
|
|
|
|
{
|
|
|
|
pmsg->rtr = RT_CAN_DTR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pmsg->rtr = RT_CAN_RTR;
|
|
|
|
}
|
|
|
|
/* get len */
|
|
|
|
pmsg->len = ll_rx_frame.DLC;
|
|
|
|
/* get hdr */
|
|
|
|
pmsg->hdr = 0;
|
|
|
|
rt_memcpy(pmsg->data, &ll_rx_frame.au8Data, ll_rx_frame.DLC);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_can_ops _can_ops =
|
|
|
|
{
|
|
|
|
_can_config,
|
|
|
|
_can_control,
|
|
|
|
_can_sendmsg,
|
|
|
|
_can_recvmsg,
|
|
|
|
};
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _can_isr(can_device *p_can_dev)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
stc_can_error_info_t stcErr;
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
(void)CAN_GetErrorInfo(p_can_dev->instance, &stcErr);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_BUS_OFF) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* BUS OFF. */
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_RX_BUF_OVF) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* RX overflow. */
|
|
|
|
rt_hw_can_isr(&p_can_dev->rt_can, RT_CAN_EVENT_RXOF_IND);
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_RX_BUF_OVF);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_TX_BUF_FULL) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* TX buffer full. */
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_TX_ABORTED) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* TX aborted. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_TX_ABORTED);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_ARBITR_LOST) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_hw_can_isr(&p_can_dev->rt_can, RT_CAN_EVENT_TX_FAIL);
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_ARBITR_LOST);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_STB_TX) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* STB transmitted. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_STB_TX);
|
|
|
|
rt_hw_can_isr(&p_can_dev->rt_can, RT_CAN_EVENT_TX_DONE);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_PTB_TX) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* PTB transmitted. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_PTB_TX);
|
|
|
|
rt_hw_can_isr(&p_can_dev->rt_can, RT_CAN_EVENT_TX_DONE);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_RX) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* Received a frame. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_RX);
|
|
|
|
rt_hw_can_isr(&p_can_dev->rt_can, RT_CAN_EVENT_RX_IND);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_RX_BUF_WARN) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* RX buffer warning. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_RX_BUF_WARN);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_RX_BUF_FULL) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* RX buffer full. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_RX_BUF_FULL);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_RX_OVERRUN) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* RX buffer overrun. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_RX_OVERRUN);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_GetStatus(p_can_dev->instance, CAN_FLAG_TEC_REC_WARN) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* TEC or REC reached warning limit. */
|
|
|
|
CAN_ClearStatus(p_can_dev->instance, CAN_FLAG_TEC_REC_WARN);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_TTC_GetStatus(p_can_dev->instance, CAN_TTC_FLAG_TIME_TRIG) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* Time trigger interrupt. */
|
|
|
|
CAN_TTC_ClearStatus(p_can_dev->instance, CAN_TTC_FLAG_TIME_TRIG);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_TTC_GetStatus(p_can_dev->instance, CAN_TTC_FLAG_TRIG_ERR) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* Trigger error interrupt. */
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
if (CAN_TTC_GetStatus(p_can_dev->instance, CAN_TTC_FLAG_WATCH_TRIG) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
/* Watch trigger interrupt. */
|
|
|
|
CAN_TTC_ClearStatus(p_can_dev->instance, CAN_TTC_FLAG_WATCH_TRIG);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined(BSP_USING_CAN1)
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _can1_irq_handler(void)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
2022-05-15 20:57:35 +08:00
|
|
|
_can_isr(&g_can_dev_array[CAN1_INDEX]);
|
2022-05-06 09:28:21 +08:00
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(BSP_USING_CAN2)
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _can2_irq_handler(void)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
2022-05-15 20:57:35 +08:00
|
|
|
_can_isr(&g_can_dev_array[CAN2_INDEX]);
|
2022-05-06 09:28:21 +08:00
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _can_clock_enable(void)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
#if defined(HC32F4A0)
|
|
|
|
#if defined(BSP_USING_CAN1)
|
|
|
|
FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_CAN1, ENABLE);
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN2)
|
|
|
|
FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_CAN2, ENABLE);
|
2022-05-06 09:28:21 +08:00
|
|
|
#endif
|
2022-05-15 20:57:35 +08:00
|
|
|
#endif
|
2022-06-08 12:10:58 +08:00
|
|
|
|
|
|
|
#if defined(HC32F460)
|
|
|
|
#if defined(BSP_USING_CAN1)
|
|
|
|
FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_CAN, ENABLE);
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-05-15 20:57:35 +08:00
|
|
|
}
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _can_irq_config(void)
|
|
|
|
{
|
2022-05-06 09:28:21 +08:00
|
|
|
struct hc32_irq_config irq_config;
|
|
|
|
#if defined(BSP_USING_CAN1)
|
2022-05-15 20:57:35 +08:00
|
|
|
irq_config.irq_num = BSP_CAN1_IRQ_NUM;
|
2022-05-06 09:28:21 +08:00
|
|
|
irq_config.int_src = CAN1_INT_SRC;
|
2022-05-15 20:57:35 +08:00
|
|
|
irq_config.irq_prio = BSP_CAN1_IRQ_PRIO;
|
2022-05-06 09:28:21 +08:00
|
|
|
/* register interrupt */
|
|
|
|
hc32_install_irq_handler(&irq_config,
|
2022-05-15 20:57:35 +08:00
|
|
|
_can1_irq_handler,
|
2022-05-06 09:28:21 +08:00
|
|
|
RT_TRUE);
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_CAN2)
|
2022-05-15 20:57:35 +08:00
|
|
|
irq_config.irq_num = BSP_CAN2_IRQ_NUM;
|
2022-05-06 09:28:21 +08:00
|
|
|
irq_config.int_src = CAN2_INT_SRC;
|
2022-05-15 20:57:35 +08:00
|
|
|
irq_config.irq_prio = BSP_CAN2_IRQ_PRIO;
|
2022-05-06 09:28:21 +08:00
|
|
|
/* register interrupt */
|
|
|
|
hc32_install_irq_handler(&irq_config,
|
2022-05-15 20:57:35 +08:00
|
|
|
_can2_irq_handler,
|
2022-05-06 09:28:21 +08:00
|
|
|
RT_TRUE);
|
|
|
|
#endif
|
2022-05-15 20:57:35 +08:00
|
|
|
}
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
extern rt_err_t rt_hw_board_can_init(CM_CAN_TypeDef *CANx);
|
|
|
|
extern void CanPhyEnable(void);
|
|
|
|
int rt_hw_can_init(void)
|
|
|
|
{
|
|
|
|
struct can_configure rt_can_config = CANDEFAULTCONFIG;
|
|
|
|
rt_can_config.privmode = RT_CAN_MODE_NOPRIV;
|
|
|
|
rt_can_config.ticks = 50;
|
|
|
|
#ifdef RT_CAN_USING_HDR
|
|
|
|
rt_can_config.maxhdr = FILTER_COUNT;
|
2022-05-06 09:28:21 +08:00
|
|
|
#endif
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
_can_irq_config();
|
|
|
|
_can_clock_enable();
|
2022-05-06 09:28:21 +08:00
|
|
|
CanPhyEnable();
|
|
|
|
int result = RT_EOK;
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (; i < CAN_INDEX_MAX; i++)
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
CAN_StructInit(&g_can_dev_array[i].ll_init);
|
|
|
|
if (g_can_dev_array[i].ll_init.pstcFilter == RT_NULL)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
g_can_dev_array[i].ll_init.pstcFilter = (stc_can_filter_config_t *)rt_malloc(sizeof(stc_can_filter_config_t) * FILTER_COUNT);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
RT_ASSERT((g_can_dev_array[i].ll_init.pstcFilter != RT_NULL));
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_memset(g_can_dev_array[i].ll_init.pstcFilter, 0, sizeof(stc_can_filter_config_t) * FILTER_COUNT);
|
|
|
|
g_can_dev_array[i].ll_init.pstcFilter[0].u32ID = 0U;
|
|
|
|
g_can_dev_array[i].ll_init.pstcFilter[0].u32IDMask = 0x1FFFFFFF;
|
2022-06-08 12:10:58 +08:00
|
|
|
g_can_dev_array[i].ll_init.pstcFilter[0].u32IDType = CAN_ID_STD_EXT;
|
2022-05-15 20:57:35 +08:00
|
|
|
g_can_dev_array[i].ll_init.u16FilterSelect = CAN_FILTER1;
|
|
|
|
g_can_dev_array[i].rt_can.config = rt_can_config;
|
2022-05-06 09:28:21 +08:00
|
|
|
|
2022-06-15 10:37:27 +08:00
|
|
|
/* register CAN device */
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_hw_board_can_init(g_can_dev_array[i].instance);
|
|
|
|
rt_hw_can_register(&g_can_dev_array[i].rt_can,
|
|
|
|
g_can_dev_array[i].init.name,
|
2022-05-06 09:28:21 +08:00
|
|
|
&_can_ops,
|
2022-05-15 20:57:35 +08:00
|
|
|
&g_can_dev_array[i]);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_can_init);
|
|
|
|
|
|
|
|
#endif /* BSP_USING_CAN */
|
|
|
|
|
|
|
|
/************************** end of file ******************/
|