2025-02-16 05:02 AM
Hello,
I'm new to embedded programming, and I'm currently working on establishing a basic SPI connection between an STM32 microcontroller and the BMI270 IMU sensor by Mikroe. My goal is to read the Chip ID (CHIP_ID register at 0x00) and get a response of 0x24, which confirms proper communication,Currently, I'm not getting the expected 0x24 and instead receiving either 0x00 or 0xFF when reading the Chip ID. Any guidance on what might be wrong or missing in my setup would be greatly appreciated, please see code work flow .
Code Execution:
Reference:
[1] - https://download.mikroe.com/documents/datasheets/bst-bmi270-ds000-2_datasheet.pdf
- Full-duplex Master
- Frame Format: Motorola
- First Bit: MSB Frame format
- Prescaler: 16
- BaudRate is 8.59 MBits/s
- CPHOL: High
- CPHA: 2 Edge
SPI PINS:
Below is my Code:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "string.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "stdint.h"
//#include "bmi270.h"
// Define BMI270 Chip Select (CS) pin (adjust as needed)
#define BMI270_CS_PORT GPIOB
#define BMI270_CS_PIN GPIO_PIN_12
// BMI270 Chip ID Register Address
#define BMI270_CHIP_ID_REG 0x00
#define BMI270_EXPECTED_ID 0x24 // Expected Chip ID for BMI270
extern SPI_HandleTypeDef hspi1; // SPI handle from CubeMX
// Read BMI270 Chip ID without functions
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma location=0x30000000
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
#pragma location=0x30000080
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
#elif defined ( __CC_ARM ) /* MDK ARM Compiler */
__attribute__((at(0x30000000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
__attribute__((at(0x30000080))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
#elif defined ( __GNUC__ ) /* GNU Compiler */
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
#endif
ETH_TxPacketConfig TxConfig;
ETH_HandleTypeDef heth;
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ETH_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_USB_OTG_HS_USB_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//Send Print function output to SWV ITM DataConsole
int _write(int file, char *ptr, int len) {
for (int i = 0; i < len; i++) {
ITM_SendChar(ptr[i]); // Send data via SWO
}
return len;
}
// Function to Test BMI270 Communication
/**
void BMI270_Test() {
uint8_t chipID = BMI270_SPI_Read(BMI270_CHIP_ID_REG);
if (chipID == BMI270_EXPECTED_ID) {
printf("BMI270 Detected! Chip ID: 0x%02X\n", chipID);
} else {
while (1) {
printf("BMI270 Not Found! Read: 0x%02X (Expected: 0x24)\n", chipID);
HAL_Delay(200);
}
}
}
/
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ETH_Init();
MX_USART3_UART_Init();
MX_USB_OTG_HS_USB_Init();
MX_SPI1_Init();
/* USER CODE BEGIN 2 */
uint8_t dummyTx = 0x00, dummyRx = 0;
uint8_t txData = BMI270_CHIP_ID_REG | 0x80;
uint8_t rxData = 0;
// Step 1: Dummy Read to Switch BMI270 to SPI Mode
HAL_GPIO_WritePin(BMI270_CS_PORT, BMI270_CS_PIN, GPIO_PIN_RESET);
HAL_Delay(1); // Ensure CS is stable before communication
HAL_SPI_TransmitReceive(&hspi1, &dummyTx, &dummyRx, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(BMI270_CS_PORT, BMI270_CS_PIN, GPIO_PIN_SET);
HAL_Delay(10); // Allow time for mode switch
// Step 2: Read CHIP_ID Register
HAL_GPIO_WritePin(BMI270_CS_PORT, BMI270_CS_PIN, GPIO_PIN_RESET);
HAL_Delay(1); // Short delay for CS stabilization
HAL_StatusTypeDef tx_status = HAL_SPI_Transmit(&hspi1, &txData, 1, HAL_MAX_DELAY);
HAL_StatusTypeDef rx_status = HAL_SPI_Receive(&hspi1, &rxData, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(BMI270_CS_PORT, BMI270_CS_PIN, GPIO_PIN_SET);
// Step 3: Validate Chip ID Response
if (tx_status == HAL_OK && rx_status == HAL_OK) {
if (rxData == BMI270_EXPECTED_ID) {
printf("BMI270 Detected! Chip ID: 0x%02X\n", rxData);
} else {
printf(" BMI270 Not Found! Read: 0x%02X (Expected: 0x24)\n", rxData);
}
} else {
printf("? SPI Transmission Error! TX: %d, RX: %d\n", tx_status, rx_status);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
===========================================================
Physical Connection STM32 -> BMI270
===========================================================
Physical Connection STM32 -> BMI270
======================================================
Debug Mode Ouput
======================================================
2025-02-16 07:51 AM
0x00 or 0xFF suggests the board is not responding. Perhaps it is not powered, or not wired correctly. Your wiring table is correct, but power is not shown.
Code looks fine.
During debugging, reduce the clock rate to 1 MHz or less to eliminate noise as the issue.
Picture of the setup might help.
2025-02-16 10:03 AM
Check you have a common ground.
Perhaps inspect with scope or logic analyzer.
Perhaps bit bang.
Would suggest using the TransmitReceive with a 2 byte TX and Rx pattern buffer. Inspect second byte returned.
2025-02-17 03:43 AM
@mendicantbias wrote:I'm currently working on establishing a basic SPI connection between an STM32 microcontroller and the BMI270 IMU sensor by Mikroe.
Do you mean this https://www.mikroe.com/6dof-imu-12-click ?
Note that the BMI270 IMU sensor itself is by Bosch Sensortec - not MikroE:
https://www.bosch-sensortec.com/products/motion-sensors/imus/bmi270/
MicroE just make the board on which the sensor is mounted.
@mendicantbias wrote:My goal is to read the Chip ID (CHIP_ID register at 0x00) and get a response of 0x24, which confirms proper communication,
Yes, that is a good way to start.
As the others have said, getting zero suggests that the thing may be not responding at all.
The board seems to have an option to choose between SPI and I2C:
Are you sure sure that you have the correct selection?