-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LSM9DS1 interrupts #5
Open
seanboe
wants to merge
6
commits into
ucsd-cubesat:main
Choose a base branch
from
seanboe:LSM9DS1-interrupts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68d8e9b
Got basic LSM9DS1 functionality working on TeensyLC
seanboe 2530810
added accelerometer interrupts - need to test
seanboe 91277c5
Interrupts working on accelerometer
seanboe 932551e
Gyro interrupts working
seanboe e9a3c15
Magnetometer interrupts working
seanboe 46b43e7
cleanup
seanboe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
#include "LSM9DS1.h" | ||
#include "LSM9DS1_registers.h" | ||
|
||
bool LSM9DS1::XLGisAvailable() { | ||
uint8_t whoami = m_accel_gyro.read(REG_WHO_AM_I); | ||
return whoami == 0x68; | ||
} | ||
|
||
uint8_t LSM9DS1::rebootXLG() { | ||
m_accel_gyro.write(REG_CTRL_REG8, 0x05); | ||
delay(10); | ||
|
@@ -105,6 +110,18 @@ void LSM9DS1::readGyroscope(float &x, float &y, float &z) { | |
} | ||
|
||
|
||
void LSM9DS1::setGyroActivity(bool wake) { | ||
uint8_t config = m_accel_gyro.read(REG_CTRL_REG9); | ||
if (wake) { | ||
// set the sleep bit off | ||
config |= ~(1 << 6); | ||
} else { | ||
config |= (1 << 6); | ||
} | ||
m_accel_gyro.write(REG_CTRL_REG1_G, config); | ||
} | ||
|
||
|
||
|
||
void LSM9DS1::readMagnetosensor(int16_t &x, int16_t &y, int16_t &z) { | ||
uint8_t buff[6]; | ||
|
@@ -125,6 +142,154 @@ void LSM9DS1::readMagnetosensor(float &x, float &y, float &z) { | |
z = z_raw * resolution / 1000; | ||
} | ||
|
||
void LSM9DS1::configXLInterrupt(INT_XL_CONFIG intConfig, bool andInterrupt, uint8_t duration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read the configuration in the registers here and update those values instead of writing directly to the register to avoid overwriting bits that were already set |
||
|
||
uint8_t config = intConfig; | ||
if (andInterrupt) { | ||
config |= (1 << 7); | ||
} | ||
m_accel_gyro.write(INT_GEN_CFG_XL, config); | ||
|
||
// Configure the duration | ||
config = 0; | ||
|
||
if (duration != 0) { | ||
config |= (duration & 0b01111111); | ||
// configure wait bit | ||
config |= (1 << 7); | ||
} | ||
|
||
m_accel_gyro.write(INT_GEN_DUR_XL, config); | ||
} | ||
|
||
void LSM9DS1::setXLaxisInterruptTHS(AXIS axis, uint8_t threshold) { | ||
uint8_t axisRegister = 0; | ||
|
||
switch (axis) { | ||
case AXIS_X: axisRegister = INT_GEN_THS_X_XL; break; | ||
case AXIS_Y: axisRegister = INT_GEN_THS_Y_XL; break; | ||
case AXIS_Z: axisRegister = INT_GEN_THS_Z_XL; break; | ||
} | ||
|
||
m_accel_gyro.write(axisRegister, threshold); | ||
} | ||
|
||
void LSM9DS1::setAllXLInterruptTHS(uint8_t threshold) { | ||
setXLaxisInterruptTHS(AXIS_X, threshold); | ||
setXLaxisInterruptTHS(AXIS_Y, threshold); | ||
setXLaxisInterruptTHS(AXIS_Z, threshold); | ||
} | ||
|
||
void LSM9DS1::configGInterrupt(INT_G_CONFIG intConfig, bool andInterrupt, bool latching, uint8_t duration) { | ||
|
||
uint8_t config = intConfig; | ||
if (andInterrupt) { | ||
config |= (1 << 7); | ||
} | ||
if (latching) { | ||
config |= (1 << 6); | ||
} | ||
|
||
m_accel_gyro.write(INT_GEN_CFG_G, config); | ||
|
||
// Configure the duration | ||
config = 0; | ||
|
||
if (duration != 0) { | ||
config |= (duration & 0b01111111); | ||
// configure wait bit | ||
config |= (1 << 7); | ||
} | ||
|
||
m_accel_gyro.write(INT_GEN_DUR_G, config); | ||
} | ||
|
||
void LSM9DS1::setGInterruptTHS(AXIS axis, uint16_t threshold, bool resetCounter) { | ||
|
||
uint8_t axisRegisterH = 0; | ||
uint8_t axisRegisterL = 0; | ||
|
||
switch (axis) { | ||
case AXIS_X: axisRegisterH = INT_GEN_THS_XH_G; axisRegisterL = INT_GEN_THS_XL_G; break; | ||
case AXIS_Y: axisRegisterH = INT_GEN_THS_YH_G; axisRegisterL = INT_GEN_THS_YL_G; break; | ||
case AXIS_Z: axisRegisterH = INT_GEN_THS_ZH_G; axisRegisterL = INT_GEN_THS_ZL_G; break; | ||
} | ||
|
||
uint8_t highByte = (threshold >> 8) & 0b01111111; | ||
uint8_t lowByte = threshold & 0b11111111; | ||
|
||
if (!resetCounter) { | ||
// Instead of resetting the counter, opt to decrement the counter | ||
highByte |= (1 << 7); | ||
} | ||
|
||
m_accel_gyro.write(axisRegisterH, highByte); | ||
m_accel_gyro.write(axisRegisterL, lowByte); | ||
} | ||
|
||
|
||
void LSM9DS1::setAllGInterruptTHS(uint16_t threshold, bool resetCounter) { | ||
setGInterruptTHS(AXIS_X, threshold, resetCounter); | ||
setGInterruptTHS(AXIS_Y, threshold, resetCounter); | ||
setGInterruptTHS(AXIS_Z, threshold, resetCounter); | ||
} | ||
|
||
|
||
void LSM9DS1::configXLGIntPin(INT_PIN pin, uint8_t config, bool pushPull, bool activeHigh) { | ||
|
||
switch (pin) { | ||
case INT1: m_accel_gyro.write(INT1_CTRL, config); | ||
case INT2: m_accel_gyro.write(INT2_CTRL, config); | ||
} | ||
|
||
// Active high / low and push-pull / open-drain settings are configured in CTRL_REG8 | ||
config = 0; | ||
if (!activeHigh) { | ||
config |= (1 << 5); | ||
} | ||
|
||
if (!pushPull) { | ||
config |= (1 << 4); | ||
} | ||
|
||
m_accel_gyro.write(REG_CTRL_REG8, config); | ||
} | ||
|
||
void LSM9DS1::configMagInterrupt(INT_MAG_CONFIG intConfig, bool latch, bool activeHigh) { | ||
|
||
uint8_t config = intConfig; | ||
|
||
// Enable interrupt functionality | ||
config |= (1); | ||
|
||
// For some reason the datsheet spec is opposite; | ||
// if the latch bit is 1, then it latches, else not | ||
if (latch) { | ||
config |= (1 << 1); | ||
} | ||
|
||
if (activeHigh) { | ||
config |= (1 << 2); | ||
} | ||
|
||
m_magneto.write(INT_CFG_M, config); | ||
} | ||
|
||
void LSM9DS1::configMagTHS(uint16_t threshold) { | ||
|
||
uint8_t highByte = (threshold >> 8) & 0b01111111; | ||
uint8_t lowByte = threshold & 0b11111111; | ||
|
||
m_magneto.write(INT_THS_H_M, highByte); | ||
m_magneto.write(INT_THS_L_M, lowByte); | ||
} | ||
|
||
|
||
void LSM9DS1::unlatchMagInt() { | ||
uint8_t settings = m_magneto.read(INT_SRC_M); | ||
} | ||
|
||
|
||
float LSM9DS1::convertRaw(int16_t raw, FS_XL scale) { | ||
float resolution = scale == FS_XL_2 ? 0.061f : | ||
scale == FS_XL_4 ? 0.122f : | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review this method and the registers used, please add clarifying comments on the register in the header file