libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruio_adc.bas
Go to the documentation of this file.
1 
9 
10 
11 ' PruIo global declarations.
12 #INCLUDE ONCE "pruio_globals.bi"
13 
14 ' Header for ADC part.
15 #INCLUDE ONCE "pruio_adc.bi"
16 ' Header for GPIO part.
17 #INCLUDE ONCE "pruio_gpio.bi"
18 ' driver header file
19 #INCLUDE ONCE "pruio.bi"
20 
21 
31 CONSTRUCTOR AdcUdt(BYVAL T AS Pruio_ PTR)
32  Top = T
33  WITH *Top
34  VAR i = .ParOffs
35  InitParA = i
36  i += 1 : .DRam[i] = &h44E0D000uL
37  i += 1 : .DRam[i] = IIF(.DevAct AND PRUIO_ACT_ADC, &h44E004BCuL, 0)
38  .ParOffs = i
39  END WITH
40 END CONSTRUCTOR
41 
42 
43 
56 FUNCTION AdcUdt.initialize CDECL( _
57  BYVAL Av AS UInt8 = PRUIO_DEF_AVRAGE _
58  , BYVAL OpD AS UInt32 = PRUIO_DEF_ODELAY _
59  , BYVAL SaD AS UInt8 = PRUIO_DEF_SDELAY) AS ZSTRING PTR
60 
61  WITH *Top
62  VAR p = .MOffs + .DRam[InitParA]
63  Init = p
64  Conf = p + .DSize
65  END WITH
66 
67  WITH *Conf
68  IF .ClAd = 0 ORELSE _
69  .REVISION = 0 THEN _ ' subsystem disabled
70  .DeAd = 0 : .ClVa = &h30000 : _
71  Init->DeAd = 0 : Init->ClAd = 0 : RETURN 0
72  .ClVa = 2
73  .SYSCONFIG = 0
74  .CTRL = &b11
75 
76  VAR a = ABS((Av > 1) + (Av > 2) + (Av > 4) + (Av > 8)) SHL 2 _
77  , d = OpD AND &h3FFFF
78  FOR i AS LONG = 1 TO 8 ' set default ADC steps
79  WITH .St_p(i)
80  .Confg = a + ((i - 1) SHL 19)
81  .Delay = d + SaD SHL 24
82  END WITH
83  NEXT
84  END WITH : RETURN 0
85 END FUNCTION
86 
87 
88 
101 FUNCTION AdcUdt.configure CDECL( _
102  BYVAL Samp AS UInt32 = PRUIO_DEF_SAMPLS _
103  , BYVAL Mask AS UInt32 = PRUIO_DEF_STPMSK _
104  , BYVAL Tmr AS UInt32 = PRUIO_DEF_TIMERV _
105  , BYVAL Mds AS UInt16 = PRUIO_DEF_LSLMOD) AS ZSTRING PTR
106 
107  WITH *Top
108  Value = 0 'set invalid in case of error
109 
110  IF 2 <> Conf->ClVa THEN ' subsystem not enabled
111  IF Samp < 2 ANDALSO _
112  Mask = 0 THEN .DRam[2] = Samp _
113  ELSE .DRam[2] = 0 : .Errr = E5 ' ADC not enabled
114  .DRam[3] = 0
115  .DRam[4] = 0
116  .DRam[5] = 0 : RETURN .Errr
117  END IF
118 
119  ChAz = 0
120  VAR c = UBOUND(Conf->St_p) _
121  , r = 0 _ ' first active step
122  , d = 0uL ' duration of all steps
123  FOR i AS LONG = c - 1 TO 0 STEP -1
124  IF 0 = BIT(Mask, i) THEN CONTINUE FOR
125  r = i ' find right-most bit
126  ChAz += 1 ' count steps
127  WITH Conf->St_p(r) ' calculate clock cycles (delay)
128  VAR opd = .Delay AND &h3FFFF _
129  , smd = .Delay SHR 24 _
130  , avr = (.Confg SHR 2) AND &b111
131  d += opd + 1 + (14 + smd) * (1 SHL avr)
132  END WITH
133  NEXT
134 
135  IF Samp < 2 THEN ' IO mode
136  Samples = 1
137  TimerVal = 0
138  Value = CAST(ANY PTR, .DRam) + PRUIO_DAT_ADC + 4
139  ELSE
140  IF r < 1 THEN .Errr = @"no step active" : RETURN .Errr
141  Samples = Samp * ChAz
142  IF (Samples SHL 1) > .ESize THEN _
143  .Errr = @"out of memory" : RETURN .Errr
144  d = (d * (Conf->ADC_CLKDIV + 1) * 1000) \ 24
145  IF Tmr <= d ORELSE Tmr < 5000 THEN _
146  .Errr = @"sample rate too big" : RETURN .Errr
147  Value = .ERam
148  TimerVal = Tmr
149  END IF
150  WITH *Conf
151  IF BIT(Mask, 31) THEN _ ' adapt idle step config
152  .IDLECONFIG = .St_p(r).Confg AND &b1111111111111111111100000
153 
154  Mask AND= (1 SHL c) - 1
155  .STEPENABLE = Mask
156  END WITH
157  LslMode = IIF(Mds < 4, Mds, CAST(UInt16, 4))
158 
159  .DRam[5] = TimerVal
160  .DRam[4] = LslMode
161  .DRam[3] = Mask
162  .DRam[2] = Samples
163  END WITH : RETURN 0
164 END FUNCTION
165 
166 
167 
223 FUNCTION AdcUdt.setStep CDECL( _
224  BYVAL Stp AS UInt8 _
225  , BYVAL ChN AS UInt8 _
226  , BYVAL Av AS UInt8 = PRUIO_DEF_AVRAGE _
227  , BYVAL SaD AS UInt8 = PRUIO_DEF_SDELAY _
228  , BYVAL OpD AS UInt32 = PRUIO_DEF_ODELAY) AS ZSTRING PTR
229 
230  WITH *Top
231  IF 2 <> Conf->ClVa THEN .Errr = E5 : RETURN .Errr ' ADC not enabled
232  IF Stp > UBOUND(Conf->St_p) THEN .Errr = E0 : RETURN .Errr ' step number too big
233  IF ChN > 7 THEN .Errr = E1 : RETURN .Errr ' channel number too big
234  END WITH
235 
236  WITH *Conf.St_p(Stp)
237  VAR a = ABS((Av > 1) + (Av > 2) + (Av > 4) + (Av > 8))
238  .Confg = (a SHL 2) + (ChN SHL 19)
239  .Delay = (OpD AND &h3FFFF) + (SaD SHL 24)
240  END WITH : RETURN 0
241 END FUNCTION
242 
243 
244 
278 FUNCTION AdcUdt.mm_trg_pin CDECL( _
279  BYVAL Ball AS UInt8 _
280  , BYVAL GpioV AS UInt8 = 0 _
281  , BYVAL Skip AS UInt16 = 0) AS UInt32
282 
283  WITH *Top
284  IF 2 <> Conf->ClVa THEN .Errr = E5 : RETURN 0 ' ADC not enabled
285  IF Skip > 1023 THEN .Errr = E2 : RETURN 0 'too much values 2 skip
286  BallCheck(" trigger", 0)
287  'VAR g = .BallGpio(Ball) _ ' resulting GPIO (index and bit number)
288  ', i = g SHR 5 ' index of GPIO
289 
290  'IF 2 <> .Gpio->Conf(i)->ClVa THEN _
291  IF 2 <> .Gpio->Conf(.BallGpio(Ball) SHR 5)->ClVa THEN _
292  .Errr = @"GPIO subsystem not enabled" : RETURN 0
293  IF 7 <> (.BallConf[Ball] AND &b111) THEN _
294  .Errr = @"pin must be in GPIO mode (mode 7)" : RETURN 0
295  END WITH
296 
297  DIM AS UInt32 r = (Skip SHL 22) _ ' number of samples to skip
298  + (1 SHL 21) _ ' GPIO bit
299  + (Ball SHL 8) _ ' Ball number
300  + IIF(GpioV = 0, 1 SHL 7, 0) ' negative bit
301  RETURN r
302 END FUNCTION
303 
304 
305 
367 FUNCTION AdcUdt.mm_trg_ain CDECL( _
368  BYVAL Stp AS UInt8 _
369  , BYVAL AdcV AS Int32 _
370  , BYVAL Rela AS UInt8 = 0 _
371  , BYVAL Skip AS UInt16 = 0) AS UInt32
372 
373  WITH *Top
374  IF 2 <> Conf->ClVa THEN .Errr = E5 : RETURN 0 ' ADC not enabled
375  IF Stp > 16 THEN .Errr = E4 : RETURN 0 ' invalid step number
376  IF Stp ANDALSO 0 = Conf->St_p(Stp).Confg THEN .Errr = E3 : RETURN 0 ' trigger step not configured
377  IF Skip > 1023 THEN .Errr = E2 : RETURN 0 ' too much values to skip
378  END WITH
379 
380  VAR v = ABS(AdcV) SHR LslMode
381  IF v < &hF THEN v = &hF ELSE IF v > &hFF0 THEN v = &hFF0
382 
383  DIM AS UInt32 r = (Skip SHL 22) _ ' number of samples to skip
384  + (v SHL 8) _ ' sample AdcV to check
385  + IIF(AdcV < 0, 1 SHL 7, 0) ' negative bit
386  IF 0 = Stp THEN r += (1 SHL 5) _ ' all step bit,
387  ELSE r += Stp - 1 _ ' or step number
388  + IIF(Rela, 1 SHL 6, 0) ' and relative bit
389  RETURN r
390 END FUNCTION
391 
392 
393 
454 FUNCTION AdcUdt.mm_trg_pre CDECL( _
455  BYVAL Stp AS UInt8 _
456  , BYVAL AdcV AS Int32 _
457  , BYVAL Samp AS UInt16 = 0 _
458  , BYVAL Rela AS UInt8 = 0) AS UInt32
459 
460  WITH *Top
461  IF 2 <> Conf->ClVa THEN .Errr = E5 : RETURN 0 ' ADC not enabled
462  IF Stp > 16 THEN .Errr = E4 : RETURN 0 ' invalid step number
463  IF Stp ANDALSO 0 = Conf->St_p(Stp).Confg THEN .Errr = E3 : RETURN 0 ' trigger step not configured
464  IF Stp ANDALSO 0 = (Conf->STEPENABLE AND (1 SHL Stp)) THEN _
465  .Errr = @"trigger step not activated" : RETURN 0
466  VAR t = (Samp + 1) * ChAz
467  IF t > ((16384 - PRUIO_DAT_ADC - 32) SHR 1) THEN _
468  .Errr = @"too much pre-samples" : RETURN 0
469  IF t > Samples THEN _
470  .Errr = @"more pre-samples than samples" : RETURN 0
471  END WITH
472 
473  VAR v = ABS(AdcV) SHR LslMode
474  IF v < &hF THEN v = &hF ELSE IF v > &hFF0 THEN v = &hFF0
475 
476  DIM AS UInt32 r = (Samp SHL 22) _ ' number of pre-samples
477  + (v SHL 8) _ ' sample AdcV to check
478  + IIF(AdcV < 0, 1 SHL 7, 0) _ ' negative bit
479  + (1 SHL 4) ' pre-trigger bit
480  IF 0 = Stp THEN r += (1 SHL 5) _ ' all step bit,
481  ELSE r += Stp - 1 _ ' or step number
482  + IIF(Rela, 1 SHL 6, 0) ' and relative bit
483  RETURN r
484 END FUNCTION
485