libpruio  0.2
Input/Output driver for digital/analog lines on Beagleboard hardware
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pruio_gpio.bas
Go to the documentation of this file.
1 /'* \file pruio_gpio.bas
2 \brief The GPIO component source code.
3 
4 Source code file containing the function bodies of the GPIO component.
5 
6 '/
7 
8 
9 /'* \brief The constructor for the GPIO features.
10 \param T A pointer of the calling PruIo structure.
11 
12 The constructor prepares the DRam parameters to run the pasm_init.p
13 instructions. The adresses of the subsystems and the adresses of the
14 clock registers get prepared, and the index of the last parameter gets
15 stored to compute the offset in the Init and Conf data blocks.
16 
17 \since 0.2
18 '/
19 CONSTRUCTOR GpioUdt(BYVAL T AS Pruio_ PTR)
20  Top = T
21  WITH *Top
22  VAR i = .ParOffs
23  InitParA = i
24  i += 1 : .DRam[i] = &h44E07000uL
25  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO0, &h44E00408uL, 0)
26 
27  i += 1 : .DRam[i] = &h4804C000uL
28  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO1, &h44E000ACuL, 0)
29 
30  i += 1 : .DRam[i] = &h481AC000uL
31  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO2, &h44E000B0uL, 0)
32 
33  i += 1 : .DRam[i] = &h481AE000uL
34  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO3, &h44E000B4uL, 0)
35  .ParOffs = i
36  END WITH
37 END CONSTRUCTOR
38 
39 
40 /'* \brief Initialize the register context after running the pasm_init.p instructions (private).
41 \returns 0 (zero) on success (may return an error string in future versions).
42 
43 This is a private function, designed to be called from the main
44 constructor PruIo::PruIo(). It sets the pointers to the Init and Conf
45 structures in the data blocks. And it initializes some register
46 context, if the subsystem woke up and is enabled.
47 
48 \since 0.2
49 '/
50 FUNCTION GpioUdt.initialize CDECL() AS ZSTRING PTR
51  WITH *Top
52  VAR p_mem = .MOffs + .DRam[InitParA] _
53  , p_val = CAST(ANY PTR, .DRam) + PRUIO_DAT_GPIO' + offsetof(GpioArr, Mix)
54 
55  FOR i AS LONG = 0 TO PRUIO_AZ_GPIO
56  Raw(i) = p_val
57  p_val += SIZEOF(GpioArr)
58 
59  Init(i) = p_mem
60  Conf(i) = p_mem + .DSize
61 
62  WITH *Conf(i)
63  IF .ClAd = 0 ORELSE _
64  .REVISION = 0 THEN _ ' subsystem not enabled
65  .DeAd = 0 : .ClVa = 0 : p_mem += 16 : CONTINUE FOR
66  .ClVa = 2
67  .CLEARDATAOUT = 0
68  .SETDATAOUT = 0
69  END WITH
70 
71  WITH *Init(i)
72  .CLEARDATAOUT = 0
73  .SETDATAOUT = 0
74  END WITH
75 
76  p_mem += SIZEOF(GpioSet)
77  NEXT
78  END WITH : RETURN 0
79 END FUNCTION
80 
81 
82 /'* \brief Configure a GPIO.
83 \param Ball The CPU ball number to test.
84 \param Mo The modus to set (as Control Module pad register value).
85 \returns Zero on success (otherwise a string with an error message).
86 
87 This function is used to configure a digital pin for GPIO. Use the
88 macros defined in pruio.bi to specify the pin number in parameter
89 *Ball* for a pin on the Beaglebone headers (ie P8_03 selects pin 3 on
90 header P8).
91 
92 Its not possible to set the state of a CPU ball (not connected to a
93 header), because only header pins are prepared for pinmuxing in the
94 libpruio device tree overlay.
95 
96 Parameter *Modus* specifies the pinmux mode for the ARM control module
97 (see \ArmRef{9} for details). By default the pin gets configured as input pin
98 with pulldown resistor. Other configurations are prepared
99 as enumerators PruIo::PinMuxing :
100 
101 | macro name | Description |
102 | ---------------: | :--------------------------------------- |
103 | PRUIO_GPIO_IN | open input pin (no resistor) |
104 | PRUIO_GPIO_IN_0 | low input pin (with pulldown resistor) |
105 | PRUIO_GPIO_IN_1 | high input pin (with pullup resistor) |
106 | PRUIO_GPIO_OUT0 | output pin set to low (no resistor) |
107 | PRUIO_GPIO_OUT1 | output pin set to high (no resistor) |
108 
109 '/
110 FUNCTION GpioUdt.config CDECL( _
111  BYVAL Ball AS UInt8 _
112  , BYVAL Mo AS UInt8 = CAST(UInt8, PRUIO_GPIO_IN_0)) AS ZSTRING PTR
113  WITH *Top
114  BallCheck(" GPIO", .Errr)
115  IF 7 <> (Mo AND &b111) THEN .Errr = @"no GPIO mode" : RETURN .Errr
116  VAR r = .BallGpio(Ball) _ ' resulting GPIO (index and bit number)
117  , i = r SHR 5 _ ' index of GPIO
118  , n = r AND 31 _ ' number of bit
119  , m = 1 SHL n ' mask for bit
120  IF 2 <> Conf(i)->ClVa THEN .Errr = E0 : RETURN .Errr ' GPIO subsystem not enabled
121 
122  VAR x = Mo AND &b1111111
123  IF x <> .BallConf[Ball] THEN IF .setPin(Ball, x) THEN RETURN .Errr
124  IF (x AND PRUIO_RX_ACTIV) = PRUIO_RX_ACTIV THEN RETURN 0 ' input, we're done
125 
126  WITH *Conf(i)
127  IF BIT(Mo, 5) THEN ' input Ball
128  .OE OR= m
129  .CLEARDATAOUT AND= NOT m
130  .SETDATAOUT AND= NOT m
131  m = 0
132  ELSE ' output Ball
133  .OE AND= NOT m
134  IF BIT(Mo, 7) THEN ' set high
135  .CLEARDATAOUT AND= NOT m
136  .SETDATAOUT OR= m
137  ELSE ' set low
138  .CLEARDATAOUT OR= m
139  .SETDATAOUT AND= NOT m
140  END IF
141  END IF
142  END WITH
143  IF .DRam[0] > PRUIO_MSG_IO_OK THEN RETURN 0
144 
145  WHILE .DRam[1] : WEND ' wait, if PRU is busy (should never happen)
146  .DRam[5] = Conf(i)->OE
147  .DRam[4] = Conf(i)->SETDATAOUT
148  .DRam[3] = Conf(i)->CLEARDATAOUT
149  .DRam[2] = Conf(i)->DeAd + &h100
150  .DRam[1] = PRUIO_COM_GPIO_CONF SHL 24
151  END WITH : RETURN 0
152 END FUNCTION
153 
154 
155 /'* \brief Set the state of a GPIO.
156 \param Ball The CPU ball number to test.
157 \param Mo The state to set (0 = low, high otherwise).
158 \returns Zero on success (otherwise a pointer to an error message).
159 
160 This function is used to set the state of an output GPIO. Set parameter
161 *Ball* to the required header pin (=CPU ball) by using the convenience
162 macros defined in pruio_pins.bi (ie. P8_03 selects pin 3 on header P8).
163 
164 Parameter *Mo* specifies either the state to set (0 or 1). Or it specifies the pinmux mode and the state.
165 
166 '/
167 FUNCTION GpioUdt.setValue CDECL( _
168  BYVAL Ball AS UInt8 _
169  , BYVAL Mo AS UInt8 = 0) AS ZSTRING PTR
170 
171  WITH *Top
172  BallCheck(" GPIO output", .Errr)
173  VAR r = .BallGpio(Ball) _ ' resulting GPIO (index and bit number)
174  , i = r SHR 5 _ ' index of GPIO
175  , m = 1 SHL (r AND 31) _ ' mask for bit
176  , x = IIF(Mo > 1, Mo AND &b1111111, PRUIO_GPIO_OUT0) '*< the pinmux mode
177 
178  IF 2 <> Conf(i)->ClVa THEN .Errr = E0 : RETURN .Errr ' GPIO subsystem not enabled
179  IF x AND &b111 <> 7 THEN .Errr = E1 : RETURN .Errr ' no GPIO mode
180  IF .BallConf[Ball] <> x THEN _
181  IF .setPin(Ball, x) THEN RETURN .Errr ' pinmux failed
182 
183  IF Mo = 1 ORELSE BIT(Mo, 7) THEN
184  Conf(i)->CLEARDATAOUT AND= NOT m
185  Conf(i)->SETDATAOUT OR= m
186  ELSE
187  Conf(i)->CLEARDATAOUT OR= m
188  Conf(i)->SETDATAOUT AND= NOT m
189  END IF
190 
191  IF .DRam[0] > PRUIO_MSG_IO_OK THEN RETURN 0
192 
193  WHILE .DRam[1] : WEND ' wait, if PRU is busy (should never happen)
194  .DRam[4] = Conf(i)->SETDATAOUT
195  .DRam[3] = Conf(i)->CLEARDATAOUT
196  .DRam[2] = Conf(i)->DeAd + &h100
197  .DRam[1] = PRUIO_COM_GPIO_OUT SHL 24
198  END WITH : RETURN 0
199 END FUNCTION
200 
201 
202 /'* \brief Get the state of a GPIO.
203 \param Ball The CPU ball number to test.
204 \returns The GPIO state (otherwise -1, check PruIo::Errr for an error message).
205 
206 This function is used to get the state of a digital pin (GPIO). Use the
207 macros defined in pruio.bi to specify the pin number in parameter
208 *Ball* for a pin on the Beaglebone headers (ie P8_03 selects pin 3 on
209 header P8).
210 
211 It's possible to get the state of a CPU ball (not connected to a
212 header), also. In this case you need to find the matching CPU ball
213 number and pass it in as the parameter *Ball*.
214 
215 The function returns the state of input and output pins. Return values
216 are
217 
218 | Value | Description |
219 | ----: | :---------------------------- |
220 | 1 | GPIO is in high state |
221 | 0 | GPIO is in low state |
222 | -1 | error (undefined ball number) |
223 
224 '/
225 FUNCTION GpioUdt.Value CDECL(BYVAL Ball AS UInt8) AS Int32
226  WITH *Top
227  BallCheck(" GPIO input", -1)
228  VAR r = .BallGpio(Ball) _ ' resulting GPIO (index and bit number)
229  , i = r SHR 5 _ ' index of GPIO
230  , n = r AND 31 ' number of bit
231  IF 2 <> Conf(i)->ClVa THEN .Errr = E0 : RETURN -1 ' GPIO subsystem not enabled
232  IF .BallConf[Ball] AND &b111 <> 7 THEN .Errr = E1 : RETURN -1 ' no GPIO pin
233  RETURN IIF(BIT(Raw(i)->Mix, n), 1, 0)
234  END WITH
235 END FUNCTION
236