Page 1 of 1

Curiosity about initialization of LSM303DLHC for mag data

Posted: Sun May 26, 2013 9:46 am
by rafix
Hi,

i'm analyzing a code for a compass implementation with smtf3 discovery.

In particular the first part of the code:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "test.h"
#include "spi.h"
#include "i2c.h"
#include "usbcfg.h"
#include "chprintf.h"
#include "math.h"

#define usb_lld_connect_bus(usbp)
#define usb_lld_disconnect_bus(usbp)

/* Virtual serial port over USB.*/
SerialUSBDriver SDU1;

static const SPIConfig spi1cfg = {
      NULL,
      /* HW dependent part.*/
      GPIOE,
      GPIOE_SPI1_CS,
      SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_CPOL | SPI_CR1_CPHA,
      0
};

static const I2CConfig i2cconfig = {
      0x00902025, //voodoo magic
      0,
      0
};

static void writeByteI2C(uint8_t addr, uint8_t reg, uint8_t val)
{
   uint8_t cmd[] = {reg, val};
   i2cAcquireBus(&I2CD1);
   (void)i2cMasterTransmitTimeout(&I2CD1, addr, cmd, 2, NULL, 0, TIME_INFINITE);
   i2cReleaseBus(&I2CD1);
}

static void initMag(void)
{
   // Highest speed
   writeByteI2C(0x1E, 0x00, 0x1C);
   writeByteI2C(0x1E, 0x02, 0x00);
}

static uint8_t readMag(float* data)
{
   uint8_t start_reg = 0x03;
   uint8_t out[7];
   i2cAcquireBus(&I2CD1);
   msg_t f = i2cMasterTransmitTimeout(&I2CD1, 0x1E, &start_reg, 1, out, 7, TIME_INFINITE);
   (void)f;
   i2cReleaseBus(&I2CD1);
   //out[6] doesn't seem to reflect actual new data, so just push out every time
   int16_t val_x = (out[0] << 8) | out[1];
   int16_t val_z = (out[2] << 8) | out[3];
   int16_t val_y = (out[4] << 8) | out[5];
   data[0] = ((float)val_x)*1.22;
   data[1] = ((float)val_y)*1.22;
   data[2] = ((float)val_z)*1.22;
   return 1;
}


About the initMag function:

how can i understand registers and values used in

Code: Select all

writeByteI2C(0x1E, 0x00, 0x1C);
writeByteI2C(0x1E, 0x02, 0x00);


Any help or suggestion will be appreciated

Thanks

Re: Curiosity about initialization of LSM303DLHC for mag dat

Posted: Sun May 26, 2013 10:46 am
by Tectu
The first parameter of writeByteI2C is the address of the slave, the second is the register to be written, the third is the actual value that shall be written inside the register specified with the second parameter.
To understand what these values do, you have to read the datasheet of your I²C slave.


~ Tectu

Re: Curiosity about initialization of LSM303DLHC for mag dat

Posted: Sun May 26, 2013 10:52 am
by rafix
Ok thank you :D

Re: Curiosity about initialization of LSM303DLHC for mag dat

Posted: Mon Jun 03, 2013 9:25 am
by tanux
Good morning guys!
what it means 0x00902025, //voodoo magic in I2CConfig? I would like to replace it with a symbolic constant but do not know its meaning.
Idem for under fileds 0 and 0!

Re: Curiosity about initialization of LSM303DLHC for mag dat

Posted: Mon Jun 03, 2013 9:48 am
by Tectu
The first parameter of the I2CConfig struct defines the op-mode of the I²C peripheral. What those values exactly do might be observable in the reference manual of your MCU.


~ Tectu

Re: Curiosity about initialization of LSM303DLHC for mag dat

Posted: Mon Jun 03, 2013 10:01 am
by tanux
ok! thanks!