提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
记录一下在RTT下使用SPI从机功能。
一、器件准备
1、USB转SPI调试工具。
2、STM32F429一块。
二、使用步骤
1.配置cubemx
将SPI2配置成全双工从模式、16Bit、MSB模式、软件CS。
2.配置ENV
打开SPI2功能。
3.软件中注册和配置SPI
int rt_hw_spi_flash_init_2(void)
{
rt_hw_spi_device_attach("spi2", "spi20", GPIOH, GPIO_PIN_8);
return RT_EOK;
}
rt_hw_spi_flash_init_2();
spi_dev_2 = (struct rt_spi_device *)rt_device_find("spi20");
struct rt_spi_configuration cfg_2;
cfg_2.data_width = 16;
cfg_2.mode = RT_SPI_SLAVE | RT_SPI_MODE_0 | RT_SPI_MSB|RT_SPI_NO_CS;
cfg_2.max_hz = 8 * 1000 *1000; /* 20M */
rt_spi_configure(spi_dev_2, &cfg_2);
使用
开启一个线程并采用阻塞的方式进行SPI收发。
void SPI2_recv_send(void *args)
{
while(1)
{
len = rt_spi_recv(spi_dev_2,rece,2);
if(len == 2)
{
rt_kprintf("rece is:%x,%x\n", rece[0],rece[1]);
len = rt_spi_send(spi_dev_2,send , 2);
if(len == 2)
{
rt_kprintf("send is:%x,%x\n", send[0],send[1]);
}
rt_thread_yield();
}
}
}
如果主机端使用的是rt_spi_send_then_recv这样的函数发送数据则在rt_spi_recv执行完后给主机返回和主机端发送数据相同大小的无效数据,主机再发一次数据使得rt_spi_send执行后返给主机的才是有效数据。