现象描述:
spi通信一次后,第二次通信spi的SR寄存器异常0xFF31(MODF, CRCERR, RXNE),通信失败。
以下为调用流程及相关代码:
NFC_ID();
NFC_REG_B_TEST();
//SPI 读 void SPI_Read_Data(uint8_t* w_data, uint32_t w_size, uint8_t* r_data, uint32_t r_size) { SET_SPI_CSS(GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi1, (uint8_t*)w_data, w_size, 1000); HAL_SPI_Receive(&hspi1, (uint8_t*)r_data, r_size, 1000); SET_SPI_CSS(GPIO_PIN_SET); }
#include "st25r3920b.h" static void ReadREG(REGIST_x blockx, uint8_t reg_addr, uint8_t* data, uint32_t size) { reg_addr |= 0x40; uint16_t reg_b = 0; if(REGIST_A == blockx) { SPI_Read_Data(®_addr, 1, data, size); } else if(REGIST_B == blockx) { reg_b = (((uint16_t)0xFB) << 8) | reg_addr; SPI_Read_Data((uint8_t*)®_b, 2, data, size); } } static void WriteREG(REGIST_x blockx, uint8_t reg_addr, uint8_t* data, uint32_t size) { reg_addr |= 0x00; if(REGIST_A == blockx) { SPI_Write_Data(®_addr, 1); SPI_Write_Data(data, size); } else if(REGIST_B == blockx) { uint16_t reg_b = (((uint16_t)0xFB) << 8) | reg_addr; SPI_Write_Data((uint8_t*)®_b, 2); SPI_Write_Data(data, size); } } void NFC_ID(void){ uint8_t chip_id = 0; ReadREG(REGIST_A, CHIP_ID__REG, &chip_id, 1); UART1_printf("ID: 0x%02X\r\n", chip_id); } void NFC_REG_B_TEST(void){ uint8_t value = 0; ReadREG(REGIST_B, TX_DRIVER_TIMING_REG, &value, 1); UART1_printf("0x29 REG: 0x%02X\r\n", value); }
问题分析调试:
用 UART1_printf("SPI_SR:%X\r\n", hspi1.Instance->SR) 输出SPI状态寄存器值
1. 单步调试,取人SR寄存器的值为: 第二次通信前0xFF31
怀疑两次SPI通信间的间隔时间太短 ==》添加延时函数后,无效果。
2. 将串口输出当道第一次通信之前,第一次通信前0x2; 发现输出后两次通信均异常
根据经验怀疑可能是 勾选了 “Use MicroLIB” 造成的,去掉勾选后果然正常了。
标签:STM32L073RZT,uint8,通信,SPI,data,reg,调试,size From: https://www.cnblogs.com/boring-luobo/p/18283677