This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52832 with MPU9250 using i2c (TWI) on SDK15.2, SD132, Segger Embedded Studio

Hi. 

I'm trying to integrate the MPU9250 with nRF52832 using i2c (TWI) on my nRF52-DK

I have found various libraries but none of them are working on my hardware. All of them have compilation errors and missing files (or parts inside files)

If anyone has made a project integrating these two and using Segger Embedded Studio for compilation and flashing, please give me some advice as to how I can do it on my side too? 

Any help would be appreciated.

Best regards,

Usman

Parents Reply Children
  • Hi. I'm looking at Nordic's twi_master.h library. And there is something very confusing. 

    There are two .c fies. One is twi_hw_master.c and other is twi_sw_master.c 

    Now, each of these files contain their own definitions of the functions 
    twi_master_transfer, and twi_master_init. And the functions are not being overloaded either. They have the same return type, same inputs. 
    For example, here's twi_master_transfer:
    In twi_hw_master.c

    bool twi_master_transfer(uint8_t address,
                             uint8_t *data,
                             uint8_t data_length,
                             bool issue_stop_condition)
    {
        bool transfer_succeeded = true;
    
        transfer_succeeded &= twi_master_issue_startcondition();
        transfer_succeeded &= twi_master_clock_byte(address);
    
        if (address & TWI_READ_BIT)
        {
            /* Transfer direction is from Slave to Master */
            while (data_length-- && transfer_succeeded)
            {
                // To indicate to slave that we've finished transferring last data byte
                // we need to NACK the last transfer.
                if (data_length == 0)
                {
                    transfer_succeeded &= twi_master_clock_byte_in(data, (bool)false);
                }
                else
                {
                    transfer_succeeded &= twi_master_clock_byte_in(data, (bool)true);
                }
                data++;
            }
        }
        else
        {
            /* Transfer direction is from Master to Slave */
            while (data_length-- && transfer_succeeded)
            {
                transfer_succeeded &= twi_master_clock_byte(*data);
                data++;
            }
        }
    
        if (issue_stop_condition || !transfer_succeeded)
        {
            transfer_succeeded &= twi_master_issue_stopcondition();
        }
    
        return transfer_succeeded;
    }

    And in twi_sw_master.c

    bool twi_master_transfer(uint8_t   address,
                             uint8_t * data,
                             uint8_t   data_length,
                             bool      issue_stop_condition)
    {
        bool transfer_succeeded = false;
        if (data_length > 0 && twi_master_clear_bus())
        {
            NRF_TWI1->ADDRESS = (address >> 1);
    
            if ((address & TWI_READ_BIT))
            {
                transfer_succeeded = twi_master_read(data, data_length, issue_stop_condition);
            }
            else
            {
                transfer_succeeded = twi_master_write(data, data_length, issue_stop_condition);
            }
        }
        return transfer_succeeded;
    }

    This seems very strange to me. Which one is the code supposed to use? 

  • It's a SW vs HW implementation

    hence the _sw_ and _hw_ in the file names!

  • I managed to make it work. Thank you so much for your help. 

Related