I have a MinIMU9v2 that utilizes both, the LSM303DLHC accelerometer, and the L3GD20H gyroscope chips, for which there are drivers in the sensorlib folder of the drivers that I found on the TI website. I am using a TivaC tm4c123gxl launchpad and attempting to get something going initially just to read and write to the accelerometer (the gyroscope is next) but I don't really feel confident that the way that I have begun this is the correct way of going about it. The following code doesn't compile, and I can't seem to find any examples for this. Any help is appreciated.
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "sensorlib/hw_lsm303dlhc.h"
#include "sensorlib/i2cm_drv.h"
#include "sensorlib/lsm303dlhc_accel.h"
// For some reason, the compiler couldn't find these
#define GPIO_PA0_U0RX 0x00000001
#define GPIO_PA1_U0TX 0x00000401
#define GPIO_PB2_I2C0SCL 0x00010802
#define GPIO_PB3_I2C0SDA 0x00010C02
//// Registers (Happen to be identical between Acc and Gyro)
//#define CTRL_REG1 0x20
//#define OUT_X_L 0x28
//#define OUT_X_H 0x29
//#define OUT_Y_L 0x2A
//#define OUT_Y_H 0x2B
//#define OUT_Z_L 0x2C
//#define OUT_Z_H 0x2D
//
//// Configurations
//#define GYRO_CONFIG 0xFF
//#define ACC_CONFIG 0x97
// Slave addresses
#define GYRO 0xD4
#define ACC 0x32
bool g_initialized = false;
bool g_read = false;
void
InitConsole(void)
{
UARTprintf("Init Console\n");
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 115200, 16000000);
}
void
InitI2C(void)
{
UARTprintf("Init I2C\n");
// Initialize the TM4C I2C hardware for I2C0
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC |
SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
// Initialize the bus
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
}
void
ReadCb(void)
{
g_read = true;
}
void
InitCb(void)
{
g_initialized = true;
}
int
main(void)
{
tLSM303DLHCAccel accel;
InitConsole();
InitI2C();
if (LSM303DLHCAccelInit(&accel, I2C0_BASE, ACC, &InitCb))
{
UARTprintf("Initializing accelerometer.\n");
while (!g_initialized) {}
}
else
{
UARTprintf("Accelerometer initialization failed.\n");
return(1);
}
uint8_t pui8Data;
if (LSM303DLHCAccelRead(accel, LSM303DLHC_O_CTRL1, &pui8Data, 1, &ReadCb))
{
UARTprintf("Reading accelerometer CTRL1.\n");
while (!g_read) {}
g_read = false;
UARTprintf("Read byte: %x", pui8Data);
}
else
{
UARTprintf("Accelerometer read failed.\n");
return(1);
}
return(0);
}