rn4870  2.0.0.0
rn4870.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 RN4870_H
36 #define RN4870_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_uart.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
66 #define RN4870_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
68  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
69  cfg.rst_n = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.uart_rts = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.uart_cts = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define RN4870_RETVAL uint8_t
79 
80 #define RN4870_OK 0x00
81 #define RN4870_INIT_ERROR 0xFF
82 
88 #define RN4870_DEVICETYPE_MASTER 0x02
89 #define RN4870_DEVICETYPE_SLAVE 0x03
90 
92 #define RN4870_ID_MASTER 0x35
93 #define RN4870_ID_SLAVE 0x32
94 #define RN4870_MTYPE_MSG 'M'
95 #define RN4870_DTYPE_STRING 0x5331
96 
97 
102 #define DRV_RX_BUFFER_SIZE 500
103  // End group macro
106 // --------------------------------------------------------------- PUBLIC TYPES
114 typedef struct
115 {
116  // Output pins
117 
118  digital_out_t rst_n;
119  digital_out_t uart_rts;
120 
121  // Input pins
122 
123  digital_in_t uart_cts;
124 
125  // Modules
126 
127  uart_t uart;
128 
129  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
130  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
131 
132  uint8_t end_flag;
133  uint8_t sentence_flag;
134 
135  char device_buffer[ 255 ];
137 
138 } rn4870_t;
139 
143 typedef struct
144 {
145  // Communication gpio pins
146 
147  pin_name_t rx_pin;
148  pin_name_t tx_pin;
149 
150  // Additional gpio pins
151 
152  pin_name_t rst_n;
153  pin_name_t uart_rts;
154  pin_name_t uart_cts;
155 
156  // static variable
157 
158  uint32_t baud_rate; // Clock speed.
160  uart_data_bits_t data_bit; // Data bits.
161  uart_parity_t parity_bit; // Parity bit.
162  uart_stop_bits_t stop_bit; // Stop bits.
163 
164 } rn4870_cfg_t;
165  // End types group
167 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
168 
174 #ifdef __cplusplus
175 extern "C"{
176 #endif
177 
187 
197 
209 void rn4870_reset ( rn4870_t *ctx );
210 
220 void rn4870_generic_write ( rn4870_t *ctx, char *data_buf, uint16_t len );
221 
233 int32_t rn4870_generic_read ( rn4870_t *ctx, char *data_buf, uint16_t max_len );
234 
244 uint8_t rn4870_int_get ( rn4870_t *ctx );
245 
254 void rn4870_rst_set ( rn4870_t *ctx, uint8_t pin_state );
255 
264 void rn4870_cs_set ( rn4870_t *ctx, uint8_t pin_state );
265 
274 void rn4870_uart_write ( rn4870_t *ctx, uint8_t * wr_buf );
275 
287 void rn4870_initialize ( rn4870_t *ctx, char *p_addr );
288 
298 void rn4870_connect ( rn4870_t *ctx, char *p_addr );
299 
311 void rn4870_send( rn4870_t *ctx, uint8_t msg_type, uint16_t data_type, uint8_t id, uint8_t *payload );
312 
322 
334 void rn4870_receive ( rn4870_t *ctx, char tmp );
335 
348 uint8_t rn4870_read ( rn4870_t *ctx, uint8_t *process_buffer );
349 
350 #ifdef __cplusplus
351 }
352 #endif
353 #endif // _RN4870_H_
354  // End public_function group
357 
358 // ------------------------------------------------------------------------- END
rn4870_cfg_t::uart_cts
pin_name_t uart_cts
Definition: rn4870.h:154
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: rn4870.h:102
rn4870_generic_write
void rn4870_generic_write(rn4870_t *ctx, char *data_buf, uint16_t len)
Generic write function.
rn4870_initialize
void rn4870_initialize(rn4870_t *ctx, char *p_addr)
Initialization RN4870 module.
rn4870_int_get
uint8_t rn4870_int_get(rn4870_t *ctx)
Get interrupt state function.
rn4870_t::rst_n
digital_out_t rst_n
Definition: rn4870.h:118
rn4870_init
RN4870_RETVAL rn4870_init(rn4870_t *ctx, rn4870_cfg_t *cfg)
Initialization function.
rn4870_connect
void rn4870_connect(rn4870_t *ctx, char *p_addr)
Connecting to slave device.
rn4870_t::uart_cts
digital_in_t uart_cts
Definition: rn4870.h:123
rn4870_cfg_t::uart_blocking
bool uart_blocking
Definition: rn4870.h:159
rn4870_t::uart
uart_t uart
Definition: rn4870.h:127
rn4870_t::uart_rts
digital_out_t uart_rts
Definition: rn4870.h:119
rn4870_cfg_t::tx_pin
pin_name_t tx_pin
Definition: rn4870.h:148
rn4870_t::end_flag
uint8_t end_flag
Definition: rn4870.h:132
rn4870_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: rn4870.h:162
RN4870_RETVAL
#define RN4870_RETVAL
Definition: rn4870.h:78
rn4870_reset
void rn4870_reset(rn4870_t *ctx)
Hardware reset function.
rn4870_send
void rn4870_send(rn4870_t *ctx, uint8_t msg_type, uint16_t data_type, uint8_t id, uint8_t *payload)
Send message function.
rn4870_cfg_t::uart_rts
pin_name_t uart_rts
Definition: rn4870.h:153
rn4870_receive
void rn4870_receive(rn4870_t *ctx, char tmp)
Receiving character function.
rn4870_t::buffer_position
uint8_t buffer_position
Definition: rn4870.h:136
rn4870_rst_set
void rn4870_rst_set(rn4870_t *ctx, uint8_t pin_state)
Set states of RST pin to desired state.
rn4870_cfg_t::rx_pin
pin_name_t rx_pin
Definition: rn4870.h:147
rn4870_cfg_t
Click configuration structure definition.
Definition: rn4870.h:144
rn4870_read
uint8_t rn4870_read(rn4870_t *ctx, uint8_t *process_buffer)
Reading received message.
rn4870_t
Click ctx object definition.
Definition: rn4870.h:115
rn4870_cs_set
void rn4870_cs_set(rn4870_t *ctx, uint8_t pin_state)
Set states of CS pin to desired state.
rn4870_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: rn4870.h:160
rn4870_disconnect
void rn4870_disconnect(rn4870_t *ctx)
Disconnecting from slave device.
rn4870_cfg_setup
void rn4870_cfg_setup(rn4870_cfg_t *cfg)
Config Object Initialization function.
rn4870_cfg_t::baud_rate
uint32_t baud_rate
Definition: rn4870.h:158
rn4870_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: rn4870.h:161
rn4870_t::sentence_flag
uint8_t sentence_flag
Definition: rn4870.h:133
rn4870_uart_write
void rn4870_uart_write(rn4870_t *ctx, uint8_t *wr_buf)
UART write function.
rn4870_cfg_t::rst_n
pin_name_t rst_n
Definition: rn4870.h:152
rn4870_generic_read
int32_t rn4870_generic_read(rn4870_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.