Routines for interaction with LSM9DS1 9D0F IMU.
More...
|
struct | LSM9DS1 |
| Data structure containing variables specific to individual slave devices. More...
|
|
|
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...
|
|
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
- Magnetometer 150Hz
- Accelerometer and gyroscope 950Hz
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
- Copyright
- GNU GPLv3
- 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>
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);
pinSetup(GPIO_DO,PORTA,PIN4);
pinSetup(GPIO_DO,PORTA,PIN3);
SPI_setup(SPI_1);
while(1){
}
return 0;
}
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.
|
|
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.
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
-
target | Pointer 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
-
target | Pointer to the LSM9DS1 data structure |
data | Container 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
-
target | Pointer 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
-
target | Pointer to the LSM9DS1 data structure |
data | Container 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
-
target | Pointer 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
-
target | Pointer to the LSM9DS1 data structure |
data | Container 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
-
target | Pointer 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
-
target | Pointer 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
-
target | Pointer 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
-
target | Pointer 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
-
target | Pointer 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
-
target | Pointer 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
-
target | Pointer to the LSM9DS1 data structure |
component | LSM9DS1_GX or LSM9DS1_M depending a on the register address. |
address | The 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.
Set the scale of the accelerometer.
- Parameters
-
Definition at line 153 of file lsm9ds1.c.
Set the scale of the gyroscope.
- Parameters
-
Definition at line 198 of file lsm9ds1.c.
Set the scale of the magnetometer.
- Parameters
-
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
-
target | Pointer to the LSM9DS1 data structure |
spiPort | The number of the peripheral port connected, typically SPI_1 or SPI_2 |
gx_port | The GPIO port which the gyro/accel chip select pin is connected |
gx_pin | The GPIO pin which the gyro/accel chip select pin is connected |
m_port | The GPIO port which the magneto chip select pin is connected |
m_pin | The GPIO pin which the magneto chip select pin is connected |
cs_pol | The 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
-
target | Pointer 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
-
target | Pointer to the LSM9DS1 data structure |
component | LSM9DS1_GX or LSM9DS1_M depending a on the register address. |
address | The device address to write to |
data | The data to write |
Definition at line 432 of file lsm9ds1.c.