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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define EXPAND2_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
67  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
68 
74 #define EXPAND2_RETVAL uint8_t
75 
76 #define EXPAND2_OK 0x00
77 #define EXPAND2_INIT_ERROR 0xFF
78 
84 #define EXPAND2_I2C_MODULE_ADDRESS_0 0x20
85 #define EXPAND2_I2C_MODULE_ADDRESS_1 0x21
86 #define EXPAND2_I2C_MODULE_ADDRESS_2 0x22
87 #define EXPAND2_I2C_MODULE_ADDRESS_3 0x23
88 #define EXPAND2_I2C_MODULE_ADDRESS_4 0x24
89 #define EXPAND2_I2C_MODULE_ADDRESS_5 0x25
90 #define EXPAND2_I2C_MODULE_ADDRESS_6 0x26
91 #define EXPAND2_I2C_MODULE_ADDRESS_7 0x27
92 
98 #define EXPAND2_PORT_DIRECTION_OUTPUT 0x00
99 #define EXPAND2_PORT_DIRECTION_INPUT 0xFF
100 
106 #define EXPAND2_IODIRA_BANK1 0x00
107 #define EXPAND2_IPOLA_BANK1 0x01
108 #define EXPAND2_GPINTENA_BANK1 0x02
109 #define EXPAND2_DEFVALA_BANK1 0x03
110 #define EXPAND2_INTCONA_BANK1 0x04
111 #define EXPAND2_IOCON_BANK1 0x05
112 #define EXPAND2_GPPUA_BANK1 0x06
113 #define EXPAND2_INTFA_BANK1 0x07
114 #define EXPAND2_INTCAPA_BANK1 0x08
115 #define EXPAND2_GPIOA_BANK1 0x09
116 #define EXPAND2_OLATA_BANK1 0x0A
117 #define EXPAND2_IODIRB_BANK1 0x10
118 #define EXPAND2_IPOLB_BANK1 0x11
119 #define EXPAND2_GPINTENB_BANK1 0x12
120 #define EXPAND2_DEFVALB_BANK1 0x13
121 #define EXPAND2_INTCONB_BANK1 0x14
122 #define EXPAND2_IOCONO_BANK1 0x15
123 #define EXPAND2_GPPUB_BANK1 0x16
124 #define EXPAND2_INTFB_BANK1 0x17
125 #define EXPAND2_INTCAPB_BANK1 0x18
126 #define EXPAND2_GPIOB_BANK1 0x19
127 #define EXPAND2_OLATB_BANK1 0x1A
128 
134 #define EXPAND2_IODIRA_BANK0 0x00
135 #define EXPAND2_IODIRB_BANK0 0x01
136 #define EXPAND2_IPOLA_BANK0 0x02
137 #define EXPAND2_IPOLB_BANK0 0x03
138 #define EXPAND2_GPINTENA_BANK0 0x04
139 #define EXPAND2_GPINTENB_BANK0 0x05
140 #define EXPAND2_DEFVALA_BANK0 0x06
141 #define EXPAND2_DEFVALB_BANK0 0x07
142 #define EXPAND2_INTCONA_BANK0 0x08
143 #define EXPAND2_INTCONB_BANK0 0x09
144 #define EXPAND2_IOCON_BANK0 0x0A
145 #define EXPAND2_GPPUA_BANK0 0x0C
146 #define EXPAND2_GPPUB_BANK0 0x0D
147 #define EXPAND2_INTFA_BANK0 0x0E
148 #define EXPAND2_INTFB_BANK0 0x0F
149 #define EXPAND2_INTCAPA_BANK0 0x10
150 #define EXPAND2_INTCAPB_BANK0 0x11
151 #define EXPAND2_GPIOA_BANK0 0x12
152 #define EXPAND2_GPIOB_BANK0 0x13
153 #define EXPAND2_OLATA_BANK0 0x14
154 #define EXPAND2_OLATB_BANK0 0x15
155 
156 #define EXPAND2_HD1_PA0 0x01
157 #define EXPAND2_HD1_PA1 0x02
158 #define EXPAND2_HD1_PA2 0x04
159 #define EXPAND2_HD1_PA3 0x08
160 #define EXPAND2_HD1_PA4 0x10
161 #define EXPAND2_HD1_PA5 0x20
162 #define EXPAND2_HD1_PA6 0x40
163 #define EXPAND2_HD1_PA7 0x80
164 
165 #define EXPAND2_HD2_PB0 0x01
166 #define EXPAND2_HD2_PB1 0x02
167 #define EXPAND2_HD2_PB2 0x04
168 #define EXPAND2_HD2_PB3 0x08
169 #define EXPAND2_HD2_PB4 0x10
170 #define EXPAND2_HD2_PB5 0x20
171 #define EXPAND2_HD2_PB6 0x40
172 #define EXPAND2_HD2_PB7 0x80
173 
174 #define EXPAND2_HD_START_POSITION 0x01
175 
176 #define EXPAND2_INT_ERR 0xFF
177  // End group macro
179 // --------------------------------------------------------------- PUBLIC TYPES
188 typedef struct
189 {
190  // Output pins
191 
192  digital_out_t rst;
193 
194  // Input pins
195 
196  digital_in_t int_pin;
197 
198  // Modules
199 
200  i2c_master_t i2c;
201 
202  // ctx variable
203 
204  uint8_t slave_address;
205 
206 } expand2_t;
207 
211 typedef struct
212 {
213  // Communication gpio pins
214 
215  pin_name_t scl;
216  pin_name_t sda;
217 
218  // Additional gpio pins
219 
220  pin_name_t rst;
221  pin_name_t int_pin;
222 
223  // static variable
224 
225  uint32_t i2c_speed;
226  uint8_t i2c_address;
227 
228 } expand2_cfg_t;
229  // End types group
231 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
232 
238 #ifdef __cplusplus
239 extern "C"{
240 #endif
241 
251 
260 
269 
280 void expand2_generic_write ( expand2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
281 
293 void expand2_generic_read ( expand2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
294 
305 void expand2_set_bits ( expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask );
306 
317 void expand2_clear_bits ( expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask );
318 
329 void expand2_toggle_bits ( expand2_t *ctx, uint8_t module_address, uint8_t reg_address, uint8_t bit_mask );
330 
340 uint8_t expand2_read_port_a ( expand2_t *ctx, uint8_t module_address );
341 
351 uint8_t expand2_read_port_b ( expand2_t *ctx, uint8_t module_address );
352 
362 uint16_t expand2_read_both_ports ( expand2_t *ctx, uint8_t module_address );
363 
373 void expand2_write_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
374 
384 void expand2_clear_bit_port_a ( expand2_t *ctx, uint8_t moduleAddress, uint8_t bitMask );
385 
395 void expand2_set_bit_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
396 
406 void expand2_toggle_bit_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
407 
417 void expand2_write_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
418 
428 void expand2_clear_bit_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
429 
439 void expand2_setBitPortB( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
440 
450 void expand2_toggle_bit_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
451 
461 void expand2_set_direction_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
462 
472 void expand2_set_input_dir_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
473 
483 void expand2_set_output_dir_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
484 
494 void expand2_set_direction_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
495 
505 void expand2_set_input_dir_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
506 
516 void expand2_set_output_dir_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t bit_mask );
517 
527 void expand2_set_pull_ups_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
528 
538 void expand2_set_pull_ups_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t write_data );
539 
549 void expand2_set_port_a ( expand2_t *ctx, uint8_t module_address, uint8_t position );
550 
560 void expand2_set_port_b ( expand2_t *ctx, uint8_t module_address, uint8_t position );
561 
574 
589 
590 #ifdef __cplusplus
591 }
592 #endif
593 #endif // _EXPAND2_H_
594  // End public_function group
597 
598 // ------------------------------------------------------------------------- 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:212
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:204
expand2_cfg_t::scl
pin_name_t scl
Definition: expand2.h:215
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:192
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:221
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:196
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:189
expand2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: expand2.h:225
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:200
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:226
EXPAND2_RETVAL
#define EXPAND2_RETVAL
Definition: expand2.h:74
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:220
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:216
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.