2024-10-23 05:53 AM
I am attempting to talk to BMP280 over SPI using Nucelo-F401RE.
I am using SPI2 on the F401RE board to do so, I am able to read from it correctly but I can't seem to write to it in order to communicate with it.
My code is as follows
/* USER CODE BEGIN WHILE */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Setting this high, to not select the chip
char send_buff [8] = {0,0,0,0,0,0,0,0};
char recieve_buff_0 [8] = {0,0,0,0,0,0,0,0};
char recieve_buff_1 [8] = {0,0,0,0,0,0,0,0};
HAL_StatusTypeDef ret;
while (1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Setting this low to enable the chip
send_buff[0] = 208;
ret = HAL_SPI_Transmit(&hspi2, send_buff, 1, 1000); // Reading the register ID
ret = HAL_SPI_Receive(&hspi2, recieve_buff_0, 1, 1000); // I receive 88 (0x58) back which is correct
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Setting this high, to not select the chip
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Setting this low to enable the chip
send_buff[0] = 244;
send_buff[1] = 39;
ret = HAL_SPI_Transmit(&hspi2, send_buff, 2, 1000); // Reading the ctrl_meas register
ret = HAL_SPI_Receive(&hspi2, recieve_buff_1, 1, 1000); // Should receive 39 but getting back all 0s
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Setting this high, to not select the chip
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
My SPI settings are as it follows:
I have attached the bmp280 datasheet as well as the main.c file for my project.
I have attached some images of my project. (I have got this sensor working with I2C and searched the internet for a possible solution but every git repo seems to only have it working with I2C not SPI).
2024-12-03 07:40 AM
Hello,
you've enabled full duplex mode. Correct function is HAL_SPI_TransmitReceive at this case. It depends on protocol with the device. I suppose you need to provide 8x SCK to write a command and then next 8x SCK to read the device status. You can call the function with two data protocol while perform dummy data for the first read out and second for the write in. Great probably you need to split it into two calls to make the SCK flow discontinuous between the data write and read what provides more time for slave to prepare proper response of the command.
Rgds,
Petr