spiisolator  2.0.0.0
spiisolator.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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef SPIISOLATOR_H
35 #define SPIISOLATOR_H
36 
37 #include "mikrosdk_version.h"
38 
39 #ifdef __GNUC__
40 #if mikroSDK_GET_VERSION < 20800ul
41 #include "rcu_delays.h"
42 #else
43 #include "delays.h"
44 #endif
45 #endif
46 
47 #include "drv_digital_out.h"
48 #include "drv_digital_in.h"
49 #include "drv_spi_master.h"
50 
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
63 #define SPIISOLATOR_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
65  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
66  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
67  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
68 
74 #define SPIISOLATOR_RETVAL uint8_t
75 
76 #define SPIISOLATOR_OK 0x00
77 #define SPIISOLATOR_INIT_ERROR 0xFF
78 
84 #define SPIISOLATOR_PRESSURE3_SPI_CMD_WRITE 0x7F
85 #define SPIISOLATOR_PRESSURE3_SPI_CMD_READ 0x80
86 
92 #define SPIISOLATOR_PRESSURE3_REG_PSR_B2 0x00
93 #define SPIISOLATOR_PRESSURE3_REG_PSR_B1 0x01
94 #define SPIISOLATOR_PRESSURE3_REG_PSR_B0 0x02
95 #define SPIISOLATOR_PRESSURE3_REG_TMP_B2 0x03
96 #define SPIISOLATOR_PRESSURE3_REG_TMP_B1 0x04
97 #define SPIISOLATOR_PRESSURE3_REG_TMP_B0 0x05
98 #define SPIISOLATOR_PRESSURE3_REG_PRS_CFG 0x06
99 #define SPIISOLATOR_PRESSURE3_REG_TMP_CFG 0x07
100 #define SPIISOLATOR_PRESSURE3_REG_MEAS_CFG 0x08
101 #define SPIISOLATOR_PRESSURE3_REG_CFG_REG 0x09
102 #define SPIISOLATOR_PRESSURE3_REG_INT_STS 0x0A
103 #define SPIISOLATOR_PRESSURE3_REG_FIFO_STS 0x0B
104 #define SPIISOLATOR_PRESSURE3_REG_RESET 0x0C
105 #define SPIISOLATOR_PRESSURE3_REG_PRODUCT_ID 0x0D
106 #define SPIISOLATOR_PRESSURE3_REG_COEF_C0_MBS 0x10
107 #define SPIISOLATOR_PRESSURE3_REG_COEF_C0_LBS_C1_MBS 0x11
108 #define SPIISOLATOR_PRESSURE3_REG_COEF_C1_LBS 0x12
109 #define SPIISOLATOR_PRESSURE3_REG_COEF_C00_MBS 0x13
110 #define SPIISOLATOR_PRESSURE3_REG_COEF_C00_LSB 0x14
111 #define SPIISOLATOR_PRESSURE3_REG_COEF_C00_XLSB_C10_MSB 0x15
112 #define SPIISOLATOR_PRESSURE3_REG_COEF_C10_LSB 0x16
113 #define SPIISOLATOR_PRESSURE3_REG_COEF_C10_XLSB 0x17
114 #define SPIISOLATOR_PRESSURE3_REG_COEF_C01_MSB 0x18
115 #define SPIISOLATOR_PRESSURE3_REG_COEF_C01_LSB 0x19
116 #define SPIISOLATOR_PRESSURE3_REG_COEF_C11_MSB 0x1A
117 #define SPIISOLATOR_PRESSURE3_REG_COEF_C11_LSB 0x1B
118 #define SPIISOLATOR_PRESSURE3_REG_COEF_C20_MSB 0x1C
119 #define SPIISOLATOR_PRESSURE3_REG_COEF_C20_LSB 0x1D
120 #define SPIISOLATOR_PRESSURE3_REG_COEF_C21_MSB 0x1E
121 #define SPIISOLATOR_PRESSURE3_REG_COEF_C21_LSB 0x1F
122 #define SPIISOLATOR_PRESSURE3_REG_COEF_C30_MSB 0x20
123 #define SPIISOLATOR_PRESSURE3_REG_COEF_C30_LSB 0x21
124 #define SPIISOLATOR_PRESSURE3_REG_COEF_SRCE 0x28
125 
131 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_1_PER_SEC 0x00
132 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_2_PER_SEC 0x10
133 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_4_PER_SEC 0x20
134 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_8_PER_SEC 0x30
135 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_16_PER_SEC 0x40
136 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_32_PER_SEC 0x50
137 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_64_PER_SEC 0x60
138 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_RATE_128_PER_SEC 0x70
139 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_1_PER_SEC 0x00
140 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_2_PER_SEC 0x01
141 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_4_PER_SEC 0x02
142 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_8_PER_SEC 0x03
143 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_16_PER_SEC 0x04
144 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_32_PER_SEC 0x05
145 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_64_PER_SEC 0x06
146 #define SPIISOLATOR_PRESSURE3_BIT_MASK_PM_PRC_128_PER_SEC 0x07
147 
153 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_EXT_INTERNAL_SENSOR 0x00
154 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_EXT_EXTERNAL_SENSOR 0x80
155 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_1_PER_SEC 0x00
156 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_2_PER_SEC 0x10
157 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_4_PER_SEC 0x20
158 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_8_PER_SEC 0x30
159 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_16_PER_SEC 0x40
160 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_32_PER_SEC 0x50
161 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_64_PER_SEC 0x60
162 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_RATE_128_PER_SEC 0x70
163 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_1_PER_SEC 0x00
164 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_2_PER_SEC 0x01
165 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_4_PER_SEC 0x02
166 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_8_PER_SEC 0x03
167 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_16_PER_SEC 0x04
168 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_32_PER_SEC 0x05
169 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_64_PER_SEC 0x06
170 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_PRC_128_PER_SEC 0x07
171 
177 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_COEF_RDY_AVB 0x80
178 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_COEF_RDY_NO_AVB 0x00
179 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_SENSOR_RDY_CMP 0x40
180 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_SENSOR_RDY_NO_CMP 0x00
181 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_TMP_RDY 0x20
182 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_PRS_RDY 0x10
183 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_MEAS_CTRL_IDLE 0x00
184 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_MEAS_CTRL_PRS 0x01
185 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_MEAS_CTRL_TMP 0x02
186 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_MEAS_CTRL_CNT_PRS 0x05
187 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_MEAS_CTRL_CNT_TMP 0x06
188 #define SPIISOLATOR_PRESSURE3_BIT_MASK_MEAS_CFG_MEAS_CTRL_CNT_PRS_TMP 0x07
189 
195 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_HL_LOW 0x00
196 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_HL_HIGH 0x80
197 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_FIFO_DISABLE 0x00
198 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_FIFO_ENABLE 0x40
199 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_TMP_DISABLE 0x00
200 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_TMP_ENABLE 0x20
201 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_PRS_DISABLE 0x00
202 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_INT_PRS_ENABLE 0x10
203 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_T_SHIFT_NO_SHIFT 0x00
204 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_T_SHIFT_SHIFT 0x08
205 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_P_SHIFT_NO_SHIFT 0x00
206 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_P_SHIFT_SHIFT 0x04
207 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_FIFO_EN_DISABLE 0x00
208 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_FIFO_EN_ENABLE 0x02
209 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_SPI_MODE_DISABLE 0x00
210 #define SPIISOLATOR_PRESSURE3_BIT_MASK_CFG_REG_SPI_MODE_ENABLE 0x01
211 
217 #define SPIISOLATOR_PRESSURE3_BIT_MASK_INT_STS_INT_FIFO_FULL_NO_ACTIVE 0x00
218 #define SPIISOLATOR_PRESSURE3_BIT_MASK_INT_STS_INT_FIFO_FULL_ACTIVE 0x04
219 #define SPIISOLATOR_PRESSURE3_BIT_MASK_INT_STS_INT_TMP_NO_ACTIVE 0x00
220 #define SPIISOLATOR_PRESSURE3_BIT_MASK_INT_STS_INT_TMP_ACTIVE 0x02
221 #define SPIISOLATOR_PRESSURE3_BIT_MASK_INT_STS_INT_PRS_NO_ACTIVE 0x00
222 #define SPIISOLATOR_PRESSURE3_BIT_MASK_INT_STS_INT_PRS_ACTIVE 0x01
223 
229 #define SPIISOLATOR_PRESSURE3_BIT_MASK_FIFO_STS_FIFO_FULL_NO 0x00
230 #define SPIISOLATOR_PRESSURE3_BIT_MASK_FIFO_STS_FIFO_FULL_FULL 0x02
231 #define SPIISOLATOR_PRESSURE3_BIT_MASK_FIFO_STS_FIFO_EMPTY_NO 0x00
232 #define SPIISOLATOR_PRESSURE3_BIT_MASK_FIFO_STS_FIFO_EMPTY_EMPTY 0x01
233 
239 #define SPIISOLATOR_PRESSURE3_BIT_MASK_RESET_SOFT_RST 0x09
240 
246 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_COEF_SRCE_INTERNAL_SENSOR 0x00
247 #define SPIISOLATOR_PRESSURE3_BIT_MASK_TMP_COEF_SRCE_EXTERNAL_SENSOR 0x80
248  // End group macro
251 // --------------------------------------------------------------- PUBLIC TYPES
260 typedef struct
261 {
262 
263  uint8_t reg_address;
264  uint8_t cmd;
265 
266 } addr_cmd_t;
267 
268 
272 typedef struct
273 {
274  digital_out_t cs;
275 
276  // Modules
277 
278  spi_master_t spi;
279  pin_name_t chip_select;
280 
281 } spiisolator_t;
282 
286 typedef struct
287 {
288  // Communication gpio pins
289 
290  pin_name_t miso;
291  pin_name_t mosi;
292  pin_name_t sck;
293  pin_name_t cs;
294 
295  // static variable
296 
297  uint32_t spi_speed;
298  spi_master_mode_t spi_mode;
299  spi_master_chip_select_polarity_t cs_polarity;
300 
302  // End types group
304 
305 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
306 
311 #ifdef __cplusplus
312 extern "C"{
313 #endif
314 
324 
333 
346 (
347  spiisolator_t *ctx,
348  uint8_t *wr_buf,
349  uint16_t wr_len,
350  uint8_t *rd_buf,
351  uint16_t rd_len
352 );
353 
354 
355 void spiisolator_write_cmd ( spiisolator_t *ctx, uint8_t cmd, uint8_t tx_data );
356 
369 void spiisolator_write_byte ( spiisolator_t *ctx, uint8_t reg_address, uint8_t write_cmd, uint8_t write_data );
370 
384 uint8_t spiisolator_read_byte ( spiisolator_t *ctx, uint8_t reg_address, uint8_t read_cmd );
385 
397 void spiisolator_write_data ( spiisolator_t *ctx, uint8_t reg_address, uint8_t write_cmd, uint16_t write_data );
398 
410 uint16_t spiisolator_read_data ( spiisolator_t *ctx, uint8_t reg_address, uint8_t read_cmd );
411 
424 void spiisolator_write_bytes ( spiisolator_t *ctx, uint8_t reg_address, uint8_t write_cmd, uint8_t *write_data, uint16_t n_bytes );
425 
438 void spiisolator_read_bytes ( spiisolator_t *ctx,uint8_t reg_address, uint8_t read_cmd, uint8_t *read_data, uint16_t n_bytes );
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 #endif // _SPIISOLATOR_H_
444  // End public_function group
447 
448 // ------------------------------------------------------------------------- END
spiisolator_cfg_t::spi_speed
uint32_t spi_speed
Definition: spiisolator.h:297
spiisolator_cfg_t::sck
pin_name_t sck
Definition: spiisolator.h:292
spiisolator_read_bytes
void spiisolator_read_bytes(spiisolator_t *ctx, uint8_t reg_address, uint8_t read_cmd, uint8_t *read_data, uint16_t n_bytes)
Sequential read function.
SPIISOLATOR_RETVAL
#define SPIISOLATOR_RETVAL
Definition: spiisolator.h:74
spiisolator_cfg_t::mosi
pin_name_t mosi
Definition: spiisolator.h:291
spiisolator_t::chip_select
pin_name_t chip_select
Definition: spiisolator.h:279
spiisolator_generic_transfer
void spiisolator_generic_transfer(spiisolator_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
spiisolator_cfg_t::cs
pin_name_t cs
Definition: spiisolator.h:293
spiisolator_read_data
uint16_t spiisolator_read_data(spiisolator_t *ctx, uint8_t reg_address, uint8_t read_cmd)
Read 16-bit data function.
spiisolator_cfg_t
Click configuration structure definition.
Definition: spiisolator.h:287
spiisolator_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: spiisolator.h:298
spiisolator_t
Click ctx object definition.
Definition: spiisolator.h:273
addr_cmd_t
Definition: spiisolator.h:261
spiisolator_write_byte
void spiisolator_write_byte(spiisolator_t *ctx, uint8_t reg_address, uint8_t write_cmd, uint8_t write_data)
Write the byte of data function.
spiisolator_t::cs
digital_out_t cs
Definition: spiisolator.h:274
spiisolator_read_byte
uint8_t spiisolator_read_byte(spiisolator_t *ctx, uint8_t reg_address, uint8_t read_cmd)
Read the byte of data function.
spiisolator_cfg_setup
void spiisolator_cfg_setup(spiisolator_cfg_t *cfg)
Config Object Initialization function.
spiisolator_init
SPIISOLATOR_RETVAL spiisolator_init(spiisolator_t *ctx, spiisolator_cfg_t *cfg)
Initialization function.
addr_cmd_t::cmd
uint8_t cmd
Definition: spiisolator.h:264
spiisolator_cfg_t::miso
pin_name_t miso
Definition: spiisolator.h:290
spiisolator_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: spiisolator.h:299
spiisolator_write_cmd
void spiisolator_write_cmd(spiisolator_t *ctx, uint8_t cmd, uint8_t tx_data)
spiisolator_write_data
void spiisolator_write_data(spiisolator_t *ctx, uint8_t reg_address, uint8_t write_cmd, uint16_t write_data)
Write 16-bit data function.
addr_cmd_t::reg_address
uint8_t reg_address
Definition: spiisolator.h:263
spiisolator_write_bytes
void spiisolator_write_bytes(spiisolator_t *ctx, uint8_t reg_address, uint8_t write_cmd, uint8_t *write_data, uint16_t n_bytes)
Sequential write function.
spiisolator_t::spi
spi_master_t spi
Definition: spiisolator.h:278