11#include " drivers/Bma421.h"
2- #include < libraries/delay/nrf_delay.h>
3- #include < libraries/log/nrf_log.h>
42#include " drivers/TwiMaster.h"
53#include < drivers/Bma421_C/bma423.h>
4+ #include < libraries/delay/nrf_delay.h>
65
76using namespace Pinetime ::Drivers;
8-
97namespace {
108 int8_t user_i2c_read (uint8_t reg_addr, uint8_t * reg_data, uint32_t length, void * intf_ptr) {
119 auto bma421 = static_cast <Bma421*>(intf_ptr);
@@ -24,7 +22,7 @@ namespace {
2422 }
2523}
2624
27- Bma421::Bma421 (TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, deviceAddress { twiAddress} {
25+ Bma421::Bma421 (TwiMaster& twiMaster, uint8_t twiAddress) : AccelerationSensor( twiMaster, twiAddress) {
2826 bma.intf = BMA4_I2C_INTF;
2927 bma.bus_read = user_i2c_read;
3028 bma.bus_write = user_i2c_write;
@@ -35,45 +33,44 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
3533}
3634
3735void Bma421::Init () {
38- if (not isResetOk)
36+ if (! isResetOk)
3937 return ; // Call SoftReset (and reset TWI device) first!
4038
39+ // Initialize interface
4140 auto ret = bma423_init (&bma);
4241 if (ret != BMA4_OK)
4342 return ;
4443
44+ // Identify chip by ID. The driver code has been modified to handle BMA421 as BMA423
4545 switch (bma.chip_id ) {
4646 case BMA423_CHIP_ID:
47- deviceType = DeviceTypes ::BMA421;
47+ deviceType = AccelerationDeviceTypes ::BMA421;
4848 break ;
4949 case BMA425_CHIP_ID:
50- deviceType = DeviceTypes ::BMA425;
50+ deviceType = AccelerationDeviceTypes ::BMA425;
5151 break ;
5252 default :
53- deviceType = DeviceTypes ::Unknown;
53+ deviceType = AccelerationDeviceTypes ::Unknown;
5454 break ;
5555 }
5656
57+ // Load proprietary firmware blob required for step counting engine
5758 ret = bma423_write_config_file (&bma);
5859 if (ret != BMA4_OK)
5960 return ;
6061
61- ret = bma4_set_interrupt_mode (BMA4_LATCH_MODE, &bma);
62- if (ret != BMA4_OK)
63- return ;
64-
62+ // Enable step counter and accelerometer, disable step detector
6563 ret = bma423_feature_enable (BMA423_STEP_CNTR, 1 , &bma);
6664 if (ret != BMA4_OK)
6765 return ;
68-
6966 ret = bma423_step_detector_enable (0 , &bma);
7067 if (ret != BMA4_OK)
7168 return ;
72-
7369 ret = bma4_set_accel_enable (1 , &bma);
7470 if (ret != BMA4_OK)
7571 return ;
7672
73+ // Configure accelerometer
7774 struct bma4_accel_config accel_conf;
7875 accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
7976 accel_conf.range = BMA4_ACCEL_RANGE_2G;
@@ -83,25 +80,13 @@ void Bma421::Init() {
8380 if (ret != BMA4_OK)
8481 return ;
8582
86- isOk = true ;
83+ isInitialized = true ;
8784}
8885
89- void Bma421::Reset () {
90- uint8_t data = 0xb6 ;
91- twiMaster.Write (deviceAddress, 0x7E , &data, 1 );
92- }
93-
94- void Bma421::Read (uint8_t registerAddress, uint8_t * buffer, size_t size) {
95- twiMaster.Read (deviceAddress, registerAddress, buffer, size);
96- }
97-
98- void Bma421::Write (uint8_t registerAddress, const uint8_t * data, size_t size) {
99- twiMaster.Write (deviceAddress, registerAddress, data, size);
100- }
101-
102- Bma421::Values Bma421::Process () {
103- if (not isOk)
86+ AccelerationValues Bma421::Process () {
87+ if (!isInitialized)
10488 return {};
89+
10590 struct bma4_accel data;
10691 bma4_read_accel_xyz (&data, &bma);
10792
@@ -116,10 +101,7 @@ Bma421::Values Bma421::Process() {
116101 bma423_activity_output (&activity, &bma);
117102
118103 // X and Y axis are swapped because of the way the sensor is mounted in the PineTime
119- return {steps, data.y , data.x , data.z };
120- }
121- bool Bma421::IsOk () const {
122- return isOk;
104+ return {steps, data.y , data.x , data.z , (int16_t *) fifo, 0 };
123105}
124106
125107void Bma421::ResetStepCounter () {
@@ -133,6 +115,3 @@ void Bma421::SoftReset() {
133115 nrf_delay_ms (1 );
134116 }
135117}
136- Bma421::DeviceTypes Bma421::DeviceType () const {
137- return deviceType;
138- }
0 commit comments