This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
rf_mbus.hpp
109 lines (89 loc) · 3.18 KB
/
rf_mbus.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/***********************************************************************************
Filename: rf_mbus.hpp
***********************************************************************************/
#ifndef _RF_MBUS_H
#define _RF_MBUS_H
#include <stdint.h>
#include <string>
//----------------------------------------------------------------------------------
// Constants
//----------------------------------------------------------------------------------
// CC1101 state machine
#define MARCSTATE_SLEEP 0x00
#define MARCSTATE_IDLE 0x01
#define MARCSTATE_XOFF 0x02
#define MARCSTATE_VCOON_MC 0x03
#define MARCSTATE_REGON_MC 0x04
#define MARCSTATE_MANCAL 0x05
#define MARCSTATE_VCOON 0x06
#define MARCSTATE_REGON 0x07
#define MARCSTATE_STARTCAL 0x08
#define MARCSTATE_BWBOOST 0x09
#define MARCSTATE_FS_LOCK 0x0A
#define MARCSTATE_IFADCON 0x0B
#define MARCSTATE_ENDCAL 0x0C
#define MARCSTATE_RX 0x0D
#define MARCSTATE_RX_END 0x0E
#define MARCSTATE_RX_RST 0x0F
#define MARCSTATE_TXRX_SWITCH 0x10
#define MARCSTATE_RXFIFO_OVERFLOW 0x11
#define MARCSTATE_FSTXON 0x12
#define MARCSTATE_TX 0x13
#define MARCSTATE_TX_END 0x14
#define MARCSTATE_RXTX_SWITCH 0x15
#define MARCSTATE_TXFIFO_UNDERFLOW 0x16
#define RX_FIFO_START_THRESHOLD 0
#define RX_FIFO_THRESHOLD 10
#define RX_AVAILABLE_FIFO 44
#define FIXED_PACKET_LENGTH 0x00
#define INFINITE_PACKET_LENGTH 0x02
enum WmBusFrameType : uint8_t {
WMBUS_FRAME_UNKNOWN = 0,
WMBUS_FRAMEA = 1,
WMBUS_FRAMEB = 2,
};
enum WmBusFrameMode : uint8_t {
WMBUS_UNKNOWN_MODE = 0,
WMBUS_T1_MODE = 1,
WMBUS_C1_MODE = 2,
};
typedef struct RXinfoDescr {
uint8_t lengthField; // The L-field in the WMBUS packet
uint16_t length; // Total number of bytes to to be read from the RX FIFO
uint16_t bytesLeft; // Bytes left to to be read from the RX FIFO
uint8_t *pByteIndex; // Pointer to current position in the byte array
bool start; // Start of Packet
bool complete; // Packet received complete
uint8_t state;
WmBusFrameMode framemode;
WmBusFrameType frametype;
} RXinfoDescr;
typedef struct WMbusFrame {
std::vector<unsigned char> frame{};
WmBusFrameMode framemode;
int8_t rssi;
uint8_t lqi;
} WMbusFrame;
//----------------------------------------------------------------------------------
// Function declarations
//----------------------------------------------------------------------------------
const std::string mode_to_string(WmBusFrameMode mode);
class rf_mbus {
public:
bool init(uint8_t mosi, uint8_t miso, uint8_t clk, uint8_t cs,
uint8_t gdo0, uint8_t gdo2, float freq);
bool task();
WMbusFrame get_frame();
private:
uint8_t start(bool force = true);
uint8_t gdo0{0};
uint8_t gdo2{0};
uint8_t MBbytes[584];
uint8_t MBpacket[291];
WMbusFrame returnFrame;
RXinfoDescr RXinfo;
uint32_t sync_time_{0};
uint8_t extra_time_{20};
uint8_t max_wait_time_ = extra_time_;
};
#endif