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