问题描述
我正在尝试使用 stm32 LL 库(用于 STML4 系统)编写 SPI 驱动程序。我正在通过向 MOSI 线写入 2 个字节并在 MISO 线上侦听 1 个字节来测试 SPI 驱动程序。使用示波器,我能够验证 2 个字节是否正确传输,并且 SPI 从设备以一个字节响应。见附件截图:
在图像中,我正在探测 SCLK 和 MISO 线。显然,MISO 线上的最后 8 位是 0b01110011,这是预期的数据。我写的驱动程序没有反映这一点,总是从 SPI DR 寄存器读取 0。由于 SPI 外设(也许我遗漏了一些东西),我在尝试在调试模式下运行代码时遇到问题,并且无法使用 printf 输出值(无法访问 UART 接口)。我希望对可能存在的问题有一些想法。
void spi_transmit_receive( SPI_TypeDef* spi,uint8_t* tx,uint8_t* rx,uint16_t size )
{
if( !LL_SPI_IsEnabled( spi ) )
{
LL_SPI_Enable( spi );
}
if( size > 1 )
{
LL_SPI_SetRxFIFOThreshold( spi,LL_SPI_RX_FIFO_HALF_FULL );
}
else
{
LL_SPI_SetRxFIFOThreshold( spi,LL_SPI_RX_FIFO_QUARTER_FULL );
}
uint8_t* rx_ptr = rx;
uint8_t* tx_ptr = tx;
uint16_t tx_count = size;
uint16_t rx_count = size;
while( tx_count > 0 || rx_count > 0 )
{
if( tx_count > 0 && LL_SPI_IsActiveFlag_TXE( spi ) )
{
if( tx_count > 1 )
{
LL_SPI_TransmitData16( spi,*((uint16_t*)tx_ptr) );
tx_ptr += sizeof( uint16_t );
tx_count -= 2;
}
else
{
LL_SPI_TransmitData8( spi,*tx_ptr );
tx_count -= 1;
}
}
if( rx_count > 0 && LL_SPI_IsActiveFlag_RXNE( spi ) )
{
if( rx_count > 1 )
{
*((uint16_t*)rx_ptr) = LL_SPI_ReceiveData16( spi );
rx_ptr += sizeof( uint16_t );
rx_count -= 2;
if( rx_count <= 1 )
{
// Switch to 8 bit mode for last 8 bits
LL_SPI_SetRxFIFOThreshold( spi,LL_SPI_RX_FIFO_QUARTER_FULL );
}
}
else
{
*rx_ptr = LL_SPI_ReceiveData8( spi );
//rx_ptr += sizeof(uint8_t);
rx_count -= 1;
}
}
}
while( LL_SPI_IsActiveFlag_BSY( spi ) ) {}
LL_SPI_Disable( spi );
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)