-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathd_analog.c
3569 lines (2891 loc) · 99 KB
/
d_analog.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
/*
* LEGO® MINDSTORMS EV3
*
* Copyright (C) 2010-2013 The LEGO Group
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*! \page AnalogModule Analog Input Module
*
* Manages communication and controlling the A to D conversion on an input port.\n
*
*- \subpage DcmDriver
*- \subpage AnalogModuleMemory
*- \subpage AnalogModuleResources
*
*/
/*! \page DcmDriver Device Connection Manager Driver
*
* Manages the detection of adding and removing different devices to an input or an output port.\n
*
* The device will change some connection levels on the port when added and that way give an event to start the evaluation of the device id.
*
* The evaluation state machine is timer interrupt driven at a low frequency (less than 1KHz) so the used CPU power is held at lowest possible level.\n
*
*
* \n
*
* Input Port
*
* - From the beginning all I/O is set as input - an open port is defined as:
* - Value at connection 1 is higher than IN1_NEAR_5V
* - Level at connection 2 is high
* - Level at connection 5 is high
* - Level at connection 6 is low
* - Value at connection 6 is lower than IN6_NEAR_GND
*
*
* \n
*
* If anything changes to a steady value (for more than STEADY_TIME) it will start a state machine that will try to
* detect what happened.
*
* When it is detected - a signal is sent to the \ref InputLibraryDeviceSetup "Input Library Device Setup" and the state will freeze in a state that only looks for
* an open port condition (for more than STEADY_TIME).
* \verbatim
*/
#define IN_CONNECT_STEADY_TIME 350 // [mS] time needed to be sure that the connection is steady
#define IN_DISCONNECT_STEADY_TIME 100 // [mS] time needed to be sure that the disconnection is steady
#define IN1_NEAR_5V 4800 // [mV] higher values mean that connection 1 is floating
#define IN1_NEAR_PIN2 3100 // [mV] higher values mean that connection 1 is shorted to connection 2 (5000 * 18K / (18K + 10k))
#define IN1_TOUCH_HIGH 950 // [mV] values in between these limits means that an old touch sensor is connected
#define IN1_TOUCH_LOW 850 // [mV]
#define IN1_NEAR_GND 100 // [mV] lower values mean that connection 1 is shorted to connection 3
#define IN6_NEAR_GND 150 // [mV] lower values mean that connection 6 is floating
/*
INPUT
*********************************************************************************************************************************************************************************************************************
MICRO CIRCUIT CONNECTION NEW UART DEVICE NEW DUMB SENSOR OLD SENSOR TACHO MOTOR NEW DUMP ACTUATOR
---------------------- ---------------------------------- ---------- --------------------- ------------------------ -------------------- ---------------- -----------------
Analogue I 10K pull up to ADC_REF 1 Short circuit to ground ID resistor to ground Analogue value Motor + Motor +
Digital I 6K4 impedance to NEAR_PIN2 voltage 2 Open Open Short to ground Motor - Motor -
Ground Ground 3 Ground Ground Ground Ground ?
Supply Supply 4 Supply Supply Supply Supply ?
Digital I/O (float) 100K pull up to 3.3V 5 RXD (float) Short to ground ? Tacho A ID resistor to 5
Analogue I + Digital I/O (float) 220K pull down to ground 6 TXD (low) Analogue value ? Tacho B ID resistor to 6
*********************************************************************************************************************************************************************************************************************
ID VALUE ON CONNECTION 1:
ADC_REF -----
IN1_NEAR_5V -----
|
|
| TACHO MOTOR and NEW DUMB ACTUATOR
|
|
IN1_NEAR_PIN2 -----
|
|
|
|
|
|
|
| NEW SENSOR ID's
|
|
|
|
|
|
| NEW UART SENSOR
0.0V -----
IMPLEMENTED DETECTION RULES (sequence matters):
I. Connection 2 low
1. Connection 5 and 6 high -> NXT IIC DEVICE
2. Connection 5 low -> NXT LIGHT SENSOR
3. Connection 1 lower than IN1_NEAR_GND -> NXT COLOR SENSOR
4. Connection 1 higher than IN1_NEAR_5V -> NXT TOUCH SENSOR
5. Connection 1 between IN1_TOUCH_HIGH and IN1_TOUCH_LOW -> NXT TOUCH SENSOR
6. else -> NXT SOUND SENSOR
II. Connection 1 loaded
1. Connection 1 higher than IN1_NEAR_PIN2 -> ERROR
2. Connection 1 lower than IN1_NEAR_GND -> NEW UART DEVICE
3. else (value on connection 1 is ID) -> NEW DUMB DEVICE
III. Connection 6 high -> NXT IIC TEMP SENSOR
IV. Connection 5 low -> ERROR
NEW ID's:
The range from 0mV to just beneath the voltage on connection 2 is free to be used for the new sensor ID's - recommendations:
I. Use a short circuit to ground to identify the UART device.
\endverbatim
*
* \n
*/
/*! \page DcmDriver
*
* Output Port
*
* - From the beginning motor driver is floating and all I/O is set as input - an open port is defined as:
* - Value at connection 5 is in between OUT5_BALANCE_LOW and OUT5_BALANCE_HIGH
* - Level at connection 5 is high
* - Level at connection 6 is high
*
* If anything changes to a steady value (for more than STEADY_TIME) it will start a state machine that will try to
* detect what happened.
*
* When it is detected - a signal is sent to the \ref InputLibraryDeviceSetup "Input Library Device Setup" and the state will freeze in a state that only looks for
* an open port condition (for more than STEADY_TIME).
* \verbatim
*/
#define OUT5_IIC_HIGH 3700 // [mV] values in between these limits means that an old IIC sensor or color sensor is connected
#define OUT5_IIC_LOW 2800 // [mV]
#define OUT5_MINITACHO_HIGH1 2000 // [mV] values in between these limits means that a mini tacho motor is pulling high when pin5 is pulling low
#define OUT5_MINITACHO_LOW1 1600 // [mV]
#define OUT5_BALANCE_HIGH 2600 // [mV] values in between these limits means that connection 5 is floating
#define OUT5_BALANCE_LOW 2400 // [mV]
#define OUT5_LIGHT_HIGH 850 // [mV] values in between these limits means that an old light sensor is connected
#define OUT5_LIGHT_LOW 650 // [mV]
#define OUT5_MINITACHO_HIGH2 450 // [mV] values in between these limits means that a mini tacho motor is pulling low when pin5 floats
#define OUT5_MINITACHO_LOW2 250 // [mV]
#define OUT5_NEAR_GND 100 // [mV] lower values mean that connection 5 is shorted to ground
/*
OUTPUT
*********************************************************************************************************************************************************************************************************************
MICRO CIRCUIT CONNECTION NEW UART DEVICE NEW DUMB SENSOR OLD SENSOR TACHO MOTOR NEW DUMP ACTUATOR
---------------------- ------------------------------- ---------- --------------------- ------------------------ -------------------- ---------------- -----------------
Motor driver + Connected to motor driver 1 ID resistor to ground ID resistor to ground Analogue value Motor + Motor +
Motor driver - 100K pull up to ADC_REF 2 Open Open Short to ground Motor - Motor -
Ground Ground 3 Ground Ground Ground Ground ?
Supply Supply 4 Supply Supply Supply Supply ?
Analogue I + Digital I/O 50K impedance to OUT5_BALANCE 5 RXD (float) Short to ground ? Tacho A ID resistor to 5
Digital I/O (low) 100K pull up to connection 2 6 TXD (low) Analogue value ? Tacho B ID resistor to 6
*********************************************************************************************************************************************************************************************************************
ID VALUE ON CONNECTION 5:
Connection 6 floating Connection 6 low and value on connection 5 changed
--------------------------------------------------------------------------------------- --------------------------------------------------
ADC_REF -------------------------------------------------------------------------------------------------
NEAR_5V ----- |
| |
| |
3.3 - | - - - - - - - - - - - - - - - - - - - - - - | - - - - - - - - - - - - - - - - - - - - - - - - 2014 ADC reference voltage
| LARGE TACHO MOTOR -
| MINI TACHO MOTOR, NEW DUMB MOTOR - OLD TACHO MOTOR
| NEW TACHO MOTOR -
OUT5_IIC_HIGH ----- |
| OLD IIC SENSOR -
OUT5_IIC_LOW ----- |
| |
OUT5_BALANCE_HIGH ----- |
| OPEN -
OUT5_BALANCE_LOW ----- |
| | -----
| | |
| | |
| | | NEW ACTUATOR ID's
| | |
| | |
| | -----
OUT5_LIGHT_HIGH ----- |
| OLD LIGHT SENSOR -
OUT5_LIGHT_LOW ----- |
| NEW TACHO MOTOR -
| MINI TACHO MOTOR -
| LARGE TACHO MOTOR -
OUT5_NEAR_GND ----- -----
| NEW DUMB SENSOR
0.0V -------------------------------------------------------------------------------------------------
When something is connected: the value on connection 5 is stored as Value5Float. Connection 6 is driven low and the value on connection 5 is then stored as Value5Low.
Connection 6 is set floating again.
The two stored values is used to determine the type of the attached device.
IMPLEMENTED DETECTION RULES (sequence matters):
I. Value5Float is equal to Value5Low
1. Value5Float is between OUT5_BALANCE_LOW and OUT5_BALANCE_HIGH and Connection 6 is low -> ERROR (NXT TOUCH SENSOR, NXT SOUND SENSOR, NEW UART SENSOR)
2. Value5Float is lower than OUT5_NEAR_GND -> ERROR (NEW DUMP SENSOR)
3. Value5Float is between OUT5_LIGHT_LOW and OUT5_LIGHT_HIGH -> ERROR (OLD IIC SENSOR)
4. Value5Float is between OUT5_IIC_LOW and OUT5_IIC_HIGH -> ERROR (OLD TEMP SENSOR)
5. Value5Float is lower than OUT5_BALANCE_LOW
Value5Float is higher than OUT5_MINITACHO_HIGH2 -> NEW TACHO
Value5Float is higher than OUT5_MINITACHO_LOW2 -> MINI TACHO (NEW MINI TACHO MOTOR)
else -> TACHO MOTOR
6. Set connection 5 low and measure new Value5Low
7. VALUE5Low is lower than OUT5_MINITACHO_LOW1 -> NEW TACHO (NEW MINI TACHO MOTOR)
VALUE5Low is lower than OUT5_MINITACHO_HIGH1 -> MINI TACHO (NEW MINI TACHO MOTOR)
else -> TACHO MOTOR
II. Value5Float is NOT equal to Value5Low
1. Value5Low is between OUT5_NEAR_GND and OUT5_BALANCE_LOW -> NEW DUMP ACTUATOR (ID is Value5Low)
2. else -> ERROR
NEW ID's:
The range from 800mV to OUT5_BALANCE_LOW is free to be used for the new sensor ID's - recommendations:
I. Use a short circuit between connection 5 and connection 6 to identify the cheapest device
II. Remember to reserve one id for the smart actuator (maybe the highest resistance = 500K)
III. Remember that the ID resistor must get connection 5 out of balance
IV. Remember the voltage on pin 1 when connected to an input port
\endverbatim
*
* \n
*/
#ifndef PCASM
#include <asm/types.h>
#include <linux/time.h>
#endif
#define HW_ID_SUPPORT
#include "../../lms2012/source/lms2012.h"
#include "../../lms2012/source/am1808.h"
#ifdef DEBUG_D_ANALOG
#define DEBUG
#endif
int Hw = 0;
enum InputPortPins
{
INPUT_PORT_PIN1,
INPUT_PORT_PIN2,
INPUT_PORT_PIN5,
INPUT_PORT_PIN6,
INPUT_PORT_BUF,
INPUT_PORT_PINS,
INPUT_PORT_VALUE
};
enum InputSpiPins
{
ADCMOSI,
ADCMISO,
ADCCLK,
ADCCS,
ADC_SPI_PINS
};
enum OutputPortPins
{
OUTPUT_PORT_PIN1,
OUTPUT_PORT_PIN2,
OUTPUT_PORT_PIN5W,
OUTPUT_PORT_PIN5R,
OUTPUT_PORT_PIN6,
OUTPUT_PORT_PINS,
OUTPUT_PORT_VALUE
};
enum AdcPowerPins
{
ADCONIGEN,
ADCBATEN,
ADC_POWER_PINS
};
#define INPUTADCPORTS 12
#define INPUTADCPOWERS 4
#define INPUTADC (INPUTADCPORTS + INPUTADCPOWERS)
#define NO_OF_INPUT_PORTS INPUTS
#define NO_OF_OUTPUT_PORTS OUTPUTS
static void InputPortFloat(int Port);
INPIN InputPortPin[NO_OF_INPUT_PORTS][INPUT_PORT_PINS];
INPIN OutputPortPin[NO_OF_OUTPUT_PORTS][OUTPUT_PORT_PINS];
INPIN AdcPowerPin[ADC_POWER_PINS];
/*! \page AnalogModuleMemory
*
* <hr size="1"/>
* <b> Map physical ADC channels to logical channels in shared memory </b>
*
* The mapping is done by following array of physical channels
* \verbatim
*
*/
static const UBYTE InputReadMap[INPUTADC] = { 6,8,10,12,5,7,9,11,1,0,13,14,2,15,3,4 };
/* \endverbatim
* \n
*/
/*! \page AnalogModuleResources Gpios and Resources used for Module
*
* Describes use of gpio and resources\n
*
* \verbatim
*/
// EP2
INPIN EP2_InputPortPin[][INPUT_PORT_PINS] =
{
{ // Input port 1
{ GP8_10 , NULL, 0 }, // Pin 1 - I_ONA - 9V enable (high)
{ GP2_2 , NULL, 0 }, // Pin 2 - LEGDETA - Digital input pulled up
{ GP0_2 , NULL, 0 }, // Pin 5 - DIGIA0 - Digital input/output
{ GP0_15 , NULL, 0 }, // Pin 6 - DIGIA1 - Digital input/output
{ GP8_11 , NULL, 0 }, // Buffer disable
},
{ // Input port 2
{ GP8_12 , NULL, 0 }, // Pin 1 - I_ONB - 9V enable (high)
{ GP8_15 , NULL, 0 }, // Pin 2 - LEGDETB - Digital input pulled up
{ GP0_14 , NULL, 0 }, // Pin 5 - DIGIB0 - Digital input/output
{ GP0_13 , NULL, 0 }, // Pin 6 - DIGIB1 - Digital input/output
{ GP8_14 , NULL, 0 }, // Buffer disable
},
{ // Input port 3
{ GP8_9 , NULL, 0 }, // Pin 1 - I_ONC - 9V enable (high)
{ GP7_11 , NULL, 0 }, // Pin 2 - LEGDETC - Digital input pulled up
{ GP0_12 , NULL, 0 }, // Pin 5 - DIGIC0 - Digital input/output
{ GP1_14 , NULL, 0 }, // Pin 6 - DIGIC1 - Digital input/output
{ GP7_9 , NULL, 0 }, // Buffer disable
},
{ // Input port 4
{ GP6_4 , NULL, 0 }, // Pin 1 - I_OND - 9V enable (high)
{ GP7_8 , NULL, 0 }, // Pin 2 - LEGDETD - Digital input pulled up
{ GP0_1 , NULL, 0 }, // Pin 5 - DIGID0 - Digital input/output
{ GP1_15 , NULL, 0 }, // Pin 6 - DIGID1 - Digital input/output
{ GP7_10 , NULL, 0 }, // Buffer disable
},
};
INPIN EP2_OutputPortPin[][OUTPUT_PORT_PINS] =
{
{ // Output port 1
{ GP3_15 , NULL, 0 }, // Pin 1 - MAIN0
{ GP3_6 , NULL, 0 }, // Pin 2 - MAIN1
{ GP5_4 , NULL, 0 }, // Pin 5 - DETA0 TP18
{ GP5_11 , NULL, 0 }, // Pin 5 - INTA0
{ GP0_4 , NULL, 0 }, // Pin 6 - DIRA
},
{ // Output port 2
{ GP0_3 , NULL, 0 }, // Pin 1 - MBIN0
{ GP2_1 , NULL, 0 }, // Pin 2 - MBIN1
{ GP2_5 , NULL, 0 }, // Pin 5 - DETB0 TP19
{ GP5_8 , NULL, 0 }, // Pin 5 - INTB0
{ GP2_9 , NULL, 0 }, // Pin 6 - DIRB
},
{ // Output port 3
{ GP6_8 , NULL, 0 }, // Pin 1 - MCIN0
{ GP5_9 , NULL, 0 }, // Pin 2 - MCIN1
{ GP3_8 , NULL, 0 }, // Pin 5 - DETC0 TP20
{ GP5_13 , NULL, 0 }, // Pin 5 - INTC0
{ GP3_14 , NULL, 0 }, // Pin 6 - DIRC
},
{ // Output port 4
{ GP5_10 , NULL, 0 }, // Pin 1 - MDIN0
{ GP5_3 , NULL, 0 }, // Pin 2 - MDIN1
{ GP5_15 , NULL, 0 }, // Pin 5 - DETD0 TP21
{ GP6_9 , NULL, 0 }, // Pin 5 - INTD0
{ GP2_8 , NULL, 0 }, // Pin 6 - DIRD
},
};
INPIN EP2_AdcPowerPin[ADC_POWER_PINS] =
{
{ GP6_14 , NULL, 0 }, // 5VONIGEN
{ GP0_6 , NULL, 0 }, // ADCBATEN
};
// FINALB
INPIN FINALB_InputPortPin[][INPUT_PORT_PINS] =
{
{ // Input port 1
{ GP8_10 , NULL, 0 }, // Pin 1 - I_ONA - 9V enable (high)
{ GP2_2 , NULL, 0 }, // Pin 2 - LEGDETA - Digital input pulled up
{ GP0_2 , NULL, 0 }, // Pin 5 - DIGIA0 - Digital input/output
{ GP0_15 , NULL, 0 }, // Pin 6 - DIGIA1 - Digital input/output
{ GP8_11 , NULL, 0 }, // Buffer disable
},
{ // Input port 2
{ GP8_12 , NULL, 0 }, // Pin 1 - I_ONB - 9V enable (high)
{ GP8_15 , NULL, 0 }, // Pin 2 - LEGDETB - Digital input pulled up
{ GP0_14 , NULL, 0 }, // Pin 5 - DIGIB0 - Digital input/output
{ GP0_13 , NULL, 0 }, // Pin 6 - DIGIB1 - Digital input/output
{ GP8_14 , NULL, 0 }, // Buffer disable
},
{ // Input port 3
{ GP8_9 , NULL, 0 }, // Pin 1 - I_ONC - 9V enable (high)
{ GP7_11 , NULL, 0 }, // Pin 2 - LEGDETC - Digital input pulled up
{ GP0_12 , NULL, 0 }, // Pin 5 - DIGIC0 - Digital input/output
{ GP1_14 , NULL, 0 }, // Pin 6 - DIGIC1 - Digital input/output
{ GP7_9 , NULL, 0 }, // Buffer disable
},
{ // Input port 4
{ GP6_4 , NULL, 0 }, // Pin 1 - I_OND - 9V enable (high)
{ GP7_8 , NULL, 0 }, // Pin 2 - LEGDETD - Digital input pulled up
{ GP1_13 , NULL, 0 }, // Pin 5 - DIGID0 - Digital input/output
{ GP1_15 , NULL, 0 }, // Pin 6 - DIGID1 - Digital input/output
{ GP7_10 , NULL, 0 }, // Buffer disable
},
};
INPIN FINALB_OutputPortPin[][OUTPUT_PORT_PINS] =
{
{ // Output port 1
{ GP0_3 , NULL, 0 }, // Pin 1 - MAIN0
{ GP4_12 , NULL, 0 }, // Pin 2 - MAIN1
{ GP5_4 , NULL, 0 }, // Pin 5 - DETA0 TP18
{ GP5_11 , NULL, 0 }, // Pin 5 - INTA0
{ GP0_4 , NULL, 0 }, // Pin 6 - DIRA
},
{ // Output port 2
{ GP3_15 , NULL, 0 }, // Pin 1 - MBIN0
{ GP3_6 , NULL, 0 }, // Pin 2 - MBIN1
{ GP2_5 , NULL, 0 }, // Pin 5 - DETB0 TP19
{ GP5_8 , NULL, 0 }, // Pin 5 - INTB0
{ GP2_9 , NULL, 0 }, // Pin 6 - DIRB
},
{ // Output port 3
{ GP5_10 , NULL, 0 }, // Pin 1 - MCIN0
{ GP5_3 , NULL, 0 }, // Pin 2 - MCIN1
{ GP3_2 , NULL, 0 }, // Pin 5 - DETC0 TP20
{ GP5_13 , NULL, 0 }, // Pin 5 - INTC0
{ GP3_14 , NULL, 0 }, // Pin 6 - DIRC
},
{ // Output port 4
{ GP6_8 , NULL, 0 }, // Pin 1 - MDIN0
{ GP5_9 , NULL, 0 }, // Pin 2 - MDIN1
{ GP5_15 , NULL, 0 }, // Pin 5 - DETD0 TP21
{ GP6_9 , NULL, 0 }, // Pin 5 - INTD0
{ GP2_8 , NULL, 0 }, // Pin 6 - DIRD
},
};
INPIN FINALB_AdcPowerPin[ADC_POWER_PINS] =
{
{ GP6_14 , NULL, 0 }, // 5VONIGEN
{ GP0_6 , NULL, 0 }, // ADCBATEN
};
// FINAL
INPIN FINAL_InputPortPin[][INPUT_PORT_PINS] =
{
{ // Input port 1
{ GP8_10 , NULL, 0 }, // Pin 1 - I_ONA - 9V enable (high)
{ GP2_2 , NULL, 0 }, // Pin 2 - LEGDETA - Digital input pulled up
{ GP0_2 , NULL, 0 }, // Pin 5 - DIGIA0 - Digital input/output
{ GP0_15 , NULL, 0 }, // Pin 6 - DIGIA1 - Digital input/output
{ GP8_11 , NULL, 0 }, // Buffer disable
},
{ // Input port 2
{ GP8_12 , NULL, 0 }, // Pin 1 - I_ONB - 9V enable (high)
{ GP8_15 , NULL, 0 }, // Pin 2 - LEGDETB - Digital input pulled up
{ GP0_14 , NULL, 0 }, // Pin 5 - DIGIB0 - Digital input/output
{ GP0_13 , NULL, 0 }, // Pin 6 - DIGIB1 - Digital input/output
{ GP8_14 , NULL, 0 }, // Buffer disable
},
{ // Input port 3
{ GP8_9 , NULL, 0 }, // Pin 1 - I_ONC - 9V enable (high)
{ GP7_11 , NULL, 0 }, // Pin 2 - LEGDETC - Digital input pulled up
{ GP0_12 , NULL, 0 }, // Pin 5 - DIGIC0 - Digital input/output
{ GP1_14 , NULL, 0 }, // Pin 6 - DIGIC1 - Digital input/output
{ GP7_9 , NULL, 0 }, // Buffer disable
},
{ // Input port 4
{ GP6_4 , NULL, 0 }, // Pin 1 - I_OND - 9V enable (high)
{ GP7_8 , NULL, 0 }, // Pin 2 - LEGDETD - Digital input pulled up
{ GP1_13 , NULL, 0 }, // Pin 5 - DIGID0 - Digital input/output
{ GP1_15 , NULL, 0 }, // Pin 6 - DIGID1 - Digital input/output
{ GP7_10 , NULL, 0 }, // Buffer disable
},
};
INPIN FINAL_OutputPortPin[][OUTPUT_PORT_PINS] =
{
{ // Output port 1
{ GP0_3 , NULL, 0 }, // Pin 1 - MAIN0
{ GP4_12 , NULL, 0 }, // Pin 2 - MAIN1
{ GP5_4 , NULL, 0 }, // Pin 5 - DETA0 TP18
{ GP5_11 , NULL, 0 }, // Pin 5 - INTA0
{ GP0_4 , NULL, 0 }, // Pin 6 - DIRA
},
{ // Output port 2
{ GP3_15 , NULL, 0 }, // Pin 1 - MBIN0
{ GP3_6 , NULL, 0 }, // Pin 2 - MBIN1
{ GP2_5 , NULL, 0 }, // Pin 5 - DETB0 TP19
{ GP5_8 , NULL, 0 }, // Pin 5 - INTB0
{ GP2_9 , NULL, 0 }, // Pin 6 - DIRB
},
{ // Output port 3
{ GP5_10 , NULL, 0 }, // Pin 1 - MCIN0
{ GP5_3 , NULL, 0 }, // Pin 2 - MCIN1
{ GP3_2 , NULL, 0 }, // Pin 5 - DETC0 TP20
{ GP5_13 , NULL, 0 }, // Pin 5 - INTC0
{ GP3_14 , NULL, 0 }, // Pin 6 - DIRC
},
{ // Output port 4
{ GP6_8 , NULL, 0 }, // Pin 1 - MDIN0
{ GP5_9 , NULL, 0 }, // Pin 2 - MDIN1
{ GP5_15 , NULL, 0 }, // Pin 5 - DETD0 TP21
{ GP6_9 , NULL, 0 }, // Pin 5 - INTD0
{ GP2_8 , NULL, 0 }, // Pin 6 - DIRD
},
};
INPIN FINAL_AdcPowerPin[ADC_POWER_PINS] =
{
{ GP6_14 , NULL, 0 }, // 5VONIGEN
{ GP0_6 , NULL, 0 }, // ADCBATEN
};
#ifndef ADC_BITBANGING
INPIN AdcSpiPin[ADC_SPI_PINS] =
{
{ SPI0_MOSI , NULL, 0 }, // ADCMOSI
{ SPI0_MISO , NULL, 0 }, // ADCMISO
{ SPI0_SCL , NULL, 0 }, // ADCCLK
{ SPI0_CS , NULL, 0 } // ADCCS
};
#else
INPIN AdcSpiPin[ADC_SPI_PINS] =
{
{ GP8_5 , NULL, 0 }, // ADCMOSI
{ GP8_6 , NULL, 0 }, // ADCMISO
{ GP1_8 , NULL, 0 }, // ADCCLK
{ GP8_2 , NULL, 0 } // ADCCS
};
#endif
/* \endverbatim
* \n
*/
#define PUDisable iowrite32(ioread32(da8xx_syscfg1_base + 0x0C) & ~0xFFFFFFFF,da8xx_syscfg1_base + 0x0C)
INPIN *pInputPortPin[] =
{
[FINAL] = (INPIN*)&FINAL_InputPortPin[0], // FINAL platform
[FINALB] = (INPIN*)&FINALB_InputPortPin[0], // FINALB platform
[EP2] = (INPIN*)&EP2_InputPortPin[0], // EP2 platform
};
INPIN *pOutputPortPin[] =
{
[FINAL] = FINAL_OutputPortPin[0], // FINAL platform
[FINALB] = FINALB_OutputPortPin[0], // FINALB platform
[EP2] = EP2_OutputPortPin[0], // EP2 platform
};
INPIN *pAdcPowerPin[] =
{
[FINAL] = FINAL_AdcPowerPin, // FINAL platform
[FINALB] = FINALB_AdcPowerPin, // FINALB platform
[EP2] = EP2_AdcPowerPin, // EP2 platform
};
//*****************************************************************************
#define MODULE_NAME "analog_module"
#define DEVICE1_NAME ANALOG_DEVICE
#define DEVICE2_NAME TEST_PIN_DEVICE
#define DEVICE3_NAME DCM_DEVICE
static int ModuleInit(void);
static void ModuleExit(void);
#define __USE_POSIX
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/sched.h>
#ifndef PCASM
#ifndef DISABLE_PREEMPTED_VM
#include <linux/slab.h>
#endif
#include <linux/mm.h>
#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/uaccess.h>
#include <linux/debugfs.h>
#include <linux/ioport.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("The LEGO Group");
MODULE_DESCRIPTION(MODULE_NAME);
MODULE_SUPPORTED_DEVICE(DEVICE1_NAME);
module_init(ModuleInit);
module_exit(ModuleExit);
#else
// Keep Eclipse happy
#endif
static void __iomem *GpioBase;
void SetGpio(int Pin)
{
int Tmp = 0;
void __iomem *Reg;
if (Pin >= 0)
{
while ((MuxRegMap[Tmp].Pin != -1) && (MuxRegMap[Tmp].Pin != Pin))
{
Tmp++;
}
if (MuxRegMap[Tmp].Pin == Pin)
{
Reg = da8xx_syscfg0_base + 0x120 + (MuxRegMap[Tmp].MuxReg << 2);
*(u32*)Reg &= MuxRegMap[Tmp].Mask;
*(u32*)Reg |= MuxRegMap[Tmp].Mode;
if (Pin < NO_OF_GPIOS)
{
#ifdef DEBUG
printk(" GP%d_%-2d 0x%08X and 0x%08X or 0x%08X\n",(Pin >> 4),(Pin & 0x0F),(u32)Reg, MuxRegMap[Tmp].Mask, MuxRegMap[Tmp].Mode);
#endif
}
else
{
#ifdef DEBUG
printk(" ANALOG SPI FUNCTION 0x%08X and 0x%08X or 0x%08X\n",(u32)Reg, MuxRegMap[Tmp].Mask, MuxRegMap[Tmp].Mode);
#endif
}
}
else
{
printk("* GP%d_%-2d ********* ERROR not found *********\n",(Pin >> 4),(Pin & 0x0F));
}
}
}
void InitGpio(void)
{
int Port;
int Pin;
// unlock
REGUnlock;
memcpy(InputPortPin,pInputPortPin[Hw],sizeof(EP2_InputPortPin));
if (memcmp((const void*)InputPortPin,(const void*)pInputPortPin[Hw],sizeof(EP2_InputPortPin)) != 0)
{
printk("%s InputPortPin tabel broken!\n",MODULE_NAME);
}
for (Port = 0;Port < NO_OF_INPUT_PORTS;Port++)
{
#ifdef DEBUG
printk(" Input port %d\n",Port + 1);
#endif
for (Pin = 0;Pin < INPUT_PORT_PINS;Pin++)
{
if (InputPortPin[Port][Pin].Pin >= 0)
{
InputPortPin[Port][Pin].pGpio = (struct gpio_controller *__iomem)(GpioBase + ((InputPortPin[Port][Pin].Pin >> 5) * 0x28) + 0x10);
InputPortPin[Port][Pin].Mask = (1 << (InputPortPin[Port][Pin].Pin & 0x1F));
SetGpio(InputPortPin[Port][Pin].Pin);
}
}
}
#ifdef DEBUG
printk(" Adc spi\n");
#endif
for (Pin = 0;Pin < ADC_SPI_PINS;Pin++)
{
AdcSpiPin[Pin].pGpio = (struct gpio_controller *__iomem)(GpioBase + ((AdcSpiPin[Pin].Pin >> 5) * 0x28) + 0x10);
AdcSpiPin[Pin].Mask = (1 << (AdcSpiPin[Pin].Pin & 0x1F));
SetGpio(AdcSpiPin[Pin].Pin);
}
memcpy(OutputPortPin,pOutputPortPin[Hw],sizeof(EP2_OutputPortPin));
if (memcmp((const void*)OutputPortPin,(const void*)pOutputPortPin[Hw],sizeof(EP2_OutputPortPin)) != 0)
{
printk("%s OutputPortPin tabel broken!\n",MODULE_NAME);
}
for (Port = 0;Port < NO_OF_OUTPUT_PORTS;Port++)
{
#ifdef DEBUG
printk(" Output port %d\n",Port + 1);
#endif
for (Pin = 0;Pin < OUTPUT_PORT_PINS;Pin++)
{
if (OutputPortPin[Port][Pin].Pin >= 0)
{
OutputPortPin[Port][Pin].pGpio = (struct gpio_controller *__iomem)(GpioBase + ((OutputPortPin[Port][Pin].Pin >> 5) * 0x28) + 0x10);
OutputPortPin[Port][Pin].Mask = (1 << (OutputPortPin[Port][Pin].Pin & 0x1F));
SetGpio(OutputPortPin[Port][Pin].Pin);
}
}
}
#ifdef DEBUG
printk(" Adc power\n");
#endif
memcpy(AdcPowerPin,pAdcPowerPin[Hw],sizeof(EP2_AdcPowerPin));
if (memcmp((const void*)AdcPowerPin,(const void*)pAdcPowerPin[Hw],sizeof(EP2_AdcPowerPin)) != 0)
{
printk("%s AdcPowerPin tabel broken!\n",MODULE_NAME);
}
for (Pin = 0;Pin < ADC_POWER_PINS;Pin++)
{
if (AdcPowerPin[Pin].Pin >= 0)
{
AdcPowerPin[Pin].pGpio = (struct gpio_controller *__iomem)(GpioBase + ((AdcPowerPin[Pin].Pin >> 5) * 0x28) + 0x10);
AdcPowerPin[Pin].Mask = (1 << (AdcPowerPin[Pin].Pin & 0x1F));
SetGpio(AdcPowerPin[Pin].Pin);
}
}
// Disable pull up/down
PUDisable;
// lock
REGLock;
}
#define PINFloat(port,pin) {\
(*InputPortPin[port][pin].pGpio).dir |= InputPortPin[port][pin].Mask;\
}
#define PINRead(port,pin) ((*InputPortPin[port][pin].pGpio).in_data & InputPortPin[port][pin].Mask)
#define PINHigh(port,pin) {\
(*InputPortPin[port][pin].pGpio).set_data = InputPortPin[port][pin].Mask;\
(*InputPortPin[port][pin].pGpio).dir &= ~InputPortPin[port][pin].Mask;\
}
#define PINLow(port,pin) {\
(*InputPortPin[port][pin].pGpio).clr_data = InputPortPin[port][pin].Mask;\
(*InputPortPin[port][pin].pGpio).dir &= ~InputPortPin[port][pin].Mask;\
}
#define POUTFloat(port,pin) {\
(*OutputPortPin[port][pin].pGpio).dir |= OutputPortPin[port][pin].Mask;\
}
#define POUTRead(port,pin) ((*OutputPortPin[port][pin].pGpio).in_data & OutputPortPin[port][pin].Mask)
#define POUTHigh(port,pin) {\
(*OutputPortPin[port][pin].pGpio).set_data = OutputPortPin[port][pin].Mask;\
(*OutputPortPin[port][pin].pGpio).dir &= ~OutputPortPin[port][pin].Mask;\
}
#define POUTLow(port,pin) {\
(*OutputPortPin[port][pin].pGpio).clr_data = OutputPortPin[port][pin].Mask;\
(*OutputPortPin[port][pin].pGpio).dir &= ~OutputPortPin[port][pin].Mask;\
}
#define IGENOn {\
(*AdcPowerPin[ADCONIGEN].pGpio).set_data = AdcPowerPin[ADCONIGEN].Mask;\
(*AdcPowerPin[ADCONIGEN].pGpio).dir &= ~AdcPowerPin[ADCONIGEN].Mask;\
}
#define IGENOff {\
(*AdcPowerPin[ADCONIGEN].pGpio).clr_data = AdcPowerPin[ADCONIGEN].Mask;\
(*AdcPowerPin[ADCONIGEN].pGpio).dir &= ~AdcPowerPin[ADCONIGEN].Mask;\
}
#define BATENOn {\
(*AdcPowerPin[ADCBATEN].pGpio).set_data = AdcPowerPin[ADCBATEN].Mask;\
(*AdcPowerPin[ADCBATEN].pGpio).dir &= ~AdcPowerPin[ADCBATEN].Mask;\
}
#define BATENOff {\
(*AdcPowerPin[ADCBATEN].pGpio).clr_data = AdcPowerPin[ADCBATEN].Mask;\
(*AdcPowerPin[ADCBATEN].pGpio).dir &= ~AdcPowerPin[ADCBATEN].Mask;\
}
#ifdef DISABLE_OLD_COLOR
static UBYTE CtoH(UBYTE Char)
{
UBYTE Hex = 0;
if ((Char >= '0') && (Char <= '9'))
{
Hex = Char - '0';
}
else
{
if ((Char >= 'a') && (Char <= 'f'))
{
Hex = Char - 'a' + 10;
}
else
{
if ((Char >= 'A') && (Char <= 'F'))
{
Hex = Char - 'A' + 10;
}
}
}
return (Hex);
}
#endif
// SW SPI *********************************************************************
#ifndef ADC_BITBANGING
#define SPI0_CLOCK 150000000
#define ADC_TIME 8 // [uS]
#define ADC_CLOCK (((ULONG)1000000 * (ULONG)16) / (ULONG)ADC_TIME)
#define CNVSPD (((ULONG)SPI0_CLOCK / (ULONG)ADC_CLOCK) - 1)
/* SPI Register number */
#define SPIGCR0 0
#define SPIGCR1 1
#define SPIINT0 2
#define SPILVL 3
#define SPIFLG 4
#define SPIPC0 5
#define SPIPC1 6
#define SPIPC2 7
#define SPIPC3 8
#define SPIPC4 9
#define SPIPC5 10
#define SPIDAT0 14
#define SPIDAT1 15
#define SPIBUF 16
#define SPIEMU 17
#define SPIDELAY 18
#define SPIDEF 19
#define SPIFMT0 20
#define SPIFMT1 21
#define SPIFMT2 22
#define SPIFMT3 23
#define INTVEC1 25
static volatile unsigned long *Spi0;
#define SPIRxempty (Spi0[SPIBUF] & 0x80000000)
#define SPITxfull (Spi0[SPIBUF] & 0x20000000)
#define SPIInit {\
Spi0[SPIGCR1] = 0x00000003; /* Master enable */\
Spi0[SPIPC0] = 0x00000E08; /* */\
Spi0[SPIDAT1] = 0x0; /* Format 0 is selected */\