DRV2605L.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "print.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <math.h>
  22. uint8_t DRV2605L_transfer_buffer[2];
  23. uint8_t DRV2605L_tx_register[0];
  24. uint8_t DRV2605L_read_buffer[0];
  25. uint8_t DRV2605L_read_register;
  26. void DRV_write(uint8_t drv_register, uint8_t settings) {
  27. DRV2605L_transfer_buffer[0] = drv_register;
  28. DRV2605L_transfer_buffer[1] = settings;
  29. i2c_transmit(DRV2605L_BASE_ADDRESS << 1, DRV2605L_transfer_buffer, 2, 100);
  30. }
  31. uint8_t DRV_read(uint8_t regaddress) {
  32. i2c_readReg(DRV2605L_BASE_ADDRESS << 1, regaddress, DRV2605L_read_buffer, 1, 100);
  33. DRV2605L_read_register = (uint8_t)DRV2605L_read_buffer[0];
  34. return DRV2605L_read_register;
  35. }
  36. void DRV_init(void) {
  37. i2c_init();
  38. /* 0x07 sets DRV2605 into calibration mode */
  39. DRV_write(DRV_MODE, 0x07);
  40. // DRV_write(DRV_FEEDBACK_CTRL,0xB6);
  41. #if FB_ERM_LRA == 0
  42. /* ERM settings */
  43. DRV_write(DRV_RATED_VOLT, (RATED_VOLTAGE / 21.33) * 1000);
  44. # if ERM_OPEN_LOOP == 0
  45. DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, (((V_PEAK * (DRIVE_TIME + BLANKING_TIME + IDISS_TIME)) / 0.02133) / (DRIVE_TIME - 0.0003)));
  46. # elif ERM_OPEN_LOOP == 1
  47. DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, (V_PEAK / 0.02196));
  48. # endif
  49. #elif FB_ERM_LRA == 1
  50. DRV_write(DRV_RATED_VOLT, ((V_RMS * sqrt(1 - ((4 * ((150 + (SAMPLE_TIME * 50)) * 0.000001)) + 0.0003) * F_LRA) / 0.02071)));
  51. # if LRA_OPEN_LOOP == 0
  52. DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, ((V_PEAK / sqrt(1 - (F_LRA * 0.0008)) / 0.02133)));
  53. # elif LRA_OPEN_LOOP == 1
  54. DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, (V_PEAK / 0.02196));
  55. # endif
  56. #endif
  57. DRVREG_FBR FB_SET;
  58. FB_SET.Bits.ERM_LRA = FB_ERM_LRA;
  59. FB_SET.Bits.BRAKE_FACTOR = FB_BRAKEFACTOR;
  60. FB_SET.Bits.LOOP_GAIN = FB_LOOPGAIN;
  61. FB_SET.Bits.BEMF_GAIN = 0; /* auto-calibration populates this field*/
  62. DRV_write(DRV_FEEDBACK_CTRL, (uint8_t)FB_SET.Byte);
  63. DRVREG_CTRL1 C1_SET;
  64. C1_SET.Bits.C1_DRIVE_TIME = DRIVE_TIME;
  65. C1_SET.Bits.C1_AC_COUPLE = AC_COUPLE;
  66. C1_SET.Bits.C1_STARTUP_BOOST = STARTUP_BOOST;
  67. DRV_write(DRV_CTRL_1, (uint8_t)C1_SET.Byte);
  68. DRVREG_CTRL2 C2_SET;
  69. C2_SET.Bits.C2_BIDIR_INPUT = BIDIR_INPUT;
  70. C2_SET.Bits.C2_BRAKE_STAB = BRAKE_STAB;
  71. C2_SET.Bits.C2_SAMPLE_TIME = SAMPLE_TIME;
  72. C2_SET.Bits.C2_BLANKING_TIME = BLANKING_TIME;
  73. C2_SET.Bits.C2_IDISS_TIME = IDISS_TIME;
  74. DRV_write(DRV_CTRL_2, (uint8_t)C2_SET.Byte);
  75. DRVREG_CTRL3 C3_SET;
  76. C3_SET.Bits.C3_LRA_OPEN_LOOP = LRA_OPEN_LOOP;
  77. C3_SET.Bits.C3_N_PWM_ANALOG = N_PWM_ANALOG;
  78. C3_SET.Bits.C3_LRA_DRIVE_MODE = LRA_DRIVE_MODE;
  79. C3_SET.Bits.C3_DATA_FORMAT_RTO = DATA_FORMAT_RTO;
  80. C3_SET.Bits.C3_SUPPLY_COMP_DIS = SUPPLY_COMP_DIS;
  81. C3_SET.Bits.C3_ERM_OPEN_LOOP = ERM_OPEN_LOOP;
  82. C3_SET.Bits.C3_NG_THRESH = NG_THRESH;
  83. DRV_write(DRV_CTRL_3, (uint8_t)C3_SET.Byte);
  84. DRVREG_CTRL4 C4_SET;
  85. C4_SET.Bits.C4_ZC_DET_TIME = ZC_DET_TIME;
  86. C4_SET.Bits.C4_AUTO_CAL_TIME = AUTO_CAL_TIME;
  87. DRV_write(DRV_CTRL_4, (uint8_t)C4_SET.Byte);
  88. DRV_write(DRV_LIB_SELECTION, LIB_SELECTION);
  89. DRV_write(DRV_GO, 0x01);
  90. /* 0x00 sets DRV2605 out of standby and to use internal trigger
  91. * 0x01 sets DRV2605 out of standby and to use external trigger */
  92. DRV_write(DRV_MODE, 0x00);
  93. // Play greeting sequence
  94. DRV_write(DRV_GO, 0x00);
  95. DRV_write(DRV_WAVEFORM_SEQ_1, DRV_GREETING);
  96. DRV_write(DRV_GO, 0x01);
  97. }
  98. void DRV_rtp_init(void) {
  99. DRV_write(DRV_GO, 0x00);
  100. DRV_write(DRV_RTP_INPUT, 20); // 20 is the lowest value I've found where haptics can still be felt.
  101. DRV_write(DRV_MODE, 0x05);
  102. DRV_write(DRV_GO, 0x01);
  103. }
  104. void DRV_amplitude(uint8_t amplitude) { DRV_write(DRV_RTP_INPUT, amplitude); }
  105. void DRV_pulse(uint8_t sequence) {
  106. DRV_write(DRV_GO, 0x00);
  107. DRV_write(DRV_WAVEFORM_SEQ_1, sequence);
  108. DRV_write(DRV_GO, 0x01);
  109. }