mbusrf2  2.1.0.0
mbusrf2.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 MBUSRF2_H
29 #define MBUSRF2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_uart.h"
52 
73 #define MBUSRF2_CMD_SFD 0xFF
74 #define MBUSRF2_CMD_DATA_REQ 0x00
75 #define MBUSRF2_CMD_DATA_IND 0x03
76 #define MBUSRF2_CMD_SET_MODE_REQ 0x04
77 #define MBUSRF2_CMD_RESET_REQ 0x05
78 #define MBUSRF2_CMD_SET_REQ 0x09
79 #define MBUSRF2_CMD_SETUARTSPEED_REQ 0x10
80 #define MBUSRF2_CMD_FACTORYRESET_REQ 0x11
81 #define MBUSRF2_CMD_GET_REQ 0x0A
82 #define MBUSRF2_CMD_SERIALNO_REQ 0x0B
83 #define MBUSRF2_CMD_FWV_REQ 0x0C
84 #define MBUSRF2_CMD_RSSI_REQ 0x0D
85 #define MBUSRF2_CMD_SET_AES_KEY_REQ 0x50
86 #define MBUSRF2_CMD_CLR_AES_KEY_REQ 0x51
87 #define MBUSRF2_CMD_GET_AES_KEY_REQ 0x52
88 #define MBUSRF2_CMD_RESPONSE 0x80
89 
95 #define MBUSRF2_TX_DRV_BUFFER_SIZE 100
96 #define MBUSRF2_RX_DRV_BUFFER_SIZE 300
97  // mbusrf2_cmd
99 
114 #define MBUSRF2_MAP_MIKROBUS( cfg, mikrobus ) \
115  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
116  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
117  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
118  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_CS ); \
119  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
120  // mbusrf2_map // mbusrf2
123 
128 typedef struct
129 {
130  // Output pins
131  digital_out_t rst;
132  digital_out_t cts;
134  // Input pins
135  digital_in_t rts;
137  // Modules
138  uart_t uart;
140  // Buffers
141  uint8_t uart_rx_buffer[ MBUSRF2_RX_DRV_BUFFER_SIZE ];
142  uint8_t uart_tx_buffer[ MBUSRF2_TX_DRV_BUFFER_SIZE ];
144 } mbusrf2_t;
145 
150 typedef struct
151 {
152  // Communication gpio pins
153  pin_name_t rx_pin;
154  pin_name_t tx_pin;
156  // Additional gpio pins
157  pin_name_t rst;
158  pin_name_t cts;
159  pin_name_t rts;
161  // Static variable
162  uint32_t baud_rate;
164  uart_data_bits_t data_bit;
165  uart_parity_t parity_bit;
166  uart_stop_bits_t stop_bit;
168 } mbusrf2_cfg_t;
169 
174 typedef enum
175 {
179 
181 
198 
212 err_t mbusrf2_init ( mbusrf2_t *ctx, mbusrf2_cfg_t *cfg );
213 
226 err_t mbusrf2_generic_write ( mbusrf2_t *ctx, uint8_t *data_in, uint16_t len );
227 
240 err_t mbusrf2_generic_read ( mbusrf2_t *ctx, uint8_t *data_out, uint16_t len );
241 
251 void mbusrf2_set_rst_pin ( mbusrf2_t *ctx, uint8_t pin_state );
252 
266 err_t mbusrf2_send_command ( mbusrf2_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t len );
267 
280 err_t mbusrf2_send_data ( mbusrf2_t *ctx, uint8_t *data_in, uint8_t len );
281 
282 #ifdef __cplusplus
283 }
284 #endif
285 #endif // MBUSRF2_H
286  // mbusrf2
288 
289 // ------------------------------------------------------------------------ END
mbusrf2_send_data
err_t mbusrf2_send_data(mbusrf2_t *ctx, uint8_t *data_in, uint8_t len)
M-BUS RF 2 send data function.
mbusrf2_generic_read
err_t mbusrf2_generic_read(mbusrf2_t *ctx, uint8_t *data_out, uint16_t len)
M-BUS RF 2 data reading function.
mbusrf2_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: mbusrf2.h:164
mbusrf2_send_command
err_t mbusrf2_send_command(mbusrf2_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t len)
M-BUS RF 2 send command function.
MBUSRF2_ERROR_TIMEOUT
@ MBUSRF2_ERROR_TIMEOUT
Definition: mbusrf2.h:178
mbusrf2_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: mbusrf2.h:165
mbusrf2_cfg_t::rst
pin_name_t rst
Definition: mbusrf2.h:157
mbusrf2_cfg_t::baud_rate
uint32_t baud_rate
Definition: mbusrf2.h:162
mbusrf2_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: mbusrf2.h:166
mbusrf2_cfg_t::cts
pin_name_t cts
Definition: mbusrf2.h:158
MBUSRF2_TX_DRV_BUFFER_SIZE
#define MBUSRF2_TX_DRV_BUFFER_SIZE
M-BUS RF 2 driver buffer size.
Definition: mbusrf2.h:95
mbusrf2_cfg_t::tx_pin
pin_name_t tx_pin
Definition: mbusrf2.h:154
mbusrf2_cfg_t::uart_blocking
bool uart_blocking
Definition: mbusrf2.h:163
MBUSRF2_OK
@ MBUSRF2_OK
Definition: mbusrf2.h:176
mbusrf2_cfg_setup
void mbusrf2_cfg_setup(mbusrf2_cfg_t *cfg)
M-BUS RF 2 configuration object setup function.
mbusrf2_t
M-BUS RF 2 Click context object.
Definition: mbusrf2.h:129
mbusrf2_return_value_t
mbusrf2_return_value_t
M-BUS RF 2 Click return value data.
Definition: mbusrf2.h:175
MBUSRF2_RX_DRV_BUFFER_SIZE
#define MBUSRF2_RX_DRV_BUFFER_SIZE
Definition: mbusrf2.h:96
MBUSRF2_ERROR
@ MBUSRF2_ERROR
Definition: mbusrf2.h:177
mbusrf2_generic_write
err_t mbusrf2_generic_write(mbusrf2_t *ctx, uint8_t *data_in, uint16_t len)
M-BUS RF 2 data writing function.
mbusrf2_t::rts
digital_in_t rts
Definition: mbusrf2.h:135
mbusrf2_t::cts
digital_out_t cts
Definition: mbusrf2.h:132
mbusrf2_t::rst
digital_out_t rst
Definition: mbusrf2.h:131
mbusrf2_cfg_t
M-BUS RF 2 Click configuration object.
Definition: mbusrf2.h:151
mbusrf2_cfg_t::rts
pin_name_t rts
Definition: mbusrf2.h:159
mbusrf2_t::uart
uart_t uart
Definition: mbusrf2.h:138
mbusrf2_set_rst_pin
void mbusrf2_set_rst_pin(mbusrf2_t *ctx, uint8_t pin_state)
M-BUS RF 2 set reset pin function.
mbusrf2_init
err_t mbusrf2_init(mbusrf2_t *ctx, mbusrf2_cfg_t *cfg)
M-BUS RF 2 initialization function.
mbusrf2_cfg_t::rx_pin
pin_name_t rx_pin
Definition: mbusrf2.h:153