expand2  2.0.0.0
expand2.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 EXPAND2_H
36 #define EXPAND2_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define EXPAND2_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
57  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
58 
64 #define EXPAND2_RETVAL uint8_t
65 
66 #define EXPAND2_OK 0x00
67 #define EXPAND2_INIT_ERROR 0xFF
68 
74 #define EXPAND2_I2C_MODULE_ADDRESS_0 0x20
75 #define EXPAND2_I2C_MODULE_ADDRESS_1 0x21
76 #define EXPAND2_I2C_MODULE_ADDRESS_2 0x22
77 #define EXPAND2_I2C_MODULE_ADDRESS_3 0x23
78 #define EXPAND2_I2C_MODULE_ADDRESS_4 0x24
79 #define EXPAND2_I2C_MODULE_ADDRESS_5 0x25
80 #define EXPAND2_I2C_MODULE_ADDRESS_6 0x26
81 #define EXPAND2_I2C_MODULE_ADDRESS_7 0x27
82 
88 #define EXPAND2_PORT_DIRECTION_OUTPUT 0x00
89 #define EXPAND2_PORT_DIRECTION_INPUT 0xFF
90 
96 #define EXPAND2_IODIRA_BANK1 0x00
97 #define EXPAND2_IPOLA_BANK1 0x01
98 #define EXPAND2_GPINTENA_BANK1 0x02
99 #define EXPAND2_DEFVALA_BANK1 0x03
100 #define EXPAND2_INTCONA_BANK1 0x04
101 #define EXPAND2_IOCON_BANK1 0x05
102 #define EXPAND2_GPPUA_BANK1 0x06
103 #define EXPAND2_INTFA_BANK1 0x07
104 #define EXPAND2_INTCAPA_BANK1 0x08
105 #define EXPAND2_GPIOA_BANK1 0x09
106 #define EXPAND2_OLATA_BANK1 0x0A
107 #define EXPAND2_IODIRB_BANK1 0x10
108 #define EXPAND2_IPOLB_BANK1 0x11
109 #define EXPAND2_GPINTENB_BANK1 0x12
110 #define EXPAND2_DEFVALB_BANK1 0x13
111 #define EXPAND2_INTCONB_BANK1 0x14
112 #define EXPAND2_IOCONO_BANK1 0x15
113 #define EXPAND2_GPPUB_BANK1 0x16
114 #define EXPAND2_INTFB_BANK1 0x17
115 #define EXPAND2_INTCAPB_BANK1 0x18
116 #define EXPAND2_GPIOB_BANK1 0x19
117 #define EXPAND2_OLATB_BANK1 0x1A
118 
124 #define EXPAND2_IODIRA_BANK0 0x00
125 #define EXPAND2_IODIRB_BANK0 0x01
126 #define EXPAND2_IPOLA_BANK0 0x02
127 #define EXPAND2_IPOLB_BANK0 0x03
128 #define EXPAND2_GPINTENA_BANK0 0x04
129 #define EXPAND2_GPINTENB_BANK0 0x05
130 #define EXPAND2_DEFVALA_BANK0 0x06
131 #define EXPAND2_DEFVALB_BANK0 0x07
132 #define EXPAND2_INTCONA_BANK0 0x08
133 #define EXPAND2_INTCONB_BANK0 0x09
134 #define EXPAND2_IOCON_BANK0 0x0A
135 #define EXPAND2_GPPUA_BANK0 0x0C
136 #define EXPAND2_GPPUB_BANK0 0x0D
137 #define EXPAND2_INTFA_BANK0 0x0E
138 #define EXPAND2_INTFB_BANK0 0x0F
139 #define EXPAND2_INTCAPA_BANK0 0x10
140 #define EXPAND2_INTCAPB_BANK0 0x11
141 #define EXPAND2_GPIOA_BANK0 0x12
142 #define EXPAND2_GPIOB_BANK0 0x13
143 #define EXPAND2_OLATA_BANK0 0x14
144 #define EXPAND2_OLATB_BANK0 0x15
145 
146 #define EXPAND2_HD1_PA0 0x01
147 #define EXPAND2_HD1_PA1 0x02
148 #define EXPAND2_HD1_PA2 0x04
149 #define EXPAND2_HD1_PA3 0x08
150 #define EXPAND2_HD1_PA4 0x10
151 #define EXPAND2_HD1_PA5 0x20
152 #define EXPAND2_HD1_PA6 0x40
153 #define EXPAND2_HD1_PA7 0x80
154 
155 #define EXPAND2_HD2_PB0 0x01
156 #define EXPAND2_HD2_PB1 0x02
157 #define EXPAND2_HD2_PB2 0x04
158 #define EXPAND2_HD2_PB3 0x08
159 #define EXPAND2_HD2_PB4 0x10
160 #define EXPAND2_HD2_PB5 0x20
161 #define EXPAND2_HD2_PB6 0x40
162 #define EXPAND2_HD2_PB7 0x80
163 
164 #define EXPAND2_HD_START_POSITION 0x01
165 
166 #define EXPAND2_INT_ERR 0xFF
167  // End group macro
169 // --------------------------------------------------------------- PUBLIC TYPES
178 typedef struct
179 {
180  // Output pins
181 
182  digital_out_t rst;
183 
184  // Input pins
185 
186  digital_in_t int_pin;
187 
188  // Modules
189 
190  i2c_master_t i2c;
191 
192  // ctx variable
193 
194  uint8_t slave_address;
195 
196 } expand2_t;
197 
201 typedef struct
202 {
203  // Communication gpio pins
204 
205  pin_name_t scl;
206  pin_name_t sda;
207 
208  // Additional gpio pins
209 
210  pin_name_t rst;
211  pin_name_t int_pin;
212 
213  // static variable
214 
215  uint32_t i2c_speed;
216  uint8_t i2c_address;
217 
218 } expand2_cfg_t;
219  // End types group
221 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
222 
228 #ifdef __cplusplus
229 extern "C"{
230 #endif
231 
240 void expand2_cfg_setup ( expand2_cfg_t *cfg );
241 
250 
258 void expand2_default_cfg ( expand2_t *ctx );
259 
270 void expand2_generic_write ( expand2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
271 
283 void expand2_generic_read ( expand2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
284 
295 void expand2_set_bits ( expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask );
296 
307 void expand2_clear_bits ( expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask );
308 
319 void expand2_toggle_bits ( expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask );
320 
330 uint8_t expand2_read_port_a ( expand2_t *ctx, uint8_t module_address );
331 
341 uint8_t expand2_read_port_b ( expand2_t *ctx, uint8_t module_address );
342 
352 uint16_t expand2_read_both_ports ( expand2_t *ctx, uint8_t module_address );
353 
363 void expand2_write_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
364 
374 void expand2_clear_bit_port_a ( expand2_t *ctx, uint8_t moduleAddress, uint8_t bitMask );
375 
385 void expand2_set_bit_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
386 
396 void expand2_toggle_bit_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
397 
407 void expand2_write_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
408 
418 void expand2_clear_bit_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
419 
429 void expand2_setBitPortB( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
430 
440 void expand2_toggle_bit_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
441 
451 void expand2_set_direction_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
452 
462 void expand2_set_input_dir_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
463 
473 void expand2_set_output_dir_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
474 
484 void expand2_set_direction_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
485 
495 void expand2_set_input_dir_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
496 
506 void expand2_set_output_dir_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
507 
517 void expand2_set_pull_ups_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
518 
528 void expand2_set_pull_ups_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
529 
539 void expand2_set_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t position );
540 
550 void expand2_set_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t position );
551 
563 void expand2_reset( expand2_t *ctx );
564 
578 uint8_t expand2_getInterrupt( expand2_t *ctx );
579 
580 #ifdef __cplusplus
581 }
582 #endif
583 #endif // _EXPAND2_H_
584  // End public_function group
587 
588 // ------------------------------------------------------------------------- END
expand2_set_direction_port_b
void expand2_set_direction_port_b(expand2_t *ctx, uint8_t module_address, uint8_t write_data)
Set expander PORTB direction function.
expand2_set_port_a
void expand2_set_port_a(expand2_t *ctx, uint8_t module_address, uint8_t position)
Active pin by position on PORTA function.
expand2_cfg_t
Click configuration structure definition.
Definition: expand2.h:201
expand2_set_bits
void expand2_set_bits(expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask)
Set register bits function.
expand2_t::slave_address
uint8_t slave_address
Definition: expand2.h:194
expand2_cfg_t::scl
pin_name_t scl
Definition: expand2.h:205
expand2_write_port_a
void expand2_write_port_a(expand2_t *ctx, uint8_t module_address, uint8_t write_data)
Write one byte of data to register for PORTA function.
expand2_t::rst
digital_out_t rst
Definition: expand2.h:182
expand2_set_bit_port_a
void expand2_set_bit_port_a(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Set bit to register for PORTA function.
expand2_cfg_t::int_pin
pin_name_t int_pin
Definition: expand2.h:211
expand2_read_port_b
uint8_t expand2_read_port_b(expand2_t *ctx, uint8_t module_address)
Read one byte of data from PORTB function.
expand2_clear_bit_port_b
void expand2_clear_bit_port_b(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Clear bit from register for PORTB function.
expand2_cfg_setup
void expand2_cfg_setup(expand2_cfg_t *cfg)
Config Object Initialization function.
expand2_write_port_b
void expand2_write_port_b(expand2_t *ctx, uint8_t module_address, uint8_t write_data)
Write one byte of data to register for PORTB function.
expand2_set_input_dir_port_b
void expand2_set_input_dir_port_b(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Set expander PORTB input direction function.
expand2_set_direction_port_a
void expand2_set_direction_port_a(expand2_t *ctx, uint8_t module_address, uint8_t write_data)
Set expander PORTA direction function.
expand2_t::int_pin
digital_in_t int_pin
Definition: expand2.h:186
expand2_toggle_bit_port_b
void expand2_toggle_bit_port_b(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Toggle bit to register for PORTB function.
expand2_t
Click ctx object definition.
Definition: expand2.h:178
expand2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: expand2.h:215
expand2_set_output_dir_port_b
void expand2_set_output_dir_port_b(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Set expander PORTB output direction function.
expand2_toggle_bit_port_a
void expand2_toggle_bit_port_a(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Toggle bit to register for PORTA function.
expand2_init
EXPAND2_RETVAL expand2_init(expand2_t *ctx, expand2_cfg_t *cfg)
Initialization function.
expand2_t::i2c
i2c_master_t i2c
Definition: expand2.h:190
expand2_set_pull_ups_port_b
void expand2_set_pull_ups_port_b(expand2_t *ctx, uint8_t module_address, uint8_t write_data)
Set pull-ups of the expander for PORTB pins function.
expand2_generic_write
void expand2_generic_write(expand2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
expand2_read_port_a
uint8_t expand2_read_port_a(expand2_t *ctx, uint8_t module_address)
Read one byte of data from PORTA function.
expand2_set_input_dir_port_a
void expand2_set_input_dir_port_a(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Set expander PORTA input direction function.
expand2_default_cfg
void expand2_default_cfg(expand2_t *ctx)
Click Default Configuration function.
expand2_clear_bit_port_a
void expand2_clear_bit_port_a(expand2_t *ctx, uint8_t moduleAddress, uint8_t bitMask)
Clear bit from register for PORTA function.
expand2_set_port_b
void expand2_set_port_b(expand2_t *ctx, uint8_t module_address, uint8_t position)
Active pin by position on PORTB function.
expand2_cfg_t::i2c_address
uint8_t i2c_address
Definition: expand2.h:216
EXPAND2_RETVAL
#define EXPAND2_RETVAL
Definition: expand2.h:64
expand2_toggle_bits
void expand2_toggle_bits(expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask)
Toggle register bits function.
expand2_set_pull_ups_port_a
void expand2_set_pull_ups_port_a(expand2_t *ctx, uint8_t module_address, uint8_t write_data)
Set pull-ups of the expander for PORTA pins function.
expand2_setBitPortB
void expand2_setBitPortB(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Set bit to register for PORTB function.
expand2_cfg_t::rst
pin_name_t rst
Definition: expand2.h:210
expand2_set_output_dir_port_a
void expand2_set_output_dir_port_a(expand2_t *ctx, uint8_t module_address, uint8_t bit_mask)
Set expander PORTA output direction function.
expand2_reset
void expand2_reset(expand2_t *ctx)
Reset function.
expand2_getInterrupt
uint8_t expand2_getInterrupt(expand2_t *ctx)
Get state of interrupt pin function.
expand2_clear_bits
void expand2_clear_bits(expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask)
Clear register bits function.
expand2_cfg_t::sda
pin_name_t sda
Definition: expand2.h:206
expand2_read_both_ports
uint16_t expand2_read_both_ports(expand2_t *ctx, uint8_t module_address)
Read two byte of data from PORTA & PORTB function.
expand2_generic_read
void expand2_generic_read(expand2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.