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

When porting Arduino i2c sensor driver to nrf52...

I am a student interested in Nordic.

My coding skills are very poor.

So I want to get help.

I want to use the vl53l1x sensor in nrf52. (My ultimate goal is to measure the distance using vl53l1x.)
I have put a lot of effort into using the vl53l1x sensor.

The conclusion I found is that the ST driver or arduino driver must be ported to nrf52.

So I am trying to change the arduino driver. 

However, it is difficult to change the nrf_twi_rx and nrf_twi_tx functions due to wire.h.

The code I changed is:

/*******************************arduino code**********************************/

void VL53L1X::writeReg(uint16_t reg, uint8_t value)
{
   bus->beginTransmission(address);
   bus->write((reg >> 8) & 0xFF); // reg high byte
   bus->write( reg & 0xFF); // reg low byte
   bus->write(value);
   last_status = bus->endTransmission();
}

uint8_t VL53L1X::readReg(regAddr reg)
{
   uint8_t value;

   bus->beginTransmission(address);
   bus->write((reg >> 8) & 0xFF); // reg high byte
   bus->write( reg & 0xFF); // reg low byte
   last_status = bus->endTransmission();

   bus->requestFrom(address, (uint8_t)1);
   value = bus->read();

   return value;
}

/**************************nRF52 Code******************************/

//void vl53l1x_set_mode()
void writeReg(uint16_t reg, uint8_t value)
{
   ret_code_t err_code;

   uint16_t tx_data[2];
   tx_data[0] = ( ((reg >> 8)& 0xFF ) | reg & 0xFF);
   tx_data[1] = value;

   err_code = nrf_drv_twi_tx(&m_twi, VL53L1X_ADDR, tx_data, sizeof(tx_data), false);
   return err_code;
}

//void read_vl53l1x_data()
uint8_t readReg(uint16_t reg)
{
   ret_code_t err_code;
   uint16_t read_data[2];
   uint16_t read_addr = ( ((reg >> 8)& 0xFF ) | reg & 0xFF);
#if 1
   err_code = nrf_drv_twi_tx(&m_twi, VL53L1X_ADDR, &read_addr, 1, false);
   if(err_code != NRF_SUCCESS)
   {
       return err_code;
   }
#endif
   err_code = nrf_drv_twi_rx(&m_twi, VL53L1X_ADDR, read_data, 1);

   return read_data[0];
}

.

.

.

.

.

.

When I built, the result was only 0. (address or gpio pin was correct.) What am I doing wrong? Please help ㅠㅠ

Related