GlobLib
HAL and API libraries for MCUs and hardware.
LSM9DS1

Routines for interaction with LSM9DS1 9D0F IMU. More...

Files

file  lsm9ds1.h
 Header file for STMicroelectronics LSM9DS1 9DOF IMU.
 

Data Structures

struct  LSM9DS1
 Data structure containing variables specific to individual slave devices. More...
 

Macros

#define LSM9DS1_DEFAULT_MAG_SCALE   LSM9DS1_MAG_8
 Default mag range set by LSM9DS1_setup()
 
#define LSM9DS1_DEFAULT_GYR_SCALE   LSM9DS1_GYR_500
 Default mag range set by LSM9DS1_setup()
 
#define LSM9DS1_DEFAULT_ACC_SCALE   LSM9DS1_ACC_4
 Default mag range set by LSM9DS1_setup()
 
#define LSM9DS1_DATA_RANGE   32767.0f
 Data range for each axis.
 
#define LSM9DS1_GX   0
 Used by read and write function to select gyro/accel.
 
#define LSM9DS1_M   1
 Used by read/write functions to select mag.
 

Enumerations

Functions

void LSM9DS1_setup (LSM9DS1 *target, uint8_t spiPort, uint16_t gx_port, uint32_t gx_pin, uint16_t m_port, uint32_t m_pin, uint8_t cs_pol)
 Initalizes the slave device and populates data structure variable. More...
 
uint8_t LSM9DS1_magReady (LSM9DS1 *target)
 Check if the magnetometer has new data available. More...
 
void LSM9DS1_getMag (LSM9DS1 *target, int16_t *data)
 Get the latest magnometer readings. More...
 
uint8_t LSM9DS1_gyrReady (LSM9DS1 *target)
 Check if the gyroscope has new data available. More...
 
void LSM9DS1_getGyr (LSM9DS1 *target, int16_t *data)
 Get the latest gyroscope readings. More...
 
uint8_t LSM9DS1_accReady (LSM9DS1 *target)
 Check if the accelerometer has new data available. More...
 
void LSM9DS1_getAcc (LSM9DS1 *target, int16_t *data)
 Get the latest accelerometer readings. More...
 
uint8_t LSM9DS1_tempReady (LSM9DS1 *target)
 Check if the temperature module has new data available. More...
 
float LSM9DS1_getTemp (LSM9DS1 *target)
 Get the latest accelerometer readings. More...
 
uint8_t LSM9DS1_gxWAI (LSM9DS1 *target)
 Read the value for the gyro/accel who am I register. More...
 
uint8_t LSM9DS1_mWAI (LSM9DS1 *target)
 Read the value for the magnetometer who am I register. More...
 
void LSM9DS1_setMagScale (LSM9DS1 *target, lsm9ds1_magScale scale)
 Set the scale of the magnetometer. More...
 
void LSM9DS1_setAccScale (LSM9DS1 *target, lsm9ds1_accScale scale)
 Set the scale of the accelerometer. More...
 
void LSM9DS1_setGyrScale (LSM9DS1 *target, lsm9ds1_gyrScale scale)
 Set the scale of the gyroscope. More...
 
uint8_t LSM9DS1_getMagScale (LSM9DS1 *target)
 Get the scale of the magnetometer. More...
 
uint8_t LSM9DS1_getAccScale (LSM9DS1 *target)
 Get the scale of the accelerometer. More...
 
uint16_t LSM9DS1_getGyrScale (LSM9DS1 *target)
 Get the scale of the gyroscope. More...
 
void LSM9DS1_writeByte (LSM9DS1 *target, uint8_t component, uint8_t address, uint8_t data)
 Write a single byte to the device's register. More...
 
uint16_t LSM9DS1_readByte (LSM9DS1 *target, uint8_t component, uint8_t address)
 Read a single byte from the device's register. More...
 

Detailed Description

Routines for interaction with LSM9DS1 9D0F IMU.

The LSM9DS1 operates with either SPI or I2C bus. This library curretly only supports SPI.

The device requires two active low chip select pins, one for the magnetometer and the other for the combined gyro and accelerometer. These must be configured externally. (see example)

The maximum updates for each component are

LSM9DS1_accReady(), LSM9DS1_gyrReady(), LSM9DS1_magReady() can be used to check if data is available when sampling near these frequencies.

Note
This library requires external functions to be declared.
  • For SPI usage
    • SPI_read()
    • SPI_repeatRead()
    • SPI_write()
    • delayms()
