hodcap  2.1.0.0
hodcap.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 HODCAP_H
29 #define HODCAP_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_spi_master.h"
52 #include "spi_specifics.h"
53 
74 #define HODCAP_REG_SENSOR_ID 0x01
75 #define HODCAP_REG_STATUS 0x02
76 #define HODCAP_REG_I_DATA 0x03
77 #define HODCAP_REG_Q_DATA 0x04
78 #define HODCAP_REG_ADC_STATUS 0x05
79 #define HODCAP_REG_EDIV 0x20
80 #define HODCAP_REG_FREQ 0x21
81 #define HODCAP_REG_TXV 0x22
82 #define HODCAP_REG_MUX_CTRL 0x23
83 #define HODCAP_REG_SDG 0x24
84 #define HODCAP_REG_CBG 0x25
85 #define HODCAP_REG_DCLK 0x26
86 #define HODCAP_REG_PGA 0x27
87 #define HODCAP_REG_PGA_OFFSET_I 0x28
88 #define HODCAP_REG_PGA_OFFSET_Q 0x29
89 #define HODCAP_REG_ADCTL 0x2A
90 #define HODCAP_REG_BLANK 0x2B
91 #define HODCAP_REG_DIAG_MUX_CTRL 0x2C
92 #define HODCAP_REG_QUICK_READ 0x90
93 
98 #define HODCAP_CMD_WRITE 0x10
99 #define HODCAP_CMD_READ 0x00
100 #define HODCAP_CMD_QUICK_READ 0x90
101  // hodcap_reg
103 
118 #define HODCAP_SENSOR_ID 0xA431u
119 
124 #define HODCAP_ADC_DATA_READY 0x0001u
125 
130 #define HODCAP_SET_EDIV_12 0x0000u
131 #define HODCAP_SET_EDIV_11 0x0001u
132 #define HODCAP_SET_EDIV_10 0x0002u
133 #define HODCAP_SET_EDIV_9 0x0003u
134 #define HODCAP_SET_EDIV_8 0x0004u
135 #define HODCAP_SET_EDIV_7 0x0005u
136 #define HODCAP_SET_EDIV_6 0x0006u
137 #define HODCAP_SET_EDIV_5 0x0007u
138 #define HODCAP_SET_EDIV_4 0x0008u
139 #define HODCAP_SET_EDIV_3 0x0009u
140 #define HODCAP_SET_EDIV_2 0x000Au
141 #define HODCAP_SET_EDIV_1 0x000Bu
142 #define HODCAP_SET_EDIV_BIT_MASK 0x000Fu
143 
148 #define HODCAP_SET_FREQ_45_45KHZ 0x0000u
149 #define HODCAP_SET_FREQ_71_4KHZ 0x0001u
150 #define HODCAP_SET_FREQ_100KHZ 0x0002u
151 #define HODCAP_SET_FREQ_125KHZ 0x0003u
152 
157 #define HODCAP_SET_TXV_0V 0x0000u
158 #define HODCAP_SET_TXV_0_5V 0x0001u
159 #define HODCAP_SET_TXV_1V 0x0002u
160 
165 #define HODCAP_SET_MUX_CTRL_MODE_OPEN 0x0000u
166 #define HODCAP_SET_MUX_CTRL_MODE_SENSOR_DRV 0x0001u
167 #define HODCAP_SET_MUX_CTRL_MODE_COMMON 0x0002u
168 #define HODCAP_SET_MUX_CTRL_MODE_NO_OP 0x0003u
169 #define HODCAP_SET_MUX_CTRL_SEL_SEN0 0x0000u
170 #define HODCAP_SET_MUX_CTRL_SEL_SEN1 0x0004u
171 #define HODCAP_SET_MUX_CTRL_SEL_SEN2 0x0008u
172 #define HODCAP_SET_MUX_CTRL_SEL_SEN3 0x000Cu
173 #define HODCAP_SET_MUX_CTRL_SEL_SEN4 0x0010u
174 #define HODCAP_SET_MUX_CTRL_SEL_SEN5 0x0014u
175 #define HODCAP_SET_MUX_CTRL_SEL_SEN6 0x0018u
176 #define HODCAP_SET_MUX_CTRL_SEL_SEN7 0x001Cu
177 #define HODCAP_SET_MUX_CTRL_SEL_SEN8 0x0020u
178 #define HODCAP_SET_MUX_CTRL_SEL_SEN9 0x0024u
179 #define HODCAP_SET_MUX_CTRL_SEL_NONE 0x0028u
180 #define HODCAP_SET_MUX_CTRL_SEL_VAR_SEN 0x002Cu
181 #define HODCAP_SET_MUX_CTRL_SEL_BIT_MASK 0x003Cu
182 
187 #define HODCAP_TOTAL_NUMBER_OF_CHANNELS 10
188 
193 #define HODCAP_SET_PGA_GAIN_36 0x0000u
194 #define HODCAP_SET_PGA_GAIN_24 0x0001u
195 #define HODCAP_SET_PGA_GAIN_16 0x0002u
196 #define HODCAP_SET_PGA_GAIN_10_67 0x0003u
197 #define HODCAP_SET_PGA_GAIN_7_11 0x0004u
198 #define HODCAP_SET_PGA_GAIN_4_74 0x0005u
199 #define HODCAP_SET_PGA_GAIN_3_16 0x0006u
200 #define HODCAP_SET_PGA_GAIN_2_11 0x0007u
201 
206 #define HODCAP_SET_DEFAULT_PGA_OFFSET_I 96
207 #define HODCAP_SET_DEFAULT_PGA_OFFSET_Q 144
208 
213 #define HODCAP_SET_ADCTL_MODE_RESET_STOP 0x0000u
214 #define HODCAP_SET_ADCTL_MODE_START_SINGLE_CONVO 0x0001u
215 #define HODCAP_SET_ADCTL_MODE_START_CONT_CONVO 0x0002u
216 #define HODCAP_SET_ADCTL_MODE_STOP_CURRENT_CONVO 0x0003u
217 #define HODCAP_SET_ADCTL_MODE_BIT_MASK 0x0003u
218 #define HODCAP_SET_ADCTL_CS_CLK_250KHZ 0x0000u
219 #define HODCAP_SET_ADCTL_CS_CLK_500KHZ 0x0004u
220 #define HODCAP_SET_ADCTL_CS_CLK_1000KHZ 0x0008u
221 #define HODCAP_SET_ADCTL_ACCU_NUMB_OF_SAMPLES_4 0x0000u
222 #define HODCAP_SET_ADCTL_ACCU_NUMB_OF_SAMPLES_8 0x0010u
223 #define HODCAP_SET_ADCTL_ACCU_NUMB_OF_SAMPLES_16 0x0020u
224 #define HODCAP_SET_ADCTL_TS_400US 0x0000u
225 #define HODCAP_SET_ADCTL_TS_576US 0x0040u
226 #define HODCAP_SET_ADCTL_TS_664US 0x0080u
227 
232 #define HODCAP_SET_MUX_CTRL_PLUS_SEN0 0x0000u
233 #define HODCAP_SET_MUX_CTRL_PLUS_SEN1 0x0001u
234 #define HODCAP_SET_MUX_CTRL_PLUS_SEN2 0x0002u
235 #define HODCAP_SET_MUX_CTRL_PLUS_SEN3 0x0003u
236 #define HODCAP_SET_MUX_CTRL_PLUS_SEN4 0x0004u
237 #define HODCAP_SET_MUX_CTRL_PLUS_SEN5 0x0005u
238 #define HODCAP_SET_MUX_CTRL_PLUS_SEN6 0x0006u
239 #define HODCAP_SET_MUX_CTRL_PLUS_SEN7 0x0007u
240 #define HODCAP_SET_MUX_CTRL_PLUS_SEN8 0x0008u
241 #define HODCAP_SET_MUX_CTRL_PLUS_SEN9 0x0009u
242 #define HODCAP_SET_MUX_CTRL_PLUS_VAR_SEN 0x000Bu
243 #define HODCAP_SET_MUX_CTRL_PLUS_NONE 0x000Fu
244 #define HODCAP_SET_MUX_CTRL_MINUS_SEN0 0x0000u
245 #define HODCAP_SET_MUX_CTRL_MINUS_SEN1 0x0010u
246 #define HODCAP_SET_MUX_CTRL_MINUS_SEN2 0x0020u
247 #define HODCAP_SET_MUX_CTRL_MINUS_SEN3 0x0030u
248 #define HODCAP_SET_MUX_CTRL_MINUS_SEN4 0x0040u
249 #define HODCAP_SET_MUX_CTRL_MINUS_SEN5 0x0050u
250 #define HODCAP_SET_MUX_CTRL_MINUS_SEN6 0x0060u
251 #define HODCAP_SET_MUX_CTRL_MINUS_SEN7 0x0070u
252 #define HODCAP_SET_MUX_CTRL_MINUS_SEN8 0x0080u
253 #define HODCAP_SET_MUX_CTRL_MINUS_SEN9 0x0090u
254 #define HODCAP_SET_MUX_CTRL_MINUS_VAR_SEN 0x00B0u
255 #define HODCAP_SET_MUX_CTRL_MINUS_NONE 0x00F0u
256 
265 #define HODCAP_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
266 #define HODCAP_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
267  // hodcap_set
269 
284 #define HODCAP_MAP_MIKROBUS( cfg, mikrobus ) \
285  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
286  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
287  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
288  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
289  cfg.clk = MIKROBUS( mikrobus, MIKROBUS_PWM )
290  // hodcap_map // hodcap
293 
298 typedef struct
299 {
300  // Output pins
301  digital_out_t clk;
303  // Modules
304  spi_master_t spi;
306  pin_name_t chip_select;
308 } hodcap_t;
309 
314 typedef struct
315 {
316  // Communication gpio pins
317  pin_name_t miso;
318  pin_name_t mosi;
319  pin_name_t sck;
320  pin_name_t cs;
322  // Additional gpio pins
323  pin_name_t clk;
325  // static variable
326  uint32_t spi_speed;
327  spi_master_mode_t spi_mode;
328  spi_master_chip_select_polarity_t cs_polarity;
330 } hodcap_cfg_t;
331 
336 typedef struct
337 {
338  uint8_t r_ediv;
339  uint8_t otpf;
340  uint8_t ppsf;
341  uint8_t bpff;
342  uint8_t regf;
343  uint8_t ocsld;
344  uint8_t ocsen;
345  uint8_t test;
346  uint8_t tx1f;
347  uint8_t tx2f;
348  uint8_t pga1f;
349  uint8_t pga2f;
350  uint8_t nclk;
351  uint8_t cbf;
354 
359 typedef enum
360 {
362  HODCAP_ERROR = -1
363 
365 
382 
396 err_t hodcap_init ( hodcap_t *ctx, hodcap_cfg_t *cfg );
397 
411 
426 err_t hodcap_generic_write ( hodcap_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
427 
442 err_t hodcap_generic_read ( hodcap_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
443 
457 err_t hodcap_write_reg ( hodcap_t *ctx, uint8_t reg, uint16_t data_in );
458 
472 err_t hodcap_read_reg ( hodcap_t *ctx, uint8_t reg, uint16_t *data_out );
473 
486 err_t hodcap_get_sensor_id ( hodcap_t *ctx, uint16_t *sensor_id );
487 
501 
514 err_t hodcap_check_adc_data_ready ( hodcap_t *ctx, uint16_t *data_ready );
515 
528 err_t hodcap_set_adc_op_mode ( hodcap_t *ctx, uint16_t adc_op_mode );
529 
542 err_t hodcap_mux_channel_selection ( hodcap_t *ctx, uint8_t sen_pos );
543 
557 err_t hodcap_get_i_q_data ( hodcap_t *ctx, uint16_t *i_data, uint16_t *q_data );
558 
571 
582 void hodcap_set_clk_state ( hodcap_t *ctx, uint8_t clk_state );
583 
584 #ifdef __cplusplus
585 }
586 #endif
587 #endif // HODCAP_H
588  // hodcap
590 
591 // ------------------------------------------------------------------------ END
hodcap_t::spi
spi_master_t spi
Definition: hodcap.h:304
hodcap_get_status
err_t hodcap_get_status(hodcap_t *ctx, hodcap_status_t *status)
HOD CAP gets sensor status function.
hodcap_write_reg
err_t hodcap_write_reg(hodcap_t *ctx, uint8_t reg, uint16_t data_in)
HOD CAP write register function.
hodcap_get_i_q_data
err_t hodcap_get_i_q_data(hodcap_t *ctx, uint16_t *i_data, uint16_t *q_data)
HOD CAP gets the I and Q data function.
hodcap_status_t::otpf
uint8_t otpf
Definition: hodcap.h:339
hodcap_cfg_t::clk
pin_name_t clk
Definition: hodcap.h:323
hodcap_cfg_t::spi_speed
uint32_t spi_speed
Definition: hodcap.h:326
hodcap_status_t::tx2f
uint8_t tx2f
Definition: hodcap.h:347
hodcap_status_t::r_ediv
uint8_t r_ediv
Definition: hodcap.h:338
spi_specifics.h
This file contains SPI specific macros, functions, etc.
hodcap_return_value_t
hodcap_return_value_t
HOD CAP Click return value data.
Definition: hodcap.h:360
hodcap_default_cfg
err_t hodcap_default_cfg(hodcap_t *ctx)
HOD CAP default configuration function.
hodcap_mux_channel_selection
err_t hodcap_mux_channel_selection(hodcap_t *ctx, uint8_t sen_pos)
HOD CAP MUX channel selection function.
hodcap_set_clk_state
void hodcap_set_clk_state(hodcap_t *ctx, uint8_t clk_state)
HOD CAP set CLK pin state function.
hodcap_status_t::pga1f
uint8_t pga1f
Definition: hodcap.h:348
hodcap_init
err_t hodcap_init(hodcap_t *ctx, hodcap_cfg_t *cfg)
HOD CAP initialization function.
hodcap_cfg_t
HOD CAP Click configuration object.
Definition: hodcap.h:315
HODCAP_OK
@ HODCAP_OK
Definition: hodcap.h:361
hodcap_status_t::bpff
uint8_t bpff
Definition: hodcap.h:341
hodcap_read_reg
err_t hodcap_read_reg(hodcap_t *ctx, uint8_t reg, uint16_t *data_out)
HOD CAP read register function.
HODCAP_ERROR
@ HODCAP_ERROR
Definition: hodcap.h:362
hodcap_status_t
HOD CAP Click status object.
Definition: hodcap.h:337
hodcap_t
HOD CAP Click context object.
Definition: hodcap.h:299
hodcap_check_adc_data_ready
err_t hodcap_check_adc_data_ready(hodcap_t *ctx, uint16_t *data_ready)
HOD CAP check ADC data ready function.
hodcap_status_t::nclk
uint8_t nclk
Definition: hodcap.h:350
hodcap_t::clk
digital_out_t clk
Definition: hodcap.h:301
hodcap_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: hodcap.h:328
hodcap_status_t::ocsen
uint8_t ocsen
Definition: hodcap.h:344
hodcap_generic_read
err_t hodcap_generic_read(hodcap_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
HOD CAP data reading function.
hodcap_status_t::ocsld
uint8_t ocsld
Definition: hodcap.h:343
hodcap_wait_adc_data_ready
err_t hodcap_wait_adc_data_ready(hodcap_t *ctx)
HOD CAP wait ADC data ready function.
hodcap_cfg_t::sck
pin_name_t sck
Definition: hodcap.h:319
hodcap_status_t::tx1f
uint8_t tx1f
Definition: hodcap.h:346
hodcap_generic_write
err_t hodcap_generic_write(hodcap_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
HOD CAP data writing function.
hodcap_cfg_t::mosi
pin_name_t mosi
Definition: hodcap.h:318
hodcap_status_t::pga2f
uint8_t pga2f
Definition: hodcap.h:349
hodcap_status_t::ppsf
uint8_t ppsf
Definition: hodcap.h:340
hodcap_status_t::cbf
uint8_t cbf
Definition: hodcap.h:351
hodcap_status_t::test
uint8_t test
Definition: hodcap.h:345
hodcap_get_sensor_id
err_t hodcap_get_sensor_id(hodcap_t *ctx, uint16_t *sensor_id)
HOD CAP gets sensor ID function.
hodcap_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: hodcap.h:327
hodcap_cfg_setup
void hodcap_cfg_setup(hodcap_cfg_t *cfg)
HOD CAP configuration object setup function.
hodcap_cfg_t::miso
pin_name_t miso
Definition: hodcap.h:317
hodcap_t::chip_select
pin_name_t chip_select
Definition: hodcap.h:306
hodcap_status_t::regf
uint8_t regf
Definition: hodcap.h:342
hodcap_cfg_t::cs
pin_name_t cs
Definition: hodcap.h:320
hodcap_set_adc_op_mode
err_t hodcap_set_adc_op_mode(hodcap_t *ctx, uint16_t adc_op_mode)
HOD CAP sets the ADC operation mode function.