rtd2  2.0.0.0
rtd2.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef RTD2_H
36 #define RTD2_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_spi_master.h"
55 #include "spi_specifics.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define RTD2_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
73  cfg.str = MIKROBUS( mikrobus, MIKROBUS_AN ); \
74  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
75  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_INT )
76 
86 #define RTD2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
87 #define RTD2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
88 
93 #define RTD2_REG_MUX0 0x00
94 #define RTD2_REG_VBIAS 0x01
95 #define RTD2_REG_MUX1 0x02
96 #define RTD2_REG_SYS0 0x03
97 #define RTD2_REG_OFC0 0x04
98 #define RTD2_REG_OFC1 0x05
99 #define RTD2_REG_OFC2 0x06
100 #define RTD2_REG_FSC0 0x07
101 #define RTD2_REG_FSC1 0x08
102 #define RTD2_REG_FSC2 0x09
103 #define RTD2_REG_IDAC0 0x0A
104 #define RTD2_REG_IDAC1 0x0B
105 #define RTD2_REG_GPIOCFG 0x0C
106 #define RTD2_REG_GPIODIR 0x0D
107 #define RTD2_REG_GPIODAT 0x0E
108 
119 #define RTD2_CMD_WAKEUP 0x01
120 #define RTD2_CMD_SLEEP 0x03
121 #define RTD2_CMD_SYNC 0x05
122 #define RTD2_CMD_RESET 0x07
123 #define RTD2_CMD_NOP 0xFF
124 
130 #define RTD2_CMD_RDATA 0x12
131 #define RTD2_CMD_RDATAC 0x14
132 #define RTD2_CMD_SDATAC 0x16
133 
139 #define RTD2_CMD_RREG 0x20
140 
146 #define RTD2_CMD_WREG 0x40
147 
153 #define RTD2_CMD_SYSOCAL 0x60
154 #define RTD2_CMD_SYSGCAL 0x61
155 #define RTD2_CMD_SELFOCAL 0x62
156 
162 #define RTD2_RW_DUMMY 0xFF
163 
175 #define RTD2_BCS_OFF 0x00
176 #define RTD2_BCS_500 0x40
177 #define RTD2_BCS_2 0x80
178 #define RTD2_BCS_10 0xC0
179 
185 #define RTD2_AINP0 0x00
186 #define RTD2_AINP1 0x08
187 #define RTD2_AINP2 0x10
188 #define RTD2_AINP3 0x18
189 #define RTD2_AIN_POS 0x38
190 
196 #define RTD2_AINN0 0x00
197 #define RTD2_AINN1 0x01
198 #define RTD2_AINN2 0x02
199 #define RTD2_AINN3 0x03
200 #define RTD2_AIN_NEG 0x07
201 
207 #define RTD2_VBIAS_OFF 0x00
208 #define RTD2_VBIAS0 0x01
209 #define RTD2_VBIAS1 0x02
210 #define RTD2_VBIAS2 0x04
211 #define RTD2_VBIAS3 0x08
212 
224 #define RTD2_INT_VREF_OFF 0x00
225 #define RTD2_INT_VREF_ON 0x20
226 #define RTD2_INT_VREF_CONV 0x40
227 
233 #define RTD2_REF0 0x00
234 #define RTD2_INT 0x10
235 #define RTD2_INT_REF0 0x18
236 
242 #define RTD2_MEAS_NORM 0x00
243 #define RTD2_MEAS_OFFSET 0x01
244 #define RTD2_MEAS_GAIN 0x02
245 #define RTD2_MEAS_TEMP 0x03
246 #define RTD2_MEAS_REF0 0x05
247 #define RTD2_MEAS_AVDD 0x06
248 #define RTD2_MEAS_DVDD 0x07
249 
261 #define RTD2_GAIN_1 0x00
262 #define RTD2_GAIN_2 0x10
263 #define RTD2_GAIN_4 0x20
264 #define RTD2_GAIN_8 0x30
265 #define RTD2_GAIN_16 0x40
266 #define RTD2_GAIN_32 0x50
267 #define RTD2_GAIN_64 0x60
268 #define RTD2_GAIN_128 0x70
269 
275 #define RTD2_DR_5 0x00
276 #define RTD2_DR_10 0x01
277 #define RTD2_DR_20 0x02
278 #define RTD2_DR_40 0x03
279 #define RTD2_DR_80 0x04
280 #define RTD2_DR_160 0x05
281 #define RTD2_DR_320 0x06
282 #define RTD2_DR_640 0x07
283 #define RTD2_DR_1000 0x08
284 #define RTD2_DR_2000 0x0F
285 
297 #define RTD2_IDAC_ID 0x90
298 
304 #define RTD2_DRDY_OFF 0x00
305 #define RTD2_DRDY_ON 0x08
306 
312 #define RTD2_IDAC_OFF 0x00
313 #define RTD2_IDAC_50 0x01
314 #define RTD2_IDAC_100 0x02
315 #define RTD2_IDAC_250 0x03
316 #define RTD2_IDAC_500 0x04
317 #define RTD2_IDAC_750 0x05
318 #define RTD2_IDAC_1000 0x06
319 #define RTD2_IDAC_1500 0x07
320 
332 #define RTD2_IDAC1_A0 0xCC
333 #define RTD2_IDAC1_A1 0xDC
334 #define RTD2_IDAC1_A2 0xEC
335 #define RTD2_IDAC1_A3 0xFC
336 
342 #define RTD2_IDAC2_A0 0xCC
343 #define RTD2_IDAC2_A1 0xCD
344 #define RTD2_IDAC2_A2 0xCE
345 #define RTD2_IDAC2_A3 0xCF
346 
358 #define RTD2_GPIO_0 0x01
359 #define RTD2_GPIO_1 0x02
360 #define RTD2_GPIO_2 0x04
361 #define RTD2_GPIO_3 0x08
362 
374 #define RTD2_IO_0 0x01
375 #define RTD2_IO_1 0x02
376 #define RTD2_IO_2 0x04
377 #define RTD2_IO_3 0x08
378 
385 #define RTD2_OUT_0 0x01
386 #define RTD2_OUT_1 0x02
387 #define RTD2_OUT_2 0x04
388 #define RTD2_OUT_3 0x08
389 
395 #define RTD2_PIN_LOW 0x00
396 #define RTD2_PIN_HIGH 0x01
397 
403 #define RTD2_ERROR 0x00
404 #define RTD2_SUCCESS 0x01
405 
411 #define RTD2_START_CONVERSION_DISABLE 0x00
412 #define RTD2_START_CONVERSION_ENABLE 0x01
413 
419 #define RTD2_NEW_DATA_IS_NOT_READY 0x00
420 #define RTD2_NEW_DATA_IS_READY 0x01
421 
427 #define COEFF_PT100_RESISTANCE_OHMS_0_C 100.00000000
428 #define COEFF_ALPHA 0.3920000000
429 #define COEFF_ADC_RES_RT_P 0.0000753846
430 #define COEFF_ADC_COR_FACT 50.980300000
431  // End group macro
433 // --------------------------------------------------------------- PUBLIC TYPES
442 typedef struct
443 {
444  digital_out_t cs;
445  // Output pins
446 
447  digital_out_t str;
448  digital_out_t rst;
449 
450  // Input pins
451  digital_in_t rdy;
452 
453  // Modules
454  spi_master_t spi;
455  pin_name_t chip_select;
456 
457 } rtd2_t;
458 
462 typedef struct
463 {
464  // Communication gpio pins
465  pin_name_t miso;
466  pin_name_t mosi;
467  pin_name_t sck;
468  pin_name_t cs;
469 
470  // Additional gpio pins
471  pin_name_t str;
472  pin_name_t rst;
473  pin_name_t rdy;
474 
475  // static variable
476  uint32_t spi_speed;
477  spi_master_mode_t spi_mode;
478  spi_master_chip_select_polarity_t cs_polarity;
479 
480 } rtd2_cfg_t;
481  // End types group
483 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
484 
489 #ifdef __cplusplus
490 extern "C"{
491 #endif
492 
502 
512 err_t rtd2_init ( rtd2_t *ctx, rtd2_cfg_t *cfg );
513 
533 void rtd2_default_cfg ( rtd2_t *ctx );
534 
547 void rtd2_hw_reset ( rtd2_t *ctx );
548 
562 void rtd2_toggle_cs ( rtd2_t *ctx, uint8_t cs_state );
563 
577 void rtd2_enable_start ( rtd2_t *ctx, uint8_t en_start );
578 
595 uint8_t rtd2_check_interupt ( rtd2_t *ctx );
596 
608 void rtd2_send_cmd ( rtd2_t *ctx, uint8_t cmd );
609 
620 void rtd2_set_wakeup ( rtd2_t *ctx );
621 
632 void rtd2_set_sleep ( rtd2_t *ctx );
633 
645 void rtd2_set_sync ( rtd2_t *ctx );
646 
658 void rtd2_set_reset ( rtd2_t *ctx );
659 
672 
685 
698 
716 void rtd2_write_continuous ( rtd2_t *ctx, uint8_t reg, uint8_t *p_tx_data, uint8_t n_bytes );
717 
735 void rtd2_read_continuous ( rtd2_t *ctx, uint8_t reg, uint8_t *p_rx_data, uint8_t n_bytes );
736 
752 void rtd2_write_reg ( rtd2_t *ctx, uint8_t reg, uint8_t tx_data );
753 
770 uint8_t rtd2_read_reg ( rtd2_t *ctx, uint8_t reg );
771 
792 uint8_t rtd2_set_burn_out_source ( rtd2_t *ctx, uint8_t burn_out );
793 
817 void rtd2_set_mux_sel ( rtd2_t *ctx, uint8_t adc_pos, uint8_t adc_neg );
818 
840 uint8_t rtd2_set_bias ( rtd2_t *ctx, uint8_t bias );
841 
861 uint8_t rtd2_set_int_ref ( rtd2_t *ctx, uint8_t s_ref );
862 
882 void rtd2_set_system_monitor ( rtd2_t *ctx, uint8_t sys_mon );
883 
916 void rtd2_set_system_control ( rtd2_t *ctx, uint8_t gain, uint8_t output_rate );
917 
938 void rtd2_set_idac_current_mag ( rtd2_t *ctx, uint8_t c_idac );
939 
962 void rtd2_set_idac_current_out ( rtd2_t *ctx, uint8_t c_out_1, uint8_t c_out_2 );
963 
979 
992 int32_t rtd2_read_output_data ( rtd2_t *ctx );
993 
1008 float rtd2_calc_temperature ( int32_t adc_val );
1009 
1024 
1025 #ifdef __cplusplus
1026 }
1027 #endif
1028 #endif // _RTD2_H_
1029  // End public_function group
1032 
1033 // ------------------------------------------------------------------------- END
rtd2_set_system_control
void rtd2_set_system_control(rtd2_t *ctx, uint8_t gain, uint8_t output_rate)
Set system control function.
rtd2_cfg_t::sck
pin_name_t sck
Definition: rtd2.h:467
rtd2_t::chip_select
pin_name_t chip_select
Definition: rtd2.h:455
rtd2_set_mux_sel
void rtd2_set_mux_sel(rtd2_t *ctx, uint8_t adc_pos, uint8_t adc_neg)
Set multiplexer selection function.
rtd2_set_idac_current_out
void rtd2_set_idac_current_out(rtd2_t *ctx, uint8_t c_out_1, uint8_t c_out_2)
Set IDAC current out function.
rtd2_t::cs
digital_out_t cs
Definition: rtd2.h:444
rtd2_set_int_ref
uint8_t rtd2_set_int_ref(rtd2_t *ctx, uint8_t s_ref)
Set int ref function.
rtd2_set_bias
uint8_t rtd2_set_bias(rtd2_t *ctx, uint8_t bias)
Set bias function.
rtd2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: rtd2.h:477
rtd2_get_temperature
float rtd2_get_temperature(rtd2_t *ctx)
Get temperature function.
rtd2_calc_temperature
float rtd2_calc_temperature(int32_t adc_val)
Calculate temperature function.
rtd2_set_system_monitor
void rtd2_set_system_monitor(rtd2_t *ctx, uint8_t sys_mon)
Set system monitor function.
rtd2_send_cmd
void rtd2_send_cmd(rtd2_t *ctx, uint8_t cmd)
Send command function.
rtd2_toggle_cs
void rtd2_toggle_cs(rtd2_t *ctx, uint8_t cs_state)
Toggle CS pin function.
rtd2_set_sleep
void rtd2_set_sleep(rtd2_t *ctx)
Set to sleep function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
rtd2_t::rst
digital_out_t rst
Definition: rtd2.h:448
rtd2_cfg_t::rdy
pin_name_t rdy
Definition: rtd2.h:473
rtd2_t
Click ctx object definition.
Definition: rtd2.h:443
rtd2_enable_start
void rtd2_enable_start(rtd2_t *ctx, uint8_t en_start)
Enable conversion start function.
rtd2_cfg_t::spi_speed
uint32_t spi_speed
Definition: rtd2.h:476
rtd2_set_read_data_once
void rtd2_set_read_data_once(rtd2_t *ctx)
Set read data once function.
rtd2_cfg_t::rst
pin_name_t rst
Definition: rtd2.h:472
rtd2_cfg_t
Click configuration structure definition.
Definition: rtd2.h:463
rtd2_set_idac_current_mag
void rtd2_set_idac_current_mag(rtd2_t *ctx, uint8_t c_idac)
Set IDAC current magnitude function.
rtd2_cfg_t::miso
pin_name_t miso
Definition: rtd2.h:465
rtd2_cfg_t::mosi
pin_name_t mosi
Definition: rtd2.h:466
rtd2_check_new_data_ready
uint8_t rtd2_check_new_data_ready(rtd2_t *ctx)
Check new data ready function.
rtd2_t::str
digital_out_t str
Definition: rtd2.h:447
rtd2_cfg_setup
void rtd2_cfg_setup(rtd2_cfg_t *cfg)
Config Object Initialization function.
rtd2_set_sync
void rtd2_set_sync(rtd2_t *ctx)
Set sync function.
rtd2_set_read_data_continuous
void rtd2_set_read_data_continuous(rtd2_t *ctx)
Set read data continuous function.
rtd2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: rtd2.h:478
rtd2_read_output_data
int32_t rtd2_read_output_data(rtd2_t *ctx)
Read output data function.
rtd2_cfg_t::str
pin_name_t str
Definition: rtd2.h:471
rtd2_default_cfg
void rtd2_default_cfg(rtd2_t *ctx)
Set default configuration function.
rtd2_set_burn_out_source
uint8_t rtd2_set_burn_out_source(rtd2_t *ctx, uint8_t burn_out)
Set burn out source function.
rtd2_hw_reset
void rtd2_hw_reset(rtd2_t *ctx)
Hardware reset function.
rtd2_t::rdy
digital_in_t rdy
Definition: rtd2.h:451
rtd2_set_wakeup
void rtd2_set_wakeup(rtd2_t *ctx)
Set wakeup function.
rtd2_t::spi
spi_master_t spi
Definition: rtd2.h:454
rtd2_read_reg
uint8_t rtd2_read_reg(rtd2_t *ctx, uint8_t reg)
Read the register function.
rtd2_init
err_t rtd2_init(rtd2_t *ctx, rtd2_cfg_t *cfg)
Initialization function.
rtd2_set_stop_data
void rtd2_set_stop_data(rtd2_t *ctx)
Set stop data function.
rtd2_set_reset
void rtd2_set_reset(rtd2_t *ctx)
Set reset function.
rtd2_read_continuous
void rtd2_read_continuous(rtd2_t *ctx, uint8_t reg, uint8_t *p_rx_data, uint8_t n_bytes)
Continuous read the data function.
rtd2_write_continuous
void rtd2_write_continuous(rtd2_t *ctx, uint8_t reg, uint8_t *p_tx_data, uint8_t n_bytes)
Continuous write the data function.
rtd2_write_reg
void rtd2_write_reg(rtd2_t *ctx, uint8_t reg, uint8_t tx_data)
Write the register function.
rtd2_cfg_t::cs
pin_name_t cs
Definition: rtd2.h:468
rtd2_check_interupt
uint8_t rtd2_check_interupt(rtd2_t *ctx)
Check interrupt status function.