rs232toi2c  2.0.0.0
rs232toi2c.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 RS232TOI2C_H
29 #define RS232TOI2C_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_i2c_master.h"
48 
69 #define RS232TOI2C_CMD_WRITE_EEPROM 0x00
70 #define RS232TOI2C_CMD_READ_EEPROM 0x01
71 #define RS232TOI2C_CMD_WRITE_CURRENT_EEPROM 0x02
72 #define RS232TOI2C_CMD_READ_CURRENT_EEPROM 0x03
73 #define RS232TOI2C_CMD_ERASE_EEPROM_PAGE 0x04
74 #define RS232TOI2C_CMD_SET_GPIO_OUT 0x06
75 #define RS232TOI2C_CMD_GET_GPIO_IN 0x07
76 #define RS232TOI2C_CMD_WRITE_GPIO_CFG 0x08
77 #define RS232TOI2C_CMD_READ_GPIO_CFG 0x09
78 #define RS232TOI2C_CMD_READ_GPIO_INT_STATUS 0x0F
79 #define RS232TOI2C_CMD_READ_UART_STATUS 0x21
80 #define RS232TOI2C_CMD_ENABLE_INT 0x22
81 #define RS232TOI2C_CMD_INT_STATUS 0x23
82 #define RS232TOI2C_CMD_WRITE_TX_FIFO 0x24
83 #define RS232TOI2C_CMD_READ_RX_FIFO 0x25
84 #define RS232TOI2C_CMD_WRITE_BAUD_RATE 0x26
85 #define RS232TOI2C_CMD_READ_BAUD_RATE 0x27
86 #define RS232TOI2C_CMD_WRITE_UART_CFG 0x28
87 #define RS232TOI2C_CMD_READ_UART_CFG 0x29
88 #define RS232TOI2C_CMD_WRITE_TRANSMIT_WATERMARK 0x2A
89 #define RS232TOI2C_CMD_READ_TRANSMIT_WATERMARK 0x2B
90 #define RS232TOI2C_CMD_WRITE_RECEIVE_WATERMARK 0x2C
91 #define RS232TOI2C_CMD_READ_RECIEVE_WATERMARK 0x2D
92 #define RS232TOI2C_CMD_ENABLE_UART 0x2E
93 #define RS232TOI2C_CMD_READ_RX_TX_FIFO_LEVEL 0x31
94 #define RS232TOI2C_CMD_READ_SYSTEM_STATUS 0xE1
95 #define RS232TOI2C_CMD_READ_LAST_OP_RESULT 0xE3
96 #define RS232TOI2C_CMD_READ_SYSTEM_VERSION 0xE5
97 #define RS232TOI2C_CMD_READ_INT_SOURCE 0xEF
98  // rs232toi2c_reg
100 
115 #define RS232TOI2C_GPIO_SUB_CMD_DIR 0x01
116 #define RS232TOI2C_GPIO_SUB_CMD_PULL_UP 0x02
117 #define RS232TOI2C_GPIO_SUB_CMD_OPEN_DRAIN 0x03
118 #define RS232TOI2C_GPIO_SUB_CMD_DEBOUNCE 0x04
119 #define RS232TOI2C_GPIO_SUB_CMD_INTERRUPT 0x05
120 #define RS232TOI2C_GPIO_SUB_CMD_RESET_DEFAULT 0x0A
121 
126 #define RS232TOI2C_GPIO_MASK_NONE 0x00
127 #define RS232TOI2C_GPIO_0_MASK 0x01
128 #define RS232TOI2C_GPIO_1_MASK 0x02
129 #define RS232TOI2C_GPIO_2_MASK 0x04
130 #define RS232TOI2C_GPIO_MASK_ALL 0x07
131 
136 #define RS232TOI2C_UART_SUB_CMD_DATABITS 0x01
137 #define RS232TOI2C_UART_SUB_CMD_PARITY 0x02
138 #define RS232TOI2C_UART_SUB_CMD_STOP_BITS 0x03
139 #define RS232TOI2C_UART_SUB_CMD_FLOW_CONTROL 0x04
140 #define RS232TOI2C_UART_SUB_CMD_RESET_FIFO 0x06
141 #define RS232TOI2C_UART_SUB_CMD_LOOPBACK 0x08
142 #define RS232TOI2C_UART_SUB_CMD_RESET_DEFAULT 0x0A
143 
148 #define RS232TOI2C_UART_DATABITS_5 0x00
149 #define RS232TOI2C_UART_DATABITS_6 0x01
150 #define RS232TOI2C_UART_DATABITS_7 0x02
151 #define RS232TOI2C_UART_DATABITS_8 0x03
152 #define RS232TOI2C_UART_DATABITS_9 0x07
153 
158 #define RS232TOI2C_UART_PARITY_NONE 0x00
159 #define RS232TOI2C_UART_PARITY_ODD 0x03
160 #define RS232TOI2C_UART_PARITY_EVEN 0x02
161 
166 #define RS232TOI2C_UART_STOP_BIT_1 0x00
167 #define RS232TOI2C_UART_STOP_BIT_2 0x01
168 
173 #define RS232TOI2C_UART_FLOW_CONTROL_NONE 0x00
174 #define RS232TOI2C_UART_FLOW_CONTROL_HARDWARE 0x01
175 #define RS232TOI2C_UART_FLOW_CONTROL_SOFTWARE 0x02
176 
181 #define RS232TOI2C_UART_RESET_TX_FIFO 0x01
182 #define RS232TOI2C_UART_RESET_RX_FIFO 0x02
183 
188 #define RS232TOI2C_UART_LOOPBACK_OFF 0x00
189 #define RS232TOI2C_UART_LOOPBACK_ON 0x01
190 
195 #define RS232TOI2C_UART_DISABLE_RX_TX 0x00
196 #define RS232TOI2C_UART_ENABLE_RX 0x01
197 #define RS232TOI2C_UART_ENABLE_TX 0x02
198 #define RS232TOI2C_UART_ENABLE_RX_TX 0x03
199 
204 #define RS232TOI2C_UART_STATUS_PARITY_ERROR 0x01
205 #define RS232TOI2C_UART_STATUS_FRAME_ERROR 0x02
206 #define RS232TOI2C_UART_STATUS_OVERRUN_ERROR 0x04
207 #define RS232TOI2C_UART_STATUS_BREAK_RECEIVED 0x08
208 #define RS232TOI2C_UART_STATUS_RX_EMPTY 0x20
209 #define RS232TOI2C_UART_STATUS_TX_FULL 0x40
210 #define RS232TOI2C_UART_STATUS_TX_EMPTY 0x80
211 
216 #define RS232TOI2C_INTERRUPT_TX_RX_ERROR 0x01
217 #define RS232TOI2C_INTERRUPT_RX_FULL 0x02
218 #define RS232TOI2C_INTERRUPT_RX_WATERMARK 0x04
219 #define RS232TOI2C_INTERRUPT_RX_DATA 0x08
220 #define RS232TOI2C_INTERRUPT_TX_FULL 0x20
221 #define RS232TOI2C_INTERRUPT_TX_WATERMARK 0x40
222 #define RS232TOI2C_INTERRUPT_TX_EMPTY 0x80
223 
228 #define RS232TOI2C_EEPROM_PAGE_SIZE 32
229 #define RS232TOI2C_EEPROM_PAGE_NUM 16
230 #define RS232TOI2C_EEPROM_SIZE 512
231 
236 #define RS232TOI2C_CMD_ACCESS_BIT 0x08
237 
243 #define RS232TOI2C_SET_DEV_ADDR_A2_GND 0x53
244 #define RS232TOI2C_SET_DEV_ADDR_A2_VCC 0x57
245  // rs232toi2c_set
247 
262 #define RS232TOI2C_MAP_MIKROBUS( cfg, mikrobus ) \
263  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
264  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
265  cfg.wp = MIKROBUS( mikrobus, MIKROBUS_CS ); \
266  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
267  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
268  // rs232toi2c_map // rs232toi2c
271 
276 typedef struct
277 {
278  // Output pins
279  digital_out_t wp;
280  digital_out_t rst;
282  // Input pins
283  digital_in_t int_pin;
285  // Modules
286  i2c_master_t i2c;
288  // I2C slave address
289  uint8_t slave_address;
291 } rs232toi2c_t;
292 
297 typedef struct
298 {
299  pin_name_t scl;
300  pin_name_t sda;
302  pin_name_t wp;
303  pin_name_t rst;
304  pin_name_t int_pin;
306  uint32_t i2c_speed;
307  uint8_t i2c_address;
310 
315 typedef enum
316 {
318  RS232TOI2C_ERROR = -1
319 
321 
338 
353 
367 
382 err_t rs232toi2c_write_command ( rs232toi2c_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t data_len );
383 
398 err_t rs232toi2c_read_command ( rs232toi2c_t *ctx, uint8_t cmd, uint8_t *data_out, uint8_t data_len );
399 
414 err_t rs232toi2c_write_eeprom ( rs232toi2c_t *ctx, uint16_t address, uint8_t *data_in, uint8_t data_len );
415 
430 err_t rs232toi2c_read_eeprom ( rs232toi2c_t *ctx, uint16_t address, uint8_t *data_out, uint8_t data_len );
431 
443 err_t rs232toi2c_erase_eeprom_page ( rs232toi2c_t *ctx, uint8_t page_num );
444 
457 err_t rs232toi2c_set_gpio_out ( rs232toi2c_t *ctx, uint8_t mask, uint8_t state );
458 
470 err_t rs232toi2c_get_gpio_in ( rs232toi2c_t *ctx, uint8_t *state );
471 
485 err_t rs232toi2c_write_gpio_config ( rs232toi2c_t *ctx, uint8_t sub_cmd, uint8_t mask, uint8_t config );
486 
499 err_t rs232toi2c_read_gpio_config ( rs232toi2c_t *ctx, uint8_t sub_cmd, uint8_t *config );
500 
513 err_t rs232toi2c_write_uart_config ( rs232toi2c_t *ctx, uint8_t sub_cmd, uint8_t config );
514 
526 err_t rs232toi2c_read_uart_config ( rs232toi2c_t *ctx, uint8_t *config );
527 
542 err_t rs232toi2c_enable_uart ( rs232toi2c_t *ctx, uint8_t state );
543 
555 err_t rs232toi2c_set_baud_rate ( rs232toi2c_t *ctx, uint32_t baud_rate );
556 
568 err_t rs232toi2c_get_baud_rate ( rs232toi2c_t *ctx, uint32_t *baud_rate );
569 
581 err_t rs232toi2c_enable_interrupts ( rs232toi2c_t *ctx, uint8_t int_mask );
582 
594 err_t rs232toi2c_get_interrupt_status ( rs232toi2c_t *ctx, uint8_t *int_status );
595 
607 err_t rs232toi2c_get_uart_status ( rs232toi2c_t *ctx, uint8_t *uart_status );
608 
620 err_t rs232toi2c_get_gpio_status ( rs232toi2c_t *ctx, uint8_t *gpio_status );
621 
634 err_t rs232toi2c_write_tx_fifo ( rs232toi2c_t *ctx, uint8_t *data_in, uint8_t data_len );
635 
648 err_t rs232toi2c_read_rx_fifo ( rs232toi2c_t *ctx, uint8_t *data_out, uint8_t *data_len );
649 
661 err_t rs232toi2c_read_system_version ( rs232toi2c_t *ctx, uint32_t *system_version );
662 
672 void rs232toi2c_set_rst_pin ( rs232toi2c_t *ctx, uint8_t state );
673 
683 void rs232toi2c_set_wp_pin ( rs232toi2c_t *ctx, uint8_t state );
684 
694 
704 
705 #ifdef __cplusplus
706 }
707 #endif
708 #endif // RS232TOI2C_H
709  // rs232toi2c
711 
712 // ------------------------------------------------------------------------ END
rs232toi2c_get_gpio_status
err_t rs232toi2c_get_gpio_status(rs232toi2c_t *ctx, uint8_t *gpio_status)
RS232 to I2C get gpio status function.
rs232toi2c_set_baud_rate
err_t rs232toi2c_set_baud_rate(rs232toi2c_t *ctx, uint32_t baud_rate)
RS232 to I2C set baud rate function.
rs232toi2c_init
err_t rs232toi2c_init(rs232toi2c_t *ctx, rs232toi2c_cfg_t *cfg)
RS232 to I2C initialization function.
rs232toi2c_cfg_t
RS232 to I2C Click configuration object.
Definition: rs232toi2c.h:298
rs232toi2c_cfg_t::wp
pin_name_t wp
Definition: rs232toi2c.h:302
rs232toi2c_write_uart_config
err_t rs232toi2c_write_uart_config(rs232toi2c_t *ctx, uint8_t sub_cmd, uint8_t config)
RS232 to I2C write uart config function.
rs232toi2c_enable_interrupts
err_t rs232toi2c_enable_interrupts(rs232toi2c_t *ctx, uint8_t int_mask)
RS232 to I2C enable interrupts function.
rs232toi2c_t::slave_address
uint8_t slave_address
Definition: rs232toi2c.h:289
rs232toi2c_erase_eeprom_page
err_t rs232toi2c_erase_eeprom_page(rs232toi2c_t *ctx, uint8_t page_num)
RS232 to I2C erase eeprom page function.
rs232toi2c_t
RS232 to I2C Click context object.
Definition: rs232toi2c.h:277
rs232toi2c_cfg_t::sda
pin_name_t sda
Definition: rs232toi2c.h:300
rs232toi2c_cfg_t::rst
pin_name_t rst
Definition: rs232toi2c.h:303
RS232TOI2C_ERROR
@ RS232TOI2C_ERROR
Definition: rs232toi2c.h:318
rs232toi2c_reset_device
void rs232toi2c_reset_device(rs232toi2c_t *ctx)
RS232 to I2C reset device function.
rs232toi2c_t::int_pin
digital_in_t int_pin
Definition: rs232toi2c.h:283
rs232toi2c_write_tx_fifo
err_t rs232toi2c_write_tx_fifo(rs232toi2c_t *ctx, uint8_t *data_in, uint8_t data_len)
RS232 to I2C write tx fifo function.
rs232toi2c_read_system_version
err_t rs232toi2c_read_system_version(rs232toi2c_t *ctx, uint32_t *system_version)
RS232 to I2C read system version function.
rs232toi2c_read_rx_fifo
err_t rs232toi2c_read_rx_fifo(rs232toi2c_t *ctx, uint8_t *data_out, uint8_t *data_len)
RS232 to I2C read rx fifo function.
rs232toi2c_read_gpio_config
err_t rs232toi2c_read_gpio_config(rs232toi2c_t *ctx, uint8_t sub_cmd, uint8_t *config)
RS232 to I2C read gpio config function.
rs232toi2c_get_gpio_in
err_t rs232toi2c_get_gpio_in(rs232toi2c_t *ctx, uint8_t *state)
RS232 to I2C get gpio in function.
RS232TOI2C_OK
@ RS232TOI2C_OK
Definition: rs232toi2c.h:317
rs232toi2c_set_rst_pin
void rs232toi2c_set_rst_pin(rs232toi2c_t *ctx, uint8_t state)
RS232 to I2C set rst pin function.
rs232toi2c_enable_uart
err_t rs232toi2c_enable_uart(rs232toi2c_t *ctx, uint8_t state)
RS232 to I2C enable uart function.
rs232toi2c_set_gpio_out
err_t rs232toi2c_set_gpio_out(rs232toi2c_t *ctx, uint8_t mask, uint8_t state)
RS232 to I2C set gpio out function.
rs232toi2c_t::wp
digital_out_t wp
Definition: rs232toi2c.h:279
rs232toi2c_set_wp_pin
void rs232toi2c_set_wp_pin(rs232toi2c_t *ctx, uint8_t state)
RS232 to I2C set wp pin function.
rs232toi2c_get_interrupt_status
err_t rs232toi2c_get_interrupt_status(rs232toi2c_t *ctx, uint8_t *int_status)
RS232 to I2C get interrupt status function.
rs232toi2c_write_eeprom
err_t rs232toi2c_write_eeprom(rs232toi2c_t *ctx, uint16_t address, uint8_t *data_in, uint8_t data_len)
RS232 to I2C write eeprom function.
rs232toi2c_write_command
err_t rs232toi2c_write_command(rs232toi2c_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t data_len)
RS232 to I2C write command function.
rs232toi2c_write_gpio_config
err_t rs232toi2c_write_gpio_config(rs232toi2c_t *ctx, uint8_t sub_cmd, uint8_t mask, uint8_t config)
RS232 to I2C write gpio config function.
rs232toi2c_t::i2c
i2c_master_t i2c
Definition: rs232toi2c.h:286
rs232toi2c_return_value_t
rs232toi2c_return_value_t
RS232 to I2C Click return value data.
Definition: rs232toi2c.h:316
rs232toi2c_cfg_t::i2c_address
uint8_t i2c_address
Definition: rs232toi2c.h:307
rs232toi2c_cfg_setup
void rs232toi2c_cfg_setup(rs232toi2c_cfg_t *cfg)
RS232 to I2C configuration object setup function.
rs232toi2c_cfg_t::scl
pin_name_t scl
Definition: rs232toi2c.h:299
rs232toi2c_get_baud_rate
err_t rs232toi2c_get_baud_rate(rs232toi2c_t *ctx, uint32_t *baud_rate)
RS232 to I2C get baud rate function.
rs232toi2c_get_int_pin
uint8_t rs232toi2c_get_int_pin(rs232toi2c_t *ctx)
RS232 to I2C get int pin function.
rs232toi2c_default_cfg
err_t rs232toi2c_default_cfg(rs232toi2c_t *ctx)
RS232 to I2C default configuration function.
rs232toi2c_read_command
err_t rs232toi2c_read_command(rs232toi2c_t *ctx, uint8_t cmd, uint8_t *data_out, uint8_t data_len)
RS232 to I2C read command function.
rs232toi2c_read_uart_config
err_t rs232toi2c_read_uart_config(rs232toi2c_t *ctx, uint8_t *config)
RS232 to I2C read uart config function.
rs232toi2c_cfg_t::int_pin
pin_name_t int_pin
Definition: rs232toi2c.h:304
rs232toi2c_get_uart_status
err_t rs232toi2c_get_uart_status(rs232toi2c_t *ctx, uint8_t *uart_status)
RS232 to I2C get uart status function.
rs232toi2c_read_eeprom
err_t rs232toi2c_read_eeprom(rs232toi2c_t *ctx, uint16_t address, uint8_t *data_out, uint8_t data_len)
RS232 to I2C read eeprom function.
rs232toi2c_t::rst
digital_out_t rst
Definition: rs232toi2c.h:280
rs232toi2c_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rs232toi2c.h:306