Repository navigation
Expand file tree
/
Copy pathusbiss.c
More file actions
1366 lines (1266 loc) · 47.8 KB
/
Copy pathusbiss.c
File metadata and controls
1366 lines (1266 loc) · 47.8 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
@copyright : Siemens AG
@license : GPLv3
@author : Andreas Kaeberlein
@address : Clemens-Winkler-Strasse 3, 09116 Chemnitz
@maintainer : Andreas Kaeberlein
@telephone : +49 371 4810-2108
@email : andreas.kaeberlein@siemens.com
@file : usbiss.c
@date : 2023-06-29
@see : https://github.com/andkae/usbiss
@see : http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm
@brief : USB-ISS driver
Driver for USB-ISS:
* I2C access functions
***********************************************************************/
/** Includes **/
/* Standard libs */
#include <stdio.h> // f.e. printf
#include <stdlib.h> // defines four variables, several macros,
// and various functions for performing
// general functions
#include <stdint.h> // defines fixed data types, like int8_t...
#include <string.h> // string handling functions
#include <strings.h> // strcasecmp
#include <stdarg.h> // variable parameter list
/** Custom Libs **/
#include "simple_uart.h" // cross platform UART driver
/** self **/
#include "usbiss.h" // some defs
/**
* @defgroup FALL_THROUGH
*
* @brief Fallthrough
*
* Defines fallthrough only for newer compiler
* avoids warning 'error: empty declaration __attribute__((fallthrough))'
*
* @since 2023-01-07
* @see https://stackoverflow.com/questions/45349079/how-to-use-attribute-fallthrough-correctly-in-gcc
*/
#if defined(__GNUC__) && __GNUC__ >= 7
#define FALL_THROUGH __attribute__ ((fallthrough))
#else
#define FALL_THROUGH ((void)0)
#endif /* __GNUC__ >= 7 */
/** @} */ // FALL_THROUGH
/**
* @brief UART Write
*
* Write to UART port
*
* @param[in,out] *self common handle #t_usbiss
* @param[in] data data array to write
* @param[in] len number of bytes to write
* @return uint32_t number of written bytes
* @since July 18, 2023
* @author Andreas Kaeberlein
*/
static uint32_t usbiss_uart_write( t_usbiss *self, void* data, uint32_t len )
{
/** Variables **/
uint32_t r = 0;
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* UART Write */
r = (uint32_t) simple_uart_write(self->uart, data, (size_t) len);
/* flush buffer */
if ( 0 != simple_uart_flush(self->uart) ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" WARN:%s: flush failed\n", __FUNCTION__);
}
}
/* function finish */
return r;
}
/**
* @brief UART read
*
* Read from UART port
*
* @param[in,out] *self common handle #t_usbiss
* @param[in] data array with read data
* @param[in] len requested number of bytes
* @return uint32_t number of read bytes
* @since July 20, 2023
* @author Andreas Kaeberlein
*/
static uint32_t usbiss_uart_read( t_usbiss *self, void* data, uint32_t len )
{
/** Variables **/
uint32_t r = 0; // number of recieved bytes
ssize_t i = 0; // help variable for return code of uart
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* check for zero request */
if ( 0 == len ) {
return 0;
}
/* read until number of required bytes are captured */
while ( r < len ) {
i = simple_uart_read(self->uart, data+r, (size_t) (len-r)); // request number of bytes
if ( i < 0 ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:READ: UART read failed ero=0x%zx\n", __FUNCTION__, i);
}
return r; // release number of captured bytes until error
}
r = r + ((uint32_t) i);
}
/* function finish */
return r;
}
/**
* @brief Read data available
*
* Outputs number of availbale read bytes on the UART
*
* @param[in,out] *self common handle #t_usbiss
* @return uint32_t number of available UART read bytes
* @since August 10, 2023
* @author Andreas Kaeberlein
*/
static uint32_t usbiss_uart_read_avail( t_usbiss *self )
{
/** Variables **/
int cnt;
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* acquire avail data count */
cnt = simple_uart_has_data(self->uart);
if ( cnt < 0 ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Failed to acquire number of available read bytes, ero=0x%x\n", __FUNCTION__, cnt);
}
return 0;
}
return (uint32_t) cnt;
}
/**
* usbiss_uint8_to_str
* convert to ascii hex
*/
static void usbiss_uint8_to_asciihex( char *buf, uint32_t buflen, uint8_t *dat, uint32_t datlen )
{
/* spave available? */
if ( 0 == buflen ) {
return;
}
/* make empty */
memset(buf, 0, buflen);
/* convert */
for ( uint32_t i = 0; i < datlen; i++ ) {
if ( !((strlen(buf) + 3) < buflen) ) { // not enough memory
break;
}
sprintf(buf+3*i, "%02x ", dat[i]);
}
buf[strlen(buf)-1] = '\0'; // delete last ' '
}
/**
* mode-to-str
* converts USBISS mode to human readable string
*/
static int usbiss_human_to_mode(const char *str, uint8_t *val)
{
if ( 0 == strcasecmp(str, "IO_MODE") ) {
*val = (uint8_t) USBISS_IO_MODE;
return 0;
} else if ( 0 == strcasecmp(str, "IO_CHANGE") ) {
*val = (uint8_t) USBISS_IO_CHANGE;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_S_20KHZ") ) {
*val = (uint8_t) USBISS_I2C_S_20KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_S_50KHZ") ) {
*val = (uint8_t) USBISS_I2C_S_50KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_S_100KHZ") ) {
*val = (uint8_t) USBISS_I2C_S_100KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_S_400KHZ") ) {
*val = (uint8_t) USBISS_I2C_S_400KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_H_100KHZ") ) {
*val = (uint8_t) USBISS_I2C_H_100KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_H_400KHZ") ) {
*val = (uint8_t) USBISS_I2C_H_400KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "I2C_H_1000KHZ") ) {
*val = (uint8_t) USBISS_I2C_H_1000KHZ;
return 0;
} else if ( 0 == strcasecmp(str, "SPI_MODE") ) {
*val = (uint8_t) USBISS_SPI_MODE;
return 0;
} else if ( 0 == strcasecmp(str, "SERIAL") ) {
*val = (uint8_t) USBISS_SERIAL;
return 0;
}
/* unsupported input */
*val = (uint8_t) __UINT8_MAX__;
return -1;
}
/**
* @brief UART Freeing
*
* Reads until UART recieve queue is empty
*
* @param[in,out] *self common handle #t_usbiss
* @return uint32_t number of read bytes from uart to free the adapter
* @since August 9, 2023
* @author Andreas Kaeberlein
*/
static uint32_t usbiss_uart_free( t_usbiss *self )
{
/** Variables **/
uint32_t cnt;
uint8_t dat[4];
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* fetch until no data */
cnt = 0;
while ( 0 != usbiss_uart_read_avail(self) ) {
usbiss_uart_read(self, dat, 1);
++cnt;
}
/* function finish */
return cnt;
}
/**
* @brief I2C Startbit
*
* sends I2C startbit and slave address with direction
*
* @param[in,out] *self common handle #t_usbiss
* @param[in] adr8 7Bit Slave Adress + 1Bit Direction (Read/Write)
* @return int
* @retval 0 OK
* @retval -1 FAIL
* @since July 10, 2023
* @author Andreas Kaeberlein
*/
static int usbiss_i2c_startbit( t_usbiss *self, uint8_t adr8 )
{
/** Variables **/
uint8_t uint8Wr[4]; // write buffer: DIRECT + START + WRITE + 16Bytes + STOP
uint8_t uint8Rd[2]; // read buffer
uint32_t uint32RdLen; // number of read bytes from UART
char charBuf[16]; // help buffer for debug outputs
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* Assemble START + ADR packet */
uint8Wr[0] = USBISS_I2C_DIRECT; // USBISS direct mode
uint8Wr[1] = USBISS_I2C_START; // START-Bit
uint8Wr[2] = (uint8_t) (USBISS_I2C_WRITE); // only one address byte written
uint8Wr[3] = adr8; // i2c address + Direction (Read/Write)
if ( 4 != usbiss_uart_write(self, uint8Wr, 4) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, 4); // convert to ascii
printf(" ERROR:%s:REQ: %s\n", __FUNCTION__, charBuf);
}
return -1;
}
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) 4); // convert to ascii
printf(" INFO:%s:START:REQ: %s\n", __FUNCTION__, charBuf);
}
uint32RdLen = usbiss_uart_read(self, uint8Rd, 2);
if ( 2 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unexpected number of %i bytes received\n", __FUNCTION__, uint32RdLen);
}
return -1;
}
if ( USBISS_CMD_ACK != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Start bit rejected, %s, ero=0x%02x\n", __FUNCTION__, usbiss_ero_str(uint8Rd[1]), uint8Rd[1]);
}
return (int) (uint8Rd[1]); // USBISS error code, #USBISS_ERROR
}
/* function finish */
return 0;
}
/**
* @brief I2C Restartbit
*
* Sends restart bit in established i2c connection, needed for direction change
*
* @param[in,out] *self common handle #t_usbiss
* @param[in] adr8 7Bit Slave Adress + 1Bit Direction (Read/Write)
* @return int
* @retval 0 OK
* @retval -1 FAIL
* @since July 11, 2023
* @author Andreas Kaeberlein
*/
static int usbiss_i2c_restartbit( t_usbiss *self, uint8_t adr8 )
{
/** Variables **/
uint8_t uint8Wr[4]; // write buffer: DIRECT + START + WRITE + 16Bytes + STOP
uint8_t uint8Rd[2]; // read buffer
uint32_t uint32RdLen; // number of read bytes from UART
char charBuf[16]; // help buffer for debug outputs
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* Assemble START + ADR packet */
uint8Wr[0] = USBISS_I2C_DIRECT; // USBISS direct mode
uint8Wr[1] = USBISS_I2C_RESTART; // RESTART-BIT
uint8Wr[2] = (uint8_t) (USBISS_I2C_WRITE); // only one address byte written
uint8Wr[3] = adr8; // i2c address + Direction (Read/Write)
if ( 4 != usbiss_uart_write(self, uint8Wr, 4) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, 4); // convert to ascii
printf(" ERROR:%s:REQ: %s\n", __FUNCTION__, charBuf);
}
return -1;
}
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) 4); // convert to ascii
printf(" INFO:%s:START:REQ: %s\n", __FUNCTION__, charBuf);
}
uint32RdLen = usbiss_uart_read(self, uint8Rd, 2);
if ( 2 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unexpected number of %i bytes received\n", __FUNCTION__, uint32RdLen);
}
return -1;
}
if ( USBISS_CMD_ACK != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Start bit rejected, %s, ero=0x%02x\n", __FUNCTION__, usbiss_ero_str(uint8Rd[1]), uint8Rd[1]);
}
return (int) (uint8Rd[1]); // USBISS error code, #USBISS_ERROR
}
/* function finish */
return 0;
}
/**
* @brief I2C Stopbit
*
* sends I2C stopbit
*
* @param[in,out] *self common handle #t_usbiss
* @return int
* @retval 0 OK
* @retval -1 FAIL
* @since July 10, 2023
* @author Andreas Kaeberlein
*/
static int usbiss_i2c_stopbit( t_usbiss *self )
{
/** Variables **/
uint8_t uint8Wr[2]; // write buffer: DIRECT + START + WRITE + 16Bytes + STOP
uint8_t uint8Rd[2]; // read buffer
uint32_t uint32RdLen; // number of read bytes from terminal
char charBuf[16]; // help buffer for debug outputs
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
uint8Wr[0] = USBISS_I2C_DIRECT; // USBISS direct mode
uint8Wr[1] = USBISS_I2C_STOP; // STOP-Bit
if ( 2 != usbiss_uart_write(self, uint8Wr, 2) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, 2); // convert to ascii
printf(" ERROR:%s:REQ: %s\n", __FUNCTION__, charBuf);
}
return -1;
}
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) 4); // convert to ascii
printf(" INFO:%s:STOP:REQ: %s\n", __FUNCTION__, charBuf);
}
uint32RdLen = usbiss_uart_read(self, uint8Rd, 2);
if ( 2 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unexpected number of %i bytes received\n", __FUNCTION__, uint32RdLen);
}
return -1;
}
if ( USBISS_CMD_ACK != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Stop bit rejected, %s, ero=0x%02x\n", __FUNCTION__, usbiss_ero_str(uint8Rd[1]), uint8Rd[1]);
}
return (int) (uint8Rd[1]); // USBISS error code, #USBISS_ERROR
}
/* function finish */
return 0;
}
/**
* @brief I2C data write
*
* brings only data bytes to I2C lines, Start/Stop bit needs to asserted by dedicated function
*
* @param[in,out] *self common handle #t_usbiss
* @param[in] data data array
* @param[in] len number of bytes in data
* @return int
* @retval 0 OK
* @retval -1 FAIL
* @since July 10, 2023
* @author Andreas Kaeberlein
*/
static int usbiss_i2c_data_wr ( t_usbiss *self, void* data, size_t len )
{
/** Variables **/
uint8_t uint8Wr[32]; // write buffer: DIRECT + START + WRITE + 16Bytes + STOP
uint8_t uint8Rd[4]; // read buffer
size_t bytesPend; // number of pending bytes
size_t dataOfs; // byte offset of current data
uint8_t uint8Chunk; // number of data bytes at current chunk
uint32_t uint32RdLen; // number of read bytes from terminal
char charBuf[256]; // help buffer for debug outputs
int intRet; // internal return code, allows to send stop bit in case of crash
size_t iter; // loop count
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* empty frame provided */
if ( 0 == len ) {
return 0;
}
/* Data Packets to line */
intRet = 0;
iter = 0;
dataOfs = 0; // next data packet start field
bytesPend = len; // all bytes pending
while ( bytesPend > 0 ) {
/* calc payload size */
uint8Chunk = (uint8_t) usbiss_min((size_t) USBISS_I2C_CHUNK, bytesPend); // calculate max number of bytes to send, -1 through i2c adr
/* assemble packet */
uint8Wr[0] = USBISS_I2C_DIRECT; // USBISS direct mode
uint8Wr[1] = (uint8_t) (USBISS_I2C_WRITE + uint8Chunk - 1);
memcpy(uint8Wr+2, data+dataOfs, uint8Chunk);
/* request i2c packet transfer */
if ( ((uint32_t) (uint8Chunk + 2)) != usbiss_uart_write(self, uint8Wr, (uint32_t) (uint8Chunk + 2)) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) (uint8Chunk + 2)); // convert to ascii
printf(" ERROR:%s:PKG=%zi:OFS=0x%zx:REQ: %s\n", __FUNCTION__, iter, dataOfs, charBuf);
}
intRet = -1;
break;
}
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) (uint8Chunk + 2)); // convert to ascii
printf(" INFO:%s:PKG=%zi:OFS=0x%zx:REQ: %s\n", __FUNCTION__, iter, dataOfs, charBuf);
}
/* check response */
uint32RdLen = usbiss_uart_read(self, uint8Rd, 2);
if ( 2 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi:RSP: Unexpected number of %i bytes received\n", __FUNCTION__, iter, uint32RdLen);
}
intRet = -1;
break;
}
if ( USBISS_CMD_ACK != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi: packet rejected, %s, ero=0x%02x\n", __FUNCTION__, iter, usbiss_ero_str(uint8Rd[1]), uint8Rd[1]);
}
intRet = (int) (uint8Rd[1]); // USBISS error code
break;
}
/* prepare next cycle */
dataOfs = dataOfs + ((size_t) uint8Chunk); // update data pointer
bytesPend = bytesPend - uint8Chunk; // update count of pending bytes
iter++;
}
/* finish function */
return intRet;
}
/**
* @brief I2C data read
*
* brings only data bytes to I2C lines, Start/Stop bit needs to asserted by dedicated function
*
* @param[in,out] *self common handle #t_usbiss
* @param[out] data data array
* @param[in,out] len number of requested bytes as in, and recieved bytes as out
* @return int
* @retval 0 OK
* @retval -1 FAIL
* @since July 10, 2023
* @author Andreas Kaeberlein
*/
static int usbiss_i2c_data_rd ( t_usbiss *self, void* data, size_t len )
{
/** variables **/
uint8_t uint8Wr[4]; // write buffer: DIRECT + START + WRITE + 16Bytes + STOP
uint8_t uint8Rd[32]; // read buffer
size_t ackBytesPend; // number of pending bytes with ACK
size_t dataOfs; // data pointer in array
uint8_t uint8Chunk; // number of data bytes at current chunk
uint32_t uint32RdLen; // number of read bytes from terminal
size_t iter; // loop count
char charBuf[256]; // help buffer for debug outputs
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* empty frame provided */
if ( 0 == len ) {
return 0;
}
/* request ACK Bytes, last byte need NCK */
dataOfs = 0;
iter = 0;
if ( len > 1 ) {
ackBytesPend = len - 1;
while ( ackBytesPend > 0 ) {
/* calc number of requested bytes size */
uint8Chunk = (uint8_t) usbiss_min((size_t) USBISS_I2C_CHUNK, ackBytesPend); // calculate max number of bytes to send, -1 through i2c adr
/* assemble request */
uint8Wr[0] = USBISS_I2C_DIRECT; // USBISS direct mode
uint8Wr[1] = (uint8_t) (USBISS_I2C_READ + uint8Chunk - 1);
/* request i2c packet transfer */
if ( 2 != usbiss_uart_write(self, uint8Wr, 2) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) 2); // convert to ascii
printf(" ERROR:%s:PKG=%zi:OFS=0x%zx:REQ: %s\n", __FUNCTION__, iter, dataOfs, charBuf);
}
return -1;
}
/* UART data read: get status bytes */
uint32RdLen = usbiss_uart_read(self, uint8Rd, 2);
if ( 2 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi:RSP:STATUS: Unexpected number of %i instead %i bytes received\n", __FUNCTION__, iter, uint32RdLen, 2);
}
return -1;
}
/* check response byte */
if ( USBISS_CMD_ACK != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi: packet rejected, %s, ero=0x%02x\n", __FUNCTION__, iter, usbiss_ero_str(uint8Rd[1]), uint8Rd[1]);
}
return (int) (uint8Rd[1]); // USBISS error code
}
/* check byte count */
if ( uint8Chunk != uint8Rd[1] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi: wrong data count recieved, exp=%i, is=%i\n", __FUNCTION__, iter, uint8Chunk, uint8Rd[1]);
}
usbiss_uart_free(self); // clean buffer
return -1;
}
/* UART data read: get payload with data */
uint32RdLen = usbiss_uart_read(self, uint8Rd+2, (uint32_t) (uint8Chunk));
if ( ((uint32_t) uint8Chunk) != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi:RSP:DATA: Unexpected number of %i instead %i bytes received\n", __FUNCTION__, iter, uint32RdLen, uint8Chunk);
}
return -1;
}
/* user message */
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Rd, (uint32_t) (uint8Chunk + 2)); // convert to ascii
printf(" INFO:%s:PKG=%zi:OFS=0x%zx:RSP: %s\n", __FUNCTION__, iter, dataOfs, charBuf);
}
/* fill data in result variable */
memcpy(data+dataOfs, uint8Rd+2, uint8Chunk);
/* prepare next cycle */
dataOfs = dataOfs + ((size_t) uint8Chunk); // update data pointer
ackBytesPend = ackBytesPend - uint8Chunk; // update count of pending bytes
iter++;
}
}
/* request last byte, NCK */
uint8Wr[0] = USBISS_I2C_DIRECT; // USBISS direct mode
uint8Wr[1] = USBISS_I2C_NCK; // read with NCK
uint8Wr[2] = USBISS_I2C_READ; // read one byte
/* issues transfer */
if ( 3 != usbiss_uart_write(self, uint8Wr, 3) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, (uint32_t) 3); // convert to ascii
printf(" ERROR:%s:PKG=%zi:OFS=0x%zx:REQ: %s\n", __FUNCTION__, iter, dataOfs, charBuf);
}
return -1;
}
/* UART data read: get status bytes */
uint32RdLen = usbiss_uart_read(self, uint8Rd, 2);
if ( 2 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi:RSP:STATUS Unexpected number of %i instead %i bytes received\n", __FUNCTION__, iter, uint32RdLen, 2);
}
return -1;
}
/* check response byte */
if ( USBISS_CMD_ACK != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi: packet rejected, %s, ero=0x%02x\n", __FUNCTION__, iter, usbiss_ero_str(uint8Rd[1]), uint8Rd[1]);
}
return (int) (uint8Rd[1]); // USBISS error code
}
/* check byte count */
if ( 1 != uint8Rd[1] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi: wrong data count recieved, exp=%i, is=%i\n", __FUNCTION__, iter, 1, uint8Rd[1]);
}
usbiss_uart_free(self); // clean buffer
return -1;
}
/* UART data read: get payload with data */
uint32RdLen = usbiss_uart_read(self, uint8Rd+2, 1);
if ( 1 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s:PKG=%zi:RSP:DATA: Unexpected number of %i instead %i bytes received\n", __FUNCTION__, iter, uint32RdLen, 1);
}
return -1;
}
/* user message */
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Rd, 3); // convert to ascii
printf(" INFO:%s:PKG=%zi:OFS=0x%zx:RSP: %s\n", __FUNCTION__, iter, dataOfs, charBuf);
}
/* fill data in result variable */
memcpy(data+dataOfs, uint8Rd+2, 1);
/* finish function */
return 0;
}
/**
* mode-to-human
* converts USBISS mode to human readable string
*/
char *usbiss_mode_to_human(uint8_t mode)
{
static char str[16];
switch(mode) {
case USBISS_IO_MODE: strncpy(str, "IO_MODE", sizeof(str)); break;
case USBISS_IO_CHANGE: strncpy(str, "IO_CHANGE", sizeof(str)); break;
case USBISS_I2C_S_20KHZ: strncpy(str, "I2C_S_20KHZ", sizeof(str)); break;
case USBISS_I2C_S_50KHZ: strncpy(str, "I2C_S_50KHZ", sizeof(str)); break;
case USBISS_I2C_S_100KHZ: strncpy(str, "I2C_S_100KHZ", sizeof(str)); break;
case USBISS_I2C_S_400KHZ: strncpy(str, "I2C_S_400KHZ", sizeof(str)); break;
case USBISS_I2C_H_100KHZ: strncpy(str, "I2C_H_100KHZ", sizeof(str)); break;
case USBISS_I2C_H_400KHZ: strncpy(str, "I2C_H_400KHZ", sizeof(str)); break;
case USBISS_I2C_H_1000KHZ: strncpy(str, "I2C_H_1000KHZ", sizeof(str)); break;
case USBISS_SPI_MODE: strncpy(str, "SPI_MODE", sizeof(str)); break;
case USBISS_SERIAL: strncpy(str, "SERIAL", sizeof(str)); break;
default: strncpy(str, "UNKNOWN", sizeof(str)); break;
}
return str;
}
/**
* mode-to-human
* converts USBISS mode to human readable string
*/
char *usbiss_ero_str(uint8_t error)
{
static char str[64];
switch(error) {
case USBISS_ERO_ID1: strncpy(str, USBISS_ERO_ID1_STR, sizeof(str)); break;
case USBISS_ERO_ID2: strncpy(str, USBISS_ERO_ID2_STR, sizeof(str)); break;
case USBISS_ERO_ID3: strncpy(str, USBISS_ERO_ID3_STR, sizeof(str)); break;
case USBISS_ERO_ID4: strncpy(str, USBISS_ERO_ID4_STR, sizeof(str)); break;
case USBISS_ERO_ID5: strncpy(str, USBISS_ERO_ID5_STR, sizeof(str)); break;
case USBISS_ERO_ID6: strncpy(str, USBISS_ERO_ID6_STR, sizeof(str)); break;
case USBISS_ERO_ID7: strncpy(str, USBISS_ERO_ID7_STR, sizeof(str)); break;
default: strncpy(str, "UNKNOWN", sizeof(str)); break;
}
return str;
}
/**
* usbiss_is_i2c_mode
* check if USB-ISS is configured for I2C mode
*/
int usbiss_is_i2c_mode( uint8_t mode )
{
/* build mode set frame */
switch ( mode ) {
/* I2C Modes */
case USBISS_I2C_S_20KHZ: FALL_THROUGH;
case USBISS_I2C_S_50KHZ: FALL_THROUGH;
case USBISS_I2C_S_100KHZ: FALL_THROUGH;
case USBISS_I2C_S_400KHZ: FALL_THROUGH;
case USBISS_I2C_H_100KHZ: FALL_THROUGH;
case USBISS_I2C_H_400KHZ: FALL_THROUGH;
case USBISS_I2C_H_1000KHZ: return 0;
}
return -1;
}
/**
* usbiss_list_uart
* List suitable port for USB-ISS connection
*/
int usbiss_list_uart( char *uart, size_t len, const char sep[] )
{
/** Variables **/
char **names;
ssize_t numTTY;
int numUsbIssTTY = 0;
char decription[1024]; // uart port description
char chrPid[5]; // product id
char chrVid[5]; // vendor id
char* pHelp; // help pointer
/* make empty */
uart[0] = '\0';
/* list UART ports and build */
numTTY = simple_uart_list(&names); // get UART ports from system
/* no ports, exit */
if ( 0 == numTTY ) {
return 0;
}
/* find USB-ISS matching */
for (ssize_t i = 0; i < numTTY; i++) {
#if defined(__linux__)
if ( NULL == strstr(names[i], "ttyACM") ) { // in linux has USB-ISS the uart port ttyACM*
continue;
}
# endif
/* accquire port description
* f.e. Linux: manufacturer='Devantech Ltd.',product='USB-ISS.',PID=ffee,VID=04d8,serial=00060147
*/
if ( 0 == simple_uart_describe(names[i], decription, sizeof(decription)) ) {
/* init */
chrPid[0] = '\0';
chrVid[0] = '\0';
/* get PID */
pHelp = strstr(decription, "PID=");
if ( (NULL != pHelp) && (!(strlen(pHelp) < 8)) ) {
strncpy(chrPid, pHelp + 4, 4);
chrPid[4] = '\0';
}
/* get VID */
pHelp = strstr(decription, "VID=");
if ( (NULL != pHelp) && (!(strlen(pHelp) < 8)) ) {
strncpy(chrVid, pHelp + 4, 4);
chrVid[4] = '\0';
}
/* VID/PID match? */
if ( (0 == strcasecmp(chrVid, USBISS_VCP_VID)) && (0 == strcasecmp(chrPid, USBISS_VCP_PID)) ) {
snprintf(uart+strlen(uart), len-strlen(uart), "%s%s", names[i], sep);
++numUsbIssTTY;
}
}
}
/* free memory */
for ( ssize_t i = 0; i < numTTY; i++) {
if ( names[i] ) {
free(names[i]);
}
}
free(names);
/* drop last separator */
if ( strlen(sep) < strlen(uart) ) {
uart[strlen(uart)-strlen(sep)] = '\0';
}
/* finish */
return numUsbIssTTY;
}
/**
* usbiss_init
* initializes common data structure
*/
int usbiss_init( t_usbiss *self )
{
/* init variable */
self->uint8MsgLevel = 0; // suppress all outputs
self->uint32BaudRate = USBISS_UART_BAUD_RATE; // default baudrate
self->uint8Fw = 0; // firmware version
self->uint8Mode = 0; // transfer mode
self->uint8IsOpen = 0; // not open
(self->charPort)[0] = '\0'; // no port provided
/* graceful end */
return 0;
}
/**
* usbiss_set_verbose
* set verbose level
*/
void usbiss_set_verbose( t_usbiss *self, uint8_t verbose )
{
self->uint8MsgLevel = verbose;
}
/**
* usbiss_open
* open handle to USBISS and checks ID + serial read
*/
int usbiss_open( t_usbiss *self, char* port, uint32_t baud )
{
/** variable **/
uint8_t uint8Wr[16]; // write buffer
uint8_t uint8Rd[16]; // read buffer
uint32_t uint32RdLen; // length of read buffer
char charBuf[16]; // string buffer
char charUartAuto[256]; // autodedect USB-ISS uart port
char* charPtrFirstUart; // pointer to first UART port
int intNumUarts; // number of UART ports in system
/* Function Call Message */
if ( 0 != self->uint8MsgLevel ) { printf("__FUNCTION__ = %s\n", __FUNCTION__); };
/* no autodetect path provided? */
if ( '\0' != port[0] ) {
if ( strlen(port) > (sizeof(self->charPort) - 1) ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: UART port path too long.\n", __FUNCTION__);
}
return -1;
}
strncpy(self->charPort, port, sizeof(self->charPort));
} else { // consider first found UART device in system as USB-ISS uart port
/* get available USB-ISS suitable ports from system */
intNumUarts = usbiss_list_uart(charUartAuto, sizeof(charUartAuto), " ");
/* select Port */
switch (intNumUarts) {
/* no UART */
case 0:
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: no USB-ISS suitable port found.\n", __FUNCTION__);
}
strncpy(self->charPort, "ERROR: no USB-ISS uart found", sizeof(self->charPort)); // caller can distinguish the reason
return -1;
break;
/* one UART */
case 1:
if ( strlen(charUartAuto) > (sizeof(self->charPort) - 1) ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: UART port path too long.\n", __FUNCTION__);
}
return -1;
}
strncpy(self->charPort, charUartAuto, sizeof(self->charPort)); // copy uart path
break;
/* multiple UART ports */
default:
charPtrFirstUart = strtok(charUartAuto, " "); // split list of UART ports and choose first
if ( strlen(charPtrFirstUart) > (sizeof(self->charPort) - 1) ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: UART port path too long.\n", __FUNCTION__);
}
return -1;
}
strncpy(self->charPort, charPtrFirstUart, sizeof(self->charPort));
break;
}
}
if ( 0 != self->uint8MsgLevel ) { // user message
printf(" INFO:%s: selected UART: %s\n", __FUNCTION__, self->charPort);
}
/* non default baudrate */
if ( 0 != baud ) {
switch (baud) {
case 9600: break;
case 14400: break;
case 19200: break;
case 38400: break;
case 57600: break;
case 115200: break;
case 230400: break;
default:
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unsupported baudrate %i.\n", __FUNCTION__, baud);
}
return -1;
}
self->uint32BaudRate = baud;
}
/* open uart port */
self->uart = simple_uart_open(self->charPort, (int) self->uint32BaudRate, "8N1");
if (!(self->uart)) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: error to open uart port %s with %i baud\n", __FUNCTION__, self->charPort, self->uint32BaudRate);
}
return -1;
}
/* check if there are no pending bytes in UART */
usbiss_uart_free(self);
/* check module id */
uint8Wr[0] = USBISS_CMD;
uint8Wr[1] = USBISS_ISS_VERSION;
if ( 2 != usbiss_uart_write(self, uint8Wr, 2) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, 2); // convert to ascii
printf(" ERROR:%s: REQ: %s\n", __FUNCTION__, charBuf);
}
return -1;
}
uint32RdLen = usbiss_uart_read(self, uint8Rd, 3);
if ( 3 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unexpected number of %i bytes received\n", __FUNCTION__, uint32RdLen);
}
return -1;
}
if ( 0 != self->uint8MsgLevel ) {
printf(" INFO:%s: ID=0x%02x, FW=0x%02x, MODE=0x%02x\n", __FUNCTION__, uint8Rd[0], uint8Rd[1], uint8Rd[2]);
}
if ( USBISS_ID != uint8Rd[0] ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unexpected module id 0x%02x\n", __FUNCTION__, uint8Rd[0]);
}
return -1;
}
self->uint8Fw = uint8Rd[1]; // USB-ISS firmware revision
self->uint8Mode = uint8Rd[2]; // current transfer mode
/* check for proper firmware revision, ISC_DIRECT is needed */
if ( USBISS_FW_MIN > self->uint8Fw ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Installed USB-ISS FW=0x%02x, Required 0x%02x. Please do an FW update.\n", __FUNCTION__, self->uint8Fw, USBISS_FW_MIN);
}
return -1;
}
/* Get Serial number */
uint8Wr[0] = USBISS_CMD;
uint8Wr[1] = USBISS_GET_SER_NUM;
if ( 2 != usbiss_uart_write(self, uint8Wr, 2) ) { // request
if ( 0 != self->uint8MsgLevel ) {
usbiss_uint8_to_asciihex(charBuf, sizeof(charBuf), uint8Wr, 2); // convert to ascii
printf(" ERROR:%s: REQ: %s\n", __FUNCTION__, charBuf);
}
return -1;
}
uint32RdLen = usbiss_uart_read(self, uint8Rd, 8);
if ( 8 != uint32RdLen ) {
if ( 0 != self->uint8MsgLevel ) {
printf(" ERROR:%s: Unexpected number of %i bytes received\n", __FUNCTION__, uint32RdLen);
}
return -1;
}
memset(self->charSerial, 0, sizeof(self->charSerial)); // make empty string
strncpy(self->charSerial, (char *) uint8Rd, 8); // serial has 8 digits
if ( 0 != self->uint8MsgLevel ) {
printf(" INFO:%s: Serial=%s\n", __FUNCTION__, self->charSerial);
}
/* mark as open */
self->uint8IsOpen = 1;
/* graceful end */
return 0;