Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
AS5145.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "sys/spi.h"
8#include "utils/bitstream.h"
9
11template<int chainlength>
13 static constexpr uint8_t BITS = 19; // number of bits in sensor data struct
14 static constexpr uint8_t TOTALBITS = BITS*chainlength; // number of bits in sensor data struct
15 // total bytes required for chain
16 static constexpr uint8_t BYTES = (TOTALBITS+7) / 8;
17 // sensor data struct. lsb to msb order.
18 struct sensor {
19 uint8_t EVEN:1;
20 uint8_t DEC:1;
21 uint8_t INC:1;
22 uint8_t LIN:1;
23 uint8_t COF:1;
24 uint8_t OCF:1;
25 int16_t POS:12;
26 uint8_t :1;
27 } chain[chainlength];
28
35 : SPI::Device(bus, cs, {SPI::Mode::M2, SPI::FirstBit::MSB}) { }
36
41 int16_t getVal(int i = 0) {
42 return chain[i].POS;
43 }
44
48 void sense() {
49 bus.trypush({
50 .dev = this,
51 .data = BYTES,
52 .dir = SPI::Request::MISO,
53 });
54 }
55
56 /*
57 * callback when data is successfully measured
58 *
59 * copy data from buffer into struct.
60 */
61 void callback(const SPI::Request rq) override {
62 BitStream bs{rq.data.buf};
63 for (int i = 0, n=0; i < chainlength; ++i, n+=BITS) {
64 chain[i].COF = bs.range<decltype(sensor::COF)>(n + 14, n + 15);
65 if (chain[i].COF) { // out of range error
66 continue;
67 }
68 chain[i].POS = bs.range<decltype(sensor::POS)>(n + 1, n + 13);
69 }
70 }
71};
Copyright (c) 2023 IACE.
Wrapper class for simple digital input/output pin.
Definition gpio.h:13
Copyright (c) 2023 IACE.
Class describing a chain of AS5145 Hall sensors.
Definition AS5145.h:12
void sense()
request measurement of sensor data
Definition AS5145.h:48
void callback(const SPI::Request rq) override
is called as soon as requested data arrived over wire
Definition AS5145.h:61
AS5145(Sink< SPI::Request > &bus, DIO cs)
Constructor.
Definition AS5145.h:34
int16_t getVal(int i=0)
Definition AS5145.h:41
Wrapper for extracting bitwise data from a stream of bytes.
Definition bitstream.h:12
generic SPI device wrapper.
Definition spi.h:22
DIO cs
GPIO chipselect line.
Definition spi.h:25
struct defining an SPI request.
Definition spi.h:54
Buffer< uint8_t > data
transfer data
Definition spi.h:56
@ MISO
Master in, Slave out.
Definition spi.h:59
generic object sink, i.e.
Definition streams.h:15
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28