expand  2.0.0.0
expand.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 EXPAND_H
36 #define EXPAND_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_spi_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define EXPAND_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define EXPAND_RETVAL uint8_t
78 
79 #define EXPAND_OK 0x00
80 #define EXPAND_INIT_ERROR 0xFF
81 
87 #define EXPAND_SPI_DEVICE_OPCODE 0x40
88 #define EXPAND_OPCODE_WRITE 0xFE
89 #define EXPAND_OPCODE_READ 0x01
90 
96 #define EXPAND_SPI_MODULE_POSITION_0 0x00
97 #define EXPAND_SPI_MODULE_POSITION_1 0x02
98 #define EXPAND_SPI_MODULE_POSITION_2 0x04
99 #define EXPAND_SPI_MODULE_POSITION_3 0x06
100 #define EXPAND_SPI_MODULE_POSITION_4 0x08
101 #define EXPAND_SPI_MODULE_POSITION_5 0x0A
102 #define EXPAND_SPI_MODULE_POSITION_6 0x0C
103 #define EXPAND_SPI_MODULE_POSITION_7 0x0E
104 
110 #define EXPAND_PORT_DIRECTION_OUTPUT 0x00
111 #define EXPAND_PORT_DIRECTION_INPUT 0xFF
112 
118 #define EXPAND_IODIRA_BANK1 0x00
119 #define EXPAND_IPOLA_BANK1 0x01
120 #define EXPAND_GPINTENA_BANK1 0x02
121 #define EXPAND_DEFVALA_BANK1 0x03
122 #define EXPAND_INTCONA_BANK1 0x04
123 #define EXPAND_IOCON_BANK1 0x05
124 #define EXPAND_GPPUA_BANK1 0x06
125 #define EXPAND_INTFA_BANK1 0x07
126 #define EXPAND_INTCAPA_BANK1 0x08
127 #define EXPAND_GPIOA_BANK1 0x09
128 #define EXPAND_OLATA_BANK1 0x0A
129 #define EXPAND_IODIRB_BANK1 0x10
130 #define EXPAND_IPOLB_BANK1 0x11
131 #define EXPAND_GPINTENB_BANK1 0x12
132 #define EXPAND_DEFVALB_BANK1 0x13
133 #define EXPAND_INTCONB_BANK1 0x14
134 #define EXPAND_IOCONO_BANK1 0x15
135 #define EXPAND_GPPUB_BANK1 0x16
136 #define EXPAND_INTFB_BANK1 0x17
137 #define EXPAND_INTCAPB_BANK1 0x18
138 #define EXPAND_GPIOB_BANK1 0x19
139 #define EXPAND_OLATB_BANK1 0x1A
140 
147 #define EXPAND_IODIRA_BANK0 0x00
148 #define EXPAND_IODIRB_BANK0 0x01
149 #define EXPAND_IPOLA_BANK0 0x02
150 #define EXPAND_IPOLB_BANK0 0x03
151 #define EXPAND_GPINTENA_BANK0 0x04
152 #define EXPAND_GPINTENB_BANK0 0x05
153 #define EXPAND_DEFVALA_BANK0 0x06
154 #define EXPAND_DEFVALB_BANK0 0x07
155 #define EXPAND_INTCONA_BANK0 0x08
156 #define EXPAND_INTCONB_BANK0 0x09
157 #define EXPAND_IOCON_BANK0 0x0A
158 #define EXPAND_GPPUA_BANK0 0x0C
159 #define EXPAND_GPPUB_BANK0 0x0D
160 #define EXPAND_INTFA_BANK0 0x0E
161 #define EXPAND_INTFB_BANK0 0x0F
162 #define EXPAND_INTCAPA_BANK0 0x10
163 #define EXPAND_INTCAPB_BANK0 0x11
164 #define EXPAND_GPIOA_BANK0 0x12
165 #define EXPAND_GPIOB_BANK0 0x13
166 #define EXPAND_OLATA_BANK0 0x14
167 #define EXPAND_OLATB_BANK0 0x15
168 
174 #define EXPAND_IOCON_BYTE_MODE 0x20
175 #define EXPAND_IOCON_HAEN 0x08
176 
177 #define EXPAND_CN4_PA0 0x01
178 #define EXPAND_CN4_PA1 0x02
179 #define EXPAND_CN4_PA2 0x04
180 #define EXPAND_CN4_PA3 0x08
181 #define EXPAND_CN4_PA4 0x10
182 #define EXPAND_CN4_PA5 0x20
183 #define EXPAND_CN4_PA6 0x40
184 #define EXPAND_CN4_PA7 0x80
185 
186 #define EXPAND_CN5_PB0 0x01
187 #define EXPAND_CN5_PB1 0x02
188 #define EXPAND_CN5_PB2 0x04
189 #define EXPAND_CN5_PB3 0x08
190 #define EXPAND_CN5_PB4 0x10
191 #define EXPAND_CN5_PB5 0x20
192 #define EXPAND_CN5_PB6 0x40
193 #define EXPAND_CN5_PB7 0x80
194 
195 #define EXPAND_CN_START_POSITION 0x01
196 
197 #define EXPAND_INT_ERR 0xFF
198  // End group macro
201 // --------------------------------------------------------------- PUBLIC TYPES
210 typedef struct
211 {
212  // Output pins
213 
214  digital_out_t cs;
215  digital_out_t rst;
216 
217  // Input pins
218 
219  digital_in_t int_pin;
220 
221  // Modules
222 
223  spi_master_t spi;
224  pin_name_t chip_select;
225 
226 } expand_t;
227 
231 typedef struct
232 {
233  // Communication gpio pins
234 
235  pin_name_t miso;
236  pin_name_t mosi;
237  pin_name_t sck;
238  pin_name_t cs;
239 
240  // Additional gpio pins
241 
242  pin_name_t rst;
243  pin_name_t int_pin;
244 
245  // static variable
246 
247  uint32_t spi_speed;
248  spi_master_mode_t spi_mode;
249  spi_master_chip_select_polarity_t cs_polarity;
250 
251 } expand_cfg_t;
252  // End types group
254 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
255 
260 #ifdef __cplusplus
261 extern "C"{
262 #endif
263 
273 
282 
292 uint8_t expand_read_byte ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr );
303 void expand_write_byte ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t write_data );
304 
313 void expand_default_configuration ( expand_t *ctx, uint8_t mod_cmd );
314 
325 void expand_set_bits ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask );
326 
337 void expand_clear_bits ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask );
338 
349 void expand_toggle_bits ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask );
350 
361 uint8_t expand_read_port_a ( expand_t *ctx, uint8_t mod_cmd );
362 
373 uint8_t expand_read_port_b ( expand_t *ctx, uint8_t mod_cmd );
374 
385 uint16_t expand_read_both_porta ( expand_t *ctx, uint8_t mod_cmd );
386 
396 void expand_write_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
397 
407 void expand_clear_bit_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
408 
418 void expand_set_bit_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
419 
429 void expand_toggle_bit_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
430 
441 void expand_write_port_b( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
442 
452 void expand_clear_bit_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
453 
463 void expand_set_bit_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
464 
474 void expand_toggle_bit_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
475 
486 void expand_set_direction_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
487 
498 void expand_set_input_dir_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
499 
510 void expand_set_output_dir_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
511 
522 void expand_set_direction_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
523 
534 void expand_set_input_dir_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
535 
546 void expand_set_output_dir_port_b( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
547 
559 void expand_set_pull_ups_port_a( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
560 
572 void expand_set_pull_ups_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
573 
582 void expand_set_potr_a( expand_t *ctx, uint8_t position );
583 
592 void expand_set_potr_b ( expand_t *ctx, uint8_t position );
593 
606 void expand_reset ( expand_t *ctx );
607 
619 
620 #ifdef __cplusplus
621 }
622 #endif
623 #endif // _EXPAND_H_
624  // End public_function group
627 
628 // ------------------------------------------------------------------------- END
expand_t
Click ctx object definition.
Definition: expand.h:211
expand_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: expand.h:249
expand_read_byte
uint8_t expand_read_byte(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr)
Generic read one bayt from register function.
expand_write_byte
void expand_write_byte(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t write_data)
Generic write one bayt to register function.
expand_t::cs
digital_out_t cs
Definition: expand.h:214
expand_cfg_setup
void expand_cfg_setup(expand_cfg_t *cfg)
Config Object Initialization function.
expand_write_port_a
void expand_write_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Write one byte of data to register for PORTA function.
expand_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: expand.h:248
expand_cfg_t::rst
pin_name_t rst
Definition: expand.h:242
expand_cfg_t::int_pin
pin_name_t int_pin
Definition: expand.h:243
expand_set_input_dir_port_a
void expand_set_input_dir_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTA input direction function.
expand_cfg_t::cs
pin_name_t cs
Definition: expand.h:238
expand_set_pull_ups_port_b
void expand_set_pull_ups_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Set pull-ups of the expander for PORTB pins function.
expand_clear_bit_port_b
void expand_clear_bit_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Clear bit from register for PORTB function.
expand_read_port_b
uint8_t expand_read_port_b(expand_t *ctx, uint8_t mod_cmd)
Read one byte of data from PORTB function.
expand_set_bit_port_a
void expand_set_bit_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set bit to register for PORTA function.
expand_clear_bits
void expand_clear_bits(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask)
Clear register bits function.
expand_write_port_b
void expand_write_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Write one byte of data to register for PORTB function.
expand_read_both_porta
uint16_t expand_read_both_porta(expand_t *ctx, uint8_t mod_cmd)
Read two byte of data from PORTA & PORTB function.
expand_toggle_bit_port_b
void expand_toggle_bit_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Toggle bit to register for PORTB function.
expand_get_interrupt
uint8_t expand_get_interrupt(expand_t *ctx)
Get state of interrupt pin function.
expand_toggle_bits
void expand_toggle_bits(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask)
Toggle register bits function.
expand_clear_bit_port_a
void expand_clear_bit_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Clear bit from register for PORTA function.
expand_init
EXPAND_RETVAL expand_init(expand_t *ctx, expand_cfg_t *cfg)
Initialization function.
expand_default_configuration
void expand_default_configuration(expand_t *ctx, uint8_t mod_cmd)
Generic write one bayt to register function.
expand_set_output_dir_port_a
void expand_set_output_dir_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTA output direction function.
expand_t::chip_select
pin_name_t chip_select
Definition: expand.h:224
expand_set_direction_port_a
void expand_set_direction_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Set expander PORTA direction function.
expand_cfg_t::mosi
pin_name_t mosi
Definition: expand.h:236
expand_set_output_dir_port_b
void expand_set_output_dir_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTB output direction function.
expand_cfg_t::miso
pin_name_t miso
Definition: expand.h:235
expand_set_pull_ups_port_a
void expand_set_pull_ups_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Set pull-ups of the expander for PORTA pins function.
expand_reset
void expand_reset(expand_t *ctx)
Reset function.
expand_set_bits
void expand_set_bits(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask)
Set register bits function.
expand_t::spi
spi_master_t spi
Definition: expand.h:223
expand_cfg_t
Click configuration structure definition.
Definition: expand.h:232
expand_set_potr_a
void expand_set_potr_a(expand_t *ctx, uint8_t position)
Active pin by position on PORTA function.
expand_t::rst
digital_out_t rst
Definition: expand.h:215
expand_cfg_t::spi_speed
uint32_t spi_speed
Definition: expand.h:247
expand_cfg_t::sck
pin_name_t sck
Definition: expand.h:237
expand_toggle_bit_port_a
void expand_toggle_bit_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Toggle bit to register for PORTA function.
expand_set_bit_port_b
void expand_set_bit_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set bit to register for PORTB function.
expand_read_port_a
uint8_t expand_read_port_a(expand_t *ctx, uint8_t mod_cmd)
Read one byte of data from PORTA function.
expand_set_input_dir_port_b
void expand_set_input_dir_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTB input direction function.
expand_set_potr_b
void expand_set_potr_b(expand_t *ctx, uint8_t position)
Active pin by position on PORTB function.
EXPAND_RETVAL
#define EXPAND_RETVAL
Definition: expand.h:77
expand_set_direction_port_b
void expand_set_direction_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Set expander PORTB direction function.
expand_t::int_pin
digital_in_t int_pin
Definition: expand.h:219