Reading from the device using SPI bus has special bit setting requirments. These are handled internally in LSM9DS1_writeByte() and LSM9DS1_readByte()
Author
Stuart Ianna
Version
1.0
Date
June 2018
Warning
None
Bug:
None
Todo:
Add I2C function
Verified Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Example - Typical usage.
#include <stm32f103cb_core.h>
#include <coms128.h>
#include <lsm9ds1.h>
//Instance for the chip
LSM9DS1 chip;
//Containers to store the data in
int16_t magData[3];
int16_t accData[3];
int16_t gyrData[3];
float temp;
int main(void){
CLOCK_setSpeed(CLOCK_EX_8_OUT_72);
USART_setup(USART_1);
USART_setBaud(USART_1,USART_BAUD_115200);
COMS(USART_1);
//SPI and GPIO chip select pins must be setup before and externally of LSM9DS1_setup()
pinSetup(GPIO_DO,PORTA,PIN4);
pinSetup(GPIO_DO,PORTA,PIN3);
SPI_setup(SPI_1);
//Setup chip
LSM9DS1_setup(&chip,SPI_1,PORTA,PIN3,PORTA,PIN4,SPI_CS_LOW);
while(1){
//Magneto is the slower update (150Hz), use it to slow the loop time
//Gyro and accel can be read faster (950Hz)
while(!LSM9DS1_magReady(&chip));
LSM9DS1_getMag(&chip,magData);
LSM9DS1_getGyr(&chip,gyrData);
LSM9DS1_getAcc(&chip,accData);
temp = LSM9DS1_getTemp(&chip);
//Print all the data
printt(magData[0]);
printt(magData[1]);
printt(magData[2]);
printt(gyrData[0]);
printt(gyrData[1]);
printt(gyrData[2]);
printt(accData[0]);
printt(accData[1]);
printt(accData[2]);
printl(temp);
}
return 0;
}

Data Structure Documentation

struct LSM9DS1

Data structure containing variables specific to individual slave devices.

These values are set by LSM9DS1_setup() and do not need to be changed manually.

Definition at line 150 of file lsm9ds1.h.

Data Fields

uint8_t port
 The port the device is connected to.
 
uint32_t gx_pin
 The pin used for gyro/accel SPI chip select.
 
uint16_t gx_port
 The port used for gyro/accel SPI chip select.
 
uint32_t m_pin
 The pin used for mag SPI chip select.
 
uint16_t m_port
 The port used for mag SPI chip select.
 
uint8_t cs_pol
 The polary of the SPI chip select line.
 

Enumeration Type Documentation

Available accelerometer ranges.

Enumerator
LSM9DS1_ACC_2 

+-2G range

LSM9DS1_ACC_4 

+-4G range

LSM9DS1_ACC_8 

+-8G range

LSM9DS1_ACC_16 

+-16G range

Definition at line 175 of file lsm9ds1.h.

Available gyroscope ranges.

Enumerator
LSM9DS1_GYR_245 

+-245 degrees / second range

LSM9DS1_GYR_500 

+-500 degrees / second range

LSM9DS1_GYR_2000 

+-2000 degrees / second range

Definition at line 187 of file lsm9ds1.h.

Available magnetometer ranges.

Enumerator
LSM9DS1_MAG_4 

+-4 Gauss range

LSM9DS1_MAG_8 

+-8 Gauss range

LSM9DS1_MAG_12 

+-12 Gauss range

LSM9DS1_MAG_16 

+-16 Gauss range

Definition at line 163 of file lsm9ds1.h.

Function Documentation

uint8_t LSM9DS1_accReady ( LSM9DS1 target)

Check if the accelerometer has new data available.

Warning
The device will return rubish if new data isn't ready.
Parameters
targetPointer to the LSM9DS1 data structure
Returns
  • 0 if data is not ready
  • non-zero if data is ready

Definition at line 377 of file lsm9ds1.c.

void LSM9DS1_getAcc ( LSM9DS1 target,
int16_t *  data 
)

Get the latest accelerometer readings.

Warning
data must be of size 3 to avoid memory fault This function can be called at a maximum of 950Hz
Parameters
targetPointer to the LSM9DS1 data structure
dataContainer for the new data. Must be of size 3.
Returns
New readings are retured in data.

Definition at line 386 of file lsm9ds1.c.

uint8_t LSM9DS1_getAccScale ( LSM9DS1 target)

Get the scale of the accelerometer.

Parameters
targetPointer to the LSM9DS1 data structure
Returns
Integer value representing the scale

Definition at line 270 of file lsm9ds1.c.

void LSM9DS1_getGyr ( LSM9DS1 target,
int16_t *  data 
)

Get the latest gyroscope readings.

Warning
data must be of size 3 to avoid memory fault This function can be called at a maximum of 950Hz
Parameters
targetPointer to the LSM9DS1 data structure
dataContainer for the new data. Must be of size 3.
Returns
New readings are retured in data.

Definition at line 364 of file lsm9ds1.c.

uint16_t LSM9DS1_getGyrScale ( LSM9DS1 target)

Get the scale of the gyroscope.

Parameters
targetPointer to the LSM9DS1 data structure
Returns
Integer value representing the scale

Definition at line 304 of file lsm9ds1.c.

void LSM9DS1_getMag ( LSM9DS1 target,
int16_t *  data 
)

Get the latest magnometer readings.

Warning
data must be of size 3 to avoid memory fault. This function can be called at a maximum of 150Hz
Parameters
targetPointer to the LSM9DS1 data structure
dataContainer for the new data. Must be of size 3.
Returns
New readings are retured in data.

Definition at line 342 of file lsm9ds1.c.

