T507 buildroot linux4.9之MCP2515 can网络开发调试

文章目录


前言

由于T507这款soc中没有带can接口,而项目中需要can接口,故采用了mcp2515 spi转can进行调试。


一、设备树配置

1.1、SPI总线确认

通过原理图确认SPI总线,并把SPI总线的片选以及总线的复用情况进行整理配置

c 复制代码
diff --git a/device/config/chips/t507/configs/demo2.0/board.dts b/device/config/chips/t507/configs/demo2.0/board.dts
index dd51dbc284..bf15f85f5a 100755
--- a/device/config/chips/t507/configs/demo2.0/board.dts
+++ b/device/config/chips/t507/configs/demo2.0/board.dts
@@ -83,13 +83,13 @@
 			};
 
 			spi1_pins_b: spi1@1 {
-				allwinner,pins = "PH5", "PH9";
-				allwinner,pname = "spi1_cs0", "spi1_cs1";
+				allwinner,pins = "PH5";
+				allwinner,pname = "spi1_cs0";
 				allwinner,function = "spi1";
 			};
 
 			spi1_pins_c: spi1@2 {
-				allwinner,pins = "PH5", "PH6", "PH7", "PH8", "PH9";
+				allwinner,pins = "PH5", "PH6", "PH7", "PH8";
 				allwinner,function = "io_disabled";
 			};
c 复制代码
diff --git a/device/config/chips/t507/configs/demo2.0/sys_config.fex b/device/config/chips/t507/configs/demo2.0/sys_config.fex
index bb583b70d6..ff3a11776a 100755
--- a/device/config/chips/t507/configs/demo2.0/sys_config.fex
+++ b/device/config/chips/t507/configs/demo2.0/sys_config.fex
@@ -132,12 +132,12 @@ uart_debug_port = 0
 uart_debug_tx   = port:PH00<2><1><default><default>
 uart_debug_rx   = port:PH01<2><1><default><default>
 
-[jtag_para]
-jtag_enable     = 1
-jtag_ms         = port:PH9<3><default><default><default>
-jtag_ck         = port:PH10<3><default><default><default>
-jtag_do         = port:PH11<3><default><default><default>
-jtag_di         = port:PH12<3><default><default><default>
+;[jtag_para]
+;jtag_enable     = 1
+;jtag_ms         = port:PH9<3><default><default><default>
+;jtag_ck         = port:PH10<3><default><default><default>
+;jtag_do         = port:PH11<3><default><default><default>
+;jtag_di         = port:PH12<3><default><default><default>

1.2、DTS配置

对mcp2515进行配置,clk信号以及中断等处理,同时在项目中如果有3.3v转5v的电平转换芯片,需要确保改转换ic的工作正常。

c 复制代码
diff --git a/kernel/linux-4.9/arch/arm64/boot/dts/sunxi/sun50iw9p1-clk.dtsi b/kernel/linux-4.9/arch/arm64/boot/dts/sunxi/sun50iw9p1-clk.dtsi
index 7708d3083b..6466bfe206 100755
--- a/kernel/linux-4.9/arch/arm64/boot/dts/sunxi/sun50iw9p1-clk.dtsi
+++ b/kernel/linux-4.9/arch/arm64/boot/dts/sunxi/sun50iw9p1-clk.dtsi
@@ -10,6 +10,13 @@ clocks {
 	      <0x0 0x07000000 0x0 0x4>;
 
 /* register fixed rate clock*/
+		mcp251x_clk: mcp251x_clk{
+			#clock-cells = <0>;
+			compatible = "allwinner,fixed-clock";
+			clock-frequency = <8000000>;
+			clock-output-names = "mcp251x_clk";
+		};
+	
 		clk_losc: losc {
 			#clock-cells = <0>;
 			compatible = "allwinner,fixed-clock";
c 复制代码
diff --git a/device/config/chips/t507/configs/demo2.0/board.dts b/device/config/chips/t507/configs/demo2.0/board.dts
index dd51dbc284..bf15f85f5a 100755
--- a/device/config/chips/t507/configs/demo2.0/board.dts
+++ b/device/config/chips/t507/configs/demo2.0/board.dts 
 
@@ -273,6 +273,15 @@
                     allwinner,drive = <1>;
                     allwinner,pull = <0>;
             }; 
+
+            can_irq: can0@0 {
+                allwinner,pins = "PH9";
+                allwinner,pname = "can_irq";
+                allwinner,function = "irq";
+                allwinner,muxsel = <6>;
+                allwinner,drive = <1>;
+                allwinner,pull = <0>;
+            };			
 			
 		};
 
