stm32F407与AD7175通信,设置SPI通信模式。读AD7175寄存器数据读错误,把写进去的值读出来,结果不一致,不知道是写没有成功,还是都没有成功。主要代码如下:
//通信设置初始化
void ad7175_spi_Init()
{
SPI_InitTypeDef spi_init;
GPIO_InitTypeDef gpio_init;
ad7175_t* pt = (ad7175_t*)&Ad7175;
pt->spi = SPI2;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
gpio_init.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
gpio_init.GPIO_Mode = GPIO_Mode_AF;
gpio_init.GPIO_Speed = GPIO_Speed_100MHz;
gpio_init.GPIO_OType = GPIO_OType_PP;
gpio_init.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOB, &gpio_init);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spi_init.SPI_Mode = SPI_Mode_Master;
spi_init.SPI_DataSize = SPI_DataSize_8b;
spi_init.SPI_CPOL = SPI_CPOL_High;
spi_init.SPI_CPHA = SPI_CPHA_2Edge;//mode 3
spi_init.SPI_NSS = SPI_NSS_Soft;
spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
spi_init.SPI_CRCPolynomial = 7;
SPI_Init(pt->spi,&spi_init);
SPI_Cmd(pt->spi,ENABLE);
pin_out_set(PIN_AD7175_CS,1); //
}
int32_t AD7175_ReadRegister(st_reg* pReg)
{
int32_t ret = 0;
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;
uint8_t crc = 0;
/* Build the Command word */
buffer[0] = COMM_REG_WEN | COMM_REG_RD | pReg->addr;
/* Read data from the device *///huangxh_2020-3-4???
ret = SPI_Read(AD7175_SLAVE_ID,
buffer,
(AD7175_st.statFollow ? pReg->size + 1 : pReg->size) + 1);
if(ret < 0)
return ret;
/* Check the CRC */
if(AD7175_st.useCRC)
{
crc = AD7175_ComputeCRC(&buffer[1], pReg->size + 1);//???
if(crc != AD7175_CRC_CHECK_CODE)
return -1;
}
/* Build the result */
pReg->value = 0;
for(i = 1; i < pReg->size + 1; i++)
{
pReg->value <<= 8;
pReg->value += buffer;
}
return ret;
}
int32_t AD7175_WriteRegister(st_reg reg)
{
int32_t ret = 0;
int32_t regValue = 0;
uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;
uint8_t crc = 0;
/* Build the Command word */
wrBuf[0] = COMM_REG_WEN | COMM_REG_WR | reg.addr;
/* Fill the write buffer */
regValue = reg.value;
for(i = 0; i < reg.size; i++)
{
wrBuf[reg.size - i] = regValue & 0xFF;
regValue >>= 8;
}
/* Compute the CRC */
if(AD7175_st.useCRC)
{
crc = AD7175_ComputeCRC(wrBuf, reg.size+1);
wrBuf[reg.size + 1] = ~crc;
}
/* Write data to the device */
/*ret = SPI_Write(AD7175_SLAVE_ID,
wrBuf,
AD7175_st.useCRC ? reg.size + 2 : reg.size + 1);*/
ret = SPI_Write(AD7175_SLAVE_ID,
wrBuf,
AD7175_st.useCRC ? reg.size + 1 : reg.size);
return ret;
}
//数据读写函数
*******************************************************************************/
u8 SPI_Write(u8 slaveDeviceId,u8* data,u8 bytesNumber)
{
// Add your code here.
if(bytesNumber<1) return -1;
pin_out_set(PIN_AD7175_CS,0);
SPI_I2S_ReadWriteByte(data[0]);
for(int i =0;i<bytesNumber;i++)
{
SPI_I2S_ReadWriteByte(data[i+1]);
}
pin_out_set(PIN_AD7175_CS,1);
return 0;
}
/***************************************************************************//**
* @brief Reads data from SPI.
*
* @param slaveDeviceId - The ID of the selected slave device.
* @param data - Data represents the read buffer.
* @param bytesNumber - Number of bytes to read.
*
* @return Number of read bytes.
*******************************************************************************/
u8 SPI_Read(u8 slaveDeviceId,u8* data,u8 bytesNumber)
{
// Add your code here.
if(bytesNumber<1) return -1;
pin_out_set(PIN_AD7175_CS,0);
u8 tst = SPI_I2S_ReadWriteByte(data[0]);
for(int i =0;i<bytesNumber;i++)
{
data[i+1] = SPI_I2S_ReadWriteByte(0xff);
}
pin_out_set(PIN_AD7175_CS,1);
return 0;
}
u8 SPI_I2S_ReadWriteByte(u8 txData)//huangxh_2020-3-5
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET){}
SPI_I2S_SendData(SPI2,txData);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET){}
return SPI_I2S_ReceiveData(SPI2);
} |