using SPI to read register value
Hello Guys
I am asking you kindly your help.
I am trying to read register data from the LSM6DS3 IMU sensor from STM using SPI. I succeeded reading the value 0x69 from register WHO_AM_I using Arduino.
Now I am stuck when trying to do it with STM32 micro-controller.
Reading data sheet I concluded the following specs: Max SCLK 10MHz, 8 bits word, MSB first, CPOL high, CPAHSE 2 Edge (sampling on trailing edge).
I think I am setting the stm32 micro-controller like so but I am not able to read any data from register WHO_AM_I .
The following code is the reading process, followed by the SPI init routine.
the problem is when I track the evolution of MOSI line and SCLK by doing a transmit then receive, I am getting the reading in picture 1 using oscilloscope,
it look different than when I use the "HAL_SPI_TransmitReceive" as shown in picture 2. in the case of transmit then receive I can read 0x8F on oscilloscope but also an other byte 0x00 which I dont know where it is coming from. in case of "HAL_SPI_TransmitReceive" I can read 0x8F only.
In both ways I can read nothing on the MISO line
uint8_t spitx[2],spirx[2];
uint8_t WHO_AM_I = 0x0F;
while (1)
{
spitx[0] = WHO_AM_I | 0x80;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0,GPIO_PIN_RESET);
// HAL_SPI_TransmitReceive (&hspi1, spitx, spirx, 1, 50);
HAL_SPI_Transmit (&hspi1, spitx, 1, 50);
HAL_SPI_Receive (&hspi1, spirx, 1, 50);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0,GPIO_PIN_SET);
memset(spitx,0,2);
memset(spirx,0,2);
HAL_Delay(50);
}
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}

