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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_uart.h"
48 
69 #define UARTTOI2C_CMD_START 'S'
70 #define UARTTOI2C_CMD_STOP 'P'
71 #define UARTTOI2C_CMD_READ_REG 'R'
72 #define UARTTOI2C_CMD_WRITE_REG 'W'
73 #define UARTTOI2C_CMD_READ_GPIO 'I'
74 #define UARTTOI2C_CMD_WRITE_GPIO 'O'
75 #define UARTTOI2C_CMD_READ_VERSION 'V'
76 #define UARTTOI2C_CMD_POWER_DOWN 'Z'
77 
82 #define UARTTOI2C_REG_BRG0 0x00
83 #define UARTTOI2C_REG_BRG1 0x01
84 #define UARTTOI2C_REG_PORT_CONF_1 0x02
85 #define UARTTOI2C_REG_PORT_CONF_2 0x03
86 #define UARTTOI2C_REG_IO_STATE 0x04
87 #define UARTTOI2C_REG_RESERVED 0x05
88 #define UARTTOI2C_REG_I2C_ADR 0x06
89 #define UARTTOI2C_REG_I2C_CLK_L 0x07
90 #define UARTTOI2C_REG_I2C_CLK_H 0x08
91 #define UARTTOI2C_REG_I2C_TO 0x09
92 #define UARTTOI2C_REG_I2C_STAT 0x0A
93 
98 #define UARTTOI2C_TIMEOUT_MS 3000
99 
104 #define UARTTOI2C_POWER_UP_BYTE_0 0x4F
105 #define UARTTOI2C_POWER_UP_BYTE_1 0x4B
106 
111 #define UARTTOI2C_NO_PIN_MASK 0x00
112 #define UARTTOI2C_PIN_0_MASK 0x01
113 #define UARTTOI2C_PIN_1_MASK 0x02
114 #define UARTTOI2C_PIN_2_MASK 0x04
115 #define UARTTOI2C_PIN_3_MASK 0x08
116 #define UARTTOI2C_PIN_4_MASK 0x10
117 #define UARTTOI2C_PIN_5_MASK 0x20
118 #define UARTTOI2C_PIN_6_MASK 0x40
119 #define UARTTOI2C_PIN_7_MASK 0x80
120 #define UARTTOI2C_ALL_PINS_MASK 0xFF
121 
126 #define UARTTOI2C_PIN_DIR_INPUT 0x00
127 #define UARTTOI2C_PIN_DIR_OUTPUT_PP 0x02
128 #define UARTTOI2C_PIN_DIR_OUTPUT_OD 0x03
129 #define UARTTOI2C_PIN_DIR_OUTPUT_MASK 0x03
130 
136 #define UARTTOI2C_TX_DRV_BUFFER_SIZE 100
137 #define UARTTOI2C_RX_DRV_BUFFER_SIZE 300
138  // uarttoi2c_cmd
140 
155 #define UARTTOI2C_MAP_MIKROBUS( cfg, mikrobus ) \
156  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
157  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
158  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
159  // uarttoi2c_map // uarttoi2c
162 
167 typedef struct
168 {
169  // Output pins
170  digital_out_t rst;
172  // Modules
173  uart_t uart;
175  // Buffers
176  char uart_rx_buffer[ UARTTOI2C_RX_DRV_BUFFER_SIZE ];
177  char uart_tx_buffer[ UARTTOI2C_TX_DRV_BUFFER_SIZE ];
179 } uarttoi2c_t;
180 
185 typedef struct
186 {
187  // Communication gpio pins
188  pin_name_t rx_pin;
189  pin_name_t tx_pin;
191  // Additional gpio pins
192  pin_name_t rst;
194  // Static variable
195  uint32_t baud_rate;
197  uart_data_bits_t data_bit;
198  uart_parity_t parity_bit;
199  uart_stop_bits_t stop_bit;
202 
207 typedef enum
208 {
212 
214 
231 
246 
260 
273 err_t uarttoi2c_generic_write ( uarttoi2c_t *ctx, char *data_in, uint16_t len );
274 
287 err_t uarttoi2c_generic_read ( uarttoi2c_t *ctx, char *data_out, uint16_t len );
288 
298 void uarttoi2c_set_rst_pin ( uarttoi2c_t *ctx, uint8_t state );
299 
311 
324 err_t uarttoi2c_write_register ( uarttoi2c_t *ctx, uint8_t reg, uint8_t data_in );
325 
339 err_t uarttoi2c_read_register ( uarttoi2c_t *ctx, uint8_t reg, uint8_t *data_out );
340 
350 void uarttoi2c_gpio_write ( uarttoi2c_t *ctx, uint8_t gpio_data );
351 
364 err_t uarttoi2c_gpio_read ( uarttoi2c_t *ctx, uint8_t *gpio_data );
365 
381 err_t uarttoi2c_gpio_config ( uarttoi2c_t *ctx, uint8_t direction, uint8_t pin_mask );
382 
395 err_t uarttoi2c_read_version ( uarttoi2c_t *ctx, uint8_t *version );
396 
410 err_t uarttoi2c_i2c_write ( uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t len );
411 
426 err_t uarttoi2c_i2c_read ( uarttoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_out, uint8_t len );
427 
444 err_t uarttoi2c_i2c_write_then_read ( uarttoi2c_t *ctx, uint8_t slave_addr,
445  uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len );
446 
447 #ifdef __cplusplus
448 }
449 #endif
450 #endif // UARTTOI2C_H
451  // uarttoi2c
453 
454 // ------------------------------------------------------------------------ END
UARTTOI2C_ERROR
@ UARTTOI2C_ERROR
Definition: uarttoi2c.h:210
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:195
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:192
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:196
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:211
uarttoi2c_t
UART to I2C Click context object.
Definition: uarttoi2c.h:168
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:188
UARTTOI2C_OK
@ UARTTOI2C_OK
Definition: uarttoi2c.h:209
uarttoi2c_cfg_t::tx_pin
pin_name_t tx_pin
Definition: uarttoi2c.h:189
uarttoi2c_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: uarttoi2c.h:199
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:186
UARTTOI2C_TX_DRV_BUFFER_SIZE
#define UARTTOI2C_TX_DRV_BUFFER_SIZE
UART to I2C driver buffer size.
Definition: uarttoi2c.h:136
uarttoi2c_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: uarttoi2c.h:198
uarttoi2c_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: uarttoi2c.h:197
uarttoi2c_t::rst
digital_out_t rst
Definition: uarttoi2c.h:170
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:208
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:137
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:173
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.