drv2605l.c (5385B)
1 /* Copyright 2018 ishtob 2 * Driver for DRV2605L written for QMK 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "drv2605l.h" 19 #include "i2c_master.h" 20 #include <math.h> 21 22 uint8_t drv2605l_write_buffer[2]; 23 uint8_t drv2605l_read_buffer; 24 25 void drv2605l_write(uint8_t reg_addr, uint8_t data) { 26 drv2605l_write_buffer[0] = reg_addr; 27 drv2605l_write_buffer[1] = data; 28 i2c_transmit(DRV2605L_I2C_ADDRESS << 1, drv2605l_write_buffer, 2, 100); 29 } 30 31 uint8_t drv2605l_read(uint8_t reg_addr) { 32 i2c_read_register(DRV2605L_I2C_ADDRESS << 1, reg_addr, &drv2605l_read_buffer, 1, 100); 33 34 return drv2605l_read_buffer; 35 } 36 37 void drv2605l_init(void) { 38 i2c_init(); 39 /* 0x07 sets DRV2605 into calibration mode */ 40 drv2605l_write(DRV2605L_REG_MODE, 0x07); 41 42 // drv2605l_write(DRV2605L_REG_FEEDBACK_CTRL,0xB6); 43 44 #if DRV2605L_FB_ERM_LRA == 0 45 /* ERM settings */ 46 drv2605l_write(DRV2605L_REG_RATED_VOLTAGE, (DRV2605L_RATED_VOLTAGE / 21.33) * 1000); 47 # if DRV2605L_ERM_OPEN_LOOP == 0 48 drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, (((DRV2605L_V_PEAK * (DRV2605L_DRIVE_TIME + DRV2605L_BLANKING_TIME + DRV2605L_IDISS_TIME)) / 0.02133) / (DRV2605L_DRIVE_TIME - 0.0003))); 49 # elif DRV2605L_ERM_OPEN_LOOP == 1 50 drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, (DRV2605L_V_PEAK / 0.02196)); 51 # endif 52 #elif DRV2605L_FB_ERM_LRA == 1 53 drv2605l_write(DRV2605L_REG_RATED_VOLTAGE, ((DRV2605L_V_RMS * sqrt(1 - ((4 * ((150 + (DRV2605L_SAMPLE_TIME * 50)) * 0.000001)) + 0.0003) * DRV2605L_F_LRA) / 0.02071))); 54 # if DRV2605L_LRA_OPEN_LOOP == 0 55 drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, ((DRV2605L_V_PEAK / sqrt(1 - (DRV2605L_F_LRA * 0.0008)) / 0.02133))); 56 # elif DRV2605L_LRA_OPEN_LOOP == 1 57 drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, (DRV2605L_V_PEAK / 0.02196)); 58 # endif 59 #endif 60 61 drv2605l_reg_feedback_ctrl_t reg_feedback_ctrl; 62 reg_feedback_ctrl.bits.ERM_LRA = DRV2605L_FB_ERM_LRA; 63 reg_feedback_ctrl.bits.BRAKE_FACTOR = DRV2605L_FB_BRAKEFACTOR; 64 reg_feedback_ctrl.bits.LOOP_GAIN = DRV2605L_FB_LOOPGAIN; 65 reg_feedback_ctrl.bits.BEMF_GAIN = 0; /* auto-calibration populates this field*/ 66 drv2605l_write(DRV2605L_REG_FEEDBACK_CTRL, (uint8_t)reg_feedback_ctrl.raw); 67 68 drv2605l_reg_ctrl1_t reg_ctrl1; 69 reg_ctrl1.bits.C1_DRIVE_TIME = DRV2605L_DRIVE_TIME; 70 reg_ctrl1.bits.C1_AC_COUPLE = DRV2605L_AC_COUPLE; 71 reg_ctrl1.bits.C1_STARTUP_BOOST = DRV2605L_STARTUP_BOOST; 72 drv2605l_write(DRV2605L_REG_CTRL1, (uint8_t)reg_ctrl1.raw); 73 74 drv2605l_reg_ctrl2_t reg_ctrl2; 75 reg_ctrl2.bits.C2_BIDIR_INPUT = DRV2605L_BIDIR_INPUT; 76 reg_ctrl2.bits.C2_BRAKE_STAB = DRV2605L_BRAKE_STAB; 77 reg_ctrl2.bits.C2_SAMPLE_TIME = DRV2605L_SAMPLE_TIME; 78 reg_ctrl2.bits.C2_BLANKING_TIME = DRV2605L_BLANKING_TIME; 79 reg_ctrl2.bits.C2_IDISS_TIME = DRV2605L_IDISS_TIME; 80 drv2605l_write(DRV2605L_REG_CTRL2, (uint8_t)reg_ctrl2.raw); 81 82 drv2605l_reg_ctrl3_t reg_ctrl3; 83 reg_ctrl3.bits.C3_LRA_OPEN_LOOP = DRV2605L_LRA_OPEN_LOOP; 84 reg_ctrl3.bits.C3_N_PWM_ANALOG = DRV2605L_N_PWM_ANALOG; 85 reg_ctrl3.bits.C3_LRA_DRIVE_MODE = DRV2605L_LRA_DRIVE_MODE; 86 reg_ctrl3.bits.C3_DATA_FORMAT_RTO = DRV2605L_DATA_FORMAT_RTO; 87 reg_ctrl3.bits.C3_SUPPLY_COMP_DIS = DRV2605L_SUPPLY_COMP_DIS; 88 reg_ctrl3.bits.C3_ERM_OPEN_LOOP = DRV2605L_ERM_OPEN_LOOP; 89 reg_ctrl3.bits.C3_NG_THRESH = DRV2605L_NG_THRESH; 90 drv2605l_write(DRV2605L_REG_CTRL3, (uint8_t)reg_ctrl3.raw); 91 92 drv2605l_reg_ctrl4_t reg_ctrl4; 93 reg_ctrl4.bits.C4_ZC_DET_TIME = DRV2605L_ZC_DET_TIME; 94 reg_ctrl4.bits.C4_AUTO_CAL_TIME = DRV2605L_AUTO_CAL_TIME; 95 drv2605l_write(DRV2605L_REG_CTRL4, (uint8_t)reg_ctrl4.raw); 96 97 drv2605l_write(DRV2605L_REG_LIBRARY_SELECTION, DRV2605L_LIBRARY); 98 99 drv2605l_write(DRV2605L_REG_GO, 0x01); 100 101 /* 0x00 sets DRV2605 out of standby and to use internal trigger 102 * 0x01 sets DRV2605 out of standby and to use external trigger */ 103 drv2605l_write(DRV2605L_REG_MODE, 0x00); 104 105 // Play greeting sequence 106 drv2605l_write(DRV2605L_REG_GO, 0x00); 107 drv2605l_write(DRV2605L_REG_WAVEFORM_SEQUENCER_1, DRV2605L_GREETING); 108 drv2605l_write(DRV2605L_REG_GO, 0x01); 109 } 110 111 void drv2605l_rtp_init(void) { 112 drv2605l_write(DRV2605L_REG_GO, 0x00); 113 drv2605l_write(DRV2605L_REG_RTP_INPUT, 20); // 20 is the lowest value I've found where haptics can still be felt. 114 drv2605l_write(DRV2605L_REG_MODE, 0x05); 115 drv2605l_write(DRV2605L_REG_GO, 0x01); 116 } 117 118 void drv2605l_amplitude(uint8_t amplitude) { 119 drv2605l_write(DRV2605L_REG_RTP_INPUT, amplitude); 120 } 121 122 void drv2605l_pulse(uint8_t sequence) { 123 drv2605l_write(DRV2605L_REG_GO, 0x00); 124 drv2605l_write(DRV2605L_REG_WAVEFORM_SEQUENCER_1, sequence); 125 drv2605l_write(DRV2605L_REG_GO, 0x01); 126 }