DRV2605L.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #pragma once
  18. #include "i2c_master.h"
  19. /* Initialization settings
  20. * Feedback Control Settings */
  21. #ifndef FB_ERM_LRA
  22. #define FB_ERM_LRA 1 /* For ERM:0 or LRA:1*/
  23. #endif
  24. #ifndef FB_BRAKEFACTOR
  25. #define FB_BRAKEFACTOR 3 /* For 1x:0, 2x:1, 3x:2, 4x:3, 6x:4, 8x:5, 16x:6, Disable Braking:7 */
  26. #endif
  27. #ifndef FB_LOOPGAIN
  28. #define FB_LOOPGAIN 1 /* For Low:0, Medium:1, High:2, Very High:3 */
  29. #endif
  30. #ifndef RATED_VOLTAGE
  31. #define RATED_VOLTAGE 2 /* 2v as safe range in case device voltage is not set */
  32. #ifndef V_PEAK
  33. #define V_PEAK 2.8
  34. #endif
  35. #endif
  36. /* LRA specific settings */
  37. #if FB_ERM_LRA == 1
  38. #ifndef V_RMS
  39. #define V_RMS 2.0
  40. #endif
  41. #ifndef V_PEAK
  42. #define V_PEAK 2.1
  43. #endif
  44. #ifndef F_LRA
  45. #define F_LRA 205
  46. #endif
  47. #endif
  48. /* Library Selection */
  49. #ifndef LIB_SELECTION
  50. #if FB_ERM_LRA == 1
  51. #define LIB_SELECTION 6 /* For Empty:0' TS2200 library A to D:1-5, LRA Library: 6 */
  52. #else
  53. #define LIB_SELECTION 1
  54. #endif
  55. #endif
  56. /* Control 1 register settings */
  57. #ifndef DRIVE_TIME
  58. #define DRIVE_TIME 25
  59. #endif
  60. #ifndef AC_COUPLE
  61. #define AC_COUPLE 0
  62. #endif
  63. #ifndef STARTUP_BOOST
  64. #define STARTUP_BOOST 1
  65. #endif
  66. /* Control 2 Settings */
  67. #ifndef BIDIR_INPUT
  68. #define BIDIR_INPUT 1
  69. #endif
  70. #ifndef BRAKE_STAB
  71. #define BRAKE_STAB 1 /* Loopgain is reduced when braking is almost complete to improve stability */
  72. #endif
  73. #ifndef SAMPLE_TIME
  74. #define SAMPLE_TIME 3
  75. #endif
  76. #ifndef BLANKING_TIME
  77. #define BLANKING_TIME 1
  78. #endif
  79. #ifndef IDISS_TIME
  80. #define IDISS_TIME 1
  81. #endif
  82. /* Control 3 settings */
  83. #ifndef NG_THRESH
  84. #define NG_THRESH 2
  85. #endif
  86. #ifndef ERM_OPEN_LOOP
  87. #define ERM_OPEN_LOOP 1
  88. #endif
  89. #ifndef SUPPLY_COMP_DIS
  90. #define SUPPLY_COMP_DIS 0
  91. #endif
  92. #ifndef DATA_FORMAT_RTO
  93. #define DATA_FORMAT_RTO 0
  94. #endif
  95. #ifndef LRA_DRIVE_MODE
  96. #define LRA_DRIVE_MODE 0
  97. #endif
  98. #ifndef N_PWM_ANALOG
  99. #define N_PWM_ANALOG 0
  100. #endif
  101. #ifndef LRA_OPEN_LOOP
  102. #define LRA_OPEN_LOOP 0
  103. #endif
  104. /* Control 4 settings */
  105. #ifndef ZC_DET_TIME
  106. #define ZC_DET_TIME 0
  107. #endif
  108. #ifndef AUTO_CAL_TIME
  109. #define AUTO_CAL_TIME 3
  110. #endif
  111. /* register defines -------------------------------------------------------- */
  112. #define DRV2605L_BASE_ADDRESS 0x5A /* DRV2605L Base address */
  113. #define DRV_STATUS 0x00
  114. #define DRV_MODE 0x01
  115. #define DRV_RTP_INPUT 0x02
  116. #define DRV_LIB_SELECTION 0x03
  117. #define DRV_WAVEFORM_SEQ_1 0x04
  118. #define DRV_WAVEFORM_SEQ_2 0x05
  119. #define DRV_WAVEFORM_SEQ_3 0x06
  120. #define DRV_WAVEFORM_SEQ_4 0x07
  121. #define DRV_WAVEFORM_SEQ_5 0x08
  122. #define DRV_WAVEFORM_SEQ_6 0x09
  123. #define DRV_WAVEFORM_SEQ_7 0x0A
  124. #define DRV_WAVEFORM_SEQ_8 0x0B
  125. #define DRV_GO 0x0C
  126. #define DRV_OVERDRIVE_TIME_OFFSET 0x0D
  127. #define DRV_SUSTAIN_TIME_OFFSET_P 0x0E
  128. #define DRV_SUSTAIN_TIME_OFFSET_N 0x0F
  129. #define DRV_BRAKE_TIME_OFFSET 0x10
  130. #define DRV_AUDIO_2_VIBE_CTRL 0x11
  131. #define DRV_AUDIO_2_VIBE_MIN_IN 0x12
  132. #define DRV_AUDIO_2_VIBE_MAX_IN 0x13
  133. #define DRV_AUDIO_2_VIBE_MIN_OUTDRV 0x14
  134. #define DRV_AUDIO_2_VIBE_MAX_OUTDRV 0x15
  135. #define DRV_RATED_VOLT 0x16
  136. #define DRV_OVERDRIVE_CLAMP_VOLT 0x17
  137. #define DRV_AUTO_CALIB_COMP_RESULT 0x18
  138. #define DRV_AUTO_CALIB_BEMF_RESULT 0x19
  139. #define DRV_FEEDBACK_CTRL 0x1A
  140. #define DRV_CTRL_1 0x1B
  141. #define DRV_CTRL_2 0x1C
  142. #define DRV_CTRL_3 0x1D
  143. #define DRV_CTRL_4 0x1E
  144. #define DRV_CTRL_5 0x1F
  145. #define DRV_OPEN_LOOP_PERIOD 0x20
  146. #define DRV_VBAT_VOLT_MONITOR 0x21
  147. #define DRV_LRA_RESONANCE_PERIOD 0x22
  148. void DRV_init(void);
  149. void DRV_write(const uint8_t drv_register, const uint8_t settings);
  150. uint8_t DRV_read(const uint8_t regaddress);
  151. void DRV_pulse(const uint8_t sequence);
  152. typedef enum DRV_EFFECT{
  153. clear_sequence = 0,
  154. strong_click = 1,
  155. strong_click_60 = 2,
  156. strong_click_30 = 3,
  157. sharp_click = 4,
  158. sharp_click_60 = 5,
  159. sharp_click_30 = 6,
  160. soft_bump = 7,
  161. soft_bump_60 = 8,
  162. soft_bump_30 = 9,
  163. dbl_click = 10,
  164. dbl_click_60 = 11,
  165. trp_click = 12,
  166. soft_fuzz = 13,
  167. strong_buzz = 14,
  168. alert_750ms = 15,
  169. alert_1000ms = 16,
  170. strong_click1 = 17,
  171. strong_click2_80 = 18,
  172. strong_click3_60 = 19,
  173. strong_click4_30 = 20,
  174. medium_click1 = 21,
  175. medium_click2_80 = 22,
  176. medium_click3_60 = 23,
  177. sharp_tick1 = 24,
  178. sharp_tick2_80 = 25,
  179. sharp_tick3_60 = 26,
  180. sh_dblclick_str = 27,
  181. sh_dblclick_str_80 = 28,
  182. sh_dblclick_str_60 = 29,
  183. sh_dblclick_str_30 = 30,
  184. sh_dblclick_med = 31,
  185. sh_dblclick_med_80 = 32,
  186. sh_dblclick_med_60 = 33,
  187. sh_dblsharp_tick = 34,
  188. sh_dblsharp_tick_80 = 35,
  189. sh_dblsharp_tick_60 = 36,
  190. lg_dblclick_str = 37,
  191. lg_dblclick_str_80 = 38,
  192. lg_dblclick_str_60 = 39,
  193. lg_dblclick_str_30 = 40,
  194. lg_dblclick_med = 41,
  195. lg_dblclick_med_80 = 42,
  196. lg_dblclick_med_60 = 43,
  197. lg_dblsharp_tick = 44,
  198. lg_dblsharp_tick_80 = 45,
  199. lg_dblsharp_tick_60 = 46,
  200. buzz = 47,
  201. buzz_80 = 48,
  202. buzz_60 = 49,
  203. buzz_40 = 50,
  204. buzz_20 = 51,
  205. pulsing_strong = 52,
  206. pulsing_strong_80 = 53,
  207. pulsing_medium = 54,
  208. pulsing_medium_80 = 55,
  209. pulsing_sharp = 56,
  210. pulsing_sharp_80 = 57,
  211. transition_click = 58,
  212. transition_click_80 = 59,
  213. transition_click_60 = 60,
  214. transition_click_40 = 61,
  215. transition_click_20 = 62,
  216. transition_click_10 = 63,
  217. transition_hum = 64,
  218. transition_hum_80 = 65,
  219. transition_hum_60 = 66,
  220. transition_hum_40 = 67,
  221. transition_hum_20 = 68,
  222. transition_hum_10 = 69,
  223. transition_rampdown_long_smooth1 = 70,
  224. transition_rampdown_long_smooth2 = 71,
  225. transition_rampdown_med_smooth1 = 72,
  226. transition_rampdown_med_smooth2 = 73,
  227. transition_rampdown_short_smooth1 = 74,
  228. transition_rampdown_short_smooth2 = 75,
  229. transition_rampdown_long_sharp1 = 76,
  230. transition_rampdown_long_sharp2 = 77,
  231. transition_rampdown_med_sharp1 = 78,
  232. transition_rampdown_med_sharp2 = 79,
  233. transition_rampdown_short_sharp1 = 80,
  234. transition_rampdown_short_sharp2 = 81,
  235. transition_rampup_long_smooth1 = 82,
  236. transition_rampup_long_smooth2 = 83,
  237. transition_rampup_med_smooth1 = 84,
  238. transition_rampup_med_smooth2 = 85,
  239. transition_rampup_short_smooth1 = 86,
  240. transition_rampup_short_smooth2 = 87,
  241. transition_rampup_long_sharp1 = 88,
  242. transition_rampup_long_sharp2 = 89,
  243. transition_rampup_med_sharp1 = 90,
  244. transition_rampup_med_sharp2 = 91,
  245. transition_rampup_short_sharp1 = 92,
  246. transition_rampup_short_sharp2 = 93,
  247. transition_rampdown_long_smooth1_50 = 94,
  248. transition_rampdown_long_smooth2_50 = 95,
  249. transition_rampdown_med_smooth1_50 = 96,
  250. transition_rampdown_med_smooth2_50 = 97,
  251. transition_rampdown_short_smooth1_50 = 98,
  252. transition_rampdown_short_smooth2_50 = 99,
  253. transition_rampdown_long_sharp1_50 = 100,
  254. transition_rampdown_long_sharp2_50 = 101,
  255. transition_rampdown_med_sharp1_50 = 102,
  256. transition_rampdown_med_sharp2_50 = 103,
  257. transition_rampdown_short_sharp1_50 = 104,
  258. transition_rampdown_short_sharp2_50 = 105,
  259. transition_rampup_long_smooth1_50 = 106,
  260. transition_rampup_long_smooth2_50 = 107,
  261. transition_rampup_med_smooth1_50 = 108,
  262. transition_rampup_med_smooth2_50 = 109,
  263. transition_rampup_short_smooth1_50 = 110,
  264. transition_rampup_short_smooth2_50 = 111,
  265. transition_rampup_long_sharp1_50 = 112,
  266. transition_rampup_long_sharp2_50 = 113,
  267. transition_rampup_med_sharp1_50 = 114,
  268. transition_rampup_med_sharp2_50 = 115,
  269. transition_rampup_short_sharp1_50 = 116,
  270. transition_rampup_short_sharp2_50 = 117,
  271. long_buzz_for_programmatic_stopping = 118,
  272. smooth_hum1_50 = 119,
  273. smooth_hum2_40 = 120,
  274. smooth_hum3_30 = 121,
  275. smooth_hum4_20 = 122,
  276. smooth_hum5_10 = 123,
  277. } DRV_EFFECT;
  278. /* Register bit array unions */
  279. typedef union DRVREG_STATUS { /* register 0x00 */
  280. uint8_t Byte;
  281. struct {
  282. uint8_t OC_DETECT :1; /* set to 1 when overcurrent event is detected */
  283. uint8_t OVER_TEMP :1; /* set to 1 when device exceeds temp threshold */
  284. uint8_t FB_STS :1; /* set to 1 when feedback controller has timed out */
  285. /* auto-calibration routine and diagnostic result
  286. * result | auto-calibation | diagnostic |
  287. * 0 | passed | actuator func normal |
  288. * 1 | failed | actuator func fault* |
  289. * * actuator is not present or is shorted, timing out, or giving out–of-range back-EMF */
  290. uint8_t DIAG_RESULT :1;
  291. uint8_t :1;
  292. uint8_t DEVICE_ID :3; /* Device IDs 3: DRV2605 4: DRV2604 5: DRV2604L 6: DRV2605L */
  293. } Bits;
  294. } DRVREG_STATUS;
  295. typedef union DRVREG_MODE { /* register 0x01 */
  296. uint8_t Byte;
  297. struct {
  298. uint8_t MODE :3; /* Mode setting */
  299. uint8_t :3;
  300. uint8_t STANDBY :1; /* 0:standby 1:ready */
  301. } Bits;
  302. } DRVREG_MODE;
  303. typedef union DRVREG_WAIT {
  304. uint8_t Byte;
  305. struct {
  306. uint8_t WAIT_MODE :1; /* Set to 1 to interpret as wait for next 7 bits x10ms */
  307. uint8_t WAIT_TIME :7;
  308. } Bits;
  309. } DRVREG_WAIT;
  310. typedef union DRVREG_FBR{ /* register 0x1A */
  311. uint8_t Byte;
  312. struct {
  313. uint8_t BEMF_GAIN :2;
  314. uint8_t LOOP_GAIN :2;
  315. uint8_t BRAKE_FACTOR :3;
  316. uint8_t ERM_LRA :1;
  317. } Bits;
  318. } DRVREG_FBR;
  319. typedef union DRVREG_CTRL1{ /* register 0x1B */
  320. uint8_t Byte;
  321. struct {
  322. uint8_t C1_DRIVE_TIME :5;
  323. uint8_t C1_AC_COUPLE :1;
  324. uint8_t :1;
  325. uint8_t C1_STARTUP_BOOST :1;
  326. } Bits;
  327. } DRVREG_CTRL1;
  328. typedef union DRVREG_CTRL2{ /* register 0x1C */
  329. uint8_t Byte;
  330. struct {
  331. uint8_t C2_IDISS_TIME :2;
  332. uint8_t C2_BLANKING_TIME :2;
  333. uint8_t C2_SAMPLE_TIME :2;
  334. uint8_t C2_BRAKE_STAB :1;
  335. uint8_t C2_BIDIR_INPUT :1;
  336. } Bits;
  337. } DRVREG_CTRL2;
  338. typedef union DRVREG_CTRL3{ /* register 0x1D */
  339. uint8_t Byte;
  340. struct {
  341. uint8_t C3_LRA_OPEN_LOOP :1;
  342. uint8_t C3_N_PWM_ANALOG :1;
  343. uint8_t C3_LRA_DRIVE_MODE :1;
  344. uint8_t C3_DATA_FORMAT_RTO :1;
  345. uint8_t C3_SUPPLY_COMP_DIS :1;
  346. uint8_t C3_ERM_OPEN_LOOP :1;
  347. uint8_t C3_NG_THRESH :2;
  348. } Bits;
  349. } DRVREG_CTRL3;
  350. typedef union DRVREG_CTRL4{ /* register 0x1E */
  351. uint8_t Byte;
  352. struct {
  353. uint8_t C4_OTP_PROGRAM :1;
  354. uint8_t :1;
  355. uint8_t C4_OTP_STATUS :1;
  356. uint8_t :1;
  357. uint8_t C4_AUTO_CAL_TIME :2;
  358. uint8_t C4_ZC_DET_TIME :2;
  359. } Bits;
  360. } DRVREG_CTRL4;
  361. typedef union DRVREG_CTRL5{ /* register 0x1F */
  362. uint8_t Byte;
  363. struct {
  364. uint8_t C5_IDISS_TIME :2;
  365. uint8_t C5_BLANKING_TIME :2;
  366. uint8_t C5_PLAYBACK_INTERVAL :1;
  367. uint8_t C5_LRA_AUTO_OPEN_LOOP :1;
  368. uint8_t C5_AUTO_OL_CNT :2;
  369. } Bits;
  370. } DRVREG_CTRL5;