EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
SPI(串行外围设备接口)是一种低成本、易使用的接口协议,具备全双工、高速、通讯简单的特点,被广泛应用于微控制器和外围设备芯片之间的通讯。当SPI接口作为主模式时可以连接Flash存储器、AD采样芯片、实时时钟RTC、LCD显示屏、音频芯片以及各种传感器。 ![]()
+ k- N7 v& S/ Q
1 R" H2 ]( h$ C2 g& i随着产品功能的愈加丰富,多处理器使用SPI接口进行通讯的场景开始出现,而多个SPI设备之间通信必须由主设备(Master)来控制从设备(Slave)。 小编手上的OKMX8MP-C开发板基于NXP i.MX8M Plus多核异构处理器设计,它的M核有1路SPI,因而为实现SPI的相互通讯,我们就需要两块OKMX8MP-C开发板的SPI互作主从设备进行通信。本文小编就将从应用角度为大家讲解M核SPI间通讯的实现方式。 ![]()
. e9 i1 O; z, j f6 @5 J( J& l+ S; p' s7 N" T7 I, _
- ]+ q2 L0 J1 |! @& U+ h一、SPI主模式1. SPI初始化 SPI初始化主要包括总线时钟、管脚和相应寄存器的初始化。具体如下:
6 Z- t/ R- r/ P @+ n/ r+ r1 A- `3 G& F(1)SPI总线时钟:现将SPI总线倍频到800MHz,再10分频到80MHz。 - <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>
复制代码 : m1 V) F% l2 U2 H. Y. {
(2)管脚配置:选择SPI2的四个管脚。 - <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>
复制代码 9 E/ Z7 w$ k: y3 N
(3)SPI速率:设置速率为500K。 - #<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>
复制代码 ( @, }) w ]9 w" F9 K" T/ E
(4)数据长度选择:8bit。 - <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>
复制代码 ! }- r, W* @5 I# y. T0 x
(5)四种模式选择:CPOL和CPHA的四种组合即为SPI的四种模式。 - <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>
复制代码 4 a( w p) J% i; E# C0 t
(6)主模式选择:设置SPI为主模式。 - <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>
复制代码 3 N' [" L/ b" g
(7)通道选择:一个 SPI 有四个硬件片选信号,每个片选信号是一个硬件通道,本程序选择通道0。 - <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>
复制代码
u. i* |- [5 h1 J1 g) p7 A(8)关闭自回环:如果开启了自回环,那么SPI数据会在芯片内回环,不会到外部管脚,在程序调试时可以排除外部端子的干扰,但真实应用时,需要关闭自回环,从外部管脚收发数据。 - <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>
复制代码 ; ` ^% ]6 f2 o' l
2. SPI收发流程 我们分别将两块OKMX8MP-C开发板命名为开发板1和开发板2,并且将开发板1的SPI接口采用主模式,使能收发中断;将开发板2的SPI接口采用从模式,使能收发中断。 SPI主发送64字节数据,SPI从接收后,将数据回传。SPI主接收回传信息后,比对接收和发送的数据是否一致,输出比对结果。如一致,本次传输结束,等待输入任何按键启动下一次传输。 ( T! v: E7 c* V. O
(1)SPI发送数据:EXAMPLE_ECSPI_MASTER_BASEADDR 表示为SPI2,g_m_handle为SPI实例,包含了发送接收中断及其回调函数,masterXfer为要发送的64字节数据。 - <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>
复制代码
2 v- ?& e( l# y X8 L/ e(2)SPI接收数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。 2 A! c W( h& X4 u; Q" e! a
(3)接收和发送数据对比: - <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>
复制代码 ) o" [5 _" o1 v) b6 U4 u( U
二、SPI从模式1. SPI初始化 SPI从模式初始化与主模式要保持一致,除了将工作模式设为从模式,其他设置均一样。主从模式选择:设置SPI为从模式。 - config->channelConfig.channelMode = kECSPI_Slave; <span class="hljs-comment" style="box-sizing: border-box; border: 0px; color: rgb(153, 153, 153);">//从模式</span>
复制代码
6 V# p, \, n& D( T& M/ t8 o: h5 e6 `+ A
2. SPI收发流程 开发板2的SPI接口采用从模式,使能收发中断。 SPI从进入等待接收状态,在片选有效后,通过接收中断获取数据,并回传信息,再次进入接收状态。 5 [6 P" [3 I3 e3 x' @
(1)SPI接收数据:EXAMPLE_ECSPI_SLAVE_BASEADDR表示为SPI2,g_m_handle为SPI实例,包含了发送接收中断及其回调函数,slaveXfer存储接收的数据。 - <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>
复制代码 & k q: t$ r- B' B. M& |
(2)SPI发送数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。 3 h) J$ [ {4 _0 t+ X
三、A核修改A核设备树中若保留SPI2,内核解析设备树,在/dev下生成设备文件spidev1.0。这样待M核运行后,A核将重新对SPI2初始化,造成M核SPI功能异常,因此需要去除A核对SPI的控制。
4 h% o$ B) i& q3 t, ~, A1. 修改设备树 (1)在设备树OK8MP-C.dts中,删除SPI2设备节点相关信息。 - <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;">
' R% D# z& x3 ~' K3 n. T- o# F0 e - </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;">. L8 E/ z8 N* u' V4 U
- </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>
复制代码 ?8 ?+ k: o5 p1 ]( N
(2)编译生成新的内核镜像Image及设备树OK8MP-C.dtb。 (3)将生成的OK8MP-C.dtb和Image拷贝至开发板/run/media/mmcblk2p1/目录下,输入sync命令,重启开发板。 (4)输ls /dev查看发现没有SPI2设备文件spidev1.0。 + T7 m( P1 K6 M) @
四、程序验证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 | 1 L1 P) X3 x0 S0 a. r
$ }! ]* L6 q! e- a
# A9 Y1 {8 V* ^: n' }
/ b" |$ ^3 h, M7 P- H& q2. M核程序 修改uboot环境变量设置M核自启动,同时将M核程序forlinx_m7_tcm_firmware.bin 放到/run/media/mmcblk2p1/目录下。注意,SPI主模式程序须放入开发板1,SPI从模式程序须放入开发板2。 7 x" g' H. A) J$ ]5 B0 [" X0 R* M
3. 实际测试 (1)开发板2先上电,M核程序启动,完成SPI初始化后,进入接收等待状态; (2)开发板1后上电,M核程序启动,完成SPI初始化后,主动发送64字节数据; (3)开发板2的SPI接收数据,通过串口打印接收的数据,并将接收的数据再次发送; & {' p( _( K, A
# ?* X" W* s( y4 n7 q* C$ w# {9 ]* w+ n6 K5 K
(4)开发板1的SPI接收到回传信息,通过串口打印接收的数据。和发送数据比对,输出结果。 ![]()
?& t) K2 g, @, ^& \* _
8 m* z8 k0 t+ B' Y$ ?: H$ v V; G2 ~6 E- y1 T- ]
(5)此时在开发板1调试串口输入任意键,即可开启新一轮的SPI发送和接收流程。 - q; R6 L8 J7 t p3 f4 [& x2 v
- a$ F- ~4 @4 i a3 l# e
|