Library for TI's DRV 2605
DRV2605.h@0:d47ac72f4048, 2017-11-10 (annotated)
- Committer:
- takuhachisu
- Date:
- Fri Nov 10 09:34:15 2017 +0000
- Revision:
- 0:d47ac72f4048
- Child:
- 1:0747096351be
Library for DRV2605
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuhachisu | 0:d47ac72f4048 | 1 | #ifndef DRV2605_H |
takuhachisu | 0:d47ac72f4048 | 2 | #define DRV2605_H |
takuhachisu | 0:d47ac72f4048 | 3 | |
takuhachisu | 0:d47ac72f4048 | 4 | #include "mbed.h" |
takuhachisu | 0:d47ac72f4048 | 5 | |
takuhachisu | 0:d47ac72f4048 | 6 | /** Modifined from Bryce WilliamsBryce Williams's code |
takuhachisu | 0:d47ac72f4048 | 7 | * References: |
takuhachisu | 0:d47ac72f4048 | 8 | * http://www.ti.com/product/DRV2605L/description&lpos=Middle_Container&lid=Alternative_Devices |
takuhachisu | 0:d47ac72f4048 | 9 | * http://www.ti.com/lit/ds/symlink/drv2605l.pdf (Datasheet) |
takuhachisu | 0:d47ac72f4048 | 10 | * http://www.ti.com/lit/an/sloa189/sloa189.pdf (Setup Guide; SLOA189) |
takuhachisu | 0:d47ac72f4048 | 11 | * https://developer.mbed.org/users/electromotivated/code/DRV2605/ |
takuhachisu | 0:d47ac72f4048 | 12 | */ |
takuhachisu | 0:d47ac72f4048 | 13 | |
takuhachisu | 0:d47ac72f4048 | 14 | // DRV2605 Slave Address |
takuhachisu | 0:d47ac72f4048 | 15 | #define DRV2605_SLAVE_ADDR 0x5A |
takuhachisu | 0:d47ac72f4048 | 16 | |
takuhachisu | 0:d47ac72f4048 | 17 | // DRV2605 Registers |
takuhachisu | 0:d47ac72f4048 | 18 | #define STATUS 0x00 |
takuhachisu | 0:d47ac72f4048 | 19 | #define MODE 0x01 |
takuhachisu | 0:d47ac72f4048 | 20 | #define REAL_TIME_PLAYBACK 0x02 |
takuhachisu | 0:d47ac72f4048 | 21 | #define LIBRARY_SELECTION 0x03 |
takuhachisu | 0:d47ac72f4048 | 22 | #define WAVEFORM_SEQUENCER_1 0x04 |
takuhachisu | 0:d47ac72f4048 | 23 | #define WAVEFORM_SEQUENCER_2 0x05 |
takuhachisu | 0:d47ac72f4048 | 24 | #define WAVEFORM_SEQUENCER_3 0x06 |
takuhachisu | 0:d47ac72f4048 | 25 | #define WAVEFORM_SEQUENCER_4 0x07 |
takuhachisu | 0:d47ac72f4048 | 26 | #define WAVEFORM_SEQUENCER_5 0x08 |
takuhachisu | 0:d47ac72f4048 | 27 | #define WAVEFORM_SEQUENCER_6 0x09 |
takuhachisu | 0:d47ac72f4048 | 28 | #define WAVEFORM_SEQUENCER_7 0x0A |
takuhachisu | 0:d47ac72f4048 | 29 | #define WAVEFORM_SEQUENCER_8 0x0B |
takuhachisu | 0:d47ac72f4048 | 30 | #define GO 0x0C |
takuhachisu | 0:d47ac72f4048 | 31 | #define OVERDRIVE_TIME_OFFSET 0x0D |
takuhachisu | 0:d47ac72f4048 | 32 | #define POSITIVE_SUSTAIN_TIME_OFFSET 0x0E |
takuhachisu | 0:d47ac72f4048 | 33 | #define NEGATIVE_SUSTAIN_TIME_OFFSET 0x0F |
takuhachisu | 0:d47ac72f4048 | 34 | #define BRAKE_TIME_OFFSET 0x10 |
takuhachisu | 0:d47ac72f4048 | 35 | #define AUDIO_TO_VIBE_CONTROL 0x11 |
takuhachisu | 0:d47ac72f4048 | 36 | #define AUDIO_TO_VIBE_MINIMUM_INPUT_LEVEL 0x12 |
takuhachisu | 0:d47ac72f4048 | 37 | #define AUDIO_TO_VIBE_MAXIMUM_INPUT_LEVEL 0x13 |
takuhachisu | 0:d47ac72f4048 | 38 | #define AUDIO_TO_VIBE_MINIMUM_OUPUT_DRIVE 0x14 |
takuhachisu | 0:d47ac72f4048 | 39 | #define AUDIO_TO_VIBE_MAXIMUM_OUTPUT_DRIVE 0x15 |
takuhachisu | 0:d47ac72f4048 | 40 | #define RATED_VOLTAGE 0x16 |
takuhachisu | 0:d47ac72f4048 | 41 | #define OVERDRIVE_CLAMP_VOLTAGE 0x17 |
takuhachisu | 0:d47ac72f4048 | 42 | #define AUTO_CALIBRATION_COMPENSATION_RESULT 0x18 |
takuhachisu | 0:d47ac72f4048 | 43 | #define AUTO_CALIBRATION_BACK_EMF_RESULT 0x19 |
takuhachisu | 0:d47ac72f4048 | 44 | #define FEEDBACK_CONTROL 0x1A |
takuhachisu | 0:d47ac72f4048 | 45 | #define CONTROL 0x1B |
takuhachisu | 0:d47ac72f4048 | 46 | #define CONTROL2 0x1C |
takuhachisu | 0:d47ac72f4048 | 47 | #define CONTROL3 0x1D |
takuhachisu | 0:d47ac72f4048 | 48 | #define CONTROL4 0x1E |
takuhachisu | 0:d47ac72f4048 | 49 | #define CONTROL5 0x1F |
takuhachisu | 0:d47ac72f4048 | 50 | #define LRA_OPEN_LOOP_PERIOD 0x20 |
takuhachisu | 0:d47ac72f4048 | 51 | #define VBAT_VOLTAGE_MONITOR 0x21 |
takuhachisu | 0:d47ac72f4048 | 52 | #define LRA_RESONANCE_PERIOD 0x22 |
takuhachisu | 0:d47ac72f4048 | 53 | |
takuhachisu | 0:d47ac72f4048 | 54 | // Modes defines the possible modes of the DRV2605L (Register Addr: 0x01) |
takuhachisu | 0:d47ac72f4048 | 55 | #define Mode_INTERNAL_TRIG 0x00 // Waveforms fired by Setting GO bit in Register 0x0C |
takuhachisu | 0:d47ac72f4048 | 56 | #define Mode_EXTERNAL_EDGE 0x01 // Rising Edge on IN/TRIG pin set GO Bit. |
takuhachisu | 0:d47ac72f4048 | 57 | #define Mode_EXTERNAL_LEVEL 0x02 // GO bit follows state of edge on IN/TRIG pin. |
takuhachisu | 0:d47ac72f4048 | 58 | #define Mode_PWM_ANALOG 0x03 // PWM or Analog Signal accepted at IN/TRIG pin. |
takuhachisu | 0:d47ac72f4048 | 59 | #define Mode_AUDIO_TO_VIBE 0x04 // An AC-coupled audio signal is accepted at the IN/TRIG pin. |
takuhachisu | 0:d47ac72f4048 | 60 | #define Mode_RTP 0x05 // Real- Time Playback |
takuhachisu | 0:d47ac72f4048 | 61 | #define Mode_DIAG 0x06 // Set to perform actuator diagnostics |
takuhachisu | 0:d47ac72f4048 | 62 | #define Mode_AUTO_CAL 0x07 // Set to perform auto calibration of device for actuator |
takuhachisu | 0:d47ac72f4048 | 63 | #define Mode_STANDBY 0x40 // Set Device to Software Standby (Low- Power Mode) |
takuhachisu | 0:d47ac72f4048 | 64 | #define Mode_RESET 0x80 // Reset Device (equivalent of power cycling the device) |
takuhachisu | 0:d47ac72f4048 | 65 | |
takuhachisu | 0:d47ac72f4048 | 66 | // FeedBack_Controls Fields Bitmasks (Register Addr: 0x1A) |
takuhachisu | 0:d47ac72f4048 | 67 | #define Actuator_ERM 0x00 |
takuhachisu | 0:d47ac72f4048 | 68 | #define Actuator_LRA 0x80 |
takuhachisu | 0:d47ac72f4048 | 69 | #define Brake_FACTORTx1 0x00 |
takuhachisu | 0:d47ac72f4048 | 70 | #define Brake_FACTORTx2 0x10 |
takuhachisu | 0:d47ac72f4048 | 71 | #define Brake_FACTORTx3 0x20 |
takuhachisu | 0:d47ac72f4048 | 72 | #define Brake_FACTORTx4 0x40 |
takuhachisu | 0:d47ac72f4048 | 73 | #define Brake_FACTORTx6 0x80 |
takuhachisu | 0:d47ac72f4048 | 74 | #define Brake_FACTORTx8 0x50 |
takuhachisu | 0:d47ac72f4048 | 75 | #define Brake_FACTORTx16 0x60 |
takuhachisu | 0:d47ac72f4048 | 76 | #define Brake_FACTORTxDISABLE 0x70 |
takuhachisu | 0:d47ac72f4048 | 77 | #define Loop_GainLOW 0x00 |
takuhachisu | 0:d47ac72f4048 | 78 | #define Loop_GainMED 0x04 |
takuhachisu | 0:d47ac72f4048 | 79 | #define Loop_GainHIGH 0x08 |
takuhachisu | 0:d47ac72f4048 | 80 | #define Loop_GainVERYHIGH 0x0C |
takuhachisu | 0:d47ac72f4048 | 81 | |
takuhachisu | 0:d47ac72f4048 | 82 | // ROM Waveform Library Selections |
takuhachisu | 0:d47ac72f4048 | 83 | #define LibraryEMPTY 0x00 |
takuhachisu | 0:d47ac72f4048 | 84 | #define LibraryA 0x01 |
takuhachisu | 0:d47ac72f4048 | 85 | #define LibraryB 0x02 |
takuhachisu | 0:d47ac72f4048 | 86 | #define LibraryC 0x03 |
takuhachisu | 0:d47ac72f4048 | 87 | #define LibraryD 0x04 |
takuhachisu | 0:d47ac72f4048 | 88 | #define LibraryE 0x05 |
takuhachisu | 0:d47ac72f4048 | 89 | #define LibraryLRA 0x06 |
takuhachisu | 0:d47ac72f4048 | 90 | #define LibraryF 0x07 |
takuhachisu | 0:d47ac72f4048 | 91 | |
takuhachisu | 0:d47ac72f4048 | 92 | |
takuhachisu | 0:d47ac72f4048 | 93 | /** Library for the TI DRV2605 |
takuhachisu | 0:d47ac72f4048 | 94 | * |
takuhachisu | 0:d47ac72f4048 | 95 | */ |
takuhachisu | 0:d47ac72f4048 | 96 | |
takuhachisu | 0:d47ac72f4048 | 97 | class DRV2605 |
takuhachisu | 0:d47ac72f4048 | 98 | { |
takuhachisu | 0:d47ac72f4048 | 99 | |
takuhachisu | 0:d47ac72f4048 | 100 | public: |
takuhachisu | 0:d47ac72f4048 | 101 | /** |
takuhachisu | 0:d47ac72f4048 | 102 | * Create a DRV2605 object |
takuhachisu | 0:d47ac72f4048 | 103 | * |
takuhachisu | 0:d47ac72f4048 | 104 | * @param &i2c pointer of I2C object |
takuhachisu | 0:d47ac72f4048 | 105 | * @param isERM Set the actuator type |
takuhachisu | 0:d47ac72f4048 | 106 | */ |
takuhachisu | 0:d47ac72f4048 | 107 | DRV2605(I2C &i2c, bool isERM); |
takuhachisu | 0:d47ac72f4048 | 108 | |
takuhachisu | 0:d47ac72f4048 | 109 | /** |
takuhachisu | 0:d47ac72f4048 | 110 | * Write value to specified register of device |
takuhachisu | 0:d47ac72f4048 | 111 | * |
takuhachisu | 0:d47ac72f4048 | 112 | * @param reg The device register to write |
takuhachisu | 0:d47ac72f4048 | 113 | * @param value The value to write to the register |
takuhachisu | 0:d47ac72f4048 | 114 | */ |
takuhachisu | 0:d47ac72f4048 | 115 | void i2cWriteByte(char reg, char value); |
takuhachisu | 0:d47ac72f4048 | 116 | |
takuhachisu | 0:d47ac72f4048 | 117 | /** |
takuhachisu | 0:d47ac72f4048 | 118 | * Read value from register of device |
takuhachisu | 0:d47ac72f4048 | 119 | * @param reg The device register to read |
takuhachisu | 0:d47ac72f4048 | 120 | * @return The result |
takuhachisu | 0:d47ac72f4048 | 121 | */ |
takuhachisu | 0:d47ac72f4048 | 122 | uint8_t i2cReadByte(char reg); |
takuhachisu | 0:d47ac72f4048 | 123 | |
takuhachisu | 0:d47ac72f4048 | 124 | /** |
takuhachisu | 0:d47ac72f4048 | 125 | * Place device into specified mode |
takuhachisu | 0:d47ac72f4048 | 126 | * @param mode The mode to place device into |
takuhachisu | 0:d47ac72f4048 | 127 | */ |
takuhachisu | 0:d47ac72f4048 | 128 | void mode(char mode); |
takuhachisu | 0:d47ac72f4048 | 129 | |
takuhachisu | 0:d47ac72f4048 | 130 | /** |
takuhachisu | 0:d47ac72f4048 | 131 | * Runs diagnostics on the Actuator and Device and returns the results. |
takuhachisu | 0:d47ac72f4048 | 132 | * The results indicate if an actuator is detected, over- current events, |
takuhachisu | 0:d47ac72f4048 | 133 | * etc. Refer to STATUS Register (0x00) in device datasheet for more |
takuhachisu | 0:d47ac72f4048 | 134 | * description register values. |
takuhachisu | 0:d47ac72f4048 | 135 | * |
takuhachisu | 0:d47ac72f4048 | 136 | * Note: This should be run if the user is having trouble getting the actuator |
takuhachisu | 0:d47ac72f4048 | 137 | * to work. |
takuhachisu | 0:d47ac72f4048 | 138 | * |
takuhachisu | 0:d47ac72f4048 | 139 | * @return The results of the diagnostics (i.e. Status Reg (0x00)) |
takuhachisu | 0:d47ac72f4048 | 140 | */ |
takuhachisu | 0:d47ac72f4048 | 141 | uint8_t diagnostics(); |
takuhachisu | 0:d47ac72f4048 | 142 | |
takuhachisu | 0:d47ac72f4048 | 143 | |
takuhachisu | 0:d47ac72f4048 | 144 | /** |
takuhachisu | 0:d47ac72f4048 | 145 | * Set ROM Library |
takuhachisu | 0:d47ac72f4048 | 146 | * |
takuhachisu | 0:d47ac72f4048 | 147 | * Note: This should be run before setWaveform function |
takuhachisu | 0:d47ac72f4048 | 148 | * @param lib Set library (see library's macro) |
takuhachisu | 0:d47ac72f4048 | 149 | */ |
takuhachisu | 0:d47ac72f4048 | 150 | void setLibrary(char lib); |
takuhachisu | 0:d47ac72f4048 | 151 | |
takuhachisu | 0:d47ac72f4048 | 152 | /** |
takuhachisu | 0:d47ac72f4048 | 153 | * Play single waveform from ROM Library |
takuhachisu | 0:d47ac72f4048 | 154 | * |
takuhachisu | 0:d47ac72f4048 | 155 | * Note: This should be run after setLibrary function |
takuhachisu | 0:d47ac72f4048 | 156 | * @param effect The Waveform Effect Library Index value to play |
takuhachisu | 0:d47ac72f4048 | 157 | * (valid values are 1 to 123) |
takuhachisu | 0:d47ac72f4048 | 158 | */ |
takuhachisu | 0:d47ac72f4048 | 159 | void setWaveform(int effect); |
takuhachisu | 0:d47ac72f4048 | 160 | |
takuhachisu | 0:d47ac72f4048 | 161 | /** |
takuhachisu | 0:d47ac72f4048 | 162 | * Load Wave Sequence into DRV2605 Sequence Registers |
takuhachisu | 0:d47ac72f4048 | 163 | * |
takuhachisu | 0:d47ac72f4048 | 164 | * Note: This should be run before setWaveform function |
takuhachisu | 0:d47ac72f4048 | 165 | * @param effect1... effect8 The effect to play. Valid inputs are |
takuhachisu | 0:d47ac72f4048 | 166 | * 0 to 123; |
takuhachisu | 0:d47ac72f4048 | 167 | * 0: Stop Condition, |
takuhachisu | 0:d47ac72f4048 | 168 | * 1- 123: Waveform Index |
takuhachisu | 0:d47ac72f4048 | 169 | */ |
takuhachisu | 0:d47ac72f4048 | 170 | void setWaveformSequence(int effect1 = 0, int effect2 = 0, |
takuhachisu | 0:d47ac72f4048 | 171 | int effect3 = 0, int effect4 = 0, |
takuhachisu | 0:d47ac72f4048 | 172 | int effect5 = 0, int effect6 = 0, |
takuhachisu | 0:d47ac72f4048 | 173 | int effect7 = 0, int effect8 = 0); |
takuhachisu | 0:d47ac72f4048 | 174 | |
takuhachisu | 0:d47ac72f4048 | 175 | /** |
takuhachisu | 0:d47ac72f4048 | 176 | * Plays the currently loaded waveform or waveform sequence. |
takuhachisu | 0:d47ac72f4048 | 177 | */ |
takuhachisu | 0:d47ac72f4048 | 178 | void play(); |
takuhachisu | 0:d47ac72f4048 | 179 | |
takuhachisu | 0:d47ac72f4048 | 180 | /** |
takuhachisu | 0:d47ac72f4048 | 181 | * Stops the currently loaded waveform or waveform sequence. |
takuhachisu | 0:d47ac72f4048 | 182 | */ |
takuhachisu | 0:d47ac72f4048 | 183 | void stop(); |
takuhachisu | 0:d47ac72f4048 | 184 | |
takuhachisu | 0:d47ac72f4048 | 185 | /** |
takuhachisu | 0:d47ac72f4048 | 186 | * Real time play back |
takuhachisu | 0:d47ac72f4048 | 187 | * |
takuhachisu | 0:d47ac72f4048 | 188 | * @param rtp intensity of the vibration (0 - 255) |
takuhachisu | 0:d47ac72f4048 | 189 | */ |
takuhachisu | 0:d47ac72f4048 | 190 | void playRtp(char rtp); |
takuhachisu | 0:d47ac72f4048 | 191 | |
takuhachisu | 0:d47ac72f4048 | 192 | /** |
takuhachisu | 0:d47ac72f4048 | 193 | TODO: Add Closed Loop Calibration |
takuhachisu | 0:d47ac72f4048 | 194 | |
takuhachisu | 0:d47ac72f4048 | 195 | Run basic DRV2605L Auto- Calibration as detailed in Section 2 of |
takuhachisu | 0:d47ac72f4048 | 196 | the Device Setup Guide for OPEN- LOOP ONLY. |
takuhachisu | 0:d47ac72f4048 | 197 | This must be done before using the device in closed- loop mode |
takuhachisu | 0:d47ac72f4048 | 198 | (unless cal has been done before with values stored in non-volatile |
takuhachisu | 0:d47ac72f4048 | 199 | mem; see datasheet for more info). |
takuhachisu | 0:d47ac72f4048 | 200 | |
takuhachisu | 0:d47ac72f4048 | 201 | NOTE: It is NOT recommended to store cal values into device |
takuhachisu | 0:d47ac72f4048 | 202 | non-volatile memory as this can be done only once. Thus do not |
takuhachisu | 0:d47ac72f4048 | 203 | use this feature unless the device is being used in a final |
takuhachisu | 0:d47ac72f4048 | 204 | project AND values have been confirmed to result in satisfactory |
takuhachisu | 0:d47ac72f4048 | 205 | performance). |
takuhachisu | 0:d47ac72f4048 | 206 | |
takuhachisu | 0:d47ac72f4048 | 207 | This uses many of the default device register values such as |
takuhachisu | 0:d47ac72f4048 | 208 | the default DRIVE_TIME . |
takuhachisu | 0:d47ac72f4048 | 209 | |
takuhachisu | 0:d47ac72f4048 | 210 | @param actuator_peak_voltage The maximum/peak voltage rating of the actuator |
takuhachisu | 0:d47ac72f4048 | 211 | */ |
takuhachisu | 0:d47ac72f4048 | 212 | uint8_t auto_cal_open_loop(float actuator_peak_voltage); |
takuhachisu | 0:d47ac72f4048 | 213 | |
takuhachisu | 0:d47ac72f4048 | 214 | /** |
takuhachisu | 0:d47ac72f4048 | 215 | * Battery voltage monitor |
takuhachisu | 0:d47ac72f4048 | 216 | * |
takuhachisu | 0:d47ac72f4048 | 217 | * @return battery voltage |
takuhachisu | 0:d47ac72f4048 | 218 | */ |
takuhachisu | 0:d47ac72f4048 | 219 | float battery_voltage(void); |
takuhachisu | 0:d47ac72f4048 | 220 | |
takuhachisu | 0:d47ac72f4048 | 221 | private: |
takuhachisu | 0:d47ac72f4048 | 222 | I2C *_i2c; |
takuhachisu | 0:d47ac72f4048 | 223 | }; |
takuhachisu | 0:d47ac72f4048 | 224 | |
takuhachisu | 0:d47ac72f4048 | 225 | #endif |