EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
SPI(串行外围设备接口)是一种低成本、易使用的接口协议,具备全双工、高速、通讯简单的特点,被广泛应用于微控制器和外围设备芯片之间的通讯。当SPI接口作为主模式时可以连接Flash存储器、AD采样芯片、实时时钟RTC、LCD显示屏、音频芯片以及各种传感器。 ![]()
# d' ?6 Z% a- J5 u, F# s' m7 T# w P- ~! y) |7 u
随着产品功能的愈加丰富,多处理器使用SPI接口进行通讯的场景开始出现,而多个SPI设备之间通信必须由主设备(Master)来控制从设备(Slave)。 小编手上的OKMX8MP-C开发板基于NXP i.MX8M Plus多核异构处理器设计,它的M核有1路SPI,因而为实现SPI的相互通讯,我们就需要两块OKMX8MP-C开发板的SPI互作主从设备进行通信。本文小编就将从应用角度为大家讲解M核SPI间通讯的实现方式。 ![]()
4 [2 B. h( B) |$ u( c& n1 Y
2 [ P6 _1 R# @/ D" r( r2 H7 R) j1 f9 V# j, x8 Q5 c
一、SPI主模式1. SPI初始化 SPI初始化主要包括总线时钟、管脚和相应寄存器的初始化。具体如下:
! a0 e8 e% `0 v(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>
复制代码
2 a0 R4 B/ P. G" M& t(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>
复制代码 ( f- |! H6 O- X a
(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>
复制代码 8 y; T7 E" _; H5 A. M. c
(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>
复制代码
5 k" c" A0 N5 J5 x5 Z(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>
复制代码 : l& U W. _ Y( y. y! a" @
(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>
复制代码 7 X3 G6 y: H. w. b3 n0 y4 P
(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>
复制代码
5 {6 k: u \& G% Q, g' C) Y(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>
复制代码 ; L: ?2 [* B3 y& X7 ~8 Q
2. SPI收发流程 我们分别将两块OKMX8MP-C开发板命名为开发板1和开发板2,并且将开发板1的SPI接口采用主模式,使能收发中断;将开发板2的SPI接口采用从模式,使能收发中断。 SPI主发送64字节数据,SPI从接收后,将数据回传。SPI主接收回传信息后,比对接收和发送的数据是否一致,输出比对结果。如一致,本次传输结束,等待输入任何按键启动下一次传输。
^' K' V# a* N1 |) H(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>
复制代码 - p3 r# Y- @ ^3 J
(2)SPI接收数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。 0 W2 ]8 ?) h3 Z! C
(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>
复制代码
* W% K7 P8 r- }$ h. h二、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>
复制代码 / B5 C7 I6 J; G9 Z1 r0 V; `0 s
; G$ P1 H$ R) a/ ]; b# H2. SPI收发流程 开发板2的SPI接口采用从模式,使能收发中断。 SPI从进入等待接收状态,在片选有效后,通过接收中断获取数据,并回传信息,再次进入接收状态。
8 {; Y/ w O! |0 b: _(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>
复制代码
/ h& a$ ^$ Q- ]2 p(2)SPI发送数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。 0 ]: f" ?# b6 s( F& K! |
三、A核修改A核设备树中若保留SPI2,内核解析设备树,在/dev下生成设备文件spidev1.0。这样待M核运行后,A核将重新对SPI2初始化,造成M核SPI功能异常,因此需要去除A核对SPI的控制。 , ^4 a; z! n% R4 ~# d
1. 修改设备树 (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;">
$ ~# F. s1 J" x% ^6 Y+ A# |, K - </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;">7 T0 f3 G. _/ Y3 b9 n
- </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>
复制代码 9 h5 l* P K, P. _- o1 I7 [
(2)编译生成新的内核镜像Image及设备树OK8MP-C.dtb。 (3)将生成的OK8MP-C.dtb和Image拷贝至开发板/run/media/mmcblk2p1/目录下,输入sync命令,重启开发板。 (4)输ls /dev查看发现没有SPI2设备文件spidev1.0。
+ E. y7 d) S8 B四、程序验证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 | % _: K$ b/ [# z- s
- f$ d( f/ L; `& H" J! K
7 q% A% Z8 Z9 J8 x F
/ K5 I3 n8 x S- Y: Y, }2. M核程序 修改uboot环境变量设置M核自启动,同时将M核程序forlinx_m7_tcm_firmware.bin 放到/run/media/mmcblk2p1/目录下。注意,SPI主模式程序须放入开发板1,SPI从模式程序须放入开发板2。
3 P% r, e& S$ U3 B3. 实际测试 (1)开发板2先上电,M核程序启动,完成SPI初始化后,进入接收等待状态; (2)开发板1后上电,M核程序启动,完成SPI初始化后,主动发送64字节数据; (3)开发板2的SPI接收数据,通过串口打印接收的数据,并将接收的数据再次发送; - v! J6 v% _7 l0 `* }
; t. t0 Q- q% u5 Y$ F
6 u' l! ?) G# A1 g; @" T* W% ?( u& N
(4)开发板1的SPI接收到回传信息,通过串口打印接收的数据。和发送数据比对,输出结果。 ![]()
, l. Q- p3 c9 }) o" n) B/ H: n+ c+ n8 B2 n6 ^
% M- R2 a: K: V' ~& L
(5)此时在开发板1调试串口输入任意键,即可开启新一轮的SPI发送和接收流程。
% l3 |/ K' T. g+ _/ Q4 h8 ?, c0 k/ x" B/ w6 |$ ^3 m
|