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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// Support for querying magnetic angle sensors via SPI
//
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include "basecmd.h" // oid_alloc
#include "board/misc.h" // timer_read_time
#include "board/gpio.h" // gpio_out_write
#include "board/irq.h" // irq_disable
#include "command.h" // DECL_COMMAND
#include "sched.h" // DECL_TASK
#include "sensor_bulk.h" // sensor_bulk_report
#include "spicmds.h" // spidev_transfer
enum {
SA_CHIP_A1333,
SA_CHIP_AS5047D,
SA_CHIP_TLE5012B,
SA_CHIP_MT6816,
SA_CHIP_MT6826S,
SA_CHIP_MAX
};
DECL_ENUMERATION("spi_angle_type", "a1333", SA_CHIP_A1333);
DECL_ENUMERATION("spi_angle_type", "as5047d", SA_CHIP_AS5047D);
DECL_ENUMERATION("spi_angle_type", "tle5012b", SA_CHIP_TLE5012B);
DECL_ENUMERATION("spi_angle_type", "mt6816", SA_CHIP_MT6816);
DECL_ENUMERATION("spi_angle_type", "mt6826s", SA_CHIP_MT6826S);
enum { TCODE_ERROR = 0xff };
enum {
SE_OVERFLOW, SE_SCHEDULE, SE_SPI_TIME, SE_CRC, SE_DUP, SE_NO_ANGLE
};
#define MAX_SPI_READ_TIME timer_from_us(50)
struct spi_angle {
struct timer timer;
uint32_t rest_ticks;
struct spidev_s *spi;
uint8_t flags, chip_type, time_shift, overflow;
struct sensor_bulk sb;
};
enum {
SA_PENDING = 1<<2,
};
#define BYTES_PER_SAMPLE 3
static struct task_wake angle_wake;
// Event handler that wakes spi_angle_task() periodically
static uint_fast8_t
angle_event(struct timer *timer)
{
struct spi_angle *sa = container_of(timer, struct spi_angle, timer);
uint8_t flags = sa->flags;
if (sa->flags & SA_PENDING)
sa->overflow++;
else
sa->flags = flags | SA_PENDING;
sched_wake_task(&angle_wake);
sa->timer.waketime += sa->rest_ticks;
return SF_RESCHEDULE;
}
void
command_config_spi_angle(uint32_t *args)
{
uint8_t chip_type = args[2];
if (chip_type > SA_CHIP_MAX)
shutdown("Invalid spi_angle chip type");
struct spi_angle *sa = oid_alloc(args[0], command_config_spi_angle
, sizeof(*sa));
sa->timer.func = angle_event;
sa->spi = spidev_oid_lookup(args[1]);
if (!spidev_have_cs_pin(sa->spi))
shutdown("angle sensor requires cs pin");
sa->chip_type = chip_type;
}
DECL_COMMAND(command_config_spi_angle,
"config_spi_angle oid=%c spi_oid=%c spi_angle_type=%c");
// Send spi_angle_data message if buffer is full
static void
angle_check_report(struct spi_angle *sa, uint8_t oid)
{
if (sa->sb.data_count + BYTES_PER_SAMPLE > ARRAY_SIZE(sa->sb.data))
sensor_bulk_report(&sa->sb, oid);
}
// Add an entry to the measurement buffer
static void
angle_add(struct spi_angle *sa, uint_fast8_t tcode, uint_fast16_t data)
{
sa->sb.data[sa->sb.data_count] = tcode;
sa->sb.data[sa->sb.data_count + 1] = data;
sa->sb.data[sa->sb.data_count + 2] = data >> 8;
sa->sb.data_count += BYTES_PER_SAMPLE;
}
// Add an error indicator to the measurement buffer
static void
angle_add_error(struct spi_angle *sa, uint_fast8_t error_code)
{
angle_add(sa, TCODE_ERROR, error_code);
}
// Add a measurement to the buffer
static void
angle_add_data(struct spi_angle *sa, uint32_t stime, uint32_t mtime
, uint_fast16_t angle)
{
uint32_t tdiff = mtime - stime;
if (sa->time_shift)
tdiff = (tdiff + (1<<(sa->time_shift - 1))) >> sa->time_shift;
if (tdiff >= TCODE_ERROR) {
angle_add_error(sa, SE_SCHEDULE);
return;
}
angle_add(sa, tdiff, angle);
}
// a1333 sensor query
static void
a1333_query(struct spi_angle *sa, uint32_t stime)
{
uint8_t msg[2] = { 0x32, 0x00 };
uint32_t mtime1 = timer_read_time();
spidev_transfer(sa->spi, 1, sizeof(msg), msg);
uint32_t mtime2 = timer_read_time();
// Data is latched on first sclk edge of response
if (mtime2 - mtime1 > MAX_SPI_READ_TIME)
angle_add_error(sa, SE_SPI_TIME);
else if (msg[0] & 0x80)
angle_add_error(sa, SE_CRC);
else
angle_add_data(sa, stime, mtime1, (msg[0] << 9) | (msg[1] << 1));
}
static int bit_parity(uint8_t *msg)
{
uint_fast8_t parity = msg[0] ^ msg[1];
parity ^= parity >> 4;
parity ^= parity >> 2;
parity ^= parity >> 1;
return parity;
}
// as5047d sensor query
static void
as5047d_query(struct spi_angle *sa, uint32_t stime)
{
uint8_t msg[2] = { 0x7F, 0xFE };
uint32_t mtime1 = timer_read_time();
spidev_transfer(sa->spi, 0, sizeof(msg), msg);
uint32_t mtime2 = timer_read_time();
// Data is latched on CS pin rising after query request
if (mtime2 - mtime1 > MAX_SPI_READ_TIME) {
angle_add_error(sa, SE_SPI_TIME);
return;
}
msg[0] = 0xC0;
msg[1] = 0x00;
spidev_transfer(sa->spi, 1, sizeof(msg), msg);
uint_fast8_t parity = bit_parity(msg);
if (parity & 1)
angle_add_error(sa, SE_CRC);
else if (msg[0] & 0x40)
angle_add_error(sa, SE_NO_ANGLE);
else
angle_add_data(sa, stime, mtime2, (msg[0] << 10) | (msg[1] << 2));
}
static void mt6816_query(struct spi_angle *sa, uint32_t stime)
{
uint8_t msg[3] = {0x83, 0x00, 0x00};
uint32_t mtime1 = timer_read_time();
spidev_transfer(sa->spi, 1, sizeof(msg), msg);
uint32_t mtime2 = timer_read_time();
// Data is latched on first sclk edge of response
if (mtime2 - mtime1 > MAX_SPI_READ_TIME) {
angle_add_error(sa, SE_SPI_TIME);
return;
}
uint_fast8_t parity = bit_parity(&msg[1]);
if (parity & 1)
angle_add_error(sa, SE_CRC);
else if (msg[2] & 0x02)
angle_add_error(sa, SE_NO_ANGLE);
else
angle_add_data(sa, stime, mtime2, (msg[1] << 8) | (msg[2] & 0xfc));
}
static uint8_t
crc8_mt(uint8_t crc, uint8_t data)
{
crc ^= data;
int i;
for (i = 0; i < 8; i++)
crc = crc & 0x80 ? (crc << 1) ^ 0x07 : crc << 1;
return crc;
}
static void mt6826s_query(struct spi_angle *sa, uint32_t stime)
{
uint8_t msg[6] = {0x30, 0x03, 0x00, 0x00, 0x00, 0x00};
uint32_t mtime1 = timer_read_time();
spidev_transfer(sa->spi, 1, sizeof(msg), msg);
uint32_t mtime2 = timer_read_time();
// Data is latched on first sclk edge of response
if (mtime2 - mtime1 > MAX_SPI_READ_TIME) {
angle_add_error(sa, SE_SPI_TIME);
return;
}
uint8_t crc = 0;
for (int i = 2; i < 5; i++)
crc = crc8_mt(crc, msg[i]);
if (crc != msg[5])
angle_add_error(sa, SE_CRC);
else if (msg[4] & 0x02)
angle_add_error(sa, SE_NO_ANGLE);
else
angle_add_data(sa, stime, mtime2, (msg[2] << 8) | msg[3]);
}
#define TLE_READ 0x80
#define TLE_READ_LATCH (TLE_READ | 0x04)
#define TLE_REG_AVAL 0x02
// crc8 "J1850" calculation for tle5012b messages
static uint8_t
crc8(uint8_t crc, uint8_t data)
{
crc ^= data;
int i;
for (i=0; i<8; i++)
crc = crc & 0x80 ? (crc << 1) ^ 0x1d : crc << 1;
return crc;
}
// microsecond delay helper
static inline void
udelay(uint32_t usecs)
{
uint32_t end = timer_read_time() + timer_from_us(usecs);
while (!timer_is_before(end, timer_read_time()))
irq_poll();
}
// tle5012b sensor query
static void
tle5012b_query(struct spi_angle *sa, uint32_t stime)
{
struct gpio_out cs_pin = spidev_get_cs_pin(sa->spi);
// Latch data (data is latched on rising CS of a NULL message)
gpio_out_write(cs_pin, 0);
udelay(1);
irq_disable();
gpio_out_write(cs_pin, 1);
uint32_t mtime = timer_read_time();
irq_enable();
uint8_t msg[10] = { TLE_READ_LATCH, (TLE_REG_AVAL << 4) | 0x03 };
uint8_t crc = 0x05; // 0x05 == crc8(crc8(0xff, msg[0]), msg[1])
spidev_transfer(sa->spi, 1, sizeof(msg), msg);
int i;
for (i=2; i<8; i++)
crc = crc8(crc, msg[i]);
if (((~crc) & 0xff) != msg[9])
angle_add_error(sa, SE_CRC);
else if (!(msg[8] & (1<<4)))
angle_add_error(sa, SE_NO_ANGLE);
else if (!(msg[2] & 0x80))
angle_add_error(sa, SE_DUP);
else if (mtime - stime > timer_from_us(32 * 32 * 1000000UL / 750000))
angle_add_error(sa, SE_SCHEDULE);
else
angle_add(sa, (msg[6] >> 1) & 0x3f, (msg[2] << 9) | (msg[3] << 1));
}
void
command_query_spi_angle(uint32_t *args)
{
uint8_t oid = args[0];
struct spi_angle *sa = oid_lookup(oid, command_config_spi_angle);
sched_del_timer(&sa->timer);
sa->flags = 0;
if (!args[2])
// End measurements
return;
// Start new measurements query
sa->timer.waketime = args[1];
sa->rest_ticks = args[2];
sensor_bulk_reset(&sa->sb);
sa->time_shift = args[3];
sched_add_timer(&sa->timer);
}
DECL_COMMAND(command_query_spi_angle,
"query_spi_angle oid=%c clock=%u rest_ticks=%u time_shift=%c");
void
command_spi_angle_transfer(uint32_t *args)
{
uint8_t oid = args[0];
struct spi_angle *sa = oid_lookup(oid, command_config_spi_angle);
uint8_t data_len = args[1];
uint8_t *data = command_decode_ptr(args[2]);
uint32_t mtime;
uint_fast8_t chip = sa->chip_type;
if (chip == SA_CHIP_TLE5012B) {
// Latch data (data is latched on rising CS of a NULL message)
struct gpio_out cs_pin = spidev_get_cs_pin(sa->spi);
gpio_out_write(cs_pin, 0);
udelay(1);
irq_disable();
gpio_out_write(cs_pin, 1);
mtime = timer_read_time();
irq_enable();
spidev_transfer(sa->spi, 1, data_len, data);
} else {
uint32_t mtime1 = timer_read_time();
spidev_transfer(sa->spi, 1, data_len, data);
uint32_t mtime2 = timer_read_time();
if (mtime2 - mtime1 > MAX_SPI_READ_TIME)
data_len = 0;
if (chip == SA_CHIP_AS5047D)
mtime = mtime2;
else
mtime = mtime1;
}
sendf("spi_angle_transfer_response oid=%c clock=%u response=%*s"
, oid, mtime, data_len, data);
}
DECL_COMMAND(command_spi_angle_transfer, "spi_angle_transfer oid=%c data=%*s");
// Background task that performs measurements
void
spi_angle_task(void)
{
if (!sched_check_wake(&angle_wake))
return;
uint8_t oid;
struct spi_angle *sa;
foreach_oid(oid, sa, command_config_spi_angle) {
uint_fast8_t flags = sa->flags;
if (!(flags & SA_PENDING))
continue;
irq_disable();
uint32_t stime = sa->timer.waketime;
uint_fast8_t overflow = sa->overflow;
sa->flags = 0;
sa->overflow = 0;
irq_enable();
stime -= sa->rest_ticks;
while (overflow--) {
angle_add_error(sa, SE_OVERFLOW);
angle_check_report(sa, oid);
}
uint_fast8_t chip = sa->chip_type;
if (chip == SA_CHIP_A1333)
a1333_query(sa, stime);
else if (chip == SA_CHIP_AS5047D)
as5047d_query(sa, stime);
else if (chip == SA_CHIP_TLE5012B)
tle5012b_query(sa, stime);
else if (chip == SA_CHIP_MT6816)
mt6816_query(sa, stime);
else if (chip == SA_CHIP_MT6826S)
mt6826s_query(sa, stime);
angle_check_report(sa, oid);
}
}
DECL_TASK(spi_angle_task);
|