adapter 2.0.0.0
adapter.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 ADAPTER_H
36#define ADAPTER_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_i2c_master.h"
55#include "drv_spi_master.h"
56
57// -------------------------------------------------------------- PUBLIC MACROS
68#define ADAPTER_MAP_MIKROBUS( cfg, mikrobus ) \
69 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
70 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
71 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
72 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
73 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
74 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
75 cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
76 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT ); \
77 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
78 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX )
85#define ADAPTER_MASTER_I2C 0
86#define ADAPTER_MASTER_SPI 1
93#define ADAPTER_RETVAL uint8_t
94
95#define ADAPTER_OK 0x00
96#define ADAPTER_INIT_ERROR 0xFF
103#define I2C_SLAVE_ADDR_DEMO 0x4C
106 // End group macro
107// --------------------------------------------------------------- PUBLIC TYPES
116typedef uint8_t adapter_select_t;
117
121typedef void ( *adapter_master_io_t )( struct adapter_s*, uint8_t, uint8_t*, uint8_t );
122
126typedef struct adapter_s
127{
128 // Output pins
129
130 digital_out_t cs;
131 digital_out_t pwm;
132 digital_out_t tx_pin;
133 digital_out_t rx_pin;
134
135 // Input pins
136
137 digital_in_t int_pin;
138
139 // Modules
140
141 i2c_master_t i2c;
142 spi_master_t spi;
143
144 // ctx variable
145
146
148 pin_name_t chip_select;
152
154
158typedef struct
159{
160 // Communication gpio pins
161
162 pin_name_t scl;
163 pin_name_t sda;
164 pin_name_t miso;
165 pin_name_t mosi;
166 pin_name_t sck;
167 pin_name_t cs;
168 pin_name_t rx_pin;
169 pin_name_t tx_pin;
170
171 // Additional gpio pins
172
173 pin_name_t pwm;
174 pin_name_t int_pin;
175
176 // static variable
177
178 uint32_t i2c_speed;
179 uint8_t i2c_address;
180
181 uint32_t spi_speed;
182 spi_master_mode_t spi_mode;
183 spi_master_chip_select_polarity_t cs_polarity;
184
186
188
189 // End types group
190// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
196#ifdef __cplusplus
197extern "C"{
198#endif
199
209
219
230void adapter_generic_write ( adapter_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
231
242void adapter_generic_read ( adapter_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
243
255void adapter_generic_transfer( adapter_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
256
265
274
283
292
301
310
321
322#ifdef __cplusplus
323}
324#endif
325#endif // _ADAPTER_H_
326
327 // End public_function group
329
330// ------------------------------------------------------------------------- END
#define ADAPTER_RETVAL
Definition adapter.h:93
uint8_t adapter_check_int(adapter_t *ctx)
Check INT function.
void adapter_cfg_setup(adapter_cfg_t *cfg)
Config Object Initialization function.
void adapter_turn_rx_on(adapter_t *ctx)
Turn RX on function.
void adapter_turn_pwm_off(adapter_t *ctx)
Turn PWM off function.
void adapter_generic_write(adapter_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void adapter_turn_tx_on(adapter_t *ctx)
Turn TX on function.
void adapter_turn_pwm_on(adapter_t *ctx)
Turn PWM on function.
void adapter_generic_transfer(adapter_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
void adapter_turn_tx_off(adapter_t *ctx)
Turn TX off function.
void adapter_generic_read(adapter_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void adapter_turn_rx_off(adapter_t *ctx)
Turn RX off function.
ADAPTER_RETVAL adapter_init(adapter_t *ctx, adapter_cfg_t *cfg)
Initialization function.
struct adapter_s adapter_t
Click ctx object definition.
uint8_t adapter_select_t
Communication type.
Definition adapter.h:116
void(* adapter_master_io_t)(struct adapter_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition adapter.h:121
Click configuration structure definition.
Definition adapter.h:159
uint32_t i2c_speed
Definition adapter.h:178
spi_master_chip_select_polarity_t cs_polarity
Definition adapter.h:183
pin_name_t sck
Definition adapter.h:166
spi_master_mode_t spi_mode
Definition adapter.h:182
pin_name_t tx_pin
Definition adapter.h:169
pin_name_t mosi
Definition adapter.h:165
adapter_select_t sel
Definition adapter.h:185
uint32_t spi_speed
Definition adapter.h:181
pin_name_t rx_pin
Definition adapter.h:168
pin_name_t scl
Definition adapter.h:162
pin_name_t int_pin
Definition adapter.h:174
pin_name_t pwm
Definition adapter.h:173
pin_name_t miso
Definition adapter.h:164
pin_name_t sda
Definition adapter.h:163
pin_name_t cs
Definition adapter.h:167
uint8_t i2c_address
Definition adapter.h:179
Click ctx object definition.
Definition adapter.h:127
digital_out_t cs
Definition adapter.h:130
adapter_select_t master_sel
Definition adapter.h:151
spi_master_t spi
Definition adapter.h:142
adapter_master_io_t read_f
Definition adapter.h:150
adapter_master_io_t write_f
Definition adapter.h:149
digital_out_t pwm
Definition adapter.h:131
digital_in_t int_pin
Definition adapter.h:137
i2c_master_t i2c
Definition adapter.h:141
digital_out_t rx_pin
Definition adapter.h:133
uint8_t slave_address
Definition adapter.h:147
digital_out_t tx_pin
Definition adapter.h:132
pin_name_t chip_select
Definition adapter.h:148