libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruio_gpio.bas
Go to the documentation of this file.
1 
9 
10 
11 ' PruIo global declarations.
12 #INCLUDE ONCE "pruio_globals.bi"
13 ' Header for GPIO part.
14 #INCLUDE ONCE "pruio_gpio.bi"
15 ' driver header file
16 #INCLUDE ONCE "pruio.bi"
17 
18 
28 CONSTRUCTOR GpioUdt(BYVAL T AS Pruio_ PTR)
29  Top = T
30  WITH *Top
31  VAR i = .ParOffs
32  InitParA = i
33  i += 1 : .DRam[i] = &h44E07000uL
34  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO0, &h44E00408uL, 0)
35 
36  i += 1 : .DRam[i] = &h4804C000uL
37  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO1, &h44E000ACuL, 0)
38 
39  i += 1 : .DRam[i] = &h481AC000uL
40  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO2, &h44E000B0uL, 0)
41 
42  i += 1 : .DRam[i] = &h481AE000uL
43  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_GPIO3, &h44E000B4uL, 0)
44  .ParOffs = i
45  END WITH
46 END CONSTRUCTOR
47 
48 
49 
59 FUNCTION GpioUdt.initialize CDECL() AS ZSTRING PTR
60  WITH *Top
61  VAR p_mem = .MOffs + .DRam[InitParA] _
62  , p_raw = CAST(ANY PTR, .DRam) + PRUIO_DAT_GPIO
63 
64  FOR i AS LONG = 0 TO PRUIO_AZ_GPIO
65  Raw(i) = p_raw
66  p_raw += SIZEOF(GpioArr)
67 
68  Init(i) = p_mem
69  Conf(i) = p_mem + .DSize
70 
71  WITH *Conf(i)
72  IF .ClAd = 0 ORELSE .REVISION = 0 THEN _ ' subsystem disabled
73  .DeAd = 0 : .ClVa = &h30000 : p_mem += 16 : _
74  Init(i)->DeAd = 0 : Init(i)->ClAd = 0 : CONTINUE FOR
75  .ClVa = 2
76  .CLEARDATAOUT = 0
77  .SETDATAOUT = 0
78  END WITH
79 
80  WITH *Init(i)
81  .CLEARDATAOUT = 0
82  .SETDATAOUT = 0
83  var a = i * 32, e = a + 31
84  FOR b AS INTEGER = 0 TO PRUIO_AZ_BALL
85  var p = b - a
86  SELECT CASE Top->BallGpio(b)
87  CASE a TO e : if bit(.OE, p) then continue for ' input pin
88  if BIT(.DATAOUT, p) then Top->BallInit[b] OR= &b10000000
89  END SELECT
90  NEXT
91  END WITH
92 
93  p_mem += SIZEOF(GpioSet)
94  NEXT
95  END WITH : RETURN 0
96 END FUNCTION
97 
98 
99 
130 FUNCTION GpioUdt.config CDECL( _
131  BYVAL Ball AS UInt8 _
132  , BYVAL Mo AS UInt8 = CAST(UInt8, PRUIO_GPIO_IN_0)) AS ZSTRING PTR
133  WITH *Top
134  BallCheck(" GPIO", .Errr)
135  IF 7 <> (Mo AND &b111) THEN .Errr = E1 : RETURN .Errr ' no GPIO mode
136  VAR r = .BallGpio(Ball) ' resulting GPIO (index and bit number)
137  Mode = Mo XOR .BallConf[Ball]
138  IF Mo <> PRUIO_PIN_RESET ANDALSO Mode = 0 THEN RETURN 0 ' nothing to config
139  Indx = r SHR 5 : IF 2 <> Conf(Indx)->ClVa THEN .Errr = E0 : RETURN .Errr ' GPIO subsystem not enabled
140  IF Mode AND &b10100000 THEN ' change direction or value?
141  Mode = Mo
142  Mask = 1 SHL (r AND 31)
143  setGpioSs()
144  END IF : RETURN .setPin(Top, Ball, Mo)
145  END WITH
146 END FUNCTION
147 
148 
149 
158 SUB GpioUdt.setGpioSs()
159  WITH *Conf(Indx)
160  IF BIT(Mode, 5) THEN ' input Ball
161  .OE OR= Mask
162  .CLEARDATAOUT AND= NOT Mask
163  .SETDATAOUT AND= NOT Mask
164  ELSE ' output Ball
165  .OE AND= NOT Mask
166  IF BIT(Mode, 7) THEN ' set high
167  .CLEARDATAOUT AND= NOT Mask
168  .SETDATAOUT OR= Mask
169  ELSE ' set low
170  .CLEARDATAOUT OR= Mask
171  .SETDATAOUT AND= NOT Mask
172  END IF
173  END IF
174  END WITH
175  WITH *Top
176  IF .DRam[0] > PRUIO_MSG_IO_OK THEN EXIT SUB
177 
178  PruReady(1) ' wait, if PRU is busy (should never happen)
179  .DRam[5] = Conf(Indx)->OE
180  .DRam[4] = Conf(Indx)->SETDATAOUT
181  .DRam[3] = Conf(Indx)->CLEARDATAOUT
182  .DRam[2] = Conf(Indx)->DeAd + &h100
183  .DRam[1] = PRUIO_COM_GPIO_CONF SHL 24
184  END WITH
185 END SUB
186 
187 
188 
208 FUNCTION GpioUdt.flush CDECL(BYVAL Indx AS UInt8) AS ZSTRING PTR
209  WITH *Top
210  IF 2 <> Conf(Indx)->ClVa THEN .Errr = E0 : RETURN .Errr ' GPIO subsystem not enabled
211  IF .DRam[0] > PRUIO_MSG_IO_OK _
212  THEN .Errr = @"main loop not running" : RETURN .Errr
213 
214  PruReady(1) ' wait, if PRU is busy (should never happen)
215  .DRam[5] = Conf(Indx)->OE
216  .DRam[4] = Conf(Indx)->SETDATAOUT
217  .DRam[3] = Conf(Indx)->CLEARDATAOUT
218  .DRam[2] = Conf(Indx)->DeAd + &h100
219  .DRam[1] = PRUIO_COM_GPIO_CONF SHL 24
220  END WITH : RETURN 0
221 END FUNCTION
222 
223 
224 
242 FUNCTION GpioUdt.setValue CDECL( _
243  BYVAL Ball AS UInt8 _
244  , BYVAL Mo AS UInt8 = 0) AS ZSTRING PTR
245 
246  WITH *Top
247  BallCheck(" GPIO output", .Errr)
248  VAR r = .BallGpio(Ball) ' resulting GPIO (index and bit number)
249  Indx = r SHR 5 : IF 2 <> Conf(Indx)->ClVa THEN .Errr = E0 : RETURN .Errr ' GPIO subsystem not enabled
250  Mask = 1 SHL (r AND 31)
251  SELECT CASE AS CONST Mo
252  CASE 0 : Mode = PRUIO_GPIO_OUT0 : r = .BallConf[Ball] XOR Mode
253  CASE 1 : Mode = PRUIO_GPIO_OUT1 : r = .BallConf[Ball] XOR Mode
254  CASE 255 : Mode = .BallInit[Ball] : r = Mo ' reset
255  CASE ELSE : IF &b111 <> (Mo AND &b111) THEN .Errr = E1 : RETURN .Errr ' no Gpio mode
256  Mode = Mo : r = .BallConf[Ball] XOR Mo
257  END SELECT
258  IF r AND &b10100000 THEN setGpioSs() ' i/o or state changed -> configure GPIO subsystem
259  IF r AND &b01011000 THEN _ ' receiver or resistor changed -> pinmux
260  IF .setPin(Top, Ball, Mode) THEN _ ' error -> re-conf and report error
261  Mode = .BallConf[Ball] : setGpioSs() : RETURN .Errr
262  /' all OK, set new mode '/ .BallConf[Ball] = Mode : RETURN 0
263  END WITH
264 END FUNCTION
265 
266 
267 
293 FUNCTION GpioUdt.Value CDECL(BYVAL Ball AS UInt8) AS Int32
294  WITH *Top
295  BallCheck(" GPIO input", -1)
296  VAR r = .BallGpio(Ball) _ ' resulting GPIO (index and bit number)
297  , i = r SHR 5 _ ' index of GPIO
298  , n = r AND 31 ' number of bit
299  IF 2 <> Conf(i)->ClVa THEN .Errr = E0 : RETURN -1 ' GPIO subsystem not enabled
300  IF &b111 <> (.BallConf[Ball] AND &b111) THEN .Errr = E2 : RETURN -1 ' no GPIO pin
301  RETURN IIF(BIT(Raw(i)->Mix, n), 1, 0)
302  END WITH
303 END FUNCTION
304