Hello giwed,
For your SPI read and write functio, they are no correct. You could refer the following reference code on STM32 and test it again.
uint8_t GTXBuffer[512], GRXBuffer[512];
/******************************************************************************* * Function Name : SPI_Read * Description : Write data into slave device. * Input : I2C1 or I2C2, slave_address7, subaddress, Write Number * Output : None * Return : number of bytes transmitted *******************************************************************************/ int8_t SensorAPI_SPIx_Read(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t ReadNumbr) { slave_address7 = slave_address7; GTXBuffer[0] = subaddress | 0x80;
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // NSS low
HAL_SPI_TransmitReceive(&SPI_HANDLE, GTXBuffer, GRXBuffer, ReadNumbr+1, BUS_TIMEOUT); // timeout 1000msec; while(SPI_HANDLE.State == HAL_SPI_STATE_BUSY); // wait for xmission complete
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // NSS high memcpy(pBuffer, GRXBuffer+1, ReadNumbr);
return 0; }
int8_t SensorAPI_SPIx_Write(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t WriteNumbr) { slave_address7 = slave_address7; GTXBuffer[0] = subaddress & 0x7F; memcpy(>XBuffer[1], pBuffer, WriteNumbr);
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // NSS low
//HAL_SPI_TransmitReceive(&hspi2, pTxData, pRxData, WriteNumbr+1, BUS_TIMEOUT); // send register address + write data HAL_SPI_Transmit(&SPI_HANDLE, GTXBuffer, WriteNumbr+1, BUS_TIMEOUT); // send register address + write data while(SPI_HANDLE.State == HAL_SPI_STATE_BUSY); // wait for xmission complete
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // NSS high
return 0; }
... View more