UUtzinger_DRV8704  0.1.1
Arduino library for the DRV8704 dual H-bridge gate driver
drv8704.h
Go to the documentation of this file.
1 
6 #ifndef DRV8704_H
7 #define DRV8704_H
8 
9 #include <Arduino.h>
10 #include <SPI.h>
11 
12 #include "drv8704_defs.h"
13 #include "drv8704_types.h"
14 #include "drv8704_regs_typedefs.h"
15 
16 class DRV8704PwmBackend;
17 
21 class DRV8704 {
22 public:
28  explicit DRV8704(const DRV8704Pins& pins, SPIClass& spi = SPI);
29 
34  bool begin();
35 
39  void end();
40 
44  void sleep();
45 
49  void wake();
50 
54  void reset();
55 
61  uint16_t readRegister(uint8_t address);
62 
68  uint16_t readRegister(RegisterAddress address) {
69  return readRegister(static_cast<uint8_t>(address));
70  }
71 
78  bool writeRegister(uint8_t address, uint16_t value);
79 
86  bool writeRegister(RegisterAddress address, uint16_t value) {
87  return writeRegister(static_cast<uint8_t>(address), value);
88  }
89 
97  bool updateRegister(uint8_t address, uint16_t mask, uint16_t value);
98 
106  bool updateRegister(RegisterAddress address, uint16_t mask, uint16_t value) {
107  return updateRegister(static_cast<uint8_t>(address), mask, value);
108  }
109 
115  bool enableBridgeDriver(bool enabled);
116 
122  bool setSenseGain(SenseGain gain);
123 
129  bool setDeadTime(DeadTime deadTime);
130 
136  bool setTorque(uint8_t torque);
137 
143  bool setOffTime(uint8_t offTime);
144 
150  bool setBlankTime(uint8_t blankTime);
151 
157  bool setDecayMode(DecayMode mode);
158 
164  bool setDecayTime(uint8_t decayTime);
165 
171  bool setOcpThreshold(OcpThreshold threshold);
172 
178  bool setOcpDeglitch(OcpDeglitch deglitch);
179 
186  bool setGateDriveTimes(GateDriveTime tDriveN, GateDriveTime tDriveP);
187 
195 
201  bool setGateDrive(const GateDriveConfig& gateDrive);
202 
208  bool applyConfig(const DRV8704Config& config);
209 
215  bool setShuntResistance(float ohms);
216 
223  bool setShuntResistance(BridgeId bridge, float ohms);
224 
231 
239 
245  Direction direction(BridgeId bridge) const;
246 
252  bool setCurrentLimit(float amps);
253 
260  bool setCurrent(BridgeId bridge, float amps);
261 
268  bool disableCurrentLimit();
269 
274  bool isCurrentLimitEnabled() const { return currentLimitEnabled_; }
275 
280  DRV8704CurrentPresetConfig currentModePresetConfig() const { return currentPresetConfig_; }
281 
286  DRV8704CurrentLimitResult currentLimitResult() const { return currentLimitResult_; }
287 
293 
298  bool hasFault();
299 
304  bool hasFaultPin() const { return pins_.faultPin >= 0; }
305 
310  bool isFaultPinActive() const;
311 
316  bool clearFaults();
317 
323  bool clearFault(FaultBit bit);
324 
329  void coast(BridgeId bridge);
330 
335  void brake(BridgeId bridge);
336 
342  void setBridgeMode(BridgeId bridge, BridgeMode mode);
343 
350 
356  bool beginPwmMode(const DRV8704PwmConfig& config = DRV8704PwmConfig());
357 
361  void endPwmMode();
362 
368  bool setPwmFrequency(uint32_t frequencyHz);
369 
379  bool setSpeed(BridgeId bridge, float speedPercent);
380 
388  bool setSpeed(BridgeId bridge, Direction direction, float speedPercent) {
389  return setDirection(bridge, direction) && setSpeed(bridge, speedPercent);
390  }
391 
399  bool setCurrent(BridgeId bridge, Direction direction, float amps) {
400  return setDirection(bridge, direction) && setCurrent(bridge, amps);
401  }
402 
407  DRV8704PwmCapability pwmCapability() const { return pwmCapability_; }
408 
414  return pwmCapability_.smallestDutyIncrementPercent;
415  }
416 
421  bool isPwmModeEnabled() const { return pwmModeEnabled_; }
422 
428 
433  bool isInitialized() const { return initialized_; }
434 
440  uint16_t cachedRegister(uint8_t address) const;
441 
446  bool syncRegisterCache();
447 
448 private:
454  bool isValidAddress(uint8_t address) const;
455 
463  bool writeRegisterVerified(uint8_t address, uint16_t value, uint16_t verifyMask);
464 
473  bool updateRegisterVerified(uint8_t address, uint16_t mask, uint16_t value, uint16_t verifyMask);
474 
478  void seedDefaultRegisterCache();
479 
485  bool checkDefaultRegisters(uint8_t& matchCount);
486 
493  void logDefaultRegisterMismatch(uint8_t address, uint16_t expected, uint16_t actual) const;
494 
500  float senseGainValue(SenseGain gain) const;
501 
507  float shuntResistance(BridgeId bridge) const;
508 
513  float currentLimitReferenceShunt() const;
514 
521  DRV8704CurrentPresetConfig presetConfig(CurrentModePreset preset, uint32_t pwmFrequencyHz) const;
522 
529  bool deriveCurrentLimit(float amps, DRV8704CurrentLimitResult& result) const;
530 
537  bool applyCurrentLimitResult(const DRV8704CurrentLimitResult& result, uint32_t pwmFrequencyHz);
538 
544  bool applyBridgeState(BridgeId bridge);
545 
550  bool performWriteReadbackProbe();
551 
555  void beginTransaction();
556 
560  void endTransaction();
561 
567  uint16_t transferFrame(uint16_t frame);
568 
575  void setBridgePins(int8_t in1Pin, int8_t in2Pin, BridgeMode mode);
576 
582  void setBridgeStaticMode(BridgeId bridge, BridgeMode mode);
583 
592  bool setBridgePwm(int8_t in1Pin, int8_t in2Pin, Direction direction, float speedPercent);
593 
594  DRV8704Pins pins_;
595  SPIClass* spi_;
596  SPISettings spiSettings_;
597  uint16_t registerCache_[DRV8704_REGISTER_COUNT];
598  float shuntOhms_[2];
599  Direction bridgeDirections_[2];
600  DRV8704BridgeState bridgeStates_[2];
601  float bridgeSpeedPercents_[2];
602  CurrentModePreset currentPreset_;
603  DRV8704CurrentPresetConfig currentPresetConfig_;
604  DRV8704CurrentLimitResult currentLimitResult_;
605  bool currentLimitEnabled_;
606  DRV8704PwmBackend* pwmBackend_;
607  DRV8704PwmConfig pwmConfig_;
608  DRV8704PwmCapability pwmCapability_;
609  bool pwmModeEnabled_;
610  bool initialized_;
611 };
612 
613 #endif // DRV8704_H
Abstract board-specific PWM backend used for DRV8704 bridge inputs.
Definition: drv8704_pwm.h:16
Arduino-facing DRV8704 device driver class.
Definition: drv8704.h:21
bool setCurrent(BridgeId bridge, Direction direction, float amps)
Compatibility overload retained during the control-model refactor.
Definition: drv8704.h:399
void wake()
Bring the DRV8704 out of sleep mode when the sleep pin is available.
Definition: drv8704.cpp:147
void setBridgeMode(BridgeId bridge, BridgeMode mode)
Low-level static bridge helper retained for compatibility.
Definition: drv8704_bridge.cpp:165
bool begin()
Initialize GPIO and SPI state for the device.
Definition: drv8704.cpp:35
DRV8704PwmCapability pwmCapability() const
Return the resolved capability of the active PWM backend.
Definition: drv8704.h:407
bool updateRegister(uint8_t address, uint16_t mask, uint16_t value)
Update selected register bits while preserving other bits.
Definition: drv8704_comm.cpp:61
bool setCurrent(BridgeId bridge, float amps)
Program the chip for current drive on one bridge using the stored direction.
Definition: drv8704_current.cpp:263
bool writeRegister(uint8_t address, uint16_t value)
Write a 12-bit register payload to the device.
Definition: drv8704_comm.cpp:33
DRV8704CurrentLimitResult currentLimitResult() const
Return the most recent derived chip-global current-limit settings.
Definition: drv8704.h:286
bool setOffTime(uint8_t offTime)
Set the fixed PWM off time.
Definition: drv8704_config.cpp:66
bool setBlankTime(uint8_t blankTime)
Set the current blanking time.
Definition: drv8704_config.cpp:75
bool setGateDriveCurrents(GateDriveSinkCurrent iDriveN, GateDriveSourceCurrent iDriveP)
Set the gate-drive current fields.
Definition: drv8704_config.cpp:124
bool setTorque(uint8_t torque)
Set the torque DAC value.
Definition: drv8704_config.cpp:58
bool writeRegister(RegisterAddress address, uint16_t value)
Write a register by symbolic address.
Definition: drv8704.h:86
bool setGateDriveTimes(GateDriveTime tDriveN, GateDriveTime tDriveP)
Set the gate-drive timing fields.
Definition: drv8704_config.cpp:115
bool beginPwmMode(const DRV8704PwmConfig &config=DRV8704PwmConfig())
Enable board-specific PWM generation for the DRV8704 input pins.
Definition: drv8704_bridge.cpp:190
bool hasFault()
Check whether any reported fault bit is active.
Definition: drv8704_status.cpp:31
bool enableBridgeDriver(bool enabled)
Enable or disable the bridge driver through the CTRL register.
Definition: drv8704_config.cpp:34
bool setOcpThreshold(OcpThreshold threshold)
Set the overcurrent threshold.
Definition: drv8704_config.cpp:99
void sleep()
Put the DRV8704 into sleep mode when the sleep pin is available.
Definition: drv8704.cpp:141
bool updateRegister(RegisterAddress address, uint16_t mask, uint16_t value)
Update selected register bits using a symbolic address.
Definition: drv8704.h:106
bool setShuntResistance(float ohms)
Set one shunt resistance value for both bridges in current mode.
Definition: drv8704_current.cpp:108
void reset()
Pulse the reset pin when available.
Definition: drv8704.cpp:154
DRV8704CurrentPresetConfig currentModePresetConfig() const
Return the preset configuration currently selected for current-limit operation.
Definition: drv8704.h:280
bool setSenseGain(SenseGain gain)
Set the current-sense amplifier gain.
Definition: drv8704_config.cpp:42
bool setCurrentModePreset(CurrentModePreset preset)
Select the predefined timing and decay preset used by current mode.
Definition: drv8704_current.cpp:122
void brake(BridgeId bridge)
Drive one bridge to brake.
Definition: drv8704_bridge.cpp:155
uint16_t cachedRegister(uint8_t address) const
Return the cached contents of a register.
Definition: drv8704.cpp:182
bool setOcpDeglitch(OcpDeglitch deglitch)
Set the overcurrent deglitch interval.
Definition: drv8704_config.cpp:107
uint16_t readRegister(uint8_t address)
Read a 12-bit register payload from the device.
Definition: drv8704_comm.cpp:9
bool setDecayMode(DecayMode mode)
Set the decay mode.
Definition: drv8704_config.cpp:83
void endPwmMode()
Disable platform PWM generation and return bridge inputs to coast.
Definition: drv8704_bridge.cpp:236
bool isFaultPinActive() const
Read the state of the optional FAULTn pin.
Definition: drv8704_status.cpp:23
bool disableCurrentLimit()
Disable the high-level current-limit configuration without forcing a bridge-state change.
Definition: drv8704_current.cpp:281
DRV8704BridgeState bridgeState(BridgeId bridge) const
Return the stored direction/runtime-state report for one bridge.
Definition: drv8704_bridge.cpp:186
Direction direction(BridgeId bridge) const
Return the configured high-level direction for one bridge.
Definition: drv8704_current.cpp:160
bool setDecayTime(uint8_t decayTime)
Set the mixed-decay transition time.
Definition: drv8704_config.cpp:91
DRV8704(const DRV8704Pins &pins, SPIClass &spi=SPI)
Construct a DRV8704 device instance.
Definition: drv8704.cpp:9
bool isCurrentLimitEnabled() const
Return whether a high-level current limit is currently configured.
Definition: drv8704.h:274
bool applyConfig(const DRV8704Config &config)
Apply a high-level configuration snapshot to the device.
Definition: drv8704_config.cpp:154
bool clearFault(FaultBit bit)
Clear one fault bit in the STATUS register.
Definition: drv8704_status.cpp:50
bool setSpeed(BridgeId bridge, Direction direction, float speedPercent)
Compatibility overload retained during the control-model refactor.
Definition: drv8704.h:388
bool isPwmModeEnabled() const
Return whether PWM mode is currently active.
Definition: drv8704.h:421
void end()
End driver operation.
Definition: drv8704.cpp:136
bool setPwmFrequency(uint32_t frequencyHz)
Update the requested PWM frequency.
Definition: drv8704_bridge.cpp:269
float smallestDutyIncrementPercent() const
Return the smallest duty-percent step of the active PWM backend.
Definition: drv8704.h:413
bool setDeadTime(DeadTime deadTime)
Set the bridge dead time.
Definition: drv8704_config.cpp:50
bool isInitialized() const
Report whether the driver completed initialization.
Definition: drv8704.h:433
bool syncRegisterCache()
Refresh the local register cache from device reads.
Definition: drv8704.cpp:190
bool hasFaultPin() const
Report whether a dedicated FAULTn pin was provided.
Definition: drv8704.h:304
void coast(BridgeId bridge)
Drive one bridge to coast.
Definition: drv8704_bridge.cpp:145
bool setCurrentLimit(float amps)
Program the chip-global current limit in amperes without changing bridge state.
Definition: drv8704_current.cpp:248
uint16_t readRegister(RegisterAddress address)
Read a register by symbolic address.
Definition: drv8704.h:68
DRV8704Status readStatus()
Read and decode the STATUS register.
Definition: drv8704_status.cpp:9
bool setDirection(BridgeId bridge, Direction direction)
Set the direction used by high-level current-drive and PWM-drive commands.
Definition: drv8704_current.cpp:148
bool setGateDrive(const GateDriveConfig &gateDrive)
Apply the complete DRIVE register configuration.
Definition: drv8704_config.cpp:133
bool clearFaults()
Clear all clearable fault bits in the STATUS register.
Definition: drv8704_status.cpp:42
DRV8704HealthCheck healthCheck()
Perform a minimal transport/status health check.
Definition: drv8704.cpp:165
bool setSpeed(BridgeId bridge, float speedPercent)
Apply an open-loop speed command to one bridge using hardware PWM.
Definition: drv8704_bridge.cpp:292
Constants, register addresses, masks, and defaults for the DRV8704 driver.
#define DRV8704_REGISTER_COUNT
Definition: drv8704_defs.h:35
Typed register views for the DRV8704 register set.
Public enums and data structures for the DRV8704 driver.
GateDriveSourceCurrent
Supported source current values for the predriver.
Definition: drv8704_types.h:76
FaultBit
Selectable fault bits in the STATUS register.
Definition: drv8704_types.h:155
OcpDeglitch
Supported OCP deglitch durations.
Definition: drv8704_types.h:56
Direction
High-level direction selector for current mode and PWM mode.
Definition: drv8704_types.h:124
CurrentModePreset
Predefined load presets for current-mode timing and decay behavior.
Definition: drv8704_types.h:144
RegisterAddress
Supported register addresses.
Definition: drv8704_types.h:167
GateDriveTime
Supported gate-drive timing values.
Definition: drv8704_types.h:66
GateDriveSinkCurrent
Supported sink current values for the predriver.
Definition: drv8704_types.h:86
DecayMode
Supported current-decay modes.
Definition: drv8704_types.h:36
OcpThreshold
Supported overcurrent thresholds.
Definition: drv8704_types.h:46
BridgeId
Bridge selector.
Definition: drv8704_types.h:96
DeadTime
Supported dead-time settings.
Definition: drv8704_types.h:26
SenseGain
Supported sense amplifier gain settings.
Definition: drv8704_types.h:16
BridgeMode
Runtime bridge command.
Definition: drv8704_types.h:104
Reported direction and runtime mode of one bridge.
Definition: drv8704_types.h:270
High-level DRV8704 configuration snapshot.
Definition: drv8704_types.h:225
Derived chip-global current-limit settings and per-bridge consequences.
Definition: drv8704_types.h:286
Timing and decay recommendation associated with a current-mode preset.
Definition: drv8704_types.h:250
Minimal transport/bring-up health result.
Definition: drv8704_types.h:378
Pin assignment bundle for a DRV8704 instance.
Definition: drv8704_types.h:181
int8_t faultPin
Definition: drv8704_types.h:185
Resolved platform PWM capabilities for the active backend.
Definition: drv8704_types.h:334
float smallestDutyIncrementPercent
Definition: drv8704_types.h:341
Requested PWM generator settings.
Definition: drv8704_types.h:322
Decoded device status and fault state.
Definition: drv8704_types.h:356
Gate-drive configuration group.
Definition: drv8704_types.h:205