uarti2cspi  2.0.0.0
uarti2cspi.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 UARTI2CSPI_H
36 #define UARTI2CSPI_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 UARTI2CSPI_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.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
76  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
77 
83 #define UARTI2CSPI_MASTER_I2C 0
84 #define UARTI2CSPI_MASTER_SPI 1
85 
91 #define UARTI2CSPI_RETVAL uint8_t
92 
93 #define UARTI2CSPI_OK 0x00
94 #define UARTI2CSPI_INIT_ERROR 0xFF
95 
101 #define UARTI2CSPI_OSCILATOR_FREQ 1843200.0
102 #define UARTI2CSPI_PRESCALER_DEF 1.0
103 #define UARTI2CSPI_ADDR 0x48
104 #define UARTI2CSPI_RHR 0x00 << 3
105 #define UARTI2CSPI_THR 0x00 << 3
106 #define UARTI2CSPI_IER 0x01 << 3
107 #define UARTI2CSPI_FCR 0x02 << 3
108 #define UARTI2CSPI_IIR 0x02 << 3
109 #define UARTI2CSPI_LCR 0x03 << 3
110 #define UARTI2CSPI_MCR 0x04 << 3
111 #define UARTI2CSPI_LSR 0x05 << 3
112 #define UARTI2CSPI_MSR 0x06 << 3
113 #define UARTI2CSPI_SPR 0x07 << 3
114 #define UARTI2CSPI_TCR 0x06 << 3
115 #define UARTI2CSPI_TLR 0x07 << 3
116 #define UARTI2CSPI_TXLVL 0x08 << 3
117 #define UARTI2CSPI_RXLVL 0x09 << 3
118 #define UARTI2CSPI_EFCR 0x0F << 3
119 
125 #define UARTI2CSPI_DLL 0x00 << 3
126 #define UARTI2CSPI_DLH 0x01 << 3
127 
128 #define UARTI2CSPI_EFR 0x02 << 3
129 #define UARTI2CSPI_XON1 0x04 << 3
130 #define UARTI2CSPI_XON2 0x05 << 3
131 #define UARTI2CSPI_XOFF1 0x06 << 3
132 #define UARTI2CSPI_XOFF2 0x07 << 3
133 
139 #define UARTI2CSPI_UART_5_BIT_DATA 0x00
140 #define UARTI2CSPI_UART_6_BIT_DATA 0x01
141 #define UARTI2CSPI_UART_7_BIT_DATA 0x02
142 #define UARTI2CSPI_UART_8_BIT_DATA 0x03
143 #define UARTI2CSPI_UART_NOPARITY 0x00
144 #define UARTI2CSPI_UART_EVENPARITY 0x18
145 #define UARTI2CSPI_UART_ODDPARITY 0x08
146 #define UARTI2CSPI_UART_PARITY_ONE 0x38
147 #define UARTI2CSPI_UART_PARITY_ZERO 0x28
148 #define UARTI2CSPI_UART_ONE_STOPBIT 0x00
149 #define UARTI2CSPI_UART_TWO_STOPBITS 0x04
150 
156 #define UARTI2CSPI_CTS_INT_EN 0x80
157 #define UARTI2CSPI_RTS_INT_EN 0x40
158 #define UARTI2CSPI_XOFF_INT_EN 0x20
159 #define UARTI2CSPI_SLEEP_INT_EN 0x10
160 #define UARTI2CSPI_MODEM_STATUS_INT_EN 0x08
161 #define UARTI2CSPI_RECEIVE_LINE_STATUS_INT_EN 0x04
162 #define UARTI2CSPI_THR_EMPTY_INT_EN 0x02
163 #define UARTI2CSPI_RXD_INT_EN 0x01
164 
170 #define UARTI2CSPI_DATA_READY 0x01
171 #define UARTI2CSPI_DATA_NOT_READY 0x00
172  // End group macro
174 // --------------------------------------------------------------- PUBLIC TYPES
183 typedef uint8_t uarti2cspi_select_t;
184 
188 struct uarti2cspi_s;
189 typedef void ( *uarti2cspi_master_write_t )( struct uarti2cspi_s*, uint8_t, uint8_t );
190 typedef void ( *uarti2cspi_master_read_t )( struct uarti2cspi_s*, uint8_t, uint8_t* );
191 
195 typedef struct uarti2cspi_s
196 {
197  // Output pins
198 
199  digital_out_t cs;
200  digital_out_t rst;
201 
202  // Input pins
203 
204  digital_in_t int_pin;
205 
206  // Modules
207 
208  i2c_master_t i2c;
209  spi_master_t spi;
210 
211  // ctx variable
212 
213  uint8_t slave_address;
214  pin_name_t chip_select;
218 
220 
224 typedef struct
225 {
226  // Communication gpio pins
227 
228  pin_name_t scl;
229  pin_name_t sda;
230  pin_name_t miso;
231  pin_name_t mosi;
232  pin_name_t sck;
233  pin_name_t cs;
234 
235  // Additional gpio pins
236 
237  pin_name_t rst;
238  pin_name_t int_pin;
239 
240  // static variable
241 
242  uint32_t i2c_speed;
243  uint8_t i2c_address;
244 
245  uint32_t spi_speed;
246  spi_master_mode_t spi_mode;
247  spi_master_chip_select_polarity_t cs_polarity;
248 
250 
252  // End types group
254 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
260 #ifdef __cplusplus
261 extern "C"{
262 #endif
263 
273 
283 
293 void uarti2cspi_generic_write ( uarti2cspi_t *ctx, uint8_t reg, uint8_t data_buf );
294 
304 void uarti2cspi_generic_read ( uarti2cspi_t *ctx, uint8_t reg, uint8_t *data_buf );
305 
314 void uarti2cspi_set_reset ( uarti2cspi_t *ctx, uint8_t state );
315 
327 void uarti2cspi_advanced_init ( uarti2cspi_t *ctx, uint32_t baud_rate, uint8_t data_bits,
328  uint8_t parity_mode, uint8_t stop_bits );
329 
339 void uarti2cspi_interrupt_enable ( uarti2cspi_t *ctx, uint8_t vector );
340 
349 void uarti2cspi_uart_write_text ( uarti2cspi_t *ctx, uint8_t *w_text );
350 
359 void uarti2cspi_uart_write ( uarti2cspi_t *ctx, uint8_t w_byte );
360 
372 
383 
384 #ifdef __cplusplus
385 }
386 #endif
387 #endif // _UARTI2CSPI_H_
388  // End public_function group
391 
392 // ------------------------------------------------------------------------- END
uarti2cspi_cfg_t::sck
pin_name_t sck
Definition: uarti2cspi.h:232
uarti2cspi_master_write_t
void(* uarti2cspi_master_write_t)(struct uarti2cspi_s *, uint8_t, uint8_t)
Definition: uarti2cspi.h:189
uarti2cspi_cfg_t::sel
uarti2cspi_select_t sel
Definition: uarti2cspi.h:249
uarti2cspi_s::slave_address
uint8_t slave_address
Definition: uarti2cspi.h:213
uarti2cspi_cfg_t
Click configuration structure definition.
Definition: uarti2cspi.h:225
uarti2cspi_init
UARTI2CSPI_RETVAL uarti2cspi_init(uarti2cspi_t *ctx, uarti2cspi_cfg_t *cfg)
Initialization function.
UARTI2CSPI_RETVAL
#define UARTI2CSPI_RETVAL
Definition: uarti2cspi.h:91
uarti2cspi_generic_write
void uarti2cspi_generic_write(uarti2cspi_t *ctx, uint8_t reg, uint8_t data_buf)
Generic write function.
uarti2cspi_cfg_t::int_pin
pin_name_t int_pin
Definition: uarti2cspi.h:238
uarti2cspi_cfg_t::cs
pin_name_t cs
Definition: uarti2cspi.h:233
uarti2cspi_interrupt_enable
void uarti2cspi_interrupt_enable(uarti2cspi_t *ctx, uint8_t vector)
Interrupt enable function.
uarti2cspi_cfg_t::i2c_address
uint8_t i2c_address
Definition: uarti2cspi.h:243
uarti2cspi_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: uarti2cspi.h:242
uarti2cspi_s::spi
spi_master_t spi
Definition: uarti2cspi.h:209
uarti2cspi_generic_read
void uarti2cspi_generic_read(uarti2cspi_t *ctx, uint8_t reg, uint8_t *data_buf)
Generic read function.
uarti2cspi_select_t
uint8_t uarti2cspi_select_t
Communication type.
Definition: uarti2cspi.h:183
uarti2cspi_uart_write
void uarti2cspi_uart_write(uarti2cspi_t *ctx, uint8_t w_byte)
Uart write function.
uarti2cspi_s::read_f
uarti2cspi_master_read_t read_f
Definition: uarti2cspi.h:216
uarti2cspi_uart_read
uint8_t uarti2cspi_uart_read(uarti2cspi_t *ctx)
Uart read function.
uarti2cspi_cfg_t::rst
pin_name_t rst
Definition: uarti2cspi.h:237
uarti2cspi_s
Click ctx object definition.
Definition: uarti2cspi.h:196
uarti2cspi_cfg_t::mosi
pin_name_t mosi
Definition: uarti2cspi.h:231
uarti2cspi_advanced_init
void uarti2cspi_advanced_init(uarti2cspi_t *ctx, uint32_t baud_rate, uint8_t data_bits, uint8_t parity_mode, uint8_t stop_bits)
Advanced initialization function.
uarti2cspi_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: uarti2cspi.h:247
uarti2cspi_set_reset
void uarti2cspi_set_reset(uarti2cspi_t *ctx, uint8_t state)
Set RST pin state function.
uarti2cspi_uart_data_ready
uint8_t uarti2cspi_uart_data_ready(uarti2cspi_t *ctx)
Uart data ready function.
uarti2cspi_cfg_t::spi_speed
uint32_t spi_speed
Definition: uarti2cspi.h:245
uarti2cspi_t
struct uarti2cspi_s uarti2cspi_t
Click ctx object definition.
uarti2cspi_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: uarti2cspi.h:246
uarti2cspi_s::int_pin
digital_in_t int_pin
Definition: uarti2cspi.h:204
uarti2cspi_s::chip_select
pin_name_t chip_select
Definition: uarti2cspi.h:214
uarti2cspi_s::cs
digital_out_t cs
Definition: uarti2cspi.h:199
uarti2cspi_cfg_t::scl
pin_name_t scl
Definition: uarti2cspi.h:228
uarti2cspi_cfg_t::miso
pin_name_t miso
Definition: uarti2cspi.h:230
uarti2cspi_cfg_t::sda
pin_name_t sda
Definition: uarti2cspi.h:229
uarti2cspi_s::i2c
i2c_master_t i2c
Definition: uarti2cspi.h:208
uarti2cspi_s::master_sel
uarti2cspi_select_t master_sel
Definition: uarti2cspi.h:217
uarti2cspi_s::write_f
uarti2cspi_master_write_t write_f
Definition: uarti2cspi.h:215
uarti2cspi_uart_write_text
void uarti2cspi_uart_write_text(uarti2cspi_t *ctx, uint8_t *w_text)
Uart write text function.
uarti2cspi_cfg_setup
void uarti2cspi_cfg_setup(uarti2cspi_cfg_t *cfg)
Config Object Initialization function.
uarti2cspi_master_read_t
void(* uarti2cspi_master_read_t)(struct uarti2cspi_s *, uint8_t, uint8_t *)
Definition: uarti2cspi.h:190
uarti2cspi_s::rst
digital_out_t rst
Definition: uarti2cspi.h:200