问题描述
我正在使用 STM32F407,只想翻译 Arduino SPI.transfer() 函数。
我想在从 IC 发送 4 个字节后接收 8 个字节。根据我的理解,传输是正确的,但我使用的 HAL_SPI_TransmitReceive 函数不会导致缓冲区在错误的时间或错误的数据读取数据。
在这里你可以看到我的传输: Logic Analyzer
接收从逻辑分析仪读取的字节: 0xc6、0x52、0x17、0xA4、0x00、0x30、0x04、0x8E
这里是我的控制台日志的字节数: 0xc2、0x00、0x03、0x80、0x00、0x10、0x00、0x86
我不知道为什么读数不一样。
这些是我的写入/接收功能:
void spi_write(uint8_t data) // Byte to be written to SPI port
{
HAL_SPI_Transmit(&hspi1,&data,1,1);
}
int8_t spi_read(uint8_t data) //!The data byte to be written
{
uint8_t spi_buf;
if(HAL_SPI_TransmitReceive(&hspi1,&spi_buf,10)){
SPI_Error_Handler();
}
return spi_buf;
}
void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port
uint8_t tx_len,//length of the tx data arry
uint8_t *rx_data,//Input: array that will store the data read by the SPI port
uint8_t rx_len //Option: number of bytes to be read from the SPI port
)
{
for (uint8_t i = 0; i < tx_len; i++)
{
spi_write(tx_Data[i]);
}
for (uint8_t i = 0; i < rx_len; i++)
{
rx_data[i] = spi_read(0xFF);
}
}
在 Arduino 板上,通过使用 SPI.transfer 而不是我的 spi_write/read,代码运行良好。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)