Skip to content

Commit ccea19b

Browse files
committed
Acceleration: Add support for SC7A20 chip
The FIFO buffer is used as well
1 parent 91c6b50 commit ccea19b

File tree

4 files changed

+535
-0
lines changed

4 files changed

+535
-0
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ list(APPEND SOURCE_FILES
442442
drivers/Bma421.cpp
443443
drivers/Bma421_C/bma4.c
444444
drivers/Bma421_C/bma423.c
445+
drivers/SC7A20.cpp
445446
components/battery/BatteryController.cpp
446447
components/ble/BleController.cpp
447448
components/ble/NotificationManager.cpp
@@ -510,6 +511,7 @@ list(APPEND RECOVERY_SOURCE_FILES
510511
drivers/Bma421.cpp
511512
drivers/Bma421_C/bma4.c
512513
drivers/Bma421_C/bma423.c
514+
drivers/SC7A20.cpp
513515
components/battery/BatteryController.cpp
514516
components/ble/BleController.cpp
515517
components/ble/NotificationManager.cpp
@@ -624,6 +626,8 @@ set(INCLUDE_FILES
624626
drivers/Bma421.h
625627
drivers/Bma421_C/bma4.c
626628
drivers/Bma421_C/bma423.c
629+
drivers/SC7A20.h
630+
drivers/SC7A20_registers.h
627631
components/battery/BatteryController.h
628632
components/ble/BleController.h
629633
components/ble/NotificationManager.h

src/drivers/SC7A20.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "drivers/SC7A20.h"
2+
#include "drivers/SC7A20_registers.h"
3+
#include <libraries/delay/nrf_delay.h>
4+
#include <task.h>
5+
6+
using namespace Pinetime::Drivers;
7+
8+
SC7A20::SC7A20(TwiMaster& twiMaster, uint8_t twiAddress) : AccelerationSensor(twiMaster, twiAddress) {
9+
}
10+
11+
void SC7A20::Init() {
12+
// Reset internal memory
13+
uint8_t data = CTRL_REG5_BOOT;
14+
Write(CTRL_REG5, &data, 1);
15+
vTaskDelay(5);
16+
data = 0;
17+
Write(CTRL_REG5, &data, 1);
18+
19+
// Read Chip ID
20+
Read(WHO_AM_I, &data, 1);
21+
if (data == 17) {
22+
deviceType = AccelerationDeviceTypes::SC7A20;
23+
} else {
24+
deviceType = AccelerationDeviceTypes::Unknown;
25+
return;
26+
}
27+
28+
// Configure resolution to be +-2g
29+
data = CTRL_REG4_FS_2G;
30+
Write(CTRL_REG4, &data, 1);
31+
32+
// Enable block update, configure 12 bit resolution mode
33+
data = CTRL_REG4_BDU | CTRL_REG4_HR;
34+
Write(CTRL_REG4, &data, 1);
35+
36+
// Use FIFO for batch data
37+
data = CTRL_REG5_FIFO_EN;
38+
Write(CTRL_REG5, &data, 1);
39+
data = FIFO_CTRL_REG_FIFO;
40+
Write(FIFO_CTRL_REG, &data, 1);
41+
42+
// Set 200 Hz sample rate, enable all axes
43+
data = CTRL_REG1_ODR_200HZ | CTRL_REG1_X_EN | CTRL_REG1_Y_EN | CTRL_REG1_Z_EN;
44+
Write(CTRL_REG1, &data, 1);
45+
46+
isInitialized = true;
47+
}
48+
49+
AccelerationValues SC7A20::Process() {
50+
if (!isInitialized)
51+
return {};
52+
53+
// Read FIFO size, should be about 20 (200 Hz ODR / 10 Hz main loop)
54+
uint8_t length = 0;
55+
Read(FIFO_SRC_REG, &length, 1);
56+
length &= FIFO_SRC_REG_FSS_MASK;
57+
58+
// Read FIFO samples one by one (full read does not work)
59+
for (uint8_t i = 0; i < length; i++) {
60+
// Set the most significant bit of the sub-address field for block read
61+
Read(0x80 | OUT_X_L, (uint8_t*) &fifo[i], sizeof(int16_t) * 3);
62+
// Shift because value is left-justified
63+
for (uint8_t j = 0; j < 3; j++)
64+
fifo[i][j] >>= (16 - 12);
65+
// X and Y axis are swapped because of the way the sensor is mounted in the P8
66+
int16_t swap = fifo[i][0];
67+
fifo[i][0] = fifo[i][1];
68+
fifo[i][1] = swap;
69+
}
70+
71+
// Restart FIFO
72+
uint8_t data = FIFO_CTRL_REG_BYPASS;
73+
Write(FIFO_CTRL_REG, &data, 1);
74+
data = FIFO_CTRL_REG_FIFO;
75+
Write(FIFO_CTRL_REG, &data, 1);
76+
77+
// Compute averages of FIFO
78+
int16_t avgs[3] = {0};
79+
// 2g range in n bits
80+
for (uint8_t i = 0; i < length; i++)
81+
for (uint8_t j = 0; j < 3; j++) {
82+
avgs[j] += ((fifo[i][j] * 2000) / (1 << (12 - 1)));
83+
}
84+
for (uint8_t j = 0; j < 3; j++)
85+
avgs[j] /= length;
86+
87+
// Step counting is not implemented
88+
return {0, avgs[0], avgs[1], avgs[2], (int16_t*) fifo, length};
89+
}

src/drivers/SC7A20.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "drivers/AccelerationSensor.h"
4+
5+
namespace Pinetime {
6+
namespace Drivers {
7+
class SC7A20 : public AccelerationSensor {
8+
public:
9+
SC7A20(TwiMaster& twiMaster, uint8_t twiAddress);
10+
SC7A20(const SC7A20&) = delete;
11+
SC7A20& operator=(const SC7A20&) = delete;
12+
SC7A20(SC7A20&&) = delete;
13+
SC7A20& operator=(SC7A20&&) = delete;
14+
15+
void Init();
16+
AccelerationValues Process();
17+
};
18+
}
19+
}

0 commit comments

Comments
 (0)