当前位置: 首页 > article >正文

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

文章目录

  • 前言
  • 一、设备树配置
    • 1.1、SPI总线确认
    • 1.2、DTS配置
  • 二、驱动配置
    • 2.1、驱动位置
    • 2.2、使用config宏配置驱动
    • 2.3、MCP2515驱动调试中遇到的问题
      • 2.3.1、问题描述
      • 2.3.2、分析问题
      • 2.3.3、处理问题
  • 三、buildroot 集成can测试工具
  • 四、测试


前言

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


一、设备树配置

1.1、SPI总线确认

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

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";
 			};
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的工作正常。

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";
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驱动:

kernel/linux-4.9/drivers/net/can/spi/mcp251x.c

spi总线:

kernel/linux-4.9/drivers/spi

can网络:

kernel/linux-4.9/drivers/net/can

使用宏来配置驱动。

2.2、使用config宏配置驱动

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处理

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:

out/t507/demo2.0/longan/buildroot/.config

集成can测试工具

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

四、测试

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设备,以便配置

http://www.kler.cn/a/383731.html

相关文章:

  • 某科技局国产服务器PVE虚拟化技术文档
  • 链路聚合与GVRP的混合构建(eNSP)
  • webrtc音频模块(三) windows Core Audio API及声音的播放
  • Mono里运行C#脚本3—mono_jit_init
  • Docker快速入门到项目部署
  • DCN-DCN路由器online_list.php存在任意文件读取漏洞
  • 耕地类项目知识点汇总(持续完善中……)
  • ubuntu22.04安装conda
  • 鸿蒙-promptAction.showToast基于PC屏幕底部提示
  • Ubuntu 安装 RTL8811cu 网卡驱动
  • CTFshow之信息收集第1关到10关。详细讲解
  • SpringBoot基础系列学习(二):配置详解
  • 汉诺塔问题代码分享及思路分享(c基础)
  • Spring Cloud微服务:构建弹性、可扩展的分布式系统
  • AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
  • Oracle OCP认证考试考点详解082系列15
  • Angular进阶之十:toPromise废弃原因及解决方案
  • 【java】实战-力扣题库:二分查找
  • 首都师范大学地信GIS导师推荐(避坑)
  • 从 vue 源码看问题 — 如何理解 vue 响应式?
  • 【贪心算法】No.1---贪心算法(1)
  • 量子电路的实现 基于ibm的qiskit
  • 基于MySQL的企业专利数据高效查询与统计实现
  • 【日记】跟人沟通有时真的好头疼(688 字)
  • 年入百万:从初中辍学到 50 万读者!
  • npm i忽略依赖冲突