]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/haptic/DRV2605L.c
Add user-overridable callback for cancelling UCIS input (#5564)
[qmk_firmware.git] / drivers / haptic / DRV2605L.c
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
23
24 uint8_t DRV2605L_transfer_buffer[2];
25 uint8_t DRV2605L_tx_register[0];
26 uint8_t DRV2605L_read_buffer[0];
27 uint8_t DRV2605L_read_register;
28
29
30 void DRV_write(uint8_t drv_register, uint8_t settings) {
31   DRV2605L_transfer_buffer[0] = drv_register;
32   DRV2605L_transfer_buffer[1] = settings;
33   i2c_transmit(DRV2605L_BASE_ADDRESS << 1, DRV2605L_transfer_buffer, 2, 100);
34 }
35
36 uint8_t DRV_read(uint8_t regaddress) {
37 #ifdef __AVR__
38   i2c_readReg(DRV2605L_BASE_ADDRESS << 1,
39     regaddress, DRV2605L_read_buffer, 1, 100);
40   DRV2605L_read_register = (uint8_t)DRV2605L_read_buffer[0];
41 #else
42   DRV2605L_tx_register[0] = regaddress;
43   if (MSG_OK != i2c_transmit_receive(DRV2605L_BASE_ADDRESS << 1,
44     DRV2605L_tx_register, 1,
45     DRV2605L_read_buffer, 1
46 )){
47     printf("err reading reg \n");
48   }
49   DRV2605L_read_register = (uint8_t)DRV2605L_read_buffer[0];
50 #endif
51 return DRV2605L_read_register;
52 }
53
54 void DRV_init(void)
55 {
56   i2c_init();
57   /* 0x07 sets DRV2605 into calibration mode */
58   DRV_write(DRV_MODE,0x07); 
59
60 //  DRV_write(DRV_FEEDBACK_CTRL,0xB6);
61     
62   #if FB_ERM_LRA == 0
63     /* ERM settings */
64   DRV_write(DRV_RATED_VOLT, (RATED_VOLTAGE/21.33)*1000);
65   #if ERM_OPEN_LOOP == 0
66   DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, (((V_PEAK*(DRIVE_TIME+BLANKING_TIME+IDISS_TIME))/0.02133)/(DRIVE_TIME-0.0003))); 
67   #elif ERM_OPEN_LOOP == 1
68   DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, (V_PEAK/0.02196));
69   #endif
70   #elif FB_ERM_LRA == 1
71   DRV_write(DRV_RATED_VOLT, ((V_RMS * sqrt(1 - ((4 * ((150+(SAMPLE_TIME*50))*0.000001)) + 0.0003)* F_LRA)/0.02071)));
72   #if LRA_OPEN_LOOP == 0
73     DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, ((V_PEAK/sqrt(1-(F_LRA*0.0008))/0.02133)));
74   #elif LRA_OPEN_LOOP == 1
75     DRV_write(DRV_OVERDRIVE_CLAMP_VOLT, (V_PEAK/0.02196));
76   #endif
77   #endif
78       
79   DRVREG_FBR FB_SET;
80     FB_SET.Bits.ERM_LRA = FB_ERM_LRA;
81     FB_SET.Bits.BRAKE_FACTOR = FB_BRAKEFACTOR;
82     FB_SET.Bits.LOOP_GAIN =FB_LOOPGAIN;
83     FB_SET.Bits.BEMF_GAIN = 0; /* auto-calibration populates this field*/
84     DRV_write(DRV_FEEDBACK_CTRL, (uint8_t) FB_SET.Byte);
85   DRVREG_CTRL1 C1_SET;
86     C1_SET.Bits.C1_DRIVE_TIME = DRIVE_TIME;
87     C1_SET.Bits.C1_AC_COUPLE = AC_COUPLE;
88     C1_SET.Bits.C1_STARTUP_BOOST = STARTUP_BOOST;
89     DRV_write(DRV_CTRL_1, (uint8_t) C1_SET.Byte);
90   DRVREG_CTRL2 C2_SET;
91     C2_SET.Bits.C2_BIDIR_INPUT = BIDIR_INPUT;
92     C2_SET.Bits.C2_BRAKE_STAB = BRAKE_STAB;
93     C2_SET.Bits.C2_SAMPLE_TIME = SAMPLE_TIME;
94     C2_SET.Bits.C2_BLANKING_TIME = BLANKING_TIME;
95     C2_SET.Bits.C2_IDISS_TIME = IDISS_TIME;
96     DRV_write(DRV_CTRL_2, (uint8_t) C2_SET.Byte);
97   DRVREG_CTRL3 C3_SET;
98     C3_SET.Bits.C3_LRA_OPEN_LOOP = LRA_OPEN_LOOP;
99     C3_SET.Bits.C3_N_PWM_ANALOG = N_PWM_ANALOG;
100     C3_SET.Bits.C3_LRA_DRIVE_MODE = LRA_DRIVE_MODE;
101     C3_SET.Bits.C3_DATA_FORMAT_RTO = DATA_FORMAT_RTO;
102     C3_SET.Bits.C3_SUPPLY_COMP_DIS = SUPPLY_COMP_DIS;
103     C3_SET.Bits.C3_ERM_OPEN_LOOP = ERM_OPEN_LOOP;
104     C3_SET.Bits.C3_NG_THRESH = NG_THRESH;
105     DRV_write(DRV_CTRL_3, (uint8_t) C3_SET.Byte);
106   DRVREG_CTRL4 C4_SET;
107     C4_SET.Bits.C4_ZC_DET_TIME = ZC_DET_TIME;
108     C4_SET.Bits.C4_AUTO_CAL_TIME = AUTO_CAL_TIME;
109     DRV_write(DRV_CTRL_4, (uint8_t) C4_SET.Byte);
110   DRV_write(DRV_LIB_SELECTION,LIB_SELECTION);
111
112   DRV_write(DRV_GO, 0x01);
113
114   /* 0x00 sets DRV2605 out of standby and to use internal trigger
115    * 0x01 sets DRV2605 out of standby and to use external trigger */
116   DRV_write(DRV_MODE,0x00); 
117
118 //Play greeting sequence
119   DRV_write(DRV_GO, 0x00);
120   DRV_write(DRV_WAVEFORM_SEQ_1, DRV_GREETING);
121   DRV_write(DRV_GO, 0x01);
122 }
123
124 void DRV_pulse(uint8_t sequence)
125 {
126   DRV_write(DRV_GO, 0x00);
127   DRV_write(DRV_WAVEFORM_SEQ_1, sequence);
128   DRV_write(DRV_GO, 0x01);
129 }