-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples.c
148 lines (132 loc) · 3.46 KB
/
samples.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <pic-modbus/modbus.h>
#include "samples.h"
/**
* Holding registers [0-2]
*/
typedef struct {
/**
* See SYS_RESET_REASON
*/
uint8_t resetReason;
uint8_t _filler1;
/**
* Count of CRC errors in the reading period
*/
uint8_t crcErrors;
uint8_t _filler2;
} SYS_REGISTERS;
#define SYS_REGS_ADDRESS (0)
#define SYS_REGS_ADDRESS_BE (LE_TO_BE_16(SYS_REGS_ADDRESS))
#define SYS_REGS_COUNT (sizeof(SYS_REGISTERS) / 2)
void samples_init() {
#ifdef HAS_LED_BLINK
blinker_init();
#endif
#ifdef HAS_I2C
i2c_init();
#endif
#ifdef HAS_BMP180
bmp180_init();
#endif
}
void samples_poll() {
#ifdef HAS_LED_BLINK
blinker_poll();
#endif
#ifdef HAS_I2C
i2c_poll();
#endif
#ifdef HAS_BMP180
bmp180_poll();
#endif
}
static uint16_t addressBe;
_Bool regs_validateReg() {
uint8_t count = bus_cl_header.address.countL;
addressBe = bus_cl_header.address.registerAddressBe;
// Exposes the system registers in the rane 0-2
if (addressBe == SYS_REGS_ADDRESS_BE) {
if (count != SYS_REGS_COUNT) {
bus_cl_exceptionCode = ERR_INVALID_SIZE;
return false;
}
if (bus_cl_header.header.function != READ_HOLDING_REGISTERS) {
bus_cl_exceptionCode = ERR_INVALID_FUNCTION;
return false;
}
return true;
}
#ifdef HAS_LED_BLINK
if (addressBe == LEDBLINK_REGS_ADDRESS_BE) {
if (count != LEDBLINK_REGS_COUNT) {
bus_cl_exceptionCode = ERR_INVALID_SIZE;
return false;
}
return true;
}
#endif
#ifdef HAS_BMP180
if (addressBe == BMP180_REGS_CALIB_ADDRESS_BE) {
if (count != BMP180_REGS_CALIB_COUNT) {
bus_cl_exceptionCode = ERR_INVALID_SIZE;
return false;
}
if (bus_cl_header.header.function != READ_HOLDING_REGISTERS) {
bus_cl_exceptionCode = ERR_INVALID_FUNCTION;
return false;
}
return true;
}
if (addressBe == BMP180_REGS_DATA_ADDRESS_BE) {
if (count != BMP180_REGS_DATA_COUNT) {
bus_cl_exceptionCode = ERR_INVALID_SIZE;
return false;
}
if (bus_cl_header.header.function != READ_HOLDING_REGISTERS) {
bus_cl_exceptionCode = ERR_INVALID_FUNCTION;
return false;
}
return true;
}
#endif
bus_cl_exceptionCode = ERR_INVALID_ADDRESS;
return false;
}
_Bool regs_onReceive() {
if (addressBe == SYS_REGS_ADDRESS_BE) {
// Ignore data, reset flags and counters
sys_resetReason = RESET_NONE;
bus_cl_crcErrors = 0;
return true;
}
#ifdef HAS_LED_BLINK
if (addressBe == LEDBLINK_REGS_ADDRESS_BE) {
memcpy(&blinker_regs, rs485_buffer, sizeof(LedBlinkRegsiters));
return blinker_conf();
}
#endif
return false;
}
void regs_onSend() {
if (addressBe == SYS_REGS_ADDRESS_BE) {
((SYS_REGISTERS*)rs485_buffer)->crcErrors = bus_cl_crcErrors;
((SYS_REGISTERS*)rs485_buffer)->resetReason = sys_resetReason;
return;
}
#ifdef HAS_LED_BLINK
if (addressBe == LEDBLINK_REGS_ADDRESS_BE) {
memcpy(rs485_buffer, &blinker_regs, sizeof(LedBlinkRegsiters));
return;
}
#endif
#ifdef HAS_BMP180
if (addressBe == BMP180_REGS_CALIB_ADDRESS_BE) {
bmp180_readCalibrationData();
return;
}
if (addressBe == BMP180_REGS_DATA_ADDRESS_BE) {
bmp180_readRawData();
return;
}
#endif
}