lugl4313820 发表于 2022-2-7 19:14

【国民技术N32G457评测】 RT_Thread Studio SPI 填坑

本帖最后由 lugl4313820 于 2022-2-7 19:30 编辑

<p>上午解决了NSS不能拉低的问题:</p>

<p>查看了drv_spi.c的配置函数:</p>

<p>结合调试的数据:</p>

<p>发现CLKPOL为2,而我传进去的参数是:</p>

<p>cfg.mode = &nbsp;RT_SPI_MODE_MASK | RT_SPI_MODE_1 | RT_SPI_MSB ; //配置为spi主,MODE1 (CPOL=0,CPHA=1)</p>

<p>感觉是不是两个配合有问题:drv_spi.c的配置为:</p>

<p>switch(configuration-&gt;mode &amp; RT_SPI_MODE_3)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; case RT_SPI_MODE_0:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPOL &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPOL_LOW;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPHA &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPHA_FIRST_EDGE;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; case RT_SPI_MODE_1:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPOL &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPOL_LOW;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPHA &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPHA_SECOND_EDGE;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; case RT_SPI_MODE_2:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPOL &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPOL_HIGH;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPHA &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPHA_FIRST_EDGE;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; case RT_SPI_MODE_3:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPOL &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPOL_HIGH;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SPI_InitStructure.CLKPHA &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPHA_SECOND_EDGE;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; }</p>

<p>是不是不能三个参数一起传进去,因为下面的配置参数直接写死了几个参数:</p>

<p>/*!&lt; SPI configuration */<br />
&nbsp; &nbsp; SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_TX;<br />
&nbsp; &nbsp; SPI_InitStructure.SpiMode &nbsp; &nbsp; &nbsp; = SPI_MODE_MASTER;<br />
&nbsp; &nbsp; SPI_InitStructure.CLKPHA &nbsp; &nbsp; &nbsp; &nbsp;= SPI_CLKPHA_SECOND_EDGE;<br />
&nbsp; &nbsp; SPI_InitStructure.NSS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = SPI_NSS_SOFT;<br />
&nbsp; &nbsp; SPI_InitStructure.CRCPoly &nbsp; &nbsp; &nbsp; = 7;</p>

<p>于是我改为mode配置为:</p>

<p>&nbsp;cfg.mode = &nbsp;RT_SPI_MODE_1 &nbsp;;</p>

<p>下载后查看时序图为:</p>

<p>&nbsp;感觉又进了一步,spi发出的数据已经有了。</p>

<p>但是只发送了一位数据,而且不是我发出去的数。</p>

<p>翻回去看了一下,没有指定数据传顺序:</p>

<p>更新成:</p>

<p>cfg.mode = &nbsp;RT_SPI_MODE_1 | &nbsp;RT_SPI_MSB;</p>

<p>下载后时序图如下:</p>

<p>&nbsp;发出来的数据也对了,就是卡在FlagStatus SPI_I2S_GetStatus(SPI_Module* SPIx, uint16_t SPI_I2S_FLAG);这里。</p>

<p>看来还是哪里配置有问题。</p>

<p>再次调试发现卡在等待接收标志那里,我本来是发命令只send,没有接收的。。这。。。。</p>

<p>再次回去修改drv_spi.c的配置函数:</p>

<p>&nbsp; &nbsp; /*!&lt; SPI configuration */<br />
&nbsp; &nbsp; SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX; //再次修改为双工</p>

<p>然后发送成功:</p>

<p>&nbsp;至此spi的发送已经调通:</p>

<p>最后总结:</p>

<p>1、drv_spi.c 的&nbsp;configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)接收cs的结构体要设置成:</p>

<p>struct n32_hw_spi_cs<br />
{<br />
&nbsp; &nbsp; GPIO_Module* GPIOx;<br />
&nbsp; &nbsp; uint32_t GPIO_Pin;<br />
};</p>

<p>还有传入的mode参数不能加入主从模式。</p>

<p>只能传:</p>

<p>cfg.mode = &nbsp;RT_SPI_MODE_1 | &nbsp;RT_SPI_MSB;这两个参数</p>

<p>最后把工程上传吧,希望给要用RT_Thread Studio 使用N32G45的人有点建议。</p>

<p>调整传输速率为70M然测试,实际工作频率为50M为最大频率。</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

Jacktang 发表于 2022-2-8 07:25

<p>最后总结中传入的mode参数不能加入主从模式么</p>

<p>谢谢楼主的建议</p>

<p>&nbsp;</p>

lugl4313820 发表于 2022-2-8 09:54

Jacktang 发表于 2022-2-8 07:25
最后总结中传入的mode参数不能加入主从模式么

谢谢楼主的建议

&nbsp;

<p>其实这个drv_spi.c驱动是网友提供的,目前还没有看到有国民技术提供的drv_spi.c的驱动。drv_spi.c中写死了只能作为maste模式来工作。很多配置只能手动去修改。包括NSS模式。</p>
页: [1]
查看完整版本: 【国民技术N32G457评测】 RT_Thread Studio SPI 填坑