tmc5072 0.3.1

A TCM5072 driver compatible with `embedded-hal`
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Microstep Table Registers
//!
//! MSLUT: Each bit gives the difference between entry x and entry x+1 when combined with the corresponding MSLUTSEL W bits:
//!  - false: W= %00: -1, %01: +0, %10: +1, %11: +2
//!  - true: W= %00: +0, %01: +1, %10: +2, %11: +3
//!
//! This is the differential coding for the first quarter of a wave.
//! Start values for CUR_A and CUR_B are stored for MSCNT position 0 in START_SIN and START_SIN90.

use super::Register;
use crate::bits::{read_from_bit, write_from_bit};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// MSLUT\[0\]: Microstep table entries 0..31
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut0 {
    /// Microstep table entries 0..31
    pub ms_lut0: u32,
}

impl Default for MsLut0 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut0 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut0: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut0> for u32 {
    fn from(data: MsLut0) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut0);
        value
    }
}

impl Register for MsLut0 {
    fn addr() -> u8 {
        0x60
    }
}

#[cfg(test)]
mod ms_lut0 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut0 {
                ms_lut0: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut0::from(0xF0F0F0F0),
            MsLut0 {
                ms_lut0: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}

/// MSLUT\[1\]: Microstep table entries 32..63
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut1 {
    /// Microstep table entries 32..63
    pub ms_lut1: u32,
}

impl Default for MsLut1 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut1 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut1: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut1> for u32 {
    fn from(data: MsLut1) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut1);
        value
    }
}

impl Register for MsLut1 {
    fn addr() -> u8 {
        0x61
    }
}

#[cfg(test)]
mod ms_lut1 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut1 {
                ms_lut1: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut1::from(0xF0F0F0F0),
            MsLut1 {
                ms_lut1: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}
/// MSLUT\[2\]: Microstep table entries 64..95
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut2 {
    /// Microstep table entries 64..95
    pub ms_lut2: u32,
}

impl Default for MsLut2 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut2 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut2: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut2> for u32 {
    fn from(data: MsLut2) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut2);
        value
    }
}

impl Register for MsLut2 {
    fn addr() -> u8 {
        0x62
    }
}

#[cfg(test)]
mod ms_lut2 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut2 {
                ms_lut2: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut2::from(0xF0F0F0F0),
            MsLut2 {
                ms_lut2: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}
/// MSLUT\[3\]: Microstep table entries 96..127
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut3 {
    /// Microstep table entries 96..127
    pub ms_lut3: u32,
}

impl Default for MsLut3 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut3 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut3: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut3> for u32 {
    fn from(data: MsLut3) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut3);
        value
    }
}

impl Register for MsLut3 {
    fn addr() -> u8 {
        0x63
    }
}

#[cfg(test)]
mod ms_lut3 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut3 {
                ms_lut3: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut3::from(0xF0F0F0F0),
            MsLut3 {
                ms_lut3: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}
/// MSLUT\[4\]: Microstep table entries 128..159
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut4 {
    /// Microstep table entries 128..159
    pub ms_lut4: u32,
}

impl Default for MsLut4 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut4 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut4: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut4> for u32 {
    fn from(data: MsLut4) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut4);
        value
    }
}

impl Register for MsLut4 {
    fn addr() -> u8 {
        0x64
    }
}

#[cfg(test)]
mod ms_lut4 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut4 {
                ms_lut4: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut4::from(0xF0F0F0F0),
            MsLut4 {
                ms_lut4: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}

/// MSLUT\[5\]: Microstep table entries 160..191
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut5 {
    /// Microstep table entries 160..191
    pub ms_lut5: u32,
}

impl Default for MsLut5 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut5 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut5: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut5> for u32 {
    fn from(data: MsLut5) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut5);
        value
    }
}

impl Register for MsLut5 {
    fn addr() -> u8 {
        0x65
    }
}

#[cfg(test)]
mod ms_lut5 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut5 {
                ms_lut5: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut5::from(0xF0F0F0F0),
            MsLut5 {
                ms_lut5: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}

/// MSLUT\[6\]: Microstep table entries 192..223
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut6 {
    /// Microstep table entries 192..223
    pub ms_lut6: u32,
}

impl Default for MsLut6 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut6 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut6: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut6> for u32 {
    fn from(data: MsLut6) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut6);
        value
    }
}

impl Register for MsLut6 {
    fn addr() -> u8 {
        0x66
    }
}

#[cfg(test)]
mod ms_lut6 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut6 {
                ms_lut6: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut6::from(0xF0F0F0F0),
            MsLut6 {
                ms_lut6: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}

/// MSLUT\[7\]: Microstep table entries 224..255
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLut7 {
    /// Microstep table entries 224..255
    pub ms_lut7: u32,
}

impl Default for MsLut7 {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLut7 {
    fn from(data: u32) -> Self {
        Self {
            ms_lut7: read_from_bit(data, 0, 0xffffffff) as u32,
        }
    }
}

impl From<MsLut7> for u32 {
    fn from(data: MsLut7) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xffffffff, data.ms_lut7);
        value
    }
}

