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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_uart.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define MBUSRF4_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
65  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
66  cfg.ind = MIKROBUS( mikrobus, MIKROBUS_AN ); \
67  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
68  cfg.sel = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
69  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT )
70 
76 #define MBUSRF4_RETVAL uint8_t
77 
78 #define MBUSRF4_OK 0x00
79 #define MBUSRF4_INIT_ERROR 0xFF
80 
86 #define MBUSRF4_ERROR_COMMAND_OR_ELEMENT 0xFF
87 #define MBUSRF4_ERROR_START_OR_END_COMMAND 0x8F
88 #define MBUSRF4_ERROR_NEXT_ELEMENT 0x4F
89 #define MBUSRF4_ERROR_CURRENT_ELEMENT 0x2F
90 #define MBUSRF4_NO_ERROR 0x00
91 
97 #define DRV_RX_BUFFER_SIZE 256
98 
104 #define MBUSRF4_CMD_RESET 0x30
105 #define MBUSRF4_CMD_FACTORY_RESET 0x31
106 #define MBUSRF4_CMD_SET_MODE 0x32
107 #define MBUSRF4_CMD_EEPROM_WRITE 0x33
108 #define MBUSRF4_CMD_EEPROM_READ 0x34
109 #define MBUSRF4_CMD_TX_MSG 0x35
110 #define MBUSRF4_CMD_RX_MSG_IND 0x36
111 #define MBUSRF4_CMD_GET_FW_VERSION 0x37
112 #define MBUSRF4_CMD_GET_SERIALNO 0x38
113 #define MBUSRF4_CMD_GET_RSSI 0x39
114 #define MBUSRF4_CMD_SET_C_FIELD 0x40
115 
121 #define MBUSRF4_EEADDR_WM_BUS_MODE 0x00
122 #define MBUSRF4_EEADDR_RF_CHANNEL 0x01
123 #define MBUSRF4_EEADDR_RF_POWER 0x02
124 #define MBUSRF4_EEADDR_RF_AUTOSLEEP 0x03
125 #define MBUSRF4_EEADDR_RX_WINDOW 0x04
126 
127 #define MBUSRF4_EEADDR_WMBUS_C_FIELD 0x10
128 #define MBUSRF4_EEADDR_WMBUS_MANUFACTER_ID0 0x11
129 #define MBUSRF4_EEADDR_WMBUS_MANUFACTER_ID1 0x12
130 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID0 0x13
131 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID1 0x14
132 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID2 0x15
133 #define MBUSRF4_EEADDR_WMBUS_DEVICE_ID3 0x16
134 #define MBUSRF4_EEADDR_WMBUS_VERSION 0x17
135 #define MBUSRF4_EEADDR_WMBUS_DEVICE_TYPE 0x18
136 
137 #define MBUSRF4_EEADDR_WMBUS_BLOCK1_FROM_MODULE_ENABLE 0x20
138 #define MBUSRF4_EEADDR_WMBUS_RSSI_ENABLE 0x21
139 #define MBUSRF4_EEADDR_WMBUS_INDICATE_TIMEOUT_LSB 0x22
140 #define MBUSRF4_EEADDR_WMBUS_INDICATE_TIMEOUT_MSB 0x23
141 #define MBUSRF4_EEADDR_WMBUS_UART_BAUDREATE 0x24
142 
148 #define MBUSRF4_EEPARAM_WMBUS_MODE_S2_SHORT_PREAMBLE 0x00
149 #define MBUSRF4_EEPARAM_WMBUS_MODE_S2_LONG_PREAMBLE 0x01
150 #define MBUSRF4_EEPARAM_WMBUS_MODE_S1 0x02
151 #define MBUSRF4_EEPARAM_WMBUS_MODE_S1_M 0x03
152 #define MBUSRF4_EEPARAM_WMBUS_MODE_T1_METER 0x04
153 #define MBUSRF4_EEPARAM_WMBUS_MODE_T2_METER 0x05
154 #define MBUSRF4_EEPARAM_WMBUS_MODE_T2_OTHER 0x06
155 #define MBUSRF4_EEPARAM_WMBUS_MODE_R2_METER 0x07
156 #define MBUSRF4_EEPARAM_WMBUS_MODE_R2_OTHER 0x08
157 #define MBUSRF4_EEPARAM_WMBUS_MODE_C1_METER_FRAME_A 0x09
158 #define MBUSRF4_EEPARAM_WMBUS_MODE_C1_METER_FRAME_B 0x0A
159 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_METER_FRAME_A 0x0B
160 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_METER_FRAME_B 0x0C
161 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_OTHER_FRAME_A 0x0D
162 #define MBUSRF4_EEPARAM_WMBUS_MODE_C2_OTHER_FRAME_B 0x0E
163 #define MBUSRF4_EEPARAM_WMBUS_MODE_T2_C2_OTHER 0x0F
164 
170 #define MBUSRF4_EEPARAM_RFGH_868p03MHz 0x00
171 #define MBUSRF4_EEPARAM_RFGH_868p09MHz 0x01
172 #define MBUSRF4_EEPARAM_RFGH_868p15MHz 0x02
173 #define MBUSRF4_EEPARAM_RFGH_868p21MHz 0x03
174 #define MBUSRF4_EEPARAM_RFGH_868p27MHz 0x04
175 #define MBUSRF4_EEPARAM_RFGH_868p33MHz 0x05
176 #define MBUSRF4_EEPARAM_RFGH_868p39MHz 0x06
177 #define MBUSRF4_EEPARAM_RFGH_868p45MHz 0x07
178 #define MBUSRF4_EEPARAM_RFGH_868p51MHz 0x08
179 #define MBUSRF4_EEPARAM_RFGH_868p57MHz 0x09
180 
186 #define MBUSRF4_EEPARAM_RFPWR_0dBm 0x00
187 #define MBUSRF4_EEPARAM_RFPWR_5dBm 0x01
188 #define MBUSRF4_EEPARAM_RFPWR_7dBm 0x02
189 #define MBUSRF4_EEPARAM_RFPWR_10dBm 0x03
190 #define MBUSRF4_EEPARAM_RFPWR_12dBm 0x04
191 
197 #define MBUSRF4_EEPARAM_RFAS_SLEEP_DISABLE 0x00
198 #define MBUSRF4_EEPARAM_RFAS_SLEEP_ENABLE 0x01
199 
205 #define MBUSRF4_WM_BUS_C_FIELD 0x44
206 #define MBUSRF4_HEADER 0xAA
207 
213 #define MBUSRF4_EEPARAM_BLOCK1_FROM_MODULE_ENABLE 0x01
214 #define MBUSRF4_EEPARAM_BLOCK1_FROM_MODULE_DISABLE 0x00
215 
221 #define MBUSRF4_EEPARAM_RSSI_ENABLE 0x01
222 #define MBUSRF4_EEPARAM_RSSI_DISABLE 0x00
223 
229 #define MBUSRF4_EEPARAM_UART_BAUDRATE_9600 0x00
230 #define MBUSRF4_EEPARAM_UART_BAUDRATE_19200 0x01
231 #define MBUSRF4_EEPARAM_UART_BAUDRATE_38400 0x02
232 #define MBUSRF4_EEPARAM_UART_BAUDRATE_57600 0x03
233 #define MBUSRF4_EEPARAM_UART_BAUDRATE_115200 0x04
234 
240 #define MBUSRF4_SET_VALUE_IN_RAM_MEMORY 0x00
241 #define MBUSRF4_SET_VALUE_IN_EEPROM_MEMORY 0xFF
242 
248 #define MBUSRF4_USER_APP_RX_MODE 0x00
249 #define MBUSRF4_USER_APP_TX_MODE 0x01
250 
256 #define MBUSRF4_COMMUNICATION_UART_MODE 0x01
257 #define MBUSRF4_COMMUNICATION_SPI_MODE 0x00
258  // End group macro
260 // --------------------------------------------------------------- PUBLIC TYPES
268 typedef struct
269 {
270  // Output pins
271 
272  digital_out_t cs;
273  digital_out_t sel;
274 
275  // Input pins
276 
277  digital_in_t ind;
278  digital_in_t rts;
279 
280  // Modules
281 
282  uart_t uart;
283 
284  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
285  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
286 
287 } mbusrf4_t;
288 
292 typedef struct
293 {
294  // Communication gpio pins
295 
296  pin_name_t rx_pin;
297  pin_name_t tx_pin;
298 
299  // Additional gpio pins
300 
301  pin_name_t ind;
302  pin_name_t cs;
303  pin_name_t sel;
304  pin_name_t rts;
305 
306  // static variable
307 
308  uint32_t baud_rate; // Clock speed.
310  uart_data_bits_t data_bit; // Data bits.
311  uart_parity_t parity_bit; // Parity bit.
312  uart_stop_bits_t stop_bit; // Stop bits.
313 
314 } mbusrf4_cfg_t;
315 
319 typedef uint8_t mbusrf4_error_t;
320  // End types group
322 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
323 
329 #ifdef __cplusplus
330 extern "C"{
331 #endif
332 
342 
352 
362 void mbusrf4_generic_write ( mbusrf4_t *ctx, char *data_buf, uint16_t len );
363 
374 int32_t mbusrf4_generic_read ( mbusrf4_t *ctx, char *data_buf, uint16_t max_len );
375 
384 
393 void mbusrf4_set_communication_mode ( mbusrf4_t *ctx, uint8_t mode );
394 
402 uint8_t mbusrf4_get_state_rts ( mbusrf4_t *ctx );
403 
415 void mbusrf4_send_command ( mbusrf4_t *ctx, uint8_t command, uint8_t length, uint8_t *payload_buff );
416 
417 
418 void mbusrf4_transmit_data ( mbusrf4_t *ctx, uint8_t *tx_buf, uint8_t tx_len );
419 
428 
429 #ifdef __cplusplus
430 }
431 #endif
432 #endif // _MBUSRF4_H_
433  // End public_function group
436 
437 // ------------------------------------------------------------------------- END
mbusrf4_cfg_t::cs
pin_name_t cs
Definition: mbusrf4.h:302
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: mbusrf4.h:97
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:311
mbusrf4_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: mbusrf4.h:310
mbusrf4_t::uart
uart_t uart
Definition: mbusrf4.h:282
mbusrf4_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: mbusrf4.h:312
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:309
mbusrf4_cfg_t::tx_pin
pin_name_t tx_pin
Definition: mbusrf4.h:297
mbusrf4_cfg_t::baud_rate
uint32_t baud_rate
Definition: mbusrf4.h:308
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:269
mbusrf4_t::ind
digital_in_t ind
Definition: mbusrf4.h:277
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:273
mbusrf4_cfg_t::ind
pin_name_t ind
Definition: mbusrf4.h:301
mbusrf4_t::cs
digital_out_t cs
Definition: mbusrf4.h:272
mbusrf4_cfg_t::rx_pin
pin_name_t rx_pin
Definition: mbusrf4.h:296
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:293
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:304
mbusrf4_cfg_t::sel
pin_name_t sel
Definition: mbusrf4.h:303
mbusrf4_t::rts
digital_in_t rts
Definition: mbusrf4.h:278
mbusrf4_error_t
uint8_t mbusrf4_error_t
Error type.
Definition: mbusrf4.h:319
MBUSRF4_RETVAL
#define MBUSRF4_RETVAL
Definition: mbusrf4.h:76