Library for TI's DRV 2605
DRV2605.cpp@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 | #include "DRV2605.h" |
takuhachisu | 0:d47ac72f4048 | 2 | |
takuhachisu | 0:d47ac72f4048 | 3 | DRV2605::DRV2605(I2C &i2c, bool isERM) |
takuhachisu | 0:d47ac72f4048 | 4 | { |
takuhachisu | 0:d47ac72f4048 | 5 | wait_us(250); // Device datasheet specified wait time before I2C |
takuhachisu | 0:d47ac72f4048 | 6 | _i2c = &i2c; |
takuhachisu | 0:d47ac72f4048 | 7 | |
takuhachisu | 0:d47ac72f4048 | 8 | // comms should be used |
takuhachisu | 0:d47ac72f4048 | 9 | // Exit Standby Mode; Enter Auto- Cal Mode |
takuhachisu | 0:d47ac72f4048 | 10 | mode(Mode_AUTO_CAL); |
takuhachisu | 0:d47ac72f4048 | 11 | |
takuhachisu | 0:d47ac72f4048 | 12 | /* Set the following registers to the appropriate values: |
takuhachisu | 0:d47ac72f4048 | 13 | • Rated Voltage (0x16) |
takuhachisu | 0:d47ac72f4048 | 14 | • Overdrive Voltage (0x17) |
takuhachisu | 0:d47ac72f4048 | 15 | • Feedback Control (0x1A) – Bits [1:0] can be left blank and will be |
takuhachisu | 0:d47ac72f4048 | 16 | populated by the auto-calibration engine |
takuhachisu | 0:d47ac72f4048 | 17 | • Control 1 (0x1B), Control 2 (0x1C), and Control 3 (0x1D) |
takuhachisu | 0:d47ac72f4048 | 18 | • Mode (0x01) – Set mode to Auto-Calibration |
takuhachisu | 0:d47ac72f4048 | 19 | • Auto-calibration Memory Interface (0x1E) – the auto-calibration time |
takuhachisu | 0:d47ac72f4048 | 20 | can be increased to improve calibration, but can be left as default |
takuhachisu | 0:d47ac72f4048 | 21 | for the initial calibration |
takuhachisu | 0:d47ac72f4048 | 22 | */ |
takuhachisu | 0:d47ac72f4048 | 23 | |
takuhachisu | 0:d47ac72f4048 | 24 | // Rated Voltage (0x16), use default values |
takuhachisu | 0:d47ac72f4048 | 25 | |
takuhachisu | 0:d47ac72f4048 | 26 | // Calc and Set Overdrive Voltage Register (0x17), use default values |
takuhachisu | 0:d47ac72f4048 | 27 | |
takuhachisu | 0:d47ac72f4048 | 28 | // Set Feedback Control Register (0x1A) |
takuhachisu | 0:d47ac72f4048 | 29 | if(isERM) |
takuhachisu | 0:d47ac72f4048 | 30 | i2cWriteByte(FEEDBACK_CONTROL, i2cReadByte(FEEDBACK_CONTROL) | Actuator_ERM); // set ERM |
takuhachisu | 0:d47ac72f4048 | 31 | else |
takuhachisu | 0:d47ac72f4048 | 32 | i2cWriteByte(FEEDBACK_CONTROL, i2cReadByte(FEEDBACK_CONTROL) | Actuator_LRA); // set LRA |
takuhachisu | 0:d47ac72f4048 | 33 | |
takuhachisu | 0:d47ac72f4048 | 34 | // Set Control 1 Register (0x1B), use default values |
takuhachisu | 0:d47ac72f4048 | 35 | |
takuhachisu | 0:d47ac72f4048 | 36 | // Set Control 2 Register (0x1C), use default values |
takuhachisu | 0:d47ac72f4048 | 37 | |
takuhachisu | 0:d47ac72f4048 | 38 | // Set Control 3 Register (0x1D), set data format for RTP unsinged |
takuhachisu | 0:d47ac72f4048 | 39 | i2cWriteByte(CONTROL3, i2cReadByte(CONTROL3) | 0x08); // set unsigned |
takuhachisu | 0:d47ac72f4048 | 40 | |
takuhachisu | 0:d47ac72f4048 | 41 | // Set Control 4 Register (0x1E), use default values |
takuhachisu | 0:d47ac72f4048 | 42 | |
takuhachisu | 0:d47ac72f4048 | 43 | // Device already set to Auto- Cal Mode at top of this code block |
takuhachisu | 0:d47ac72f4048 | 44 | |
takuhachisu | 0:d47ac72f4048 | 45 | if(isERM){ |
takuhachisu | 0:d47ac72f4048 | 46 | // Start auto- calibration |
takuhachisu | 0:d47ac72f4048 | 47 | i2cWriteByte(GO, 0x01); |
takuhachisu | 0:d47ac72f4048 | 48 | // Wait for calibration to complete |
takuhachisu | 0:d47ac72f4048 | 49 | while(i2cReadByte(GO)); |
takuhachisu | 0:d47ac72f4048 | 50 | } |
takuhachisu | 0:d47ac72f4048 | 51 | } |
takuhachisu | 0:d47ac72f4048 | 52 | |
takuhachisu | 0:d47ac72f4048 | 53 | void DRV2605::i2cWriteByte(char reg, char value) |
takuhachisu | 0:d47ac72f4048 | 54 | { |
takuhachisu | 0:d47ac72f4048 | 55 | char buff[2] = {reg, value}; |
takuhachisu | 0:d47ac72f4048 | 56 | _i2c->write(DRV2605_SLAVE_ADDR<<1, buff, 2); |
takuhachisu | 0:d47ac72f4048 | 57 | } |
takuhachisu | 0:d47ac72f4048 | 58 | |
takuhachisu | 0:d47ac72f4048 | 59 | uint8_t DRV2605::i2cReadByte(char reg) |
takuhachisu | 0:d47ac72f4048 | 60 | { |
takuhachisu | 0:d47ac72f4048 | 61 | char result; // Temp result storage |
takuhachisu | 0:d47ac72f4048 | 62 | _i2c->write(DRV2605_SLAVE_ADDR<<1, ®, 1, true); |
takuhachisu | 0:d47ac72f4048 | 63 | _i2c->read(DRV2605_SLAVE_ADDR<<1, &result, 1); |
takuhachisu | 0:d47ac72f4048 | 64 | |
takuhachisu | 0:d47ac72f4048 | 65 | return result; |
takuhachisu | 0:d47ac72f4048 | 66 | } |
takuhachisu | 0:d47ac72f4048 | 67 | |
takuhachisu | 0:d47ac72f4048 | 68 | void DRV2605::mode(char mode) |
takuhachisu | 0:d47ac72f4048 | 69 | { |
takuhachisu | 0:d47ac72f4048 | 70 | i2cWriteByte(MODE, mode); |
takuhachisu | 0:d47ac72f4048 | 71 | } |
takuhachisu | 0:d47ac72f4048 | 72 | |
takuhachisu | 0:d47ac72f4048 | 73 | uint8_t DRV2605::diagnostics() |
takuhachisu | 0:d47ac72f4048 | 74 | { |
takuhachisu | 0:d47ac72f4048 | 75 | mode(Mode_DIAG); |
takuhachisu | 0:d47ac72f4048 | 76 | i2cWriteByte(GO, 1); |
takuhachisu | 0:d47ac72f4048 | 77 | while(i2cReadByte(GO)); // Wait for GO bit to clear |
takuhachisu | 0:d47ac72f4048 | 78 | |
takuhachisu | 0:d47ac72f4048 | 79 | return i2cReadByte(STATUS); // Return Status Reg Value |
takuhachisu | 0:d47ac72f4048 | 80 | } |
takuhachisu | 0:d47ac72f4048 | 81 | |
takuhachisu | 0:d47ac72f4048 | 82 | void DRV2605::setLibrary(char lib) |
takuhachisu | 0:d47ac72f4048 | 83 | { |
takuhachisu | 0:d47ac72f4048 | 84 | i2cWriteByte(LIBRARY_SELECTION, lib); // Select ROM Library |
takuhachisu | 0:d47ac72f4048 | 85 | } |
takuhachisu | 0:d47ac72f4048 | 86 | |
takuhachisu | 0:d47ac72f4048 | 87 | void DRV2605::setWaveform(int effect) |
takuhachisu | 0:d47ac72f4048 | 88 | { |
takuhachisu | 0:d47ac72f4048 | 89 | i2cWriteByte(WAVEFORM_SEQUENCER_1, effect); // Load waveform index to play |
takuhachisu | 0:d47ac72f4048 | 90 | i2cWriteByte(WAVEFORM_SEQUENCER_2, 0); // Insert stop condition so we don't play other registers if filled |
takuhachisu | 0:d47ac72f4048 | 91 | } |
takuhachisu | 0:d47ac72f4048 | 92 | |
takuhachisu | 0:d47ac72f4048 | 93 | void DRV2605::setWaveformSequence(int effect1, int effect2, |
takuhachisu | 0:d47ac72f4048 | 94 | int effect3, int effect4, |
takuhachisu | 0:d47ac72f4048 | 95 | int effect5, int effect6, |
takuhachisu | 0:d47ac72f4048 | 96 | int effect7, int effect8) |
takuhachisu | 0:d47ac72f4048 | 97 | { |
takuhachisu | 0:d47ac72f4048 | 98 | i2cWriteByte(WAVEFORM_SEQUENCER_1, effect1); |
takuhachisu | 0:d47ac72f4048 | 99 | i2cWriteByte(WAVEFORM_SEQUENCER_2, effect2); |
takuhachisu | 0:d47ac72f4048 | 100 | i2cWriteByte(WAVEFORM_SEQUENCER_3, effect3); |
takuhachisu | 0:d47ac72f4048 | 101 | i2cWriteByte(WAVEFORM_SEQUENCER_4, effect4); |
takuhachisu | 0:d47ac72f4048 | 102 | i2cWriteByte(WAVEFORM_SEQUENCER_5, effect5); |
takuhachisu | 0:d47ac72f4048 | 103 | i2cWriteByte(WAVEFORM_SEQUENCER_6, effect6); |
takuhachisu | 0:d47ac72f4048 | 104 | i2cWriteByte(WAVEFORM_SEQUENCER_7, effect7); |
takuhachisu | 0:d47ac72f4048 | 105 | i2cWriteByte(WAVEFORM_SEQUENCER_8, effect8); |
takuhachisu | 0:d47ac72f4048 | 106 | } |
takuhachisu | 0:d47ac72f4048 | 107 | |
takuhachisu | 0:d47ac72f4048 | 108 | void DRV2605::play() |
takuhachisu | 0:d47ac72f4048 | 109 | { |
takuhachisu | 0:d47ac72f4048 | 110 | i2cWriteByte(GO, 1); |
takuhachisu | 0:d47ac72f4048 | 111 | } |
takuhachisu | 0:d47ac72f4048 | 112 | |
takuhachisu | 0:d47ac72f4048 | 113 | void DRV2605::stop() |
takuhachisu | 0:d47ac72f4048 | 114 | { |
takuhachisu | 0:d47ac72f4048 | 115 | i2cWriteByte(GO, 0); |
takuhachisu | 0:d47ac72f4048 | 116 | } |
takuhachisu | 0:d47ac72f4048 | 117 | |
takuhachisu | 0:d47ac72f4048 | 118 | void DRV2605::playRtp(char rtp) |
takuhachisu | 0:d47ac72f4048 | 119 | { |
takuhachisu | 0:d47ac72f4048 | 120 | i2cWriteByte(REAL_TIME_PLAYBACK, rtp); |
takuhachisu | 0:d47ac72f4048 | 121 | } |
takuhachisu | 0:d47ac72f4048 | 122 | |
takuhachisu | 0:d47ac72f4048 | 123 | float DRV2605::battery_voltage(void) |
takuhachisu | 0:d47ac72f4048 | 124 | { |
takuhachisu | 0:d47ac72f4048 | 125 | return (float)i2cReadByte(VBAT_VOLTAGE_MONITOR) / 255.0 * 5.6; |
takuhachisu | 0:d47ac72f4048 | 126 | } |