mbusrf3  2.1.0.0
mbusrf3.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 MBUSRF3_H
29 #define MBUSRF3_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 MBUSRF3_CMD_SFD 0xFF
74 #define MBUSRF3_CMD_DATA_REQ 0x00
75 #define MBUSRF3_CMD_DATA_IND 0x03
76 #define MBUSRF3_CMD_SET_MODE_REQ 0x04
77 #define MBUSRF3_CMD_RESET_REQ 0x05
78 #define MBUSRF3_CMD_SET_REQ 0x09
79 #define MBUSRF3_CMD_SETUARTSPEED_REQ 0x10
80 #define MBUSRF3_CMD_FACTORYRESET_REQ 0x11
81 #define MBUSRF3_CMD_GET_REQ 0x0A
82 #define MBUSRF3_CMD_SERIALNO_REQ 0x0B
83 #define MBUSRF3_CMD_FWV_REQ 0x0C
84 #define MBUSRF3_CMD_RSSI_REQ 0x0D
85 #define MBUSRF3_CMD_SET_AES_KEY_REQ 0x50
86 #define MBUSRF3_CMD_CLR_AES_KEY_REQ 0x51
87 #define MBUSRF3_CMD_GET_AES_KEY_REQ 0x52
88 #define MBUSRF3_CMD_RESPONSE 0x80
89 
95 #define MBUSRF3_TX_DRV_BUFFER_SIZE 100
96 #define MBUSRF3_RX_DRV_BUFFER_SIZE 300
97  // mbusrf3_cmd
99 
114 #define MBUSRF3_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.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
119  // mbusrf3_map // mbusrf3
122 
127 typedef struct
128 {
129  // Output pins
130  digital_out_t rst;
132  // Input pins
133  digital_in_t rts;
135  // Modules
136  uart_t uart;
138  // Buffers
139  uint8_t uart_rx_buffer[ MBUSRF3_RX_DRV_BUFFER_SIZE ];
140  uint8_t uart_tx_buffer[ MBUSRF3_TX_DRV_BUFFER_SIZE ];
142 } mbusrf3_t;
143 
148 typedef struct
149 {
150  // Communication gpio pins
151  pin_name_t rx_pin;
152  pin_name_t tx_pin;
154  // Additional gpio pins
155  pin_name_t rst;
156  pin_name_t rts;
158  // Static variable
159  uint32_t baud_rate;
161  uart_data_bits_t data_bit;
162  uart_parity_t parity_bit;
163  uart_stop_bits_t stop_bit;
165 } mbusrf3_cfg_t;
166 
171 typedef enum
172 {
176 
178 
195 
209 err_t mbusrf3_init ( mbusrf3_t *ctx, mbusrf3_cfg_t *cfg );
210 
223 err_t mbusrf3_generic_write ( mbusrf3_t *ctx, uint8_t *data_in, uint16_t len );
224 
237 err_t mbusrf3_generic_read ( mbusrf3_t *ctx, uint8_t *data_out, uint16_t len );
238 
248 void mbusrf3_set_rst_pin ( mbusrf3_t *ctx, uint8_t pin_state );
249 
263 err_t mbusrf3_send_command ( mbusrf3_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t len );
264 
277 err_t mbusrf3_send_data ( mbusrf3_t *ctx, uint8_t *data_in, uint8_t len );
278 
279 #ifdef __cplusplus
280 }
281 #endif
282 #endif // MBUSRF3_H
283  // mbusrf3
285 
286 // ------------------------------------------------------------------------ END
mbusrf3_cfg_t::uart_blocking
bool uart_blocking
Definition: mbusrf3.h:160
mbusrf3_init
err_t mbusrf3_init(mbusrf3_t *ctx, mbusrf3_cfg_t *cfg)
M-BUS RF 3 initialization function.
MBUSRF3_ERROR
@ MBUSRF3_ERROR
Definition: mbusrf3.h:174
MBUSRF3_RX_DRV_BUFFER_SIZE
#define MBUSRF3_RX_DRV_BUFFER_SIZE
Definition: mbusrf3.h:96
mbusrf3_cfg_t::rx_pin
pin_name_t rx_pin
Definition: mbusrf3.h:151
mbusrf3_cfg_t::tx_pin
pin_name_t tx_pin
Definition: mbusrf3.h:152
mbusrf3_send_command
err_t mbusrf3_send_command(mbusrf3_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t len)
M-BUS RF 3 send command function.
mbusrf3_set_rst_pin
void mbusrf3_set_rst_pin(mbusrf3_t *ctx, uint8_t pin_state)
M-BUS RF 3 set reset pin function.
mbusrf3_cfg_setup
void mbusrf3_cfg_setup(mbusrf3_cfg_t *cfg)
M-BUS RF 3 configuration object setup function.
mbusrf3_t::rts
digital_in_t rts
Definition: mbusrf3.h:133
mbusrf3_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: mbusrf3.h:161
mbusrf3_generic_write
err_t mbusrf3_generic_write(mbusrf3_t *ctx, uint8_t *data_in, uint16_t len)
M-BUS RF 3 data writing function.
mbusrf3_generic_read
err_t mbusrf3_generic_read(mbusrf3_t *ctx, uint8_t *data_out, uint16_t len)
M-BUS RF 3 data reading function.
mbusrf3_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: mbusrf3.h:163
MBUSRF3_TX_DRV_BUFFER_SIZE
#define MBUSRF3_TX_DRV_BUFFER_SIZE
M-BUS RF 3 driver buffer size.
Definition: mbusrf3.h:95
mbusrf3_t::uart
uart_t uart
Definition: mbusrf3.h:136
mbusrf3_t
M-BUS RF 3 Click context object.
Definition: mbusrf3.h:128
MBUSRF3_ERROR_TIMEOUT
@ MBUSRF3_ERROR_TIMEOUT
Definition: mbusrf3.h:175
mbusrf3_t::rst
digital_out_t rst
Definition: mbusrf3.h:130
mbusrf3_cfg_t::rts
pin_name_t rts
Definition: mbusrf3.h:156
mbusrf3_cfg_t
M-BUS RF 3 Click configuration object.
Definition: mbusrf3.h:149
mbusrf3_send_data
err_t mbusrf3_send_data(mbusrf3_t *ctx, uint8_t *data_in, uint8_t len)
M-BUS RF 3 send data function.
mbusrf3_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: mbusrf3.h:162
mbusrf3_cfg_t::baud_rate
uint32_t baud_rate
Definition: mbusrf3.h:159
mbusrf3_cfg_t::rst
pin_name_t rst
Definition: mbusrf3.h:155
mbusrf3_return_value_t
mbusrf3_return_value_t
M-BUS RF 3 Click return value data.
Definition: mbusrf3.h:172
MBUSRF3_OK
@ MBUSRF3_OK
Definition: mbusrf3.h:173