cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO H723ZGT connect to BMI270 Mikroe via SPI

mendicantbias
Associate

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:

  • Step 1: Perform a dummy SPI read to switch BMI270 from I2C to SPI following the documentation on [1] page 19 (Quick Start Guide ) and 139 (6.3. Primary Interface Digital Protocol Selection)
  • Step 2: Send CHIP_ID read command and receive the response.
  • Step 3: Validate if received 0x24 (BMI270 detected) or print an error message.

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

mendicantbias_0-1739708991507.png
SPI PINS:

  • PA5 (SPI1_SCK): Alternate Function Push-Pull, High Output Level, No pull-up/pull-down, High Speed.
  • PA6 (SPI1_MISO): Alternate Function Push-Pull, High Output Level, No pull-up/pull-down, High Speed.
  • PD7 (SPI1_MOSI): Alternate Function Push-Pull, High Output Level, No pull-up/pull-down, High Speed.
  • PB12 (SPI1_CS): Output Push-Pull, High Output Level, No pull-up/pull-down, High Speed.

mendicantbias_3-1739709407612.png

 

mendicantbias_5-1739709723660.png

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

mendicantbias_6-1739710529502.png

 

======================================================
Debug Mode Ouput
======================================================

mendicantbias_7-1739710600670.png

 

 

3 REPLIES 3
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@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 ?

AndrewNeil_0-1739792159427.png

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:

AndrewNeil_1-1739792556904.png

Are you sure sure that you have the correct selection?