-
Notifications
You must be signed in to change notification settings - Fork 18
/
chip.c
2086 lines (1792 loc) · 67.3 KB
/
chip.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
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 2019-2021 NXP.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of the NXP Semiconductor nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include "bcu_parser.h"
#include "chip.h"
#include "port.h"
#include "ftdi_def.h"
#define MAX_FTDI_BUFFER_SIZE 1024
extern char GV_LOCATION_ID[];
struct name_and_init_func chip_list[] =
{
"pca9548", pca9548_create,
"ft4232h_i2c", ft4232h_i2c_create,
"ft4232h_gpio", ft4232h_gpio_create,
"ft4232h_eeprom", ft4232h_eeprom_create,
"pac1934", pac1934_create,
"pca6408a", pca6408a_create,
"pca6416a", pca6416a_create,
"pca9655e", pca6416a_create,
"pcal6524h", pcal6524h_create,
"adp5585", adp5585_create,
"at24cxx", at24cxx_create,
"pct2075", pct2075_create,
};
int num_of_chips = sizeof(chip_list) / sizeof(struct name_and_init_func);
//////////////////////////PCT2075//////////////////////////////////
void* pct2075_create(char* chip_specification, void* parent)
{
struct pct2075* temp = (struct pct2075*)malloc(sizeof(struct pct2075));
if (temp == NULL)
{
printf("malloc failed\n");
return NULL;
}
temp->temp_dev.device.parent = parent;
temp->temp_dev.temp_read = temp_read;
temp->temp_dev.temp_enable = temp_enable;
temp->addr = extract_parameter_value(chip_specification, "addr");
//printf("AT24CXX created!\n");
return temp;
}
float temp_read(void* pct2075)
{
struct pct2075* temp = pct2075;
struct i2c_device* parent = (void*)temp->temp_dev.device.parent;
char addr_plus_write = (temp->addr) << 1;
char addr_plus_read = (temp->addr << 1) + 1;
int status, i = 0;
unsigned char data_buffer[2] = {0};
short data = 0;
parent->i2c_start(parent);
status = parent->i2c_write(parent, addr_plus_write, I2C_TYPE_TEMP);
if (status)
{
// printf("oh no! no ack received!\n");
return -10;
}
parent->i2c_write(parent, 0x00, I2C_TYPE_TEMP);
parent->i2c_start(parent);
parent->i2c_write(parent, addr_plus_read, I2C_TYPE_TEMP);
parent->i2c_read(parent, &data_buffer[0], 0, I2C_TYPE_TEMP);
parent->i2c_read(parent, &data_buffer[1], 1, I2C_TYPE_TEMP);
parent->i2c_stop(parent);
data = (data_buffer[0] << 8 | data_buffer[1]) >> 5;
if (data >= 1024)
return (float)((data - 2048) / 8.0);
else
return (float)((data) / 8.0);
return 0;
}
int temp_enable(void* pct2075, int enable)
{
struct pct2075* temp = pct2075;
struct i2c_device* parent = (void*)temp->temp_dev.device.parent;
char addr_plus_write = (temp->addr) << 1;
int status, i = 0;
unsigned char data_buffer[2] = {0};
short data = 0;
parent->i2c_start(parent);
status = parent->i2c_write(parent, addr_plus_write, I2C_TYPE_TEMP);
if (status)
{
// printf("oh no! no ack received!\n");
return -10;
}
parent->i2c_write(parent, 0x01, I2C_TYPE_TEMP);
parent->i2c_write(parent, enable ? 0x1 : 0x0, I2C_TYPE_TEMP);
parent->i2c_stop(parent);
return 0;
}
//////////////////////////AT24CXX//////////////////////////////////
void* at24cxx_create(char* chip_specification, void* parent)
{
struct at24cxx* at24 = (struct at24cxx*)malloc(sizeof(struct at24cxx));
if (at24 == NULL)
{
printf("malloc failed\n");
return NULL;
}
at24->eeprom_device.device.parent = parent;
at24->eeprom_device.eeprom_erase = at24cxx_erase;
at24->eeprom_device.eeprom_read = at24cxx_read;
at24->eeprom_device.eeprom_write = at24cxx_write;
at24->eeprom_device.eeprom_check_board = at24cxx_check_board;
at24->addr = extract_parameter_value(chip_specification, "addr");
at24->type = extract_parameter_value(chip_specification, "type");
//printf("AT24CXX created!\n");
return at24;
}
int at24cxx_erase(void* at24cxx)
{
unsigned char buf[256];
for (int i = 0; i < 256; i++)
buf[i] = 0xFF;
return at24cxx_write(at24cxx, buf, 0, 64, NULL);
}
int at24cxx_read(void* at24cxx, unsigned char* data_buffer, unsigned int startaddr, int size, unsigned char* sn_buf)
{
struct at24cxx* at24 = at24cxx;
struct i2c_device* parent = (void*)at24->eeprom_device.device.parent;
char addr_plus_write = (at24->addr) << 1;
char addr_plus_read = (at24->addr << 1) + 1;
int status, i = 0;
parent->i2c_start(parent);
status = parent->i2c_write(parent, addr_plus_write, I2C_TYPE_AT24);
if (status)
{
// printf("oh no! no ack received!\n");
return -10;
}
if (at24->type == EEPROM_TYPE_AT24C32) {
status = parent->i2c_write(parent, 0, I2C_TYPE_AT24);
if (status)
return -10;
}
status = parent->i2c_write(parent, startaddr, I2C_TYPE_AT24);
if (status)
return -10;
parent->i2c_start(parent);
status = parent->i2c_write(parent, addr_plus_read, I2C_TYPE_AT24);
if (status)
return -10;
for (i = 0; i < size - 1; i++)
parent->i2c_read(parent, &data_buffer[i], 0, I2C_TYPE_AT24);
parent->i2c_read(parent, &data_buffer[i], 1, I2C_TYPE_AT24);
parent->i2c_stop(parent);
msleep(10);
return 0;
}
int at24cxx_write(void* at24cxx, unsigned char* data_buffer, unsigned int startaddr, int size, unsigned char* sn_buf)
{
struct at24cxx* at24 = at24cxx;
struct i2c_device* parent = (void*)at24->eeprom_device.device.parent;
char addr_plus_write = (at24->addr) << 1;
char addr_plus_read = (at24->addr << 1) + 1;
int status, i = 0;
parent->i2c_start(parent);
status = parent->i2c_write(parent, addr_plus_write, I2C_TYPE_AT24);
if (status)
{
// printf("oh no! no ack received!\n");
return -10;
}
if (at24->type == EEPROM_TYPE_AT24C32)
parent->i2c_write(parent, 0, I2C_TYPE_AT24);
parent->i2c_write(parent, startaddr, I2C_TYPE_AT24);
for (i = 0; i < size; i++)
{
if (i != 0 && (startaddr + i) % 8 == 0)//AT24C02每8字节换页
{
parent->i2c_stop(parent);
msleep(10);
parent->i2c_start(parent);
parent->i2c_write(parent, addr_plus_write, I2C_TYPE_AT24);
if (at24->type == EEPROM_TYPE_AT24C32)
parent->i2c_write(parent, 0, I2C_TYPE_AT24);
parent->i2c_write(parent, startaddr + i, I2C_TYPE_AT24);
}
parent->i2c_write(parent, data_buffer[i], I2C_TYPE_AT24);
}
parent->i2c_stop(parent);
msleep(10);
return 0;
}
int at24cxx_check_board(void* at24cxx)
{
struct at24cxx* at24 = at24cxx;
struct i2c_device* parent = (void*)at24->eeprom_device.device.parent;
char addr_plus_write = (at24->addr) << 1;
char addr_plus_read = (at24->addr << 1) + 1;
//refresh the powerister
parent->i2c_start(parent);
if(parent->i2c_write(parent, addr_plus_write, I2C_TYPE_AT24))
{
parent->i2c_stop(parent);
// printf("pac 1934 failure get ack\n");
return -1;
}
else
{
parent->i2c_stop(parent);
return 0;
}
}
//////////////////////////PCA9548//////////////////////////////////
void* pca9548_create(char* chip_specification, void* parent)
{
struct pca9548* pca = (struct pca9548*)malloc(sizeof(struct pca9548));
if (pca == NULL)
{
printf("malloc failed\n");
return NULL;
}
pca->i2c_device.device.parent = parent;
pca->i2c_device.i2c_read = pca9548_read;
pca->i2c_device.i2c_readn = pca9548_readn;
pca->i2c_device.i2c_write = pca9548_write;
pca->i2c_device.i2c_start = pca9548_start;
pca->i2c_device.i2c_stop = pca9548_stop;
pca->channel = extract_parameter_value(chip_specification, "channel");
pca->addr = extract_parameter_value(chip_specification, "addr");
pca9548_set_channel(pca);
//printf("pca9548 created!\n");
return pca;
}
int pca9548_read(void* pca9548, unsigned char* data_buffer, int is_nack, int type)
{
struct pca9548* pca = pca9548;
struct i2c_device* parent = (void*)pca->i2c_device.device.parent;
parent->i2c_read(parent, data_buffer, is_nack, type);
return 0;
}
int pca9548_readn(void* pca9548, unsigned char* data_buffer, int type, int len)
{
struct pca9548* pca = pca9548;
struct i2c_device* parent = (void*)pca->i2c_device.device.parent;
parent->i2c_readn(parent, data_buffer, type, len);
return 0;
}
int pca9548_write(void* pca9548, unsigned char data, int type)
{
struct pca9548* pca = pca9548;
struct i2c_device* parent = (void*)pca->i2c_device.device.parent;
return parent->i2c_write(parent, data, type);
}
int pca9548_start(void* pca9548)
{
struct pca9548* pca = pca9548;
struct i2c_device* parent = (void*)pca->i2c_device.device.parent;
parent->i2c_start(parent);
return 0;
}
int pca9548_stop(void* pca9548)
{
struct pca9548* pca = pca9548;
struct i2c_device* parent = (void*)pca->i2c_device.device.parent;
parent->i2c_stop(parent);
return 0;
}
int pca9548_set_channel(struct pca9548* pca9548)
{
//my observation is that we must wait for 20ms from last set_channel to guarantee sucessfully switch
//msleep(20);
struct pca9548* pca = pca9548;
struct i2c_device* parent = (void*)pca->i2c_device.device.parent;
unsigned char addr_plus_write = pca->addr << 1;
unsigned char change_channel_cmd = 1 << (pca->channel);
int status;
parent->i2c_start(parent);
status = parent->i2c_write(parent, addr_plus_write, I2C_TYPE_PCA9548);
if (status)
{
printf("oh no! no ack received!\n");
}
parent->i2c_write(parent, change_channel_cmd, I2C_TYPE_PCA9548);
parent->i2c_stop(parent);
return 0;
}
////////////////////////////////ft4232h_eeprom///////////////////////////////////
struct ftdi_info g_ftdi_info[MAX_FT_I2C_CHANNEL_NUMBER];
void* ft4232h_eeprom_create(char* chip_specification, void* parent)
{
int status = 0;
struct ft4232h_eeprom* ftee = malloc(sizeof(struct ft4232h_eeprom));
if (ftee == NULL)
{
printf("malloc failed\n");
return NULL;
}
ftee->ftdi_info = &g_ftdi_info[0];
ftee->eeprom_device.device.parent = parent;
ftee->eeprom_device.eeprom_read = ft4232h_eeprom_read;
ftee->eeprom_device.eeprom_write = ft4232h_eeprom_write;
ftee->eeprom_device.eeprom_erase = ft4232h_eeprom_erase;
ft_init(ftee->ftdi_info);
if (!ftee->ftdi_info->isinit)
{
if (strlen(GV_LOCATION_ID) == 0) {
#if defined(__linux__) || defined(__APPLE__)
status = ft_open_channel(ftee->ftdi_info, 0);
#else
status = ft_open_channel(ftee->ftdi_info, 2);
#endif
}
else {
#if defined(__linux__) || defined(__APPLE__)
status = ft_open_channel_by_id(ftee->ftdi_info, 0, GV_LOCATION_ID);
#else
status = ft_open_channel_by_id(ftee->ftdi_info, 2, GV_LOCATION_ID);
#endif
}
}
if (status != 0)
{
printf("failed to open ftdi device, err = %d\n", status);
#if defined(__linux__) || defined(__APPLE__)
printf("***please make sure you run bcu with sudo\n");
#endif
free(ftee);
return NULL;
}
return ftee;
}
int ft4232h_eeprom_read(void* ft_eeprom, unsigned char* data_buffer, unsigned int startaddr, int size, unsigned char* sn_buf)
{
struct ft4232h_eeprom* eeprom = ft_eeprom;
struct ftdi_info *ftdi = eeprom->ftdi_info;
return ft_read_eeprom(ftdi, startaddr, data_buffer, size, sn_buf);
}
int ft4232h_eeprom_write(void* ft_eeprom, unsigned char* data_buffer, unsigned int startaddr, int size, unsigned char* sn_buf)
{
struct ft4232h_eeprom* eeprom = ft_eeprom;
struct ftdi_info *ftdi = eeprom->ftdi_info;
return ft_write_eeprom(ftdi, startaddr, data_buffer, size, sn_buf);
}
int ft4232h_eeprom_erase(void* ft_eeprom)
{
struct ft4232h_eeprom* eeprom = ft_eeprom;
struct ftdi_info *ftdi = eeprom->ftdi_info;
return ft_erase_eeprom(ftdi);
}
////////////////////////////////ft4232H///////////////////////////////////
void* ft4232h_i2c_create(char* chip_specification, void* parent)
{
struct ft4232h* ft = malloc(sizeof(struct ft4232h));
if (ft == NULL)
{
printf("malloc failed\n");
return NULL;
}
ft->i2c_device.device.parent = parent;
ft->i2c_device.i2c_read = ft4232h_i2c_read;
ft->i2c_device.i2c_readn = ft4232h_i2c_readn;
ft->i2c_device.i2c_write = ft4232h_i2c_write;
ft->i2c_device.i2c_start = ft4232h_i2c_start;
ft->i2c_device.i2c_stop = ft4232h_i2c_stop;
ft->i2c_device.device.free = ft4232h_i2c_free;
ft->channel = extract_parameter_value(chip_specification, "channel");
if (extract_parameter_value(chip_specification, "dir_bitmask") == -1)
{
printf("set dir_bitmask as default value 0x00\n");
ft->dir_bitmask = 0x00;//default should be zero if parameter is not entered;
}
else
ft->dir_bitmask = extract_parameter_value(chip_specification, "dir_bitmask");
if (extract_parameter_value(chip_specification, "val_bitmask") == -1)
{
printf("set val_bitmask as default value 0x00\n");
ft->val_bitmask = 0x00;//default should be zero if parameter is not entered;
}
else
ft->val_bitmask = extract_parameter_value(chip_specification, "val_bitmask");
ft->ftdi_info = &g_ftdi_info[ft->channel];
for (int i = 0; i < MAX_FT_I2C_CHANNEL_NUMBER; i++)
{
if (g_ftdi_info[i].isinit & 0xF0)
{
g_ftdi_info[i].isinit = 0x1;
if (ft->ftdi_info->isinit & 0xF)
ft->ftdi_info->isinit = 0x10;
}
}
if (!(ft->ftdi_info->isinit & 0xF))
{
if (!(ft->ftdi_info->isinit & 0xF0))
{
ft_init(ft->ftdi_info);
int status = 0;
if (strlen(GV_LOCATION_ID) == 0) {
status = ft_open_channel(ft->ftdi_info, ft->channel + 1);
}
else {
status = ft_open_channel_by_id(ft->ftdi_info, ft->channel + 1, GV_LOCATION_ID);
}
if (status != 0)
{
printf("failed to open ftdi device!\n");
#if defined(__linux__) || defined(__APPLE__)
printf("***please make sure you run bcu with sudo\n");
#endif
free(ft);
return NULL;
}
}
ft4232h_i2c_init(ft);
ft->ftdi_info->isinit = 0x1;
}
//printf("ft4232h created!\n");
return ft;
}
void ft4232h_i2c_remove_all(int enable_1_exit_handler)
{
int i;
for (i = 0; i < MAX_FT_I2C_CHANNEL_NUMBER; i++)
{
if (!enable_1_exit_handler && i == 1)
continue;
if (g_ftdi_info[i].isinit)
{
ft_close(&g_ftdi_info[i]);
g_ftdi_info[i].isinit = 0;
}
// ft4232h_i2c_free(&g_ft[i]);
}
}
int ft4232h_i2c_read(void* ft4232h, unsigned char* data_buffer, int is_nack, int type)
{
struct ft4232h* ft = ft4232h;
unsigned char buffer[MAX_FTDI_BUFFER_SIZE];
unsigned char in_buffer[MAX_FTDI_BUFFER_SIZE];
int i = 0, ftStatus = 0;
#ifdef _WIN32
if (type == I2C_TYPE_GPIO)
#endif
ft_clear_buffer(ft->ftdi_info);
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAIN | ft->dir_bitmask;
buffer[i++] = MPSSE_CMD_DATA_IN_BITS_POS_EDGE;
buffer[i++] = DATA_SIZE_8BITS;
buffer[i++] = MPSSE_CMD_SEND_IMMEDIATE;
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
buffer[i++] = MPSSE_CMD_DATA_OUT_BITS_NEG_EDGE;
buffer[i++] = 0;// 0 means send one bit
if (is_nack)
buffer[i++] = 0x80;// MSB zero means ACK
else
buffer[i++] = 0x00;// MSB zero means ACK
ftStatus = ft_write(ft->ftdi_info, buffer, i); //Send off the commands
if (ftStatus < 0) return ftStatus;
ftStatus = ft_read(ft->ftdi_info, in_buffer, 1); //Read one byte
if (ftStatus < 0) return ftStatus;
*data_buffer = in_buffer[0];
#ifdef _WIN32
if (type == I2C_TYPE_GPIO)
#endif
ft_clear_buffer(ft->ftdi_info);
return 0;
}
int ft4232h_i2c_readn(void* ft4232h, unsigned char* data_buffer, int type, int len)
{
struct ft4232h* ft = ft4232h;
unsigned char buffer[MAX_FTDI_BUFFER_SIZE] = {0};
unsigned char in_buffer[MAX_FTDI_BUFFER_SIZE] = {0};
int i = 0, ftStatus = 0, j;
// #ifdef _WIN32
// if (type == I2C_TYPE_GPIO)
// #endif
// ft_clear_buffer(ft->ftdi_info);
for (j = 0; j < len; j++)
{
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAIN | ft->dir_bitmask;
buffer[i++] = MPSSE_CMD_DATA_IN_BITS_POS_EDGE;
buffer[i++] = DATA_SIZE_8BITS;
buffer[i++] = MPSSE_CMD_SEND_IMMEDIATE;
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
buffer[i++] = MPSSE_CMD_DATA_OUT_BITS_NEG_EDGE;
buffer[i++] = 0;// 0 means send one bit
if (j < len - 1)
buffer[i++] = 0x00;// MSB zero means ACK
else
buffer[i++] = 0xFF;// MSB zero means ACK
}
ftStatus = ft_write(ft->ftdi_info, buffer, i); //Send off the commands
if (ftStatus < 0)
return ftStatus;
us_sleep(500);
ftStatus = ft_read(ft->ftdi_info, in_buffer, len); //Read one byte
if (ftStatus < 0)
return ftStatus;
memcpy(data_buffer, in_buffer, len);
#ifdef _WIN32
if (type == I2C_TYPE_GPIO)
#endif
ft_clear_buffer(ft->ftdi_info);
return 0;
}
int ft4232h_i2c_write(void* ft4232h, unsigned char data, int type)
{
struct ft4232h* ft = ft4232h;
unsigned char buffer[MAX_FTDI_BUFFER_SIZE];
unsigned char in_buffer[MAX_FTDI_BUFFER_SIZE];
int i = 0, ftStatus = 0;
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE; //Command to set directions of lower 8 pins and force value on bits set as output
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
/* Command to write 8bits */
buffer[i++] = MPSSE_CMD_DATA_OUT_BITS_NEG_EDGE;/* MPSSE command */
buffer[i++] = DATA_SIZE_8BITS;
buffer[i++] = data;
/* Set SDA to input mode before reading ACK bit */
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask; /*Value*/
buffer[i++] = DIRECTION_SCLOUT_SDAIN | ft->dir_bitmask; /*Direction*/
/* Command to get ACK bit */
buffer[i++] = MPSSE_CMD_DATA_IN_BITS_POS_EDGE;/* MPSSE command */
buffer[i++] = DATA_SIZE_1BIT; /*Read only one bit */
/*Command MPSSE to send data to PC immediately */
buffer[i++] = MPSSE_CMD_SEND_IMMEDIATE;
/*set direction*/
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;/* MPSSE command */
buffer[i++] = VALUE_SCLLOW_SDAHIGH | ft->val_bitmask; /*Value*/
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask; /*Direction*/
ftStatus = ft_write(ft->ftdi_info, buffer, i); //Send off the commands
if (ftStatus < 0) return ftStatus;
ftStatus = ft_read(ft->ftdi_info, in_buffer, 1);
if (ftStatus < 0) return ftStatus;
if (((in_buffer[0] & 0x1) != 0)) //Check ACK bit 0 on data byte read out
{
// printf("can't get the ACK bit after write\n");
return -1; /*Error, can't get the ACK bit */
}
#ifdef _WIN32
if (type == I2C_TYPE_GPIO)
#endif
ft_clear_buffer(ft->ftdi_info);
return 0;
}
int ft4232h_i2c_start(void* ft4232h)
{
struct ft4232h* ft = ft4232h;
unsigned char buffer[MAX_FTDI_BUFFER_SIZE];
int i = 0, ftStatus = 0;
for (int j = 0; j < 40; j++)
{
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLHIGH_SDAHIGH | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
}
for (int j = 0; j < 20; j++)
{
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLHIGH_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
}
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
ftStatus = ft_write(ft->ftdi_info, buffer, i); //Send off the commands
if (ftStatus < 0)
return ftStatus;
return 0;
}
int ft4232h_i2c_stop(void* ft4232h)
{
struct ft4232h* ft = ft4232h;
unsigned char buffer[MAX_FTDI_BUFFER_SIZE];
int i = 0, ftStatus = 0;
/* SCL low, SDA low */
for (int j = 0; j < 25; j++)
{
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLLOW_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
}
/* SCL high, SDA low */
for (int j = 0; j < 25; j++)
{
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLHIGH_SDALOW | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
}
/* SCL high, SDA high */
for (int j = 0; j < 25; j++)
{
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE;
buffer[i++] = VALUE_SCLHIGH_SDAHIGH | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
}
//buffer[i++]=0x80;
//buffer[i++]=0x00;
//buffer[i++]=0x10; /* Tristate the SCL & SDA pins */
ftStatus = ft_write(ft->ftdi_info, buffer, i); //Send off the commands
if (ftStatus < 0)
return ftStatus;
return 0;
}
int ft4232h_i2c_init(struct ft4232h* ft)
{
unsigned char buffer[MAX_FTDI_BUFFER_SIZE];
int i = 0, ftStatus = 0;
ft_set_bitmode(ft->ftdi_info, 0, 0); //resetting the controller
ft_set_bitmode(ft->ftdi_info, 0, BM_MPSSE);//set as MPSSE
buffer[i++] = MPSSE_CMD_DISABLE_CLOCK_DIVIDE_BY_5; //Ensure disable clock divide by 5 for 60Mhz master clock
buffer[i++] = MPSSE_CMD_DISABLE_ADAPTIVE_CLOCKING; //Ensure turn off adaptive clocking
buffer[i++] = MPSSE_CMD_ENABLE_3PHASE_CLOCKING; //PAC 1934 need it. Enable 3 phase data clock, used by I2C to allow data on one clock edges
ftStatus = ft_write(ft->ftdi_info, buffer, i);
if (ftStatus < 0)
return ftStatus;
i = 0; //Clear output buffer
buffer[i++] = MPSSE_CMD_SET_DATA_BITS_LOWBYTE; //Command to set directions of lower 8 pins and force value on bits set as output
buffer[i++] = VALUE_SCLHIGH_SDAHIGH | ft->val_bitmask;
buffer[i++] = DIRECTION_SCLOUT_SDAOUT | ft->dir_bitmask;
// The SK clock frequency can be worked out by below algorithm with divide by 5 set as off
// SK frequency = 60MHz /((1 + [(1 +0xValueH*256) OR 0xValueL])*2)
buffer[i++] = MPSSE_CMD_SET_CLOCK_DIVISOR; //Command to set clock divisor
buffer[i++] = (unsigned char)(CLOCK_DIVISOR_400K & '\xFF'); //Set 0xValueL of clock divisor
buffer[i++] = (unsigned char)((CLOCK_DIVISOR_400K >> 8) & '\xFF'); //Set 0xValueH of clock divisor
ftStatus = ft_write(ft->ftdi_info, buffer, i);
if (ftStatus < 0)
return ftStatus;
i = 0; //Clear output buffer
msleep(20); //Delay for a while
//Turn off loop back in case
buffer[i++] = MPSEE_CMD_DISABLE_LOOPBACK; //Command to turn off loop back of TDI/TDO connection
ftStatus = ft_write(ft->ftdi_info, buffer, i);
if (ftStatus < 0)
return ftStatus;
msleep(30); //Delay for a while
//printf("I2C initialization finished!\n");
return 0;
}
int ft4232h_i2c_free(void* ft4232h)
{
struct ft4232h* ft = ft4232h;
// int ftStatus = 0;
// ftStatus = ft_close(ft->ftdi_info);
// if (ftStatus < 0)
// return ftStatus;
ft->ftdi_info = NULL;
return 0;
}
///////////////////////////////ft4232H_GPIO/////////////////////////////////
void* ft4232h_gpio_create(char* chip_specification, void* parent)
{
struct ft4232h_gpio* ft = malloc(sizeof(struct ft4232h_gpio));
if (ft == NULL)
{
printf("malloc failed\n");
return NULL;
}
ft->gpio_device.device.parent = parent;
ft->gpio_device.gpio_read=ft4232h_gpio_read;
ft->gpio_device.gpio_write = ft4232h_gpio_write;
//ft->gpio_device.gpio_toggle=ft4232h_gpio_toggle;
ft->gpio_device.device.free = ft4232h_gpio_free;
ft->gpio_device.pin_bitmask = extract_parameter_value(chip_specification, "pin_bitmask");
ft->channel = extract_parameter_value(chip_specification, "channel");
ft->ftdi_info = &g_ftdi_info[ft->channel];
if (!ft->ftdi_info->isinit)
{
ft_init(ft->ftdi_info);
int status = 0;
if (strlen(GV_LOCATION_ID) == 0) {
status = ft_open_channel(ft->ftdi_info, ft->channel + 1);
}
else {
status = ft_open_channel_by_id(ft->ftdi_info, ft->channel + 1, GV_LOCATION_ID);
}
if (status != 0)
{
printf("failed to open ftdi device!\n");
#if defined(__linux__) || defined(__APPLE__)
printf("***please make sure you run bcu with sudo\n");
#endif
free(ft);
return NULL;
}
ft->ftdi_info->isinit = ft->ftdi_info->isinit << 4;
}
if (ft->ftdi_info->isinit & 0xF)
{
ft_set_bitmode(ft->ftdi_info, 0, 0);
ft->ftdi_info->isinit = ft->ftdi_info->isinit << 4;
}
return ft;
}
int ft4232h_gpio_write(void* ft4232h, unsigned char bit_value)
{
struct ft4232h_gpio* ft = ft4232h;
int mask = 0xFF;
if (ft_set_bitmode(ft->ftdi_info, mask, BM_BITBANG) < 0)
printf("failed to set bitmode\n");
unsigned char current_output;
ft_read_pins(ft->ftdi_info, ¤t_output);
//printf("previous output: %x\n", current_output);
unsigned char data;
data = (current_output & (~ft->gpio_device.pin_bitmask)) | (bit_value & ft->gpio_device.pin_bitmask);
if (ft_write(ft->ftdi_info, &data, 1) < 0)
{
printf("failed to write\n");
return -1;
}
return 0;
}
int ft4232h_gpio_read(void* ft4232h, unsigned char* bit_value_buffer)
{
struct ft4232h_gpio* ft = ft4232h;
unsigned char current_output;
ft_read_pins(ft->ftdi_info, ¤t_output);
bit_value_buffer[0] = current_output & ft->gpio_device.pin_bitmask;
return 0;
}
int ft4232h_gpio_toggle(void* ft4232h)
{
printf("toggle is not yet implemented!\n");
return 0;
}
int ft4232h_gpio_free(void* ft4232h)
{
struct ft4232h_gpio* ft = ft4232h;
// ft_close(ft->ftdi_info);
ft->ftdi_info = NULL;
return 0;
}
////////////////////////////////PAC1934///////////////////////////////////
void* pac1934_create(char* chip_specification, void* parent)
{
struct pac1934* pac = (struct pac1934*)malloc(sizeof(struct pac1934));
if (pac == NULL)
{
printf("malloc failed\n");
return NULL;
}
pac->power_device.device.parent = parent;
pac->power_device.power_get_addr = get_pac1934_addr;
pac->power_device.power_get_group = get_pac1934_group;
pac->power_device.power_get_sensor = get_pac1934_sensor;
pac->power_device.power_get_cur_res = get_pac1934_cur_res;
pac->power_device.power_get_unused_res = get_pac1934_unused_res;
pac->power_device.power_set_hwfilter = pac1934_set_hwfilter;
pac->power_device.power_set_bipolar = pac1934_set_bipolar;
pac->power_device.power_write_bipolar = pac1934_write_bipolar;
pac->power_device.power_write_no_skip = pac1934_write_no_skip;
pac->power_device.power_set_snapshot = pac1934_snapshot;
pac->power_device.power_get_data = pac1934_get_data;
pac->power_device.switch_sensor = pac1934_switch;
pac->group = extract_parameter_value(chip_specification, "group");
pac->sensor = extract_parameter_value(chip_specification, "sensor");
pac->addr = extract_parameter_value(chip_specification, "addr");
pac->rs1 = extract_parameter_value(chip_specification, "rsense1");
pac->group2 = extract_parameter_value(chip_specification, "group2");
pac->sensor2 = extract_parameter_value(chip_specification, "sensor2");
pac->rs2 = extract_parameter_value(chip_specification, "rsense2");
pac->cur_group = pac->group;
pac->cur_sensor = pac->sensor;
pac->cur_rs = pac->rs1;
//printf("pac1934 created!\n");
return pac;
}
int pac1934_switch(void *pac1934, int i)
{
struct pac1934* pac = pac1934;
if (i == 0)
{
pac->cur_group = pac->group;
pac->cur_sensor = pac->sensor;
pac->cur_rs = pac->rs1;
}
else if (i == 1)
{
if(pac->group2 != -1)
pac->cur_group = pac->group2;
if(pac->sensor2 != -1)
pac->cur_sensor = pac->sensor2;
pac->cur_rs = pac->rs2;
}
else
return -1;
return 0;
}
int get_pac1934_addr(void* pac1934)
{
struct pac1934* pac = pac1934;
return pac->addr;
}
int get_pac1934_group(void* pac1934)
{
struct pac1934* pac = pac1934;
return pac->cur_group;
}
int get_pac1934_sensor(void* pac1934)
{
struct pac1934* pac = pac1934;
return pac->cur_sensor;
}
int get_pac1934_cur_res(void* pac1934)
{
struct pac1934* pac = pac1934;
return pac->cur_rs;
}
int get_pac1934_unused_res(void* pac1934)
{
struct pac1934* pac = pac1934;
if (pac->rs1 == pac->cur_rs)
return pac->rs2;
else
return pac->rs1;
}
void pac1934_set_hwfilter(void* pac1934, int onoff)
{
struct pac1934* pac = pac1934;
pac->hwfilter = onoff;
}
void pac1934_set_bipolar(void* pac1934, int value)
{
struct pac1934* pac = pac1934;
pac->bipolar = value;