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

Read data from sensor i2c

Hi,

I want read accel data from BNO055 sensor to NRF51 DK board, but SCL and SDA pins are 1 always.

My code is the next one:

#include "twi_master.h"
#include "twi_master_config.h"

#define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER_2 	(7U)
#define TWI_MASTER_CONFIG_DATA_PIN_NUMBER_2 	(30U)

static bool twi_master_clear_bus(void)
{
    uint32_t twi_state;
    bool     bus_clear;
    uint32_t clk_pin_config;
    uint32_t data_pin_config;

    // Save and disable TWI hardware so software can take control over the pins.
    twi_state        = NRF_TWI1->ENABLE;
    NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;

    clk_pin_config = \
        NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER];
    NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] =      \
        (GPIO_PIN_CNF_SENSE_Disabled  << GPIO_PIN_CNF_SENSE_Pos) \
      | (GPIO_PIN_CNF_DRIVE_S0D1    << GPIO_PIN_CNF_DRIVE_Pos)   \
      | (GPIO_PIN_CNF_PULL_Pullup   << GPIO_PIN_CNF_PULL_Pos)    \
      | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)   \
      | (GPIO_PIN_CNF_DIR_Output    << GPIO_PIN_CNF_DIR_Pos);

    data_pin_config = \
        NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER];
    NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] =       \
        (GPIO_PIN_CNF_SENSE_Disabled  << GPIO_PIN_CNF_SENSE_Pos) \
      | (GPIO_PIN_CNF_DRIVE_S0D1    << GPIO_PIN_CNF_DRIVE_Pos)   \
      | (GPIO_PIN_CNF_PULL_Pullup   << GPIO_PIN_CNF_PULL_Pos)    \
      | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)   \
      | (GPIO_PIN_CNF_DIR_Output    << GPIO_PIN_CNF_DIR_Pos);

    TWI_SDA_HIGH();
    TWI_SCL_HIGH();
    TWI_DELAY();

    if ((TWI_SDA_READ() == 1) && (TWI_SCL_READ() == 1))
    {
        bus_clear = true;
    }
    else
    {
        uint_fast8_t i;
        bus_clear = false;

        // Clock max 18 pulses worst case scenario(9 for master to send the rest of command and 9
        // for slave to respond) to SCL line and wait for SDA come high.
        for (i=18; i--;)
        {
            TWI_SCL_LOW();
            TWI_DELAY();
            TWI_SCL_HIGH();
            TWI_DELAY();

            if (TWI_SDA_READ() == 1)
            {
                bus_clear = true;
                break;
            }
        }
    }

    NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = clk_pin_config;
    NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER]  = data_pin_config;

    NRF_TWI1->ENABLE = twi_state;

    return bus_clear;
}

bool twi_master_init_2(void)
{
    /* To secure correct signal levels on the pins used by the TWI
       master when the system is in OFF mode, and when the TWI master is
       disabled, these pins must be configured in the GPIO peripheral.
    */
    NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] =     \
        (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
      | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos) \
      | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)  \
      | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos) \
      | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos);

    NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] =      \
        (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
      | (GPIO_PIN_CNF_DRIVE_S0D1     << GPIO_PIN_CNF_DRIVE_Pos) \
      | (GPIO_PIN_CNF_PULL_Pullup    << GPIO_PIN_CNF_PULL_Pos)  \
      | (GPIO_PIN_CNF_INPUT_Connect  << GPIO_PIN_CNF_INPUT_Pos) \
      | (GPIO_PIN_CNF_DIR_Input      << GPIO_PIN_CNF_DIR_Pos);

    NRF_TWI1->EVENTS_RXDREADY = 0;
    NRF_TWI1->EVENTS_TXDSENT  = 0;
    NRF_TWI1->PSELSCL         = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER_2;
    NRF_TWI1->PSELSDA         = TWI_MASTER_CONFIG_DATA_PIN_NUMBER_2;
    NRF_TWI1->FREQUENCY       = 400000; //TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos
    NRF_PPI->CH[0].EEP        = (uint32_t)&NRF_TWI1->EVENTS_BB;
    NRF_PPI->CH[0].TEP        = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
    NRF_PPI->CHENCLR          = PPI_CHENCLR_CH0_Msk;
    NRF_TWI1->ENABLE          = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;

    return twi_master_clear_bus();
}

int main(void)
{
    twi_master_init_2(); 
		
    while (true)
    {
//i2c
				//uint8_t addW = (addr<<1); 
				//uint8_t addR = (addr<<1)|0x01; 
				uint8_t addR = (0x52)|0x01; 
				uint8_t dataW[2] = {0x3D, 0x01}; 
				uint8_t dataR[2] = {0x00};
			
				//twi_master_transfer(addW,dataW,2,1); 
				twi_master_transfer(0X52,dataW,2,1);
				nrf_delay_us(400); 
				
				dataW[0] = 0x08;
				twi_master_transfer(0X52,dataW,1,1);
				//twi_master_transfer(addW,&dataW[0],1,0); 
				//nrf_delay_us(400); 
				//twi_master_transfer(addR,dataR,1,1); 
				twi_master_transfer(addR,dataR,2,1); 
				nrf_delay_us(400);
				
				printf("\n\rAccel L: %d\n\r",dataR[0]);
				printf("\n\rAccel M: %d\n\r",dataR[1]);
				
				nrf_delay_us(1000000);
    }
}

