EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
SPI(串行外围设备接口)是一种低成本、易使用的接口协议,具备全双工、高速、通讯简单的特点,被广泛应用于微控制器和外围设备芯片之间的通讯。当SPI接口作为主模式时可以连接Flash存储器、AD采样芯片、实时时钟RTC、LCD显示屏、音频芯片以及各种传感器。 , N- @$ K! [6 f8 d
. H- t. F2 X4 n( Z L
随着产品功能的愈加丰富,多处理器使用SPI接口进行通讯的场景开始出现,而多个SPI设备之间通信必须由主设备(Master)来控制从设备(Slave)。 小编手上的OKMX8MP-C开发板基于NXP i.MX8M Plus多核异构处理器设计,它的M核有1路SPI,因而为实现SPI的相互通讯,我们就需要两块OKMX8MP-C开发板的SPI互作主从设备进行通信。本文小编就将从应用角度为大家讲解M核SPI间通讯的实现方式。 2 k4 G5 [( C$ H; G# A/ D+ b8 P3 z6 V
* h+ U3 g% V2 r, u5 Y. k$ J8 Y
/ [, `% ^3 V g9 v# |
一、SPI主模式1. SPI初始化 SPI初始化主要包括总线时钟、管脚和相应寄存器的初始化。具体如下: 7 C) t; ^8 `9 T! `
(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>
复制代码
& L$ [* v$ s% |3 g( d2 d(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>
复制代码 6 [7 F0 U1 r% O: g' J& e
(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>
复制代码 % }" `6 J) `' W, B* D
(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>
复制代码 1 G) Y! {: }) r" Z; g
(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>
复制代码 d. \2 j/ g7 W3 R* c
(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>
复制代码 + g7 U2 H9 [. D/ _6 w* e
(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>
复制代码 1 U: ]9 |9 b9 T1 b7 H9 e0 U
(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>
复制代码 , p, B* y* H: G. L. ^
2. SPI收发流程 我们分别将两块OKMX8MP-C开发板命名为开发板1和开发板2,并且将开发板1的SPI接口采用主模式,使能收发中断;将开发板2的SPI接口采用从模式,使能收发中断。 SPI主发送64字节数据,SPI从接收后,将数据回传。SPI主接收回传信息后,比对接收和发送的数据是否一致,输出比对结果。如一致,本次传输结束,等待输入任何按键启动下一次传输。
1 O% w( _9 n3 d1 {4 ?(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>
复制代码
7 C' v' ~+ P4 X, i! }9 c% d& u9 L(2)SPI接收数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。
2 @; B( Q; q6 i" A0 F(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>
复制代码
9 }# l8 s/ Q1 K4 t二、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>
复制代码 : d& B5 n5 [$ P, M, s
2 u1 G" r! j* Y* Q: x% A2. SPI收发流程 开发板2的SPI接口采用从模式,使能收发中断。 SPI从进入等待接收状态,在片选有效后,通过接收中断获取数据,并回传信息,再次进入接收状态。
; d& v$ S. o, g/ v8 R0 S( H& }(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>
复制代码 / G6 c; B/ }. g6 t+ x; y3 S2 I' X
(2)SPI发送数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。
1 L7 R& O7 z, P* W" Y2 V三、A核修改A核设备树中若保留SPI2,内核解析设备树,在/dev下生成设备文件spidev1.0。这样待M核运行后,A核将重新对SPI2初始化,造成M核SPI功能异常,因此需要去除A核对SPI的控制。
! s2 d4 E8 ~$ j1 k9 r* v- a+ f1. 修改设备树 (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;">0 q4 Y+ M. \/ w0 {, [" y
- </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;">
9 q# ]- }2 |7 ` - </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 l6 p3 A2 t! L+ u4 E+ v6 H0 \(2)编译生成新的内核镜像Image及设备树OK8MP-C.dtb。 (3)将生成的OK8MP-C.dtb和Image拷贝至开发板/run/media/mmcblk2p1/目录下,输入sync命令,重启开发板。 (4)输ls /dev查看发现没有SPI2设备文件spidev1.0。
; E8 d. d$ k" N r四、程序验证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 |
9 j {6 W" s% c
/ `7 ^9 u4 Y1 |' _" i! q8 ]
4 l5 b# w1 d! U; r( I! k( C/ s" |$ K) ^! i& F$ y
2. M核程序 修改uboot环境变量设置M核自启动,同时将M核程序forlinx_m7_tcm_firmware.bin 放到/run/media/mmcblk2p1/目录下。注意,SPI主模式程序须放入开发板1,SPI从模式程序须放入开发板2。
/ ~+ X/ U8 d! X& N3. 实际测试 (1)开发板2先上电,M核程序启动,完成SPI初始化后,进入接收等待状态; (2)开发板1后上电,M核程序启动,完成SPI初始化后,主动发送64字节数据; (3)开发板2的SPI接收数据,通过串口打印接收的数据,并将接收的数据再次发送; ![]()
5 w% y6 E! l/ F' V5 y# R
+ i: f* ^! b: I: q5 H7 K2 ?# H `. ]* M# I ^
(4)开发板1的SPI接收到回传信息,通过串口打印接收的数据。和发送数据比对,输出结果。 & ~ k- n b- \& x: ?9 b
+ o( ^5 F% k' c' U" N) @
2 Z- F* T$ ?0 z- C' U6 e/ V
(5)此时在开发板1调试串口输入任意键,即可开启新一轮的SPI发送和接收流程。
( _0 o3 e6 X3 B; z% @* |: r7 T A7 @) R8 T7 O# {5 F$ H
|