forked from Klipper3d/klipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_adxl345.c
179 lines (165 loc) · 4.99 KB
/
sensor_adxl345.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Support for gathering acceleration data from ADXL345 chip
//
// Copyright (C) 2020 Kevin O'Connor <[email protected]>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // memcpy
#include "board/irq.h" // irq_disable
#include "board/misc.h" // timer_read_time
#include "basecmd.h" // oid_alloc
#include "command.h" // DECL_COMMAND
#include "sched.h" // DECL_TASK
#include "spicmds.h" // spidev_transfer
struct adxl345 {
struct timer timer;
uint32_t rest_ticks;
struct spidev_s *spi;
uint16_t sequence, limit_count;
uint8_t flags, data_count;
uint8_t data[48];
};
enum {
AX_HAVE_START = 1<<0, AX_RUNNING = 1<<1, AX_PENDING = 1<<2,
};
static struct task_wake adxl345_wake;
static uint_fast8_t
adxl345_event(struct timer *timer)
{
struct adxl345 *ax = container_of(timer, struct adxl345, timer);
ax->flags |= AX_PENDING;
sched_wake_task(&adxl345_wake);
return SF_DONE;
}
void
command_config_adxl345(uint32_t *args)
{
struct adxl345 *ax = oid_alloc(args[0], command_config_adxl345
, sizeof(*ax));
ax->timer.func = adxl345_event;
ax->spi = spidev_oid_lookup(args[1]);
}
DECL_COMMAND(command_config_adxl345, "config_adxl345 oid=%c spi_oid=%c");
// Report local measurement buffer
static void
adxl_report(struct adxl345 *ax, uint8_t oid)
{
sendf("adxl345_data oid=%c sequence=%hu data=%*s"
, oid, ax->sequence, ax->data_count, ax->data);
ax->data_count = 0;
ax->sequence++;
}
// Chip registers
#define AR_POWER_CTL 0x2D
#define AR_DATAX0 0x32
#define AR_FIFO_STATUS 0x39
#define AM_READ 0x80
#define AM_MULTI 0x40
// Query accelerometer data
static void
adxl_query(struct adxl345 *ax, uint8_t oid)
{
uint8_t msg[9] = { AR_DATAX0 | AM_READ | AM_MULTI, 0, 0, 0, 0, 0, 0, 0, 0 };
spidev_transfer(ax->spi, 1, sizeof(msg), msg);
memcpy(&ax->data[ax->data_count], &msg[1], 6);
ax->data_count += 6;
if (ax->data_count + 6 > ARRAY_SIZE(ax->data))
adxl_report(ax, oid);
uint_fast8_t fifo_status = msg[8] & ~0x80; // Ignore trigger bit
if (fifo_status >= 31 && ax->limit_count != 0xffff)
ax->limit_count++;
if (fifo_status > 1 && fifo_status <= 32) {
// More data in fifo - wake this task again
sched_wake_task(&adxl345_wake);
} else if (ax->flags & AX_RUNNING) {
// Sleep until next check time
sched_del_timer(&ax->timer);
ax->flags &= ~AX_PENDING;
irq_disable();
ax->timer.waketime = timer_read_time() + ax->rest_ticks;
sched_add_timer(&ax->timer);
irq_enable();
}
}
// Startup measurements
static void
adxl_start(struct adxl345 *ax, uint8_t oid)
{
sched_del_timer(&ax->timer);
ax->flags = AX_RUNNING;
uint8_t msg[2] = { AR_POWER_CTL, 0x08 };
uint32_t start1_time = timer_read_time();
spidev_transfer(ax->spi, 0, sizeof(msg), msg);
irq_disable();
uint32_t start2_time = timer_read_time();
ax->timer.waketime = start2_time + ax->rest_ticks;
sched_add_timer(&ax->timer);
irq_enable();
sendf("adxl345_start oid=%c start1_time=%u start2_time=%u"
, oid, start1_time, start2_time);
}
// End measurements
static void
adxl_stop(struct adxl345 *ax, uint8_t oid)
{
// Disable measurements
sched_del_timer(&ax->timer);
ax->flags = 0;
uint8_t msg[2] = { AR_POWER_CTL, 0x00 };
uint32_t end1_time = timer_read_time();
spidev_transfer(ax->spi, 0, sizeof(msg), msg);
uint32_t end2_time = timer_read_time();
// Drain any measurements still in fifo
uint_fast8_t i;
for (i=0; i<33; i++) {
msg[0] = AR_FIFO_STATUS | AM_READ;
msg[1] = 0;
spidev_transfer(ax->spi, 1, sizeof(msg), msg);
if (!(msg[1] & 0x3f))
break;
adxl_query(ax, oid);
}
// Report final data
if (ax->data_count)
adxl_report(ax, oid);
sendf("adxl345_end oid=%c end1_time=%u end2_time=%u"
" limit_count=%hu sequence=%hu"
, oid, end1_time, end2_time, ax->limit_count, ax->sequence);
}
void
command_query_adxl345(uint32_t *args)
{
struct adxl345 *ax = oid_lookup(args[0], command_config_adxl345);
if (!args[2]) {
// End measurements
adxl_stop(ax, args[0]);
return;
}
// Start new measurements query
sched_del_timer(&ax->timer);
ax->timer.waketime = args[1];
ax->rest_ticks = args[2];
ax->flags = AX_HAVE_START;
ax->sequence = ax->limit_count = 0;
ax->data_count = 0;
sched_add_timer(&ax->timer);
}
DECL_COMMAND(command_query_adxl345,
"query_adxl345 oid=%c clock=%u rest_ticks=%u");
void
adxl345_task(void)
{
if (!sched_check_wake(&adxl345_wake))
return;
uint8_t oid;
struct adxl345 *ax;
foreach_oid(oid, ax, command_config_adxl345) {
uint_fast8_t flags = ax->flags;
if (!(flags & AX_PENDING))
continue;
if (flags & AX_HAVE_START)
adxl_start(ax, oid);
else
adxl_query(ax, oid);
}
}
DECL_TASK(adxl345_task);