drivers/can: add Kvaser PCI card driver (qemu only)

add Kvaser PCI card driver support, works only with QEMU now:

https://www.qemu.org/docs/master/system/devices/can.html#examples-how-to-use-can-emulation-for-sja1000-based-boards

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
This commit is contained in:
p-szafonimateusz 2024-10-24 13:25:47 +02:00 committed by Xiang Xiao
parent 0df0a105d1
commit 033f203e2a
7 changed files with 1952 additions and 10 deletions

View file

@ -20,12 +20,18 @@
#
# ##############################################################################
set(SRCS)
if(CONFIG_CAN)
set(SRCS can.c can_sender.c)
if(CONFIG_CAN_MCP2515)
list(APPEND SRCS mcp2515.c)
endif()
target_sources(drivers PRIVATE ${SRCS})
endif()
if(CONFIG_CAN_MCP2515)
list(APPEND SRCS mcp2515.c)
endif()
if(CONFIG_CAN_KVASER)
list(APPEND SRCS kvaser_pci.c)
endif()
target_sources(drivers PRIVATE ${SRCS})

View file

@ -7,8 +7,10 @@ config ARCH_HAVE_CAN_ERRORS
bool
default n
menuconfig CAN
bool "CAN Driver Support"
menu "CAN Driver Support"
config CAN
bool "CAN Character Driver Support"
default n
---help---
This selection enables building of the "upper-half" CAN driver.
@ -267,3 +269,34 @@ config CAN_SJA1000_DEBUG
endif # CAN_SJA1000
endif # CAN
config CAN_KVASER
bool "Kvaser PCI CAN card"
default n
depends on PCI
---help---
Enable driver support for Kvase PCI CAN card.
NOTE: for now works only with QEMU
if CAN_KVASER
choice
prompt "Kvaser PCI CAN device type"
default CAN_KVASER_CHARDEV if CAN
default CAN_KVASER_SOCKET if NET_CAN
config CAN_KVASER_CHARDEV
bool "Kvaser PCI can device as chardev"
depends on CAN
select ARCH_HAVE_CAN_ERRORS
config CAN_KVASER_SOCKET
bool "Kvaser PCI can device as socketCAN"
depends on NET_CAN
select NET_CAN_HAVE_ERRORS
endchoice # "Kvaser PCI CAN device type"
endif # CAN_KVASER
endmenu # CAN Driver Support

View file

@ -23,8 +23,8 @@
# Don't build anything if there is no CAN support
ifeq ($(CONFIG_CAN),y)
CSRCS += can.c can_sender.c
endif
ifeq ($(CONFIG_CAN_MCP2515),y)
CSRCS += mcp2515.c
@ -34,9 +34,12 @@ ifeq ($(CONFIG_CAN_SJA1000),y)
CSRCS += sja1000.c
endif
ifeq ($(CONFIG_CAN_KVASER),y)
CSRCS += kvaser_pci.c
endif
# Include CAN device driver build support
DEPPATH += --dep-path can
VPATH += :can
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)drivers$(DELIM)can
endif

1814
drivers/can/kvaser_pci.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -29,6 +29,8 @@
#include <nuttx/config.h>
#include <nuttx/bits.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -48,6 +50,10 @@
#define SJA1000_MODE_REG (0x00)
/* Sleep mode */
#define SJA1000_SLEEP_MODE (BIT(4))
/* SJA1000_RX_FILTER_MODE : R/W; bitpos: [3]; default: 0;
* This bit is used to configure the filter mode. 0: Dual filter mode; 1:
* Single filter mode.
@ -458,6 +464,10 @@
#define SJA1000_TIME_SEG1_V 0x0000000F
#define SJA1000_TIME_SEG1_S 0
/* Output control */
#define SJA1000_OUTCTRL_REG 0x08
/* SJA1000_ARB_LOST_CAP_REG register
* Arbitration Lost Capture Register
*/
@ -906,4 +916,10 @@
#define SJA1000_CD_V 0x00000007
#define SJA1000_CD_S 0
/* Frame information */
#define SJA1000_FI_DLC_MASK (0xf) /* Data length code bit */
#define SJA1000_FI_RTR (BIT(6)) /* Remote transmission request */
#define SJA1000_FI_FF (BIT(7)) /* Frame format */
#endif /* __DRIVERS_CAN_SJA1000_H */

View file

@ -32,6 +32,7 @@
#include <nuttx/virtio/virtio-pci.h>
#include <nuttx/net/e1000.h>
#include <nuttx/net/igc.h>
#include <nuttx/can/kvaser_pci.h>
#include "pci_drivers.h"
@ -160,6 +161,16 @@ int pci_register_drivers(void)
}
#endif
/* Initialzie Kvaser pci driver */
#ifdef CONFIG_CAN_KVASER
ret = pci_kvaser_init();
if (ret < 0)
{
pcierr("pci_kvaser_init failed, ret=%d\n", ret);
}
#endif
ret = pci_dev_register();
if (ret < 0)
{

View file

@ -0,0 +1,59 @@
/****************************************************************************
* include/nuttx/can/kvaser_pci.h
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_CAN_KVASER_H
#define __INCLUDE_NUTTX_CAN_KVASER_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: pci_kvaser_init
*
* Description:
* Register a pci driver
*
****************************************************************************/
int pci_kvaser_init(void);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __INCLUDE_NUTTX_CAN_KVASER_H */