uarttoi2c  2.1.0.0
uarttoi2c.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 UARTTOI2C_H
29 #define UARTTOI2C_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_uart.h"
52 
73 #define UARTTOI2C_CMD_START 'S'
74 #define UARTTOI2C_CMD_STOP 'P'
75 #define UARTTOI2C_CMD_READ_REG 'R'
76 #define UARTTOI2C_CMD_WRITE_REG 'W'
77 #define UARTTOI2C_CMD_READ_GPIO 'I'
78 #define UARTTOI2C_CMD_WRITE_GPIO 'O'
79 #define UARTTOI2C_CMD_READ_VERSION 'V'
80 #define UARTTOI2C_CMD_POWER_DOWN 'Z'
81 
86 #define UARTTOI2C_REG_BRG0 0x00
87 #define UARTTOI2C_REG_BRG1 0x01
88 #define UARTTOI2C_REG_PORT_CONF_1 0x02
89 #define UARTTOI2C_REG_PORT_CONF_2 0x03
90 #define UARTTOI2C_REG_IO_STATE 0x04
91 #define UARTTOI2C_REG_RESERVED 0x05
92 #define UARTTOI2C_REG_I2C_ADR 0x06
93 #define UARTTOI2C_REG_I2C_CLK_L 0x07
94 #define UARTTOI2C_REG_I2C_CLK_H 0x08
95 #define UARTTOI2C_REG_I2C_TO 0x09
96 #define UARTTOI2C_REG_I2C_STAT 0x0A
97 
102 #define UARTTOI2C_TIMEOUT_MS 3000
103 
108 #define UARTTOI2C_POWER_UP_BYTE_0 0x4F
109 #define UARTTOI2C_POWER_UP_BYTE_1 0x4B
110 
115 #define UARTTOI2C_NO_PIN_MASK 0x00
116 #define UARTTOI2C_PIN_0_MASK 0x01
117 #define UARTTOI2C_PIN_1_MASK 0x02
118 #define UARTTOI2C_PIN_2_MASK 0x04
119 #define UARTTOI2C_PIN_3_MASK 0x08
120 #define UARTTOI2C_PIN_4_MASK 0x10
121 #define UARTTOI2C_PIN_5_MASK 0x20
122 #define UARTTOI2C_PIN_6_MASK 0x40
123 #define UARTTOI2C_PIN_7_MASK 0x80
124 #define UARTTOI2C_ALL_PINS_MASK 0xFF
125 
130 #define UARTTOI2C_PIN_DIR_INPUT 0x00
131 #define UARTTOI2C_PIN_DIR_OUTPUT_PP 0x02
132 #define UARTTOI2C_PIN_DIR_OUTPUT_OD 0x03
133 #define UARTTOI2C_PIN_DIR_OUTPUT_MASK 0x03
134 
140 #define UARTTOI2C_TX_DRV_BUFFER_SIZE 100
141 #define UARTTOI2C_RX_DRV_BUFFER_SIZE 300
142  // uarttoi2c_cmd
144 
159 #define UARTTOI2C_MAP_MIKROBUS( cfg, mikrobus ) \
160  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
161  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
162  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
163  // uarttoi2c_map // uarttoi2c
166 
171 typedef struct
172 {
173  // Output pins
174  digital_out_t rst;
176  // Modules
177  uart_t uart;
179  // Buffers
180  char uart_rx_buffer[ UARTTOI2C_RX_DRV_BUFFER_SIZE ];
181  char uart_tx_buffer[ UARTTOI2C_TX_DRV_BUFFER_SIZE ];
183 } uarttoi2c_t;
184 
189 typedef struct
190 {
191  // Communication gpio pins
192  pin_name_t rx_pin;
193  pin_name_t tx_pin;
195  // Additional gpio pins
196  pin_name_t rst;
198  // Static variable
199  uint32_t baud_rate;
201  uart_data_bits_t data_bit;
202  uart_parity_t parity_bit;
203  uart_stop_bits_t stop_bit;
206 
211 typedef enum
212 {
216 
218 
235 
250 
264 
277 err_t uarttoi2c_generic_write ( uarttoi2c_t *ctx, char *data_in, uint16_t len );
278 
291 err_t uarttoi2c_generic_read ( uarttoi2c_t *ctx, char *data_out, uint16_t len );
292 
302 void uarttoi2c_set_rst_pin ( uarttoi2c_t *ctx, uint8_t state );
303 
315 
328 err_t uarttoi2c_write_register ( uarttoi2c_t *ctx, uint8_t reg, uint8_t data_in );
329 
343 err_t uarttoi2c_read_register ( uarttoi2c_t *ctx, uint8_t reg, uint8_t *data_out );
344 
354 void uarttoi2c_gpio_write ( uarttoi2c_t *ctx, uint8_t gpio_data );
355 
368 err_t uarttoi2c_gpio_read ( uarttoi2c_t *ctx, uint8_t *gpio_data );
369 
385 err_t uarttoi2c_gpio_config ( uarttoi2c_t *ctx, uint8_t direction, uint8_t pin_mask );
386 
399 err_t uarttoi2c_read_version ( uarttoi2c_t *ctx, uint8_t *version );
400 
414 err_t uarttoi2c_i2c_write ( uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t len );
415 
430 err_t uarttoi2c_i2c_read ( uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_out, uint8_t len );
431 
448 err_t uarttoi2c_i2c_write_then_read ( uarttoi2c_t *ctx, uint8_t slave_addr,
449  uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len );
450 
451 #ifdef __cplusplus
452 }
453 #endif
454 #endif // UARTTOI2C_H
455  // uarttoi2c
457 
458 // ------------------------------------------------------------------------ END
UARTTOI2C_ERROR
@ UARTTOI2C_ERROR
Definition: uarttoi2c.h:214
uarttoi2c_gpio_read
err_t uarttoi2c_gpio_read(uarttoi2c_t *ctx, uint8_t *gpio_data)
UART to I2C gpio read function.
uarttoi2c_cfg_t::baud_rate
uint32_t baud_rate
Definition: uarttoi2c.h:199
uarttoi2c_generic_write
err_t uarttoi2c_generic_write(uarttoi2c_t *ctx, char *data_in, uint16_t len)
UART to I2C data writing function.
uarttoi2c_cfg_t::rst
pin_name_t rst
Definition: uarttoi2c.h:196
uarttoi2c_init
err_t uarttoi2c_init(uarttoi2c_t *ctx, uarttoi2c_cfg_t *cfg)
UART to I2C initialization function.
uarttoi2c_cfg_t::uart_blocking
bool uart_blocking
Definition: uarttoi2c.h:200
uarttoi2c_gpio_write
void uarttoi2c_gpio_write(uarttoi2c_t *ctx, uint8_t gpio_data)
UART to I2C gpio write function.
uarttoi2c_i2c_write
err_t uarttoi2c_i2c_write(uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t len)
UART to I2C i2c write function.
UARTTOI2C_TIMEOUT_ERROR
@ UARTTOI2C_TIMEOUT_ERROR
Definition: uarttoi2c.h:215
uarttoi2c_t
UART to I2C Click context object.
Definition: uarttoi2c.h:172
uarttoi2c_gpio_config
err_t uarttoi2c_gpio_config(uarttoi2c_t *ctx, uint8_t direction, uint8_t pin_mask)
UART to I2C gpio config function.
uarttoi2c_cfg_t::rx_pin
pin_name_t rx_pin
Definition: uarttoi2c.h:192
UARTTOI2C_OK
@ UARTTOI2C_OK
Definition: uarttoi2c.h:213
uarttoi2c_cfg_t::tx_pin
pin_name_t tx_pin
Definition: uarttoi2c.h:193
uarttoi2c_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: uarttoi2c.h:203
uarttoi2c_read_version
err_t uarttoi2c_read_version(uarttoi2c_t *ctx, uint8_t *version)
UART to I2C read version function.
uarttoi2c_generic_read
err_t uarttoi2c_generic_read(uarttoi2c_t *ctx, char *data_out, uint16_t len)
UART to I2C data reading function.
uarttoi2c_cfg_t
UART to I2C Click configuration object.
Definition: uarttoi2c.h:190
UARTTOI2C_TX_DRV_BUFFER_SIZE
#define UARTTOI2C_TX_DRV_BUFFER_SIZE
UART to I2C driver buffer size.
Definition: uarttoi2c.h:140
uarttoi2c_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: uarttoi2c.h:202
uarttoi2c_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: uarttoi2c.h:201
uarttoi2c_t::rst
digital_out_t rst
Definition: uarttoi2c.h:174
uarttoi2c_i2c_write_then_read
err_t uarttoi2c_i2c_write_then_read(uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len)
UART to I2C i2c write then read function.
uarttoi2c_return_value_t
uarttoi2c_return_value_t
UART to I2C Click return value data.
Definition: uarttoi2c.h:212
uarttoi2c_set_rst_pin
void uarttoi2c_set_rst_pin(uarttoi2c_t *ctx, uint8_t state)
UART to I2C set rst pin function.
uarttoi2c_default_cfg
err_t uarttoi2c_default_cfg(uarttoi2c_t *ctx)
UART to I2C default configuration function.
UARTTOI2C_RX_DRV_BUFFER_SIZE
#define UARTTOI2C_RX_DRV_BUFFER_SIZE
Definition: uarttoi2c.h:141
uarttoi2c_write_register
err_t uarttoi2c_write_register(uarttoi2c_t *ctx, uint8_t reg, uint8_t data_in)
UART to I2C write register function.
uarttoi2c_read_register
err_t uarttoi2c_read_register(uarttoi2c_t *ctx, uint8_t reg, uint8_t *data_out)
UART to I2C read register function.
uarttoi2c_t::uart
uart_t uart
Definition: uarttoi2c.h:177
uarttoi2c_reset_device
err_t uarttoi2c_reset_device(uarttoi2c_t *ctx)
UART to I2C reset device function.
uarttoi2c_cfg_setup
void uarttoi2c_cfg_setup(uarttoi2c_cfg_t *cfg)
UART to I2C configuration object setup function.
uarttoi2c_i2c_read
err_t uarttoi2c_i2c_read(uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_out, uint8_t len)
UART to I2C i2c read function.