expand18  2.1.0.0
expand18.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 EXPAND18_H
29 #define EXPAND18_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_spi_master.h"
52 #include "spi_specifics.h"
53 
74 #define EXPAND18_REG_INPUT_P0 0x00
75 #define EXPAND18_REG_INPUT_P1 0x01
76 #define EXPAND18_REG_OUTPUT_P0 0x02
77 #define EXPAND18_REG_OUTPUT_P1 0x03
78 #define EXPAND18_REG_POLARITY_INV_P0 0x04
79 #define EXPAND18_REG_POLARITY_INV_P1 0x05
80 #define EXPAND18_REG_CONFIG_P0 0x06
81 #define EXPAND18_REG_CONFIG_P1 0x07
82 #define EXPAND18_REG_OUTPUT_DRIVE_P0A 0x40
83 #define EXPAND18_REG_OUTPUT_DRIVE_P0B 0x41
84 #define EXPAND18_REG_OUTPUT_DRIVE_P1A 0x42
85 #define EXPAND18_REG_OUTPUT_DRIVE_P1B 0x43
86 #define EXPAND18_REG_INPUT_LATCH_P0 0x44
87 #define EXPAND18_REG_INPUT_LATCH_P1 0x45
88 #define EXPAND18_REG_PULL_ENABLE_P0 0x46
89 #define EXPAND18_REG_PULL_ENABLE_P1 0x47
90 #define EXPAND18_REG_PULL_SELECTION_P0 0x48
91 #define EXPAND18_REG_PULL_SELECTION_P1 0x49
92 #define EXPAND18_REG_INT_MASK_P0 0x4A
93 #define EXPAND18_REG_INT_MASK_P1 0x4B
94 #define EXPAND18_REG_INT_STATUS_P0 0x4C
95 #define EXPAND18_REG_INT_STATUS_P1 0x4D
96 #define EXPAND18_REG_OUTPUT_PORT_CONFIG 0x4F
97 #define EXPAND18_REG_INT_EDGE_P0A 0x50
98 #define EXPAND18_REG_INT_EDGE_P0B 0x51
99 #define EXPAND18_REG_INT_EDGE_P1A 0x52
100 #define EXPAND18_REG_INT_EDGE_P1B 0x53
101 #define EXPAND18_REG_INT_CLEAR_P0 0x54
102 #define EXPAND18_REG_INT_CLEAR_P1 0x55
103 #define EXPAND18_REG_INPUT_STATUS_P0 0x56
104 #define EXPAND18_REG_INPUT_STATUS_P1 0x57
105 #define EXPAND18_REG_PIN_OUTPUT_P0_CONFIG 0x58
106 #define EXPAND18_REG_PIN_OUTPUT_P1_CONFIG 0x59
107 #define EXPAND18_REG_SWITCH_DEBOUNCE_EN_0 0x5A
108 #define EXPAND18_REG_SWITCH_DEBOUNCE_EN_1 0x5B
109 #define EXPAND18_REG_SWITCH_DEBOUNCE_CNT 0x5C
110  // expand18_reg
112 
127 #define EXPAND18_NO_PIN_MASK 0x00
128 #define EXPAND18_PIN_0_MASK 0x01
129 #define EXPAND18_PIN_1_MASK 0x02
130 #define EXPAND18_PIN_2_MASK 0x04
131 #define EXPAND18_PIN_3_MASK 0x08
132 #define EXPAND18_PIN_4_MASK 0x10
133 #define EXPAND18_PIN_5_MASK 0x20
134 #define EXPAND18_PIN_6_MASK 0x40
135 #define EXPAND18_PIN_7_MASK 0x80
136 #define EXPAND18_ALL_PINS_MASK 0xFF
137 
142 #define EXPAND18_PORT_0 0x00
143 #define EXPAND18_PORT_1 0x01
144 
149 #define EXPAND18_OUTPUT_DIRECTION 0x00
150 #define EXPAND18_INPUT_DIRECTION 0x01
151 
156 #define EXPAND18_PIN_LOW_LEVEL 0x00
157 #define EXPAND18_PIN_HIGH_LEVEL 0x01
158 
163 #define EXPAND18_AUTO_INCREMENT_BIT 0x80
164 
169 #define EXPAND18_DEVICE_ID 0x000830
170 
176 #define EXPAND18_DEVICE_ADDRESS_0 0x40
177 #define EXPAND18_DEVICE_ADDRESS_1 0x42
178 #define EXPAND18_ADDRESS_READ_BIT 0x01
179 
188 #define EXPAND18_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
189 #define EXPAND18_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
190  // expand18_set
192 
207 #define EXPAND18_MAP_MIKROBUS( cfg, mikrobus ) \
208  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
209  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
210  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
211  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
212  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
213  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
214  // expand18_map // expand18
217 
222 typedef struct
223 {
224  // Output pins
225  digital_out_t rst;
227  // Input pins
228  digital_in_t int_pin;
230  // Modules
231  spi_master_t spi;
233  pin_name_t chip_select;
235  uint8_t slave_address;
237 } expand18_t;
238 
243 typedef struct
244 {
245  // Communication gpio pins
246  pin_name_t miso;
247  pin_name_t mosi;
248  pin_name_t sck;
249  pin_name_t cs;
251  // Additional gpio pins
252  pin_name_t rst;
253  pin_name_t int_pin;
255  // static variable
256  uint32_t spi_speed;
257  spi_master_mode_t spi_mode;
258  spi_master_chip_select_polarity_t cs_polarity;
260  uint8_t spi_address;
263 
268 typedef enum
269 {
271  EXPAND18_ERROR = -1
272 
274 
291 
306 
320 
335 err_t expand18_generic_write ( expand18_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
336 
351 err_t expand18_generic_read ( expand18_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
352 
365 err_t expand18_write_register ( expand18_t *ctx, uint8_t reg, uint8_t data_in );
366 
379 err_t expand18_read_register ( expand18_t *ctx, uint8_t reg, uint8_t *data_out );
380 
390 void expand18_set_rst_pin ( expand18_t *ctx, uint8_t state );
391 
401 
411 
427 err_t expand18_set_pin_direction ( expand18_t *ctx, uint8_t direction, uint8_t port, uint8_t pin_mask );
428 
441 err_t expand18_set_all_pins_direction ( expand18_t *ctx, uint8_t direction );
442 
454 err_t expand18_set_all_pins_value ( expand18_t *ctx, uint8_t set_mask );
455 
470 err_t expand18_set_pin_value ( expand18_t *ctx, uint8_t port, uint8_t clr_mask, uint8_t set_mask );
471 
485 err_t expand18_read_port_value ( expand18_t *ctx, uint8_t port, uint8_t *data_out );
486 
487 #ifdef __cplusplus
488 }
489 #endif
490 #endif // EXPAND18_H
491  // expand18
493 
494 // ------------------------------------------------------------------------ END
expand18_generic_read
err_t expand18_generic_read(expand18_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Expand 18 data reading function.
expand18_set_pin_value
err_t expand18_set_pin_value(expand18_t *ctx, uint8_t port, uint8_t clr_mask, uint8_t set_mask)
Expand 18 set pin value function.
expand18_get_int_pin
uint8_t expand18_get_int_pin(expand18_t *ctx)
Expand 18 get INT pin function.
expand18_t::slave_address
uint8_t slave_address
Definition: expand18.h:235
expand18_set_all_pins_value
err_t expand18_set_all_pins_value(expand18_t *ctx, uint8_t set_mask)
Expand 18 set all pins value function.
expand18_set_rst_pin
void expand18_set_rst_pin(expand18_t *ctx, uint8_t state)
Expand 18 set RST pin function.
EXPAND18_OK
@ EXPAND18_OK
Definition: expand18.h:270
expand18_t::rst
digital_out_t rst
Definition: expand18.h:225
spi_specifics.h
This file contains SPI specific macros, functions, etc.
expand18_cfg_t::sck
pin_name_t sck
Definition: expand18.h:248
expand18_generic_write
err_t expand18_generic_write(expand18_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Expand 18 data writing function.
expand18_cfg_t::mosi
pin_name_t mosi
Definition: expand18.h:247
expand18_read_register
err_t expand18_read_register(expand18_t *ctx, uint8_t reg, uint8_t *data_out)
Expand 18 read register function.
expand18_set_pin_direction
err_t expand18_set_pin_direction(expand18_t *ctx, uint8_t direction, uint8_t port, uint8_t pin_mask)
Expand 18 set pin direction function.
expand18_t::int_pin
digital_in_t int_pin
Definition: expand18.h:228
expand18_cfg_t::spi_address
uint8_t spi_address
Definition: expand18.h:260
expand18_cfg_t::miso
pin_name_t miso
Definition: expand18.h:246
expand18_return_value_t
expand18_return_value_t
Expand 18 Click return value data.
Definition: expand18.h:269
expand18_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: expand18.h:257
expand18_t::spi
spi_master_t spi
Definition: expand18.h:231
expand18_cfg_setup
void expand18_cfg_setup(expand18_cfg_t *cfg)
Expand 18 configuration object setup function.
expand18_cfg_t::spi_speed
uint32_t spi_speed
Definition: expand18.h:256
expand18_default_cfg
err_t expand18_default_cfg(expand18_t *ctx)
Expand 18 default configuration function.
expand18_set_all_pins_direction
err_t expand18_set_all_pins_direction(expand18_t *ctx, uint8_t direction)
Expand 18 set all pins direction function.
expand18_cfg_t::cs
pin_name_t cs
Definition: expand18.h:249
expand18_write_register
err_t expand18_write_register(expand18_t *ctx, uint8_t reg, uint8_t data_in)
Expand 18 write register function.
expand18_reset_device
void expand18_reset_device(expand18_t *ctx)
Expand 18 reset device function.
expand18_t::chip_select
pin_name_t chip_select
Definition: expand18.h:233
expand18_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: expand18.h:258
expand18_cfg_t::int_pin
pin_name_t int_pin
Definition: expand18.h:253
expand18_cfg_t
Expand 18 Click configuration object.
Definition: expand18.h:244
expand18_read_port_value
err_t expand18_read_port_value(expand18_t *ctx, uint8_t port, uint8_t *data_out)
Expand 18 read port value function.
expand18_cfg_t::rst
pin_name_t rst
Definition: expand18.h:252
expand18_init
err_t expand18_init(expand18_t *ctx, expand18_cfg_t *cfg)
Expand 18 initialization function.
expand18_t
Expand 18 Click context object.
Definition: expand18.h:223
EXPAND18_ERROR
@ EXPAND18_ERROR
Definition: expand18.h:271