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

Use DMP of MPU9250

my code is below. git_mpu9250_dmp.zip

Hello.

I use nRF52 & MPU9250. I want to use DMP, but I can't read data with DMP. Because it have failed that writting firmware.

I want to check connect sensor between nRF, I made a code for reading register of "WHO_AM_I". At first time, read "WHO_AM_I", return 0x71. After faild writting firmware, read "WHO_AM_I",return 0x0d.

I wrote firmware below, 16,0,0,70,0,0,0,0,24,0,0,0,2,0,3,0,0... read mem below, 0,83,5a,44,9,46,10,6,0,0,15,bb,48,79,c1,3c...

What was happened?

  • Thanks your comment. and sorry, I mistyped. Error happened at in mpu_load_firmware(), and it returns -5. Because this function failed memcmp. I want to write 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, but it was not written. I think there is problems mpu9250_write. However, I can't understand what this solution.

  • I thought again, I solved this problem. This problem was mistake resister to be written.

    Thank you for the advice,it was very helpful!!

        if ((errCode = mpu_write_mem(ii, this_write, progBuffer))) {
    					free(progBuffer);
            return -3;
        }
        
        if (mpu_read_mem(ii, this_write, cur)){
    					free(progBuffer);
            return -4;
    			}
            
        if (memcmp(progBuffer, cur, this_write)) {
    				printf("this_write = %d \r\n",this_write);
    				for (int i = 0; i < this_write ; i++){
    				  printf("progBuffer = %x , cur = %x\r\n",progBuffer[i],cur[i]);
    				}
    				free(progBuffer);
          return -5;
        }
    

    Additional note @2017/05/22

    I don't know necessary 2 i2c_write.

    #define i2c_write_one call_mpu9250_write_m
    #define i2c_write call_mpu9250_write
    
    static uint32_t mpu9250_write(uint8_t address, uint8_t reg, uint32_t length, uint8_t const * p_data)
    {
        uint32_t err_code;
    	
    		for(uint8_t i = 0; i < length ; i++){
    			
    			uint8_t packet[2] = {(reg+i), p_data[i]};
    #ifdef PTEST
    			printf("calledw #2\r\n");
    
    				nrf_delay_ms(10);
    			printf("%d,%d,%d\r\n",address,packet[0],packet[1]);
    	
    #endif
    			err_code = nrf_drv_twi_tx(&m_twi_instance, address, packet, 2, false);
    			if(err_code != NRF_SUCCESS) {
    				printf("mpu9250_write error %d\r\n",err_code);
    				nrf_delay_ms(50);
    				
    			  return err_code;
    			}
    			 #ifdef PTEST
    			printf(" timer ");
    			//	nrf_delay_ms(10);
    			#endif
    			
    			if(!waitInt(MPU9250_TWI_TIMEOUT)){
    					printf(" timeout...w ");
    					nrf_delay_ms(10);
    					return NRF_ERROR_TIMEOUT;
    			}
    			twi_done = false;
    	
    		}
    			
    			#ifdef PTEST
    		printf("write_end! %d\r\n",err_code);
    				nrf_delay_ms(10);
    			#endif
        return err_code;
    }
    
    
    uint32_t call_mpu9250_write(uint8_t address, uint8_t reg, uint32_t length, uint8_t const * p_data){
    		return mpu9250_write(address, reg, length , p_data);
    }
    
    static uint32_t mpu9250_write_m(uint8_t address, uint8_t reg, uint32_t length, uint8_t const * p_data)
    {
        uint32_t err_code;
    	for(uint8_t i = 0; i < length ; i++){
    		uint8_t packet[2] = {(reg), p_data[i]}; //write to same regster
    
    #ifdef PTEST
    		printf("calledwm #2\r\n");
    		nrf_delay_ms(10);
    		printf("%d,%d,%d\r\n",address,packet[0],packet[1]);
    	    nrf_delay_ms(10);
    #endif
    			
    		err_code = nrf_drv_twi_tx(&m_twi_instance, address, packet, 2, false);
    
    #ifdef PTEST
    		printf("end nrf_drv_twi_tx\r\n");
    		nrf_delay_ms(10);
    #endif			
    			
    		if(err_code != NRF_SUCCESS) {
    			printf("mpu9250_write error %d\r\n",err_code);
    			nrf_delay_ms(50);
    			return err_code;
    		}
    			
    #ifdef PTEST
    		printf(" timer ");
    		nrf_delay_ms(10);
    #endif
    			
    		if(!waitInt(MPU9250_TWI_TIMEOUT)){ //if waitInt() return true,then twi is done.
    			printf(" timeout...wm ");
    			nrf_delay_ms(10);
    			return NRF_ERROR_TIMEOUT;
    		}
    			
    		twi_done = false;
    	}
    		
    #ifdef PTEST
    	printf("write_m_end! %d\r\n",err_code);
    	nrf_delay_ms(10);
    	#endif
       return err_code;
    }
    
    uint32_t call_mpu9250_write_m(uint8_t address, uint8_t reg, uint32_t length, uint8_t const * p_data){
    		return mpu9250_write_m(address, reg, length , p_data);
    }
    
    int mpu_write_mem(unsigned short mem_addr, unsigned short length,
            unsigned char *data)
    {
        unsigned char tmp[2];
    
        if (!data)
            return -1;
        if (!st->chip_cfg.sensors)
            return -2;
    
    //		printf("sensors = %d\r\n",st->chip_cfg.sensors);
    		
        tmp[0] = (unsigned char)(mem_addr >> 8);
        tmp[1] = (unsigned char)(mem_addr & 0xFF);
    
        /* Check bank boundaries. */
        if (tmp[0] + length > st->hw->bank_size)
            return -3;
    
        if (i2c_write(st->hw->addr, st->reg->bank_sel, 2, tmp))
            return -4;
        if (i2c_write_one(st->hw->addr, st->reg->mem_r_w, length, data))
            return -5;
        return 0;
    }
    
  • Hi Sagisawa Yukichi,

    I'm also getting same error i'm glad to here that you solved that error if you don't mind please help me where did you done the modification to load DMP firmware successfully so it really helpful to me.

    Thanks .

  • Hi satish, I added my code that I changed. I missed writing register.

  • Thanks a lot Sagisawa Yukichi,i will check with my code.once again thanks for your reply.

Related