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

Unable to Read or write MMA8652 registers using TWI

[https://drive.google.com/open?id=0ByjgT_Zt3YyJS2tKQko3cW10SlU]

hi i am working on MMA8652 i am working on mma7660 twi example in sdk11 for nrf52. i have changed the registers as the same way the MMA8652 need. here is my read and write functions

to read data:-

#define MMA8652_I2C_ADDR				(0x1D)

uint8_t	W_reg = MMA8652_REG_WHO_AM_I;

	err_code=nrf_drv_twi_tx(&m_twi_mma_8652, MMA8652_I2C_ADDR, &W_reg, sizeof(W_reg), false);

	APP_ERROR_CHECK(err_code);

	nrf_delay_ms(5);

	err_code = nrf_drv_twi_rx(&m_twi_mma_8652, MMA8652_I2C_ADDR, &W_reg, sizeof(W_reg));

	APP_ERROR_CHECK(err_code);

when i am going to run this the application is going to stuck.

and also i have one doubt. in sdk twi example there is one function for reading, in that example in twi handler directly reading from device address. and the result is going to structure instance with x,y,x,tilt.

here is that function

typedef struct

{

    elem_t  x;

    elem_t  y;

    elem_t  z;

    uint8_t tilt;


} sample;

err_code = nrf_drv_twi_rx(&m_twi_mma_8652, MMA8652_I2C_ADDR, (uint8_t*)&sample, sizeof(sample));

how it is possible to read directly from MMA8652_I2C_ADDR(0X1D)

if i am using same procedure i am getting x value is always 0 and y, z values are varying. is this procedure is correct to read x,y,z data from the accelerometer.

EDIT:-------

void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context) { ret_code_t err_code; static sample_t m_sample;

switch(p_event->type)
{
case NRF_DRV_TWI_EVT_DONE:
	//string_send("1twirxsuc\r\n");
	if ((p_event->type == NRF_DRV_TWI_EVT_DONE) &&
			(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX))
	{
		if(m_set_mode_done != true)
		{
			m_set_mode_done  = true;
			return;
		}
		m_xfer_done = false;
		err_code = nrf_drv_twi_rx(&m_twi_mma_8652, 0X1D, (uint8_t*)&m_sample, sizeof(m_sample));
		if(err_code == NRF_SUCCESS){
			string_send("rx Done\r\n");
		}
		else
			string_send("rxfail\r\n");

		APP_ERROR_CHECK(err_code);
		if(err_code == NRF_SUCCESS){
			//string_send("2twi rx suc\r\n");
		}
	}
	else
	{
		//string_send("elsetwirx\r\n");
		read_data(&m_sample);
		m_xfer_done = true;

	}
	break;
default:
	break;
}

}

Parents
  • Yes, the twi_master driver is deprecated in favor of the new nrf_drv_twi.c driver. Unfortunately, I do not have a MMA8652 that I can test against. I modified the twi_sensor example in SDK v11.0.0 so that its more inline with the snippets I've put in the comments. It first does a nrf_drv_twi_tx to tell the MMA7660 which register it should read from, in this case MMA7660_REG_XOUT. Then it reads 3 bytes which is MMA7660_REG_XOUT, MMA7660_REG_YOUT and MMA7660_REG_ZOUT and prints it to the uart. Here is the main file: main.c

Reply
  • Yes, the twi_master driver is deprecated in favor of the new nrf_drv_twi.c driver. Unfortunately, I do not have a MMA8652 that I can test against. I modified the twi_sensor example in SDK v11.0.0 so that its more inline with the snippets I've put in the comments. It first does a nrf_drv_twi_tx to tell the MMA7660 which register it should read from, in this case MMA7660_REG_XOUT. Then it reads 3 bytes which is MMA7660_REG_XOUT, MMA7660_REG_YOUT and MMA7660_REG_ZOUT and prints it to the uart. Here is the main file: main.c

Children
No Data
Related