mbusrf4  2.0.0.0
mbusrf4.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 MBUSRF4_H
36 #define MBUSRF4_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_uart.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define MBUSRF4_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
55  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
56  cfg.ind = MIKROBUS( mikrobus, MIKROBUS_AN ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.sel = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
59  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT )
60 
66 #define MBUSRF4_RETVAL uint8_t
67 
68 #define MBUSRF4_OK 0x00
69 #define MBUSRF4_INIT_ERROR 0xFF
70 
76 #define MBUSRF4_ERROR_COMMAND_OR_ELEMENT 0xFF
77 #define MBUSRF4_ERROR_START_OR_END_COMMAND 0x8F
78 #define MBUSRF4_ERROR_NEXT_ELEMENT 0x4F
79 #define MBUSRF4_ERROR_CURRENT_ELEMENT 0x2F
80 #define MBUSRF4_NO_ERROR 0x00
81 
87 #define DRV_RX_BUFFER_SIZE 256
88 
94 #define MBUSRF4_CMD_RESET 0x30
95 #define MBUSRF4_CMD_FACTORY_RESET 0x31
96 #define MBUSRF4_CMD_SET_MODE 0x32
97 #define MBUSRF4_CMD_EEPROM_WRITE 0x33
98 #define MBUSRF4_CMD_EEPROM_READ 0x34
99 #define MBUSRF4_CMD_TX_MSG 0x35
100 #define MBUSRF4_CMD_RX_MSG_IND 0x36
101 #define MBUSRF4_CMD_GET_FW_VERSION 0x37
102 #define MBUSRF4_CMD_GET_SERIALNO 0x38
103 #define MBUSRF4_CMD_GET_RSSI 0x39
104 #define MBUSRF4_CMD_SET_C_FIELD 0x40
105 
111 #define MBUSRF4_EEADDR_WM_BUS_MODE 0x00
112 #define MBUSRF4_EEADDR_RF_CHANNEL 0x01
113 #define MBUSRF4_EEADDR_RF_POWER 0x02
114 #define MBUSRF4_EEADDR_RF_AUTOSLEEP 0x03
115 #define MBUSRF4_EEADDR_RX_WINDOW 0x04
116 
117 #define MBUSRF4_EEADDR_WMBUS_C_FIELD 0x10
118 #define MBUSRF4_EEADDR_WMBUS_MANUFACTER_ID0 0x11
119 #define MBUSRF4_EEADDR_WMBUS_MANUFACTER_ID1 0x12
120 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID0 0x13
121 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID1 0x14
122 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID2 0x15
123 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID3 0x16
124 #define MBUSRF4_EEADDR_WMBUS_VERSION 0x17
125 #define MBUSRF4_EEADDR_WMBUS_DEVICE_TYPE 0x18
126 
127 #define MBUSRF4_EEADDR_WMBUS_BLOCK1_FROM_MODULE_ENABLE 0x20
128 #define MBUSRF4_EEADDR_WMBUS_RSSI_ENABLE 0x21
129 #define MBUSRF4_EEADDR_WMBUS_INDICATE_TIMEOUT_LSB 0x22
130 #define MBUSRF4_EEADDR_WMBUS_INDICATE_TIMEOUT_MSB 0x23
131 #define MBUSRF4_EEADDR_WMBUS_UART_BAUDREATE 0x24
132 
138 #define MBUSRF4_EEPARAM_WMBUS_MODE_S2_SHORT_PREAMBLE 0x00
139 #define MBUSRF4_EEPARAM_WMBUS_MODE_S2_LONG_PREAMBLE 0x01
140 #define MBUSRF4_EEPARAM_WMBUS_MODE_S1 0x02
141 #define MBUSRF4_EEPARAM_WMBUS_MODE_S1_M 0x03
142 #define MBUSRF4_EEPARAM_WMBUS_MODE_T1_METER 0x04
143 #define MBUSRF4_EEPARAM_WMBUS_MODE_T2_METER 0x05
144 #define MBUSRF4_EEPARAM_WMBUS_MODE_T2_OTHER 0x06
145 #define MBUSRF4_EEPARAM_WMBUS_MODE_R2_METER 0x07
146 #define MBUSRF4_EEPARAM_WMBUS_MODE_R2_OTHER 0x08
147 #define MBUSRF4_EEPARAM_WMBUS_MODE_C1_METER_FRAME_A 0x09
148 #define MBUSRF4_EEPARAM_WMBUS_MODE_C1_METER_FRAME_B 0x0A
149 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_METER_FRAME_A 0x0B
150 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_METER_FRAME_B 0x0C
151 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_OTHER_FRAME_A 0x0D
152 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_OTHER_FRAME_B 0x0E
153 #define MBUSRF4_EEPARAM_WMBUS_MODE_T2_C2_OTHER 0x0F
154 
160 #define MBUSRF4_EEPARAM_RFGH_868p03MHz 0x00
161 #define MBUSRF4_EEPARAM_RFGH_868p09MHz 0x01
162 #define MBUSRF4_EEPARAM_RFGH_868p15MHz 0x02
163 #define MBUSRF4_EEPARAM_RFGH_868p21MHz 0x03
164 #define MBUSRF4_EEPARAM_RFGH_868p27MHz 0x04
165 #define MBUSRF4_EEPARAM_RFGH_868p33MHz 0x05
166 #define MBUSRF4_EEPARAM_RFGH_868p39MHz 0x06
167 #define MBUSRF4_EEPARAM_RFGH_868p45MHz 0x07
168 #define MBUSRF4_EEPARAM_RFGH_868p51MHz 0x08
169 #define MBUSRF4_EEPARAM_RFGH_868p57MHz 0x09
170 
176 #define MBUSRF4_EEPARAM_RFPWR_0dBm 0x00
177 #define MBUSRF4_EEPARAM_RFPWR_5dBm 0x01
178 #define MBUSRF4_EEPARAM_RFPWR_7dBm 0x02
179 #define MBUSRF4_EEPARAM_RFPWR_10dBm 0x03
180 #define MBUSRF4_EEPARAM_RFPWR_12dBm 0x04
181 
187 #define MBUSRF4_EEPARAM_RFAS_SLEEP_DISABLE 0x00
188 #define MBUSRF4_EEPARAM_RFAS_SLEEP_ENABLE 0x01
189 
195 #define MBUSRF4_WM_BUS_C_FIELD 0x44
196 #define MBUSRF4_HEADER 0xAA
197 
203 #define MBUSRF4_EEPARAM_BLOCK1_FROM_MODULE_ENABLE 0x01
204 #define MBUSRF4_EEPARAM_BLOCK1_FROM_MODULE_DISABLE 0x00
205 
211 #define MBUSRF4_EEPARAM_RSSI_ENABLE 0x01
212 #define MBUSRF4_EEPARAM_RSSI_DISABLE 0x00
213 
219 #define MBUSRF4_EEPARAM_UART_BAUDRATE_9600 0x00
220 #define MBUSRF4_EEPARAM_UART_BAUDRATE_19200 0x01
221 #define MBUSRF4_EEPARAM_UART_BAUDRATE_38400 0x02
222 #define MBUSRF4_EEPARAM_UART_BAUDRATE_57600 0x03
223 #define MBUSRF4_EEPARAM_UART_BAUDRATE_115200 0x04
224 
230 #define MBUSRF4_SET_VALUE_IN_RAM_MEMORY 0x00
231 #define MBUSRF4_SET_VALUE_IN_EEPROM_MEMORY 0xFF
232 
238 #define MBUSRF4_USER_APP_RX_MODE 0x00
239 #define MBUSRF4_USER_APP_TX_MODE 0x01
240 
246 #define MBUSRF4_COMMUNICATION_UART_MODE 0x01
247 #define MBUSRF4_COMMUNICATION_SPI_MODE 0x00
248  // End group macro
250 // --------------------------------------------------------------- PUBLIC TYPES
258 typedef struct
259 {
260  // Output pins
261 
262  digital_out_t cs;
263  digital_out_t sel;
264 
265  // Input pins
266 
267  digital_in_t ind;
268  digital_in_t rts;
269 
270  // Modules
271 
272  uart_t uart;
273 
274  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
275  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
276 
277 } mbusrf4_t;
278 
282 typedef struct
283 {
284  // Communication gpio pins
285 
286  pin_name_t rx_pin;
287  pin_name_t tx_pin;
288 
289  // Additional gpio pins
290 
291  pin_name_t ind;
292  pin_name_t cs;
293  pin_name_t sel;
294  pin_name_t rts;
295 
296  // static variable
297 
298  uint32_t baud_rate; // Clock speed.
300  uart_data_bits_t data_bit; // Data bits.
301  uart_parity_t parity_bit; // Parity bit.
302  uart_stop_bits_t stop_bit; // Stop bits.
303 
304 } mbusrf4_cfg_t;
305 
309 typedef uint8_t mbusrf4_error_t;
310  // End types group
312 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
313 
319 #ifdef __cplusplus
320 extern "C"{
321 #endif
322 
332 
342 
352 void mbusrf4_generic_write ( mbusrf4_t *ctx, char *data_buf, uint16_t len );
353 
364 int32_t mbusrf4_generic_read ( mbusrf4_t *ctx, char *data_buf, uint16_t max_len );
365 
374 
383 void mbusrf4_set_communication_mode ( mbusrf4_t *ctx, uint8_t mode );
384 
392 uint8_t mbusrf4_get_state_rts ( mbusrf4_t *ctx );
393 
405 void mbusrf4_send_command ( mbusrf4_t *ctx, uint8_t command, uint8_t length, uint8_t *payload_buff );
406 
407 
408 void mbusrf4_transmit_data ( mbusrf4_t *ctx, uint8_t *tx_buf, uint8_t tx_len );
409 
418 
419 #ifdef __cplusplus
420 }
421 #endif
422 #endif // _MBUSRF4_H_
423  // End public_function group
426 
427 // ------------------------------------------------------------------------- END
mbusrf4_cfg_t::cs
pin_name_t cs
Definition: mbusrf4.h:292
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: mbusrf4.h:87
mbusrf4_generic_read
int32_t mbusrf4_generic_read(mbusrf4_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
mbusrf4_init
MBUSRF4_RETVAL mbusrf4_init(mbusrf4_t *ctx, mbusrf4_cfg_t *cfg)
Initialization function.
mbusrf4_set_communication_mode
void mbusrf4_set_communication_mode(mbusrf4_t *ctx, uint8_t mode)
Set communication SPI/UART.
mbusrf4_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: mbusrf4.h:301
mbusrf4_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: mbusrf4.h:300
mbusrf4_t::uart
uart_t uart
Definition: mbusrf4.h:272
mbusrf4_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: mbusrf4.h:302
mbusrf4_generic_write
void mbusrf4_generic_write(mbusrf4_t *ctx, char *data_buf, uint16_t len)
Generic write function.
mbusrf4_cfg_setup
void mbusrf4_cfg_setup(mbusrf4_cfg_t *cfg)
Config Object Initialization function.
mbusrf4_cfg_t::uart_blocking
bool uart_blocking
Definition: mbusrf4.h:299
mbusrf4_cfg_t::tx_pin
pin_name_t tx_pin
Definition: mbusrf4.h:287
mbusrf4_cfg_t::baud_rate
uint32_t baud_rate
Definition: mbusrf4.h:298
mbusrf4_get_state_ind
uint8_t mbusrf4_get_state_ind(mbusrf4_t *ctx)
Get Interrupt state of the IND(AN) pin.
mbusrf4_t
Click ctx object definition.
Definition: mbusrf4.h:259
mbusrf4_t::ind
digital_in_t ind
Definition: mbusrf4.h:267
mbusrf4_send_command
void mbusrf4_send_command(mbusrf4_t *ctx, uint8_t command, uint8_t length, uint8_t *payload_buff)
Send command - UART communication.
mbusrf4_t::sel
digital_out_t sel
Definition: mbusrf4.h:263
mbusrf4_cfg_t::ind
pin_name_t ind
Definition: mbusrf4.h:291
mbusrf4_t::cs
digital_out_t cs
Definition: mbusrf4.h:262
mbusrf4_cfg_t::rx_pin
pin_name_t rx_pin
Definition: mbusrf4.h:286
mbusrf4_get_state_rts
uint8_t mbusrf4_get_state_rts(mbusrf4_t *ctx)
Get Interrupt state of the RTS(INT) pin.
mbusrf4_cfg_t
Click configuration structure definition.
Definition: mbusrf4.h:283
mbusrf4_transmit_data
void mbusrf4_transmit_data(mbusrf4_t *ctx, uint8_t *tx_buf, uint8_t tx_len)
mbusrf4_cfg_t::rts
pin_name_t rts
Definition: mbusrf4.h:294
mbusrf4_cfg_t::sel
pin_name_t sel
Definition: mbusrf4.h:293
mbusrf4_t::rts
digital_in_t rts
Definition: mbusrf4.h:268
mbusrf4_error_t
uint8_t mbusrf4_error_t
Error type.
Definition: mbusrf4.h:309
MBUSRF4_RETVAL
#define MBUSRF4_RETVAL
Definition: mbusrf4.h:66