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
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54#include "drv_spi_master.h"
55
56
57// -------------------------------------------------------------- PUBLIC MACROS
68#define EXPAND_MAP_MIKROBUS( cfg, mikrobus ) \
69 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
73 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
74 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
81#define EXPAND_RETVAL uint8_t
82
83#define EXPAND_OK 0x00
84#define EXPAND_INIT_ERROR 0xFF
91#define EXPAND_SPI_DEVICE_OPCODE 0x40
92#define EXPAND_OPCODE_WRITE 0xFE
93#define EXPAND_OPCODE_READ 0x01
100#define EXPAND_SPI_MODULE_POSITION_0 0x00
101#define EXPAND_SPI_MODULE_POSITION_1 0x02
102#define EXPAND_SPI_MODULE_POSITION_2 0x04
103#define EXPAND_SPI_MODULE_POSITION_3 0x06
104#define EXPAND_SPI_MODULE_POSITION_4 0x08
105#define EXPAND_SPI_MODULE_POSITION_5 0x0A
106#define EXPAND_SPI_MODULE_POSITION_6 0x0C
107#define EXPAND_SPI_MODULE_POSITION_7 0x0E
114#define EXPAND_PORT_DIRECTION_OUTPUT 0x00
115#define EXPAND_PORT_DIRECTION_INPUT 0xFF
122#define EXPAND_IODIRA_BANK1 0x00
123#define EXPAND_IPOLA_BANK1 0x01
124#define EXPAND_GPINTENA_BANK1 0x02
125#define EXPAND_DEFVALA_BANK1 0x03
126#define EXPAND_INTCONA_BANK1 0x04
127#define EXPAND_IOCON_BANK1 0x05
128#define EXPAND_GPPUA_BANK1 0x06
129#define EXPAND_INTFA_BANK1 0x07
130#define EXPAND_INTCAPA_BANK1 0x08
131#define EXPAND_GPIOA_BANK1 0x09
132#define EXPAND_OLATA_BANK1 0x0A
133#define EXPAND_IODIRB_BANK1 0x10
134#define EXPAND_IPOLB_BANK1 0x11
135#define EXPAND_GPINTENB_BANK1 0x12
136#define EXPAND_DEFVALB_BANK1 0x13
137#define EXPAND_INTCONB_BANK1 0x14
138#define EXPAND_IOCONO_BANK1 0x15
139#define EXPAND_GPPUB_BANK1 0x16
140#define EXPAND_INTFB_BANK1 0x17
141#define EXPAND_INTCAPB_BANK1 0x18
142#define EXPAND_GPIOB_BANK1 0x19
143#define EXPAND_OLATB_BANK1 0x1A
151#define EXPAND_IODIRA_BANK0 0x00
152#define EXPAND_IODIRB_BANK0 0x01
153#define EXPAND_IPOLA_BANK0 0x02
154#define EXPAND_IPOLB_BANK0 0x03
155#define EXPAND_GPINTENA_BANK0 0x04
156#define EXPAND_GPINTENB_BANK0 0x05
157#define EXPAND_DEFVALA_BANK0 0x06
158#define EXPAND_DEFVALB_BANK0 0x07
159#define EXPAND_INTCONA_BANK0 0x08
160#define EXPAND_INTCONB_BANK0 0x09
161#define EXPAND_IOCON_BANK0 0x0A
162#define EXPAND_GPPUA_BANK0 0x0C
163#define EXPAND_GPPUB_BANK0 0x0D
164#define EXPAND_INTFA_BANK0 0x0E
165#define EXPAND_INTFB_BANK0 0x0F
166#define EXPAND_INTCAPA_BANK0 0x10
167#define EXPAND_INTCAPB_BANK0 0x11
168#define EXPAND_GPIOA_BANK0 0x12
169#define EXPAND_GPIOB_BANK0 0x13
170#define EXPAND_OLATA_BANK0 0x14
171#define EXPAND_OLATB_BANK0 0x15
178#define EXPAND_IOCON_BYTE_MODE 0x20
179#define EXPAND_IOCON_HAEN 0x08
180
181#define EXPAND_CN4_PA0 0x01
182#define EXPAND_CN4_PA1 0x02
183#define EXPAND_CN4_PA2 0x04
184#define EXPAND_CN4_PA3 0x08
185#define EXPAND_CN4_PA4 0x10
186#define EXPAND_CN4_PA5 0x20
187#define EXPAND_CN4_PA6 0x40
188#define EXPAND_CN4_PA7 0x80
189
190#define EXPAND_CN5_PB0 0x01
191#define EXPAND_CN5_PB1 0x02
192#define EXPAND_CN5_PB2 0x04
193#define EXPAND_CN5_PB3 0x08
194#define EXPAND_CN5_PB4 0x10
195#define EXPAND_CN5_PB5 0x20
196#define EXPAND_CN5_PB6 0x40
197#define EXPAND_CN5_PB7 0x80
198
199#define EXPAND_CN_START_POSITION 0x01
200
201#define EXPAND_INT_ERR 0xFF
204 // End group macro
205// --------------------------------------------------------------- PUBLIC TYPES
214typedef struct
215{
216 // Output pins
217
218 digital_out_t cs;
219 digital_out_t rst;
220
221 // Input pins
222
223 digital_in_t int_pin;
224
225 // Modules
226
227 spi_master_t spi;
228 pin_name_t chip_select;
229
230} expand_t;
231
235typedef struct
236{
237 // Communication gpio pins
238
239 pin_name_t miso;
240 pin_name_t mosi;
241 pin_name_t sck;
242 pin_name_t cs;
243
244 // Additional gpio pins
245
246 pin_name_t rst;
247 pin_name_t int_pin;
248
249 // static variable
250
251 uint32_t spi_speed;
252 spi_master_mode_t spi_mode;
253 spi_master_chip_select_polarity_t cs_polarity;
254
256
257 // End types group
258// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
259
264#ifdef __cplusplus
265extern "C"{
266#endif
267
277
286
296uint8_t expand_read_byte ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr );
307void expand_write_byte ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t write_data );
308
317void expand_default_configuration ( expand_t *ctx, uint8_t mod_cmd );
318
329void expand_set_bits ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask );
330
341void expand_clear_bits ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask );
342
353void expand_toggle_bits ( expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask );
354
365uint8_t expand_read_port_a ( expand_t *ctx, uint8_t mod_cmd );
366
377uint8_t expand_read_port_b ( expand_t *ctx, uint8_t mod_cmd );
378
389uint16_t expand_read_both_porta ( expand_t *ctx, uint8_t mod_cmd );
390
400void expand_write_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
401
411void expand_clear_bit_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
412
422void expand_set_bit_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
423
433void expand_toggle_bit_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
434
445void expand_write_port_b( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
446
456void expand_clear_bit_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
457
467void expand_set_bit_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
468
478void expand_toggle_bit_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
479
490void expand_set_direction_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
491
502void expand_set_input_dir_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
503
514void expand_set_output_dir_port_a ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
515
526void expand_set_direction_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
527
538void expand_set_input_dir_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
539
550void expand_set_output_dir_port_b( expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask );
551
563void expand_set_pull_ups_port_a( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
564
576void expand_set_pull_ups_port_b ( expand_t *ctx, uint8_t mod_cmd, uint8_t write_data );
577
586void expand_set_potr_a( expand_t *ctx, uint8_t position );
587
596void expand_set_potr_b ( expand_t *ctx, uint8_t position );
597
610void expand_reset ( expand_t *ctx );
611
623
624#ifdef __cplusplus
625}
626#endif
627#endif // _EXPAND_H_
628
629 // End public_function group
631
632// ------------------------------------------------------------------------- END
#define EXPAND_RETVAL
Definition expand.h:81
void expand_set_potr_b(expand_t *ctx, uint8_t position)
Active pin by position on PORTB function.
void expand_set_input_dir_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTB input direction function.
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.
void expand_reset(expand_t *ctx)
Reset function.
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.
void expand_set_potr_a(expand_t *ctx, uint8_t position)
Active pin by position on PORTA function.
void expand_set_direction_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Set expander PORTB direction function.
uint16_t expand_read_both_porta(expand_t *ctx, uint8_t mod_cmd)
Read two byte of data from PORTA & PORTB function.
void expand_clear_bit_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Clear bit from register for PORTA function.
void expand_cfg_setup(expand_cfg_t *cfg)
Config Object Initialization function.
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.
uint8_t expand_read_byte(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr)
Generic read one bayt from register function.
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.
void expand_set_bit_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set bit to register for PORTB function.
void expand_toggle_bit_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Toggle bit to register for PORTA function.
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.
uint8_t expand_read_port_b(expand_t *ctx, uint8_t mod_cmd)
Read one byte of data from PORTB function.
void expand_default_configuration(expand_t *ctx, uint8_t mod_cmd)
Generic write one bayt to register function.
void expand_set_bit_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set bit to register for PORTA function.
void expand_set_input_dir_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTA input direction function.
uint8_t expand_get_interrupt(expand_t *ctx)
Get state of interrupt pin function.
void expand_set_direction_port_a(expand_t *ctx, uint8_t mod_cmd, uint8_t write_data)
Set expander PORTA direction function.
void expand_toggle_bit_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Toggle bit to register for PORTB function.
uint8_t expand_read_port_a(expand_t *ctx, uint8_t mod_cmd)
Read one byte of data from PORTA function.
void expand_clear_bit_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Clear bit from register for PORTB function.
void expand_clear_bits(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask)
Clear register bits function.
void expand_set_output_dir_port_b(expand_t *ctx, uint8_t mod_cmd, uint8_t bit_mask)
Set expander PORTB output direction function.
void expand_set_bits(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask)
Set register bits function.
void expand_toggle_bits(expand_t *ctx, uint8_t mod_cmd, uint8_t reg_addr, uint8_t bit_mask)
Toggle register bits function.
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_RETVAL expand_init(expand_t *ctx, expand_cfg_t *cfg)
Initialization function.
Click configuration structure definition.
Definition expand.h:236
spi_master_chip_select_polarity_t cs_polarity
Definition expand.h:253
pin_name_t sck
Definition expand.h:241
spi_master_mode_t spi_mode
Definition expand.h:252
pin_name_t mosi
Definition expand.h:240
uint32_t spi_speed
Definition expand.h:251
pin_name_t int_pin
Definition expand.h:247
pin_name_t miso
Definition expand.h:239
pin_name_t rst
Definition expand.h:246
pin_name_t cs
Definition expand.h:242
Click ctx object definition.
Definition expand.h:215
digital_out_t cs
Definition expand.h:218
spi_master_t spi
Definition expand.h:227
digital_in_t int_pin
Definition expand.h:223
digital_out_t rst
Definition expand.h:219
pin_name_t chip_select
Definition expand.h:228