-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunication.cpp
455 lines (380 loc) · 12.6 KB
/
communication.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Technische Richtlinie FA205
// Bibliothek: communication.c
// Controller: ESP32
// I2C und RS232 Routinen
// Version: 1.0
// erstellt am: 18.8.2023
// letzte Änderung: 4.9.2023
// Autor: Rahm
#include "communication.h"
#include <arduino.h>
//#include "esp_log.h"
//#include "driver/i2c.h"
#ifdef _SERIALBT_
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
#endif
// Ab hier Hardware-I2C-Funktionen
#ifdef _HARD_I2C_
// *****************************************************************************************
// I2C- Routinen zur Ansteuerung eins I2C-Slaves
// I2C-Bus-Funktionen i2c_init, i2c_start, i2c_stop, i2c_write, i2c_read, i2c_ack, i2c_nack
// *****************************************************************************************
#define I2C_MASTER_SCL_IO 22 /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO 21 /*!< gpio number for I2C master data */
//#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
const i2c_port_t I2C_MASTER_NUM = I2C_NUM_0; /*!< I2C port number for master dev */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL (i2c_ack_type_t) ACK /*!< I2C ack value */
#define NACK_VAL (i2c_ack_type_t) NACK /*!< I2C nack value */
#define I2C_MASTER_FREQ_HZ _I2C_FREQUENZ_ /*!< I2C master clock frequency */
// ****** Initialiserung I2C-Bus **********************************************
i2c_cmd_handle_t cmd;
//#define _I2C_MAX_DATAREAD_ 5
uint8_t i2c_data[_I2C_MAX_DATAREAD_]; //max. 5 Byte lesen
volatile uint8_t i2c_dp = 0; // Index auf i2c_data[]
void i2c_init (void)
{
i2c_port_t i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf = {
conf.mode = I2C_MODE_MASTER,
conf.sda_io_num = I2C_MASTER_SDA_IO,
conf.scl_io_num = I2C_MASTER_SCL_IO,
conf.sda_pullup_en = GPIO_PULLUP_DISABLE,
conf.scl_pullup_en = GPIO_PULLUP_DISABLE,
conf.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_master_port, &conf);
i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
i2c_filter_enable(i2c_master_port, 4); // cyc_num = 0...7
}
// ****** Startbedingung I2C-Bus **********************************************
void i2c_start (void)
{
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
}
void i2c_rstart (void)
{
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS); //bestehenden cmd (ohne stop) senden
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
}
//****** Stoppbedingung I2C-Bus ***********************************************************
void i2c_stop (void)
{
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
}
// ****************************************************************************************
// * Byte ausgeben an I2C-Bus , Rückgabewert = ack = ACK/NACK
// ****************************************************************************************
uint8_t i2c_write (uint8_t xbyte)
{
esp_err_t ret = i2c_master_write_byte(cmd, xbyte, ACK_CHECK_EN);
if (ret == ESP_OK) return ACK; // Tatsächlich wird hier nur geprüft, ob
else return NACK; // die Parameterübergabe korrekt ist.
// ack vom Slave kann erst bei i2c_stop() gecheckt werden.
}
// ****************************************************************************************
// * Byte einlesen vom I2C-Bus.
// ****************************************************************************************
uint8_t i2c_read (uint8_t ack)
{
uint8_t i;
if (i2c_dp >= _I2C_MAX_DATAREAD_) i2c_dp = 0;
if(ack==ACK)
{
i2c_master_read_byte(cmd, &i2c_data[i2c_dp], ACK_VAL);
i = i2c_dp;
i2c_dp++;
}
else
{
i2c_master_read_byte(cmd, &i2c_data[i2c_dp], NACK_VAL);
i = i2c_dp;
i2c_dp = 0;
}
return i; // ret Anzahl Index auf Datenpuffer
}
#endif
// Ab hier Soft-I2C
#ifdef _SOFT_I2C_
//Globale Definitionen
// ... für I2C-Funktionen
#define SDA 21
#define SCL 22
// ****************************************************************************
// I2C- Routinen zur Ansteuerung eins I2C-Slaves
// I2C-Bus-Funktionen i2c_init, i2c_start, i2c_stop, i2c_write, i2c_read
// ****************************************************************************
// **** Zeitverzögerung zur Verlangsamung der Datenübertragungsrate ***********
// **** i=2 bis i=100 wählen je nach I2C-IC und Pull-Up-Widerstand
void i2c_delay(void)
{
volatile uint8_t i; // i nicht rausoptimieren (volatile)!!
for (i=25;i!=0;i--);
}
// ****************************************************************************
// ****** Initialiserung I2C-Bus **********************************************
void i2c_init (void)
{ // Leitungen in den Grundzustand High
pinMode(SDA,OUTPUT);
digitalWrite(SDA,HIGH);
pinMode(SCL,OUTPUT);
digitalWrite(SCL,HIGH);
i2c_delay();
}
// ****** Startbedingung I2C-Bus **********************************************
void i2c_start (void)
{
//pinMode(SDA,OUTPUT);
digitalWrite(SDA,HIGH);
digitalWrite(SCL,HIGH);
i2c_delay();
digitalWrite(SDA,LOW);
i2c_delay();
digitalWrite(SCL,LOW);
i2c_delay();
}
//****** Stoppbedingung I2C-Bus ***********************************************************
void i2c_stop (void)
{
//pinMode(SDA,OUTPUT);
digitalWrite(SDA,LOW);
digitalWrite(SCL,HIGH);
i2c_delay();
digitalWrite(SDA,HIGH);
i2c_delay();
}
//*****************************************************************************************
// * Byte ausgeben an I2C-Bus , Rückgabewert = ack = ACK/NACK
// * msb-first
// ****************************************************************************************
uint8_t i2c_write (uint8_t value)
{
uint8_t z; // Zähler
uint8_t ack; // Acknoledge-Bit
//pinMode(SDA,OUTPUT);
for (z = 8; z != 0; z --) // Zähler: serielle Ausgabe von 8 Bit
{
if ((value & 0x80) == 0x80) // Ausgabe: MSB first
digitalWrite(SDA,HIGH); // SDA = 1;
else
digitalWrite(SDA,LOW); // SDA = 0;
value <<=1; // nächstes Bit!
i2c_delay();
digitalWrite(SCL,HIGH); // SCL = 1 Daten sind gültig
i2c_delay();
digitalWrite(SCL,LOW); // SCL = 0 Datenausabe beendet
i2c_delay();
}
digitalWrite(SDA,HIGH); // SDA = 1 Leitung freigeben für Acknoledge-Bit
i2c_delay();
digitalWrite(SCL,HIGH); // SCL = 1 Slave kann bestätigen
i2c_delay(); // warten
pinMode(SDA,INPUT);
ack = digitalRead(SDA);
i2c_delay();
digitalWrite(SCL,LOW); // SCL = 0 Slave soll Ackn-Ausgabe beenden
i2c_delay();
pinMode(SDA,OUTPUT);
return ack; // Acknoledge-Bit ist Rückgabewert der Funktion
// ack = 0 bedeutet Slave hat "verstanden" !!!!!!
}
//*****************************************************************************************
// * Byte einlesen vom I2C-Bus.
// ****************************************************************************************
uint8_t i2c_read (uint8_t ack)
{
uint8_t z, value = 0;
digitalWrite(SDA,HIGH); // SDA = 1 Leitung freigeben (high) für Daten
pinMode(SDA,INPUT);
for (z = 8; z != 0; z--) // Zähler: serielles Einlesen von 8 Bit
{
digitalWrite(SCL,HIGH); // SCL = 1 Daten sind gültig
i2c_delay();
value <<= 1;
value |= digitalRead(SDA); //SDA Datenbit in Puffer
i2c_delay();
digitalWrite(SCL,LOW); // SCL = 0 Daten lesen beendet
i2c_delay();
}
pinMode(SDA,OUTPUT);
digitalWrite(SDA,ACK); // Acknoledge-Bit
i2c_delay();
digitalWrite(SCL,HIGH); // SCL = 1 Ackn. gültig
//I2C_PORT |= (1<<SCL);
i2c_delay();
digitalWrite(SCL,LOW); // SCL = 0 Ackn. beendet
i2c_delay();
digitalWrite(SDA,LOW); // SDA = 0 Leitung SDA vorbereiten für Stoppbed.
i2c_delay();
return ( value ); // eingelesenes Byte = Rückgabewert
}
#endif
// ****************************************************************************
// RS232-Routinen zur Kommunikation mit PC-Terminal
//
// ****************************************************************************
void rs232_init(void)
{
rs232_baud(BAUD);
}
void rs232_baud ( uint32_t baud ) // Ändert die Baudrate
{
#if defined(_SERIAL0_)
Serial.end();
delay_ms(20);
Serial.begin(baud);
while (!Serial);
#elif defined(_SERIAL1_)
Serial1.end();
delay_ms(20);
Serial1.begin(baud);
while (!Serial1);
#elif defined(_SERIALBT_)
SerialBT.end();
delay_ms(20);
SerialBT.begin(_DEVICENAME_);
while (!SerialBT);
#endif
}
uint8_t rs232_get ( void )
{
#if defined(_SERIAL0_)
if(Serial.available() > 0)
return Serial.read();
else return 0; // Wenn kein Zeichen im Puffer, dann 0 zurückgeben!
#elif defined(_SERIAL1_)
if(Serial1.available() > 0)
return Serial1.read();
else return 0; // Wenn kein Zeichen im Puffer, dann 0 zurückgeben!
#elif defined(_SERIALBT_)
if(SerialBT.available() > 0)
return SerialBT.read();
else return 0; // Wenn kein Zeichen im Puffer, dann 0 zurückgeben!
#endif
}
void rs232_put ( uint8_t value )
{
#if defined(_SERIAL0_)
Serial.write(value);
#elif defined(_SERIAL1_)
Serial1.write(value);
#elif defined(_SERIALBT_)
SerialBT.write(value);
#endif
}
void rs232_print ( const char *text )
{
while (*text != '\0')
rs232_put(*text++);
}
// RS232-Erweiterungen sind nicht Teil der Technischen Richtlinie FA205!!
// Erzeugt ein Eingabeprompt am Terminal 00 .. 99
uint8_t rs232_inputdd(void )
{
uint8_t buf1,buf2;
do
{
while((buf1 = rs232_get()) == 0);
} while ((buf1 < '0') || (buf1 > '9'));
rs232_put(buf1); // Echo
do
{
while((buf2 = rs232_get()) == 0);
} while ((buf1 < '0') || (buf1 > '9'));
rs232_put(buf2); // Echo
buf1 -= '0';
buf2 -= '0';
return (buf1*10 + buf2);
}
// Ausgabe einer Dezimalzahl 00..99 auf RS232
void rs232_printdd(uint8_t value)
{
uint8_t buf;
buf = value / 10;
rs232_put(buf+'0'); // 10er-Stelle anzeigen
buf = value % 10;
rs232_put(buf+'0'); // 1er-Stelle anzeigen
}
void rs232_byte(uint8_t val)
{
uint8_t buffer[3];
uint8_t n = 0;
do
{
buffer[n++] = val%10 + '0';
} while ((val /= 10) > 0);
while (n<3) // Rest von buffer mit blank füllen
{
buffer[n++] = ' ';
}
while (n > 0) // Ausgabe auf das Display (umgekehrt)
{
n--;
rs232_put(buffer[n]);
}
}
void rs232_int(uint16_t val)
{
uint8_t buffer[5];
uint8_t n = 0;
do
{
buffer[n++] = val%10 + '0';
} while ((val /= 10) > 0);
while (n<5) // Rest von buffer mit blank f�llen
{
buffer[n++] = ' ';
}
while (n > 0) // Ausgabe auf das Display (umgekehrt)
{
n--;
rs232_put(buffer[n]);
}
}
uint8_t rs232_binary_get ( void )
{
while (!rs232_is_received()); // warten, bis Byte im Puffer!
#ifdef _SERIAL1_
return Serial1.read(); // wert zurückgeben
#else
return Serial.read(); // wert zurückgeben
#endif
}
uint8_t rs232_readbytes(uint8_t *buf, int8_t _length)
{
uint16_t timeout;
uint8_t i = 0;
//Puffer löschen
for (uint8_t n=0; n<=_length;n++) buf[n] = 0;
while(_length > 0)
{
timeout = 0;
while(!rs232_is_received())
{
if(timeout>=0xfffe) return 0;
else timeout++;
}
*buf++ = rs232_binary_get();
_length--;
i++;
}
return i;
}
uint8_t rs232_is_received( void )
{
#ifdef _SERIAL1_
if (Serial1.available()<0) return 0; // 0, wenn kein Byte im Puffer!
#else
if (Serial.available()<0) return 0; // 0, wenn kein Byte im Puffer!
#endif
return 1; // 1, wenn ein Byte im Puffer
}