libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
tlv493d.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_TLV493D_H
3#define DSY_TLV493D_H
4
5#define TLV493D_DEFAULTMODE POWERDOWNMODE
6
7#define TLV493D_ADDRESS1 0x5E
8#define TLV493D_ADDRESS2 0x1F
9#define TLV493D_BUSIF_READSIZE 10
10#define TLV493D_BUSIF_WRITESIZE 4
11#define TLV493D_NUM_OF_REGMASKS 25
12#define TLV493D_DEFAULTMODE POWERDOWNMODE
13#define TLV493D_MEASUREMENT_READOUT 7
14
15#define TLV493D_B_MULT 0.098f
16#define TLV493D_TEMP_MULT 1.1
17#define TLV493D_TEMP_OFFSET 315
18
19#define REGMASK_READ 0
20#define REGMASK_WRITE 1
21
22namespace daisy
23{
30{
31 public:
34
55
57 {
58 config_ = config;
59
60 I2CHandle::Config i2c_config;
62 i2c_config.periph = config_.periph;
63 i2c_config.speed = config_.speed;
64
65 i2c_config.pin_config.scl = config_.scl;
66 i2c_config.pin_config.sda = config_.sda;
67
68 err_ = false;
69
70 err_ |= I2CHandle::Result::OK != i2c_.Init(i2c_config);
71 }
72
73 // used for the weird reset sequence. This ends in an error but that's OK
75 {
76 i2c_.TransmitBlocking(add, data, size, 10);
77 }
78
80 {
82 != i2c_.TransmitBlocking(config_.address, data, size, 10);
83 }
84
86 {
88 != i2c_.ReceiveBlocking(config_.address, data, size, 10);
89 }
90
91 bool GetError()
92 {
93 bool tmp = err_;
94 err_ = false;
95
96 return tmp;
97 }
98
99 uint8_t GetAddress() { return config_.address; }
100
101 private:
102 I2CHandle i2c_;
103 Config config_;
104 bool err_;
105};
106
107
112template <typename Transport>
114{
115 public:
118
147
155
163
172
174 {0, 0, 0, 1000}, // POWERDOWNMODE
175 {1, 0, 0, 0}, // FASTMODE
176 {0, 1, 1, 10}, // LOWPOWERMODE
177 {0, 1, 0, 100}, // ULTRALOWPOWERMODE
178 {1, 1, 1, 10} // MASTERCONTROLLEDMODE
179 };
180
181 const RegMask_t RegMasks[25] = {
182 {REGMASK_READ, 0, 0xFF, 0}, // R_BX1
183 {REGMASK_READ, 4, 0xF0, 4}, // R_BX2
184 {REGMASK_READ, 1, 0xFF, 0}, // R_BY1
185 {REGMASK_READ, 4, 0x0F, 0}, // R_BY2
186 {REGMASK_READ, 2, 0xFF, 0}, // R_BZ1
187 {REGMASK_READ, 5, 0x0F, 0}, // R_BZ2
188 {REGMASK_READ, 3, 0xF0, 4}, // R_TEMP1
189 {REGMASK_READ, 6, 0xFF, 0}, // R_TEMP2
190 {REGMASK_READ, 3, 0x0C, 2}, // R_FRAMECOUNTER
191 {REGMASK_READ, 3, 0x03, 0}, // R_CHANNEL
192 {REGMASK_READ, 5, 0x10, 4}, // R_POWERDOWNFLAG
193 {REGMASK_READ, 7, 0x18, 3}, // R_RES1
194 {REGMASK_READ, 8, 0xFF, 0}, // R_RES2
195 {REGMASK_READ, 9, 0x1F, 0}, // R_RES3
196 {REGMASK_WRITE, 1, 0x80, 7}, // W_PARITY
197 {REGMASK_WRITE, 1, 0x60, 5}, // W_ADDR
198 {REGMASK_WRITE, 1, 0x04, 2}, // W_INT
199 {REGMASK_WRITE, 1, 0x02, 1}, // W_FAST
200 {REGMASK_WRITE, 1, 0x01, 0}, // W_LOWPOWER
201 {REGMASK_WRITE, 3, 0x80, 7}, // W_TEMP_EN
202 {REGMASK_WRITE, 3, 0x40, 6}, // W_LOWPOWER
203 {REGMASK_WRITE, 3, 0x20, 5}, // W_POWERDOWN
204 {REGMASK_WRITE, 1, 0x18, 3}, // W_RES1
205 {REGMASK_WRITE, 2, 0xFF, 0}, // W_RES2
206 {REGMASK_WRITE, 3, 0x1F, 0} // W_RES3
207 };
208
209 struct Config
210 {
211 typename Transport::Config transport_config;
213 };
214
216 {
217 OK = 0,
218 ERR
219 };
220
225 {
226 config_ = config;
227 System::Delay(40); // 40ms startup delay
228
229 transport_.Init(config_.transport_config);
230
231 ResetSensor();
232
233 // get all register data from sensor
234 ReadOut();
235 // copy factory settings to write registers
239 // enable parity detection
241 // config sensor to lowpower mode
242 // also contains parity calculation and writeout to sensor
244
245 prev_sample_period_ = System::GetNow();
246
247 return GetTransportErr();
248 }
249
250 void ReadOut() { transport_.Read(regReadData, TLV493D_BUSIF_READSIZE); }
251
252 void WriteOut() { transport_.Write(regWriteData, TLV493D_BUSIF_WRITESIZE); }
253
255 {
257 {
258 SetToRegs(&RegMasks[regMaskIndex], regWriteData, data);
259 }
260 }
261
263 {
265 {
266 const RegMask_t *mask = &(RegMasks[regMaskIndex]);
267 if(mask->rw == REGMASK_READ)
268 {
269 return GetFromRegs(mask, regReadData);
270 }
271 else
272 {
273 return GetFromRegs(mask, regWriteData);
274 }
275 }
276 return 0;
277 }
278
279
281 {
283 if(now - prev_sample_period_ >= GetMeasurementDelay())
284 {
285 prev_sample_period_ = now;
286
287 ReadOut();
288
289 // construct results from registers
290 mXdata = ConcatResults(GetRegBits(R_BX1), GetRegBits(R_BX2), true);
291 mYdata = ConcatResults(GetRegBits(R_BY1), GetRegBits(R_BY2), true);
292 mZdata = ConcatResults(GetRegBits(R_BZ1), GetRegBits(R_BZ2), true);
293 mTempdata = ConcatResults(
295
296 // SetAccessMode(POWERDOWNMODE);
298
299 mExpectedFrameCount = GetRegBits(R_FRAMECOUNTER) + 1;
300 }
301 }
302
303 void SetInterrupt(bool enable)
304 {
305 SetRegBits(W_INT, enable);
306 CalcParity();
307 WriteOut();
308 }
309
310 void EnableTemp(bool enable)
311 {
312 SetRegBits(W_TEMP_NEN, enable);
313 CalcParity();
314 WriteOut();
315 }
316
317 float GetX() { return static_cast<float>(mXdata) * TLV493D_B_MULT; }
318
319
320 float GetY() { return static_cast<float>(mYdata) * TLV493D_B_MULT; }
321
322
323 float GetZ() { return static_cast<float>(mZdata) * TLV493D_B_MULT; }
324
325
326 float GetTemp()
327 {
328 return static_cast<float>(mTempdata - TLV493D_TEMP_OFFSET)
330 }
331
332
333 float GetAmount()
334 {
335 // sqrt(x^2 + y^2 + z^2)
336 return TLV493D_B_MULT
337 * sqrt(pow(static_cast<float>(mXdata), 2)
338 + pow(static_cast<float>(mYdata), 2)
339 + pow(static_cast<float>(mZdata), 2));
340 }
341
342
344 {
345 // arctan(y/x)
346 return atan2(static_cast<float>(mYdata), static_cast<float>(mXdata));
347 }
348
349
350 float GetPolar()
351 {
352 // arctan(z/(sqrt(x^2+y^2)))
353 return atan2(static_cast<float>(mZdata),
354 sqrt(pow(static_cast<float>(mXdata), 2)
355 + pow(static_cast<float>(mYdata), 2)));
356 }
357
358 uint16_t GetMeasurementDelay() { return accModes[mMode].measurementTime; }
359
361 {
362 const AccessMode_t *modeConfig = &(accModes[mode]);
366 CalcParity();
367 WriteOut();
368 mMode = mode;
369 }
370
372 {
373 uint8_t i;
374 uint8_t y = 0x00;
375 // set parity bit to 1
376 // algorithm will calculate an even parity and replace this bit,
377 // so parity becomes odd
379 // combine array to one byte first
380 for(i = 0; i < TLV493D_BUSIF_WRITESIZE; i++)
381 {
382 y ^= regWriteData[i];
383 }
384 // combine all bits of this byte
385 y = y ^ (y >> 1);
386 y = y ^ (y >> 2);
387 y = y ^ (y >> 4);
388 // parity is in the LSB of y
389 SetRegBits(W_PARITY, y & 0x01);
390 }
391
393 {
394 //16-bit signed integer for 12-bit values of sensor
395 int16_t value = 0x0000;
396 if(upperFull)
397 {
398 value = upperByte << 8;
399 value |= (lowerByte & 0x0F) << 4;
400 }
401 else
402 {
403 value = (upperByte & 0x0F) << 12;
404 value |= lowerByte << 4;
405 }
406 value >>= 4; //shift left so that value is a signed 12 bit integer
407 return value;
408 }
409
410 private:
411 Config config_;
412 Transport transport_;
413 uint8_t regReadData[TLV493D_BUSIF_READSIZE];
414 uint8_t regWriteData[TLV493D_BUSIF_WRITESIZE];
415 int16_t mXdata, mYdata, mZdata, mTempdata, mExpectedFrameCount, mMode;
416 uint32_t prev_sample_period_;
417
419 Result GetTransportErr()
420 {
421 Result ret = transport_.GetError() ? ERR : OK;
422 return ret;
423 }
424
425 // internal function called by begin()
426 void ResetSensor()
427 {
428 uint8_t data;
429 if(transport_.GetAddress() == TLV493D_ADDRESS1)
430 {
431 // if the sensor shall be initialized with i2c address 0x1F
432 data = 0xFF;
433 }
434 else
435 {
436 // if the sensor shall be initialized with address 0x5E
437 data = 0x00;
438 }
439
440 // Write data to slave add 0x00
441 transport_.WriteAddress(0x00, &data, 1);
442 }
443
444 uint8_t GetFromRegs(const RegMask_t *mask, uint8_t *regData)
445 {
446 return (regData[mask->byteAdress] & mask->bitMask) >> mask->shift;
447 }
448
449
450 void SetToRegs(const RegMask_t *mask, uint8_t *regData, uint8_t toWrite)
451 {
452 if(mask->rw == REGMASK_WRITE)
453 {
454 uint8_t regValue = regData[mask->byteAdress];
455 regValue &= ~(mask->bitMask);
456 regValue |= (toWrite << mask->shift) & mask->bitMask;
457 regData[mask->byteAdress] = regValue;
458 }
459 }
460};
461
465} // namespace daisy
466#endif
Definition i2c.h:26
Result Init(const Config &config)
Result ReceiveBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Result TransmitBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Definition leddriver.h:33
void Init(I2CHandle i2c, const uint8_t(&addresses)[numDrivers], DmaBuffer dma_buffer_a, DmaBuffer dma_buffer_b, Pin oe_pin=Pin(PORTX, 0))
Definition leddriver.h:65
static uint32_t GetNow()
static void Delay(uint32_t delay_ms)
Device support for TLV493D Magnetic Sensor.
Definition tlv493d.h:114
float GetY()
Definition tlv493d.h:320
void SetInterrupt(bool enable)
Definition tlv493d.h:303
float GetX()
Definition tlv493d.h:317
void UpdateData()
Definition tlv493d.h:280
void CalcParity()
Definition tlv493d.h:371
Result
Definition tlv493d.h:216
@ ERR
Definition tlv493d.h:218
@ OK
Definition tlv493d.h:217
Tlv493d()
Definition tlv493d.h:116
float GetTemp()
Definition tlv493d.h:326
float GetZ()
Definition tlv493d.h:323
AccessMode_e
Definition tlv493d.h:165
@ MASTERCONTROLLEDMODE
Definition tlv493d.h:170
@ ULTRALOWPOWERMODE
Definition tlv493d.h:169
@ POWERDOWNMODE
Definition tlv493d.h:166
@ FASTMODE
Definition tlv493d.h:167
@ LOWPOWERMODE
Definition tlv493d.h:168
void SetRegBits(uint8_t regMaskIndex, uint8_t data)
Definition tlv493d.h:254
const RegMask_t RegMasks[25]
Definition tlv493d.h:181
float GetAmount()
Definition tlv493d.h:333
uint8_t GetRegBits(uint8_t regMaskIndex)
Definition tlv493d.h:262
float GetPolar()
Definition tlv493d.h:350
void EnableTemp(bool enable)
Definition tlv493d.h:310
uint16_t GetMeasurementDelay()
Definition tlv493d.h:358
float GetAzimuth()
Definition tlv493d.h:343
Result Init(Config config)
Definition tlv493d.h:224
const AccessMode_t accModes[5]
Definition tlv493d.h:173
int16_t ConcatResults(uint8_t upperByte, uint8_t lowerByte, bool upperFull)
Definition tlv493d.h:392
void ReadOut()
Definition tlv493d.h:250
void WriteOut()
Definition tlv493d.h:252
Registers_e
Definition tlv493d.h:120
@ W_INT
Definition tlv493d.h:137
@ W_LOWPOWER
Definition tlv493d.h:139
@ R_BX1
Definition tlv493d.h:121
@ R_RES2
Definition tlv493d.h:133
@ R_BZ2
Definition tlv493d.h:126
@ R_CHANNEL
Definition tlv493d.h:130
@ W_PARITY_EN
Definition tlv493d.h:142
@ R_POWERDOWNFLAG
Definition tlv493d.h:131
@ W_RES3
Definition tlv493d.h:145
@ R_BY2
Definition tlv493d.h:124
@ R_TEMP2
Definition tlv493d.h:128
@ R_TEMP1
Definition tlv493d.h:127
@ W_RES2
Definition tlv493d.h:144
@ R_RES1
Definition tlv493d.h:132
@ R_BY1
Definition tlv493d.h:123
@ W_LP_PERIOD
Definition tlv493d.h:141
@ W_TEMP_NEN
Definition tlv493d.h:140
@ W_FAST
Definition tlv493d.h:138
@ W_RES1
Definition tlv493d.h:143
@ R_FRAMECOUNTER
Definition tlv493d.h:129
@ W_ADDR
Definition tlv493d.h:136
@ W_PARITY
Definition tlv493d.h:135
@ R_BZ1
Definition tlv493d.h:125
@ R_BX2
Definition tlv493d.h:122
@ R_RES3
Definition tlv493d.h:134
~Tlv493d()
Definition tlv493d.h:117
void SetAccessMode(AccessMode_e mode)
Definition tlv493d.h:360
Definition tlv493d.h:30
~Tlv493dI2CTransport()
Definition tlv493d.h:33
Tlv493dI2CTransport()
Definition tlv493d.h:32
void Write(uint8_t *data, uint16_t size)
Definition tlv493d.h:79
void WriteAddress(uint8_t add, uint8_t *data, uint16_t size)
Definition tlv493d.h:74
uint8_t GetAddress()
Definition tlv493d.h:99
void Read(uint8_t *data, uint16_t size)
Definition tlv493d.h:85
bool GetError()
Definition tlv493d.h:91
void Init(Config config)
Definition tlv493d.h:56
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTB
Definition daisy_core.h:178
Definition i2c.h:30
struct daisy::I2CHandle::Config::@15 pin_config
Mode mode
Definition i2c.h:65
Speed
Definition i2c.h:51
Speed speed
Definition i2c.h:64
Peripheral periph
Definition i2c.h:57
Peripheral
Definition i2c.h:40
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition tlv493d.h:157
uint16_t measurementTime
Definition tlv493d.h:161
uint8_t lp
Definition tlv493d.h:159
uint8_t fast
Definition tlv493d.h:158
uint8_t lpPeriod
Definition tlv493d.h:160
Definition tlv493d.h:210
Config()
Definition tlv493d.h:212
Transport::Config transport_config
Definition tlv493d.h:211
Definition tlv493d.h:149
uint8_t shift
Definition tlv493d.h:153
uint8_t rw
Definition tlv493d.h:150
uint8_t byteAdress
Definition tlv493d.h:151
uint8_t bitMask
Definition tlv493d.h:152
Definition tlv493d.h:36
Config()
Definition tlv493d.h:44
I2CHandle::Config::Speed speed
Definition tlv493d.h:38
uint8_t address
Definition tlv493d.h:42
Pin sda
Definition tlv493d.h:40
I2CHandle::Config::Peripheral periph
Definition tlv493d.h:37
Pin scl
Definition tlv493d.h:39
#define TLV493D_B_MULT
Definition tlv493d.h:15
#define TLV493D_BUSIF_READSIZE
Definition tlv493d.h:9
#define REGMASK_READ
Definition tlv493d.h:19
#define TLV493D_ADDRESS1
Definition tlv493d.h:7
#define REGMASK_WRITE
Definition tlv493d.h:20
#define TLV493D_TEMP_MULT
Definition tlv493d.h:16
#define TLV493D_BUSIF_WRITESIZE
Definition tlv493d.h:10
#define TLV493D_NUM_OF_REGMASKS
Definition tlv493d.h:11
#define TLV493D_TEMP_OFFSET
Definition tlv493d.h:17