🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Problems with BMI270 on M5StampFly (M5StampS3)

    Arduino
    2
    3
    425
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      KamilB
      last edited by

      Hello,

      I am trying to write a code for M5StampFly myself but I have run into an issue with an imu unit.
      I cannot configure it to work. I have tried many different approaches.
      The code below read a status and a chip id. The chip id always returns random values with the same SPI settings and status always returns 0. I tried changing SPI settings, using different libraries, powering stampFly with a battery and then running code, reseting imu before reading and many others.
      I would gladly appreciate some help.
      Thanks in advance.

      Code (ignore polish comments):

      #include <SPI.h>
      
      // PINy pod SPI - zakładam ze poprawnie
      #define BMI270_CS   46  
      #define BMI270_SCK  44  
      #define BMI270_MISO 43  
      #define BMI270_MOSI 14  
      
      // Definicje rejestrów BMI270 zakresy z https://github.com/simondlevy/BMI270-SPI/blob/main/src/BMI270.h
      #define BMI270_REG_CHIP_ID       0x00  // Rejestr identyfikatora chipu
      #define BMI270_REG_CMD           0x7E  // Rejestr poleceń
      #define BMI270_REG_ACC_CONF      0x40  // Konfiguracja akcelerometru
      #define BMI270_REG_ACC_RANGE     0x41  // Zakres akcelerometru
      #define BMI270_REG_GYR_CONF      0x42  // Konfiguracja żyroskopu
      #define BMI270_REG_GYR_RANGE     0x43  // Zakres żyroskopu
      #define BMI270_REG_DATA          0x0C  // Początek danych akcelerometru i żyroskopu
      
      // Funkcja do zapisu wartości do rejestru BMI270
      void writeRegister(uint8_t reg, uint8_t value) {
        SPI.beginTransaction(SPISettings(10000000, LSBFIRST, SPI_MODE0));
        digitalWrite(BMI270_CS, LOW);
        SPI.transfer(reg & 0x7F);  // Bit 7 ustawiony na 0 dla operacji zapisu
        SPI.transfer(value);
        digitalWrite(BMI270_CS, HIGH);
        SPI.endTransaction();
      }
      
      // Funkcja do odczytu wartości z rejestru BMI270
      uint8_t readRegister(uint8_t reg) {
        SPI.beginTransaction(SPISettings(10000000, LSBFIRST, SPI_MODE0));
        digitalWrite(BMI270_CS, LOW);
        SPI.transfer(reg | 0x80);  // Bit 7 ustawiony na 1 dla operacji odczytu
        uint8_t value = SPI.transfer(0x00);
        digitalWrite(BMI270_CS, HIGH);
        SPI.endTransaction();
        return value;
      }
      
      // Funkcja do odczytu wielobajtowych danych z BMI270
      void readRegisters(uint8_t startReg, uint8_t* buffer, uint8_t length) {
        SPI.beginTransaction(SPISettings(10000000, LSBFIRST, SPI_MODE0));
        digitalWrite(BMI270_CS, LOW);
        SPI.transfer(startReg | 0x80);  // Bit 7 ustawiony na 1 dla operacji odczytu
        for (uint8_t i = 0; i < length; i++) {
          buffer[i] = SPI.transfer(0x00);
        }
        digitalWrite(BMI270_CS, HIGH);
        SPI.endTransaction();
      }
      
      // Funkcja resetująca BMI270
      void resetBMI270() {
        writeRegister(BMI270_REG_CMD, 0xB6);  // Reset
        delay(100);  // opoznienie
      }
      
      // Funkcja inicjalizująca BMI270
      void initializeBMI270() {
        resetBMI270();
      
        // Konfiguracja akcelerometru
        writeRegister(BMI270_REG_ACC_CONF, 0xA8);  // Próbkowanie 1600 Hz, filtr dolnoprzepustowy
        writeRegister(BMI270_REG_ACC_RANGE, 0x00); // Zakres ±2g
      
        // Konfiguracja żyroskopu
        writeRegister(BMI270_REG_GYR_CONF, 0xA9);  // Próbkowanie 1600 Hz, filtr dolnoprzepustowy
        writeRegister(BMI270_REG_GYR_RANGE, 0x00); // Zakres ±2000°/s
      
        delay(50);  // Opoxnienie
      }
      
      // Funkcja odczytująca dane z akcelerometru i żyroskopu
      void readSensorData(int16_t* accData, int16_t* gyrData) {
        uint8_t data[12];
        readRegisters(BMI270_REG_DATA, data, 12);
      
        // Przetwarzanie danych akcelerometru (16-bitowe wartości w porządku little-endian)
        accData[0] = (int16_t)((data[1] << 8) | data[0]);  // Oś X
        accData[1] = (int16_t)((data[3] << 8) | data[2]);  // Oś Y
        accData[2] = (int16_t)((data[5] << 8) | data[4]);  // Oś Z
      
        // Przetwarzanie danych żyroskopu (16-bitowe wartości w porządku little-endian)
        gyrData[0] = (int16_t)((data[7] << 8) | data[6]);  // Oś X
        gyrData[1] = (int16_t)((data[9] << 8) | data[8]);  // Oś Y
        gyrData[2] = (int16_t)((data[11] << 8) | data[10]); // Oś Z
      }
      
      //uint8_t chip_id = 0;
      
      void setup() {
        Serial.begin(115200);
        pinMode(BMI270_CS, OUTPUT);
        digitalWrite(BMI270_CS, HIGH);
        SPI.begin(BMI270_SCK, BMI270_MISO, BMI270_MOSI, BMI270_CS);
        delay(100);
      
        initializeBMI270();
      
        // Sprawdzenie identyfikatora chipu
        uint8_t chip_id = readRegister(BMI270_REG_CHIP_ID);
        Serial.print("Chip ID: 0x");
        Serial.println(chip_id, HEX);
      
        uint8_t status = readRegister(0x01);
      Serial.print("Status Register: 0x");
      Serial.println(status, HEX);
      }
      
      void loop() {
        int16_t accData[3];  // Tablica na dane akcelerometru
        int16_t gyrData[3];  // Tablica na dane żyroskopu
      
        // Serial.print("Chip ID: 0x");
        // Serial.println(chip_id, HEX);
      
        //readSensorData(accData, gyrData);
      
        // Akcelerometr
        //Serial.print("Akcelerometr [mg]: X=");
        //Serial.print(accData[0]);
        //Serial.print(" Y=");
        //Serial.print(accData[1]);
        //Serial.print(" Z=");
        //Serial.println(accData[2]);
      
        // Żyroskop
        //Serial.print("Żyroskop [mdps]: X=");
        //Serial.print(gyrData[0]);
        //Serial.print(" Y=");
        //Serial.print(gyrData[1]);
        //Serial.print(" Z=");
        //Serial.println(gyrData[2]);
        delay(200);
      }
      
      felmueF 1 Reply Last reply Reply Quote 0
      • felmueF
        felmue @KamilB
        last edited by

        Hello @KamilB

        I do not have the hardware to test this myself, but I think you might want to pull GPIO12 HIGH to make sure you're talking to BMI270 only. See here.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        K 1 Reply Last reply Reply Quote 0
        • K
          KamilB @felmue
          last edited by

          @felmue Thanks for answering!
          Tried it and now I am getting always chip id 0xFF or 0x0 depended on data order in SPISettings (MSB or LSB first). Tried it with and without battery plugged in but there was no difference.
          Any other ideas?

          1 Reply Last reply Reply Quote 0
          • First post
            Last post