uint8_t LSM9DS1_getMagScale ( LSM9DS1 target)

Get the scale of the magnetometer.

Parameters
targetPointer to the LSM9DS1 data structure
Returns
Integer value representing the scale

Definition at line 235 of file lsm9ds1.c.

float LSM9DS1_getTemp ( LSM9DS1 target)

Get the latest accelerometer readings.

Note
Update frequency for this module is not specified, tested ok at 150Hz
Parameters
targetPointer to the LSM9DS1 data structure
Returns
The temperature converted to degrees celcius

Definition at line 408 of file lsm9ds1.c.

uint8_t LSM9DS1_gxWAI ( LSM9DS1 target)

Read the value for the gyro/accel who am I register.

This function can be used to test the device connection.

Parameters
targetPointer to the LSM9DS1 data structure
Returns
A correct reading will be 104 (decimal)

Definition at line 420 of file lsm9ds1.c.

uint8_t LSM9DS1_gyrReady ( LSM9DS1 target)

Check if the gyroscope has new data available.

Warning
The device will return rubish if new data isn't ready.
Parameters
targetPointer to the LSM9DS1 data structure
Returns
  • 0 if data is not ready
  • non-zero if data is ready

Definition at line 355 of file lsm9ds1.c.

uint8_t LSM9DS1_magReady ( LSM9DS1 target)

Check if the magnetometer has new data available.

Warning
The device will return rubish if new data isn't ready.
Parameters
targetPointer to the LSM9DS1 data structure
Returns
  • 0 if data is not ready
  • non-zero if data is ready

Definition at line 333 of file lsm9ds1.c.

uint8_t LSM9DS1_mWAI ( LSM9DS1 target)

Read the value for the magnetometer who am I register.

This function can be used to test the device connection.

Parameters
targetPointer to the LSM9DS1 data structure
Returns
A correct reading will be 61 (decimal) (0x3D hex!)

Definition at line 426 of file lsm9ds1.c.

uint16_t LSM9DS1_readByte ( LSM9DS1 target,
uint8_t  component,
uint8_t  address 
)

Read a single byte from the device's register.

Parameters
targetPointer to the LSM9DS1 data structure
componentLSM9DS1_GX or LSM9DS1_M depending a on the register address.
addressThe device address to read from
Returns
The byte of data located at that address
Note
16-bit data type is return for compatability with SPI peripheral library.

Definition at line 446 of file lsm9ds1.c.

void LSM9DS1_setAccScale ( LSM9DS1 target,
lsm9ds1_accScale  scale 
)

Set the scale of the accelerometer.

Parameters
targetPointer to the LSM9DS1 data structure
scaleAccelerometer scale defined by lsm9ds1_accScale

Definition at line 153 of file lsm9ds1.c.

void LSM9DS1_setGyrScale ( LSM9DS1 target,
lsm9ds1_gyrScale  scale 
)

Set the scale of the gyroscope.

Parameters
targetPointer to the LSM9DS1 data structure
scaleGyroscope scale defined by lsm9ds1_gyrScale

Definition at line 198 of file lsm9ds1.c.

void LSM9DS1_setMagScale ( LSM9DS1 target,
lsm9ds1_magScale  scale 
)

Set the scale of the magnetometer.

Parameters
targetPointer to the LSM9DS1 data structure
scaleMagnometer scale defined by lsm9ds1_magScale

Definition at line 106 of file lsm9ds1.c.

void LSM9DS1_setup ( LSM9DS1 target,
uint8_t  spiPort,
uint16_t  gx_port,
uint32_t  gx_pin,
uint16_t  m_port,
uint32_t  m_pin,
uint8_t  cs_pol 
)

Initalizes the slave device and populates data structure variable.

Note
The relavent SPI port and GPIO pins must be setup before this is called
Parameters
targetPointer to the LSM9DS1 data structure
spiPortThe number of the peripheral port connected, typically SPI_1 or SPI_2
gx_portThe GPIO port which the gyro/accel chip select pin is connected
gx_pinThe GPIO pin which the gyro/accel chip select pin is connected
m_portThe GPIO port which the magneto chip select pin is connected
m_pinThe GPIO pin which the magneto chip select pin is connected
cs_polThe polarity of the chip select pin. This must be active low (SPI_CS_LOW)

Definition at line 32 of file lsm9ds1.c.

uint8_t LSM9DS1_tempReady ( LSM9DS1 target)

Check if the temperature module has new data available.

Warning
The device will return rubish if new data isn't ready.
Parameters
targetPointer to the LSM9DS1 data structure
Returns
  • 0 if data is not ready
  • non-zero if data is ready

Definition at line 399 of file lsm9ds1.c.

void LSM9DS1_writeByte ( LSM9DS1 target,
uint8_t  component,
uint8_t  address,
uint8_t  data 
)

Write a single byte to the device's register.

Parameters
targetPointer to the LSM9DS1 data structure
componentLSM9DS1_GX or LSM9DS1_M depending a on the register address.
addressThe device address to write to
dataThe data to write

Definition at line 432 of file lsm9ds1.c.