Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ list(APPEND SOURCE_FILES
drivers/DebugPins.cpp
drivers/InternalFlash.cpp
drivers/Hrs3300.cpp
drivers/AccelerationSensor.cpp
drivers/Bma421.cpp
drivers/Bma421_C/bma4.c
drivers/Bma421_C/bma423.c
drivers/SC7A20.cpp
components/battery/BatteryController.cpp
components/ble/BleController.cpp
components/ble/NotificationManager.cpp
Expand Down Expand Up @@ -510,9 +512,11 @@ list(APPEND RECOVERY_SOURCE_FILES
drivers/DebugPins.cpp
drivers/InternalFlash.cpp
drivers/Hrs3300.cpp
drivers/AccelerationSensor.cpp
drivers/Bma421.cpp
drivers/Bma421_C/bma4.c
drivers/Bma421_C/bma423.c
drivers/SC7A20.cpp
components/battery/BatteryController.cpp
components/ble/BleController.cpp
components/ble/NotificationManager.cpp
Expand Down Expand Up @@ -626,9 +630,12 @@ set(INCLUDE_FILES
drivers/InternalFlash.h
drivers/Hrs3300.h
drivers/PinMap.h
drivers/AccelerationSensor.h
drivers/Bma421.h
drivers/Bma421_C/bma4.c
drivers/Bma421_C/bma423.c
drivers/SC7A20.h
drivers/SC7A20_registers.h
components/battery/BatteryController.h
components/ble/BleController.h
components/ble/NotificationManager.h
Expand Down Expand Up @@ -798,23 +805,28 @@ add_definitions(-DTARGET_DEVICE_NAME="${TARGET_DEVICE}")
if(TARGET_DEVICE STREQUAL "PINETIME")
add_definitions(-DDRIVER_PINMAP_PINETIME)
add_definitions(-DCLOCK_CONFIG_LF_SRC=1) # XTAL
add_definitions(-DDRIVER_ACC_BMA421)
elseif(TARGET_DEVICE STREQUAL "MOY-TFK5") # P8a
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=1) # XTAL
add_definitions(-DDRIVER_ACC_BMA421)
elseif(TARGET_DEVICE STREQUAL "MOY-TIN5") # P8a variant 2
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=1) # XTAL
add_definitions(-DDRIVER_ACC_SC7A20)
elseif(TARGET_DEVICE STREQUAL "MOY-TON5") # P8b
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=0) # RC
add_definitions(-DMYNEWT_VAL_BLE_LL_SCA=500)
add_definitions(-DCLOCK_CONFIG_LF_CAL_ENABLED=1)
add_definitions(-DDRIVER_ACC_SC7A20)
elseif(TARGET_DEVICE STREQUAL "MOY-UNK") # P8b mirrored
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=0) # RC
add_definitions(-DMYNEWT_VAL_BLE_LL_SCA=500)
add_definitions(-DCLOCK_CONFIG_LF_CAL_ENABLED=1)
add_definitions(-DDRIVER_DISPLAY_MIRROR)
add_definitions(-DDRIVER_ACC_SC7A20)
else()
message(FATAL_ERROR "Invalid TARGET_DEVICE")
endif()
Expand Down
17 changes: 5 additions & 12 deletions src/components/motion/MotionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,19 @@ bool MotionController::Should_ShakeWake(uint16_t thresh) {
lastZForShake = z;
return wake;
}

int32_t MotionController::currentShakeSpeed() {
return accumulatedspeed;
}

