Skip to content

Commit

Permalink
Jostle (#1)
Browse files Browse the repository at this point in the history
* adding twi/i2c drivers

* spi comms to mma8451 working

* Jostle detection working and reporting via mesh network

* Adding config files
  • Loading branch information
ps2 authored May 22, 2017
1 parent d441fa4 commit 180d984
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-ou

C_SOURCE_FILES += src/main.c src/config.c src/sensor.c src/app_cmd.c \
src/scheduler.c src/proximity.c src/heartbeat.c src/battery.c src/shoe_accel.c \
src/app_evt.c src/mesh_control.c bsp/bsp.c
src/app_evt.c src/mesh_control.c bsp/bsp.c src/i2c.c src/jostle_detect.c
C_SOURCE_FILES += $(COMPONENTS)/libraries/timer/app_timer.c

CXX_SOURCE_FILES += $(SIMBLEE_BASE)/libraries/SimbleeBLE/SimbleeBLE.cpp
Expand Down Expand Up @@ -134,7 +134,9 @@ C_SOURCE_FILES += $(RBC_MESH)/src/rand.c
C_SOURCE_FILES += $(COMPONENTS)/ble/common/ble_advdata.c
C_SOURCE_FILES += $(COMPONENTS)/toolchain/system_nrf51.c
C_SOURCE_FILES += $(COMPONENTS)/softdevice/common/softdevice_handler/softdevice_handler.c

C_SOURCE_FILES += $(COMPONENTS)/drivers_nrf/twi_master/incubated/twi_hw_master.c
C_SOURCE_FILES += $(COMPONENTS)/drivers_nrf/gpiote/nrf_drv_gpiote.c
C_SOURCE_FILES += $(COMPONENTS)/drivers_nrf/common/nrf_drv_common.c

# assembly files common to all targets
#ASM_SOURCE_FILES += $(COMPONENTS)/toolchain/gcc/gcc_startup_nrf51.s
Expand Down Expand Up @@ -162,8 +164,11 @@ INC_PATHS += -I$(COMPONENTS)/toolchain/gcc
INC_PATHS += -I$(COMPONENTS)/libraries/util
INC_PATHS += -I$(COMPONENTS)/libraries/timer
INC_PATHS += -I$(COMPONENTS)/ble/common
INC_PATHS += -I$(COMPONENTS)/drivers_nrf/common
INC_PATHS += -I$(COMPONENTS)/drivers_nrf/hal
INC_PATHS += -I$(COMPONENTS)/drivers_nrf/pstorage
INC_PATHS += -I$(COMPONENTS)/drivers_nrf/twi_master/incubated/
INC_PATHS += -I$(COMPONENTS)/drivers_nrf/gpiote

INC_PATHS += -I$(COMPONENTS)/toolchain/gcc
INC_PATHS += -I$(COMPONENTS)/toolchain
Expand Down
2 changes: 2 additions & 0 deletions bsp/lesson_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ extern "C" {

#define SIMBLEE

#define MMA8541

#define LEDS_NUMBER 0
#define BUTTONS_NUMBER 0

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ipython
pyyaml
sensei_client
pyserial
77 changes: 77 additions & 0 deletions src/i2c.c
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, &reg, 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" */
18 changes: 18 additions & 0 deletions src/i2c.h
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
111 changes: 111 additions & 0 deletions src/jostle_detect.c
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
12 changes: 12 additions & 0 deletions src/jostle_detect.h
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
Loading

0 comments on commit 180d984

Please sign in to comment.