DRV8434S library
Loading...
Searching...
No Matches
DRV8434S.h
Go to the documentation of this file.
1// Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2
13
14#pragma once
15
16#include <Arduino.h>
17#include <SPI.h>
18
20enum class DRV8434SRegAddr : uint8_t
21{
22 FAULT = 0x00,
23 DIAG1 = 0x01,
24 DIAG2 = 0x02,
25 CTRL1 = 0x03,
26 CTRL2 = 0x04,
27 CTRL3 = 0x05,
28 CTRL4 = 0x06,
29 CTRL5 = 0x07,
30 CTRL6 = 0x08,
31 CTRL7 = 0x09,
32 CTRL8 = 0x0A,
33 CTRL9 = 0x0B,
34};
35
36
43{
44public:
48 void setChipSelectPin(uint8_t pin)
49 {
50 csPin = pin;
51 pinMode(csPin, OUTPUT);
52 digitalWrite(csPin, HIGH);
53 }
54
56 uint8_t readReg(uint8_t address)
57 {
58 // Arduino out / DRV8434 in: First byte contains read/write bit and register
59 // address; second byte is unused.
60 // Arduino in / DRV8434 out: First byte contains status; second byte
61 // contains data in register being read.
62
63 selectChip();
64 lastStatus = transfer((0x20 | (address & 0b11111)) << 1);
65 uint8_t data = transfer(0);
66 deselectChip();
67 return data;
68 }
69
71 uint16_t readReg(DRV8434SRegAddr address)
72 {
73 return readReg((uint8_t)address);
74 }
75
77 uint8_t writeReg(uint8_t address, uint8_t value)
78 {
79 // Arduino out / DRV8434 in: First byte contains read/write bit and register
80 // address; second byte contains data to write to register.
81 // Arduino in / DRV8434 out: First byte contains status; second byte
82 // contains old (existing) data in register being written to.
83
84 selectChip();
85 lastStatus = transfer((address & 0b11111) << 1);
86 uint8_t oldData = transfer(value);
87 // The CS line must go low after writing for the value to actually take
88 // effect.
89 deselectChip();
90 return oldData;
91 }
92
94 void writeReg(DRV8434SRegAddr address, uint8_t value)
95 {
96 writeReg((uint8_t)address, value);
97 }
98
99private:
100
101 SPISettings settings = SPISettings(500000, MSBFIRST, SPI_MODE1);
102
103 uint8_t transfer(uint8_t value)
104 {
105 return SPI.transfer(value);
106 }
107
108 void selectChip()
109 {
110 digitalWrite(csPin, LOW);
111 SPI.beginTransaction(settings);
112 }
113
114 void deselectChip()
115 {
116 SPI.endTransaction();
117 digitalWrite(csPin, HIGH);
118 }
119
120 uint8_t csPin;
121
122public:
123
128 uint8_t lastStatus = 0;
129};
130
131
136enum class DRV8434SFaultBit : uint8_t
137{
139 FAULT = 7,
140
142 SPI_ERROR = 6,
143
145 UVLO = 5,
146
148 CPUV = 4,
149
151 OCP = 3,
152
154 STL = 2,
155
157 TF = 1,
158
160 OL = 0,
161};
162
167enum class DRV8434SDiag1Bit : uint8_t
168{
170 OCP_LS2_B = 7,
171
173 OCP_HS2_B = 6,
174
176 OCP_LS1_B = 5,
177
179 OCP_HS1_B = 4,
180
182 OCP_LS2_A = 3,
183
185 OCP_HS2_A = 2,
186
188 OCP_LS1_A = 1,
189
191 OCP_HS1_A = 0,
192};
193
198enum class DRV8434SDiag2Bit : uint8_t
199{
201 OTW = 6,
202
204 OTS = 5,
205
207 STL_LRN_OK = 4,
208
210 STALL = 3,
211
213 OL_B = 1,
214
216 OL_A = 0,
217};
218
220enum class DRV8434SDecayMode : uint8_t
221{
222 Slow = 0b000,
223 IncSlowDecMixed30 = 0b001,
224 IncSlowDecMixed60 = 0b010,
225 IncSlowDecFast = 0b011,
226 Mixed30 = 0b100,
227 Mixed60 = 0b101,
228 SmartTuneDynamicDecay = 0b110,
229 SmartTuneRippleControl = 0b111,
230};
231
233enum class DRV8434SStepMode : uint8_t
234{
236 MicroStep1_100 = 0b0000,
237
239 MicroStep1 = 0b0001,
240
242 MicroStep2_NC = 0b0010,
243
245 MicroStep2 = 0b0011,
246
247 MicroStep4 = 0b0100,
248 MicroStep8 = 0b0101,
249 MicroStep16 = 0b0110,
250 MicroStep32 = 0b0111,
251 MicroStep64 = 0b1000,
252 MicroStep128 = 0b1001,
253 MicroStep256 = 0b1010,
254};
255
256
260{
261public:
264 {
265 // All settings set to power-on defaults
266 ctrl1 = 0x00;
267 ctrl2 = 0x0F;
268 ctrl3 = 0x06;
269 ctrl4 = 0x30;
270 ctrl5 = 0x08;
271 ctrl6 = 0x03;
272 ctrl7 = 0x20;
273 }
274
277 void setChipSelectPin(uint8_t pin)
278 {
280 }
281
288 {
289 ctrl1 = 0x00;
290 ctrl2 = 0x0F;
291 ctrl3 = 0x06;
292 ctrl4 = 0x30;
293 ctrl5 = 0x08;
294 ctrl6 = 0x03;
295 ctrl7 = 0x20;
297 }
298
309 {
310 return driver.readReg(DRV8434SRegAddr::CTRL1) == ctrl1 &&
311 driver.readReg(DRV8434SRegAddr::CTRL2) == ctrl2 &&
312 driver.readReg(DRV8434SRegAddr::CTRL3) == ctrl3 &&
313 driver.readReg(DRV8434SRegAddr::CTRL4) == ctrl4 &&
314 driver.readReg(DRV8434SRegAddr::CTRL5) == ctrl5 &&
315 driver.readReg(DRV8434SRegAddr::CTRL6) == ctrl6 &&
316 driver.readReg(DRV8434SRegAddr::CTRL7) == ctrl7;
317 }
318
327 {
328 writeCachedReg(DRV8434SRegAddr::CTRL1);
329 writeCachedReg(DRV8434SRegAddr::CTRL3);
330 writeCachedReg(DRV8434SRegAddr::CTRL4);
331 writeCachedReg(DRV8434SRegAddr::CTRL5);
332 writeCachedReg(DRV8434SRegAddr::CTRL6);
333 writeCachedReg(DRV8434SRegAddr::CTRL7);
334
335 // CTRL2 is written last because it contains the EN_OUT bit, and we want to
336 // try to have all the other settings correct first.
337 writeCachedReg(DRV8434SRegAddr::CTRL2);
338 }
339
363 void setCurrentPercent(uint8_t percent)
364 {
365 if (percent > 100) { percent = 100; }
366 if (percent < 6) { percent = 6; }
367
368 uint8_t td = ((uint16_t)percent * 4 + 3) / 25; // convert 6-100% to 1-16, rounding up by at most 0.75%
369 td = 16 - td; // convert 1-16 to 15-0 (15 = 6.25%, 0 = 100%)
370 ctrl1 = (ctrl1 & 0b00001111) | (td << 4);
371 writeCachedReg(DRV8434SRegAddr::CTRL1);
372 }
373
393 void setCurrentMilliamps(uint16_t current, uint16_t fullCurrent = 2000)
394 {
395 if (fullCurrent > 4000) { fullCurrent = 4000; }
396 if (current > fullCurrent) { current = fullCurrent; }
397
398 uint8_t td = (current * 16 / fullCurrent); // convert 0-fullCurrent to 0-16
399 if (td == 0) { td = 1; } // restrict to 1-16
400 td = 16 - td; // convert 1-16 to 15-0 (15 = 6.25%, 0 = 100%)
401 ctrl1 = (ctrl1 & 0b00001111) | (td << 4);
402 writeCachedReg(DRV8434SRegAddr::CTRL1);
403 }
404
407 {
408 ctrl2 |= (1 << 7);
409 writeCachedReg(DRV8434SRegAddr::CTRL2);
410 }
411
414 {
415 ctrl2 &= ~(1 << 7);
416 writeCachedReg(DRV8434SRegAddr::CTRL2);
417 }
418
426 {
427 ctrl2 = (ctrl2 & 0b11111000) | (((uint8_t)mode & 0b111) << 8);
428 writeCachedReg(DRV8434SRegAddr::CTRL2);
429 }
430
439 void setDirection(bool value)
440 {
441 if (value)
442 {
443 ctrl3 |= (1 << 7);
444 }
445 else
446 {
447 ctrl3 &= ~(1 << 7);
448 }
449 writeCachedReg(DRV8434SRegAddr::CTRL3);
450 }
451
456 {
457 return (ctrl3 >> 7) & 1;
458 }
459
467 void step()
468 {
469 driver.writeReg(DRV8434SRegAddr::CTRL3, ctrl3 | (1 << 6));
470 }
471
475 {
476 ctrl3 |= (1 << 5);
477 writeCachedReg(DRV8434SRegAddr::CTRL3);
478 }
479
483 {
484 ctrl3 &= ~(1 << 5);
485 writeCachedReg(DRV8434SRegAddr::CTRL3);
486 }
487
491 {
492 ctrl3 |= (1 << 4);
493 writeCachedReg(DRV8434SRegAddr::CTRL3);
494 }
495
499 {
500 ctrl3 &= ~(1 << 4);
501 writeCachedReg(DRV8434SRegAddr::CTRL3);
502 }
503
518 {
519 if (mode > DRV8434SStepMode::MicroStep256)
520 {
521 // Invalid mode; pick 1/16 micro-step by default.
522 mode = DRV8434SStepMode::MicroStep16;
523 }
524
525 ctrl3 = (ctrl3 & 0b11110000) | (uint8_t)mode;
526 writeCachedReg(DRV8434SRegAddr::CTRL3);
527 }
528
538 void setStepMode(uint16_t mode)
539 {
541
542 switch (mode)
543 {
544 case 1: sm = DRV8434SStepMode::MicroStep1; break;
545 case 2: sm = DRV8434SStepMode::MicroStep2; break;
546 case 4: sm = DRV8434SStepMode::MicroStep4; break;
547 case 8: sm = DRV8434SStepMode::MicroStep8; break;
548 case 16: sm = DRV8434SStepMode::MicroStep16; break;
549 case 32: sm = DRV8434SStepMode::MicroStep32; break;
550 case 64: sm = DRV8434SStepMode::MicroStep64; break;
551 case 128: sm = DRV8434SStepMode::MicroStep128; break;
552 case 256: sm = DRV8434SStepMode::MicroStep256; break;
553
554 // Invalid mode; pick 1/16 micro-step by default.
555 default: sm = DRV8434SStepMode::MicroStep16;
556 }
557
558 setStepMode(sm);
559 }
560
575 uint8_t readFault()
576 {
577 return driver.readReg(DRV8434SRegAddr::FAULT);
578 }
579
586 uint8_t readDiag1()
587 {
588 return driver.readReg(DRV8434SRegAddr::DIAG1);
589 }
590
597 uint8_t readDiag2()
598 {
599 return driver.readReg(DRV8434SRegAddr::DIAG2);
600 }
601
612 {
613 driver.writeReg(DRV8434SRegAddr::CTRL4, ctrl4 | (1 << 7));
614 }
615
619 {
620 uint8_t * cachedReg = cachedRegPtr(address);
621 if (!cachedReg) { return 0; }
622 return *cachedReg;
623 }
624
634 void setReg(DRV8434SRegAddr address, uint8_t value)
635 {
636 uint8_t * cachedReg = cachedRegPtr(address);
637 if (!cachedReg) { return; }
638 *cachedReg = value;
639 driver.writeReg(address, value);
640 }
641
642protected:
643
644 uint8_t ctrl1, ctrl2, ctrl3, ctrl4, ctrl5, ctrl6, ctrl7;
645
648 uint8_t * cachedRegPtr(DRV8434SRegAddr address)
649 {
650 switch (address)
651 {
652 case DRV8434SRegAddr::CTRL1: return &ctrl1;
653 case DRV8434SRegAddr::CTRL2: return &ctrl2;
654 case DRV8434SRegAddr::CTRL3: return &ctrl3;
655 case DRV8434SRegAddr::CTRL4: return &ctrl4;
656 case DRV8434SRegAddr::CTRL5: return &ctrl5;
657 case DRV8434SRegAddr::CTRL6: return &ctrl6;
658 case DRV8434SRegAddr::CTRL7: return &ctrl7;
659 default: return nullptr;
660 }
661 }
662
665 {
666 uint8_t * cachedReg = cachedRegPtr(address);
667 if (!cachedReg) { return; }
668 driver.writeReg(address, *cachedReg);
669 }
670
671public:
678};
DRV8434SDiag2Bit
Definition: DRV8434S.h:199
@ STL_LRN_OK
Stall detection learning successful.
@ OL_A
Open load on AOUT.
@ OTW
Overtemperature warning.
@ OTS
Overtemperature shutdown.
@ OL_B
Open load on BOUT.
@ STALL
Motor stall condition.
DRV8434SDecayMode
Possible arguments to setDecayMode().
Definition: DRV8434S.h:221
DRV8434SDiag1Bit
Definition: DRV8434S.h:168
@ OCP_HS2_A
Overcurrent fault on high-side FET of half bridge 2 in AOUT.
@ OCP_LS2_A
Overcurrent fault on low-side FET of half bridge 2 in AOUT.
@ OCP_LS2_B
Overcurrent fault on low-side FET of half bridge 2 in BOUT.
@ OCP_HS2_B
Overcurrent fault on high-side FET of half bridge 2 in BOUT.
@ OCP_LS1_B
Overcurrent fault on low-side FET of half bridge 1 in BOUT.
@ OCP_HS1_A
Overcurrent fault on high-side FET of half bridge 1 in AOUT.
@ OCP_LS1_A
Overcurrent fault on low-side FET of half bridge 1 in AOUT.
@ OCP_HS1_B
Overcurrent fault on high-side FET of half bridge 1 in BOUT.
DRV8434SFaultBit
Definition: DRV8434S.h:137
@ STL
Motor stall.
@ SPI_ERROR
SPI protocol error (latched)
@ UVLO
Supply undervoltage lockout fault.
@ OCP
Overcurrent fault.
@ CPUV
Charge pump undervoltage fault.
@ OL
Open load.
@ TF
Overtemperature warning or shutdown.
DRV8434SRegAddr
Addresses of control and status registers.
Definition: DRV8434S.h:21
DRV8434SStepMode
Possible arguments to setStepMode().
Definition: DRV8434S.h:234
@ MicroStep2_NC
Non-circular 1/2 step.
@ MicroStep1_100
Full step with 100% current.
@ MicroStep2
Circular 1/2 step.
@ MicroStep1
Full step with 71% current.
void resetSettings()
Definition: DRV8434S.h:287
void enableSPIStep()
Definition: DRV8434S.h:490
uint8_t getCachedReg(DRV8434SRegAddr address)
Definition: DRV8434S.h:618
void applySettings()
Definition: DRV8434S.h:326
void clearFaults()
Definition: DRV8434S.h:611
void enableSPIDirection()
Definition: DRV8434S.h:474
bool verifySettings()
Definition: DRV8434S.h:308
void setReg(DRV8434SRegAddr address, uint8_t value)
Definition: DRV8434S.h:634
void setDecayMode(DRV8434SDecayMode mode)
Definition: DRV8434S.h:425
void writeCachedReg(DRV8434SRegAddr address)
Writes the cached value of the given register to the device.
Definition: DRV8434S.h:664
void setChipSelectPin(uint8_t pin)
Definition: DRV8434S.h:277
void step()
Definition: DRV8434S.h:467
void setCurrentMilliamps(uint16_t current, uint16_t fullCurrent=2000)
Definition: DRV8434S.h:393
uint8_t readDiag2()
Definition: DRV8434S.h:597
void setDirection(bool value)
Definition: DRV8434S.h:439
void disableDriver()
Disables the driver (EN_OUT = 0).
Definition: DRV8434S.h:413
bool getDirection()
Definition: DRV8434S.h:455
uint8_t * cachedRegPtr(DRV8434SRegAddr address)
Definition: DRV8434S.h:648
void setStepMode(DRV8434SStepMode mode)
Definition: DRV8434S.h:517
uint8_t readDiag1()
Definition: DRV8434S.h:586
uint8_t readFault()
Definition: DRV8434S.h:575
void setStepMode(uint16_t mode)
Definition: DRV8434S.h:538
void setCurrentPercent(uint8_t percent)
Definition: DRV8434S.h:363
void enableDriver()
Enables the driver (EN_OUT = 1).
Definition: DRV8434S.h:406
void disableSPIStep()
Definition: DRV8434S.h:498
void disableSPIDirection()
Definition: DRV8434S.h:482
DRV8434SSPI driver
Definition: DRV8434S.h:677
DRV8434S()
The default constructor.
Definition: DRV8434S.h:263
void setChipSelectPin(uint8_t pin)
Definition: DRV8434S.h:48
uint8_t readReg(uint8_t address)
Reads the register at the given address and returns its raw value.
Definition: DRV8434S.h:56
uint8_t writeReg(uint8_t address, uint8_t value)
Writes the specified value to a register.
Definition: DRV8434S.h:77
void writeReg(DRV8434SRegAddr address, uint8_t value)
Writes the specified value to a register.
Definition: DRV8434S.h:94
uint8_t lastStatus
Definition: DRV8434S.h:128
uint16_t readReg(DRV8434SRegAddr address)
Reads the register at the given address and returns its raw value.
Definition: DRV8434S.h:71