wiring_lmk 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/ext/wiringpi/WiringPi/devLib/ds1302.c +254 -0
  3. data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
  4. data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
  5. data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
  6. data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
  7. data/ext/wiringpi/WiringPi/devLib/lcd.c +507 -0
  8. data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
  9. data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +684 -0
  10. data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
  11. data/ext/wiringpi/WiringPi/devLib/maxdetect.c +167 -0
  12. data/ext/wiringpi/WiringPi/devLib/maxdetect.h +40 -0
  13. data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
  14. data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
  15. data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +178 -0
  16. data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
  17. data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
  18. data/ext/wiringpi/WiringPi/devLib/piNes.c +117 -0
  19. data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
  20. data/ext/wiringpi/WiringPi/gpio/extensions.c +700 -0
  21. data/ext/wiringpi/WiringPi/gpio/extensions.h +26 -0
  22. data/ext/wiringpi/WiringPi/gpio/gpio.c +1280 -0
  23. data/ext/wiringpi/WiringPi/gpio/pins.c +33 -0
  24. data/ext/wiringpi/WiringPi/gpio/readall.c +337 -0
  25. data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +201 -0
  26. data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
  27. data/ext/wiringpi/WiringPi/wiringPi/max31855.c +99 -0
  28. data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
  29. data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
  30. data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
  31. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
  32. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
  33. data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
  34. data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
  35. data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
  36. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
  37. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
  38. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +189 -0
  39. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
  40. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +234 -0
  41. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
  42. data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
  43. data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
  44. data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
  45. data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
  46. data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
  47. data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
  48. data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +113 -0
  49. data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
  50. data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
  51. data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
  52. data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
  53. data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
  54. data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +92 -0
  55. data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
  56. data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
  57. data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
  58. data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
  59. data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
  60. data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +168 -0
  61. data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
  62. data/ext/wiringpi/WiringPi/wiringPi/softServo.c +247 -0
  63. data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
  64. data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
  65. data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
  66. data/ext/wiringpi/WiringPi/wiringPi/sr595.c +113 -0
  67. data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
  68. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +3688 -0
  69. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +229 -0
  70. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +236 -0
  71. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
  72. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +121 -0
  73. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +35 -0
  74. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +253 -0
  75. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
  76. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
  77. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
  78. data/ext/wiringpi/extconf.rb +8 -0
  79. data/ext/wiringpi/wiringpi_wrap.c +4396 -0
  80. data/lib/wiring_lmk.rb +42 -0
  81. data/lib/wiring_lmk/event.rb +21 -0
  82. data/lib/wiring_lmk/gpio.rb +139 -0
  83. data/lib/wiring_lmk/i2c.rb +26 -0
  84. data/lib/wiring_lmk/mcp23x17.rb +31 -0
  85. data/lib/wiring_lmk/serial.rb +49 -0
  86. data/lib/wiring_lmk/spi.rb +15 -0
  87. metadata +136 -0
