找回密码
 注册
关于网站域名变更的通知
查看: 333|回复: 1
打印 上一主题 下一主题

【玩转多核异构】i.MX8M Plus开发板的M核SPI主从模式通讯

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2023-4-7 10:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
SPI(串行外围设备接口)是一种低成本、易使用的接口协议,具备全双工、高速、通讯简单的特点,被广泛应用于微控制器和外围设备芯片之间的通讯。当SPI接口作为主模式时可以连接Flash存储器、AD采样芯片、实时时钟RTC、LCD显示屏、音频芯片以及各种传感器。

& h5 I) T7 n$ e* D: d

+ T, ?* [! n2 a
随着产品功能的愈加丰富,多处理器使用SPI接口进行通讯的场景开始出现,而多个SPI设备之间通信必须由主设备(Master)来控制从设备(Slave)。
小编手上的OKMX8MP-C开发板基于NXP i.MX8M Plus多核异构处理器设计,它的M核有1路SPI,因而为实现SPI的相互通讯,我们就需要两块OKMX8MP-C开发板的SPI互作主从设备进行通信。本文小编就将从应用角度为大家讲解M核SPI间通讯的实现方式。

7 }+ W. q4 I3 \- @  h

# r# w8 y7 I5 }3 w) R; i( J* n: G7 i. [! }
一、SPI主模式
1. SPI初始化
SPI初始化主要包括总线时钟、管脚和相应寄存器的初始化。具体如下:

' A; S( V8 L0 l. l& i
(1)SPI总线时钟:现将SPI总线倍频到800MHz,再10分频到80MHz。
  1. <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;"="">CLOCK_SetRootMux</span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">(kCLOCK_RootEcspi2, kCLOCK_EcspiRootmuxSysPll1); </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">//SPI2总线时钟使用PLL1-800MHz </span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"=""><span class="hljs-selector-tag" style="box-sizing: border-box;" microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;"="">CLOCK_SetRootDivider</span><span microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">(kCLOCK_RootEcspi2, </span><span class="hljs-number" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(168,="" 46,="" 46);"="">2</span><span microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">U, </span><span class="hljs-number" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(168,="" 46,="" 46);"="">5</span><span microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">U); </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">//分频因子为2*5=10,设置SPI2总线时钟为80MHz</span></span>
复制代码
9 t! D, p1 j6 Y0 q
(2)管脚配置:选择SPI2的四个管脚。
  1. <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;"="">IOMUXC_SetPinMux</span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">(IOMUXC_ECSPI2_MISO_ECSPI2_MISO, </span><span class="hljs-number" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(168,="" 46,="" 46);"="">0</span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">U); </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// SPI2-</span>
复制代码

1 y, M  O  w' o; X
(3)SPI速率:设置速率为500K。
  1. #<span class="hljs-meta-keyword" style="box-sizing: border-box; border: 0px;">define</span> TRANSFER_BAUDRATE 500000U <span class="hljs-comment" style="box-sizing: border-box; border: 0px; color: rgb(153, 153, 153);">// 速率 500K</span>