@@ -293,7 +302,8 @@
        spi1: spi@05011000 {
 			pinctrl-0 = <&spi1_pins_a &spi1_pins_b>;
 			pinctrl-1 = <&spi1_pins_c>;
 			spi_slave_mode = <0>;
-			status = "disabled";
+			status = "okay"; 
+			mcp2515{
+                status = "okay"; 
+                reg = <0x0>;
+                compatible = "microchip,mcp2515";
+                spi-max-frequency = <24000000>;
+                clocks = <&mcp251x_clk>;
+                interrupt-parent = <&pio>;
+                interrupts = <PH  9  IRQ_TYPE_EDGE_BOTH>;
+                pinctrl-names = "default";
+                pinctrl-0 = <&can_irq>;
+            }; 
 		}; 

二、驱动配置

2.1、驱动位置

定为驱动,使用内核中的驱动即可,包括mcp2515驱动,spi总线以及can网络配置

mcp2515驱动:

c 复制代码
kernel/linux-4.9/drivers/net/can/spi/mcp251x.c

spi总线:

c 复制代码
kernel/linux-4.9/drivers/spi

can网络:

c 复制代码
kernel/linux-4.9/drivers/net/can

使用宏来配置驱动。

2.2、使用config宏配置驱动

c 复制代码
diff --git a/kernel/linux-4.9/arch/arm64/configs/sun50iw9p1smp_longan_defconfig b/kernel/linux-4.9/arch/arm64/configs/sun50iw9p1smp_longan_defconfig
index d9b09b74ab..9ec92f7774 100755
--- a/kernel/linux-4.9/arch/arm64/configs/sun50iw9p1smp_longan_defconfig
+++ b/kernel/linux-4.9/arch/arm64/configs/sun50iw9p1smp_longan_defconfig
@@ -1038,7 +1038,11 @@ CONFIG_NET_FLOW_LIMIT=y
 # CONFIG_NET_PKTGEN is not set
 # CONFIG_NET_DROP_MONITOR is not set
 # CONFIG_HAMRADIO is not set
-# CONFIG_CAN is not set
+CONFIG_CAN=y 
+CONFIG_CAN_RAW=y
+CONFIG_CAN_BCM=y
+CONFIG_CAN_GW=y
+CONFIG_CAN_MCP251X=y
 # CONFIG_IRDA is not set
 CONFIG_BT=y
 CONFIG_BT_BREDR=y
@@ -1382,7 +1386,7 @@ CONFIG_TUN=y
 CONFIG_ETHERNET=y
 CONFIG_NET_VENDOR_ALLWINNER=y
 # CONFIG_SUN4I_EMAC is not set
-# CONFIG_SUNXI_GMAC is not set
+CONFIG_SUNXI_GMAC=y
 # CONFIG_ALTERA_TSE is not set
 CONFIG_NET_VENDOR_AMAZON=y
 CONFIG_NET_VENDOR_AMD=y
@@ -1912,7 +1916,10 @@ CONFIG_I2C_SUNXI=y
 # CONFIG_I2C_DEBUG_CORE is not set
 # CONFIG_I2C_DEBUG_ALGO is not set
 # CONFIG_I2C_DEBUG_BUS is not set
-# CONFIG_SPI is not set
+CONFIG_SPI=y
+CONFIG_SPI_MASTER=y
+CONFIG_SPI_SPIDEV=y
+CONFIG_SPI_SUNXI=y
 # CONFIG_SPMI is not set
 # CONFIG_HSI is not set

2.3、MCP2515驱动调试中遇到的问题

2.3.1、问题描述

调试mcp2515 spi转can过程中,软件写复位读出来的状态是0x01复位不了mcp2515

2.3.2、分析问题

软件复位中,spi的写操作过快,没有将mcp2515寄存器进行复位完成

2.3.3、处理问题

在软复位中,添加timer处理

