logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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. #include "drv2605l.h"
  18. #include "i2c_master.h"
  19. #include <math.h>
  20. uint8_t drv2605l_write_buffer[2];
  21. uint8_t drv2605l_read_buffer;
  22. void drv2605l_write(uint8_t reg_addr, uint8_t data) {
  23. drv2605l_write_buffer[0] = reg_addr;
  24. drv2605l_write_buffer[1] = data;
  25. i2c_transmit(DRV2605L_I2C_ADDRESS << 1, drv2605l_write_buffer, 2, 100);
  26. }
  27. uint8_t drv2605l_read(uint8_t reg_addr) {
  28. i2c_read_register(DRV2605L_I2C_ADDRESS << 1, reg_addr, &drv2605l_read_buffer, 1, 100);
  29. return drv2605l_read_buffer;
  30. }
  31. void drv2605l_init(void) {
  32. i2c_init();
  33. /* 0x07 sets DRV2605 into calibration mode */
  34. drv2605l_write(DRV2605L_REG_MODE, 0x07);
  35. // drv2605l_write(DRV2605L_REG_FEEDBACK_CTRL,0xB6);
  36. #if DRV2605L_FB_ERM_LRA == 0
  37. /* ERM settings */
  38. drv2605l_write(DRV2605L_REG_RATED_VOLTAGE, (DRV2605L_RATED_VOLTAGE / 21.33) * 1000);
  39. # if DRV2605L_ERM_OPEN_LOOP == 0
  40. 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)));
  41. # elif DRV2605L_ERM_OPEN_LOOP == 1
  42. drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, (DRV2605L_V_PEAK / 0.02196));
  43. # endif
  44. #elif DRV2605L_FB_ERM_LRA == 1
  45. 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)));
  46. # if DRV2605L_LRA_OPEN_LOOP == 0
  47. drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, ((DRV2605L_V_PEAK / sqrt(1 - (DRV2605L_F_LRA * 0.0008)) / 0.02133)));
  48. # elif DRV2605L_LRA_OPEN_LOOP == 1
  49. drv2605l_write(DRV2605L_REG_OVERDRIVE_CLAMP_VOLTAGE, (DRV2605L_V_PEAK / 0.02196));
  50. # endif
  51. #endif
  52. drv2605l_reg_feedback_ctrl_t reg_feedback_ctrl;
  53. reg_feedback_ctrl.bits.ERM_LRA = DRV2605L_FB_ERM_LRA;
  54. reg_feedback_ctrl.bits.BRAKE_FACTOR = DRV2605L_FB_BRAKEFACTOR;
  55. reg_feedback_ctrl.bits.LOOP_GAIN = DRV2605L_FB_LOOPGAIN;
  56. reg_feedback_ctrl.bits.BEMF_GAIN = 0; /* auto-calibration populates this field*/
  57. drv2605l_write(DRV2605L_REG_FEEDBACK_CTRL, (uint8_t)reg_feedback_ctrl.raw);
  58. drv2605l_reg_ctrl1_t reg_ctrl1;
  59. reg_ctrl1.bits.C1_DRIVE_TIME = DRV2605L_DRIVE_TIME;
  60. reg_ctrl1.bits.C1_AC_COUPLE = DRV2605L_AC_COUPLE;
  61. reg_ctrl1.bits.C1_STARTUP_BOOST = DRV2605L_STARTUP_BOOST;
  62. drv2605l_write(DRV2605L_REG_CTRL1, (uint8_t)reg_ctrl1.raw);
  63. drv2605l_reg_ctrl2_t reg_ctrl2;
  64. reg_ctrl2.bits.C2_BIDIR_INPUT = DRV2605L_BIDIR_INPUT;
  65. reg_ctrl2.bits.C2_BRAKE_STAB = DRV2605L_BRAKE_STAB;
  66. reg_ctrl2.bits.C2_SAMPLE_TIME = DRV2605L_SAMPLE_TIME;
  67. reg_ctrl2.bits.C2_BLANKING_TIME = DRV2605L_BLANKING_TIME;
  68. reg_ctrl2.bits.C2_IDISS_TIME = DRV2605L_IDISS_TIME;
  69. drv2605l_write(DRV2605L_REG_CTRL2, (uint8_t)reg_ctrl2.raw);
  70. drv2605l_reg_ctrl3_t reg_ctrl3;
  71. reg_ctrl3.bits.C3_LRA_OPEN_LOOP = DRV2605L_LRA_OPEN_LOOP;
  72. reg_ctrl3.bits.C3_N_PWM_ANALOG = DRV2605L_N_PWM_ANALOG;
  73. reg_ctrl3.bits.C3_LRA_DRIVE_MODE = DRV2605L_LRA_DRIVE_MODE;
  74. reg_ctrl3.bits.C3_DATA_FORMAT_RTO = DRV2605L_DATA_FORMAT_RTO;
  75. reg_ctrl3.bits.C3_SUPPLY_COMP_DIS = DRV2605L_SUPPLY_COMP_DIS;
  76. reg_ctrl3.bits.C3_ERM_OPEN_LOOP = DRV2605L_ERM_OPEN_LOOP;
  77. reg_ctrl3.bits.C3_NG_THRESH = DRV2605L_NG_THRESH;
  78. drv2605l_write(DRV2605L_REG_CTRL3, (uint8_t)reg_ctrl3.raw);
  79. drv2605l_reg_ctrl4_t reg_ctrl4;
  80. reg_ctrl4.bits.C4_ZC_DET_TIME = DRV2605L_ZC_DET_TIME;
  81. reg_ctrl4.bits.C4_AUTO_CAL_TIME = DRV2605L_AUTO_CAL_TIME;
  82. drv2605l_write(DRV2605L_REG_CTRL4, (uint8_t)reg_ctrl4.raw);
  83. drv2605l_write(DRV2605L_REG_LIBRARY_SELECTION, DRV2605L_LIBRARY);
  84. drv2605l_write(DRV2605L_REG_GO, 0x01);
  85. /* 0x00 sets DRV2605 out of standby and to use internal trigger
  86. * 0x01 sets DRV2605 out of standby and to use external trigger */
  87. drv2605l_write(DRV2605L_REG_MODE, 0x00);
  88. // Play greeting sequence
  89. drv2605l_write(DRV2605L_REG_GO, 0x00);
  90. drv2605l_write(DRV2605L_REG_WAVEFORM_SEQUENCER_1, DRV2605L_GREETING);
  91. drv2605l_write(DRV2605L_REG_GO, 0x01);
  92. }
  93. void drv2605l_rtp_init(void) {
  94. drv2605l_write(DRV2605L_REG_GO, 0x00);
  95. drv2605l_write(DRV2605L_REG_RTP_INPUT, 20); // 20 is the lowest value I've found where haptics can still be felt.
  96. drv2605l_write(DRV2605L_REG_MODE, 0x05);
  97. drv2605l_write(DRV2605L_REG_GO, 0x01);
  98. }
  99. void drv2605l_amplitude(uint8_t amplitude) {
  100. drv2605l_write(DRV2605L_REG_RTP_INPUT, amplitude);
  101. }
  102. void drv2605l_pulse(uint8_t sequence) {
  103. drv2605l_write(DRV2605L_REG_GO, 0x00);
  104. drv2605l_write(DRV2605L_REG_WAVEFORM_SEQUENCER_1, sequence);
  105. drv2605l_write(DRV2605L_REG_GO, 0x01);
  106. }