Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TMAG5273Demo.hxx
2#include "os/OS.hxx"
3
4#include <endian.h>
5
6class MagSensorTest : public OSThread
7{
8public:
10 {
11 start("mag_sensor", 0, 2048);
12 }
13
14 void *entry() override
15 {
16 sen_.init("/dev/i2c0");
17 // Enables all three mag channels, and sleeps 10 msec between
18 // conversions.
19 sen_.register_write(TMAG5273::SENSOR_CONFIG_1, 0x72);
20 // Operating mode = sleep-and-wake.
21 sen_.register_write(TMAG5273::DEVICE_CONFIG_2, 0x3);
22 // Clear error bits
23 sen_.register_write(TMAG5273::DEVICE_STATUS, 0xF);
24 // Int config to disable the nINT pin
25 sen_.register_write(TMAG5273::INT_CONFIG_1, 0x1);
26 // Clears POR bit
27 sen_.register_write(TMAG5273::CONV_STATUS, 0x0);
28 // Sets lower gain / higher range
29 //sen_.register_write(TMAG5273::SENSOR_CONFIG_2, 0b11);
30
31 sen_.set_offset_1(-157/2/16); //-5);
32 sen_.set_offset_2((110+330)/2/16); //-128);
33
34 // Average 32 samples together.
36 // Enable angle sensor on x-z axis
38
39 // 20 / 32 gain config, Z channel is higher.
40 //sen_.set_angle_gain(160, true);
41
42 while (true)
43 {
44 uint8_t rc[3];
45 sen_.register_read(TMAG5273::DEVICE_ID, rc, 3);
46 uint8_t status = 0;
47 sen_.register_read(TMAG5273::DEVICE_STATUS, &status, 1);
48 uint8_t conv_status = 0;
49 sen_.register_read(TMAG5273::CONV_STATUS, &conv_status, 1);
50 LOG(ALWAYS,
51 "hello %02x %02x %02x, status=0x%02x convst=0x%02x",
52 rc[0], rc[1], rc[2], status, conv_status);
53 int16_t results[4];
54 sen_.register_read(TMAG5273::X_MSB_RESULT, (uint8_t *)results, 6);
55 sen_.register_read(
56 TMAG5273::ANGLE_RESULT_MSB, (uint8_t *)&results[3], 2);
57 results[0] = be16toh(results[0]);
58 results[1] = be16toh(results[1]);
59 results[2] = be16toh(results[2]);
60 results[3] = be16toh(results[3]);
61 int frac = (results[3] & 0xf) * 100 / 16;
62 LOG(ALWAYS, "x=%6d y=%6d z=%6d angle=%d.%02d", results[0],
63 results[1], results[2], results[3] >> 4, frac);
64 // Clear error bits and trigger a new conversion
65 sen_.register_write(TMAG5273::DEVICE_STATUS | 0x80, 0xF);
66 microsleep(200000);
67 }
68 }
69
70 TMAG5273 sen_;
71};
void * entry() override
User entry point for the created thread.
This class provides a threading API.
Definition OS.hxx:46
void start(const char *name, int priority, size_t stack_size)
Starts the thread.
Definition OS.hxx:78
Implementation of TMAG5273 sensor driver.
Definition TMAG5273.hxx:50
@ AVG_32
average 32 saples
void register_write(uint8_t reg, const uint8_t *data, uint16_t len)
Writes one or more (sequential) register.
Definition TMAG5273.hxx:478
void set_offset_2(int8_t offset)
Sets offset correction for the second axis.
Definition TMAG5273.hxx:363
void set_oversampling(ConversionAverage avg)
Sets the oversampling+averaging mode.
Definition TMAG5273.hxx:321
void set_offset_1(int8_t offset)
Sets offset correction for the first axis.
Definition TMAG5273.hxx:353
@ XZ_ENABLE
X + Z derived angle.
int register_read(uint8_t reg, uint8_t *data, uint16_t len)
Reads one or more (sequential) registers.
Definition TMAG5273.hxx:461
void set_angle_en(AngleEnable angle)
Sets whether angle measurement should be enabled.
Definition TMAG5273.hxx:329
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int ALWAYS
Loglevel that is always printed.
Definition logging.h:49
static void microsleep(uint32_t microseconds)
Sleep a given number of microseconds.