forked from GeorgeHahn-Lab651/sensei_mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding twi/i2c drivers * spi comms to mma8451 working * Jostle detection working and reporting via mesh network * Adding config files
- Loading branch information
Showing
11 changed files
with
391 additions
and
3 deletions.
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
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 |
---|---|---|
|
@@ -7,6 +7,8 @@ extern "C" { | |
|
||
#define SIMBLEE | ||
|
||
#define MMA8541 | ||
|
||
#define LEDS_NUMBER 0 | ||
#define BUTTONS_NUMBER 0 | ||
|
||
|
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,3 +1,4 @@ | ||
ipython | ||
pyyaml | ||
sensei_client | ||
pyserial |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright (c) 2013 Microsoft Corporation. All Rights Reserved. | ||
* | ||
*/ | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include "twi_master.h" | ||
#include "i2c.h" | ||
|
||
void i2c_init() { | ||
twi_master_init(); | ||
} | ||
|
||
// read n-bytes from i2c register at the given address where subsequent bytes | ||
// are read from incrementally increasing register addresses. | ||
bool i2c_read_data(uint8_t address, uint8_t reg, uint8_t* data, uint8_t len) | ||
{ | ||
|
||
address = address << 1; | ||
|
||
// initialize data to zero so we don't return random values. | ||
for (int i = 0; i < len; i++) | ||
{ | ||
data[i] = 0; | ||
} | ||
|
||
// Write: register address we want to start reading from | ||
if (twi_master_transfer(address, ®, 1, TWI_DONT_ISSUE_STOP)) | ||
{ | ||
// Read: the number of bytes requested. | ||
if (twi_master_transfer(address | TWI_READ_BIT, data, len, TWI_ISSUE_STOP)) | ||
{ | ||
// Read succeeded. | ||
return true; | ||
} | ||
} | ||
|
||
// read or write failed. | ||
return false; | ||
} | ||
|
||
// read the i2c register at the given address (see table 13 in the LSM9DS0 spec) | ||
// first we write the register address to tell the device to prepare that value | ||
// then we read 1 byte in response from the same i2c device. | ||
uint8_t i2c_read_reg(uint8_t address, uint8_t reg) | ||
{ | ||
uint8_t data = 0; | ||
|
||
if (i2c_read_data(address, reg, &data, 1)) | ||
{ | ||
return data; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
// write 1 byte to the i2c register at the given address (see table 11 in the LSM9DS0 spec) | ||
bool i2c_write_reg(uint8_t address, uint8_t reg, uint8_t value) | ||
{ | ||
uint8_t data[2]; | ||
data[0] = reg; | ||
data[1] = value; | ||
|
||
address = address << 1; | ||
|
||
// Write: register protocol | ||
if (twi_master_transfer(address, data, 2, TWI_ISSUE_STOP)) | ||
{ | ||
return true; | ||
} | ||
|
||
// read or write failed. | ||
return false; | ||
} | ||
|
||
|
||
/*lint --flb "Leave library region" */ |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef _SENSEI_I2C_H | ||
#define _SENSEI_I2C_H | ||
|
||
void i2c_init(); | ||
|
||
// read n-bytes from i2c register at the given address where subsequent bytes | ||
// are read from incrementally increasing register addresses. | ||
bool i2c_read_data(uint8_t address, uint8_t reg, uint8_t* data, uint8_t len); | ||
|
||
// read the i2c register at the given address | ||
// first we write the register address to tell the device to prepare that value | ||
// then we read 1 byte in response from the same i2c device. | ||
uint8_t i2c_read_reg(uint8_t address, uint8_t reg); | ||
|
||
// write 1 byte to the i2c register at the given address | ||
bool i2c_write_reg(uint8_t address, uint8_t reg, uint8_t value); | ||
|
||
#endif // _SENSEI_I2C_H |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
#include "jostle_detect.h" | ||
|
||
#ifdef MMA8541 | ||
|
||
#include "nrf_drv_gpiote.h" | ||
#include "i2c.h" | ||
#include "app_error.h" | ||
|
||
#define MMA8451_DEFAULT_ADDRESS (0x1C) // if A is GND, it's 0x1C | ||
|
||
#define MMA8451_REG_OUT_X_MSB 0x01 | ||
#define MMA8451_REG_SYSMOD 0x0B | ||
#define MMA8451_REG_WHOAMI 0x0D | ||
#define MMA8451_REG_XYZ_DATA_CFG 0x0E | ||
#define MMA8451_REG_PL_STATUS 0x10 | ||
#define MMA8451_REG_PL_CFG 0x11 | ||
#define MMA8451_REG_FF_MT_CFG 0x15 | ||
#define MMA8451_REG_FF_MT_SRC 0x16 | ||
#define MMA8451_REG_FF_MT_THS 0x17 | ||
#define MMA8451_REG_FF_MT_COUNT 0x18 | ||
#define MMA8451_REG_CTRL_REG1 0x2A | ||
#define MMA8451_REG_CTRL_REG2 0x2B | ||
#define MMA8451_REG_CTRL_REG4 0x2D | ||
#define MMA8451_REG_CTRL_REG5 0x2E | ||
|
||
#define INT_EN_FF_MT (1<<2) | ||
#define INT_EN_PULSE (1<<3) | ||
|
||
#define INT1_GPIO_PIN 24 | ||
#define INT2_GPIO_PIN 22 | ||
|
||
static bool jostle_detected; | ||
|
||
typedef enum | ||
{ | ||
MMA8451_RANGE_8_G = 0b10, // +/- 8g | ||
MMA8451_RANGE_4_G = 0b01, // +/- 4g | ||
MMA8451_RANGE_2_G = 0b00 // +/- 2g (default value) | ||
} mma8451_range_t; | ||
|
||
#define MMA8451_HIGH_PASS_FILTER 0b10000 | ||
|
||
static bool write_register(uint8_t reg, uint8_t value) { | ||
return i2c_write_reg(MMA8451_DEFAULT_ADDRESS, reg, value); | ||
} | ||
|
||
static uint8_t read_register(uint8_t reg) { | ||
return i2c_read_reg(MMA8451_DEFAULT_ADDRESS, reg); | ||
} | ||
|
||
void motion_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) | ||
{ | ||
read_register(MMA8451_REG_FF_MT_SRC); | ||
jostle_detected = true; | ||
} | ||
|
||
void jostle_detect_init() { | ||
i2c_init(); | ||
|
||
ret_code_t err_code; | ||
|
||
err_code = nrf_drv_gpiote_init(); | ||
APP_ERROR_CHECK(err_code); | ||
|
||
nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true); | ||
in_config.pull = NRF_GPIO_PIN_PULLUP; | ||
|
||
err_code = nrf_drv_gpiote_in_init(INT1_GPIO_PIN, &in_config, motion_handler); | ||
APP_ERROR_CHECK(err_code); | ||
|
||
nrf_drv_gpiote_in_event_enable(INT1_GPIO_PIN, true); | ||
|
||
// Configure mma8451 | ||
write_register(MMA8451_REG_CTRL_REG2, 0x40); // reset | ||
while (read_register(MMA8451_REG_CTRL_REG2) & 0x40); | ||
|
||
// enable 4G range | ||
write_register(MMA8451_REG_XYZ_DATA_CFG, MMA8451_RANGE_4_G); | ||
// High res | ||
write_register(MMA8451_REG_CTRL_REG2, 0x02); | ||
|
||
// Setup motion detection | ||
write_register(MMA8451_REG_FF_MT_CFG, 0b11111000); // Enable motion, and x,y,z | ||
write_register(MMA8451_REG_FF_MT_THS, 17); // Threshold: 17 * 0.063g = 1.071g | ||
write_register(MMA8451_REG_FF_MT_COUNT, 15); // debounce counter: 15 * 1.25ms = 18.75ms | ||
|
||
// Setup interrupts | ||
write_register(MMA8451_REG_CTRL_REG4, INT_EN_FF_MT | INT_EN_PULSE); // Enable Freefall/Motion int, and pulse | ||
write_register(MMA8451_REG_CTRL_REG5, 0b00000100); // Route motion to INT1 | ||
|
||
// Turn on orientation config | ||
//write_register(MMA8451_REG_PL_CFG, 0x40); | ||
|
||
// ASLP_RATE = 50hz | ||
// ODR = 800hz (1.25ms period) | ||
// LNOISE = 1 | ||
// F_READ = normal | ||
// ACTIVE = active | ||
write_register(MMA8451_REG_CTRL_REG1, 0b00000101); | ||
} | ||
|
||
bool jostle_detect_get_flag() { | ||
return jostle_detected; | ||
} | ||
|
||
void jostle_detect_clear_flag() { | ||
jostle_detected = false; | ||
} | ||
|
||
#endif // MMA8541 |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef JOSTLE_DETECT_H | ||
#define JOSTLE_DETECT_H | ||
|
||
#include "stdint.h" | ||
#include "boards.h" | ||
|
||
void jostle_detect_init(); | ||
bool jostle_detect_get_flag(); | ||
void jostle_detect_clear_flag(); | ||
|
||
|
||
#endif // JOSTLE_DETECT_H |
Oops, something went wrong.