I have an STM32F303 discovery board for which the mems gyro L3GD20H is connected to the SPI1 peripheral by default, that is the Sclk, MISO and MOSI are connected to pins PA5, PA6 and PA7 respectively. The chip enable is connected to PE3. I need to understand the high level functions from the HAL library for the STM32F3 microcontroller. Hence, for experimentation, I try to communicate with the gyro using HAL_SPI_Transmit() and HAL_SPI_Receive().
I use CubeMX to initialize all peripherals. Settings for the SPI1 is shown below
Since CubeMX takes care of initialization code, I will only show the additional code that enables the control register ctrlReg1 with 0x0f (see figure)
I do not care about settings, I just wish to get some data in polling mode. The XDataOut_L register (0x28) is the one that I try to read
I use CubeMX and AC6, that is System Workbench (Eclipse-based), to build/compile and run/debug the project. The user-defined code is as follows
uint8_t data_ctrl1, address_ctrl1;
uint8_t data_out1, address_out1;
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3, GPIO_PIN_RESET);
address_ctrl1 = 0x20;
data_ctrl1 = 0x0f;
HAL_SPI_Transmit(&hspi1, &address_ctrl1, 1, 50);
HAL_SPI_Transmit(&hspi1, &data_ctrl1, 1, 50);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3, GPIO_PIN_SET);
while(1)
{
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
address_out1 = 0x28 | 0x80; // MSB bit = 1 to read
HAL_SPI_Transmit(&hspi1, &address_out1, 1, 50);
HAL_SPI_Receive(&hspi1, &data_out1, 1, 50);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
HAL_Delay(100);
}
The code is based on an example on YouTube (MohamedYaqoobSTM) where Mohamed successfully reads his LIS302DL on his STM32F4 Discovery board.
My question is why will not the SPI_HAL_Receive() work as intended? I always get result 255 on the data_out1 buffer...