void MotionController::IsSensorOk(bool isOk) {
isSensorOk = isOk;
}
void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) {
switch (types) {
case Drivers::Bma421::DeviceTypes::BMA421:
this->deviceType = DeviceTypes::BMA421;
break;
case Drivers::Bma421::DeviceTypes::BMA425:
this->deviceType = DeviceTypes::BMA425;
break;
default:
this->deviceType = DeviceTypes::Unknown;
break;
}

void MotionController::Init(Pinetime::Drivers::AccelerationDeviceTypes types) {
this->deviceType = types;
}

void MotionController::SetService(Pinetime::Controllers::MotionService* service) {
this->service = service;
}
12 changes: 3 additions & 9 deletions src/components/motion/MotionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ namespace Pinetime {
namespace Controllers {
class MotionController {
public:
enum class DeviceTypes {
Unknown,
BMA421,
BMA425,
};

void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps);

int16_t X() const {
Expand Down Expand Up @@ -44,11 +38,11 @@ namespace Pinetime {
return isSensorOk;
}

DeviceTypes DeviceType() const {
Pinetime::Drivers::AccelerationDeviceTypes DeviceType() const {
return deviceType;
}

void Init(Pinetime::Drivers::Bma421::DeviceTypes types);
void Init(Pinetime::Drivers::AccelerationDeviceTypes types);
void SetService(Pinetime::Controllers::MotionService* service);

private:
Expand All @@ -59,7 +53,7 @@ namespace Pinetime {
int16_t z;
int16_t lastYForWakeUp = 0;
bool isSensorOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown;
Pinetime::Drivers::AccelerationDeviceTypes deviceType = Pinetime::Drivers::AccelerationDeviceTypes::Unknown;
Pinetime::Controllers::MotionService* service = nullptr;

int16_t lastXForShake = 0;
Expand Down
10 changes: 6 additions & 4 deletions src/displayapp/screens/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
using namespace Pinetime::Applications::Screens;

namespace {
const char* ToString(const Pinetime::Controllers::MotionController::DeviceTypes deviceType) {
const char* ToString(const Pinetime::Drivers::AccelerationDeviceTypes deviceType) {
switch (deviceType) {
case Pinetime::Controllers::MotionController::DeviceTypes::BMA421:
case Pinetime::Drivers::AccelerationDeviceTypes::BMA421:
return "BMA421";
case Pinetime::Controllers::MotionController::DeviceTypes::BMA425:
case Pinetime::Drivers::AccelerationDeviceTypes::BMA425:
return "BMA425";
case Pinetime::Controllers::MotionController::DeviceTypes::Unknown:
case Pinetime::Drivers::AccelerationDeviceTypes::SC7A20:
return "SC7A20";
case Pinetime::Drivers::AccelerationDeviceTypes::Unknown:
return "???";
}
return "???";
Expand Down
39 changes: 39 additions & 0 deletions src/drivers/AccelerationSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "AccelerationSensor.h"

namespace Pinetime {
namespace Drivers {

AccelerationSensor::AccelerationSensor(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster(twiMaster), deviceAddress(twiAddress) {
}

void AccelerationSensor::SoftReset() {
}

void AccelerationSensor::Init() {
}

AccelerationValues AccelerationSensor::Process() {
return {0};
}

void AccelerationSensor::ResetStepCounter() {
}

void AccelerationSensor::Read(uint8_t registerAddress, uint8_t* buffer, size_t size) {
twiMaster.Read(deviceAddress, registerAddress, buffer, size);
}

void AccelerationSensor::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
twiMaster.Write(deviceAddress, registerAddress, data, size);
}

AccelerationDeviceTypes AccelerationSensor::DeviceType() const {
return deviceType;
}

bool AccelerationSensor::IsInitialized() const {
return isInitialized;
}

}
}
47 changes: 47 additions & 0 deletions src/drivers/AccelerationSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "drivers/TwiMaster.h"

namespace Pinetime {
namespace Drivers {

enum class AccelerationDeviceTypes : uint8_t { Unknown, BMA421, BMA425, SC7A20 };

struct AccelerationValues {
uint32_t steps;
int16_t x;
int16_t y;
int16_t z;
int16_t* samples = nullptr;
uint16_t samples_length = 0;
};

class AccelerationSensor {
public:
AccelerationSensor(TwiMaster& twiMaster, uint8_t twiAddress);
AccelerationSensor(const AccelerationSensor&) = delete;
AccelerationSensor& operator=(const AccelerationSensor&) = delete;
AccelerationSensor(AccelerationSensor&&) = delete;
AccelerationSensor& operator=(AccelerationSensor&&) = delete;

virtual void SoftReset();
virtual void Init();
virtual AccelerationValues Process();
virtual void ResetStepCounter();

void Read(uint8_t registerAddress, uint8_t* buffer, size_t size);
void Write(uint8_t registerAddress, const uint8_t* data, size_t size);

bool IsInitialized() const;
AccelerationDeviceTypes DeviceType() const;

protected:
TwiMaster& twiMaster;
uint8_t deviceAddress;
bool isInitialized = false;
AccelerationDeviceTypes deviceType = AccelerationDeviceTypes::Unknown;
int16_t fifo[32][3] = {0};
};

}
}
53 changes: 16 additions & 37 deletions src/drivers/Bma421.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "drivers/Bma421.h"
#include <libraries/delay/nrf_delay.h>
#include <libraries/log/nrf_log.h>
#include "drivers/TwiMaster.h"
#include <drivers/Bma421_C/bma423.h>
#include <libraries/delay/nrf_delay.h>

using namespace Pinetime::Drivers;

namespace {
int8_t user_i2c_read(uint8_t reg_addr, uint8_t* reg_data, uint32_t length, void* intf_ptr) {
auto bma421 = static_cast<Bma421*>(intf_ptr);
Expand All @@ -24,7 +22,7 @@ namespace {
}
}

Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, deviceAddress {twiAddress} {
Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : AccelerationSensor(twiMaster, twiAddress) {
bma.intf = BMA4_I2C_INTF;
bma.bus_read = user_i2c_read;
bma.bus_write = user_i2c_write;
Expand All @@ -35,45 +33,44 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
}

void Bma421::Init() {
if (not isResetOk)
if (!isResetOk)
return; // Call SoftReset (and reset TWI device) first!

// Initialize interface
auto ret = bma423_init(&bma);
if (ret != BMA4_OK)
return;

// Identify chip by ID. The driver code has been modified to handle BMA421 as BMA423
switch (bma.chip_id) {
case BMA423_CHIP_ID:
deviceType = DeviceTypes::BMA421;
deviceType = AccelerationDeviceTypes::BMA421;
break;
case BMA425_CHIP_ID:
deviceType = DeviceTypes::BMA425;
deviceType = AccelerationDeviceTypes::BMA425;
break;
default:
deviceType = DeviceTypes::Unknown;
deviceType = AccelerationDeviceTypes::Unknown;
break;
}

// Load proprietary firmware blob required for step counting engine
ret = bma423_write_config_file(&bma);
if (ret != BMA4_OK)
return;

ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
if (ret != BMA4_OK)
return;

// Enable step counter and accelerometer, disable step detector
ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
if (ret != BMA4_OK)
return;

ret = bma423_step_detector_enable(0, &bma);
if (ret != BMA4_OK)
return;

ret = bma4_set_accel_enable(1, &bma);
if (ret != BMA4_OK)
return;

// Configure accelerometer
struct bma4_accel_config accel_conf;
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
accel_conf.range = BMA4_ACCEL_RANGE_2G;
Expand All @@ -83,25 +80,13 @@ void Bma421::Init() {
if (ret != BMA4_OK)
return;

isOk = true;
isInitialized = true;
}

void Bma421::Reset() {
uint8_t data = 0xb6;
twiMaster.Write(deviceAddress, 0x7E, &data, 1);
}

void Bma421::Read(uint8_t registerAddress, uint8_t* buffer, size_t size) {
twiMaster.Read(deviceAddress, registerAddress, buffer, size);
}

void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
twiMaster.Write(deviceAddress, registerAddress, data, size);
}

Bma421::Values Bma421::Process() {
if (not isOk)
AccelerationValues Bma421::Process() {
if (!isInitialized)
return {};

struct bma4_accel data;
bma4_read_accel_xyz(&data, &bma);

Expand All @@ -116,10 +101,7 @@ Bma421::Values Bma421::Process() {
bma423_activity_output(&activity, &bma);

// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
return {steps, data.y, data.x, data.z};
}
bool Bma421::IsOk() const {
return isOk;
return {steps, data.y, data.x, data.z, (int16_t*) fifo, 0};
}

void Bma421::ResetStepCounter() {
Expand All @@ -133,6 +115,3 @@ void Bma421::SoftReset() {
nrf_delay_ms(1);
}
}
Bma421::DeviceTypes Bma421::DeviceType() const {
return deviceType;
}
Loading