@@ -0,0 +1,35 @@
1
+ /*
2
+ * softServo.h:
3
+ * Provide N channels of software driven PWM suitable for RC
4
+ * servo motors.
5
+ * Copyright (c) 2012 Gordon Henderson
6
+ ***********************************************************************
7
+ * This file is part of wiringPi:
8
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
9
+ *
10
+ * wiringPi is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU Lesser General Public License as
12
+ * published by the Free Software Foundation, either version 3 of the
13
+ * License, or (at your option) any later version.
14
+ *
15
+ * wiringPi is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ * GNU Lesser General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU Lesser General Public
21
+ * License along with wiringPi.
22
+ * If not, see <http://www.gnu.org/licenses/>.
23
+ ***********************************************************************
24
+ */
25
+
26
+ #ifdef __cplusplus
27
+ extern "C" {
28
+ #endif
29
+
30
+ extern void softServoWrite (int pin, int value) ;
31
+ extern int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) ;
32
+
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
@@ -0,0 +1,150 @@
1
+ /*
2
+ * softTone.c:
3
+ * For that authentic retro sound...
4
+ * Er... A little experiment to produce tones out of a Pi using
5
+ * one (or 2) GPIO pins and a piezeo "speaker" element.
6
+ * (Or a high impedance speaker, but don'y blame me if you blow-up
7
+ * the GPIO pins!)
8
+ * Copyright (c) 2012 Gordon Henderson
9
+ ***********************************************************************
10
+ * This file is part of wiringPi:
11
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
12
+ *
13
+ * wiringPi is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU Lesser General Public License as
15
+ * published by the Free Software Foundation, either version 3 of the
16
+ * License, or (at your option) any later version.
17
+ *
18
+ * wiringPi is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU Lesser General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU Lesser General Public
24
+ * License along with wiringPi.
25
+ * If not, see <http://www.gnu.org/licenses/>.
26
+ ***********************************************************************
27
+ */
28
+
29
+ #include <stdio.h>
30
+ #include <pthread.h>
31
+
32
+ #include "wiringPi.h"
33
+ #include "softTone.h"
34
+
35
+ #define MAX_PINS 64
36
+
37
+ #define PULSE_TIME 100
38
+
39
+ static int freqs [MAX_PINS] ;
40
+ static pthread_t threads [MAX_PINS] ;
41
+
42
+ static int newPin = -1 ;
43
+
44
+
45
+ /*
46
+ * softToneThread:
47
+ * Thread to do the actual PWM output
48
+ *********************************************************************************
49
+ */
50
+
51
+ static PI_THREAD (softToneThread)
52
+ {
53
+ int pin, freq, halfPeriod ;
54
+ struct sched_param param ;
55
+
56
+ param.sched_priority = sched_get_priority_max (SCHED_RR) ;
57
+ pthread_setschedparam (pthread_self (), SCHED_RR, &param) ;
58
+
59
+ pin = newPin ;
60
+ newPin = -1 ;
61
+
62
+ piHiPri (50) ;
63
+
64
+ for (;;)
65
+ {
66
+ freq = freqs [pin] ;
67
+ if (freq == 0)
68
+ delay (1) ;
69
+ else
70
+ {
71
+ halfPeriod = 500000 / freq ;
72
+
73
+ digitalWrite (pin, HIGH) ;
74
+ delayMicroseconds (halfPeriod) ;
75
+
76
+ digitalWrite (pin, LOW) ;
77
+ delayMicroseconds (halfPeriod) ;
78
+ }
79
+ }
80
+
81
+ return NULL ;
82
+ }
83
+
84
+
85
+ /*
86
+ * softToneWrite:
87
+ * Write a frequency value to the given pin
88
+ *********************************************************************************
89
+ */
90
+
91
+ void softToneWrite (int pin, int freq)
92
+ {
93
+ pin &= 63 ;
94
+
95
+ /**/ if (freq < 0)
96
+ freq = 0 ;
97
+ else if (freq > 5000) // Max 5KHz
98
+ freq = 5000 ;
99
+
100
+ freqs [pin] = freq ;
101
+ }
102
+
103
+
104
+ /*
105
+ * softToneCreate:
106
+ * Create a new tone thread.
107
+ *********************************************************************************
108
+ */
109
+
110
+ int softToneCreate (int pin)
111
+ {
112
+ int res ;
113
+ pthread_t myThread ;
114
+
115
+ pinMode (pin, OUTPUT) ;
116
+ digitalWrite (pin, LOW) ;
117
+
118
+ if (threads [pin] != 0)
119
+ return -1 ;
120
+
121
+ freqs [pin] = 0 ;
122
+
123
+ newPin = pin ;
124
+ res = pthread_create (&myThread, NULL, softToneThread, NULL) ;
125
+
126
+ while (newPin != -1)
127
+ delay (1) ;
128
+
129
+ threads [pin] = myThread ;
130
+
131
+ return res ;
132
+ }
133
+
134
+
135
+ /*
136
+ * softToneStop:
137
+ * Stop an existing softTone thread
138
+ *********************************************************************************
139
+ */
140
+
141
+ void softToneStop (int pin)
142
+ {
143
+ if (threads [pin] != 0)
144
+ {
145
+ pthread_cancel (threads [pin]) ;
146
+ pthread_join (threads [pin], NULL) ;
147
+ threads [pin] = 0 ;
148
+ digitalWrite (pin, LOW) ;
149
+ }
150
+ }
@@ -0,0 +1,39 @@
1
+ /*
2
+ * softTone.c:
3
+ * For that authentic retro sound...
4
+ * Er... A little experiment to produce tones out of a Pi using
5
+ * one (or 2) GPIO pins and a piezeo "speaker" element.
6
+ * (Or a high impedance speaker, but don'y blame me if you blow-up
7
+ * the GPIO pins!)
8
+ * Copyright (c) 2012 Gordon Henderson
9
+ ***********************************************************************
10
+ * This file is part of wiringPi:
11
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
12
+ *
13
+ * wiringPi is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU Lesser General Public License as
15
+ * published by the Free Software Foundation, either version 3 of the
16
+ * License, or (at your option) any later version.
17
+ *
18
+ * wiringPi is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU Lesser General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU Lesser General Public
24
+ * License along with wiringPi.
25
+ * If not, see <http://www.gnu.org/licenses/>.
26
+ ***********************************************************************
27
+ */
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ extern int softToneCreate (int pin) ;
34
+ extern void softToneStop (int pin) ;
35
+ extern void softToneWrite (int pin, int freq) ;
36
+
37
+ #ifdef __cplusplus
38
+ }
39
+ #endif
@@ -0,0 +1,113 @@
1
+ /*
2
+ * sr595.c:
3
+ * Extend wiringPi with the 74x595 shift register as a GPIO
4
+ * expander chip.
5
+ * Note that the code can cope with a number of 595's
6
+ * daisy-chained together - up to 4 for now as we're storing
7
+ * the output "register" in a single unsigned int.
8
+ *
9
+ * Copyright (c) 2013 Gordon Henderson
10
+ ***********************************************************************
11
+ * This file is part of wiringPi:
12
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
13
+ *
14
+ * wiringPi is free software: you can redistribute it and/or modify
15
+ * it under the terms of the GNU Lesser General Public License as
16
+ * published by the Free Software Foundation, either version 3 of the
17
+ * License, or (at your option) any later version.
18
+ *
19
+ * wiringPi is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ * GNU Lesser General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU Lesser General Public
25
+ * License along with wiringPi.
26
+ * If not, see <http://www.gnu.org/licenses/>.
27
+ ***********************************************************************
28
+ */
29
+
30
+ #include <stdio.h>
31
+ #include <stdint.h>
32
+
33
+ #include "wiringPi.h"
34
+
35
+ #include "sr595.h"
36
+
37
+
38
+ /*
39
+ * myDigitalWrite:
40
+ *********************************************************************************
41
+ */
42
+
43
+ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
44
+ {
45
+ unsigned int mask ;
46
+ int dataPin, clockPin, latchPin ;
47
+ int bit, bits, output ;
48
+
49
+ pin -= node->pinBase ; // Normalise pin number
50
+ bits = node->pinMax - node->pinBase + 1 ; // ie. number of clock pulses
51
+ dataPin = node->data0 ;
52
+ clockPin = node->data1 ;
53
+ latchPin = node->data2 ;
54
+ output = node->data3 ;
55
+
56
+ mask = 1 << pin ;
57
+
58
+ if (value == LOW)
59
+ output &= (~mask) ;
60
+ else
61
+ output |= mask ;
62
+
63
+ node->data3 = output ;
64
+
65
+ // A low -> high latch transition copies the latch to the output pins
66
+
67
+ digitalWrite (latchPin, LOW) ;
68
+ delayMicroseconds (1) ;
69
+ for (bit = bits - 1 ; bit >= 0 ; --bit)
70
+ {
71
+ digitalWrite (dataPin, output & (1 << bit)) ;
72
+
73
+ digitalWrite (clockPin, HIGH) ;
74
+ delayMicroseconds (1) ;
75
+ digitalWrite (clockPin, LOW) ;
76
+ delayMicroseconds (1) ;
77
+ }
78
+ digitalWrite (latchPin, HIGH) ;
79
+ delayMicroseconds (1) ;
80
+ }
81
+
82
+
83
+ /*
84
+ * sr595Setup:
85
+ * Create a new instance of a 74x595 shift register GPIO expander.
86
+ *********************************************************************************
87
+ */
88
+
89
+ int sr595Setup (const int pinBase, const int numPins,
90
+ const int dataPin, const int clockPin, const int latchPin)
91
+ {
92
+ struct wiringPiNodeStruct *node ;
93
+
94
+ node = wiringPiNewNode (pinBase, numPins) ;
95
+
96
+ node->data0 = dataPin ;
97
+ node->data1 = clockPin ;
98
+ node->data2 = latchPin ;
99
+ node->data3 = 0 ; // Output register
100
+ node->digitalWrite = myDigitalWrite ;
101
+
102
+ // Initialise the underlying hardware
103
+
104
+ digitalWrite (dataPin, LOW) ;
105
+ digitalWrite (clockPin, LOW) ;
106
+ digitalWrite (latchPin, HIGH) ;
107
+
108
+ pinMode (dataPin, OUTPUT) ;
109
+ pinMode (clockPin, OUTPUT) ;
110
+ pinMode (latchPin, OUTPUT) ;
111
+
112
+ return 0 ;
113
+ }
@@ -0,0 +1,34 @@
1
+ /*
2
+ * sr595.h:
3
+ * Extend wiringPi with the 74x595 shift registers.
4
+ * Copyright (c) 2013 Gordon Henderson
5
+ ***********************************************************************
6
+ * This file is part of wiringPi:
7
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
8
+ *
9
+ * wiringPi is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * wiringPi is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with wiringPi.
21
+ * If not, see <http://www.gnu.org/licenses/>.
22
+ ***********************************************************************
23
+ */
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ extern int sr595Setup (const int pinBase, const int numPins,
30
+ const int dataPin, const int clockPin, const int latchPin) ;
31
+
32
+ #ifdef __cplusplus
33
+ }
34
+ #endif
@@ -0,0 +1,3688 @@
1
+ /*
2
+ * wiringPi:
3
+ * Arduino compatable (ish) Wiring library for the Raspberry Pi
4
+ * Copyright (c) 2012 Gordon Henderson
5
+ * Additional code for pwmSetClock by Chris Hall <chris@kchall.plus.com>
6
+ *
7
+ * Thanks to code samples from Gert Jan van Loo and the
8
+ * BCM2835 ARM Peripherals manual, however it's missing
9
+ * the clock section /grr/mutter/
10
+ ***********************************************************************
11
+ * This file is part of wiringPi:
12
+ * https://projects.drogon.net/raspberry-pi/wiringpi/
13
+ *
14
+ * wiringPi is free software: you can redistribute it and/or modify
15
+ * it under the terms of the GNU Lesser General Public License as
16
+ * published by the Free Software Foundation, either version 3 of the
17
+ * License, or (at your option) any later version.
18
+ *
19
+ * wiringPi is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ * GNU Lesser General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU Lesser General Public
25
+ * License along with wiringPi.
26
+ * If not, see <http://www.gnu.org/licenses/>.
27
+ ***********************************************************************
28
+ */
29
+
30
+ // Revisions:
31
+ // 19 Jul 2012:
32
+ // Moved to the LGPL
33
+ // Added an abstraction layer to the main routines to save a tiny
34
+ // bit of run-time and make the clode a little cleaner (if a little
35
+ // larger)
36
+ // Added waitForInterrupt code
37
+ // Added piHiPri code
38
+ //
39
+ // 9 Jul 2012:
40
+ // Added in support to use the /sys/class/gpio interface.
41
+ // 2 Jul 2012:
42
+ // Fixed a few more bugs to do with range-checking when in GPIO mode.
43
+ // 11 Jun 2012:
44
+ // Fixed some typos.
45
+ // Added c++ support for the .h file
46
+ // Added a new function to allow for using my "pin" numbers, or native
47
+ // GPIO pin numbers.
48
+ // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
49
+ //
50
+ // 02 May 2012:
51
+ // Added in the 2 UART pins
52
+ // Change maxPins to numPins to more accurately reflect purpose
53
+
54
+
55
+ #include <stdio.h>
56
+ #include <stdarg.h>
57
+ #include <stdint.h>
58
+ #include <stdlib.h>
59
+ #include <ctype.h>
60
+ #include <poll.h>
61
+ #include <unistd.h>
62
+ #include <errno.h>
63
+ #include <string.h>
64
+ #include <time.h>
65
+ #include <fcntl.h>
66
+ #include <pthread.h>
67
+ #include <sys/time.h>
68
+ #include <sys/mman.h>
69
+ #include <sys/stat.h>
70
+ #include <sys/wait.h>
71
+ #include <sys/ioctl.h>
72
+
73
+ #include "softPwm.h"
74
+ #include "softTone.h"
75
+
76
+ #include "wiringPi.h"
77
+
78
+ #ifndef TRUE
79
+ #define TRUE (1==1)
80
+ #define FALSE (1==2)
81
+ #endif
82
+
83
+ // Environment Variables
84
+
85
+ #define ENV_DEBUG "WIRINGPI_DEBUG"
86
+ #define ENV_CODES "WIRINGPI_CODES"
87
+
88
+
89
+ // Mask for the bottom 64 pins which belong to the Banana Pro
90
+ // The others are available for the other devices
91
+
92
+ #define PI_GPIO_MASK (0xFFFFFFC0)
93
+
94
+ struct wiringPiNodeStruct *wiringPiNodes = NULL ;
95
+
96
+
97
+ // Access from ARM Running Linux
98
+ // Taken from Gert/Doms code. Some of this is not in the manual
99
+ // that I can find )-:
100
+
101
+ #define PAGE_SIZE (4*1024)
102
+ #define BLOCK_SIZE (4*1024)
103
+
104
+ // Locals to hold pointers to the hardware
105
+
106
+ static volatile uint32_t *gpio ;
107
+ static volatile uint32_t *pwm ;
108
+ static volatile uint32_t *clk ;
109
+ static volatile uint32_t *pads ;
110
+
111
+ #ifdef USE_TIMER
112
+ static volatile uint32_t *timer ;
113
+ static volatile uint32_t *timerIrqRaw ;
114
+ #endif
115
+
116
+ /*add for BananaPro by LeMaker team*/
117
+ // for mmap BananaPro
118
+ #define MAX_PIN_NUM (0x40) //64
119
+ #define SUNXI_GPIO_BASE (0x01C20800)
120
+ #define MAP_SIZE (4096*2)
121
+ #define MAP_MASK (MAP_SIZE - 1)
122
+ //sunxi_pwm
123
+ #define SUNXI_PWM_BASE (0x01c20e00)
124
+ #define SUNXI_PWM_CTRL_REG (SUNXI_PWM_BASE)
125
+ #define SUNXI_PWM_CH0_PERIOD (SUNXI_PWM_BASE + 0x4)
126
+ #define SUNXI_PWM_CH1_PERIOD (SUNXI_PWM_BASE + 0x8)
127
+
128
+ #define SUNXI_PWM_CH0_EN (1 << 4)
129
+ #define SUNXI_PWM_CH0_ACT_STA (1 << 5)
130
+ #define SUNXI_PWM_SCLK_CH0_GATING (1 << 6)
131
+ #define SUNXI_PWM_CH0_MS_MODE (1 << 7) //pulse mode
132
+ #define SUNXI_PWM_CH0_PUL_START (1 << 8)
133
+
134
+ #define SUNXI_PWM_CH1_EN (1 << 19)
135
+ #define SUNXI_PWM_CH1_ACT_STA (1 << 20)
136
+ #define SUNXI_PWM_SCLK_CH1_GATING (1 << 21)
137
+ #define SUNXI_PWM_CH1_MS_MODE (1 << 22) //pulse mode
138
+ #define SUNXI_PWM_CH1_PUL_START (1 << 23)
139
+
140
+
141
+ #define PWM_CLK_DIV_120 0
142
+ #define PWM_CLK_DIV_180 1
143
+ #define PWM_CLK_DIV_240 2
144
+ #define PWM_CLK_DIV_360 3
145
+ #define PWM_CLK_DIV_480 4
146
+ #define PWM_CLK_DIV_12K 8
147
+ #define PWM_CLK_DIV_24K 9
148
+ #define PWM_CLK_DIV_36K 10
149
+ #define PWM_CLK_DIV_48K 11
150
+ #define PWM_CLK_DIV_72K 12
151
+
152
+ #define GPIO_PADS_BP (0x00100000)
153
+ #define CLOCK_BASE_BP (0x00101000)
154
+ // addr should 4K*n
155
+ #define GPIO_BASE_BP (0x01C20000)
156
+ #define GPIO_TIMER_BP (0x0000B000)
157
+ #define GPIO_PWM_BP (0x01c20000) //need 4k*n
158
+
159
+ static int wiringPinMode = WPI_MODE_UNINITIALISED ;
160
+ int wiringPiCodes = FALSE ;
161
+ /*end 2014.09.18*/
162
+
163
+
164
+
165
+ //add for S500
166
+ #define S500_MAX_PIN_NUM (0x40) //64
167
+ #define S500_GPIO_PADS (0x00100000)
168
+ #define S500_CLOCK_BASE (0xB0160000)
169
+ #define S500_GPIO_BASE (0xB01B0000)
170
+ #define S500_GPIO_TIMER (0xB0168000)
171
+ #define S500_GPIO_PWM (0xB01B0050)
172
+
173
+
174
+ static int s500_physToGpio [64] =
175
+ {
176
+ -1, //0
177
+ -1, -1, //1 2
178
+ 131, -1, //3(SDA2), 4
179
+ 130, -1, //5(SCK2), 6
180
+ 50, 91, //7(B18), 8(UART0_TX)
181
+ -1, 90, //9, 10(UART0_RX)
182
+ 64, 40, //11(C0), 12(B8-PWM)
183
+ 65, -1, //13(C1), 14
184
+ 68, 25, //15(C4), 16(A25)
185
+ -1, 70, //17, 18(C6)
186
+ 89, -1, //19(MOSI),20
187
+ 88, 69, //21(MISO),22(C5)
188
+ 86, 87, //23(SCLK),24(SS)
189
+ -1, 51, //25, 26(GPIOB19)
190
+ 48, 46, //27(B16), 28(B14)
191
+ 47, -1, //29(B15), 30
192
+ 42, 45, //31(B10), 32(B13)
193
+ 32, -1, //33(B0), 34
194
+ 33, 28, //35(B1), 36(A28)
195
+ 34, 31, //37(B2), 38(A31)
196
+ -1, 27, //39, 40(A27)
197
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //41-> 55
198
+ -1, -1, -1, -1, -1, -1, -1, -1 // 56-> 63
199
+ } ;
200
+
201
+ static int s500_pinToGpio [64] =
202
+ {
203
+ 64, 40, 65, 68, 25, 70, 69, 50, //WPI MAP: 0-7
204
+ 131, 130, // I2C - SDA0, SCL0 wpi 8 - 9
205
+ 87, 51, // SPI - CE1, CE0 wpi 10 - 11
206
+ 89, 88, 86, // SPI - MOSI, MISO, SCLK wpi 12 - 14
207
+ 91, 90, // UART - Tx, Rx wpi 15 - 16
208
+ -1, -1, -1, -1, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
209
+ 47, 42, 32, 33, 34, // B+ wpi 21, 22, 23, 24, 25
210
+ 45, 28, 31, 27, // B+ wpi 26, 27, 28, 29
211
+ 48, 46, // B+ wpi 30, 31
212
+
213
+ // Padding:
214
+
215
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 47
216
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63
217
+ } ;
218
+
219
+ static int s500_pinTobcm [64] =
220
+ {
221
+ 48, 46, //map to BCM GPIO0,1
222
+ 131, 130, //map to BCM GPIO2,3
223
+ 50, 47, //map to BCM GPIO4,5
224
+ 42, 51, //map to BCM GPIO6,7
225
+ 87, 88, //map to BCM GPIO8,9
226
+ 89, 86, //map to BCM GPIO10,11
227
+ 45, 32, //map to BCM GPIO12,13
228
+ 91, 90, //map to BCM GPIO14,15
229
+ 28, 64, //map to BCM GPIO16,17
230
+ 40, 33, //map to BCM GPIO18,19
231
+ 31, 27, //map to BCM GPIO20,21
232
+ 68, 25, //map to BCM GPIO22,23
233
+ 70, 69, //map to BCM GPIO24,25
234
+ 34, 65, //map to BCM GPIO26,27
235
+ -1, -1, //map to BCM GPIO28,29
236
+ -1, -1, //map to BCM GPIO30,31
237
+ -1, -1, //map to BCM GPIO32,33
238
+ -1, -1, //map to BCM GPIO34,35
239
+ -1, -1, //map to BCM GPIO36,37
240
+ -1, -1, //map to BCM GPIO38,39
241
+ -1, -1, //map to BCM GPIO40
242
+ -1, -1, //28... 43
243
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //44... 59
244
+ -1, -1, -1, -1 // ...63
245
+ } ;
246
+
247
+
248
+
249
+ static int s500ValidGpio [132] =
250
+ {
251
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //0-> 15
252
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, 25, -1, 27, 28, -1, -1, 31, //16-> 31
253
+ 32, 33, 34, -1, -1, -1, -1, -1, 40, 41, 42, -1, -1, 45, 46, 47, //32-> 47
254
+ 48, -1, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //48-> 63
255
+ 64, 65, -1, -1, 68, 69, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //64-> 79
256
+ -1, -1, -1, -1, -1, -1, 86, 87, 88, 89, 90, 91, -1, -1, -1, -1, //80-> 95
257
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //96-> 111
258
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //112-> 127
259
+ -1, -1, 130, 131 //128->131
260
+ } ;
261
+
262
+
263
+
264
+
265
+ // Data for use with the boardId functions.
266
+ // The order of entries here to correspond with the PI_MODEL_X
267
+ // and PI_VERSION_X defines in wiringPi.h
268
+ // Only intended for the gpio command - use at your own risk!
269
+
270
+ const char *piModelNames [7] =
271
+ {
272
+ "Unknown",
273
+ "Model A",
274
+ "Model B",
275
+ "Model B+",
276
+ "Compute Module",
277
+ "Banana Pro", //add for BananaPro by LeMaker team
278
+ "Guitar"
279
+ } ;
280
+
281
+ const char *piRevisionNames [5] =
282
+ {
283
+ "Unknown",
284
+ "1",
285
+ "1.1",
286
+ "1.2",
287
+ "2",
288
+ } ;
289
+
290
+ const char *piMakerNames [5] =
291
+ {
292
+ "Unknown",
293
+ "Egoman",
294
+ "Sony",
295
+ "Qusda",
296
+ "LeMaker", //add for BananaPro by LeMaker team
297
+ } ;
298
+
299
+
300
+ // Time for easy calculations
301
+
302
+ static uint64_t epochMilli, epochMicro ;
303
+
304
+ // Misc
305
+
306
+ static int wiringPiMode = WPI_MODE_UNINITIALISED ;
307
+ static volatile int pinPass = -1 ;
308
+ static pthread_mutex_t pinMutex ;
309
+
310
+ // Debugging & Return codes
311
+
312
+ int wiringPiDebug = FALSE ;
313
+ int wiringPiReturnCodes = FALSE ;
314
+
315
+ // sysFds:
316
+ // Map a file descriptor from the /sys/class/gpio/gpioX/value
317
+
318
+ static int sysFds [64] =
319
+ {
320
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
321
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
322
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
323
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
324
+ } ;
325
+
326
+
327
+ static int s500_sysFds [132] =
328
+ {
329
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
330
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
331
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
332
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
333
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
334
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
335
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
336
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
337
+ -1, -1, -1, -1
338
+ } ;
339
+
340
+
341
+
342
+ // ISR Data
343
+
344
+ static void (*isrFunctions [64])(void) ;
345
+
346
+
347
+ // Doing it the Arduino way with lookup tables...
348
+ // Yes, it's probably more innefficient than all the bit-twidling, but it
349
+ // does tend to make it all a bit clearer. At least to me!
350
+
351
+ // pinToGpio:
352
+ // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
353
+ // Cope for 3 different board revisions here.
354
+
355
+ static int *pinToGpio ;
356
+
357
+
358
+
359
+ // physToGpio:
360
+ // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
361
+ // Cope for 2 different board revisions here.
362
+ // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
363
+
364
+ static int *physToGpio ;
365
+
366
+
367
+ /*add for BananaPro by LeMaker team*/
368
+ //map tableb for BP
369
+
370
+ static int *physToPin ;
371
+
372
+ static int upDnConvert[3] = {0, 2, 1};
373
+
374
+ static int pinToGpio_BP [64] =
375
+ {
376
+ 275, 259,
377
+ 274, 273,
378
+ 244, 245,
379
+ 272, 226,
380
+ 53, 52,
381
+ 266, 270,
382
+ 268, 269,
383
+ 267, 228,
384
+ 229, -1,
385
+ -1, -1,
386
+ -1, 35,
387
+ 277, 45,
388
+ 39, 37,
389
+ 276, 38,
390
+ 44, 40,
391
+ 257, 256, // ...31
392
+
393
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
394
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63
395
+ } ;
396
+
397
+ static int pinTobcm_BP [64] =
398
+ {
399
+ 257, 256, //map to BCM GPIO0,1
400
+ 53, 52, //map to BCM GPIO2,3
401
+ 226, 35, //map to BCM GPIO4,5
402
+ 277, 270, //map to BCM GPIO6,7
403
+ 266, 269, //map to BCM GPIO8,9
404
+ 268, 267, //map to BCM GPIO10,11
405
+ 276, 45, //map to BCM GPIO12,13
406
+ 228, 229, //map to BCM GPIO14,15
407
+ 38, 275, //map to BCM GPIO16,17
408
+ 259, 39, //map to BCM GPIO18,19
409
+ 44, 40, //map to BCM GPIO20,21
410
+ 273, 244, //map to BCM GPIO22,23
411
+ 245, 272, //map to BCM GPIO24,25
412
+ 37, 274, //map to BCM GPIO26,27
413
+
414
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 29... 44
415
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //45... 60
416
+ -1, -1, -1, -1 // ...63
417
+ } ;
418
+ static int physToGpio_BP [64] =
419
+ {
420
+ -1, // 0
421
+ -1, -1, //1, 2
422
+ 53, -1, //3, 4
423
+ 52, -1, //5, 6
424
+ 226, 228, //7, 8
425
+ -1, 229, //9, 10
426
+ 275, 259, //11, 12
427
+ 274, -1, //13, 14
428
+ 273, 244, //15, 16
429
+ -1, 245, //17, 18
430
+ 268, -1, //19, 20
431
+ 269, 272, //21, 22
432
+ 267, 266, //23, 24
433
+ -1, 270, //25, 26
434
+ 257, 256, //27, 28
435
+ 35, -1, //29, 30
436
+ 277, 276, //31, 32
437
+ 45, -1, //33, 34
438
+ 39, 38, //35, 36
439
+ 37, 44, //37, 38
440
+ -1, 40, //39, 40
441
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //41-> 55
442
+ -1, -1, -1, -1, -1, -1, -1, -1 // 56-> 63
443
+ } ;
444
+
445
+
446
+ static int syspin [64] =
447
+ {
448
+ -1, -1, 2, 3, 4, 5, 6, 7, //GPIO0,1 used to I2C
449
+ 8, 9, 10, 11, 12, 13, 14, 15,
450
+ 16, 17, 18, 19, 20, 21, 22, 23,
451
+ 24, 25, 26, 27, -1, -1, -1, -1,
452
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
453
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
454
+ } ;
455
+
456
+ static int edge [64] =
457
+ {
458
+ -1, -1, -1, -1, 4, -1, -1, 7, //support the INT
459
+ 8, 9, 10, 11, -1, -1, 14, 15,
460
+ -1, 17, -1, -1, -1, -1, 22, 23,
461
+ 24, 25, -1, 27, -1, -1, -1, -1,
462
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
463
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
464
+ } ;
465
+
466
+ static int pinToGpioR [64] =
467
+ {
468
+ 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
469
+ 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
470
+ 8, 7, // SPI - CE1, CE0 wpi 10 - 11
471
+ 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
472
+ 14, 15, // UART - Tx, Rx wpi 15 - 16
473
+ -1, -1, -1, -1, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
474
+ 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
475
+ 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
476
+ 0, 1, // B+ wpi 30, 31
477
+
478
+ // Padding:
479
+
480
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
481
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
482
+ } ;
483
+
484
+ static int physToGpioR [64] =//head num map to BCMpin
485
+ {
486
+ -1, // 0
487
+ -1, -1, // 1, 2
488
+ 2, -1,
489
+ 3, -1,
490
+ 4, 14,
491
+ -1, 15,
492
+ 17, 18,
493
+ 27, -1,
494
+ 22, 23,
495
+ -1, 24,
496
+ 10, -1,
497
+ 9, 25,
498
+ 11, 8,
499
+ -1, 7, // 25, 26
500
+
501
+ 0, 1, //27, 28
502
+ 5, -1, //29, 30
503
+ 6, 12, //31, 32
504
+ 13, -1, //33, 34
505
+ 19, 16, //35, 36
506
+ 26, 20, //37, 38
507
+ -1, 21, //39, 40
508
+ // Padding:
509
+
510
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 56
511
+ -1, -1, -1, -1, -1, -1, -1, // ... 63
512
+ } ;
513
+
514
+ static int physToPinR3 [64] = //return wiringPI pin
515
+ {
516
+ -1, // 0
517
+ -1, -1, // 1, 2
518
+ 8, -1, //3, 4
519
+ 9, -1, //5, 6
520
+ 7, 15, //7, 8
521
+ -1, 16, //9,10
522
+ 0, 1, //11,12
523
+ 2, -1, //13,14
524
+ 3, 4, //15,16
525
+ -1, 5, //17,18
526
+ 12, -1, //19,20
527
+ 13, 6, //21,22
528
+ 14, 10, //23, 24
529
+ -1, 11, // 25, 26
530
+
531
+ 30, 31, //27, 28
532
+ 21, -1, //29, 30
533
+ 22, 26, //31, 32
534
+ 23, -1, //33, 34
535
+ 24, 27, //35, 36
536
+ 25, 28, //37, 38
537
+ -1, 29, //39, 40
538
+ // Padding:
539
+
540
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 56
541
+ -1, -1, -1, -1, -1, -1, -1, // ... 63
542
+ } ;
543
+
544
+ static int BP_PIN_MASK[9][32] = //[BANK] [INDEX]
545
+ {
546
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PA
547
+ { -1, -1, -1, 3, -1, 5, 6, 7, 8, -1, -1, -1, 12, 13, -1, -1, -1, -1, -1, -1, 20, 21, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PB
548
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PC
549
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PD
550
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PE
551
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PF
552
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PG
553
+ { -1, -1, 2, -1, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 20, 21, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PH
554
+ {0, 1, -1, 3, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, -1, 16, 17, 18, 19, 20, 21, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //PI
555
+ };
556
+ static int version = 0;
557
+ static int pwmmode = 0;
558
+
559
+ /*end 20140918*/
560
+
561
+ /*
562
+ * Functions
563
+ *********************************************************************************
564
+ */
565
+
566
+ /*add for BananaPro by LeMaker team*/
567
+ uint32_t sunxi_readl(uint32_t addr)
568
+ {
569
+ uint32_t val = 0;
570
+ uint32_t mmap_base = (addr & ~MAP_MASK);
571
+ uint32_t mmap_seek = ((addr - mmap_base) >> 2);
572
+ val = *(gpio + mmap_seek);
573
+ return val;
574
+
575
+ }
576
+ void sunxi_writel(uint32_t val, uint32_t addr)
577
+ {
578
+ uint32_t mmap_base = (addr & ~MAP_MASK);
579
+ uint32_t mmap_seek = ((addr - mmap_base) >> 2);
580
+ *(gpio + mmap_seek) = val;
581
+ }
582
+
583
+ static uint32_t s500_readl(volatile uint32_t *addr)
584
+ {
585
+ uint32_t val = 0;
586
+
587
+ val = *addr;
588
+ return val;
589
+
590
+ }
591
+ static void s500_writel(uint32_t val, volatile uint32_t *addr)
592
+ {
593
+ *addr = val;
594
+ }
595
+
596
+
597
+ //pwm for BananaPro only for pwm1
598
+ void sunxi_pwm_set_enable(int en)
599
+ {
600
+ int val = 0;
601
+ val = sunxi_readl(SUNXI_PWM_CTRL_REG);
602
+ if(en)
603
+ {
604
+ val |= (SUNXI_PWM_CH1_EN | SUNXI_PWM_SCLK_CH1_GATING);
605
+ }
606
+ else
607
+ {
608
+ val &= ~(SUNXI_PWM_CH1_EN | SUNXI_PWM_SCLK_CH1_GATING);
609
+ }
610
+ if (wiringPiDebug)
611
+ printf(">>function%s,no:%d,enable? :0x%x\n", __func__, __LINE__, val);
612
+ sunxi_writel(val, SUNXI_PWM_CTRL_REG);
613
+ delay (1) ;
614
+ }
615
+
616
+ void s500_pwm_set_enable(int en)
617
+ {
618
+ uint32_t val = 0;
619
+ volatile uint32_t *phyaddr = NULL;
620
+
621
+ //Disable output
622
+ phyaddr = gpio + (0x000C >> 2);
623
+ val = s500_readl(phyaddr);
624
+ val &= ~(0x100);
625
+ s500_writel(val, phyaddr);
626
+
627
+ //Disable input
628
+ phyaddr = gpio + (0x0010 >> 2);
629
+ val = s500_readl(phyaddr);
630
+ val &= ~(0x100);
631
+ s500_writel(val, phyaddr);
632
+
633
+ //MFP_CTL1
634
+ phyaddr = gpio + (0x0044 >> 2);
635
+ val = s500_readl(phyaddr);
636
+ val &= 0xFC7FFFFF;
637
+ val |= 0x01800000;
638
+ s500_writel(val, phyaddr);
639
+
640
+ }
641
+
642
+ void sunxi_pwm_set_mode(int mode)
643
+ {
644
+ int val = 0;
645
+ val = sunxi_readl(SUNXI_PWM_CTRL_REG);
646
+ mode &= 1; //cover the mode to 0 or 1
647
+ if(mode)
648
+ {
649
+ //pulse mode
650
+ val |= ( SUNXI_PWM_CH1_MS_MODE | SUNXI_PWM_CH1_PUL_START);
651
+ pwmmode = 1;
652
+ }
653
+ else
654
+ {
655
+ //cycle mode
656
+ val &= ~( SUNXI_PWM_CH1_MS_MODE);
657
+ pwmmode = 0;
658
+ }
659
+ val |= ( SUNXI_PWM_CH1_ACT_STA);
660
+ if (wiringPiDebug)
661
+ printf(">>function%s,no:%d,mode? :0x%x\n", __func__, __LINE__, val);
662
+ sunxi_writel(val, SUNXI_PWM_CTRL_REG);
663
+ delay (1) ;
664
+ }
665
+
666
+ void s500_pwm_set_mode(int mode)
667
+ {
668
+ //Nothing
669
+ }
670
+
671
+
672
+ void sunxi_pwm_set_clk(int clk)
673
+ {
674
+ int val = 0;
675
+
676
+ // sunxi_pwm_set_enable(0);
677
+ val = sunxi_readl(SUNXI_PWM_CTRL_REG);
678
+ //clear clk to 0
679
+ val &= 0xf801f0;
680
+ val |= ((clk & 0xf) << 15); //todo check wether clk is invalid or not
681
+ sunxi_writel(val, SUNXI_PWM_CTRL_REG);
682
+ sunxi_pwm_set_enable(1);
683
+ if (wiringPiDebug)
684
+ printf(">>function%s,no:%d,clk? :0x%x\n", __func__, __LINE__, val);
685
+ delay (1) ;
686
+ }
687
+
688
+ void s500_pwm_set_clk_source(int select)
689
+ {
690
+ uint32_t regval = 0;
691
+ volatile uint32_t *phyaddr = clk + (0x007C >> 2);
692
+
693
+ regval = s500_readl(phyaddr);
694
+ if(select == 0)//IC_32K
695
+ {
696
+ regval &= ~(1 << 12 );
697
+ }
698
+ else//HOSC 24M
699
+ {
700
+ regval |= (1 << 12);
701
+ }
702
+
703
+ s500_writel(regval, phyaddr);
704
+
705
+ if(wiringPiDebug)
706
+ {
707
+ printf(">>function%s,no:%d,clk sel :0x%x\n", __func__, __LINE__, regval);
708
+ }
709
+
710
+ delay (1);
711
+ }
712
+
713
+ /*
714
+ clk_div ��Χ: 0~1023
715
+ */
716
+ void s500_pwm_set_clk(int clk_div)
717
+ {
718
+ uint32_t regval = 0;
719
+ int temp;
720
+ volatile uint32_t *phyaddr = clk + (0x007C >> 2);
721
+
722
+ regval = s500_readl(phyaddr);
723
+ regval &= (1 << 12);
724
+
725
+ temp = clk_div;
726
+ temp |= regval;
727
+ s500_writel(temp, phyaddr);
728
+
729
+ if(wiringPiDebug)
730
+ {
731
+ printf(">>function%s,no:%d,clk sel :0x%x\n", __func__, __LINE__, temp);
732
+ }
733
+
734
+ delay (1);
735
+ }
736
+
737
+
738
+
739
+ /**
740
+ * ch0 and ch1 set the same,16 bit period and 16 bit act
741
+ */
742
+ uint32_t sunxi_pwm_get_period(void)
743
+ {
744
+ uint32_t period_cys = 0;
745
+ period_cys = sunxi_readl(SUNXI_PWM_CH1_PERIOD);//get ch1 period_cys
746
+ period_cys &= 0xffff0000;//get period_cys
747
+ period_cys = period_cys >> 16;
748
+ if (wiringPiDebug)
749
+ printf(">>func:%s,no:%d,period/range:%d", __func__, __LINE__, period_cys);
750
+ delay (1) ;
751
+ return period_cys;
752
+ }
753
+
754
+
755
+ uint32_t s500_pwm_get_period(void)
756
+ {
757
+ uint32_t period = 0;
758
+ volatile uint32_t *phyaddr = gpio + ((0x0050 + 4 * 3) >> 2);
759
+ period = s500_readl(phyaddr);
760
+ period &= 0x3FF;
761
+
762
+ if (wiringPiDebug)
763
+ {
764
+ printf(">>func:%s,no:%d,period/range:%d", __func__, __LINE__, period);
765
+ }
766
+
767
+ return period;
768
+ }
769
+
770
+
771
+ uint32_t sunxi_pwm_get_act(void)
772
+ {
773
+ uint32_t period_act = 0;
774
+ period_act = sunxi_readl(SUNXI_PWM_CH1_PERIOD);//get ch1 period_cys
775
+ period_act &= 0xffff;//get period_act
776
+ if (wiringPiDebug)
777
+ printf(">>func:%s,no:%d,period/range:%d", __func__, __LINE__, period_act);
778
+ delay (1) ;
779
+ return period_act;
780
+ }
781
+
782
+
783
+ uint32_t s500_pwm_get_act(void)
784
+ {
785
+ uint32_t act = 0;
786
+ volatile uint32_t *phyaddr = gpio + ((0x0050 + 4 * 3) >> 2);
787
+ act = s500_readl(phyaddr);
788
+ act &= 0xFFC00;
789
+ act >>= 10;
790
+
791
+ if (wiringPiDebug)
792
+ {
793
+ printf(">>func:%s,no:%d,act:%d", __func__, __LINE__, act);
794
+ }
795
+
796
+ return act;
797
+ }
798
+
799
+
800
+
801
+
802
+ void sunxi_pwm_set_period(int period_cys)
803
+ {
804
+ uint32_t val = 0;
805
+ //all clear to 0
806
+ if (wiringPiDebug)
807
+ printf(">>func:%s no:%d\n", __func__, __LINE__);
808
+ period_cys &= 0xffff; //set max period to 2^16
809
+ period_cys = period_cys << 16;
810
+ val = sunxi_readl(SUNXI_PWM_CH1_PERIOD);
811
+ val &= 0x0000ffff;
812
+ period_cys |= val;
813
+ sunxi_writel(period_cys, SUNXI_PWM_CH1_PERIOD);
814
+ delay (1) ;
815
+
816
+ }
817
+
818
+
819
+ void s500_pwm_set_period(int period)
820
+ {
821
+ uint32_t val = 0;
822
+ volatile uint32_t *phyaddr = gpio + ((0x0050 + 4 * 3) >> 2);
823
+
824
+ period &= 0x3FF; //set max period to 2^10
825
+ val = s500_readl(phyaddr);
826
+ val &= 0x1FFC00;
827
+ period |= val;
828
+ s500_writel(period, phyaddr);
829
+
830
+ if (wiringPiDebug)
831
+ {
832
+ printf(">>func:%s,no:%d,period/range:%d\n", __func__, __LINE__, period);
833
+ }
834
+
835
+ delay (1);
836
+ }
837
+
838
+
839
+
840
+ void sunxi_pwm_set_act(int act_cys)
841
+ {
842
+ uint32_t per0 = 0;
843
+ //keep period the same, clear act_cys to 0 first
844
+ if (wiringPiDebug)
845
+ printf(">>func:%s no:%d\n", __func__, __LINE__);
846
+ per0 = sunxi_readl(SUNXI_PWM_CH1_PERIOD);
847
+ per0 &= 0xffff0000;
848
+ act_cys &= 0xffff;
849
+ act_cys |= per0;
850
+ sunxi_writel(act_cys, SUNXI_PWM_CH1_PERIOD);
851
+ delay (1) ;
852
+ }
853
+
854
+
855
+ void s500_pwm_set_act(int act)
856
+ {
857
+ uint32_t val = 0;
858
+ volatile uint32_t *phyaddr = gpio + ((0x0050 + 4 * 3) >> 2);
859
+
860
+ act &= 0x3FF; //set max period to 2^10
861
+ val = s500_readl(phyaddr);
862
+ val &= 0x1003FF;
863
+ act <<= 10;
864
+ act |= val;
865
+ s500_writel(act, phyaddr);
866
+
867
+ if (wiringPiDebug)
868
+ {
869
+ printf(">>func:%s,no:%d,act:%d\n", __func__, __LINE__, act);
870
+ }
871
+
872
+ delay (1);
873
+ }
874
+
875
+
876
+ uint32_t s500_pwm_get_polarity(void)
877
+ {
878
+ uint32_t val = 0;
879
+ volatile uint32_t *phyaddr = gpio + ((0x0050 + 4 * 3) >> 2);
880
+
881
+ val = s500_readl(phyaddr);
882
+ val &= 0x100000;
883
+ val >>= 20;
884
+
885
+ if (wiringPiDebug)
886
+ {
887
+ printf(">>func:%s,no:%d,act:%d\n", __func__, __LINE__, val);
888
+ }
889
+
890
+ delay (1);
891
+
892
+ return val;
893
+ }
894
+
895
+
896
+
897
+ /*
898
+ ** Polarity select
899
+ ** 0:PWM low voltage level active
900
+ ** 1:PWM high voltage level active
901
+ */
902
+ void s500_pwm_set_polarity(int act)
903
+ {
904
+ uint32_t val = 0;
905
+ volatile uint32_t *phyaddr = gpio + ((0x0050 + 4 * 3) >> 2);
906
+
907
+ act &= 0x01; //range :0~1
908
+ val = s500_readl(phyaddr);
909
+ val &= 0x0FFFFF;
910
+ act <<= 20;
911
+ act |= val;
912
+ s500_writel(act, phyaddr);
913
+
914
+ if (wiringPiDebug)
915
+ {
916
+ printf(">>func:%s,no:%d,act:%d\n", __func__, __LINE__, act);
917
+ }
918
+
919
+ delay (1);
920
+ }
921
+
922
+
923
+
924
+
925
+ int sunxi_get_gpio_mode(int pin)
926
+ {
927
+ uint32_t regval = 0;
928
+ int bank = pin >> 5;
929
+ int index = pin - (bank << 5);
930
+ int offset = ((index - ((index >> 3) << 3)) << 2);
931
+ uint32_t reval = 0;
932
+ uint32_t phyaddr = SUNXI_GPIO_BASE + (bank * 36) + ((index >> 3) << 2);
933
+ if (wiringPiDebug)
934
+ printf("func:%s pin:%d, bank:%d index:%d phyaddr:0x%x\n", __func__, pin , bank, index, phyaddr);
935
+ if(BP_PIN_MASK[bank][index] != -1)
936
+ {
937
+ regval = sunxi_readl(phyaddr);
938
+ if (wiringPiDebug)
939
+ printf("read reg val: 0x%x offset:%d return: %d\n", regval, offset, reval);
940
+ //reval=regval &(reval+(7 << offset));
941
+ reval = (regval >> offset) & 7;
942
+ if (wiringPiDebug)
943
+ printf("read reg val: 0x%x offset:%d return: %d\n", regval, offset, reval);
944
+ return reval;
945
+ }
946
+ else
947
+ {
948
+ printf("line:%dpin number error\n", __LINE__);
949
+ return reval;
950
+ }
951
+ }
952
+
953
+
954
+ void sunxi_set_gpio_mode(int pin, int mode)
955
+ {
956
+ uint32_t regval = 0;
957
+ int bank = pin >> 5;
958
+ int index = pin - (bank << 5);
959
+ int offset = ((index - ((index >> 3) << 3)) << 2);
960
+ uint32_t phyaddr = SUNXI_GPIO_BASE + (bank * 36) + ((index >> 3) << 2);
961
+ if (wiringPiDebug)
962
+ printf("func:%s pin:%d, MODE:%d bank:%d index:%d phyaddr:0x%x\n", __func__, pin , mode, bank, index, phyaddr);
963
+ if(BP_PIN_MASK[bank][index] != -1)
964
+ {
965
+ regval = sunxi_readl(phyaddr);
966
+ if (wiringPiDebug)
967
+ printf("read reg val: 0x%x offset:%d\n", regval, offset);
968
+ if(INPUT == mode)
969
+ {
970
+ regval &= ~(7 << offset);
971
+ sunxi_writel(regval, phyaddr);
972
+ regval = sunxi_readl(phyaddr);
973
+ if (wiringPiDebug)
974
+ printf("Input mode set over reg val: 0x%x\n", regval);
975
+ }
976
+ else if(OUTPUT == mode)
977
+ {
978
+ regval &= ~(7 << offset);
979
+ regval |= (1 << offset);
980
+ if (wiringPiDebug)
981
+ printf("Out mode ready set val: 0x%x\n", regval);
982
+ sunxi_writel(regval, phyaddr);
983
+ regval = sunxi_readl(phyaddr);
984
+ if (wiringPiDebug)
985
+ printf("Out mode set over reg val: 0x%x\n", regval);
986
+ }
987
+ else if(PWM_OUTPUT == mode)
988
+ {
989
+ // set pin PWMx to pwm mode
990
+ regval &= ~(7 << offset);
991
+ regval |= (0x2 << offset);
992
+ if (wiringPiDebug)
993
+ printf(">>>>>line:%d PWM mode ready to set val: 0x%x\n", __LINE__, regval);
994
+ sunxi_writel(regval, phyaddr);
995
+ delayMicroseconds (200);
996
+ regval = sunxi_readl(phyaddr);
997
+ if (wiringPiDebug)
998
+ printf("<<<<<PWM mode set over reg val: 0x%x\n", regval);
999
+ //clear all reg
1000
+ sunxi_writel(0, SUNXI_PWM_CTRL_REG);
1001
+ sunxi_writel(0, SUNXI_PWM_CH0_PERIOD);
1002
+ sunxi_writel(0, SUNXI_PWM_CH1_PERIOD);
1003
+
1004
+ //set default M:S to 1/2
1005
+ sunxi_pwm_set_period(1024);
1006
+ sunxi_pwm_set_act(512);
1007
+ pwmSetMode(PWM_MODE_MS);
1008
+ sunxi_pwm_set_clk(PWM_CLK_DIV_120);//default clk:24M/120
1009
+ delayMicroseconds (200);
1010
+ }
1011
+ }
1012
+ else
1013
+ {
1014
+ printf("line:%dpin number error\n", __LINE__);
1015
+ }
1016
+
1017
+ return ;
1018
+ }
1019
+ void sunxi_digitalWrite(int pin, int value)
1020
+ {
1021
+ uint32_t regval = 0;
1022
+ int bank = pin >> 5;
1023
+ int index = pin - (bank << 5);
1024
+ uint32_t phyaddr = SUNXI_GPIO_BASE + (bank * 36) + 0x10; // +0x10 -> data reg
1025
+ if (wiringPiDebug)
1026
+ printf("func:%s pin:%d, value:%d bank:%d index:%d phyaddr:0x%x\n", __func__, pin , value, bank, index, phyaddr);
1027
+ if(BP_PIN_MASK[bank][index] != -1)
1028
+ {
1029
+ regval = sunxi_readl(phyaddr);
1030
+ if (wiringPiDebug)
1031
+ printf("befor write reg val: 0x%x,index:%d\n", regval, index);
1032
+ if(0 == value)
1033
+ {
1034
+ regval &= ~(1 << index);
1035
+ sunxi_writel(regval, phyaddr);
1036
+ regval = sunxi_readl(phyaddr);
1037
+ if (wiringPiDebug)
1038
+ printf("LOW val set over reg val: 0x%x\n", regval);
1039
+ }
1040
+ else
1041
+ {
1042
+ regval |= (1 << index);
1043
+ sunxi_writel(regval, phyaddr);
1044
+ regval = sunxi_readl(phyaddr);
1045
+ if (wiringPiDebug)
1046
+ printf("HIGH val set over reg val: 0x%x\n", regval);
1047
+ }
1048
+ }
1049
+ else
1050
+ {
1051
+ printf("pin number error\n");
1052
+ }
1053
+
1054
+ return ;
1055
+ }
1056
+ int sunxi_digitalRead(int pin)
1057
+ {
1058
+ uint32_t regval = 0;
1059
+ int bank = pin >> 5;
1060
+ int index = pin - (bank << 5);
1061
+ uint32_t phyaddr = SUNXI_GPIO_BASE + (bank * 36) + 0x10; // +0x10 -> data reg
1062
+ if (wiringPiDebug)
1063
+ printf("func:%s pin:%d,bank:%d index:%d phyaddr:0x%x\n", __func__, pin, bank, index, phyaddr);
1064
+ if(BP_PIN_MASK[bank][index] != -1)
1065
+ {
1066
+ regval = sunxi_readl(phyaddr);
1067
+ regval = regval >> index;
1068
+ regval &= 1;
1069
+ if (wiringPiDebug)
1070
+ printf("***** read reg val: 0x%x,bank:%d,index:%d,line:%d\n", regval, bank, index, __LINE__);
1071
+ return regval;
1072
+ }
1073
+ else
1074
+ {
1075
+ printf("pin number error\n");
1076
+ return regval;
1077
+ }
1078
+ }
1079
+ void sunxi_pullUpDnControl (int pin, int pud)
1080
+ {
1081
+ uint32_t regval = 0;
1082
+ int bank = pin >> 5;
1083
+ int index = pin - (bank << 5);
1084
+ int sub = index >> 4;
1085
+ int sub_index = index - 16 * sub;
1086
+ uint32_t phyaddr = SUNXI_GPIO_BASE + (bank * 36) + 0x1c + sub * 4; // +0x10 -> pullUpDn reg
1087
+ if (wiringPiDebug)
1088
+ printf("func:%s pin:%d,bank:%d index:%d sub:%d phyaddr:0x%x\n", __func__, pin, bank, index, sub, phyaddr);
1089
+ if(BP_PIN_MASK[bank][index] != -1)
1090
+ {
1091
+ //PI13~PI21 need check again
1092
+ regval = sunxi_readl(phyaddr);
1093
+ if (wiringPiDebug)
1094
+ printf("pullUpDn reg:0x%x, pud:0x%x sub_index:%d\n", regval, pud, sub_index);
1095
+ regval &= ~(3 << (sub_index << 1));
1096
+ regval |= (pud << (sub_index << 1));
1097
+ if (wiringPiDebug)
1098
+ printf("pullUpDn val ready to set:0x%x\n", regval);
1099
+ sunxi_writel(regval, phyaddr);
1100
+ regval = sunxi_readl(phyaddr);
1101
+ if (wiringPiDebug)
1102
+ printf("pullUpDn reg after set:0x%x addr:0x%x\n", regval, phyaddr);
1103
+ }
1104
+ else
1105
+ {
1106
+ printf("pin number error\n");
1107
+ }
1108
+ delay (1) ;
1109
+ return ;
1110
+ }
1111
+ /*end 2014.09.18*/
1112
+
1113
+ /*
1114
+ * wiringPiFailure:
1115
+ * Fail. Or not.
1116
+ *********************************************************************************
1117
+ */
1118
+
1119
+ int wiringPiFailure (int fatal, const char *message, ...)
1120
+ {
1121
+ va_list argp ;
1122
+ char buffer [1024] ;
1123
+
1124
+ if (!fatal && wiringPiReturnCodes)
1125
+ return -1 ;
1126
+
1127
+ va_start (argp, message) ;
1128
+ vsnprintf (buffer, 1023, message, argp) ;
1129
+ va_end (argp) ;
1130
+
1131
+ fprintf (stderr, "%s", buffer) ;
1132
+ exit (EXIT_FAILURE) ;
1133
+
1134
+ return 0 ;
1135
+ }
1136
+
1137
+
1138
+ /*
1139
+ * piBoardRev:
1140
+ * Return a number representing the hardware revision of the board.
1141
+ *********************************************************************************
1142
+ * 3 --- Banana Pro
1143
+ * 4 --- S500
1144
+ *
1145
+ *********************************************************************************
1146
+ */
1147
+
1148
+ static void piBoardRevOops (const char *why)
1149
+ {
1150
+ fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
1151
+ fprintf (stderr, " -> %s\n", why) ;
1152
+ fprintf (stderr, " -> You may want to check:\n") ;
1153
+ fprintf (stderr, " -> http://www.lemaker.org/\n") ; /*modify for BananaPro by LeMmaker team*/
1154
+ exit (EXIT_FAILURE) ;
1155
+ }
1156
+
1157
+ /*add for BananaPro by LeMaker team*/
1158
+ int isA20(void)
1159
+ {
1160
+ FILE *cpuFd ;
1161
+ char line [120] ;
1162
+ char *d;
1163
+ if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
1164
+ piBoardRevOops ("Unable to open /proc/cpuinfo") ;
1165
+ while (fgets (line, 120, cpuFd) != NULL)
1166
+ {
1167
+ if (strncmp (line, "Hardware", 8) == 0)
1168
+ break ;
1169
+ }
1170
+
1171
+ fclose (cpuFd) ;
1172
+ if (strncmp (line, "Hardware", 8) != 0)
1173
+ piBoardRevOops ("No \"Hardware\" line") ;
1174
+
1175
+ for (d = &line [strlen (line) - 1] ; (*d == '\n') || (*d == '\r') ; --d)
1176
+ *d = 0 ;
1177
+ if (wiringPiDebug)
1178
+ printf ("piboardRev: Hardware string: %s\n", line) ;
1179
+
1180
+ if (strstr(line, "sun7i") != NULL)
1181
+ {
1182
+ if (wiringPiDebug)
1183
+ printf ("Hardware:%s\n", line) ;
1184
+ return 1 ;
1185
+ }
1186
+ else
1187
+ {
1188
+ if (wiringPiDebug)
1189
+ printf ("Hardware:%s\n", line) ;
1190
+ return 0 ;
1191
+ }
1192
+ }
1193
+ /*end 2014.09.18*/
1194
+
1195
+ /*add for S500*/
1196
+ int isS500(void)
1197
+ {
1198
+ FILE *cpuFd ;
1199
+ char line [120] ;
1200
+ char *d;
1201
+ if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
1202
+ {
1203
+ piBoardRevOops ("Unable to open /proc/cpuinfo") ;
1204
+ }
1205
+
1206
+ while (fgets (line, 120, cpuFd) != NULL)
1207
+ {
1208
+ if (strncmp (line, "Hardware", 8) == 0)
1209
+ break ;
1210
+ }
1211
+
1212
+ fclose (cpuFd) ;
1213
+
1214
+ if (strncmp (line, "Hardware", 8) != 0)
1215
+ {
1216
+ piBoardRevOops ("No \"Hardware\" line") ;
1217
+ }
1218
+
1219
+ for (d = &line [strlen (line) - 1] ; (*d == '\n') || (*d == '\r') ; --d)
1220
+ {
1221
+ *d = 0 ;
1222
+ }
1223
+
1224
+ if (wiringPiDebug)
1225
+ {
1226
+ printf ("piboardRev: Hardware string: %s\n", line) ;
1227
+ }
1228
+
1229
+ if (strstr(line, "gs705a") != NULL)
1230
+ {
1231
+ if (wiringPiDebug)
1232
+ printf ("Hardware:%s\n", line) ;
1233
+ return 1 ;
1234
+ }
1235
+ else
1236
+ {
1237
+ if (wiringPiDebug)
1238
+ printf ("Hardware:%s\n", line) ;
1239
+ return 0 ;
1240
+ }
1241
+ }
1242
+
1243
+
1244
+ int piBoardRev (void)
1245
+ {
1246
+ //add for S500
1247
+ if(isS500())
1248
+ {
1249
+ version = S500_REV;
1250
+
1251
+ if (wiringPiDebug)
1252
+ {
1253
+ printf ("piboardRev: %d\n", version) ;
1254
+ }
1255
+
1256
+ return S500_REV ;
1257
+ }
1258
+ /*add for BananaPro by LeMaker team*/
1259
+ else if(isA20())
1260
+ {
1261
+ version = BP_REV;
1262
+ if (wiringPiDebug)
1263
+ printf ("piboardRev: %d\n", version) ;
1264
+ return BP_REV ;
1265
+ }
1266
+ /*end 2014.09.18*/
1267
+ else
1268
+ {
1269
+ printf("piboardRev is error!!!\n");
1270
+ }
1271
+ return -1;
1272
+
1273
+ }
1274
+
1275
+
1276
+ /*
1277
+ * piBoardId:
1278
+ * Do more digging into the board revision string as above, but return
1279
+ * as much details as we can.
1280
+ * This is undocumented and really only intended for the GPIO command.
1281
+ * Use at your own risk!
1282
+ *********************************************************************************
1283
+ */
1284
+
1285
+ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
1286
+ {
1287
+ FILE *cpuFd ;
1288
+ char line [120] ;
1289
+ char *c ;
1290
+
1291
+ (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
1292
+
1293
+ //add for s500
1294
+ if(version == S500_REV)
1295
+ {
1296
+ *model = PI_MODEL_BPR;
1297
+ *rev = PI_VERSION_1_2;
1298
+ *mem = 1024;
1299
+ *maker = PI_MAKER_LEMAKER;
1300
+
1301
+ return;
1302
+ }
1303
+
1304
+
1305
+ if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
1306
+ piBoardRevOops ("Unable to open /proc/cpuinfo") ;
1307
+
1308
+ while (fgets (line, 120, cpuFd) != NULL)
1309
+ if (strncmp (line, "Revision", 8) == 0)
1310
+ break ;
1311
+
1312
+ fclose (cpuFd) ;
1313
+
1314
+ if (strncmp (line, "Revision", 8) != 0)
1315
+ piBoardRevOops ("No \"Revision\" line") ;
1316
+
1317
+ // Chomp trailing CR/NL
1318
+
1319
+ for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
1320
+ *c = 0 ;
1321
+
1322
+ if (wiringPiDebug)
1323
+ printf ("piboardId: Revision string: %s\n", line) ;
1324
+
1325
+ // Scan to first digit
1326
+
1327
+ for (c = line ; *c ; ++c)
1328
+ if (isdigit (*c))
1329
+ break ;
1330
+
1331
+ // Make sure its long enough
1332
+
1333
+ if (strlen (c) < 4)
1334
+ piBoardRevOops ("Bogus \"Revision\" line") ;
1335
+
1336
+ // If longer than 4, we'll assume it's been overvolted
1337
+
1338
+ *overVolted = strlen (c) > 4 ;
1339
+
1340
+ // Extract last 4 characters:
1341
+
1342
+ c = c + strlen (c) - 4 ;
1343
+
1344
+ // Fill out the replys as appropriate
1345
+
1346
+ //add for BananaPro by LeMaker team
1347
+ if (strcmp (c, "0000") == 0)
1348
+ {
1349
+ *model = PI_MODEL_BPR;
1350
+ *rev = PI_VERSION_1_2;
1351
+ *mem = 1024;
1352
+ *maker = PI_MAKER_LEMAKER;
1353
+ }
1354
+ //end 2014.09.30
1355
+ else
1356
+ {
1357
+ *model = 0 ;
1358
+ *rev = 0 ;
1359
+ *mem = 0 ;
1360
+ *maker = 0 ;
1361
+ }
1362
+ }
1363
+
1364
+
1365
+
1366
+ /*
1367
+ * wpiPinToGpio:
1368
+ * Translate a wiringPi Pin number to native GPIO pin number.
1369
+ * Provided for external support.
1370
+ *********************************************************************************
1371
+ */
1372
+
1373
+ int wpiPinToGpio (int wpiPin)
1374
+ {
1375
+ return pinToGpio [wpiPin & 63] ;
1376
+ }
1377
+
1378
+
1379
+ /*
1380
+ * physPinToGpio:
1381
+ * Translate a physical Pin number to native GPIO pin number.
1382
+ * Provided for external support.
1383
+ *********************************************************************************
1384
+ */
1385
+
1386
+ int physPinToGpio (int physPin)
1387
+ {
1388
+ return physToGpio [physPin & 63] ;
1389
+ }
1390
+
1391
+ /*
1392
+ * physPinToGpio:
1393
+ * Translate a physical Pin number to wiringPi pin number. add by lemaker team for BananaPi
1394
+ * Provided for external support.
1395
+ *********************************************************************************
1396
+ */
1397
+ int physPinToPin(int physPin)
1398
+ {
1399
+ return physToPin [physPin & 63] ;
1400
+ }
1401
+
1402
+ /*
1403
+ * setPadDrive:
1404
+ * Set the PAD driver value
1405
+ *********************************************************************************
1406
+ */
1407
+
1408
+ void setPadDrive (int group, int value)
1409
+ {
1410
+
1411
+ //add for s500
1412
+ if(version == S500_REV)
1413
+ {
1414
+ return;
1415
+ }
1416
+ /*add for BananaPro by LeMaker team*/
1417
+ else if(BP_REV == version)
1418
+ {
1419
+ return;
1420
+ }
1421
+ /*end 2014.08.19*/
1422
+ else
1423
+ {
1424
+
1425
+ }
1426
+
1427
+ }
1428
+
1429
+
1430
+ /*
1431
+ * getAlt:
1432
+ * Returns the ALT bits for a given port. Only really of-use
1433
+ * for the gpio readall command (I think)
1434
+ *********************************************************************************
1435
+ */
1436
+
1437
+ int s500_get_gpio_mode(int pin);
1438
+
1439
+
1440
+ int getAlt (int pin)
1441
+ {
1442
+ int alt ;
1443
+
1444
+ pin &= 63 ;
1445
+
1446
+ if(version == S500_REV)
1447
+ {
1448
+ alt = s500_get_gpio_mode(pin);
1449
+
1450
+ return alt ;
1451
+ }
1452
+ /*add for BananaPro by LeMaker team*/
1453
+ else if(BP_REV == version)
1454
+ {
1455
+ if (wiringPiMode == WPI_MODE_PINS)
1456
+ pin = pinToGpio_BP [pin] ;
1457
+ else if (wiringPiMode == WPI_MODE_PHYS)
1458
+ pin = physToGpio_BP[pin] ;
1459
+ else if (wiringPiMode == WPI_MODE_GPIO)
1460
+ pin = pinTobcm_BP[pin]; //need map A20 to bcm
1461
+ else return 0 ;
1462
+
1463
+ if(-1 == pin)
1464
+ {
1465
+ printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
1466
+ return -1;
1467
+ }
1468
+ alt = sunxi_get_gpio_mode(pin);
1469
+ return alt ;
1470
+ }
1471
+ /*end 2014.08.19*/
1472
+ else
1473
+ {
1474
+ printf("Hardware revision is error!!!");
1475
+ }
1476
+
1477
+ return -1;
1478
+
1479
+ }
1480
+
1481
+
1482
+ /*
1483
+ * pwmSetMode:
1484
+ * Select the native "balanced" mode, or standard mark:space mode
1485
+ *********************************************************************************
1486
+ */
1487
+
1488
+ void pwmSetMode (int mode)
1489
+ {
1490
+ /* add for S500 */
1491
+ if(version == S500_REV)
1492
+ {
1493
+ return;
1494
+ }
1495
+ /*add for BananaPro by LeMaker team*/
1496
+ else if (BP_REV == version)
1497
+ {
1498
+ sunxi_pwm_set_mode(mode);
1499
+ return;
1500
+ }
1501
+ /*end 2014.08.19*/
1502
+ else
1503
+ {
1504
+ printf("Hardware revision is error!!!");
1505
+ }
1506
+
1507
+ }
1508
+
1509
+
1510
+ /*
1511
+ * pwmSetRange:
1512
+ * Set the PWM range register. We set both range registers to the same
1513
+ * value. If you want different in your own code, then write your own.
1514
+ *********************************************************************************
1515
+ */
1516
+
1517
+ void pwmSetRange (unsigned int range)
1518
+ {
1519
+ /* add for S500 */
1520
+ if(version == S500_REV)
1521
+ {
1522
+ s500_pwm_set_period(range);
1523
+ delayMicroseconds (10) ;
1524
+ return;
1525
+ }
1526
+ /*add for BananaPro by LeMaker team*/
1527
+ else if (BP_REV == version)
1528
+ {
1529
+ sunxi_pwm_set_period(range);
1530
+ return;
1531
+ }
1532
+ /*end 2014.08.19*/
1533
+ else
1534
+ {
1535
+ printf("Hardware revision is error!!!");
1536
+ }
1537
+ }
1538
+
1539
+
1540
+ /*
1541
+ * pwmSetClock:
1542
+ * Set/Change the PWM clock. Originally my code, but changed
1543
+ * (for the better!) by Chris Hall, <chris@kchall.plus.com>
1544
+ * after further study of the manual and testing with a 'scope
1545
+ *********************************************************************************
1546
+ */
1547
+
1548
+ void pwmSetClock (int divisor)
1549
+ {
1550
+ /* add for S500 */
1551
+ if(version == S500_REV)
1552
+ {
1553
+ s500_pwm_set_clk(divisor);
1554
+ delayMicroseconds (10) ;
1555
+ return;
1556
+ }
1557
+ /*add for BananaPro by LeMaker team*/
1558
+ else if (BP_REV == version)
1559
+ {
1560
+ sunxi_pwm_set_clk(divisor);
1561
+ sunxi_pwm_set_enable(1);
1562
+ return;
1563
+ }
1564
+ /*end 2014.08.19*/
1565
+ else
1566
+ {
1567
+ printf("Hardware revision is error!!!");
1568
+ }
1569
+
1570
+ }
1571
+
1572
+
1573
+ /*
1574
+ * gpioClockSet:
1575
+ * Set the freuency on a GPIO clock pin
1576
+ *********************************************************************************
1577
+ */
1578
+
1579
+ void gpioClockSet (int pin, int freq)
1580
+ {
1581
+
1582
+ /* add for S500 */
1583
+ if(version == S500_REV)
1584
+ {
1585
+ return;
1586
+ }
1587
+ /*add for BananaPro by LeMaker team*/
1588
+ else if (BP_REV == version)
1589
+ {
1590
+ return;
1591
+ }
1592
+ /*end 2014.08.19*/
1593
+ else
1594
+ {
1595
+ printf("Hardware revision is error!!!");
1596
+ }
1597
+
1598
+ }
1599
+
1600
+
1601
+ /*
1602
+ * wiringPiFindNode:
1603
+ * Locate our device node
1604
+ *********************************************************************************
1605
+ */
1606
+
1607
+ struct wiringPiNodeStruct *wiringPiFindNode (int pin)
1608
+ {
1609
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
1610
+
1611
+ while (node != NULL)
1612
+ if ((pin >= node->pinBase) && (pin <= node->pinMax))
1613
+ return node ;
1614
+ else
1615
+ node = node->next ;
1616
+
1617
+ return NULL ;
1618
+ }
1619
+
1620
+
1621
+ /*
1622
+ * wiringPiNewNode:
1623
+ * Create a new GPIO node into the wiringPi handling system
1624
+ *********************************************************************************
1625
+ */
1626
+
1627
+ static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode)
1628
+ {
1629
+ return ;
1630
+ }
1631
+ static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud)
1632
+ {
1633
+ return ;
1634
+ }
1635
+ static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin)
1636
+ {
1637
+ return LOW ;
1638
+ }
1639
+ static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value)
1640
+ {
1641
+ return ;
1642
+ }
1643
+ static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value)
1644
+ {
1645
+ return ;
1646
+ }
1647
+ static int analogReadDummy (struct wiringPiNodeStruct *node, int pin)
1648
+ {
1649
+ return 0 ;
1650
+ }
1651
+ static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value)
1652
+ {
1653
+ return ;
1654
+ }
1655
+
1656
+ struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
1657
+ {
1658
+ int pin ;
1659
+ struct wiringPiNodeStruct *node ;
1660
+
1661
+ // Minimum pin base is 64
1662
+
1663
+ if (pinBase < 64)
1664
+ (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
1665
+
1666
+ // Check all pins in-case there is overlap:
1667
+
1668
+ for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
1669
+ if (wiringPiFindNode (pin) != NULL)
1670
+ (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
1671
+
1672
+ node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
1673
+ if (node == NULL)
1674
+ (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
1675
+
1676
+ node->pinBase = pinBase ;
1677
+ node->pinMax = pinBase + numPins - 1 ;
1678
+ node->pinMode = pinModeDummy ;
1679
+ node->pullUpDnControl = pullUpDnControlDummy ;
1680
+ node->digitalRead = digitalReadDummy ;
1681
+ node->digitalWrite = digitalWriteDummy ;
1682
+ node->pwmWrite = pwmWriteDummy ;
1683
+ node->analogRead = analogReadDummy ;
1684
+ node->analogWrite = analogWriteDummy ;
1685
+ node->next = wiringPiNodes ;
1686
+ wiringPiNodes = node ;
1687
+
1688
+ return node ;
1689
+ }
1690
+
1691
+
1692
+ #ifdef notYetReady
1693
+ /*
1694
+ * pinED01:
1695
+ * pinED10:
1696
+ * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
1697
+ * Pin must already be in input mode with appropriate pull up/downs set.
1698
+ *********************************************************************************
1699
+ */
1700
+
1701
+ void pinEnableED01Pi (int pin)
1702
+ {
1703
+ pin = pinToGpio [pin & 63] ;
1704
+ }
1705
+ #endif
1706
+
1707
+
1708
+ /*
1709
+ *********************************************************************************
1710
+ * Core Functions
1711
+ *********************************************************************************
1712
+ */
1713
+
1714
+ /*
1715
+ * pinModeAlt:
1716
+ * This is an un-documented special to let you set any pin to any mode
1717
+ *********************************************************************************
1718
+ */
1719
+
1720
+ void pinModeAlt (int pin, int mode)
1721
+ {
1722
+
1723
+ /*add for BananaPro by LeMaker team*/
1724
+ if (BP_REV == version || S500_REV == version)
1725
+ {
1726
+ return;
1727
+ }
1728
+ /*end 2014.08.19*/
1729
+
1730
+ }
1731
+
1732
+
1733
+ int s500_get_gpio_mode(int pin)
1734
+ {
1735
+ uint32_t regval = 0;
1736
+ int bank = pin >> 5;
1737
+ volatile uint32_t *phyaddr = NULL;
1738
+
1739
+ if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1740
+ {
1741
+ if (wiringPiMode == WPI_MODE_PHYS)
1742
+ {
1743
+ pin = s500_physToGpio[pin] ;
1744
+ }
1745
+ else if(wiringPiMode == WPI_MODE_PINS)
1746
+ {
1747
+ pin = s500_pinToGpio[pin];
1748
+ }
1749
+ else if(wiringPiMode == WPI_MODE_GPIO)
1750
+ {
1751
+ pin = s500_pinTobcm[pin];
1752
+ }
1753
+ else
1754
+ {
1755
+ return 0;
1756
+ }
1757
+
1758
+ if(pin != -1)
1759
+ {
1760
+ //Input
1761
+ phyaddr = gpio + (bank * 3) + 0x01; // +0x04(Byte) -> Input Enable Register
1762
+ regval = s500_readl(phyaddr);
1763
+ if(regval == 1)
1764
+ {
1765
+ if(wiringPiDebug)
1766
+ {
1767
+ printf("func:%s pin:%d, input reval:0x%x\n", __func__, pin , regval);
1768
+ }
1769
+
1770
+ return 0;
1771
+ }
1772
+
1773
+ //Output
1774
+ phyaddr = gpio + (bank * 3) + 0x00; // +0x00 -> Output Enable Register
1775
+ regval = s500_readl(phyaddr);
1776
+ if(regval == 1)
1777
+ {
1778
+ if(wiringPiDebug)
1779
+ {
1780
+ printf("func:%s pin:%d, output reval:0x%x\n", __func__, pin , regval);
1781
+ }
1782
+
1783
+ return 1;
1784
+ }
1785
+
1786
+ //other function
1787
+ return 4;
1788
+ }
1789
+
1790
+ }
1791
+ else
1792
+ {
1793
+ printf("line:%dpin number error\n", __LINE__);
1794
+
1795
+ }
1796
+
1797
+ return -1;
1798
+
1799
+ }
1800
+
1801
+
1802
+
1803
+
1804
+ static void s500_set_gpio_mode(int pin, int mode)
1805
+ {
1806
+ uint32_t regval = 0;
1807
+
1808
+
1809
+ if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1810
+ {
1811
+ if (wiringPiMode == WPI_MODE_PHYS)
1812
+ {
1813
+ pin = s500_physToGpio[pin] ;
1814
+ }
1815
+ else if(wiringPiMode == WPI_MODE_PINS)
1816
+ {
1817
+ pin = s500_pinToGpio[pin];
1818
+ }
1819
+ else if(wiringPiMode == WPI_MODE_GPIO)
1820
+ {
1821
+ pin = s500_pinTobcm[pin];
1822
+ }
1823
+ else
1824
+ {
1825
+ return;
1826
+ }
1827
+
1828
+
1829
+ if(pin != -1)
1830
+ {
1831
+ int bank = pin >> 5;
1832
+ int index = pin - (bank << 5);
1833
+ volatile uint32_t *phyaddr = NULL;
1834
+
1835
+ //LVDS�ź���Ҫ��תΪ�����źŲ���ʹ��
1836
+
1837
+ if(pin == 42 || pin == 45 || pin == 46 || pin == 47 || pin == 48 || pin == 50 || pin == 51)
1838
+ {
1839
+ //lvds port must be set digital function.The default function is LVDS ODD PAD.
1840
+ phyaddr = gpio + (0x0044 >> 2);
1841
+ regval = s500_readl(phyaddr);
1842
+ regval |= (1 << 22);
1843
+ regval &= ~(1 << 21);
1844
+ s500_writel(regval, phyaddr);
1845
+ }
1846
+ else if(pin == 64 || pin == 65)
1847
+ {
1848
+ phyaddr = gpio + (0x0044 >> 2);
1849
+ regval = s500_readl(phyaddr);
1850
+ if(pin == 64)
1851
+ {
1852
+ regval |= (1 << 13);
1853
+ regval |= (1 << 12);
1854
+ }
1855
+ else
1856
+ {
1857
+ regval |= (1 << 11);
1858
+ regval |= (1 << 10);
1859
+ }
1860
+ s500_writel(regval, phyaddr);
1861
+ }
1862
+ else if(pin == 68 || pin == 69)
1863
+ {
1864
+ phyaddr = gpio + (0x0048 >> 2);
1865
+ regval = s500_readl(phyaddr);
1866
+ regval |= (1 << 30);
1867
+ regval &= ~(1 << 29);
1868
+ s500_writel(regval, phyaddr);
1869
+ }
1870
+
1871
+ if(INPUT == mode || OUTPUT == mode)
1872
+ {
1873
+ // Disable input/output function
1874
+ if(INPUT == mode)
1875
+ {
1876
+ phyaddr = gpio + (bank * 3) + 0x00; // +0x00 -> Output Enable Register
1877
+ }
1878
+ else //OUTPUT
1879
+ {
1880
+ phyaddr = gpio + (bank * 3) + 0x01; // +0x04(Byte) -> Input Enable Register
1881
+ }
1882
+ regval = s500_readl(phyaddr);
1883
+ if (wiringPiDebug)
1884
+ {
1885
+ printf("func:%s pin:%d, MODE:%d bank:%d index:%d phyaddr:0x%x read reg val: 0x%x \n", __func__, pin , mode, bank, index, (uint32_t)phyaddr, regval);
1886
+ }
1887
+ regval &= ~(1 << index);
1888
+ s500_writel(regval, phyaddr);
1889
+ if (wiringPiDebug)
1890
+ {
1891
+ regval = s500_readl(phyaddr);
1892
+ printf("set over reg val: 0x%x\n", regval);
1893
+ }
1894
+
1895
+ //Enable input/output function
1896
+ if(INPUT == mode)
1897
+ {
1898
+ phyaddr = gpio + (bank * 3) + 0x01; // +0x04(Byte) -> Input Enable Register
1899
+ }
1900
+ else //OUTPUT
1901
+ {
1902
+ phyaddr = gpio + (bank * 3) + 0x00; // +0x00 -> Output Enable Register
1903
+ }
1904
+ regval = s500_readl(phyaddr);
1905
+ if (wiringPiDebug)
1906
+ {
1907
+ printf("func:%s pin:%d, MODE:%d bank:%d index:%d phyaddr:0x%x read reg val: 0x%x \n", __func__, pin , mode, bank, index, (uint32_t)phyaddr, regval);
1908
+ }
1909
+ regval |= (1 << index);
1910
+ s500_writel(regval, phyaddr);
1911
+ if (wiringPiDebug)
1912
+ {
1913
+ regval = s500_readl(phyaddr);
1914
+ printf("set over reg val: 0x%x\n", regval);
1915
+ }
1916
+
1917
+ }
1918
+ else if(PWM_OUTPUT == mode)
1919
+ {
1920
+ if(pin != 40)
1921
+ {
1922
+ printf("the pin you choose is not surport hardware PWM\n");
1923
+ printf("you can select KS_OUT1/GPIOB8 for PWM pin\n");
1924
+ printf("or you can use it in softPwm mode\n");
1925
+ return ;
1926
+ }
1927
+
1928
+
1929
+ s500_pwm_set_enable(1);
1930
+ //set default M:S to 1/2
1931
+ s500_pwm_set_clk_source(1); //default clk:24M
1932
+ s500_pwm_set_clk(120); // 24M/120
1933
+ s500_pwm_set_period(1023);
1934
+ s500_pwm_set_act(512);
1935
+ delayMicroseconds (200);
1936
+ printf("PWM_OUTPUT...............\n");
1937
+ }
1938
+ else
1939
+ {
1940
+ return;
1941
+ }
1942
+ }
1943
+ else
1944
+ {
1945
+ printf("func:%s line:%dpin number error\n", __func__, __LINE__);
1946
+ }
1947
+ }
1948
+ else
1949
+ {
1950
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
1951
+
1952
+ if ((node = wiringPiFindNode (pin)) != NULL)
1953
+ {
1954
+ node->pinMode (node, pin, mode) ;
1955
+ }
1956
+ }
1957
+
1958
+ }
1959
+
1960
+
1961
+ static void sunxi_set_gpio_mode_wrap(int pin, int mode)
1962
+ {
1963
+ if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1964
+ {
1965
+ if (wiringPiMode == WPI_MODE_PINS)
1966
+ pin = pinToGpio_BP [pin] ;
1967
+ else if (wiringPiMode == WPI_MODE_PHYS)
1968
+ pin = physToGpio_BP[pin] ;
1969
+ else if (wiringPiMode == WPI_MODE_GPIO)
1970
+ pin = pinTobcm_BP[pin]; //need map A20 to bcm
1971
+ else return ;
1972
+
1973
+ if (-1 == pin) /*VCC or GND return directly*/
1974
+ {
1975
+ //printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
1976
+ return;
1977
+ }
1978
+
1979
+ if (mode == INPUT)
1980
+ {
1981
+ sunxi_set_gpio_mode(pin, INPUT);
1982
+ wiringPinMode = INPUT;
1983
+ return ;
1984
+ }
1985
+ else if (mode == OUTPUT)
1986
+ {
1987
+ sunxi_set_gpio_mode(pin, OUTPUT); //gootoomoon_set_mode
1988
+ wiringPinMode = OUTPUT;
1989
+ return ;
1990
+ }
1991
+ else if (mode == PWM_OUTPUT)
1992
+ {
1993
+ if(pin != 259)
1994
+ {
1995
+ printf("the pin you choose is not surport hardware PWM\n");
1996
+ printf("you can select PI3 for PWM pin\n");
1997
+ printf("or you can use it in softPwm mode\n");
1998
+ return ;
1999
+ }
2000
+ //printf("you choose the hardware PWM:%d\n", 1);
2001
+ sunxi_set_gpio_mode(pin, PWM_OUTPUT);
2002
+ wiringPinMode = PWM_OUTPUT;
2003
+ return ;
2004
+ }
2005
+ else
2006
+ return ;
2007
+ }
2008
+ else
2009
+ {
2010
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2011
+
2012
+ if ((node = wiringPiFindNode (pin)) != NULL)
2013
+ node->pinMode (node, pin, mode) ;
2014
+ return ;
2015
+ }
2016
+
2017
+ }
2018
+
2019
+
2020
+
2021
+ /*
2022
+ * pinMode:
2023
+ * Sets the mode of a pin to be input, output or PWM output
2024
+ *********************************************************************************
2025
+ */
2026
+
2027
+ void pinMode (int pin, int mode)
2028
+ {
2029
+
2030
+ //add for S500
2031
+ if(S500_REV == version )
2032
+ {
2033
+ if (wiringPiDebug)
2034
+ {
2035
+ printf ("%s,%d,pin:%d,mode:%d\n", __func__, __LINE__, pin, mode) ;
2036
+ }
2037
+
2038
+ s500_set_gpio_mode(pin, mode);
2039
+
2040
+ return ;
2041
+ }
2042
+ /*add for BananaPro by LeMaker team*/
2043
+ else if(BP_REV == version )
2044
+ {
2045
+ if (wiringPiDebug)
2046
+ printf ("%s,%d,pin:%d,mode:%d\n", __func__, __LINE__, pin, mode) ;
2047
+
2048
+ sunxi_set_gpio_mode_wrap(pin, mode);
2049
+
2050
+ return ;
2051
+ }
2052
+ /*end 2014.08.19*/
2053
+ else
2054
+ {
2055
+ printf("Hardware revision is error !!!\n");
2056
+ }
2057
+
2058
+ }
2059
+
2060
+
2061
+ void s500_pullUpDnControl (int pin, int pud)
2062
+ {
2063
+ uint32_t regval = 0;
2064
+ volatile uint32_t *phyaddr = NULL;
2065
+
2066
+ if(wiringPiMode == WPI_MODE_PINS)
2067
+ {
2068
+ pin = s500_pinToGpio[pin];
2069
+ }
2070
+ else if (wiringPiMode == WPI_MODE_PHYS)
2071
+ {
2072
+ pin = s500_physToGpio[pin] ;
2073
+ }
2074
+ else if (wiringPiMode == WPI_MODE_GPIO)
2075
+ {
2076
+ pin = s500_pinTobcm[pin];
2077
+ }
2078
+ else
2079
+ {
2080
+ return ;
2081
+ }
2082
+
2083
+ if (-1 == pin)
2084
+ {
2085
+ printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
2086
+ return;
2087
+ }
2088
+
2089
+ pud &= 0x01;
2090
+
2091
+ switch(pin)
2092
+ {
2093
+ case 40: /* KS_OUT1/GPIOB8 */
2094
+ {
2095
+ phyaddr = gpio + (0x0060 >> 2);
2096
+ regval = s500_readl(phyaddr);
2097
+ if(pud)//Enable
2098
+ {
2099
+ regval |= (1 << 1); // bit 1
2100
+ }
2101
+ else//Disable
2102
+ {
2103
+ regval &= ~(1 << 1);
2104
+ }
2105
+ s500_writel(regval, phyaddr);
2106
+ }
2107
+ break;
2108
+
2109
+ case 41: /* KS_OUT2/GPIOB9 */
2110
+ {
2111
+ phyaddr = gpio + (0x0060 >> 2);
2112
+ regval = s500_readl(phyaddr);
2113
+ if(pud)//Enable
2114
+ {
2115
+ regval |= (1 << 28); // bit 28
2116
+ }
2117
+ else//Disable
2118
+ {
2119
+ regval &= ~(1 << 28);
2120
+ }
2121
+ s500_writel(regval, phyaddr);
2122
+ }
2123
+ break;
2124
+
2125
+ case 65: /* DSI_DN3/GPIOC1 */
2126
+ {
2127
+ phyaddr = gpio + (0x0060 >> 2); //0x0060/4 is bit to 32bit format
2128
+ regval = s500_readl(phyaddr);
2129
+ if(pud)//Enable
2130
+ {
2131
+ regval |= (1 << 26); // bit 26
2132
+ }
2133
+ else//Disable
2134
+ {
2135
+ regval &= ~(1 << 26);
2136
+ }
2137
+ s500_writel(regval, phyaddr);
2138
+ }
2139
+ break;
2140
+
2141
+ case 68: /* DSI_CP/GPIOC4 */
2142
+ {
2143
+ phyaddr = gpio + (0x0064 >> 2); //0x0064/4 is bit to 32bit format
2144
+ regval = s500_readl(phyaddr);
2145
+ if(pud)//Enable
2146
+ {
2147
+ regval |= (1 << 31); // bit 31
2148
+ }
2149
+ else//Disable
2150
+ {
2151
+ regval &= ~(1 << 31);
2152
+ }
2153
+ s500_writel(regval, phyaddr);
2154
+ }
2155
+ break;
2156
+
2157
+ case 69: /* DSI_CN/GPIOC5 */
2158
+ {
2159
+ phyaddr = gpio + (0x0064 >> 2); //0x0064/4 is bit to 32bit format
2160
+ regval = s500_readl(phyaddr);
2161
+ if(pud)//Enable
2162
+ {
2163
+ regval |= (1 << 30); // bit 30
2164
+ }
2165
+ else//Disable
2166
+ {
2167
+ regval &= ~(1 << 30);
2168
+ }
2169
+ s500_writel(regval, phyaddr);
2170
+ }
2171
+ break;
2172
+
2173
+ case 90: /* UART0_RX/GPIOC26 */
2174
+ {
2175
+ phyaddr = gpio + (0x0064 >> 2);
2176
+ regval = s500_readl(phyaddr);
2177
+ if(pud)//Enable
2178
+ {
2179
+ regval |= (1 << 2); // bit 2
2180
+ }
2181
+ else//Disable
2182
+ {
2183
+ regval &= ~(1 << 2);
2184
+ }
2185
+ s500_writel(regval, phyaddr);
2186
+ }
2187
+ break;
2188
+
2189
+ case 91: /* UART0_TX/GPIOC27 */
2190
+ {
2191
+ phyaddr = gpio + (0x0064 >> 2);
2192
+ regval = s500_readl(phyaddr);
2193
+ if(pud)//Enable
2194
+ {
2195
+ regval |= (1 << 1); // bit 1
2196
+ }
2197
+ else//Disable
2198
+ {
2199
+ regval &= ~(1 << 1);
2200
+ }
2201
+ s500_writel(regval, phyaddr);
2202
+ }
2203
+ break;
2204
+
2205
+ case 130: /* TWI2_SCK/GPIOE2 */
2206
+ {
2207
+ phyaddr = gpio + (0x0068 >> 2);
2208
+ regval = s500_readl(phyaddr);
2209
+ if(pud)//Enable
2210
+ {
2211
+ regval |= (1 << 7); // bit 7
2212
+ }
2213
+ else//Disable
2214
+ {
2215
+ regval &= ~(1 << 7);
2216
+ }
2217
+ s500_writel(regval, phyaddr);
2218
+ }
2219
+ break;
2220
+
2221
+ case 131: /* TWI2_SDA/GPIOE3 */
2222
+ {
2223
+ phyaddr = gpio + (0x0068 >> 2);
2224
+ regval = s500_readl(phyaddr);
2225
+ if(pud)//Enable
2226
+ {
2227
+ regval |= (1 << 8); // bit 8
2228
+ }
2229
+ else//Disable
2230
+ {
2231
+ regval &= ~(1 << 8);
2232
+ }
2233
+ s500_writel(regval, phyaddr);
2234
+ }
2235
+ break;
2236
+
2237
+ default:
2238
+ return;
2239
+ }
2240
+
2241
+ if(wiringPiDebug)
2242
+ {
2243
+ printf ("%s,%d,pin:%d, set over reg val: 0x%x\n", __func__, __LINE__, pin, regval) ;
2244
+ }
2245
+
2246
+ delay (10) ;
2247
+
2248
+ return ;
2249
+ }
2250
+
2251
+
2252
+ /*
2253
+ * pullUpDownCtrl:
2254
+ * Control the internal pull-up/down resistors on a GPIO pin
2255
+ * The Arduino only has pull-ups and these are enabled by writing 1
2256
+ * to a port when in input mode - this paradigm doesn't quite apply
2257
+ * here though.
2258
+ *********************************************************************************
2259
+ */
2260
+
2261
+ void pullUpDnControl (int pin, int pud)
2262
+ {
2263
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2264
+
2265
+
2266
+ //add for s500
2267
+ if(version == S500_REV)
2268
+ {
2269
+ if((pin & PI_GPIO_MASK) == 0) // On-Board Pin
2270
+ {
2271
+ s500_pullUpDnControl(pin, pud);
2272
+ }
2273
+ else
2274
+ {
2275
+ if((node = wiringPiFindNode (pin)) != NULL)
2276
+ {
2277
+ node->pullUpDnControl (node, pin, pud) ;
2278
+ }
2279
+ }
2280
+
2281
+ return;
2282
+ }
2283
+ /*add for BananaPro by LeMaker team*/
2284
+ else if(version == BP_REV)
2285
+ {
2286
+ if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
2287
+ {
2288
+ if (wiringPiMode == WPI_MODE_PINS)
2289
+ pin = pinToGpio_BP [pin] ;
2290
+ else if (wiringPiMode == WPI_MODE_PHYS)
2291
+ pin = physToGpio_BP[pin] ;
2292
+ else if (wiringPiMode == WPI_MODE_GPIO)
2293
+ pin = pinTobcm_BP[pin]; //need map A20 to bcm
2294
+ else return ;
2295
+ if (wiringPiDebug)
2296
+ printf ("%s,%d,pin:%d\n", __func__, __LINE__, pin) ;
2297
+
2298
+ if (-1 == pin)
2299
+ {
2300
+ printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
2301
+ return;
2302
+ }
2303
+
2304
+ pud = upDnConvert[pud];
2305
+ sunxi_pullUpDnControl(pin, pud);
2306
+ return;
2307
+ }
2308
+ else // Extension module
2309
+ {
2310
+ if ((node = wiringPiFindNode (pin)) != NULL)
2311
+ node->pullUpDnControl (node, pin, pud) ;
2312
+ return ;
2313
+ }
2314
+ }
2315
+ /*end 2014.08.19*/
2316
+ else
2317
+ {
2318
+ printf("Hardware revision is error !!!\n");
2319
+ }
2320
+
2321
+
2322
+ }
2323
+
2324
+ static int s500_digitalRead(int pin)
2325
+ {
2326
+ uint32_t regval = 0;
2327
+ char c ;
2328
+
2329
+ if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
2330
+ {
2331
+ if (wiringPiMode == WPI_MODE_PHYS)
2332
+ {
2333
+ pin = s500_physToGpio[pin] ;
2334
+ }
2335
+ else if(wiringPiMode == WPI_MODE_PINS)
2336
+ {
2337
+ pin = s500_pinToGpio[pin];
2338
+ }
2339
+ else if(wiringPiMode == WPI_MODE_GPIO)
2340
+ {
2341
+ pin = s500_pinTobcm[pin];
2342
+ }
2343
+ else if(wiringPiMode == WPI_MODE_GPIO_SYS)
2344
+ {
2345
+ pin = s500_pinTobcm[pin];
2346
+
2347
+ if(pin == -1)
2348
+ {
2349
+ printf("%d %s,%d invalid pin,please check it over.\n", pin, __func__, __LINE__);
2350
+ return 0;
2351
+ }
2352
+
2353
+ if (s500_sysFds [pin] == -1)
2354
+ {
2355
+ if (wiringPiDebug)
2356
+ {
2357
+ printf ("pin %d sysFds -1.%s,%d\n", pin , __func__, __LINE__) ;
2358
+ }
2359
+ return LOW ;
2360
+ }
2361
+
2362
+ if (wiringPiDebug)
2363
+ {
2364
+ printf ("pin %d :%d.%s,%d\n", pin , s500_sysFds [pin], __func__, __LINE__) ;
2365
+ }
2366
+
2367
+ lseek (s500_sysFds [pin], 0L, SEEK_SET) ;
2368
+ int ret = read (s500_sysFds [pin], &c, 1) ;
2369
+ ret = ret ;//Ϊ��ȥ�����뾯��
2370
+
2371
+ return (c == '0') ? LOW : HIGH ;
2372
+
2373
+ }
2374
+ else
2375
+ {
2376
+ return LOW;
2377
+ }
2378
+
2379
+ if(pin != -1)
2380
+ {
2381
+ int bank = pin >> 5;
2382
+ int index = pin - (bank << 5);
2383
+ volatile uint32_t *phyaddr = gpio + (bank * 3) + 0x02; // +0x08 -> data reg
2384
+
2385
+ regval = s500_readl(phyaddr);
2386
+ regval = regval >> index;
2387
+ regval &= 1;
2388
+
2389
+ if (wiringPiDebug)
2390
+ {
2391
+ printf("***** read reg val: 0x%x,bank:%d,index:%d,line:%d\n", regval, bank, index, __LINE__);
2392
+ }
2393
+
2394
+ return regval;
2395
+ }
2396
+ else
2397
+ {
2398
+ printf("pin number error\n");
2399
+ return regval;
2400
+ }
2401
+ }
2402
+ else
2403
+ {
2404
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2405
+
2406
+ if ((node = wiringPiFindNode (pin)) == NULL)
2407
+ {
2408
+ return LOW ;
2409
+ }
2410
+
2411
+ return node->digitalRead (node, pin) ;
2412
+ }
2413
+ }
2414
+
2415
+
2416
+ static int sunxi_digitalRead_wrap(int pin)
2417
+ {
2418
+ char c ;
2419
+ if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
2420
+ {
2421
+ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
2422
+ {
2423
+
2424
+ if(pin == 0)
2425
+ {
2426
+ //printf("%d %s,%d invalid pin,please check it over.\n",pin,__func__, __LINE__);
2427
+ return 0;
2428
+ }
2429
+ if(syspin[pin] == -1)
2430
+ {
2431
+ //printf("%d %s,%d invalid pin,please check it over.\n",pin,__func__, __LINE__);
2432
+ return 0;
2433
+ }
2434
+ if (sysFds [pin] == -1)
2435
+ {
2436
+ if (wiringPiDebug)
2437
+ printf ("pin %d sysFds -1.%s,%d\n", pin , __func__, __LINE__) ;
2438
+ return LOW ;
2439
+ }
2440
+ if (wiringPiDebug)
2441
+ printf ("pin %d :%d.%s,%d\n", pin , sysFds [pin], __func__, __LINE__) ;
2442
+ lseek (sysFds [pin], 0L, SEEK_SET) ;
2443
+ int ret = read (sysFds [pin], &c, 1) ;
2444
+ ret = ret;//Ϊ�˳�ȥ���뾯��
2445
+ return (c == '0') ? LOW : HIGH ;
2446
+ }
2447
+ else if (wiringPiMode == WPI_MODE_PINS)
2448
+ pin = pinToGpio_BP [pin] ;
2449
+ else if (wiringPiMode == WPI_MODE_PHYS)
2450
+ pin = physToGpio_BP[pin] ;
2451
+ else if (wiringPiMode == WPI_MODE_GPIO)
2452
+ pin = pinTobcm_BP[pin]; //need map A20 to bcm
2453
+ else
2454
+ return LOW ;
2455
+ if(-1 == pin)
2456
+ {
2457
+ printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
2458
+ return LOW;
2459
+ }
2460
+ return sunxi_digitalRead(pin);
2461
+ }
2462
+ else
2463
+ {
2464
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2465
+
2466
+ if ((node = wiringPiFindNode (pin)) == NULL)
2467
+ return LOW ;
2468
+ return node->digitalRead (node, pin) ;
2469
+ }
2470
+ }
2471
+
2472
+
2473
+ /*
2474
+ * digitalRead:
2475
+ * Read the value of a given Pin, returning HIGH or LOW
2476
+ *********************************************************************************
2477
+ */
2478
+
2479
+ int digitalRead (int pin)
2480
+ {
2481
+
2482
+ //add for S500
2483
+ if(S500_REV == version )
2484
+ {
2485
+ return s500_digitalRead(pin);
2486
+ }
2487
+ /*add for BananaPro by LeMaker team*/
2488
+ else if(BP_REV == version)
2489
+ {
2490
+ return sunxi_digitalRead_wrap(pin);
2491
+ }
2492
+ /*end 2014.08.19*/
2493
+ else
2494
+ {
2495
+ printf("Hardware revision is error\n");
2496
+ }
2497
+
2498
+ return -1;
2499
+ }
2500
+
2501
+
2502
+ static void s500_digitalWrite(int pin, int value)
2503
+ {
2504
+ uint32_t regval = 0;
2505
+
2506
+
2507
+ if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
2508
+ {
2509
+ if (wiringPiMode == WPI_MODE_PHYS)
2510
+ {
2511
+ pin = s500_physToGpio[pin] ;
2512
+ }
2513
+ else if(wiringPiMode == WPI_MODE_PINS)
2514
+ {
2515
+ pin = s500_pinToGpio[pin];
2516
+ }
2517
+ else if(wiringPiMode == WPI_MODE_GPIO)
2518
+ {
2519
+ pin = s500_pinTobcm[pin];
2520
+ }
2521
+ else if(wiringPiMode == WPI_MODE_GPIO_SYS)
2522
+ {
2523
+ pin = s500_pinTobcm[pin];
2524
+
2525
+ if (s500_sysFds [pin] != -1)
2526
+ {
2527
+ int ret = -1;
2528
+ if (value == LOW)
2529
+ {
2530
+ ret = write (s500_sysFds [pin], "0\n", 2) ;
2531
+ }
2532
+ else
2533
+ {
2534
+ ret = write (s500_sysFds [pin], "1\n", 2) ;
2535
+ }
2536
+ ret = ret;//Ϊ�˳�ȥ���뾯��
2537
+ }
2538
+ return;
2539
+ }
2540
+ else
2541
+ {
2542
+ return;
2543
+ }
2544
+
2545
+ if(pin != -1)
2546
+ {
2547
+ int bank = pin >> 5;
2548
+ int index = pin - (bank << 5);
2549
+ volatile uint32_t *phyaddr = gpio + (bank * 3) + 0x02; // +0x08 -> data reg
2550
+
2551
+ regval = s500_readl(phyaddr);
2552
+
2553
+ if (wiringPiDebug)
2554
+ {
2555
+ printf("befor write reg val: 0x%x,index:%d\n", regval, index);
2556
+ }
2557
+
2558
+ if(0 == value) //Low
2559
+ {
2560
+ regval &= ~(1 << index);
2561
+ s500_writel(regval, phyaddr);
2562
+ }
2563
+ else //High
2564
+ {
2565
+ regval |= (1 << index);
2566
+ s500_writel(regval, phyaddr);
2567
+ }
2568
+
2569
+ if (wiringPiDebug)
2570
+ {
2571
+ regval = s500_readl(phyaddr);
2572
+ printf("after write reg val: 0x%x\n", regval);
2573
+ }
2574
+
2575
+ }
2576
+ else
2577
+ {
2578
+ printf("pin number error\n");
2579
+ return ;//regval;
2580
+ }
2581
+ }
2582
+ else
2583
+ {
2584
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2585
+
2586
+ if ((node = wiringPiFindNode (pin)) != NULL)
2587
+ {
2588
+ node->digitalWrite (node, pin, value) ;
2589
+ }
2590
+ }
2591
+
2592
+ }
2593
+
2594
+
2595
+ static void sxunxi_digitalWrite_wrap(int pin, int value)
2596
+ {
2597
+ if (wiringPiDebug)
2598
+ printf ("%s,%d\n", __func__, __LINE__) ;
2599
+
2600
+ if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
2601
+ {
2602
+ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
2603
+ {
2604
+ if (wiringPiDebug)
2605
+ {
2606
+ printf("%d %s,%d invalid pin,please check it over.\n", pin, __func__, __LINE__);
2607
+ }
2608
+
2609
+ if(pin == 0)
2610
+ {
2611
+ //printf("%d %s,%d invalid pin,please check it over.\n",pin,__func__, __LINE__);
2612
+ return;
2613
+ }
2614
+
2615
+ if(syspin[pin] == -1)
2616
+ {
2617
+ //printf("%d %s,%d invalid pin,please check it over.\n",pin,__func__, __LINE__);
2618
+ return;
2619
+ }
2620
+
2621
+ if (sysFds [pin] == -1)
2622
+ {
2623
+ if (wiringPiDebug)
2624
+ printf ("pin %d sysFds -1.%s,%d\n", pin , __func__, __LINE__) ;
2625
+ }
2626
+
2627
+ if (sysFds [pin] != -1)
2628
+ {
2629
+ int ret = -1;
2630
+ if (wiringPiDebug)
2631
+ printf ("pin %d :%d.%s,%d\n", pin , sysFds [pin], __func__, __LINE__) ;
2632
+ if (value == LOW)
2633
+ ret = write (sysFds [pin], "0\n", 2) ;
2634
+ else
2635
+ ret = write (sysFds [pin], "1\n", 2) ;
2636
+
2637
+ ret = ret;//Ϊ�˳�ȥ���뾯��
2638
+ }
2639
+ return ;
2640
+ }
2641
+ else if (wiringPiMode == WPI_MODE_PINS)
2642
+ pin = pinToGpio_BP [pin] ;
2643
+ else if (wiringPiMode == WPI_MODE_PHYS)
2644
+ pin = physToGpio_BP[pin] ;
2645
+ else if (wiringPiMode == WPI_MODE_GPIO)
2646
+ pin = pinTobcm_BP[pin]; //need map A20 to bcm
2647
+ else
2648
+ return ;
2649
+
2650
+ if(-1 == pin)
2651
+ {
2652
+ //printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
2653
+ return ;
2654
+ }
2655
+ sunxi_digitalWrite(pin, value);
2656
+ }
2657
+ else
2658
+ {
2659
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2660
+ if ((node = wiringPiFindNode (pin)) != NULL)
2661
+ node->digitalWrite (node, pin, value) ;
2662
+ }
2663
+ }
2664
+
2665
+
2666
+
2667
+ /*
2668
+ * digitalWrite:
2669
+ * Set an output bit
2670
+ *********************************************************************************
2671
+ */
2672
+
2673
+ void digitalWrite (int pin, int value)
2674
+ {
2675
+ //add for S500
2676
+ if(S500_REV == version )
2677
+ {
2678
+ s500_digitalWrite(pin, value);
2679
+ return;
2680
+ }
2681
+ /*add for BananaPro by LeMaker team*/
2682
+ else if(BP_REV == version)
2683
+ {
2684
+ sxunxi_digitalWrite_wrap(pin, value);
2685
+ return;
2686
+ }
2687
+ /*end 2014.08.19*/
2688
+ else
2689
+ {
2690
+ printf("Hardware revison is error !!!\n");
2691
+ }
2692
+
2693
+ }
2694
+
2695
+
2696
+ void s500_pwmWrite (int pin, int value)
2697
+ {
2698
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2699
+
2700
+ if (pin < MAX_PIN_NUM)
2701
+ {
2702
+ if (wiringPiMode == WPI_MODE_PHYS)
2703
+ {
2704
+ pin = s500_physToGpio[pin] ;
2705
+ }
2706
+ else if(wiringPiMode == WPI_MODE_PINS)
2707
+ {
2708
+ pin = s500_pinToGpio[pin];
2709
+ }
2710
+ else if(wiringPiMode == WPI_MODE_GPIO)
2711
+ {
2712
+ pin = s500_pinTobcm[pin];
2713
+ }
2714
+ else
2715
+ {
2716
+ return;
2717
+ }
2718
+
2719
+ if(pin != -1)
2720
+ {
2721
+ if(pin != 40)
2722
+ {
2723
+ printf("please use soft pwmmode or choose PWM pin\n");
2724
+ return ;
2725
+ }
2726
+
2727
+ uint32_t a_val = 0;
2728
+
2729
+ a_val = s500_pwm_get_period();
2730
+
2731
+ if(wiringPiDebug)
2732
+ {
2733
+ printf("==> no:%d period now is :%d,act_val to be set:%d\n", __LINE__, a_val, value);
2734
+ }
2735
+
2736
+ if(value > 1023 || value < 0)
2737
+ {
2738
+ printf("val pwmWrite 0 <= X <= 1023\n");;
2739
+ return;
2740
+ }
2741
+
2742
+ s500_pwm_set_act(value);
2743
+
2744
+ }
2745
+ }
2746
+ else
2747
+ {
2748
+ printf ("not on board :%s,%d\n", __func__, __LINE__) ;
2749
+ if((node = wiringPiFindNode (pin)) != NULL)
2750
+ {
2751
+ if (wiringPiDebug)
2752
+ {
2753
+ printf ("Jim find node%s,%d\n", __func__, __LINE__) ;
2754
+ }
2755
+ node->digitalWrite (node, pin, value) ;
2756
+ }
2757
+ }
2758
+
2759
+ if (wiringPiDebug)
2760
+ {
2761
+ printf ("this fun is ok now %s,%d\n", __func__, __LINE__) ;
2762
+ }
2763
+ }
2764
+
2765
+
2766
+ void sunxi_pwmWrite (int pin, int value)
2767
+ {
2768
+ uint32_t a_val = 0;
2769
+
2770
+ if(pwmmode == 1) //sycle
2771
+ {
2772
+ sunxi_pwm_set_mode(1);
2773
+ }
2774
+ else
2775
+ {
2776
+ //sunxi_pwm_set_mode(0);
2777
+ }
2778
+
2779
+ if (pin < MAX_PIN_NUM) // On-Board Pin needto fix me Jim
2780
+ {
2781
+ if (wiringPiMode == WPI_MODE_PINS)
2782
+ pin = pinToGpio_BP [pin] ;
2783
+ else if (wiringPiMode == WPI_MODE_PHYS)
2784
+ {
2785
+ pin = physToGpio_BP[pin] ;
2786
+ }
2787
+ else if (wiringPiMode == WPI_MODE_GPIO)
2788
+ pin = pinTobcm_BP[pin]; //need map A20 to bcm
2789
+ else
2790
+ return ;
2791
+ if(-1 == pin)
2792
+ {
2793
+ printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
2794
+ return ;
2795
+ }
2796
+ if(pin != 259)
2797
+ {
2798
+ printf("please use soft pwmmode or choose PWM pin\n");
2799
+ return ;
2800
+ }
2801
+ a_val = sunxi_pwm_get_period();
2802
+ if (wiringPiDebug)
2803
+ printf("==> no:%d period now is :%d,act_val to be set:%d\n", __LINE__, a_val, value);
2804
+ if(value > a_val)
2805
+ {
2806
+ printf("val pwmWrite 0 <= X <= 1024\n");
2807
+ printf("Or you can set new range by yourself by pwmSetRange(range\n");
2808
+ return;
2809
+ }
2810
+ //if value changed chang it
2811
+ sunxi_pwm_set_enable(0);
2812
+ sunxi_pwm_set_act(value);
2813
+ sunxi_pwm_set_enable(1);
2814
+ }
2815
+ else
2816
+ {
2817
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2818
+ printf ("not on board :%s,%d\n", __func__, __LINE__) ;
2819
+ if ((node = wiringPiFindNode (pin)) != NULL)
2820
+ {
2821
+ if (wiringPiDebug)
2822
+ printf ("Jim find node%s,%d\n", __func__, __LINE__) ;
2823
+ node->digitalWrite (node, pin, value) ;
2824
+ }
2825
+ }
2826
+
2827
+ if (wiringPiDebug)
2828
+ printf ("this fun is ok now %s,%d\n", __func__, __LINE__) ;
2829
+
2830
+ }
2831
+
2832
+
2833
+
2834
+ /*
2835
+ * pwmWrite:
2836
+ * Set an output PWM value
2837
+ *********************************************************************************
2838
+ */
2839
+
2840
+ void pwmWrite (int pin, int value)
2841
+ {
2842
+ if(version == S500_REV)
2843
+ {
2844
+ s500_pwmWrite(pin, value);
2845
+
2846
+ return;
2847
+ }
2848
+ /*add for BananaPro by LeMaker team*/
2849
+ else if(BP_REV == version)
2850
+ {
2851
+ sunxi_pwmWrite(pin, value);
2852
+
2853
+ return;
2854
+ }
2855
+ /*end 2014.08.19*/
2856
+ else
2857
+ {
2858
+ printf("Hardware revison is error !!!\n");
2859
+ }
2860
+ }
2861
+
2862
+
2863
+ /*
2864
+ * analogRead:
2865
+ * Read the analog value of a given Pin.
2866
+ * There is no on-board Pi analog hardware,
2867
+ * so this needs to go to a new node.
2868
+ *********************************************************************************
2869
+ */
2870
+
2871
+ int analogRead (int pin)
2872
+ {
2873
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2874
+
2875
+ if ((node = wiringPiFindNode (pin)) == NULL)
2876
+ return 0 ;
2877
+ else
2878
+ return node->analogRead (node, pin) ;
2879
+ }
2880
+
2881
+
2882
+ /*
2883
+ * analogWrite:
2884
+ * Write the analog value to the given Pin.
2885
+ * There is no on-board Pi analog hardware,
2886
+ * so this needs to go to a new node.
2887
+ *********************************************************************************
2888
+ */
2889
+
2890
+ void analogWrite (int pin, int value)
2891
+ {
2892
+ struct wiringPiNodeStruct *node = wiringPiNodes ;
2893
+
2894
+ if ((node = wiringPiFindNode (pin)) == NULL)
2895
+ return ;
2896
+
2897
+ node->analogWrite (node, pin, value) ;
2898
+ }
2899
+
2900
+
2901
+ /*
2902
+ * pwmToneWrite:
2903
+ * Pi Specific.
2904
+ * Output the given frequency on the Pi's PWM pin
2905
+ *********************************************************************************
2906
+ */
2907
+
2908
+ void pwmToneWrite (int pin, int freq)
2909
+ {
2910
+ int range ;
2911
+
2912
+ if (freq == 0)
2913
+ pwmWrite (pin, 0) ; // Off
2914
+ else
2915
+ {
2916
+ range = 600000 / freq ;
2917
+ pwmSetRange (range) ;
2918
+ pwmWrite (pin, freq / 2) ;
2919
+ }
2920
+ }
2921
+
2922
+ static void s500_digitalWriteByte (int value)
2923
+ {
2924
+ int mask = 1 ;
2925
+ int pin ;
2926
+ int phy2gpio[8] = {11, 12, 13, 15, 16, 18, 22, 7};
2927
+ int bcm2gpio[8] = {17, 18, 27, 22, 23, 24, 26, 4};
2928
+
2929
+ if(wiringPiMode == WPI_MODE_PINS)
2930
+ {
2931
+ for (pin = 0 ; pin < 8 ; ++pin)
2932
+ {
2933
+ pinMode(pin, OUTPUT);
2934
+ delay(1);
2935
+ digitalWrite (pin, value & mask) ;
2936
+ mask <<= 1 ;
2937
+ }
2938
+ }
2939
+ else if(wiringPiMode == WPI_MODE_PHYS)
2940
+ {
2941
+ for (pin = 0 ; pin < 8 ; ++pin)
2942
+ {
2943
+ pinMode(phy2gpio[pin], OUTPUT);
2944
+ delay(1);
2945
+ digitalWrite (phy2gpio[pin], value & mask) ;
2946
+ mask <<= 1 ;
2947
+ }
2948
+ }
2949
+ else if(wiringPiMode == WPI_MODE_GPIO || wiringPiMode == WPI_MODE_GPIO_SYS)
2950
+ {
2951
+ for (pin = 0 ; pin < 8 ; ++pin)
2952
+ {
2953
+ pinMode(bcm2gpio[pin], OUTPUT);
2954
+ delay(1);
2955
+ digitalWrite (bcm2gpio[pin], value & mask) ;
2956
+ mask <<= 1 ;
2957
+ }
2958
+ }
2959
+ else
2960
+ {
2961
+ printf("wiringPiMode is error\n");
2962
+ return;
2963
+ }
2964
+
2965
+ }
2966
+
2967
+ static int head2win[8] = {11, 12, 13, 15, 16, 18, 22, 7}; /*add for BananaPro by lemaker team*/
2968
+
2969
+ static void sunxi_digitalWriteByte (int value)
2970
+ {
2971
+ int mask = 1 ;
2972
+ int pin ;
2973
+
2974
+ if (wiringPiMode == WPI_MODE_GPIO_SYS || wiringPiMode == WPI_MODE_GPIO)
2975
+ {
2976
+
2977
+ for (pin = 0 ; pin < 8 ; ++pin)
2978
+ {
2979
+ pinMode(pin, OUTPUT);
2980
+ delay(1);
2981
+ digitalWrite (pinToGpio [pin], value & mask) ;
2982
+ mask <<= 1 ;
2983
+ }
2984
+
2985
+ }
2986
+ else if(wiringPiMode == WPI_MODE_PINS)
2987
+ {
2988
+
2989
+ for (pin = 0 ; pin < 8 ; ++pin)
2990
+ {
2991
+
2992
+ pinMode(pin, OUTPUT);
2993
+ delay(1);
2994
+ digitalWrite (pin, value & mask) ;
2995
+ mask <<= 1 ;
2996
+ }
2997
+ }
2998
+ else
2999
+ {
3000
+ for (pin = 0 ; pin < 8 ; ++pin)
3001
+ {
3002
+ pinMode(head2win[pin], OUTPUT);
3003
+ delay(1);
3004
+ digitalWrite (head2win[pin], value & mask) ;
3005
+ mask <<= 1 ;
3006
+ }
3007
+ }
3008
+
3009
+ }
3010
+
3011
+
3012
+
3013
+ /*
3014
+ * digitalWriteByte:
3015
+ * Pi Specific
3016
+ * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
3017
+ * fast as possible.
3018
+ * However it still needs 2 operations to set the bits, so any external
3019
+ * hardware must not rely on seeing a change as there will be a change
3020
+ * to set the outputs bits to zero, then another change to set the 1's
3021
+ *********************************************************************************
3022
+ */
3023
+
3024
+ void digitalWriteByte (int value)
3025
+ {
3026
+ //add for s500
3027
+ if(version == S500_REV)
3028
+ {
3029
+ s500_digitalWriteByte(value);
3030
+ return;
3031
+ }
3032
+ /*add for BananaPro by LeMaker team*/
3033
+ else if(BP_REV == version)
3034
+ {
3035
+ sunxi_digitalWriteByte(value);
3036
+ return ;
3037
+ }
3038
+ /*end 2014.08.19*/
3039
+ else
3040
+ {
3041
+ printf("Hardware revison is error !!!\n");
3042
+ }
3043
+
3044
+ }
3045
+
3046
+
3047
+ /*
3048
+ * waitForInterrupt:
3049
+ * Pi Specific.
3050
+ * Wait for Interrupt on a GPIO pin.
3051
+ * This is actually done via the /sys/class/gpio interface regardless of
3052
+ * the wiringPi access mode in-use. Maybe sometime it might get a better
3053
+ * way for a bit more efficiency.
3054
+ *********************************************************************************
3055
+ */
3056
+
3057
+ int waitForInterrupt (int pin, int mS)
3058
+ {
3059
+ int fd, x ;
3060
+ uint8_t c ;
3061
+ struct pollfd polls ;
3062
+
3063
+ if(version == S500_REV)//add for S500
3064
+ {
3065
+ if (wiringPiMode == WPI_MODE_PINS)
3066
+ {
3067
+ pin = s500_pinToGpio[pin];
3068
+ }
3069
+ else if (wiringPiMode == WPI_MODE_PHYS)
3070
+ {
3071
+ pin = s500_physToGpio [pin] ;
3072
+ }
3073
+
3074
+ if ((fd = s500_sysFds [pin]) == -1)
3075
+ {
3076
+ return -2 ;
3077
+ }
3078
+ }
3079
+ else
3080
+ {
3081
+ if (wiringPiMode == WPI_MODE_PINS)
3082
+ pin = pinToGpio [pin] ;
3083
+ else if (wiringPiMode == WPI_MODE_PHYS)
3084
+ pin = physToGpio [pin] ;
3085
+
3086
+ if ((fd = sysFds [pin]) == -1)
3087
+ return -2 ;
3088
+ }
3089
+
3090
+ // Setup poll structure
3091
+
3092
+ polls.fd = fd ;
3093
+ polls.events = POLLPRI ; // Urgent data!
3094
+
3095
+ // Wait for it ...
3096
+
3097
+ x = poll (&polls, 1, mS) ;
3098
+
3099
+ // Do a dummy read to clear the interrupt
3100
+ // A one character read appars to be enough.
3101
+
3102
+ //(void)read (fd, &c, 1) ;
3103
+ int ret = read (fd, &c, 1) ;
3104
+ ret = ret;
3105
+
3106
+ return x ;
3107
+ }
3108
+
3109
+
3110
+ /*
3111
+ * interruptHandler:
3112
+ * This is a thread and gets started to wait for the interrupt we're
3113
+ * hoping to catch. It will call the user-function when the interrupt
3114
+ * fires.
3115
+ *********************************************************************************
3116
+ */
3117
+
3118
+ static void *interruptHandler (void *arg)
3119
+ {
3120
+ int myPin ;
3121
+
3122
+ (void)piHiPri (55) ; // Only effective if we run as root
3123
+
3124
+ myPin = pinPass ;
3125
+ pinPass = -1 ;
3126
+
3127
+ for (;;)
3128
+ if (waitForInterrupt (myPin, -1) > 0)
3129
+ isrFunctions [myPin] () ;
3130
+
3131
+ return NULL ;
3132
+ }
3133
+
3134
+
3135
+ /*
3136
+ * wiringPiISR:
3137
+ * Pi Specific.
3138
+ * Take the details and create an interrupt handler that will do a call-
3139
+ * back to the user supplied function.
3140
+ *********************************************************************************
3141
+ */
3142
+
3143
+ int wiringPiISR (int pin, int mode, void (*function)(void))
3144
+ {
3145
+ pthread_t threadId ;
3146
+ const char *modeS ;
3147
+ char fName [64] ;
3148
+ char pinS [8] ;
3149
+ pid_t pid ;
3150
+ int count, i ;
3151
+ char c ;
3152
+ int bcmGpioPin ;
3153
+
3154
+ if ((pin < 0) || (pin > 63))
3155
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
3156
+
3157
+ if(version == S500_REV)//add for S500
3158
+ {
3159
+ if (wiringPiMode == WPI_MODE_UNINITIALISED)
3160
+ {
3161
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
3162
+ }
3163
+ else if (wiringPiMode == WPI_MODE_PINS)
3164
+ {
3165
+ bcmGpioPin = s500_pinToGpio[pin];
3166
+ }
3167
+ else if (wiringPiMode == WPI_MODE_PHYS)
3168
+ {
3169
+ bcmGpioPin = s500_physToGpio [pin] ;
3170
+ }
3171
+ else
3172
+ {
3173
+ bcmGpioPin = s500_pinTobcm [pin] ;
3174
+ }
3175
+ }
3176
+ else
3177
+ {
3178
+ if (wiringPiMode == WPI_MODE_UNINITIALISED)
3179
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
3180
+ else if (wiringPiMode == WPI_MODE_PINS)
3181
+ bcmGpioPin = pinToGpio [pin] ;
3182
+ else if (wiringPiMode == WPI_MODE_PHYS)
3183
+ bcmGpioPin = physToGpio [pin] ;
3184
+ else
3185
+ bcmGpioPin = pin ;
3186
+ }
3187
+
3188
+ /*add for BananaPro by LeMaker team*/
3189
+
3190
+ if(BP_REV == version)
3191
+ {
3192
+ if(-1 == bcmGpioPin) /**/
3193
+ {
3194
+ printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", __func__, __LINE__, pin);
3195
+ return -1;
3196
+ }
3197
+
3198
+ if(edge[bcmGpioPin] == -1)
3199
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin not sunpprt on bananaPi (%d,%d)\n", pin, bcmGpioPin) ;
3200
+ }
3201
+ /*end 2014.08.19*/
3202
+
3203
+ // Now export the pin and set the right edge
3204
+ // We're going to use the gpio program to do this, so it assumes
3205
+ // a full installation of wiringPi. It's a bit 'clunky', but it
3206
+ // is a way that will work when we're running in "Sys" mode, as
3207
+ // a non-root user. (without sudo)
3208
+
3209
+ if (mode != INT_EDGE_SETUP)
3210
+ {
3211
+ if (mode == INT_EDGE_FALLING)
3212
+ modeS = "falling" ;
3213
+ else if (mode == INT_EDGE_RISING)
3214
+ modeS = "rising" ;
3215
+ else
3216
+ modeS = "both" ;
3217
+
3218
+ sprintf (pinS, "%d", bcmGpioPin) ;
3219
+
3220
+ if ((pid = fork ()) < 0) // Fail
3221
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
3222
+
3223
+ if (pid == 0) // Child, exec
3224
+ {
3225
+ if (access ("/usr/local/bin/gpio", X_OK) == 0)
3226
+ {
3227
+ execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
3228
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
3229
+ }
3230
+ else if (access ("/usr/bin/gpio", X_OK) == 0)
3231
+ {
3232
+ execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
3233
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
3234
+ }
3235
+ else
3236
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
3237
+ }
3238
+ else // Parent, wait
3239
+ wait (NULL) ;
3240
+ }
3241
+
3242
+ // Now pre-open the /sys/class node - but it may already be open if
3243
+ // we are in Sys mode...
3244
+ if(version == S500_REV)//add for S500
3245
+ {
3246
+ if (s500_sysFds [bcmGpioPin] == -1)
3247
+ {
3248
+ sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
3249
+ if ((s500_sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
3250
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
3251
+ }
3252
+
3253
+
3254
+ // Clear any initial pending interrupt
3255
+ int ret = -1;
3256
+ ioctl (s500_sysFds [bcmGpioPin], FIONREAD, &count) ;
3257
+ for (i = 0 ; i < count ; ++i)
3258
+ ret = read (s500_sysFds [bcmGpioPin], &c, 1) ;
3259
+
3260
+ ret = ret;
3261
+ }
3262
+ else
3263
+ {
3264
+ if (sysFds [bcmGpioPin] == -1)
3265
+ {
3266
+ sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
3267
+ if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
3268
+ return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
3269
+ }
3270
+
3271
+
3272
+ // Clear any initial pending interrupt
3273
+ int ret = -1;
3274
+ ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
3275
+ for (i = 0 ; i < count ; ++i)
3276
+ ret = read (sysFds [bcmGpioPin], &c, 1) ;
3277
+
3278
+ ret = ret;
3279
+ }
3280
+
3281
+ isrFunctions [pin] = function ;
3282
+
3283
+ pthread_mutex_lock (&pinMutex) ;
3284
+ pinPass = pin ;
3285
+ pthread_create (&threadId, NULL, interruptHandler, NULL) ;
3286
+ while (pinPass != -1)
3287
+ delay (1) ;
3288
+ pthread_mutex_unlock (&pinMutex) ;
3289
+
3290
+ return 0 ;
3291
+ }
3292
+
3293
+
3294
+ /*
3295
+ * initialiseEpoch:
3296
+ * Initialise our start-of-time variable to be the current unix
3297
+ * time in milliseconds and microseconds.
3298
+ *********************************************************************************
3299
+ */
3300
+
3301
+ static void initialiseEpoch (void)
3302
+ {
3303
+ struct timeval tv ;
3304
+
3305
+ gettimeofday (&tv, NULL) ;
3306
+ epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
3307
+ epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
3308
+ }
3309
+
3310
+
3311
+ /*
3312
+ * delay:
3313
+ * Wait for some number of milliseconds
3314
+ *********************************************************************************
3315
+ */
3316
+
3317
+ void delay (unsigned int howLong)
3318
+ {
3319
+ struct timespec sleeper, dummy ;
3320
+
3321
+ sleeper.tv_sec = (time_t)(howLong / 1000) ;
3322
+ sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
3323
+
3324
+ nanosleep (&sleeper, &dummy) ;
3325
+ }
3326
+
3327
+
3328
+ /*
3329
+ * delayMicroseconds:
3330
+ * This is somewhat intersting. It seems that on the Pi, a single call
3331
+ * to nanosleep takes some 80 to 130 microseconds anyway, so while
3332
+ * obeying the standards (may take longer), it's not always what we
3333
+ * want!
3334
+ *
3335
+ * So what I'll do now is if the delay is less than 100uS we'll do it
3336
+ * in a hard loop, watching a built-in counter on the ARM chip. This is
3337
+ * somewhat sub-optimal in that it uses 100% CPU, something not an issue
3338
+ * in a microcontroller, but under a multi-tasking, multi-user OS, it's
3339
+ * wastefull, however we've no real choice )-:
3340
+ *
3341
+ * Plan B: It seems all might not be well with that plan, so changing it
3342
+ * to use gettimeofday () and poll on that instead...
3343
+ *********************************************************************************
3344
+ */
3345
+
3346
+ void delayMicrosecondsHard (unsigned int howLong)
3347
+ {
3348
+ struct timeval tNow, tLong, tEnd ;
3349
+
3350
+ gettimeofday (&tNow, NULL) ;
3351
+ tLong.tv_sec = howLong / 1000000 ;
3352
+ tLong.tv_usec = howLong % 1000000 ;
3353
+ timeradd (&tNow, &tLong, &tEnd) ;
3354
+
3355
+ while (timercmp (&tNow, &tEnd, < ))
3356
+ gettimeofday (&tNow, NULL) ;
3357
+ }
3358
+
3359
+ void delayMicroseconds (unsigned int howLong)
3360
+ {
3361
+ struct timespec sleeper ;
3362
+ unsigned int uSecs = howLong % 1000000 ;
3363
+ unsigned int wSecs = howLong / 1000000 ;
3364
+
3365
+ if (howLong == 0)
3366
+ return ;
3367
+ else if (howLong < 100)
3368
+ delayMicrosecondsHard (howLong) ;
3369
+ else
3370
+ {
3371
+ sleeper.tv_sec = wSecs ;
3372
+ sleeper.tv_nsec = (long)(uSecs * 1000L) ;
3373
+ nanosleep (&sleeper, NULL) ;
3374
+ }
3375
+ }
3376
+
3377
+
3378
+ /*
3379
+ * millis:
3380
+ * Return a number of milliseconds as an unsigned int.
3381
+ *********************************************************************************
3382
+ */
3383
+
3384
+ unsigned int millis (void)
3385
+ {
3386
+ struct timeval tv ;
3387
+ uint64_t now ;
3388
+
3389
+ gettimeofday (&tv, NULL) ;
3390
+ now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
3391
+
3392
+ return (uint32_t)(now - epochMilli) ;
3393
+ }
3394
+
3395
+
3396
+ /*
3397
+ * micros:
3398
+ * Return a number of microseconds as an unsigned int.
3399
+ *********************************************************************************
3400
+ */
3401
+
3402
+ unsigned int micros (void)
3403
+ {
3404
+ struct timeval tv ;
3405
+ uint64_t now ;
3406
+
3407
+ gettimeofday (&tv, NULL) ;
3408
+ now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
3409
+
3410
+ return (uint32_t)(now - epochMicro) ;
3411
+ }
3412
+
3413
+
3414
+ /*
3415
+ * wiringPiSetup:
3416
+ * Must be called once at the start of your program execution.
3417
+ *
3418
+ * Default setup: Initialises the system into wiringPi Pin mode and uses the
3419
+ * memory mapped hardware directly.
3420
+ *
3421
+ * Changed now to revert to "gpio" mode if we're running on a Compute Module.
3422
+ *********************************************************************************
3423
+ */
3424
+
3425
+ int wiringPiSetup (void)
3426
+ {
3427
+ int fd ;
3428
+ int boardRev ;
3429
+
3430
+ if (getenv (ENV_DEBUG) != NULL)
3431
+ wiringPiDebug = TRUE ;
3432
+
3433
+ if (getenv (ENV_CODES) != NULL)
3434
+ wiringPiReturnCodes = TRUE ;
3435
+
3436
+ if (geteuid () != 0)
3437
+ (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
3438
+
3439
+ if (wiringPiDebug)
3440
+ printf ("wiringPi: wiringPiSetup called\n") ;
3441
+
3442
+ boardRev = piBoardRev () ;
3443
+
3444
+ if(version == S500_REV) //add for s500
3445
+ {
3446
+ pinToGpio = pinToGpioR;
3447
+ physToGpio = physToGpioR;
3448
+ }
3449
+ else if (BP_REV == boardRev) /*modify for BananaPro by LeMaker team*/
3450
+ {
3451
+ pinToGpio = pinToGpioR ;
3452
+ physToGpio = physToGpioR ;
3453
+ physToPin = physToPinR3;
3454
+ }
3455
+ else
3456
+ {
3457
+
3458
+ }
3459
+
3460
+ // Open the master /dev/memory device
3461
+
3462
+ if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
3463
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
3464
+
3465
+
3466
+ if (S500_REV == boardRev)
3467
+ {
3468
+
3469
+ #define S500_MAP_SIZE 4096UL
3470
+ #define S500_MAP_MASK (S500_MAP_SIZE - 1)
3471
+
3472
+ // GPIO:
3473
+ gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, S500_GPIO_BASE) ;
3474
+ if ((int32_t)gpio == -1)
3475
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
3476
+
3477
+ // PWM
3478
+ pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, S500_GPIO_PWM & ~S500_MAP_MASK) ;
3479
+ if ((int32_t)pwm == -1)
3480
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
3481
+
3482
+ // Clock control (needed for PWM)
3483
+ clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, S500_CLOCK_BASE) ;
3484
+ if ((int32_t)clk == -1)
3485
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
3486
+
3487
+ // The drive pads
3488
+ pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, S500_GPIO_PADS) ;
3489
+ if ((int32_t)pads == -1)
3490
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
3491
+
3492
+ #ifdef USE_TIMER
3493
+ // The system timer
3494
+ timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, S500_GPIO_TIMER) ;
3495
+ if ((int32_t)timer == -1)
3496
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
3497
+
3498
+ // Set the timer to free-running, 1MHz.
3499
+ // 0xF9 is 249, the timer divide is base clock / (divide+1)
3500
+ // so base clock is 250MHz / 250 = 1MHz.
3501
+
3502
+ *(timer + TIMER_CONTROL) = 0x0000280 ;
3503
+ *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
3504
+ timerIrqRaw = timer + TIMER_IRQ_RAW ;
3505
+ #endif
3506
+
3507
+ }
3508
+ else if (BP_REV == boardRev) /*modify for BananaPro by LeMaker team*/
3509
+ {
3510
+ // GPIO:
3511
+ gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE_BP);
3512
+
3513
+ if ((int32_t)gpio == -1)
3514
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
3515
+
3516
+ // PWM
3517
+
3518
+ pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_PWM_BP) ;
3519
+ if ((int32_t)pwm == -1)
3520
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
3521
+
3522
+ // Clock control (needed for PWM)
3523
+
3524
+ clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE_BP) ;
3525
+ if ((int32_t)clk == -1)
3526
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
3527
+
3528
+ // The drive pads
3529
+
3530
+ pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_PADS_BP) ;
3531
+ if ((int32_t)pads == -1)
3532
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
3533
+
3534
+ #ifdef USE_TIMER
3535
+ // The system timer
3536
+
3537
+ timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER_BP) ;
3538
+ if ((int32_t)timer == -1)
3539
+ return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
3540
+
3541
+ // Set the timer to free-running, 1MHz.
3542
+ // 0xF9 is 249, the timer divide is base clock / (divide+1)
3543
+ // so base clock is 250MHz / 250 = 1MHz.
3544
+
3545
+ *(timer + TIMER_CONTROL) = 0x0000280 ;
3546
+ *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
3547
+ timerIrqRaw = timer + TIMER_IRQ_RAW ;
3548
+ #endif
3549
+ }
3550
+ else
3551
+ {
3552
+
3553
+ }
3554
+
3555
+ initialiseEpoch () ;
3556
+
3557
+ // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
3558
+
3559
+ wiringPiMode = WPI_MODE_PINS ;
3560
+
3561
+ return 0 ;
3562
+ }
3563
+
3564
+
3565
+ /*
3566
+ * wiringPiSetupGpio:
3567
+ * Must be called once at the start of your program execution.
3568
+ *
3569
+ * GPIO setup: Initialises the system into GPIO Pin mode and uses the
3570
+ * memory mapped hardware directly.
3571
+ *********************************************************************************
3572
+ */
3573
+
3574
+ int wiringPiSetupGpio (void)
3575
+ {
3576
+ (void)wiringPiSetup () ;
3577
+
3578
+ if (wiringPiDebug)
3579
+ printf ("wiringPi: wiringPiSetupGpio called\n") ;
3580
+
3581
+ wiringPiMode = WPI_MODE_GPIO ;
3582
+
3583
+ return 0 ;
3584
+ }
3585
+
3586
+
3587
+ /*
3588
+ * wiringPiSetupPhys:
3589
+ * Must be called once at the start of your program execution.
3590
+ *
3591
+ * Phys setup: Initialises the system into Physical Pin mode and uses the
3592
+ * memory mapped hardware directly.
3593
+ *********************************************************************************
3594
+ */
3595
+
3596
+ int wiringPiSetupPhys (void)
3597
+ {
3598
+ (void)wiringPiSetup () ;
3599
+
3600
+ if (wiringPiDebug)
3601
+ printf ("wiringPi: wiringPiSetupPhys called\n") ;
3602
+
3603
+ wiringPiMode = WPI_MODE_PHYS ;
3604
+
3605
+ return 0 ;
3606
+ }
3607
+
3608
+
3609
+ /*
3610
+ * wiringPiSetupSys:
3611
+ * Must be called once at the start of your program execution.
3612
+ *
3613
+ * Initialisation (again), however this time we are using the /sys/class/gpio
3614
+ * interface to the GPIO systems - slightly slower, but always usable as
3615
+ * a non-root user, assuming the devices are already exported and setup correctly.
3616
+ */
3617
+
3618
+ int wiringPiSetupSys (void)
3619
+ {
3620
+ int boardRev ;
3621
+ int pin ;
3622
+ char fName [128] ;
3623
+
3624
+ if (getenv (ENV_DEBUG) != NULL)
3625
+ wiringPiDebug = TRUE ;
3626
+
3627
+ if (getenv (ENV_CODES) != NULL)
3628
+ wiringPiReturnCodes = TRUE ;
3629
+
3630
+ if (wiringPiDebug)
3631
+ printf ("wiringPi: wiringPiSetupSys called\n") ;
3632
+
3633
+ boardRev = piBoardRev () ;
3634
+
3635
+ if(version == S500_REV) //add for s500
3636
+ {
3637
+ pinToGpio = pinToGpioR;
3638
+ physToGpio = physToGpioR;
3639
+ }
3640
+ else if (BP_REV == boardRev) /*modify for BananaPro by LeMaker team*/
3641
+ {
3642
+ pinToGpio = pinToGpioR ;
3643
+ physToGpio = physToGpioR ;
3644
+ physToPin = physToPinR3;
3645
+ }
3646
+ else
3647
+ {
3648
+
3649
+ }
3650
+
3651
+ // Open and scan the directory, looking for exported GPIOs, and pre-open
3652
+ // the 'value' interface to speed things up for later
3653
+ if(boardRev == S500_REV)
3654
+ {
3655
+ char cmdstr[80] = {'\0'};
3656
+ int ret = -1;
3657
+
3658
+ for (pin = 0 ; pin < 132 ; ++pin)
3659
+ {
3660
+ if(s500ValidGpio[pin] != -1)
3661
+ {
3662
+ snprintf(cmdstr, 80, "/usr/local/bin/gpio export %d out", pin);
3663
+ ret = system(cmdstr);
3664
+ sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
3665
+ s500_sysFds [pin] = open (fName, O_RDWR) ;
3666
+ }
3667
+ }
3668
+ ret = ret;
3669
+ }
3670
+ else if(BP_REV == boardRev) /*modify for BananaPro by LeMaker team*/
3671
+ {
3672
+ for (pin = 1 ; pin < 32 ; ++pin)
3673
+ {
3674
+ sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
3675
+ sysFds [pin] = open (fName, O_RDWR) ;
3676
+ }
3677
+ }
3678
+ else
3679
+ {
3680
+
3681
+ }
3682
+
3683
+ initialiseEpoch () ;
3684
+
3685
+ wiringPiMode = WPI_MODE_GPIO_SYS ;
3686
+
3687
+ return 0 ;
3688
+ }