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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 | //
//
//
#include "fw_board_razzeto_v3.h"
#include <Wire.h>
#define VALVE_IN_PIN A0
#define VALVE_OUT_PIN 32
#define BUZZER 12
#define ALARM_LED 13
#define TCAADDR 0x70
bool HW_V3::Init()
{
Serial.begin(115200);
ledcSetup(0, 10000, 12);
ledcAttachPin(VALVE_IN_PIN, 0);
ledcWrite(0, 0);
digitalWrite(VALVE_OUT_PIN, LOW);
digitalWrite(ALARM_LED, LOW);
digitalWrite(BUZZER, LOW);
pinMode(VALVE_OUT_PIN, OUTPUT);
pinMode(ALARM_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
Wire.begin();
for (int i = 0; i < 8; i++) {
//i2c_MuxSelect(i);
Serial.println("SCAN I2C BUS: " + String(i));
__service_i2c_detect();
}
iic_devs[0].t_device = IIC_PS_0;
iic_devs[0].muxport = 4;
iic_devs[0].address = 0x76;
iic_devs[1].t_device = IIC_PS_1;
iic_devs[1].muxport = 4;
iic_devs[1].address = 0x77;
iic_devs[2].t_device = IIC_PS_2;
iic_devs[2].muxport = 5;
iic_devs[2].address = 0x76;
iic_devs[3].t_device = IIC_FLOW1;
iic_devs[3].muxport = 2;
iic_devs[3].address = 0x2E;
iic_devs[4].t_device = IIC_MUX;
iic_devs[4].muxport = -1;
iic_devs[4].address = 0x70;
iic_devs[5].t_device = IIC_GENERAL_CALL_SENSIRION;
iic_devs[5].muxport = 2;
iic_devs[5].address = 0x00;
}
bool HW_V3::I2CWrite(t_i2cdevices device, uint8_t* wbuffer, int wlength, bool stop)
{
uint8_t address;
uint8_t result;
t_i2cdev dev = GetIICDevice(device);
address = dev.address;
i2c_MuxSelect(dev.muxport);
Wire.beginTransmission(address);
for (int i=0;i< wlength; i++)
Wire.write(wbuffer[i]);
result = Wire.endTransmission();
if (result != 0)
return false;
else
return true;
}
bool HW_V3::I2CRead(t_i2cdevices device, uint8_t* wbuffer, int wlength, uint8_t* rbuffer, int rlength, bool stop)
{
uint8_t address;
uint8_t count;
uint8_t result;
t_i2cdev dev = GetIICDevice(device);
address = dev.address;
i2c_MuxSelect(dev.muxport);
Wire.beginTransmission(address);
for (int i = 0;i < wlength; i++)
Wire.write(wbuffer[i]);
result = Wire.endTransmission();
if (result != 0)
return false;
count = Wire.requestFrom((uint16_t)address, (uint8_t) rlength, stop);
if (count < rlength)
return false;
for (int i = 0;i < rlength; i++)
{
rbuffer[i] = Wire.read();
}
return true;
}
bool HW_V3::I2CRead(t_i2cdevices device, uint8_t* rbuffer, int rlength, bool stop)
{
uint8_t count;
uint8_t address;
t_i2cdev dev = GetIICDevice(device);
address = dev.address;
i2c_MuxSelect(dev.muxport);
count = Wire.requestFrom((uint16_t)address, (uint8_t)rlength, stop);
if (count < rlength)
return false;
for (int i = 0;i < rlength; i++)
{
rbuffer[i] = Wire.read();
}
return true;
}
bool HW_V3::PWMSet(hw_pwm id, float value)
{
if ((value < 0) || (value > 100.0)) return false;
switch (id)
{
case PWM_PV1:
uint32_t v = (uint32_t)value * 4095.0 / 100.0;
ledcWrite(0, v);
break;
}
return true;
}
bool HW_V3::IOSet(hw_gpio id, bool value)
{
switch (id)
{
case GPIO_PV2:
digitalWrite(VALVE_OUT_PIN, value ? HIGH : LOW);
break;
case GPIO_BUZZER:
digitalWrite(BUZZER, value ? HIGH : LOW);
break;
case GPIO_LED:
digitalWrite(ALARM_LED, value ? HIGH : LOW);
break;
case GPIO_RELEALLARM:
//digitalWrite(ALARM_LED, LOW);
break;
default:
return false;
break;
}
return true;
}
bool HW_V3::IOGet(hw_gpio id, bool* value)
{
switch (id)
{
case GPIO_PV2:
*value = digitalRead(VALVE_OUT_PIN);
break;
case GPIO_BUZZER:
*value = digitalRead(BUZZER);
break;
case GPIO_LED:
*value = digitalRead(ALARM_LED);
break;
case GPIO_RELEALLARM:
*value = 0;
//*value = digitalRead(ALARM_LED);
break;
default:
return false;
break;
}
return true;
}
void HW_V3::__delay_blocking_ms(uint32_t ms)
{
delay(ms);
}
void HW_V3::PrintDebugConsole(String s)
{
Serial.print(s);
}
void HW_V3::PrintLineDebugConsole(String s)
{
Serial.println(s);
}
void HW_V3::Tick()
{
return;
}
bool HW_V3::DataAvailableOnUART0()
{
return Serial.available();
}
bool HW_V3::WriteUART0(String s)
{
Serial.println(s);
return true;
}
String HW_V3::ReadUART0UntilEOL()
{
//PERICOLO. SE IL \n NON VIENE INVIATO TUTTO STALLA!!!!
return Serial.readStringUntil('\n');
}
uint64_t HW_V3::GetMillis()
{
return (uint64_t) millis();
}
int64_t HW_V3::Get_dT_millis(uint64_t ms)
{
return (int64_t)(millis() - ms);
}
void HW_V3::__service_i2c_detect()
{
byte error, address;
int nDevices;
Serial.println("Scanning... I2C");
nDevices = 0;
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.println(address, HEX);
nDevices++;
}
else if (error == 4) {
Serial.print("Unknow error at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
}
void HW_V3::i2c_MuxSelect(uint8_t i)
{
if (i > 7)
return;
if (i < 0)<--- Unsigned less than zero
return;
if (current_muxpos == i) return;
current_muxpos = i;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
t_i2cdev HW_V3::GetIICDevice(t_i2cdevices device)
{
for (int i = 0; i < IIC_COUNT; i++)
{
if (iic_devs[i].t_device == device)
{
return iic_devs[i];
}
}
}
void HW_V3::GetPowerStatus(bool* batteryPowered, float* charge)
{
*batteryPowered = false;
*charge = 100;
}
float HW_V3::GetPIN()
{
return 3.8;
}
float HW_V3::GetBoardTemperature()
{
return 40;
}
uint16_t HW_V3::GetSupervisorAlarms()
{
return 0;
}
// # # ###
// ## # #
// # # # #
// # # # #
// # # # #
// # ## #
// # # ###
//
// Nuclear Instruments 2020 - All rights reserved
// Any commercial use of this code is forbidden
// Contact info@nuclearinstruments.eu
|