Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    Basic BMM150 DRDY Boilerplate?

    Basic BMM150 DRDY Boilerplate?

    Ixck
    Member

    For three weeks I've been trying to get the BMM150 to work, but there are just sooo many issues it's almost unbearable.

    I can read all the registers via I2C, but only when I'm using my own I2C functions. Following exactly what the datasheet recommends, in accordance with the provided API examples (generic, mag_drdy_interrupt), I get -32,768, and that's it. In fact, when I use EXACTLY the same code as mag_drdy_interrupt, I still get -32,768...

    I need to be able to read the data through the API.

     

     

    #include <stdio.h>
    #include "boards.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "nrf_drv_twi.h"
    #include "nrfx_twi.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "bmm150.h"
    #include "bmm150_common.h"
    
    /* TWI instance ID. */
    #define TWI_INSTANCE_ID     0
    
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
    uint8_t registerRX[] = {7};
    
    const uint8_t registerTX[] = {0x4D};
    
    struct bmm150_dev dev;
    struct bmm150_settings mySettings;
    struct bmm150_mag_data mag_data;
    
    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_lm75b_config = {
           .scl                = NRF_GPIO_PIN_MAP(0, 27),
           .sda                = NRF_GPIO_PIN_MAP(0, 26),
           .frequency          = NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, NULL, NULL);
        //printf("Error Code init\t%d\n", err_code);
    
        nrf_drv_twi_enable(&m_twi);
    }
    
    void readRegister(int registerAddressX, bool printCodes){
      
        uint8_t registerTX1[] = {registerAddressX}; // Pointer
        uint8_t registerRX1[] = {7};
    
        ret_code_t tx_code;
        ret_code_t rx_code;
    
        tx_code = nrf_drv_twi_tx(&m_twi, 0x10, &registerTX1, 1, true);
        rx_code = nrf_drv_twi_rx(&m_twi, 0x10, registerRX1, 1);
        printf("Register (%x", registerAddressX);
        printf("):\t%x\n", registerRX1[0]);
    
        
        if(printCodes){
          printf("TX Code:\t%d\n", tx_code);
          printf("RX Code:\t%d\n", rx_code);
        }
    }
    
    void writeRegister(int registerAddressY, int setValue, bool printCodes){
      
      uint8_t registerTX2[] = {registerAddressY, setValue};
    
      ret_code_t tx_code;
    
      tx_code = nrf_drv_twi_tx(&m_twi, 0x10, &registerTX2, 2, false);
      
      if(printCodes){
          printf("TX Code:\t%d\n", tx_code);
      }
    
    }
    
    void checkAllRegisters(){
    
      readRegister(0x52, false);
      readRegister(0x51, false);
      readRegister(0x50, false);
      readRegister(0x4F, false);
      readRegister(0x4E, false);
      readRegister(0x4D, false);
      readRegister(0x4C, false);
      readRegister(0x4B, false);
      readRegister(0x4A, false);
    }
    
    void checkXYZRegisters(){
    
        uint8_t XYZRegisters[] = {0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49};
        uint8_t registerValue[8];
    
        for(int i = 0; i < sizeof(XYZRegisters); i++){
          nrf_drv_twi_tx(&m_twi, 0x10, &XYZRegisters, 1, true);
          nrf_drv_twi_rx(&m_twi, 0x10, registerValue, 8);
    
          printf("-----------------\n");
          printf("register:\t%x\n", XYZRegisters[i]);
          printf("registerValue:\t%d\n", registerValue[i]*32);
          //printf("XYZRegisters.length\t%d\n", sizeof(XYZRegisters)); // Line to print the size of the registers array
      }
    }
    
    void checkSpecificAxis(char Axis){
    
        uint8_t XYZRegisters[2];
        uint8_t registerValue[2];
    
        switch(Axis){
    
        case 'X':
        XYZRegisters[0] = 0x43;
        XYZRegisters[1] = 0x42;
        nrf_drv_twi_tx(&m_twi, 0x10, &XYZRegisters, 1, true);
        nrf_drv_twi_rx(&m_twi, 0x10, registerValue, 2);
        
        break;
    
        case 'Y':
        XYZRegisters[0] = 0x45;
        XYZRegisters[1] = 0x44;
        nrf_drv_twi_tx(&m_twi, 0x10, &XYZRegisters, 1, true);
        nrf_drv_twi_rx(&m_twi, 0x10, registerValue, 2);
        
        break;
    
        case 'Z':
        XYZRegisters[0] = 0x47;
        XYZRegisters[1] = 0x46;
        nrf_drv_twi_tx(&m_twi, 0x10, &XYZRegisters, 1, true);
        nrf_drv_twi_rx(&m_twi, 0x10, registerValue, 2);
        
        break;
    
      }
    
          printf("\n%c", Axis);
          printf("\t");
          printf("%d", registerValue[0]*32);
          printf("\t");
          printf("%d", registerValue[1]*32);
    }
    
    //=====================================================
    //                        MAIN
    //=====================================================
    
    int main(void)
    {
    
        int a, b, c, d, e, f, g, h, i;
    
        dev.intf = BMM150_I2C_INTF;
        //dev.read = readSensor;
        //dev.write = writeSensor;
        dev.read = bmm150_user_i2c_reg_read;
        dev.write = bmm150_user_i2c_reg_write;
        dev.delay_us = 3;
    
        twi_init();
    
        writeRegister(0x4C, 0x00, false);
    
        bmm150_interface_selection(&dev);
        a = bmm150_init(&dev);
        printf("rslt A:\t%d\n", a);
        
        mySettings.pwr_mode = BMM150_POWERMODE_NORMAL;
        b = bmm150_set_op_mode(&mySettings, &dev);
        printf("rslt B:\t%d\n", b);
    
        mySettings.preset_mode = BMM150_PRESETMODE_REGULAR;
        c = bmm150_set_presetmode(&mySettings, &dev);
        printf("rslt C:\t%d\n", c);
        
        mySettings.int_settings.drdy_pin_en = 0x01;
        d = bmm150_set_sensor_settings(BMM150_SEL_DRDY_PIN_EN, &mySettings, &dev);
        printf("rslt D:\t%d\n", d);
        
        e = bmm150_get_interrupt_status(&dev);
        printf("rslt E:\t%d\n", e);
    
        printf("Dev->int_status:\t%d\n", dev.int_status);
        printf("BMM150_INT_ASSRTED_DRDY:\t%d\n", BMM150_INT_ASSERTED_DRDY);
    
    
    
        //================================
        //    Startup Config Functions
        //================================
    
        /*
        mySettings.data_rate = BMM150_DATA_RATE_10HZ;
        mySettings.xyz_axes_control = BMM150_XYZ_CHANNEL_ENABLE;
        
        bmm150_set_sensor_settings(BMM150_SEL_DATA_RATE, &mySettings, &dev);
        */
        
    
        printf("Mag X:\t%d\n", mag_data.x);
        printf("Mag Y:\t%d\n", mag_data.y);
        printf("Mag Z:\t%d\n", mag_data.z);
    
        while(true){
          printf("Mag_data:\t%d\n", bmm150_read_mag_data(&mag_data, &dev));
          printf("Mag X:\t%d\n", mag_data.x);
          printf("Mag Y:\t%d\n", mag_data.y);
          printf("Mag Z:\t%d\n", mag_data.z);
        }
    
    }

     

    Result:

    Mag_data:	0
    Mag X:	-32768
    Mag Y:	-32768
    Mag Z:	-32768
    1 REPLY 1

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Ixck,

    I setup attached BMM150 example code on STM32 for your reference, it worked well. Please check your host MCU driver code.

    test result.png

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist