i2ctospi  2.0.0.0
i2ctospi.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 I2CTOSPI_H
36 #define I2CTOSPI_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 
56 // -------------------------------------------------------------- PUBLIC MACROS
66 #define I2CTOSPI_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define I2CTOSPI_RETVAL uint8_t
79 
80 #define I2CTOSPI_OK 0x00
81 #define I2CTOSPI_INIT_ERROR 0xFF
82 
88 #define I2CTOSPI_I2C_SLAVE_ADDRESS_0 0x28
89 #define I2CTOSPI_I2C_SLAVE_ADDRESS_1 0x29
90 #define I2CTOSPI_I2C_SLAVE_ADDRESS_2 0x2A
91 #define I2CTOSPI_I2C_SLAVE_ADDRESS_3 0x2B
92 #define I2CTOSPI_I2C_SLAVE_ADDRESS_4 0x2C
93 #define I2CTOSPI_I2C_SLAVE_ADDRESS_5 0x2D
94 #define I2CTOSPI_I2C_SLAVE_ADDRESS_6 0x2E
95 #define I2CTOSPI_I2C_SLAVE_ADDRESS_7 0x2F
96 
102 #define I2CTOSPI_CONFIGURE_SPI 0xF0
103 #define I2CTOSPI_CLEAR_INTERRUPT 0xF1
104 #define I2CTOSPI_IDLE_MODE 0xF2
105 #define I2CTOSPI_GPIO_WRITE 0xF4
106 #define I2CTOSPI_GPIO_READ 0xF5
107 #define I2CTOSPI_GPIO_ENABLE 0xF6
108 #define I2CTOSPI_GPIO_CONFIGURATION 0xF7
109 
115 #define I2CTOSPI_CONFIGURE_SPI_ORDER_MSB_FIRST 0x00
116 #define I2CTOSPI_CONFIGURE_SPI_ORDER_LSB_FIRST 0x20
117 #define I2CTOSPI_CONFIGURE_SPI_MODE_SPICLK_LOW_LEADING_EDGE 0x00
118 #define I2CTOSPI_CONFIGURE_SPI_MODE_SPICLK_LOW_TRAILING_EDGE 0x04
119 #define I2CTOSPI_CONFIGURE_SPI_MODE_SPICLK_HIGH_LEADING_EDGE 0x08
120 #define I2CTOSPI_CONFIGURE_SPI_MODE_SPICLK_HIGH_TRAILING_EDGE 0x0C
121 #define I2CTOSPI_CONFIGURE_SPI_CLOCK_RATE_1843kHz 0x00
122 #define I2CTOSPI_CONFIGURE_SPI_CLOCK_RATE_461kHz 0x01
123 #define I2CTOSPI_CONFIGURE_SPI_CLOCK_RATE_115kHz 0x02
124 #define I2CTOSPI_CONFIGURE_SPI_CLOCK_RATE_58kHz 0x03
125 
131 #define I2CTOSPI_CONFIGURE_GPIO_WRITE_PORT_ENABLE_SS0 0x01
132 #define I2CTOSPI_CONFIGURE_GPIO_WRITE_PORT_ENABLE_SS1 0x02
133 #define I2CTOSPI_CONFIGURE_GPIO_WRITE_PORT_ENABLE_SS2 0x04
134 #define I2CTOSPI_CONFIGURE_GPIO_WRITE_PORT_ENABLE_SS3 0x08
135 
141 #define I2CTOSPI_CONFIGURE_GPIO_READ_PORT_ENABLE_SS0 0x01
142 #define I2CTOSPI_CONFIGURE_GPIO_READ_PORT_ENABLE_SS1 0x02
143 #define I2CTOSPI_CONFIGURE_GPIO_READ_PORT_ENABLE_SS2 0x04
144 #define I2CTOSPI_CONFIGURE_GPIO_READ_PORT_ENABLE_SS3 0x08
145 
151 #define I2CTOSPI_CONFIGURE_GPIO_BIDIRECTIONAL_PORT_ENABLE_SS0 0x01
152 #define I2CTOSPI_CONFIGURE_GPIO_BIDIRECTIONAL_PORT_ENABLE_SS1 0x02
153 #define I2CTOSPI_CONFIGURE_GPIO_BIDIRECTIONAL_PORT_ENABLE_SS2 0x04
154 #define I2CTOSPI_CONFIGURE_GPIO_BIDIRECTIONAL_PORT_ENABLE_SS3 0x08
155 
161 #define I2CTOSPI_CONFIGURE_GPIO_QUASI_BIDIRECTIONAL_SS0 0x00
162 #define I2CTOSPI_CONFIGURE_GPIO_PUSH_PULL_SS0 0x01
163 #define I2CTOSPI_CONFIGURE_GPIO_INPUT_ONLY_SS0 0x02
164 #define I2CTOSPI_CONFIGURE_GPIO_OPEN_DRAIN_SS0 0x03
165 #define I2CTOSPI_CONFIGURE_GPIO_QUASI_BIDIRECTIONAL_SS1 0x00
166 #define I2CTOSPI_CONFIGURE_GPIO_PUSH_PULL_SS1 0x04
167 #define I2CTOSPI_CONFIGURE_GPIO_INPUT_ONLY_SS1 0x08
168 #define I2CTOSPI_CONFIGURE_GPIO_OPEN_DRAIN_SS1 0x0C
169 #define I2CTOSPI_CONFIGURE_GPIO_QUASI_BIDIRECTIONAL_SS2 0x00
170 #define I2CTOSPI_CONFIGURE_GPIO_PUSH_PULL_SS2 0x10
171 #define I2CTOSPI_CONFIGURE_GPIO_INPUT_ONLY_SS2 0x20
172 #define I2CTOSPI_CONFIGURE_GPIO_OPEN_DRAIN_SS2 0x30
173 #define I2CTOSPI_CONFIGURE_GPIO_QUASI_BIDIRECTIONAL_SS3 0x00
174 #define I2CTOSPI_CONFIGURE_GPIO_PUSH_PULL_SS3 0x40
175 #define I2CTOSPI_CONFIGURE_GPIO_INPUT_ONLY_SS3 0x80
176 #define I2CTOSPI_CONFIGURE_GPIO_OPEN_DRAIN_SS3 0xC0
177 
178 #define I2CTOSPI_SLAVEDEVICE_SS0 0x01
179 #define I2CTOSPI_SLAVEDEVICE_SS1 0x02
180 #define I2CTOSPI_SLAVEDEVICE_SS2 0x04
181 #define I2CTOSPI_SLAVEDEVICE_SS3 0x08
182 
183 #define I2CTOSPI_RTC5_COMMAND_WRITE 0x12
184 #define I2CTOSPI_RTC5_COMMAND_READ 0x13
185 
186 #define I2CTOSPI_RTC5_REG_TIME_HUN_OF_SEC 0x00
187 #define I2CTOSPI_RTC5_REG_TIME_SEC 0x01
188 #define I2CTOSPI_RTC5_REG_TIME_MIN 0x02
189 #define I2CTOSPI_RTC5_REG_TIME_HOUR 0x03
190 #define I2CTOSPI_RTC5_REG_TIME_DAY_OF_THE_WEEK 0x04
191 #define I2CTOSPI_RTC5_REG_TIME_DATE_DAY 0x05
192 #define I2CTOSPI_RTC5_REG_TIME_DATE_MONTH 0x06
193 #define I2CTOSPI_RTC5_REG_TIME_DATE_YEAR 0x07
194 #define I2CTOSPI_RTC5_REG_CONFIG 0x08
195 #define I2CTOSPI_RTC5_REG_CALIBRATION 0x09
196 
197 #define I2CTOSPI_RTC5_COMMAND_CLEAR 0x54
198 #define I2CTOSPI_RTC5_COMMAND_ENABLE_COUNTING 0x80
199  // End group macro
201 // --------------------------------------------------------------- PUBLIC TYPES
210 typedef struct
211 {
212  // Output pins
213 
214  digital_out_t rst;
215  digital_out_t cs;
216 
217  // Input pins
218 
219  digital_in_t int_pin;
220 
221  // Modules
222 
223  i2c_master_t i2c;
224 
225  // ctx variable
226 
227  uint8_t slave_address;
228 
229 } i2ctospi_t;
230 
234 typedef struct
235 {
236  // Communication gpio pins
237 
238  pin_name_t scl;
239  pin_name_t sda;
240 
241  // Additional gpio pins
242 
243  pin_name_t rst;
244  pin_name_t cs;
245  pin_name_t int_pin;
246 
247  // static variable
248 
249  uint32_t i2c_speed;
250  uint8_t i2c_address;
251 
253 
257 typedef struct
258 {
259  // Variable
260  uint8_t slave_device;
261  uint8_t function_id;
262  uint8_t reg_addr;
263  uint8_t n_bytes;
264 
266 
270 typedef struct
271 {
272  // Variable
273  uint8_t ss3_val;
274  uint8_t ss2_val;
275  uint8_t ss1_val;
276  uint8_t ss0_val;
277 
279  // End types group
281 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
282 
288 #ifdef __cplusplus
289 extern "C"{
290 #endif
291 
301 
311 
320 
331 void i2ctospi_generic_write ( i2ctospi_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
332 
343 void i2ctospi_generic_read ( i2ctospi_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
344 
357 
368 
385 void i2ctospi_spi_write_byte ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t write_data );
386 
405 
423 void i2ctospi_spi_burst_write ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t *write_data );
424 
443 void i2ctospi_spi_burst_read ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t *read_data );
444 
461 void i2ctospi_read_buffer_bytes ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t *read_data );
462 
478 void i2ctospi_write_command ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t command_data );
479 
495 void i2ctospi_write_enable ( i2ctospi_t *ctx, uint8_t ss_pin_data );
496 
525 void i2ctospi_config_spi ( i2ctospi_t *ctx, uint8_t config_data );
526 
541 
552 
572 void i2ctospi_set_write_state_of_gpio ( i2ctospi_t *ctx, uint8_t gpio_state );
573 
591 void i2ctospi_set_read_state_of_gpio ( i2ctospi_t *ctx, uint8_t gpio_state );
592 
610 void i2ctospi_enable_gpio ( i2ctospi_t *ctx, uint8_t ss_pin_number );
611 
647 
648 #ifdef __cplusplus
649 }
650 #endif
651 #endif // _I2CTOSPI_H_
652  // End public_function group
655 
656 // ------------------------------------------------------------------------- END
i2ctospi_gpio_t::ss3_val
uint8_t ss3_val
Definition: i2ctospi.h:273
i2ctospi_write_command
void i2ctospi_write_command(i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t command_data)
Generic write command byte of data function.
i2ctospi_spi_t::n_bytes
uint8_t n_bytes
Definition: i2ctospi.h:263
i2ctospi_clear_interrupt
void i2ctospi_clear_interrupt(i2ctospi_t *ctx)
Clear interrupt function.
i2ctospi_get_interrupt
uint8_t i2ctospi_get_interrupt(i2ctospi_t *ctx)
Gets state of the interrupt function.
i2ctospi_t::int_pin
digital_in_t int_pin
Definition: i2ctospi.h:219
i2ctospi_cfg_t::cs
pin_name_t cs
Definition: i2ctospi.h:244
i2ctospi_t::slave_address
uint8_t slave_address
Definition: i2ctospi.h:227
i2ctospi_cfg_t::scl
pin_name_t scl
Definition: i2ctospi.h:238
i2ctospi_t::rst
digital_out_t rst
Definition: i2ctospi.h:214
i2ctospi_gpio_t
Click gpio object definition.
Definition: i2ctospi.h:271
i2ctospi_types_gpio
void i2ctospi_types_gpio(i2ctospi_t *ctx, i2ctospi_gpio_t *gpio)
Enable the pins defined as GPIO function.
i2ctospi_t::cs
digital_out_t cs
Definition: i2ctospi.h:215
i2ctospi_set_idle_mode
void i2ctospi_set_idle_mode(i2ctospi_t *ctx)
Set Idle mode function.
i2ctospi_spi_t::slave_device
uint8_t slave_device
Definition: i2ctospi.h:260
i2ctospi_gpio_t::ss1_val
uint8_t ss1_val
Definition: i2ctospi.h:275
i2ctospi_write_enable
void i2ctospi_write_enable(i2ctospi_t *ctx, uint8_t ss_pin_data)
SPI EEPROM wite enable function.
i2ctospi_spi_t::reg_addr
uint8_t reg_addr
Definition: i2ctospi.h:262
i2ctospi_default_cfg
void i2ctospi_default_cfg(i2ctospi_t *ctx)
Click Default Configuration function.
i2ctospi_hw_reset
void i2ctospi_hw_reset(i2ctospi_t *ctx)
Hardware reset function.
I2CTOSPI_RETVAL
#define I2CTOSPI_RETVAL
Definition: i2ctospi.h:78
i2ctospi_cfg_t
Click configuration structure definition.
Definition: i2ctospi.h:235
i2ctospi_gpio_t::ss2_val
uint8_t ss2_val
Definition: i2ctospi.h:274
i2ctospi_generic_read
void i2ctospi_generic_read(i2ctospi_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
i2ctospi_t::i2c
i2c_master_t i2c
Definition: i2ctospi.h:223
i2ctospi_init
I2CTOSPI_RETVAL i2ctospi_init(i2ctospi_t *ctx, i2ctospi_cfg_t *cfg)
Initialization function.
i2ctospi_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: i2ctospi.h:249
i2ctospi_spi_burst_read
void i2ctospi_spi_burst_read(i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t *read_data)
Generic SPI read - single address burst read function.
i2ctospi_read_buffer_bytes
void i2ctospi_read_buffer_bytes(i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t *read_data)
Generic sequential read from the data buffer function.
i2ctospi_set_read_state_of_gpio
void i2ctospi_set_read_state_of_gpio(i2ctospi_t *ctx, uint8_t gpio_state)
Set state of the pins defined as GPIO read function.
i2ctospi_cfg_t::sda
pin_name_t sda
Definition: i2ctospi.h:239
i2ctospi_generic_write
void i2ctospi_generic_write(i2ctospi_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
i2ctospi_cfg_t::int_pin
pin_name_t int_pin
Definition: i2ctospi.h:245
i2ctospi_spi_read_byte
uint8_t i2ctospi_spi_read_byte(i2ctospi_t *ctx, i2ctospi_spi_t *spi)
Generic SPI read the byte of data from data buffer function.
i2ctospi_t
Click ctx object definition.
Definition: i2ctospi.h:211
i2ctospi_spi_t::function_id
uint8_t function_id
Definition: i2ctospi.h:261
i2ctospi_gpio_t::ss0_val
uint8_t ss0_val
Definition: i2ctospi.h:276
i2ctospi_config_spi
void i2ctospi_config_spi(i2ctospi_t *ctx, uint8_t config_data)
Configure SPI Interface function.
i2ctospi_spi_write_byte
void i2ctospi_spi_write_byte(i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t write_data)
Generic SPI write the byte of data to data buffer function.
i2ctospi_spi_burst_write
void i2ctospi_spi_burst_write(i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t *write_data)
Generic SPI write - single address burst write function.
i2ctospi_set_write_state_of_gpio
void i2ctospi_set_write_state_of_gpio(i2ctospi_t *ctx, uint8_t gpio_state)
Set state of the pins defined as GPIO write function.
i2ctospi_cfg_t::rst
pin_name_t rst
Definition: i2ctospi.h:243
i2ctospi_cfg_setup
void i2ctospi_cfg_setup(i2ctospi_cfg_t *cfg)
Config Object Initialization function.
i2ctospi_enable_gpio
void i2ctospi_enable_gpio(i2ctospi_t *ctx, uint8_t ss_pin_number)
Enable the pins defined as GPIO function.
i2ctospi_cfg_t::i2c_address
uint8_t i2c_address
Definition: i2ctospi.h:250
i2ctospi_spi_t
Click spi object definition.
Definition: i2ctospi.h:258