mux5  2.1.0.0
mux5.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 MUX5_H
29 #define MUX5_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_i2c_master.h"
48 #include "drv_spi_master.h"
49 #include "spi_specifics.h"
50 
71 #define MUX5_REG_DIR0 0x00
72 #define MUX5_REG_DIR1 0x01
73 #define MUX5_REG_DIR2 0x02
74 #define MUX5_REG_DIR3 0x03
75 #define MUX5_REG_SHDW0 0x10
76 #define MUX5_REG_SHDW1 0x11
77 #define MUX5_REG_SHDW2 0x12
78 #define MUX5_REG_SHDW3 0x13
79 #define MUX5_REG_CMD_A 0x14
80 #define MUX5_REG_CMD_B 0x15
81  // mux5_reg
83 
98 #define MUX5_I2C_DRIVER 0
99 #define MUX5_SPI_DRIVER 1
100 #define MUX5_DRIVER_SEL MUX5_I2C_DRIVER
101 
106 #define MUX5_CHANNEL_1 0x0001
107 #define MUX5_CHANNEL_2 0x0002
108 #define MUX5_CHANNEL_3 0x0004
109 #define MUX5_CHANNEL_4 0x0008
110 #define MUX5_CHANNEL_5 0x0010
111 #define MUX5_CHANNEL_6 0x0020
112 #define MUX5_CHANNEL_7 0x0040
113 #define MUX5_CHANNEL_8 0x0080
114 #define MUX5_CHANNEL_9 0x0100
115 #define MUX5_CHANNEL_10 0x0200
116 #define MUX5_CHANNEL_11 0x0400
117 #define MUX5_CHANNEL_12 0x0800
118 #define MUX5_CHANNEL_13 0x1000
119 #define MUX5_CHANNEL_14 0x2000
120 #define MUX5_CHANNEL_15 0x4000
121 #define MUX5_CHANNEL_16 0x8000
122 #define MUX5_CHANNEL_ALL 0xFFFF
123 
128 #define MUX5_CHANNEL_STATE_HIGH_Z 0
129 #define MUX5_CHANNEL_STATE_COM_A 1
130 #define MUX5_CHANNEL_STATE_COM_B 2
131 #define MUX5_CHANNEL_STATE_COM_AB 3
132 
138 #define MUX5_DEVICE_ADDRESS_0 0x4C
139 #define MUX5_DEVICE_ADDRESS_1 0x4D
140 
149 #define MUX5_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
150 #define MUX5_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
151  // mux5_set
153 
168 #define MUX5_MAP_MIKROBUS( cfg, mikrobus ) \
169  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
170  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
171  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
172  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
173  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
174  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
175  cfg.com_b = MIKROBUS( mikrobus, MIKROBUS_AN ); \
176  cfg.sd = MIKROBUS( mikrobus, MIKROBUS_RST ); \
177  cfg.com_a = MIKROBUS( mikrobus, MIKROBUS_INT )
178  // mux5_map // mux5
181 
186 typedef struct mux5_s
187 {
188  digital_out_t sd;
190  digital_in_t com_b;
191  digital_in_t com_a;
193 #if ( MUX5_DRIVER_SEL == MUX5_SPI_DRIVER )
194  spi_master_t spi;
195  pin_name_t chip_select;
196 #else
197  i2c_master_t i2c;
198  uint8_t slave_address;
199 #endif
200  uint16_t ch_a;
201  uint16_t ch_b;
204 
209 typedef struct
210 {
211  pin_name_t scl;
212  pin_name_t sda;
213  pin_name_t miso;
214  pin_name_t mosi;
215  pin_name_t sck;
216  pin_name_t cs;
217  pin_name_t com_b;
218  pin_name_t sd;
219  pin_name_t com_a;
221  uint32_t i2c_speed;
222  uint8_t i2c_address;
224  uint32_t spi_speed;
225  spi_master_mode_t spi_mode;
226  spi_master_chip_select_polarity_t cs_polarity;
228 } mux5_cfg_t;
229 
234 typedef enum
235 {
236  MUX5_OK = 0,
237  MUX5_ERROR = -1
238 
240 
257 
271 err_t mux5_init ( mux5_t *ctx, mux5_cfg_t *cfg );
272 
285 err_t mux5_default_cfg ( mux5_t *ctx );
286 
287 #if ( MUX5_DRIVER_SEL == MUX5_I2C_DRIVER )
288 
300 err_t mux5_i2c_write_register ( mux5_t *ctx, uint8_t reg, uint8_t data_in );
301 
314 err_t mux5_i2c_read_register ( mux5_t *ctx, uint8_t reg, uint8_t *data_out );
315 #endif
316 
334 err_t mux5_set_channels_state ( mux5_t *ctx, uint16_t ch_mask, uint8_t ch_state );
335 
345 
355 
364 uint8_t mux5_get_com_a_pin ( mux5_t *ctx );
365 
374 uint8_t mux5_get_com_b_pin ( mux5_t *ctx );
375 
376 #ifdef __cplusplus
377 }
378 #endif
379 #endif // MUX5_H
380  // mux5
382 
383 // ------------------------------------------------------------------------ END
mux5_init
err_t mux5_init(mux5_t *ctx, mux5_cfg_t *cfg)
MUX 5 initialization function.
mux5_cfg_t::i2c_address
uint8_t i2c_address
Definition: mux5.h:222
mux5_t
struct mux5_s mux5_t
MUX 5 Click context object.
mux5_s::sd
digital_out_t sd
Definition: mux5.h:188
mux5_cfg_t::com_a
pin_name_t com_a
Definition: mux5.h:219
mux5_s::i2c
i2c_master_t i2c
Definition: mux5.h:197
mux5_cfg_t::mosi
pin_name_t mosi
Definition: mux5.h:214
spi_specifics.h
This file contains SPI specific macros, functions, etc.
mux5_disable_device
void mux5_disable_device(mux5_t *ctx)
MUX 5 disable device function.
mux5_cfg_t::cs
pin_name_t cs
Definition: mux5.h:216
MUX5_ERROR
@ MUX5_ERROR
Definition: mux5.h:237
mux5_i2c_read_register
err_t mux5_i2c_read_register(mux5_t *ctx, uint8_t reg, uint8_t *data_out)
MUX 5 I2C read register function.
mux5_set_channels_state
err_t mux5_set_channels_state(mux5_t *ctx, uint16_t ch_mask, uint8_t ch_state)
MUX 5 set channels state function.
mux5_cfg_t::spi_speed
uint32_t spi_speed
Definition: mux5.h:224
mux5_cfg_t::scl
pin_name_t scl
Definition: mux5.h:211
mux5_s::ch_a
uint16_t ch_a
Definition: mux5.h:200
mux5_s::slave_address
uint8_t slave_address
Definition: mux5.h:198
mux5_cfg_t::sck
pin_name_t sck
Definition: mux5.h:215
mux5_s::com_b
digital_in_t com_b
Definition: mux5.h:190
mux5_cfg_t::com_b
pin_name_t com_b
Definition: mux5.h:217
mux5_get_com_b_pin
uint8_t mux5_get_com_b_pin(mux5_t *ctx)
MUX 5 get com b pin function.
mux5_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: mux5.h:221
mux5_cfg_t::sda
pin_name_t sda
Definition: mux5.h:212
mux5_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: mux5.h:226
mux5_cfg_t::miso
pin_name_t miso
Definition: mux5.h:213
mux5_s::com_a
digital_in_t com_a
Definition: mux5.h:191
mux5_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: mux5.h:225
mux5_get_com_a_pin
uint8_t mux5_get_com_a_pin(mux5_t *ctx)
MUX 5 get com a pin function.
mux5_i2c_write_register
err_t mux5_i2c_write_register(mux5_t *ctx, uint8_t reg, uint8_t data_in)
MUX 5 I2C write register function.
mux5_enable_device
void mux5_enable_device(mux5_t *ctx)
MUX 5 enable device function.
mux5_cfg_t::sd
pin_name_t sd
Definition: mux5.h:218
MUX5_OK
@ MUX5_OK
Definition: mux5.h:236
mux5_s::ch_b
uint16_t ch_b
Definition: mux5.h:201
mux5_cfg_setup
void mux5_cfg_setup(mux5_cfg_t *cfg)
MUX 5 configuration object setup function.
mux5_s
MUX 5 Click context object.
Definition: mux5.h:187
mux5_cfg_t
MUX 5 Click configuration object.
Definition: mux5.h:210
mux5_default_cfg
err_t mux5_default_cfg(mux5_t *ctx)
MUX 5 default configuration function.
mux5_return_value_t
mux5_return_value_t
MUX 5 Click return value data.
Definition: mux5.h:235