cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication STM32F429ZI and L3GD20

nikolacabrilo
Associate

Hello,

I am trying to get started with gyroscope L3GD20 on discovery board. I had problems with HAL SPI library so I wrote my own. When I try to communicate with gyro I cant get any answer, I try to read WHO_AM_I register but all the time I got answer 255. Sometimes when I play with address I got value 155 for example and that's it.

Can You tell me if there is some mistake in my code? Thanks in advance

void Initialize_MEMS_Onboard();
void InitializeSPIManually();
void ReadFromMEMSManually(uint8_t address);
void SendDataSPIManually(uint8_t dataToSend);
uint8_t counterForReceive = 0;
 
uint8_t data1;
 
 
 
 
 
int
main(int argc, char* argv[])
{
  // At this stage the system clock should have already been configured
  // at high speed.
	HAL_Init();
	Initialize_MEMS_Onboard();
 
 
  // Infinite loop
  while (1)
    {
       // Add your code here.
    }
}
 
 
#define CTRL_REG1 0x20
#define CTRL_REG3 0x22
#define CTRL_REG5 0x24
#define FIFO_CTRL_REG 0x2E
#define FIFO_START_ADDRESS 0x28
#define WHO_AM_I 0x0F
void Initialize_MEMS_Onboard(){
 
 
	__SPI5_CLK_ENABLE();
	InitializeSPIManually();
 
 
 
	__HAL_RCC_GPIOF_CLK_ENABLE();
	__HAL_RCC_GPIOC_CLK_ENABLE();
	__HAL_RCC_GPIOA_CLK_ENABLE();
	GPIO_InitTypeDef GPIO_InitStruct;
 
	GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
	HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
 
	GPIO_InitStruct.Pin = GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_SET);
 
	GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
	ReadFromMEMSManually(WHO_AM_I);
 
 
 
}
 
 
 
 
void ReadFromMEMSManually(uint8_t address){
	uint8_t addressToSend = (address<<2) | 0b01;
	counterForReceive = 0;
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_RESET);
	SendDataSPIManually(addressToSend);
	SendDataSPIManually(0xff);
	while(counterForReceive<2);
	counterForReceive = 0;
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_SET);
	trace_printf("%d \n",data1);
}
 
void InitializeSPIManually(){
	SPI5->CR1 |= SPI_CR1_BR;
	SPI5->CR1 |= SPI_CR1_CPOL;
	SPI5->CR1 |= SPI_CR1_SSM;
	SPI5->CR1 |= SPI_CR1_SSI;
	//SPI5->CR2 |= SPI_CR2_TXEIE;
	SPI5->CR2 |= SPI_CR2_RXNEIE;
	SPI5->CR1 |= SPI_CR1_MSTR;
	SPI5->CR1 |= SPI_CR1_SPE;
	HAL_NVIC_EnableIRQ(SPI5_IRQn);
	HAL_NVIC_SetPriority(SPI5_IRQn, 0x0,0);
 
}
 
void SendDataSPIManually(uint8_t dataToSend){
	while((SPI5->SR & SPI_SR_TXE) == 0 );
	SPI5->DR = dataToSend;
}
 
void SPI5_IRQHandler(void){
	if(SPI5->SR & SPI_SR_RXNE){
		//receive data here
		data1 = SPI5->DR;
		counterForReceive++;
	}
	if(SPI5->SR & SPI_SR_TXE){
 
		}
}

0 REPLIES 0