调试 LIS3DH这个3轴加速度传感器,用spi读取数据。移植官方的驱动,关键要实现 u8_t LIS3DH_ReadReg(u8_t Reg, u8_t* Data);
u8_t LIS3DH_WriteReg(u8_t WriteAddr, u8_t Data);
这两个接口,我的实现如下:
u8_t LIS3DH_ReadReg(u8_t Reg, u8_t* Data) {
//To be completed with either I2c or SPI reading function
//i.e. *Data = SPI_Mems_Read_Reg( Reg );
HAL_StatusTypeDef ret;
u8_t read_set;
read_set = (Reg | 0x80);
/* LOW */
SPI_CS_RESET();
ret = HAL_SPI_Transmit(&hspi1, &read_set, sizeof(u8_t), 1000);
if(ret != HAL_OK)
{
return 0;
}
ret = HAL_SPI_Transmit(&hspi1, 0, sizeof(u8_t), 100);
ret = HAL_SPI_Receive(&hspi1, (u8_t*)Data, sizeof(u8_t), 1000);
if(ret != HAL_OK)
{
return 0;
}
/* HIGH */
SPI_CS_SET();
HAL_Delay(2);
return 1;
}
关键在于,传感器多次中断以后,HAL_SPI_Transmit这个函数就挂掉了,一直返回 HAL_busy,具体一看是 hspi->State 这个状态不对,
打印出来发现状态值不属于这个枚举之一,是一个乱七八糟的值,每次还不一样?
typedef enum
{
HAL_SPI_STATE_RESET = 0x00U, /*!< SPI not yet initialized or disabled */
HAL_SPI_STATE_READY = 0x01U, /*!< SPI initialized and ready for use */
HAL_SPI_STATE_BUSY = 0x02U, /*!< SPI process is ongoing */
HAL_SPI_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
HAL_SPI_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
HAL_SPI_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission and Reception process is ongoing */
HAL_SPI_STATE_ERROR = 0x03U /*!< SPI error state */
}HAL_SPI_StateTypeDef;
关键,HAL-busy 以后就再也不会ready了,因此中断再进来,也无法读取成功。。
求大神解答啊
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>