c 复制代码
diff --git a/kernel/linux-4.9/drivers/net/can/spi/mcp251x.c b/kernel/linux-4.9/drivers/net/can/spi/mcp251x.c
index f3f05fea8e..82169895a0 100755
--- a/kernel/linux-4.9/drivers/net/can/spi/mcp251x.c
+++ b/kernel/linux-4.9/drivers/net/can/spi/mcp251x.c
@@ -627,7 +627,7 @@ static int mcp251x_setup(struct net_device *net, struct mcp251x_priv *priv,
 static int mcp251x_hw_reset(struct spi_device *spi)
 {
 	struct mcp251x_priv *priv = spi_get_drvdata(spi);
-	u8 reg;
+	unsigned long timeout;
 	int ret;
 
 	/* Wait for oscillator startup timer after power up */
@@ -638,13 +638,14 @@ static int mcp251x_hw_reset(struct spi_device *spi)
 	if (ret)
 		return ret;
 
-	/* Wait for oscillator startup timer after reset */
-	mdelay(MCP251X_OST_DELAY_MS);
-	
-	reg = mcp251x_read_reg(spi, CANSTAT);
-	if ((reg & CANCTRL_REQOP_MASK) != CANCTRL_REQOP_CONF)
-		return -ENODEV;
-
+ 	timeout = jiffies + HZ;
+ 	while ((mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK) != CANCTRL_REQOP_CONF) {
+ 		usleep_range(MCP251X_OST_DELAY_MS * 1000,MCP251X_OST_DELAY_MS * 1000 * 2); 
+ 		if (time_after(jiffies, timeout)) {
+ 			printk("MCP251x didn't enter in conf mode after reset\n");
+ 			return -EBUSY;
+ 		}
+ 	}
 	return 0;
 }

三、buildroot 集成can测试工具

sdk的buildroot中can测试工具是没有的,需要自行进行添加,同时需要将out目录下的.config删除

.config:

c 复制代码
out/t507/demo2.0/longan/buildroot/.config

集成can测试工具

c 复制代码
diff --git a/buildroot/buildroot-201902/configs/sun50iw9p1_longan_defconfig b/buildroot/buildroot-201902/configs/sun50iw9p1_longan_defconfig
index 8cafd7e907..3b3c8b1f28 100755
--- a/buildroot/buildroot-201902/configs/sun50iw9p1_longan_defconfig
+++ b/buildroot/buildroot-201902/configs/sun50iw9p1_longan_defconfig
@@ -1943,7 +1943,7 @@ BR2_PACKAGE_QEMU_ARCH_SUPPORTS_TARGET=y
 # BR2_PACKAGE_BRIDGE_UTILS is not set
 # BR2_PACKAGE_BWM_NG is not set
 # BR2_PACKAGE_C_ICAP is not set
-# BR2_PACKAGE_CAN_UTILS is not set
+BR2_PACKAGE_CAN_UTILS=y
 # BR2_PACKAGE_CANNELLONI is not set
 # BR2_PACKAGE_CHRONY is not set
 # BR2_PACKAGE_CIVETWEB is not set
@@ -2024,7 +2024,7 @@ BR2_PACKAGE_HOSTAPD_VLAN_NETLINK=y
 # BR2_PACKAGE_IODINE is not set
 # BR2_PACKAGE_IPERF is not set
 # BR2_PACKAGE_IPERF3 is not set
-# BR2_PACKAGE_IPROUTE2 is not set
+BR2_PACKAGE_IPROUTE2=y
 # BR2_PACKAGE_IPSEC_TOOLS is not set
 # BR2_PACKAGE_IPSET is not set
 # BR2_PACKAGE_IPTABLES is not set

四、测试

c 复制代码
ip link set can0 down 		#关闭can设备;
ip link set can0 up type can bitrate 250000 #设置can波特率
ip link set can0 up   		#开启can设备  
cansend can0 123#000102030405060708 #发送信息
candump can0  			    #接收can总线发来数据
ip -details link show can0 	#显示can设备详细信息 
ifconfig can0 down 			#关闭can设备,以便配置
相关推荐
vvw&1 小时前
使用Ubuntu快速部署MinIO对象存储
linux·运维·服务器·ubuntu·minio·cos·oss
归仁6 小时前
wvp 推拉转级联时频繁出现流无法观看的解决办法
linux·服务器·python·音视频
万叶学编程6 小时前
Linux之初体验
linux
小小不董7 小时前
Oracle OCP认证考试考点详解082系列08
linux·运维·服务器·数据库·oracle·dba
初露寒秋8 小时前
linux $ 命令
linux·后端
weixin_404679319 小时前
pdf转图片
linux·运维·开发语言·python·pdf
咖啡里的茶i10 小时前
Linux常用命令(一)
linux
verse_armour10 小时前
【linux】端口监听和终止进程
linux·运维·服务器
bala556911 小时前
阿里云-部署CNI flannel集群网络
linux·服务器·阿里云·docker·kubernetes·云计算