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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
SPI(串行外围设备接口)是一种低成本、易使用的接口协议,具备全双工、高速、通讯简单的特点,被广泛应用于微控制器和外围设备芯片之间的通讯。当SPI接口作为主模式时可以连接Flash存储器、AD采样芯片、实时时钟RTC、LCD显示屏、音频芯片以及各种传感器。
0 g& @  u0 [8 O4 E$ V3 r/ j" w
' w6 V9 i/ X+ h
随着产品功能的愈加丰富,多处理器使用SPI接口进行通讯的场景开始出现,而多个SPI设备之间通信必须由主设备(Master)来控制从设备(Slave)。
小编手上的OKMX8MP-C开发板基于NXP i.MX8M Plus多核异构处理器设计,它的M核有1路SPI,因而为实现SPI的相互通讯,我们就需要两块OKMX8MP-C开发板的SPI互作主从设备进行通信。本文小编就将从应用角度为大家讲解M核SPI间通讯的实现方式。
' m6 v8 S0 d" w4 H  A! e
" P% C- j. h# y- W3 D2 v

4 K3 S' K: W7 ^0 Y, @5 h) f9 k一、SPI主模式
1. SPI初始化
SPI初始化主要包括总线时钟、管脚和相应寄存器的初始化。具体如下:
, M4 o2 f. H1 D" z4 o
(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>
复制代码
0 p% n& A! F; m6 {4 v
(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>
复制代码
5 j4 n+ J6 ]) o/ I/ r- }% ~5 E. y( l
(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>
复制代码
- i' \7 v* q5 W. D. `, ]
(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>
复制代码
2 ~/ F) `" q4 \
(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>
复制代码

3 A; a7 M. U$ b8 {
(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>
复制代码

' o# {+ W% Y. _; ?* i  U# h
(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>
复制代码
$ y& v9 v2 n% F& b* T! n2 _
(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>
复制代码
. U' E3 W  J" z1 h/ R
2. SPI收发流程
我们分别将两块OKMX8MP-C开发板命名为开发板1和开发板2,并且将开发板1的SPI接口采用主模式,使能收发中断;将开发板2的SPI接口采用从模式,使能收发中断。
SPI主发送64字节数据,SPI从接收后,将数据回传。SPI主接收回传信息后,比对接收和发送的数据是否一致,输出比对结果。如一致,本次传输结束,等待输入任何按键启动下一次传输。
( K+ ^7 C* T& n& c
(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 ?+ @! z$ E9 Q) k2 U3 O  M
(2)SPI接收数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。

% a: Y# M! b' ?6 J& @
(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>
复制代码

- G, _; |2 Y5 q- {二、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>
复制代码
& d' c& U' P( D1 X
+ J' `8 S* A8 l* a' @
2. SPI收发流程
开发板2的SPI接口采用从模式,使能收发中断。
SPI从进入等待接收状态,在片选有效后,通过接收中断获取数据,并回传信息,再次进入接收状态。
) I# `/ s( A+ y9 G  V% Q
(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>
复制代码

" ~  K( O9 x% c. `0 [
(2)SPI发送数据:SPI总线的发送和接收都是主模式控制的,因此接收函数的过程和发送是一致的。
. Q) o4 v- s- r1 G1 d9 `8 X& }
三、A核修改
A核设备树中若保留SPI2,内核解析设备树,在/dev下生成设备文件spidev1.0。这样待M核运行后,A核将重新对SPI2初始化,造成M核SPI功能异常,因此需要去除A核对SPI的控制。
# a6 p4 h$ e% L2 J% X
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;">4 P4 R! n) K" L- `! w  b' W
  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;">3 E# L" h& n. ]# L
  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>
复制代码
! A8 ^3 P% O  y5 O: n8 M
(2)编译生成新的内核镜像Image及设备树OK8MP-C.dtb。
(3)将生成的OK8MP-C.dtb和Image拷贝至开发板/run/media/mmcblk2p1/目录下,输入sync命令,重启开发板。
(4)输ls /dev查看发现没有SPI2设备文件spidev1.0。

! t& u7 Q2 o6 B, G9 t四、程序验证
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 `8 a: i2 n; r+ H) A. L
5 N. {: A' y; ?9 D: _/ E+ d/ l- ~
) ?& e7 S, i8 p! O5 q7 {" ]1 E
+ ^, _" ]6 ^) i' I. t1 F: [, y
2. M核程序
修改uboot环境变量设置M核自启动,同时将M核程序forlinx_m7_tcm_firmware.bin
放到/run/media/mmcblk2p1/目录下。注意,SPI主模式程序须放入开发板1,SPI从模式程序须放入开发板2。

8 D7 t& L+ p; y7 K! u7 v% R6 U
3. 实际测试
(1)开发板2先上电,M核程序启动,完成SPI初始化后,进入接收等待状态;
(2)开发板1后上电,M核程序启动,完成SPI初始化后,主动发送64字节数据;
(3)开发板2的SPI接收数据,通过串口打印接收的数据,并将接收的数据再次发送;
6 K$ @) j2 A) M4 S" s2 b
1 G( q* e+ ^- e% ?# X( M: _
( @( f3 L- }( L' g; s$ t" ?
(4)开发板1的SPI接收到回传信息,通过串口打印接收的数据。和发送数据比对,输出结果。
% i6 Y  L4 i4 g) u- X! ?' u
" A3 ]1 R. d8 e

- a. f$ t& |/ X  x
(5)此时在开发板1调试串口输入任意键,即可开启新一轮的SPI发送和接收流程。
8 V+ Z% W* |8 X3 K; ~8 k  O# j: M
& H, V. p, X3 {6 d& Q* p8 z3 R

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-6-14 13:26 , Processed in 0.093750 second(s), 23 queries , Gzip On.

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

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

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