haptic4  2.1.0.0
haptic4.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef HAPTIC4_H
29 #define HAPTIC4_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define HAPTIC4_REG_CHIP_REV 0x00
74 #define HAPTIC4_REG_IRQ_EVENT1 0x03
75 #define HAPTIC4_REG_IRQ_EVENT_WARNING_DIAG 0x04
76 #define HAPTIC4_REG_IRQ_EVENT_SEQ_DIAG 0x05
77 #define HAPTIC4_REG_IRQ_STATUS1 0x06
78 #define HAPTIC4_REG_IRQ_MASK1 0x07
79 #define HAPTIC4_REG_CIF_I2C1 0x08
80 #define HAPTIC4_REG_FRQ_LRA_PER_H 0x0A
81 #define HAPTIC4_REG_FRQ_LRA_PER_L 0x0B
82 #define HAPTIC4_REG_ACTUATOR1 0x0C
83 #define HAPTIC4_REG_ACTUATOR2 0x0D
84 #define HAPTIC4_REG_ACTUATOR3 0x0E
85 #define HAPTIC4_REG_CALIB_V2I_H 0x0F
86 #define HAPTIC4_REG_CALIB_V2I_L 0x10
87 #define HAPTIC4_REG_CALIB_IMP_H 0x11
88 #define HAPTIC4_REG_CALIB_IMP_L 0x12
89 #define HAPTIC4_REG_TOP_CFG1 0x13
90 #define HAPTIC4_REG_TOP_CFG2 0x14
91 #define HAPTIC4_REG_TOP_CFG3 0x15
92 #define HAPTIC4_REG_TOP_CFG4 0x16
93 #define HAPTIC4_REG_TOP_INT_CFG1 0x17
94 #define HAPTIC4_REG_TOP_INT_CFG6_H 0x1C
95 #define HAPTIC4_REG_TOP_INT_CFG6_L 0x1D
96 #define HAPTIC4_REG_TOP_INT_CFG7_H 0x1E
97 #define HAPTIC4_REG_TOP_INT_CFG7_L 0x1F
98 #define HAPTIC4_REG_TOP_INT_CFG8 0x20
99 #define HAPTIC4_REG_TOP_CTL1 0x22
100 #define HAPTIC4_REG_TOP_CTL2 0x23
101 #define HAPTIC4_REG_SEQ_CTL1 0x24
102 #define HAPTIC4_REG_SWG_C1 0x25
103 #define HAPTIC4_REG_SWG_C2 0x26
104 #define HAPTIC4_REG_SWG_C3 0x27
105 #define HAPTIC4_REG_SEQ_CTL2 0x28
106 #define HAPTIC4_REG_GPI_0_CTL 0x29
107 #define HAPTIC4_REG_GPI_1_CTL 0x2A
108 #define HAPTIC4_REG_GPI_2_CTL 0x2B
109 #define HAPTIC4_REG_MEM_CTL1 0x2C
110 #define HAPTIC4_REG_MEM_CTL2 0x2D
111 #define HAPTIC4_REG_ADC_DATA_H1 0x2E
112 #define HAPTIC4_REG_ADC_DATA_L1 0x2F
113 #define HAPTIC4_REG_POLARITY 0x43
114 #define HAPTIC4_REG_LRA_AVR_H 0x44
115 #define HAPTIC4_REG_LRA_AVR_L 0x45
116 #define HAPTIC4_REG_FRQ_LRA_PER_ACT_H 0x46
117 #define HAPTIC4_REG_FRQ_LRA_PER_ACT_L 0x47
118 #define HAPTIC4_REG_FRQ_PHASE_H 0x48
119 #define HAPTIC4_REG_FRQ_PHASE_L 0x49
120 #define HAPTIC4_REG_FRQ_CTL 0x4C
121 #define HAPTIC4_REG_TRIM3 0x5F
122 #define HAPTIC4_REG_TRIM4 0x60
123 #define HAPTIC4_REG_TRIM6 0x62
124 #define HAPTIC4_REG_TOP_CFG5 0x6E
125 #define HAPTIC4_REG_IRQ_EVENT_ACTUATOR_FAULT 0x81
126 #define HAPTIC4_REG_IRQ_STATUS2 0x82
127 #define HAPTIC4_REG_IRQ_MASK2 0x83
128 #define HAPTIC4_REG_SNP_MEM_X 0x84
129  // haptic4_reg
131 
146 #define HAPTIC4_CHIP_REV 0xBA
147 
152 #define HAPTIC4_PIN_STATE_LOW 0x00
153 #define HAPTIC4_PIN_STATE_HIGH 0x01
154 
159 #define HAPTIC4_TOP_CFG1_EMB_MODE_MANUAL 0x00
160 #define HAPTIC4_TOP_CFG1_EMB_MODE_AUTO 0x80
161 #define HAPTIC4_TOP_CFG1_EMB_MODE_MASK 0x80
162 #define HAPTIC4_TOP_CFG1_ACT_TYPE_LRA 0x00
163 #define HAPTIC4_TOP_CFG1_ACT_TYPE_ERM 0x20
164 #define HAPTIC4_TOP_CFG1_ACT_TYPE_MASK 0x20
165 #define HAPTIC4_TOP_CFG1_BEMF_SENSE_CUSTOM_WF 0x00
166 #define HAPTIC4_TOP_CFG1_BEMF_SENSE_STANDARD 0x10
167 #define HAPTIC4_TOP_CFG1_BEMF_SENSE_MASK 0x10
168 #define HAPTIC4_TOP_CFG1_FREQ_TRACK_DISABLE 0x00
169 #define HAPTIC4_TOP_CFG1_FREQ_TRACK_ENABLE 0x08
170 #define HAPTIC4_TOP_CFG1_FREQ_TRACK_MASK 0x08
171 #define HAPTIC4_TOP_CFG1_ACCEL_DISABLE 0x00
172 #define HAPTIC4_TOP_CFG1_ACCEL_ENABLE 0x04
173 #define HAPTIC4_TOP_CFG1_ACCEL_MASK 0x04
174 #define HAPTIC4_TOP_CFG1_RAPID_STOP_DISABLE 0x00
175 #define HAPTIC4_TOP_CFG1_RAPID_STOP_ENABLE 0x02
176 #define HAPTIC4_TOP_CFG1_RAPID_STOP_MASK 0x02
177 #define HAPTIC4_TOP_CFG1_AMPLITUDE_PID_DISABLE 0x00
178 #define HAPTIC4_TOP_CFG1_AMPLITUDE_PID_ENABLE 0x01
179 #define HAPTIC4_TOP_CFG1_AMPLITUDE_PID_MASK 0x01
180 
185 #define HAPTIC4_TOP_CTL1_SEQ_STOP 0x00
186 #define HAPTIC4_TOP_CTL1_SEQ_START 0x10
187 #define HAPTIC4_TOP_CTL1_SEQ_MASK 0x10
188 #define HAPTIC4_TOP_CTL1_STANDBY_DISABLE 0x00
189 #define HAPTIC4_TOP_CTL1_STANDBY_ENABLE 0x08
190 #define HAPTIC4_TOP_CTL1_STANDBY_MASK 0x08
191 #define HAPTIC4_TOP_CTL1_OP_MODE_INACTIVE 0x00
192 #define HAPTIC4_TOP_CTL1_OP_MODE_DRO 0x01
193 #define HAPTIC4_TOP_CTL1_OP_MODE_PWM 0x02
194 #define HAPTIC4_TOP_CTL1_OP_MODE_RTWM 0x03
195 #define HAPTIC4_TOP_CTL1_OP_MODE_ETWM 0x04
196 #define HAPTIC4_TOP_CTL1_OP_MODE_MASK 0x07
197 
202 #define HAPTIC4_ACTUATOR_VOLTAGE_MIN 0.0f
203 #define HAPTIC4_ACTUATOR_VOLTAGE_MAX 6.0f
204 #define HAPTIC4_ACTUATOR_VOLTAGE_RESOLUTION 0.0234f
205 #define HAPTIC4_ACTUATOR_IMAX_MIN 28.6f
206 #define HAPTIC4_ACTUATOR_IMAX_MAX 251.8f
207 #define HAPTIC4_ACTUATOR_IMAX_DIVIDER 7.2f
208 #define HAPTIC4_ACTUATOR_IMAX_RESOLUTION 0x1F
209 #define HAPTIC4_ACTUATOR_IMPEDANCE_MIN 4.0f
210 #define HAPTIC4_ACTUATOR_IMPEDANCE_MAX 50.0f
211 #define HAPTIC4_ACTUATOR_V2I_FACTOR_DIVIDER 1.6104f
212 #define HAPTIC4_ACTUATOR_LRA_FREQ_MIN 0.0f
213 #define HAPTIC4_ACTUATOR_LRA_FREQ_MAX 300.0f
214 #define HAPTIC4_ACTUATOR_LRA_FREQ_RESOLUTION 0.00000133332f
215 #define HAPTIC4_VIBRATION_LEVEL_MIN -1.0f
216 #define HAPTIC4_VIBRATION_LEVEL_MAX 1.0f
217 #define HAPTIC4_VIBRATION_LEVEL_MIN_ACC_EN 0.0f
218 #define HAPTIC4_VIBRATION_LEVEL_RESOLUTION 0.008f
219 #define HAPTIC4_ROUND_TO_NEAREST_INT 0.5f
220 
225 #define HAPTIC4_DEFAULT_ACTUATOR_TYPE HAPTIC4_TOP_CFG1_ACT_TYPE_LRA
226 #define HAPTIC4_DEFAULT_ACTUATOR_ABS_VOLT 2.808f
227 #define HAPTIC4_DEFAULT_ACTUATOR_NOM_VOLT 2.106f
228 #define HAPTIC4_DEFAULT_ACTUATOR_IMAX 194.0f
229 #define HAPTIC4_DEFAULT_ACTUATOR_IMPEDANCE 16.0f
230 #define HAPTIC4_DEFAULT_ACTUATOR_LRA_FREQ 174.0f
231 
237 #define HAPTIC4_DEVICE_ADDRESS 0x4A
238  // haptic4_set
240 
255 #define HAPTIC4_MAP_MIKROBUS( cfg, mikrobus ) \
256  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
257  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
258  cfg.gp1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
259  cfg.gp2 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
260  cfg.gp0 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
261  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
262  // haptic4_map // haptic4
265 
270 typedef struct
271 {
272  // Output pins
273  digital_out_t gp1;
274  digital_out_t gp2;
275  digital_out_t gp0;
277  // Input pins
278  digital_in_t irq;
280  // Modules
281  i2c_master_t i2c;
283  // I2C slave address
284  uint8_t slave_address;
286 } haptic4_t;
287 
292 typedef struct
293 {
294  pin_name_t scl;
295  pin_name_t sda;
297  pin_name_t gp1;
298  pin_name_t gp2;
299  pin_name_t gp0;
300  pin_name_t irq;
302  uint32_t i2c_speed;
303  uint8_t i2c_address;
305 } haptic4_cfg_t;
306 
311 typedef enum
312 {
314  HAPTIC4_ERROR = -1
315 
317 
334 
348 err_t haptic4_init ( haptic4_t *ctx, haptic4_cfg_t *cfg );
349 
363 
378 err_t haptic4_generic_write ( haptic4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
379 
394 err_t haptic4_generic_read ( haptic4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
395 
396 
409 err_t haptic4_write_register ( haptic4_t *ctx, uint8_t reg, uint8_t data_in );
410 
423 err_t haptic4_read_register ( haptic4_t *ctx, uint8_t reg, uint8_t *data_out );
424 
439 err_t haptic4_write_register_bits ( haptic4_t *ctx, uint8_t reg, uint8_t mask, uint8_t data_in );
440 
452 
462 void haptic4_set_gp0 ( haptic4_t *ctx, uint8_t pin_state );
463 
473 void haptic4_set_gp1 ( haptic4_t *ctx, uint8_t pin_state );
474 
484 void haptic4_set_gp2 ( haptic4_t *ctx, uint8_t pin_state );
485 
495 
507 err_t haptic4_set_actuator_type ( haptic4_t *ctx, uint8_t type );
508 
520 err_t haptic4_set_actuator_abs_volt ( haptic4_t *ctx, float abs_volt );
521 
533 err_t haptic4_set_actuator_nom_volt ( haptic4_t *ctx, float nom_volt );
534 
546 err_t haptic4_set_actuator_imax ( haptic4_t *ctx, float max_curr );
547 
560 err_t haptic4_set_actuator_impedance ( haptic4_t *ctx, float impedance );
561 
573 err_t haptic4_set_actuator_lra_freq ( haptic4_t *ctx, float frequency );
574 
586 err_t haptic4_set_operation_mode ( haptic4_t *ctx, uint8_t op_mode );
587 
599 err_t haptic4_set_freq_track ( haptic4_t *ctx, uint8_t freq_track_en );
600 
612 err_t haptic4_set_acceleration_mode ( haptic4_t *ctx, uint8_t accel_en );
613 
626 err_t haptic4_set_vibration_level ( haptic4_t *ctx, float level );
627 
640 err_t haptic4_get_vibration_level ( haptic4_t *ctx, float *level );
641 
642 #ifdef __cplusplus
643 }
644 #endif
645 #endif // HAPTIC4_H
646  // haptic4
648 
649 // ------------------------------------------------------------------------ END
haptic4_cfg_t::gp0
pin_name_t gp0
Definition: haptic4.h:299
haptic4_set_acceleration_mode
err_t haptic4_set_acceleration_mode(haptic4_t *ctx, uint8_t accel_en)
Haptic 4 set accel mode function.
haptic4_get_int_pin
uint8_t haptic4_get_int_pin(haptic4_t *ctx)
Haptic 4 get INT pin function.
haptic4_t::gp1
digital_out_t gp1
Definition: haptic4.h:273
haptic4_check_communication
err_t haptic4_check_communication(haptic4_t *ctx)
Haptic 4 check communication function.
haptic4_t::i2c
i2c_master_t i2c
Definition: haptic4.h:281
haptic4_return_value_t
haptic4_return_value_t
Haptic 4 Click return value data.
Definition: haptic4.h:312
haptic4_cfg_t::sda
pin_name_t sda
Definition: haptic4.h:295
haptic4_write_register_bits
err_t haptic4_write_register_bits(haptic4_t *ctx, uint8_t reg, uint8_t mask, uint8_t data_in)
Haptic 4 write register bits function.
haptic4_set_operation_mode
err_t haptic4_set_operation_mode(haptic4_t *ctx, uint8_t op_mode)
Haptic 4 set operation mode function.
haptic4_write_register
err_t haptic4_write_register(haptic4_t *ctx, uint8_t reg, uint8_t data_in)
Haptic 4 write register function.
haptic4_set_freq_track
err_t haptic4_set_freq_track(haptic4_t *ctx, uint8_t freq_track_en)
Haptic 4 set freq track function.
haptic4_t::irq
digital_in_t irq
Definition: haptic4.h:278
haptic4_set_actuator_type
err_t haptic4_set_actuator_type(haptic4_t *ctx, uint8_t type)
Haptic 4 set actuator type function.
haptic4_set_gp1
void haptic4_set_gp1(haptic4_t *ctx, uint8_t pin_state)
Haptic 4 set GP1 pin state function.
haptic4_cfg_t
Haptic 4 Click configuration object.
Definition: haptic4.h:293
haptic4_t::gp0
digital_out_t gp0
Definition: haptic4.h:275
haptic4_t::slave_address
uint8_t slave_address
Definition: haptic4.h:284
haptic4_set_vibration_level
err_t haptic4_set_vibration_level(haptic4_t *ctx, float level)
Haptic 4 set vibration level function.
haptic4_t
Haptic 4 Click context object.
Definition: haptic4.h:271
haptic4_default_cfg
err_t haptic4_default_cfg(haptic4_t *ctx)
Haptic 4 default configuration function.
haptic4_set_gp0
void haptic4_set_gp0(haptic4_t *ctx, uint8_t pin_state)
Haptic 4 set GP0 pin state function.
haptic4_get_vibration_level
err_t haptic4_get_vibration_level(haptic4_t *ctx, float *level)
Haptic 4 get vibration level function.
haptic4_set_actuator_nom_volt
err_t haptic4_set_actuator_nom_volt(haptic4_t *ctx, float nom_volt)
Haptic 4 set actuator nom volt function.
HAPTIC4_ERROR
@ HAPTIC4_ERROR
Definition: haptic4.h:314
haptic4_set_actuator_imax
err_t haptic4_set_actuator_imax(haptic4_t *ctx, float max_curr)
Haptic 4 set actuator imax function.
haptic4_cfg_t::gp1
pin_name_t gp1
Definition: haptic4.h:297
haptic4_cfg_t::i2c_address
uint8_t i2c_address
Definition: haptic4.h:303
haptic4_cfg_t::scl
pin_name_t scl
Definition: haptic4.h:294
haptic4_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: haptic4.h:302
haptic4_t::gp2
digital_out_t gp2
Definition: haptic4.h:274
haptic4_cfg_setup
void haptic4_cfg_setup(haptic4_cfg_t *cfg)
Haptic 4 configuration object setup function.
haptic4_set_actuator_impedance
err_t haptic4_set_actuator_impedance(haptic4_t *ctx, float impedance)
Haptic 4 set actuator impedance function.
haptic4_set_actuator_abs_volt
err_t haptic4_set_actuator_abs_volt(haptic4_t *ctx, float abs_volt)
Haptic 4 set actuator abs volt function.
haptic4_generic_read
err_t haptic4_generic_read(haptic4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Haptic 4 I2C reading function.
haptic4_init
err_t haptic4_init(haptic4_t *ctx, haptic4_cfg_t *cfg)
Haptic 4 initialization function.
HAPTIC4_OK
@ HAPTIC4_OK
Definition: haptic4.h:313
haptic4_read_register
err_t haptic4_read_register(haptic4_t *ctx, uint8_t reg, uint8_t *data_out)
Haptic 4 read register function.
haptic4_set_actuator_lra_freq
err_t haptic4_set_actuator_lra_freq(haptic4_t *ctx, float frequency)
Haptic 4 set actuator lra freq function.
haptic4_cfg_t::irq
pin_name_t irq
Definition: haptic4.h:300
haptic4_set_gp2
void haptic4_set_gp2(haptic4_t *ctx, uint8_t pin_state)
Haptic 4 set GP2 pin state function.
haptic4_generic_write
err_t haptic4_generic_write(haptic4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Haptic 4 I2C writing function.
haptic4_cfg_t::gp2
pin_name_t gp2
Definition: haptic4.h:298