50 pinMode(csPin, OUTPUT);
51 digitalWrite(csPin, HIGH);
63 uint16_t data = transfer16((uint16_t)(address & 0x7) << 13) & 0xFFF;
71 return readReg((uint8_t)address);
82 transfer16(((uint16_t)(address & 0x7) << 13) | (1 << 12) | (value & 0xFFF));
96 SPISettings settings = SPISettings(5000000, MSBFIRST, SPI_MODE0);
98 uint16_t transfer16(uint16_t value)
100 return SPI.transfer16(value);
105 digitalWrite(csPin, LOW);
106 SPI.beginTransaction(settings);
111 SPI.endTransaction();
112 digitalWrite(csPin, HIGH);
181 driver.setChipSelectPin(pin);
200 driver.writeReg(MP6602RegAddr::TSTP, 0x510);
215 return driver.readReg(MP6602RegAddr::CTRL) == (ctrl & ~(1 << 1)) &&
216 driver.readReg(MP6602RegAddr::CTRL2) == ctrl2 &&
217 driver.readReg(MP6602RegAddr::ISET) == iset &&
218 driver.readReg(MP6602RegAddr::STALL) == stall &&
219 (
driver.readReg(MP6602RegAddr::BEMF) & 0xF00) == (bemf & 0xF00);
249 if (current > 4000) { current = 4000; }
250 if (current < 125) { current = 125; }
252 uint8_t is = (current - 125) / 125;
253 iset = (iset & 0xFC0) | is;
266 if (current > 4000) { current = 4000; }
267 if (current < 125) { current = 125; }
269 uint8_t il = (current - 125) / 125;
270 iset = (iset & 0x03F) | ((uint16_t)il << 6);
287 if (time == 0) { ah = 0; }
288 else if (time <= 15) { ah = 1; }
289 else if (time <= 31) { ah = 2; }
290 else if (time <= 62) { ah = 3; }
291 else if (time <= 125) { ah = 4; }
292 else if (time <= 250) { ah = 5; }
293 else if (time <= 500) { ah = 6; }
296 ctrl = (ctrl & 0x1FF) | ((uint16_t)ah << 9);
349 return (ctrl >> 2) & 1;
357 driver.writeReg(MP6602RegAddr::CTRL, ctrl | (1 << 1));
382 ctrl = (ctrl & 0xFC7) | ((uint8_t)mode << 3);
433 return driver.readReg(MP6602RegAddr::FAULT) & 0x3FF;
446 return driver.readReg(MP6602RegAddr::OCP) & 0xF;
460 driver.writeReg(MP6602RegAddr::OCP, 0x55F);
464 driver.writeReg(MP6602RegAddr::FAULT, 0x3BE);
472 if (!cachedReg) {
return 0; }
488 if (!cachedReg) {
return; }
489 *cachedReg = value & 0xFFF;
490 driver.writeReg(address, value);
495 uint16_t ctrl, ctrl2, iset, stall, bemf;
503 case MP6602RegAddr::CTRL:
return &ctrl;
504 case MP6602RegAddr::CTRL2:
return &ctrl2;
505 case MP6602RegAddr::ISET:
return &iset;
506 case MP6602RegAddr::STALL:
return &stall;
507 case MP6602RegAddr::BEMF:
return &bemf;
508 default:
return nullptr;
516 if (!cachedReg) {
return; }
517 driver.writeReg(address, *cachedReg);
@ OCPBH
Bridge B high-side OCP (such as as short to ground).
@ OCPAL
Bridge A low-side OCP (such as short to VIN).
@ OCPAH
Bridge A high-side OCP (such as short to ground).
@ OCPBL
Bridge B low-side OCP (such as short to VIN).
MP6602StepMode
Possible arguments to setStepMode().
@ MicroStep1
Full step with 71% current.
@ OLB
Open load on bridge B.
@ VINUV
VIN under-voltage.
@ FLT
Fault indication (logical OR of other bits in FAULT register except VCCUV and VINUV; 0 when nFAULT pi...
@ OTW
Over-temperature warning.
@ OTS
Over-temperature shutdown.
@ VCCUV
VCC under-voltage (set at start-up and reset).
@ OVP
Over-voltage protection.
@ OLA
Open load on bridge A.
void setStepMode(uint16_t mode)
void setAutoHoldTimeMs(uint16_t time)
void setStepMode(MP6602StepMode mode)
uint16_t * cachedRegPtr(MP6602RegAddr address)
MP6602()
The default constructor.
void setCurrentMilliamps(uint16_t current)
uint16_t getCachedReg(MP6602RegAddr address)
void setHoldCurrentMilliamps(uint16_t current)
void writeCachedReg(MP6602RegAddr address)
Writes the cached value of the given register to the device.
void setChipSelectPin(uint8_t pin)
void setReg(MP6602RegAddr address, uint16_t value)
void setDirection(bool value)
void writeReg(MP6602RegAddr address, uint16_t value)
Writes the specified value to a register.
uint16_t readReg(uint8_t address)
Reads the register at the given address and returns its raw value.
void writeReg(uint8_t address, uint16_t value)
Writes the specified value to a register.
void setChipSelectPin(uint8_t pin)
uint16_t readReg(MP6602RegAddr address)
Reads the register at the given address and returns its raw value.