Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

2 of 2
added 42 characters in body
hcheung
  • 4.4k
  • 3
  • 16
  • 28

LSM6DS3 SPI-communication problem with STM32F4

I'm having trouble getting data from the LSM6DS3 sensor over 4-wire SPI. The datasheet says 4-wire SPI is the default, so I didn't send anything for the sensor settings, and immediately tried to read it. When I try to read the value of the "WHO_AM_I" register, I get too short pulses in response that cannot be read.

I use this board. I use this board.

4-wire SPI. Master - STM32F411.
LSM6DS3 to STM32F411
3v3 to 3,3 В
GND to GND
SA0 to PA6 - SPI1 MISO
CS to PA4 - GPIOA4
SDA to PA7 - SPI1 MOSI
SCL to PA5 - SPI1 SCK

In the case when I send the value 0x8F (1 read bit + 00001111 "WHO_AM_I" address), I get the result as shown below. At the same time I should get the value 0x69 or 01101001 in binary form. And if you look carefully at the results below, you will see that something is interfering, the pulse is too short to be recorded.

Oscilloscope

Logic analyzer

For some reason I can't insert the table of registers that is in clause 9.11 "WHO_AM_I (0Fh)", so here is the link: (https://www.alldatasheet.com/datasheet-pdf/pdf/796179/STMICROELECTRONICS/LSM6DS3.html) I tried to read the value in another register "CTRL9_XL" and there the same situation. I should get 00111000, but I got 00110000, as if the pulses were compressed and recorded one less value: Oscilloscope Logic analyzer

 //SPI config:
 void SpiConfig(void){
 RCC->APB2ENR |= (1 << 12);
 /////////////////////////////////////////////////// //////////
 SPI1->CR1 |= (1 << 0) | (1 << 1); //CPHA=1 CPOL=1
 SPI1->CR1 |= (1 << 2); //Master mod
 SPI1->CR1 |= (2 << 3); //010: fPCLK/8 BR[2:0] 5 MHz SPI CLK
 SPI1->CR1 &= ~(1 << 7); //LSBFIRST = 0 => MSB first, then LSB
 SPI1->CR1 |= (1 << 8 ) | (1 << 9); //High level SSI and SSM Software control enabled
 SPI1->CR1 &= ~(1 << 10); //RXONLY = 0 => Full duplex mode
 SPI1->CR1 &= ~(1 << 11); //8-bit
 SPI1->CR2 = 0;
 }
 //GPIO config:
 void SpiGPIOConfig(void){
 RCC->AHB1ENR |= (1 << 0); //Enable RCC for GPIO_A
 GPIOA->MODER |= (2 << 10) | (2 << 12) | (2 << 14) | (1 << 8);
 //Alternative function for PA5/6/7 common output for PA4
 GPIOA->MODER &= ~(1 << 9); //Push-Pull for PA5/6/7/4 (default)
 GPIOA->OSPEEDR |= (3 << 10) | (3 << 12) | (3 << 14) | (3 << 8); //High speed for PA5/6/7/4
 GPIOA->AFR[0] |= (5 << 20) | (5 << 24) | (5 << 28); //AF5 (AFRL 0 - 7) on PA5/6/7
 }
 //Transmit:
 void SPI_Transmit (uint8_t *data, int size){
 //1. Wait for the TXE bit to be set in the registration state
 int i = 0;
 while(i < size){
 while (!((SPI1->SR) & (1 << 1))){}; //Wait for the TXE bit to be set -> This indicates that the buffer is empty
 SPI1->DR = data[i]; //Load the data into the data register
 i++;
 }
 while(!((SPI1->SR) & (1 << 1))){}; //Wait for the TXE bit to be set -> This indicates that the buffer is empty
 while(((SPI1->SR) & (1 << 7))){}; //Wait for the BSY bit to clear -> This means that the SPIs are not busy with communications
 uint8_t temp = SPI1->DR;
 temperature = SPI1->SR;
 }
 //Main function:
 uint8_t RxData[6];
 address uint8_t = 0x8f;
 while(1){
 CS_EN(); // pull the pin low
 SPI_Transmit (&address, 1); // send the address
 SPI_Receive (RxData, 1); // get 1 byte of data
 CS_DIS(); // pull the pin high
 Delay_ms(1);
 }

If you need to provide something else to find the error, then tell me, I will provide everything you need

AltStyle によって変換されたページ (->オリジナル) /