impl Register for MsLut7 {
    fn addr() -> u8 {
        0x67
    }
}

#[cfg(test)]
mod ms_lut7 {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLut7 {
                ms_lut7: 0xF0F0F0F0,
                ..Default::default()
            }),
            0xF0F0F0F0
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLut7::from(0xF0F0F0F0),
            MsLut7 {
                ms_lut7: 0xF0F0F0F0,
                ..Default::default()
            },
        )
    }
}

/// MSLUTSEL: Look up Table Segmentation
///
/// Width control bit coding W0…W3:
///  - %00: MSLUT entry 0, 1 select: -1, +0
///  - %01: MSLUT entry 0, 1 select: +0, +1
///  - %10: MSLUT entry 0, 1 select: +1, +2
///  - %11: MSLUT entry 0, 1 select: +2, +3
///
/// The sine wave look up table can be divided into up to four segments using an individual step width control entry Wx.
/// The segment borders are selected by X1, X2 and X3.
///  - Segment 0 goes from 0 to X1-1.
///  - Segment 1 goes from X1 to X2-1.
///  - Segment 2 goes from X2 to X3-1.
///  - Segment 3 goes from X3 to 255.
///
/// For defined response the values shall satisfy: 0<X1<X2<X3
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLutSel {
    /// W0: LUT width select from ofs00 to ofs(X1-1)
    pub w0: u8,
    /// W1: LUT width select from ofs(X1) to ofs(X2-1)
    pub w1: u8,
    /// W2: LUT width select from ofs(X2) to ofs(X3-1)
    pub w2: u8,
    /// W3: LUT width select from ofs(X3) to ofs255
    pub w3: u8,
    /// X1: LUT segment 1 start
    pub x1: u8,
    /// X2: LUT segment 2 start
    pub x2: u8,
    /// X3: LUT segment 3 start
    pub x3: u8,
}

impl Default for MsLutSel {
    fn default() -> Self {
        Self::from(0u32)
    }
}

impl From<u32> for MsLutSel {
    fn from(data: u32) -> Self {
        Self {
            w0: read_from_bit(data, 0, 0x3) as u8,
            w1: read_from_bit(data, 2, 0x3) as u8,
            w2: read_from_bit(data, 4, 0x3) as u8,
            w3: read_from_bit(data, 6, 0x3) as u8,
            x1: read_from_bit(data, 8, 0xff) as u8,
            x2: read_from_bit(data, 16, 0xff) as u8,
            x3: read_from_bit(data, 24, 0xff) as u8,
        }
    }
}

impl From<MsLutSel> for u32 {
    fn from(data: MsLutSel) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0x3, data.w0 as u32);
        write_from_bit(&mut value, 2, 0x3, data.w1 as u32);
        write_from_bit(&mut value, 4, 0x3, data.w2 as u32);
        write_from_bit(&mut value, 6, 0x3, data.w3 as u32);
        write_from_bit(&mut value, 8, 0xff, data.x1 as u32);
        write_from_bit(&mut value, 16, 0xff, data.x2 as u32);
        write_from_bit(&mut value, 24, 0xff, data.x3 as u32);
        value
    }
}

impl Register for MsLutSel {
    fn addr() -> u8 {
        0x68
    }
}

#[cfg(test)]
mod ms_lut_sel {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLutSel {
                x1: 0x66,
                ..Default::default()
            }),
            0x00006600
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLutSel::from(0x00006600),
            MsLutSel {
                x1: 0x66,
                ..Default::default()
            },
        )
    }
}

/// MSLUTSTART
///
/// Start values are transferred to the microstep registers CUR_A and CUR_B, whenever the reference position MSCNT=0 is passed.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MsLutStart {
    /// START_SIN: gives the absolute current at microstep table entry 0.
    pub start_sin: u8,
    /// START_SIN90: gives the absolute current for microstep table entry at positions 256.
    pub start_sin90: u8,
}

impl Default for MsLutStart {
    fn default() -> Self {
        Self {
            start_sin: 0,
            start_sin90: 247,
        }
    }
}

impl From<u32> for MsLutStart {
    fn from(data: u32) -> Self {
        Self {
            start_sin: read_from_bit(data, 0, 0xff) as u8,
            start_sin90: read_from_bit(data, 8, 0xff) as u8,
        }
    }
}

impl From<MsLutStart> for u32 {
    fn from(data: MsLutStart) -> Self {
        let mut value = 0;
        write_from_bit(&mut value, 0, 0xff, data.start_sin as u32);
        write_from_bit(&mut value, 8, 0xff, data.start_sin90 as u32);
        value
    }
}

impl Register for MsLutStart {
    fn addr() -> u8 {
        0x69
    }
}

#[cfg(test)]
mod ms_lut_start {
    use super::*;
    #[test]
    fn to_u32() {
        assert_eq!(
            u32::from(MsLutStart {
                start_sin90: 247,
                ..Default::default()
            }),
            0x0000F700
        )
    }
    #[test]
    fn from_u32() {
        assert_eq!(
            MsLutStart::from(0x0000F700),
            MsLutStart {
                start_sin90: 247,
                ..Default::default()
            },
        )
    }
}