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
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
57// -------------------------------------------------------------- PUBLIC MACROS
67#define MBUSRF4_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
69 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
70 cfg.ind = MIKROBUS( mikrobus, MIKROBUS_AN ); \
71 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72 cfg.sel = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
73 cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT )
80#define MBUSRF4_RETVAL uint8_t
81
82#define MBUSRF4_OK 0x00
83#define MBUSRF4_INIT_ERROR 0xFF
90#define MBUSRF4_ERROR_COMMAND_OR_ELEMENT 0xFF
91#define MBUSRF4_ERROR_START_OR_END_COMMAND 0x8F
92#define MBUSRF4_ERROR_NEXT_ELEMENT 0x4F
93#define MBUSRF4_ERROR_CURRENT_ELEMENT 0x2F
94#define MBUSRF4_NO_ERROR 0x00
101#define DRV_RX_BUFFER_SIZE 256
108#define MBUSRF4_CMD_RESET 0x30
109#define MBUSRF4_CMD_FACTORY_RESET 0x31
110#define MBUSRF4_CMD_SET_MODE 0x32
111#define MBUSRF4_CMD_EEPROM_WRITE 0x33
112#define MBUSRF4_CMD_EEPROM_READ 0x34
113#define MBUSRF4_CMD_TX_MSG 0x35
114#define MBUSRF4_CMD_RX_MSG_IND 0x36
115#define MBUSRF4_CMD_GET_FW_VERSION 0x37
116#define MBUSRF4_CMD_GET_SERIALNO 0x38
117#define MBUSRF4_CMD_GET_RSSI 0x39
118#define MBUSRF4_CMD_SET_C_FIELD 0x40
125#define MBUSRF4_EEADDR_WM_BUS_MODE 0x00
126#define MBUSRF4_EEADDR_RF_CHANNEL 0x01
127#define MBUSRF4_EEADDR_RF_POWER 0x02
128#define MBUSRF4_EEADDR_RF_AUTOSLEEP 0x03
129#define MBUSRF4_EEADDR_RX_WINDOW 0x04
130
131#define MBUSRF4_EEADDR_WMBUS_C_FIELD 0x10
132#define MBUSRF4_EEADDR_WMBUS_MANUFACTER_ID0 0x11
133#define MBUSRF4_EEADDR_WMBUS_MANUFACTER_ID1 0x12
134#define MBUSRF4_EEADDR_WMBUS_DEVICE_ID0 0x13
135#define MBUSRF4_EEADDR_WMBUS_DEVICE_ID1 0x14
136#define MBUSRF4_EEADDR_WMBUS_DEVICE_ID2 0x15
137#define MBUSRF4_EEADDR_WMBUS_DEVICE_ID3 0x16
138#define MBUSRF4_EEADDR_WMBUS_VERSION 0x17
139#define MBUSRF4_EEADDR_WMBUS_DEVICE_TYPE 0x18
140
141#define MBUSRF4_EEADDR_WMBUS_BLOCK1_FROM_MODULE_ENABLE 0x20
142#define MBUSRF4_EEADDR_WMBUS_RSSI_ENABLE 0x21
143#define MBUSRF4_EEADDR_WMBUS_INDICATE_TIMEOUT_LSB 0x22
144#define MBUSRF4_EEADDR_WMBUS_INDICATE_TIMEOUT_MSB 0x23
145#define MBUSRF4_EEADDR_WMBUS_UART_BAUDREATE 0x24
152#define MBUSRF4_EEPARAM_WMBUS_MODE_S2_SHORT_PREAMBLE 0x00
153#define MBUSRF4_EEPARAM_WMBUS_MODE_S2_LONG_PREAMBLE 0x01
154#define MBUSRF4_EEPARAM_WMBUS_MODE_S1 0x02
155#define MBUSRF4_EEPARAM_WMBUS_MODE_S1_M 0x03
156#define MBUSRF4_EEPARAM_WMBUS_MODE_T1_METER 0x04
157#define MBUSRF4_EEPARAM_WMBUS_MODE_T2_METER 0x05
158#define MBUSRF4_EEPARAM_WMBUS_MODE_T2_OTHER 0x06
159#define MBUSRF4_EEPARAM_WMBUS_MODE_R2_METER 0x07
160#define MBUSRF4_EEPARAM_WMBUS_MODE_R2_OTHER 0x08
161#define MBUSRF4_EEPARAM_WMBUS_MODE_C1_METER_FRAME_A 0x09
162#define MBUSRF4_EEPARAM_WMBUS_MODE_C1_METER_FRAME_B 0x0A
163#define MBUSRF4_EEPARAM_WMBUS_MODE_C2_METER_FRAME_A 0x0B
164#define MBUSRF4_EEPARAM_WMBUS_MODE_C2_METER_FRAME_B 0x0C
165#define MBUSRF4_EEPARAM_WMBUS_MODE_C2_OTHER_FRAME_A 0x0D
166#define MBUSRF4_EEPARAM_WMBUS_MODE_C2_OTHER_FRAME_B 0x0E
167#define MBUSRF4_EEPARAM_WMBUS_MODE_T2_C2_OTHER 0x0F
174#define MBUSRF4_EEPARAM_RFGH_868p03MHz 0x00
175#define MBUSRF4_EEPARAM_RFGH_868p09MHz 0x01
176#define MBUSRF4_EEPARAM_RFGH_868p15MHz 0x02
177#define MBUSRF4_EEPARAM_RFGH_868p21MHz 0x03
178#define MBUSRF4_EEPARAM_RFGH_868p27MHz 0x04
179#define MBUSRF4_EEPARAM_RFGH_868p33MHz 0x05
180#define MBUSRF4_EEPARAM_RFGH_868p39MHz 0x06
181#define MBUSRF4_EEPARAM_RFGH_868p45MHz 0x07
182#define MBUSRF4_EEPARAM_RFGH_868p51MHz 0x08
183#define MBUSRF4_EEPARAM_RFGH_868p57MHz 0x09
190#define MBUSRF4_EEPARAM_RFPWR_0dBm 0x00
191#define MBUSRF4_EEPARAM_RFPWR_5dBm 0x01
192#define MBUSRF4_EEPARAM_RFPWR_7dBm 0x02
193#define MBUSRF4_EEPARAM_RFPWR_10dBm 0x03
194#define MBUSRF4_EEPARAM_RFPWR_12dBm 0x04
201#define MBUSRF4_EEPARAM_RFAS_SLEEP_DISABLE 0x00
202#define MBUSRF4_EEPARAM_RFAS_SLEEP_ENABLE 0x01
209#define MBUSRF4_WM_BUS_C_FIELD 0x44
210#define MBUSRF4_HEADER 0xAA
217#define MBUSRF4_EEPARAM_BLOCK1_FROM_MODULE_ENABLE 0x01
218#define MBUSRF4_EEPARAM_BLOCK1_FROM_MODULE_DISABLE 0x00
225#define MBUSRF4_EEPARAM_RSSI_ENABLE 0x01
226#define MBUSRF4_EEPARAM_RSSI_DISABLE 0x00
233#define MBUSRF4_EEPARAM_UART_BAUDRATE_9600 0x00
234#define MBUSRF4_EEPARAM_UART_BAUDRATE_19200 0x01
235#define MBUSRF4_EEPARAM_UART_BAUDRATE_38400 0x02
236#define MBUSRF4_EEPARAM_UART_BAUDRATE_57600 0x03
237#define MBUSRF4_EEPARAM_UART_BAUDRATE_115200 0x04
244#define MBUSRF4_SET_VALUE_IN_RAM_MEMORY 0x00
245#define MBUSRF4_SET_VALUE_IN_EEPROM_MEMORY 0xFF
252#define MBUSRF4_USER_APP_RX_MODE 0x00
253#define MBUSRF4_USER_APP_TX_MODE 0x01
260#define MBUSRF4_COMMUNICATION_UART_MODE 0x01
261#define MBUSRF4_COMMUNICATION_SPI_MODE 0x00
263 // End group macro
264// --------------------------------------------------------------- PUBLIC TYPES
272typedef struct
273{
274 // Output pins
275
276 digital_out_t cs;
277 digital_out_t sel;
278
279 // Input pins
280
281 digital_in_t ind;
282 digital_in_t rts;
283
284 // Modules
285
286 uart_t uart;
287
288 char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
289 char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
290
291} mbusrf4_t;
292
296typedef struct
297{
298 // Communication gpio pins
299
300 pin_name_t rx_pin;
301 pin_name_t tx_pin;
302
303 // Additional gpio pins
304
305 pin_name_t ind;
306 pin_name_t cs;
307 pin_name_t sel;
308 pin_name_t rts;
309
310 // static variable
311
312 uint32_t baud_rate; // Clock speed.
314 uart_data_bits_t data_bit; // Data bits.
315 uart_parity_t parity_bit; // Parity bit.
316 uart_stop_bits_t stop_bit; // Stop bits.
317
319
323typedef uint8_t mbusrf4_error_t;
324
325 // End types group
326// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
327
333#ifdef __cplusplus
334extern "C"{
335#endif
336
346
356
366void mbusrf4_generic_write ( mbusrf4_t *ctx, char *data_buf, uint16_t len );
367
378int32_t mbusrf4_generic_read ( mbusrf4_t *ctx, char *data_buf, uint16_t max_len );
379
388
397void mbusrf4_set_communication_mode ( mbusrf4_t *ctx, uint8_t mode );
398
406uint8_t mbusrf4_get_state_rts ( mbusrf4_t *ctx );
407
419void mbusrf4_send_command ( mbusrf4_t *ctx, uint8_t command, uint8_t length, uint8_t *payload_buff );
420
421
422void mbusrf4_transmit_data ( mbusrf4_t *ctx, uint8_t *tx_buf, uint8_t tx_len );
423
432
433#ifdef __cplusplus
434}
435#endif
436#endif // _MBUSRF4_H_
437
438 // End public_function group
440
441// ------------------------------------------------------------------------- END
#define DRV_RX_BUFFER_SIZE
Definition mbusrf4.h:101
#define MBUSRF4_RETVAL
Definition mbusrf4.h:80
void mbusrf4_send_command(mbusrf4_t *ctx, uint8_t command, uint8_t length, uint8_t *payload_buff)
Send command - UART communication.
void mbusrf4_cfg_setup(mbusrf4_cfg_t *cfg)
Config Object Initialization function.
MBUSRF4_RETVAL mbusrf4_init(mbusrf4_t *ctx, mbusrf4_cfg_t *cfg)
Initialization function.
int32_t mbusrf4_generic_read(mbusrf4_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
uint8_t mbusrf4_get_state_rts(mbusrf4_t *ctx)
Get Interrupt state of the RTS(INT) pin.
void mbusrf4_set_communication_mode(mbusrf4_t *ctx, uint8_t mode)
Set communication SPI/UART.
void mbusrf4_transmit_data(mbusrf4_t *ctx, uint8_t *tx_buf, uint8_t tx_len)
void mbusrf4_generic_write(mbusrf4_t *ctx, char *data_buf, uint16_t len)
Generic write function.
uint8_t mbusrf4_get_state_ind(mbusrf4_t *ctx)
Get Interrupt state of the IND(AN) pin.
uint8_t mbusrf4_error_t
Error type.
Definition mbusrf4.h:323
Click configuration structure definition.
Definition mbusrf4.h:297
pin_name_t rts
Definition mbusrf4.h:308
uint32_t baud_rate
Definition mbusrf4.h:312
bool uart_blocking
Definition mbusrf4.h:313
uart_data_bits_t data_bit
Definition mbusrf4.h:314
pin_name_t tx_pin
Definition mbusrf4.h:301
pin_name_t rx_pin
Definition mbusrf4.h:300
pin_name_t sel
Definition mbusrf4.h:307
uart_stop_bits_t stop_bit
Definition mbusrf4.h:316
uart_parity_t parity_bit
Definition mbusrf4.h:315
pin_name_t ind
Definition mbusrf4.h:305
pin_name_t cs
Definition mbusrf4.h:306
Click ctx object definition.
Definition mbusrf4.h:273
digital_out_t cs
Definition mbusrf4.h:276
digital_out_t sel
Definition mbusrf4.h:277
uart_t uart
Definition mbusrf4.h:286
digital_in_t rts
Definition mbusrf4.h:282
digital_in_t ind
Definition mbusrf4.h:281