The connection is the next:

This is the pin connection:

NRF51-P07 with BNO055-18 (SCL). NRF51-P30 with BNO055-17 (SDA). NRF51-VDD with BNO055-1,2 and 22 (i2c adr mode). NRF51-GND with BNO055-3. PS0 and PS1 to GND to activate the I2C interface.

Rpull1 are connected between VDD and 18. Rpull2 are connected between VDD and 17.

what can be wrong?

Thanks

Parents
  • Hi,

    I've been testing the sensor but there is a thing it's doesn't work fine. BNO055 Sensor read data depending on an operating mode.

    In my code, I set the Operating Mode to 0x01(ACCONLY). Sensor only should read accel data, but it read data of magnetometer and gyroscope. After, I read the operating mode register and his value is 0x0C (NDOF Mode)

    My code is the next one:

    <

    	while(true){
    			uint8_t addR = (0x52)|0x01; 
    			uint8_t dataW[2] = {0x3D, 0x02}; 
    			uint8_t dataR[18] = {0x00};
    			uint8_t estado[1] = {0x00};
    		
    
    			twi_master_transfer(0X52,dataW,2,1);
    			nrf_delay_us(400); 
    			
    			dataW[0] = 0x08;
    
    			twi_master_transfer(0x52,dataW,1,1);
    
    			
    			uint16_t contador = 0;
    			
    			for(contador = 0; contador < 18; contador++){
    			dataR[contador] = 0;
    			}
    			
    			twi_master_transfer(addR,dataR,18,1); 
    		
    			printf("\n\rAccelX L: %d\n\r",dataR[0]);
    			printf("\n\rAccelX M: %d\n\r",dataR[1]);
    			printf("\n\rAccelY L: %d\n\r",dataR[2]);
    			printf("\n\rAccelY M: %d\n\r",dataR[3]);
    			printf("\n\rAccelZ L: %d\n\r",dataR[4]);
    			printf("\n\rAccelZ M: %d\n\r",dataR[5]);
    			printf("\n\rMagX L: %d\n\r",dataR[6]);
    			printf("\n\rMagX M: %d\n\r",dataR[7]);
    			printf("\n\rMagY L: %d\n\r",dataR[8]);
    			printf("\n\rMagY M: %d\n\r",dataR[9]);
    			printf("\n\rMagZ L: %d\n\r",dataR[10]);
    			printf("\n\rMagZ M: %d\n\r",dataR[11]);
    			printf("\n\rGirX L: %d\n\r",dataR[12]);
    			printf("\n\rGirX M: %d\n\r",dataR[13]);
    			printf("\n\rGirY L: %d\n\r",dataR[14]);
    			printf("\n\rGirY M: %d\n\r",dataR[15]);
    			printf("\n\rGirZ L: %d\n\r",dataR[16]);
    			printf("\n\rGirZ M: %d\n\r",dataR[17]);
    			
    			dataW[0] = 0x3D;
    			twi_master_transfer(0x52,dataW,1,1);
    			twi_master_transfer(addR,estado,1,1); 
    			printf("\n\rEstado: %X\n\r",estado[0]);
    			nrf_delay_us(1000000);		
    	
    	}
    

    <>

  • It doesn't work. It doesn't change the operating mode. It only changes the operating mode when I turn off the board.

    < // Write Operating Mode uint8_t dataW[2] = {0x3D, 0x01}; twi_master_transfer(0X52,dataW,2,0);

    // Read all data (accel, magnetometer and gyroscope). dataW[0] = 0x08; twi_master_transfer(0x52,dataW,1,0); twi_master_transfer(addR,dataR,18,1);

    // Read Operating Mode dataW[0] = 0x3D; twi_master_transfer(0x52,dataW,1,0); twi_master_transfer(addR,estado,1,1); <>

Reply
  • It doesn't work. It doesn't change the operating mode. It only changes the operating mode when I turn off the board.

    < // Write Operating Mode uint8_t dataW[2] = {0x3D, 0x01}; twi_master_transfer(0X52,dataW,2,0);

    // Read all data (accel, magnetometer and gyroscope). dataW[0] = 0x08; twi_master_transfer(0x52,dataW,1,0); twi_master_transfer(addR,dataR,18,1);

    // Read Operating Mode dataW[0] = 0x3D; twi_master_transfer(0x52,dataW,1,0); twi_master_transfer(addR,estado,1,1); <>

Children
No Data
Related