复制代码
1 d7 L. _$ `# p( K
(4)数据长度选择:8bit。
  1. <span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->burstLength = </span><span class="hljs-number" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(168,="" 46,="" 46);"="">8</span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 数据长度 8bit</span>
复制代码

9 }; i! M6 d8 s% a2 O9 D
(5)四种模式选择:CPOL和CPHA的四种组合即为SPI的四种模式。
  1. <span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->clockInactiveState = kECSPI_ClockInactiveStateLow; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 时钟SCL: 活动时低电平,空闲时高电平 </span><div><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->dataLineInactiveState = kECSPI_DataLineInactiveStateLow; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 数据MOSI&MISO: 活动时低电平,空闲时高电平 </span></div><div><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->chipSlectActiveState = kECSPI_ChipSelectActiveStateLow; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 片选SS: 低电平选中,高电平无效 </span></div><div><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->polarity = kECSPI_PolarityActiveHigh; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 时钟信号极性,即CPOL为0的话 SCLK高电平有效(空闲的时候为低电平),为1的话SCLK低电平有效(空闲的时候为高电平)。 </span></div><div><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->phase = kECSPI_ClockPhaseFirstEdge; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 时钟相位,即CPHA为0的话串行时钟的第一个跳变沿(上升沿或下降沿)采集数据,为1的话串行时钟的第二个跳变沿(上升沿或下降沿)采集数据。</span></div>
复制代码
% D3 E9 F: Q8 a! f9 e
(6)主模式选择:设置SPI为主模式。
  1. <span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->channelConfig.channelMode = kECSPI_Master; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 主模式</span>
复制代码
0 U' R8 e) v# T) L6 Q! a1 u; [3 U) ~1 [$ j
(7)通道选择:一个 SPI 有四个硬件片选信号,每个片选信号是一个硬件通道,本程序选择通道0。
  1. <span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">config->channel = kECSPI_Channel0; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 通道0</span>
复制代码

3 Z, \/ E+ x: r
(8)关闭自回环:如果开启了自回环,那么SPI数据会在芯片内回环,不会到外部管脚,在程序调试时可以排除外部端子的干扰,但真实应用时,需要关闭自回环,从外部管脚收发数据。
  1. <span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">Config->enableLoopBack = </span><span class="hljs-keyword" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(17,="" 75,="" 166);"="">false</span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">; </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">// 不回环,使用外部管脚</span>
复制代码
. x& B7 T* m$ T
2. SPI收发流程
我们分别将两块OKMX8MP-C开发板命名为开发板1和开发板2,并且将开发板1的SPI接口采用主模式,使能收发中断;将开发板2的SPI接口采用从模式,使能收发中断。
SPI主发送64字节数据,SPI从接收后,将数据回传。SPI主接收回传信息后,比对接收和发送的数据是否一致,输出比对结果。如一致,本次传输结束,等待输入任何按键启动下一次传输。
; \0 i; ]( C7 \/ E% l+ i: ~* Z
(1)SPI发送数据:EXAMPLE_ECSPI_MASTER_BASEADDR 表示为SPI2,g_m_handle为SPI实例,包含了发送接收中断及其回调函数,masterXfer为要发送的64字节数据。
  1. <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;"="">ECSPI_MasterTransferNonBlocking</span><span style="color: rgb(100, 100, 100); font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;"="">(EXAMPLE_ECSPI_MASTER_BASEADDR, &g_m_handle, &masterXfer); </span><span class="hljs-comment" style="box-sizing: border-box; font-family: myFont, " microsoft="" yahei";="" font-size:="" 16px;="" text-align:="" justify;="" border:="" 0px;="" color:="" rgb(153,="" 153,="" 153);"="">//主模式中断方式发送数据</span>
复制代码
0 n& l0 X% M. Y5 o; O
(2)SPI接收数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。

& O- y2 L+ q; d
(3)接收和发送数据对比:
  1. <code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__keyword" style="outline: 0px; max-width: 1000%; color: rgb(202, 125, 55); box-sizing: border-box !important;">for</span> (i = <span class="code-snippet__number" style="outline: 0px; max-width: 1000%; color: rgb(14, 156, 229); box-sizing: border-box !important;">0</span>U; i < TRANSFER_SIZE; i++)  </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">{  </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">    <span class="code-snippet__keyword" style="outline: 0px; max-width: 1000%; color: rgb(202, 125, 55); box-sizing: border-box !important;">if</span> (masterTxData[i] != masterRxData[i])  </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">    {  </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">       errorCount++;  </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">    }  </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">}</span></code>
复制代码
, K( h# V, z$ H4 m# R# M( A
二、SPI从模式
1. SPI初始化
SPI从模式初始化与主模式要保持一致,除了将工作模式设为从模式,其他设置均一样。主从模式选择:设置SPI为从模式。
  1. config->channelConfig.channelMode = kECSPI_Slave; <span class="hljs-comment" style="box-sizing: border-box; border: 0px; color: rgb(153, 153, 153);">//从模式</span>
复制代码
  h  s, x1 V, E

+ j. U) J4 _! X7 ]# N. y1 |5 X
2. SPI收发流程
开发板2的SPI接口采用从模式,使能收发中断。
SPI从进入等待接收状态,在片选有效后,通过接收中断获取数据,并回传信息,再次进入接收状态。
9 N! {3 }, n6 v) U: D' ]) t
(1)SPI接收数据:EXAMPLE_ECSPI_SLAVE_BASEADDR表示为SPI2,g_m_handle为SPI实例,包含了发送接收中断及其回调函数,slaveXfer存储接收的数据。
  1. <span class="hljs-selector-tag" style="box-sizing: border-box; border: 0px;">ECSPI_SlaveTransferNonBlocking</span>(EXAMPLE_ECSPI_SLAVE_BASEADDR, &g_s_handle, &slaveXfer); <span class="hljs-comment" style="box-sizing: border-box; border: 0px; color: rgb(153, 153, 153);">//从模式中断方式接收数据</span>
复制代码
2 n/ \; ?1 V2 c5 d' R
(2)SPI发送数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。
' J( H/ o3 a( r8 \4 L
三、A核修改
A核设备树中若保留SPI2,内核解析设备树,在/dev下生成设备文件spidev1.0。这样待M核运行后,A核将重新对SPI2初始化,造成M核SPI功能异常,因此需要去除A核对SPI的控制。

8 g5 J, x! }  J; x) c' E( h9 N
1. 修改设备树
(1)在设备树OK8MP-C.dts中,删除SPI2设备节点相关信息。
  1. <code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">&ecspi2 {  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__comment" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); font-style: italic; box-sizing: border-box !important;">    #address-cells = <1>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__comment" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); font-style: italic; box-sizing: border-box !important;">    #size-cells = <0>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    fsl,spi-num-chipselects </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <1>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    pinctrl-names </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> "default";  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    pinctrl-0 </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <&pinctrl_ecspi2 &pinctrl_ecspi2_cs>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    cs-gpios </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <&gpio5 13 GPIO_ACTIVE_LOW>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    status </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> "okay";  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    spidev1</span>:<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> spi@0 {  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">        reg </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <0>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">        compatible </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> "rohm,dh2228fv";  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">        spi-max-frequency </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <500000>;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">    };  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">};  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">3 C! i; a0 X; ^! y# M6 {: |6 x
  2. </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">5 l" o6 o7 R! t+ M$ g& P
  3. </span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">pinctrl_ecspi2</span>:<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> ecspi2grp {  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">        fsl,pins </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">        MX8MP_IOMUXC_ECSPI2_SCLK__ECSPI2_SCLK       0x82  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">        MX8MP_IOMUXC_ECSPI2_MOSI__ECSPI2_MOSI       0x82  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">        MX8MP_IOMUXC_ECSPI2_MISO__ECSPI2_MISO       0x82  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">        >;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">};  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">pinctrl_ecspi2_cs</span>:<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> ecspi2cs {  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">    fsl,pins </span>=<span class="code-snippet__string" style="outline: 0px; max-width: 1000%; color: rgb(221, 17, 68); box-sizing: border-box !important;"> <  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">        MX8MP_IOMUXC_ECSPI2_SS0__GPIO5_IO13     0x40000  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__attr" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;">    >;  </span></span></code><code style="outline: 0px; max-width: 1000%; display: flex; position: relative;" liberation="" mono",="" menlo,="" courier,="" monospace;="" box-sizing:="" border-box="" !important;"=""><span class="code-snippet_outer" style="outline: 0px; max-width: 1000%; box-sizing: border-box !important;"><span class="code-snippet__meta" style="outline: 0px; max-width: 1000%; color: rgb(175, 175, 175); box-sizing: border-box !important;">};</span>  </span></code>
复制代码

4 }' i& \; c9 o+ w
(2)编译生成新的内核镜像Image及设备树OK8MP-C.dtb。
(3)将生成的OK8MP-C.dtb和Image拷贝至开发板/run/media/mmcblk2p1/目录下,输入sync命令,重启开发板。
(4)输ls /dev查看发现没有SPI2设备文件spidev1.0。

: B6 O/ K( o2 F四、程序验证
1. 硬件连接
使用杜邦线将两块OKMX8MP-C开发板的SPI一一对应连接,线序如下:
开发板1--SPI主模式
开发板2--SPI从模式
管脚名称
开发板位置
管脚名称
开发板位置
MISO
P40-10
MISO
P40-10
MOSI
P40-8
MOSI
P40-8
SCK
P40-1
SCK
P40-1
SS0
P40-3
SS0
P40-3
GND
P40-4/P40-7
GND
P40-4/P40-7

  X! J" J/ v; j+ g: B0 \5 j
6 |$ C% W6 m0 G; ~/ d
# d- ]) e& ~# ~  B5 C. `1 g7 E: F
4 J( ~* o8 y8 {3 Y' |
2. M核程序
修改uboot环境变量设置M核自启动,同时将M核程序forlinx_m7_tcm_firmware.bin
放到/run/media/mmcblk2p1/目录下。注意,SPI主模式程序须放入开发板1,SPI从模式程序须放入开发板2。

  Q+ w& E! G/ w/ {
3. 实际测试
(1)开发板2先上电,M核程序启动,完成SPI初始化后,进入接收等待状态;
(2)开发板1后上电,M核程序启动,完成SPI初始化后,主动发送64字节数据;
(3)开发板2的SPI接收数据,通过串口打印接收的数据,并将接收的数据再次发送;
) K: G% v8 F0 |/ X- c

& z7 t) W8 A1 z9 G+ b# ~

) {! }8 P& B) L" K$ b7 a
(4)开发板1的SPI接收到回传信息,通过串口打印接收的数据。和发送数据比对,输出结果。
7 K: y8 S! |5 i9 Q# I; K+ `
6 u0 T( I6 e* P! w
* f3 Q$ n- J; ~* G- @
(5)此时在开发板1调试串口输入任意键,即可开启新一轮的SPI发送和接收流程。
  `" D7 E4 ~& D' h  O  a
1 K8 V, g! v7 g

该用户从未签到

2#
发表于 2023-4-7 11:21 | 只看该作者
SPI这个总线用的也很普遍,在控制外设中,是不可缺少的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-10-10 09:25 , Processed in 0.156250 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表