forked from cinderblock/3-Phase-Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLX90363.h
More file actions
224 lines (181 loc) · 5.34 KB
/
MLX90363.h
File metadata and controls
224 lines (181 loc) · 5.34 KB
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
/*
* File: MLX90363.h
* Author: Cameron
*
* Created on December 10, 2014, 4:11 PM
*/
#ifndef MLX90363_H
#define MLX90363_H
#include <avr/interrupt.h>
#include <AVR++/bitTypes.h>
#include <AVR++/IOpin.h>
#include "Board.h"
#include "Clock.h"
ISR(SPI_STC_vect);
class MLX90363 {
friend void SPI_STC_vect();
static inline void isr();
/**
* The fixed message length that the MLX90363 sends
*/
static constexpr u1 messageLength = 8;
/**
* Staged transmit buffer. Will be sent automatically by the interrupt routine
* if properly started and left alone so that repeat messages are trivial.
*/
static u1 TxBuffer[messageLength];
/**
* The buffer for the incoming/received message
*/
static u1 RxBuffer[messageLength];
/**
* Where we are while sending/reading the data in the SPI interrupt
*/
static u1 bufferPosition;
static ::Clock::MicroTime dataReadyTime;
enum class ResponseState : u1;
static ResponseState responseState;
volatile static u2 alpha;
volatile static u2 beta;
volatile static u2 X;
volatile static u2 Y;
volatile static u2 Z;
volatile static u1 err;
volatile static u1 VG;
volatile static u1 ROLL;
/**
* INTERNAL: Reset the buffer position and start the transmission sequence.
*
* Unsafe because it does not check if there is already a transmission running.
*/
static void startTransmittingUnsafe();
/**
* Calculate and write the correct CRC for the message in and to the TxBuffer
*/
static void fillTxBufferCRC();
/**
* Check the checksum of the data in the RxBuffer
* @return
*/
static bool checkRxBufferCRC();
/**
* The slave select line
*/
static constexpr IOpin &SS = Board::MagSel;
static constexpr IOpin &MISO = Board::SPI::MISO;
static constexpr IOpin &MOSI = Board::SPI::MOSI;
static constexpr IOpin &SCLK = Board::SPI::SCLK;
/**
* OpCodes from the MLX90363 datasheet.
*
*/
enum class Opcode: b6 {
// Following the format from the datasheet, they organized all the opcodes as
// Outgoing or Incoming
GET1 = 0x13,
GET2 = 0x14,
GET3 = 0x15, Get3Ready = 0x2D,
MemoryRead = 0x01, MemoryRead_Answer = 0x02,
EEPROMWrite = 0x03, EEPROMWrite_Challenge = 0x04,
EEChallengeAns = 0x05, EEReadAnswer = 0x28,
EEReadChallenge = 0x0F, EEPROMWrite_Status = 0x0E,
NOP__Challenge = 0x10, Challenge__NOP_MISO_Packet = 0x11,
DiagnosticDetails = 0x16, Diagnostics_Answer = 0x17,
OscCounterStart = 0x18, OscCounterStart_Acknowledge = 0x19,
OscCounterStop = 0x1A, OscCounterStopAck_CounterValue = 0x1B,
Reboot = 0x2F,
Standby = 0x31, StandbyAck = 0x32,
Error_frame = 0x3D,
NothingToTransmit = 0x3E,
Ready_Message = 0x2C,
};
/**
* Handle a standard Alpha response from the MLX90363
*/
static void handleAlpha();
/**
* Handle a standard AlphaBeta response from the MLX90363
*/
static void handleAlphaBeta();
/**
* Handle a standard XYZ response from the MLX90363
*/
static void handleXYZ();
public:
/**
* MLX requires a time between data checks.
* This function returns true when the required time has passed
* as well as not currently transmitting
*/
static bool isMeasurementReady();
enum class ResponseState: u1 {
Init, Ready, Receiving, Received, failedCRC, TypeA, TypeAB, TypeXYZ, Other
};
/**
* The 2-bit marker attached to all incoming messages for easy processing
*/
enum class MessageType: b2 {
Alpha = 0, AlphaBeta = 1, XYZ = 2, Other = 3
};
/**
* Initialize the hardware.
*
* One thing that is not done is to set the AVR's hardware SS line to an output,
* preventing the hardware from automatically switching to slave mode. Make sure
* that it is set to an output (and stays that way) before calling this function.
*/
static void init();
static inline bool hasNewData(u1& lastRoll) {
u1 const r = ROLL;
if (r == lastRoll)
return false;
lastRoll = r;
return true;
}
/**
* Set the SPI hardware's divider
* @param
*/
static void setSPISpeed(u1 const);
/**
* Check if we're still talking on the SPI bus
* @return
*/
inline static bool isTransmitting() {
// Any of these would work. Not sure which is most effective
//return bufferPosition != messageLength;
//return !SS.isHigh();
// Let's use the low level test from Board::
return Board::SPI::isSlaveSelected();
}
/**
* Start sending whatever is in the buffer, unless it's already being sent
*
*/
static void startTransmitting();
static b6 getReceivedOpCode();
/**
* Handle a received message.
*
* Checks the CRC
* Checks for standard marker
* and passes the message off to another internal handler
*/
static void handleResponse();
// inline static u2 getAlpha() {return (u2 const) alpha;}
inline static u2 getAlpha() {cli(); u2 const a = alpha; sei(); return a;}
inline static u2 getBeta() {return beta;}
inline static u2 getX() {return X;}
inline static u2 getY() {return Y;}
inline static u2 getZ() {return Z;}
inline static u1 getRoll() {return ROLL;}
inline static u1 getErr() {return err;}
/**
* Construct a standard GET1 message
* @param type
* @param timeout
* @param resetRoll
*/
static void prepareGET1Message(MessageType const type, const u2 timeout = 0xffff, bool const resetRoll = false);
};
#endif /* MLX90363_H */