ultrasonic5  2.1.0.0
ultrasonic5.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 ULTRASONIC5_H
29 #define ULTRASONIC5_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 #include "drv_analog_in.h"
50 #include "drv_pwm.h"
51 
72 #define ULTRASONIC5_REG_BPF_CONFIG_1 0x10
73 #define ULTRASONIC5_REG_BPF_CONFIG_2 0x11
74 #define ULTRASONIC5_REG_DEV_CTRL_1 0x12
75 #define ULTRASONIC5_REG_DEV_CTRL_2 0x13
76 #define ULTRASONIC5_REG_DEV_CTRL_3 0x14
77 #define ULTRASONIC5_REG_VDRV_CTRL 0x16
78 #define ULTRASONIC5_REG_ECHO_INT_CONFIG 0x17
79 #define ULTRASONIC5_REG_ZC_CONFIG 0x18
80 #define ULTRASONIC5_REG_BURST_PULSE 0x1A
81 #define ULTRASONIC5_REG_TOF_CONFIG 0x1B
82 #define ULTRASONIC5_REG_DEV_STAT 0x1C
83 #define ULTRASONIC5_REG_DEVICE_ID 0x1D
84 #define ULTRASONIC5_REG_REV_ID 0x1E
85  // ultrasonic5_reg
87 
102 #define ULTRASONIC5_BPF_CONFIG_1_FC_TRIM_FRC 0x80
103 #define ULTRASONIC5_BPF_CONFIG_1_BYPASS 0x40
104 #define ULTRASONIC5_BPF_CONFIG_1_HPF_FREQ_MASK 0x3F
105 #define ULTRASONIC5_BPF_CONFIG_1_RESET 0x00
106 
111 #define ULTRASONIC5_BPF_CONFIG_2_Q_SEL_4 0x00
112 #define ULTRASONIC5_BPF_CONFIG_2_Q_SEL_5 0x10
113 #define ULTRASONIC5_BPF_CONFIG_2_Q_SEL_2 0x20
114 #define ULTRASONIC5_BPF_CONFIG_2_Q_SEL_3 0x30
115 #define ULTRASONIC5_BPF_CONFIG_2_Q_SEL_MASK 0x30
116 #define ULTRASONIC5_BPF_CONFIG_2_FC_TRIM_MASK 0x0F
117 #define ULTRASONIC5_BPF_CONFIG_2_RESET 0x00
118 
123 #define ULTRASONIC5_DEV_CTRL_1_LOGAMP_FRC 0x80
124 #define ULTRASONIC5_DEV_CTRL_1_LOGAMP_SLP_ADJ_MASK 0x70
125 #define ULTRASONIC5_DEV_CTRL_1_LOGAMP_INT_ADJ_MASK 0x0F
126 #define ULTRASONIC5_DEV_CTRL_1_RESET 0x00
127 
132 #define ULTRASONIC5_DEV_CTRL_2_LOGAMP_DIS_FIRST 0x80
133 #define ULTRASONIC5_DEV_CTRL_2_LOGAMP_DIS_LAST 0x40
134 #define ULTRASONIC5_DEV_CTRL_2_VOUT_SCALE_SEL_5V 0x04
135 #define ULTRASONIC5_DEV_CTRL_2_LNA_GAIN_15V 0x00
136 #define ULTRASONIC5_DEV_CTRL_2_LNA_GAIN_10V 0x01
137 #define ULTRASONIC5_DEV_CTRL_2_LNA_GAIN_20V 0x02
138 #define ULTRASONIC5_DEV_CTRL_2_LNA_GAIN_12_5V 0x03
139 #define ULTRASONIC5_DEV_CTRL_2_LNA_GAIN_MASK 0x03
140 #define ULTRASONIC5_DEV_CTRL_2_RESET 0x00
141 
146 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_64US 0x00
147 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_48US 0x04
148 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_32US 0x08
149 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_24US 0x0C
150 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_16US 0x10
151 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_8US 0x14
152 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_4US 0x18
153 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_DIS 0x1C
154 #define ULTRASONIC5_DEV_CTRL_3_DRV_PLS_FLT_DT_MASK 0x1C
155 #define ULTRASONIC5_DEV_CTRL_3_IO_MODE_0 0x00
156 #define ULTRASONIC5_DEV_CTRL_3_IO_MODE_1 0x01
157 #define ULTRASONIC5_DEV_CTRL_3_IO_MODE_2 0x02
158 #define ULTRASONIC5_DEV_CTRL_3_IO_MODE_3 0x03
159 #define ULTRASONIC5_DEV_CTRL_3_IO_MODE_MASK 0x03
160 #define ULTRASONIC5_DEV_CTRL_3_RESET 0x00
161 
166 #define ULTRASONIC5_VDRV_CTRL_DIS_VDRV_REG_LSTN 0x40
167 #define ULTRASONIC5_VDRV_CTRL_VDRV_HI_Z 0x20
168 #define ULTRASONIC5_VDRV_CTRL_VDRV_CURR_LVL_20MA 0x10
169 #define ULTRASONIC5_VDRV_CTRL_VDRV_VOLT_LVL_5V 0x00
170 #define ULTRASONIC5_VDRV_CTRL_VDRV_VOLT_LVL_MASK 0x0F
171 #define ULTRASONIC5_VDRV_CTRL_RESET 0x20
172 
177 #define ULTRASONIC5_ECHO_INT_CONFIG_CMP_EN 0x10
178 #define ULTRASONIC5_ECHO_INT_CONFIG_THR_SEL_MASK 0x0F
179 #define ULTRASONIC5_ECHO_INT_CONFIG_RESET 0x07
180 
185 #define ULTRASONIC5_ZC_CONFIG_CMP_EN 0x80
186 #define ULTRASONIC5_ZC_CONFIG_EN_ECHO_INT 0x40
187 #define ULTRASONIC5_ZC_CONFIG_CMP_IN_SEL 0x20
188 #define ULTRASONIC5_ZC_CONFIG_CMP_STG_SEL_MASK 0x18
189 #define ULTRASONIC5_ZC_CONFIG_CMP_HYST_MASK 0x07
190 #define ULTRASONIC5_ZC_CONFIG_RESET 0x14
191 
196 #define ULTRASONIC5_BURST_PULSE_HALF_BRG_MODE 0x80
197 #define ULTRASONIC5_BURST_PULSE_PRE_DRIVER_MODE 0x40
198 #define ULTRASONIC5_BURST_PULSE_BURST_PULSE_16 0x0F
199 #define ULTRASONIC5_BURST_PULSE_BURST_PULSE_MASK 0x3F
200 #define ULTRASONIC5_BURST_PULSE_RESET 0x00
201 
206 #define ULTRASONIC5_TOF_CONFIG_SLEEP_MODE_EN 0x80
207 #define ULTRASONIC5_TOF_CONFIG_STDBY_MODE_EN 0x40
208 #define ULTRASONIC5_TOF_CONFIG_VDRV_TRIGGER 0x02
209 #define ULTRASONIC5_TOF_CONFIG_CMD_TRIGGER 0x01
210 #define ULTRASONIC5_TOF_CONFIG_RESET 0x00
211 
216 #define ULTRASONIC5_DEF_FREQ 40000
217 #define ULTRASONIC5_DEF_DYTY 0.5f
218 
223 #define ULTRASONIC5_DEVICE_ID 0xB9
224 
229 #define ULTRASONIC5_ODD_PARITY 0x01
230 
239 #define ULTRASONIC5_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
240 #define ULTRASONIC5_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
241  // ultrasonic5_set
243 
258 #define ULTRASONIC5_MAP_MIKROBUS( cfg, mikrobus ) \
259  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
260  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
261  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
262  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
263  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
264  cfg.io1 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
265  cfg.io2 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
266  cfg.out4 = MIKROBUS( mikrobus, MIKROBUS_INT )
267  // ultrasonic5_map // ultrasonic5
270 
275 typedef struct
276 {
277  // Output pins
278  digital_out_t io1;
280  // Input pins
281  digital_in_t out4;
283  // Modules
284  spi_master_t spi;
285  analog_in_t adc;
286  pwm_t pwm;
288  // ctx variable
289  uint32_t pwm_freq;
290  pin_name_t chip_select;
292 } ultrasonic5_t;
293 
298 typedef struct
299 {
300  // Communication gpio pins
301  pin_name_t miso;
302  pin_name_t mosi;
303  pin_name_t sck;
304  pin_name_t cs;
305  pin_name_t an;
306  pin_name_t io2;
308  // Additional gpio pins
309  pin_name_t io1;
310  pin_name_t out4;
312  uint32_t spi_speed;
313  spi_master_mode_t spi_mode;
314  spi_master_chip_select_polarity_t cs_polarity;
316  analog_in_resolution_t resolution;
317  float vref;
319  uint32_t dev_pwm_freq;
322 
327 typedef enum
328 {
330  ULTRASONIC5_ERROR = -1
331 
333 
350 
365 
379 
392 err_t ultrasonic5_write_register ( ultrasonic5_t *ctx, uint8_t reg, uint8_t data_in );
393 
406 err_t ultrasonic5_read_register ( ultrasonic5_t *ctx, uint8_t reg, uint8_t *data_out );
407 
419 
429 
439 
449 
461 err_t ultrasonic5_read_an_pin_value ( ultrasonic5_t *ctx, uint16_t *data_out );
462 
476 err_t ultrasonic5_read_an_pin_voltage ( ultrasonic5_t *ctx, float *data_out );
477 
489 err_t ultrasonic5_set_duty_cycle ( ultrasonic5_t *ctx, float duty_cycle );
490 
502 
514 
515 #ifdef __cplusplus
516 }
517 #endif
518 #endif // ULTRASONIC5_H
519  // ultrasonic5
521 
522 // ------------------------------------------------------------------------ END
ultrasonic5_cfg_t
Ultrasonic 5 Click configuration object.
Definition: ultrasonic5.h:299
ultrasonic5_get_out4_pin
uint8_t ultrasonic5_get_out4_pin(ultrasonic5_t *ctx)
Ultrasonic 5 get out4 pin function.
ultrasonic5_cfg_t::mosi
pin_name_t mosi
Definition: ultrasonic5.h:302
ultrasonic5_t::out4
digital_in_t out4
Definition: ultrasonic5.h:281
ultrasonic5_read_an_pin_value
err_t ultrasonic5_read_an_pin_value(ultrasonic5_t *ctx, uint16_t *data_out)
Ultrasonic 5 read AN pin value function.
ultrasonic5_cfg_t::an
pin_name_t an
Definition: ultrasonic5.h:305
spi_specifics.h
This file contains SPI specific macros, functions, etc.
ultrasonic5_pwm_start
err_t ultrasonic5_pwm_start(ultrasonic5_t *ctx)
Ultrasonic 5 start PWM module.
ULTRASONIC5_ERROR
@ ULTRASONIC5_ERROR
Definition: ultrasonic5.h:330
ULTRASONIC5_OK
@ ULTRASONIC5_OK
Definition: ultrasonic5.h:329
ultrasonic5_default_cfg
err_t ultrasonic5_default_cfg(ultrasonic5_t *ctx)
Ultrasonic 5 default configuration function.
ultrasonic5_write_register
err_t ultrasonic5_write_register(ultrasonic5_t *ctx, uint8_t reg, uint8_t data_in)
Ultrasonic 5 data writing function.
ultrasonic5_t::pwm
pwm_t pwm
Definition: ultrasonic5.h:286
ultrasonic5_cfg_t::vref
float vref
Definition: ultrasonic5.h:317
ultrasonic5_read_register
err_t ultrasonic5_read_register(ultrasonic5_t *ctx, uint8_t reg, uint8_t *data_out)
Ultrasonic 5 data reading function.
ultrasonic5_cfg_t::resolution
analog_in_resolution_t resolution
Definition: ultrasonic5.h:316
ultrasonic5_t::spi
spi_master_t spi
Definition: ultrasonic5.h:284
ultrasonic5_cfg_t::io1
pin_name_t io1
Definition: ultrasonic5.h:309
ultrasonic5_init
err_t ultrasonic5_init(ultrasonic5_t *ctx, ultrasonic5_cfg_t *cfg)
Ultrasonic 5 initialization function.
ultrasonic5_cfg_t::miso
pin_name_t miso
Definition: ultrasonic5.h:301
ultrasonic5_pwm_stop
err_t ultrasonic5_pwm_stop(ultrasonic5_t *ctx)
Ultrasonic 5 stop PWM module.
ultrasonic5_read_an_pin_voltage
err_t ultrasonic5_read_an_pin_voltage(ultrasonic5_t *ctx, float *data_out)
Ultrasonic 5 read AN pin voltage level function.
ultrasonic5_t::chip_select
pin_name_t chip_select
Definition: ultrasonic5.h:290
ultrasonic5_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: ultrasonic5.h:319
ultrasonic5_cfg_t::io2
pin_name_t io2
Definition: ultrasonic5.h:306
ultrasonic5_cfg_t::spi_speed
uint32_t spi_speed
Definition: ultrasonic5.h:312
ultrasonic5_t
Ultrasonic 5 Click context object.
Definition: ultrasonic5.h:276
ultrasonic5_t::pwm_freq
uint32_t pwm_freq
Definition: ultrasonic5.h:289
ultrasonic5_cfg_setup
void ultrasonic5_cfg_setup(ultrasonic5_cfg_t *cfg)
Ultrasonic 5 configuration object setup function.
ultrasonic5_cfg_t::cs
pin_name_t cs
Definition: ultrasonic5.h:304
ultrasonic5_cfg_t::out4
pin_name_t out4
Definition: ultrasonic5.h:310
ultrasonic5_t::io1
digital_out_t io1
Definition: ultrasonic5.h:278
ultrasonic5_t::adc
analog_in_t adc
Definition: ultrasonic5.h:285
ultrasonic5_clear_io1_pin
void ultrasonic5_clear_io1_pin(ultrasonic5_t *ctx)
Ultrasonic 5 clear io1 pin function.
ultrasonic5_set_io1_pin
void ultrasonic5_set_io1_pin(ultrasonic5_t *ctx)
Ultrasonic 5 set io1 pin function.
ultrasonic5_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: ultrasonic5.h:314
ultrasonic5_check_communication
err_t ultrasonic5_check_communication(ultrasonic5_t *ctx)
Ultrasonic 5 check communication function.
ultrasonic5_cfg_t::sck
pin_name_t sck
Definition: ultrasonic5.h:303
ultrasonic5_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: ultrasonic5.h:313
ultrasonic5_return_value_t
ultrasonic5_return_value_t
Ultrasonic 5 Click return value data.
Definition: ultrasonic5.h:328
ultrasonic5_set_duty_cycle
err_t ultrasonic5_set_duty_cycle(ultrasonic5_t *ctx, float duty_cycle)
Ultrasonic 5 sets PWM duty cycle.