libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruio.bas
Go to the documentation of this file.
1 
10 
11 
12 
13 #DEFINE __PRUIO_COMPILING__
14 
15 ' uio driver header file
16 #INCLUDE ONCE "pruio_prussdrv.bi"
17 ' driver header file
18 #INCLUDE ONCE "pruio.bi"
19 ' Header file with convenience macros BBB2x46.
20 #INCLUDE ONCE "pruio_pins.bi"
21 ' Header file with convenience macros PBB2x36.
22 #INCLUDE ONCE "pruio_pins_pocket.bi"
23 ' Header file with convenience macros BB_Blue.
24 #INCLUDE ONCE "pruio_pins_blue.bi"
25 ' Header file with convenience macros SD card slot.
26 #INCLUDE ONCE "pruio_pins_sd.bi"
27 ' Header file with Pru_Init instructions.
28 #INCLUDE ONCE "pasm_init.bi"
29 ' Header file with Pru_Run instructions.
30 #INCLUDE ONCE "pasm_run.bi"
31 ' FB include
32 #INCLUDE ONCE "dir.bi"
33 
34 
35 #DEFINE PMUX_NAME "pruio-"
36 
37 #DEFINE PMUX_ERRR 258
38 
39 #DEFINE ArrayBytes(_A_) (UBOUND(_A_) + 1) * SIZEOF(_A_(0))
40 
41 
42 DECLARE FUNCTION memcpy CDECL ALIAS "memcpy"(BYVAL AS ANY PTR, BYVAL AS ANY PTR, BYVAL AS ULONG /'size_t'/) AS ANY PTR
43 
44 ' private functions from pruio_prussdrv.bas
45 DECLARE FUNCTION setPin_save CDECL( _
46  BYVAL AS Pruio_ PTR _
47  , BYVAL AS UInt8 _
48  , BYVAL AS UInt8) AS ZSTRING PTR
49 DECLARE FUNCTION setPin_lkm_bb CDECL( _
50  BYVAL AS Pruio_ PTR _
51  , BYVAL AS UInt8 _
52  , BYVAL AS UInt8) AS ZSTRING PTR
53 DECLARE FUNCTION setPin_lkm CDECL( _
54  BYVAL AS Pruio_ PTR _
55  , BYVAL AS UInt8 _
56  , BYVAL AS UInt8) AS ZSTRING PTR
57 DECLARE FUNCTION setPin_dtbo CDECL( _
58  BYVAL AS Pruio_ PTR _
59  , BYVAL AS UInt8 _
60  , BYVAL AS UInt8) AS ZSTRING PTR
61 DECLARE FUNCTION setPin_nogo CDECL( _
62  BYVAL AS Pruio_ PTR _
63  , BYVAL AS UInt8 _
64  , BYVAL AS UInt8) AS ZSTRING PTR
65 
66 
146 CONSTRUCTOR PruIo( _
147  BYVAL Act AS UInt16 = PRUIO_DEF_ACTIVE _
148  , BYVAL Av AS UInt8 = PRUIO_DEF_AVRAGE _
149  , BYVAL OpD AS UInt32 = PRUIO_DEF_ODELAY _
150  , BYVAL SaD AS UInt8 = PRUIO_DEF_SDELAY)
151  VAR fnr = FREEFILE ' test interrupt access
152  IF OPEN("/dev/uio5" FOR OUTPUT AS fnr) THEN _
153  Errr = @"cannot open /dev/uio5" : EXIT CONSTRUCTOR
154  CLOSE #fnr
155 
156  STATIC AS STRING mux, bbb ' check for BB type and pinmuxing features
157  IF 0 = OPEN("/proc/device-tree/model" FOR INPUT AS fnr) THEN
158  LINE INPUT #fnr, bbb
159  'IF bbb = "TI_AM335x_PocketBeagle" THEN BbType = PBB2x36
160  'IF bbb = "TI AM335x BeagleBone Blue" THEN BbType = BB_Blue
161  IF INSTR(bbb, "Pocket") THEN BbType = PBB2x36
162  IF INSTR(bbb, "Blue") THEN BbType = BB_Blue
163  CLOSE #fnr
164  END IF
165 
166  IF 0 = OPEN("/sys/devices/platform/libpruio/state" FOR OUTPUT AS fnr) THEN
167  IF Act AND PRUIO_ACT_FREMUX THEN
168  setPin = IIF(BbType, @setPin_lkm(), @setPin_lkm_bb())
169  ELSE
170  setPin = @setPin_save()
171  Errr = setPin(@THIS, 255, 0)
172  IF Errr THEN CLOSE #fnr : setPin = @setPin_nogo() : fnr = 0
173  END IF : MuxFnr = fnr
174  END IF
175  IF 0 = MuxFnr THEN ' no LKM, test old style device tree overlays
176  VAR p = "/sys/devices/" _
177  , n = DIR(p & "ocp.*", fbDirectory)
178  IF LEN(n) THEN ' old kernel 3.x
179  mux = p & n & "/" & PMUX_NAME : MuxFnr = 256
180  ELSE ' new kernel 4.x
181  p &= "platform/ocp/ocp:"
182  IF LEN(DIR(p & "pruio-*", fbDirectory)) THEN mux = p & PMUX_NAME : MuxFnr = 257
183  END IF
184  IF MuxFnr THEN setPin = @setPin_dtbo() : MuxAcc = SADD(mux) : Errr = 0 _
185  ELSE setPin = @setPin_nogo()
186  END IF
187 
188  IF Act AND PRUIO_ACT_PRU1 THEN
189  PruIntNo = ARM_PRU1_INTERRUPT
190  PruIRam = PRUSS0_PRU1_IRAM
191  PruDRam = PRUSS0_PRU1_DRAM
192  PruNo = PRU1
193  ELSE
194  PruIntNo = ARM_PRU1_INTERRUPT
195  PruIRam = PRUSS0_PRU0_IRAM
196  PruDRam = PRUSS0_PRU0_DRAM
197  PruNo = PRU0
198  END IF
199  IF prussdrv_open(PRUIO_EVNT) THEN _ ' open PRU Interrupt
200  Errr = @"failed opening PRUIO_EVNT" : EXIT CONSTRUCTOR
201 
202  prussdrv_map_prumem(PruDRam, CAST(ANY PTR, @DRam))
203  prussdrv_map_extmem(@ERam)
204  ESize = prussdrv_extmem_sIze()
205  EAddr = prussdrv_get_phys_addr(ERam)
206 
207  prussdrv_pru_disable(PruNo) ' disable PRU (if running before)
208  DRam[0] = 0
209  DRam[1] = PRUIO_DAT_ALL ' start of data block
210  ParOffs = 1
211  DevAct = Act
212 
213 ' order must match the order in
214 ' pruio_init.p xxx_Init and in
215 ' pruio_run.p xxx_Config macro calls
216 '& AdcUdt::AdcUdt(); GpioUdt::GpioUdt(); PwmssUdt::PwmssUdt(); TimerUdt::TimerUdt(); /*
217  Adc = NEW AdcUdt(@THIS)
218  Gpio = NEW GpioUdt(@THIS)
219 
220  VAR offs = ParOffs
221  ParOffs += 1 : DRam[ParOffs] = &h44E10800 ' pinmux registers
222 
223  PwmSS = NEW PwmssUdt(@THIS)
224  TimSS = NEW TimerUdt(@THIS)
225 
226  ASSERT(ParOffs < DRam[1] SHR 4)
227  VAR l = ArrayBytes(Pru_Init)
228  IF 0 >= prussdrv_pru_write_memory(PruIRam, 0, @Pru_Init(0), l) THEN _
229  Errr = @"failed loading Pru_Init instructions" : EXIT CONSTRUCTOR
230  prussdrv_pruintc_init(IntcInit) ' get interrupts initialized
231  prussdrv_pru_enable(PruNo)
232 
233  Pwm = NEW PwmMod(@THIS)
234  Cap = NEW CapMod(@THIS)
235  Qep = NEW QepMod(@THIS)
236 '& */ PwmMod::PwmMod(); CapMod::CapMod(); QepMod::QepMod();
237  Tim = TimSS ' redundant, but consistent API for the user
238 
239  prussdrv_pru_wait_event(PRUIO_EVNT)
240  IF DRam[0] <> PRUIO_MSG_INIT_OK THEN _
241  Errr = @"failed executing Pru_Init instructions" : EXIT CONSTRUCTOR
242 
243  DSize = DRam[ParOffs] - DRam[1]
244  DInit = ALLOCATE(DSize * 2)
245  IF 0 = DInit THEN Errr = @"out of memory" : EXIT CONSTRUCTOR
246 
247  MOffs = DInit - DRam[1]
248  DConf = DInit + DSize
249  VAR p0 = CAST(ANY PTR, DRam) + DRam[1]
250  memcpy(DInit, p0, DSize)
251  memcpy(DConf, p0, DSize)
252 
253  Init = MOffs + DRam[offs]
254  Conf = CAST(ANY PTR, Init) + DSize
255  BallInit = CAST(ANY PTR, Init) + OFFSETOF(BallSet, Value(0))
256  BallConf = BallInit + DSize ' initialize invalid:
257  FOR b AS INTEGER = 0 TO PRUIO_AZ_BALL : BallConf[b] OR= &b11000 : NEXT
258 
259  IF Adc->initialize(Av, OpD, SaD) THEN EXIT CONSTRUCTOR
260  IF Gpio->initialize() THEN EXIT CONSTRUCTOR
261  IF PwmSS->initialize() THEN EXIT CONSTRUCTOR
262  IF TimSS->initialize() THEN EXIT CONSTRUCTOR
263  '& AdcUdt::initialize(); GpioUdt::initialize(); PwmssUdt::initialize(); TimerUdt::initialize();
264 END CONSTRUCTOR
265 
266 
267 
283 DESTRUCTOR PruIo()
284  VAR mux = "" : Errr = 0
285  IF DRam THEN
286  IF DInit THEN
287  IF MuxFnr THEN ' pinmuxing active
288  IF BallInit <> BallConf THEN ' reset pinmuxing
289  FOR i AS INTEGER = 0 TO PRUIO_AZ_BALL
290  IF (BallConf[i] AND &b11000) = &b11000 THEN CONTINUE FOR ' untouched
291  IF (BallConf[i] XOR BallInit[i]) AND &b1111111 THEN _ ' re-mux
292  IF setPin(@THIS, i, BallInit[i]) THEN mux &= _ ' error
293  !"\nre-mux " _
294  & BIN(BallConf[i], 7) & " -> " & BIN(BallInit[i], 7) _
295  & " failed (" & *Errr & ")"
296  NEXT
297  END IF : Errr = 0
298  IF MuxFnr < 256 THEN CLOSE #MuxFnr ' close MuxFile
299  END IF
300  prussdrv_pru_disable(PruNo)
301 
302  DRam[2] = 0
303  DRam[1] = PRUIO_DAT_ALL ' reset subsystems configuration
304  memcpy(CAST(ANY PTR, DRam) + DRam[1], DInit, DSize)
305  DEALLOCATE(DInit)
306 
307  VAR l = ArrayBytes(Pru_Run)
308  IF 0 >= prussdrv_pru_write_memory(PruIRam, 0, @Pru_Run(0), l) THEN
309  Errr = @"failed loading Pru_Exit instructions"
310  ELSE
311  prussdrv_pruintc_init(IntcInit) ' get interrupt initialized
312  prussdrv_pru_enable(PruNo)
313  prussdrv_pru_wait_event(PRUIO_EVNT)
314  IF DRam[0] <> PRUIO_MSG_CONF_OK THEN _
315  Errr = @"failed executing Pru_Exit instructions"
316  END IF
317  ELSE
318  Errr = @"destructor warning: subsystems are NOT restored"
319  END IF
320  prussdrv_pru_disable(PruNo)
321  prussdrv_exit() ' power down PRU
322  ELSE
323  Errr = @"destructor warning: constructor failed"
324  END IF
325 
326  IF Errr THEN mux &= !"\n" & *Errr
327  IF LEN(mux) THEN _
328  VAR fnr = FREEFILE : OPEN ERR AS fnr : PRINT #fnr, MID(mux, 2) : CLOSE #fnr
329 END DESTRUCTOR
330 
331 
332 
451 FUNCTION PruIo.config CDECL( _
452  BYVAL Samp AS UInt32 = PRUIO_DEF_SAMPLS _
453  , BYVAL Mask AS UInt32 = PRUIO_DEF_STPMSK _
454  , BYVAL Tmr AS UInt32 = PRUIO_DEF_TIMERV _
455  , BYVAL Mds AS UInt16 = PRUIO_DEF_LSLMOD) AS ZSTRING PTR
456 
457  prussdrv_pru_disable(PruNo) ' disable PRU
458  IF Adc->configure(Samp, Mask, Tmr, Mds) THEN RETURN Errr
459 
460  DRam[1] = PRUIO_DAT_ALL
461  memcpy(CAST(ANY PTR, DRam) + DRam[1], DConf, DSize)
462 
463  VAR l = ArrayBytes(Pru_Run)
464  IF 0 >= prussdrv_pru_write_memory(PruIRam, 0, @Pru_Run(0), l) THEN _
465  Errr = @"failed loading Pru_Run instructions" : RETURN Errr
466  prussdrv_pruintc_init(IntcInit) ' get interrupt initialized
467  prussdrv_pru_enable(PruNo)
468  prussdrv_pru_wait_event(PRUIO_EVNT)
469  SELECT CASE AS CONST Samp
470  CASE 0 : l = DRam[0] <> PRUIO_MSG_CONF_OK
471  CASE 1 : l = DRam[0] <> PRUIO_MSG_IO_OK
472  CASE ELSE : l = DRam[0] <> PRUIO_MSG_MM_WAIT
473  END SELECT
474  IF l THEN Errr = @"failed executing Pru_Run instructions" : RETURN Errr
475  IF Samp < 2 THEN RETURN 0
476 
477  prussdrv_pru_clear_event(PRUIO_EVNT, PRUIO_IRPT)
478  prussdrv_pru_send_event(PruIntNo) ' prepare fast MM start
479  RETURN 0
480 END FUNCTION
481 
482 
483 
505 FUNCTION PruIo.Pin CDECL( _
506  BYVAL Ball AS UInt8, _
507  BYVAL Mo AS UInt8 = 1) AS ZSTRING PTR
508  STATIC AS STRING*50 t
509 
510  VAR x = nameBall(Ball)
511  IF x THEN
512  t = *x
513  ELSE
514  IF Ball > PRUIO_AZ_BALL THEN Errr = @"unknown pin number" : RETURN Errr
515  t = "b " & RIGHT("00" & Ball, 3)
516  END IF
517 
518  VAR r = IIF(Mo, BallConf[Ball], BallInit[Ball]) _
519  , m = r AND &b111
520  IF m = 7 THEN
521  VAR i = BallGpio(Ball) SHR 5 _
522  , n = BallGpio(Ball) AND 31 _
523  , m = 1 SHL n
524  t &= ", GPIO " & i & "/" & RIGHT("0" & n, 2)
525  IF BIT(r, 5) THEN
526  t &= ": input"
527  ELSE
528  WITH *IIF(Mo, Gpio->Conf(i), Gpio->Init(i))
529  t &= ": output-" & *IIF((.DATAOUT OR .SETDATAOUT) AND m, @"1", @"0")
530  END WITH
531  END IF
532  ELSE
533  t &= ", mode " & m _
534  & ": input " & *IIF(BIT(r, 5), @"enabled", @"disabled")
535  END IF
536 
537  IF BIT(r, 3) THEN
538  t &= ", nopull"
539  ELSE
540  IF BIT(r, 4) THEN t &= ", pullup" ELSE t &= ", pulldown"
541  END IF : RETURN SADD(t)
542 END FUNCTION
543 
544 
545 
553 #MACRO BB_NAMES()
554  CASE P8_03 : RETURN @"P8_03"
555  CASE P8_04 : RETURN @"P8_04"
556  CASE P8_05 : RETURN @"P8_05"
557  CASE P8_06 : RETURN @"P8_06"
558  CASE P8_07 : RETURN @"P8_07"
559  CASE P8_08 : RETURN @"P8_08"
560  CASE P8_09 : RETURN @"P8_09"
561  CASE P8_10 : RETURN @"P8_10"
562  CASE P8_11 : RETURN @"P8_11"
563  CASE P8_12 : RETURN @"P8_12"
564  CASE P8_13 : RETURN @"P8_13"
565  CASE P8_14 : RETURN @"P8_14"
566  CASE P8_15 : RETURN @"P8_15"
567  CASE P8_16 : RETURN @"P8_16"
568  CASE P8_17 : RETURN @"P8_17"
569  CASE P8_18 : RETURN @"P8_18"
570  CASE P8_19 : RETURN @"P8_19"
571  CASE P8_20 : RETURN @"P8_20"
572  CASE P8_21 : RETURN @"P8_21"
573  CASE P8_22 : RETURN @"P8_22"
574  CASE P8_23 : RETURN @"P8_23"
575  CASE P8_24 : RETURN @"P8_24"
576  CASE P8_25 : RETURN @"P8_25"
577  CASE P8_26 : RETURN @"P8_26"
578  CASE P8_27 : RETURN @"P8_27"
579  CASE P8_28 : RETURN @"P8_28"
580  CASE P8_29 : RETURN @"P8_29"
581  CASE P8_30 : RETURN @"P8_30"
582  CASE P8_31 : RETURN @"P8_31"
583  CASE P8_32 : RETURN @"P8_32"
584  CASE P8_33 : RETURN @"P8_33"
585  CASE P8_34 : RETURN @"P8_34"
586  CASE P8_35 : RETURN @"P8_35"
587  CASE P8_36 : RETURN @"P8_36"
588  CASE P8_37 : RETURN @"P8_37"
589  CASE P8_38 : RETURN @"P8_38"
590  CASE P8_39 : RETURN @"P8_39"
591  CASE P8_40 : RETURN @"P8_40"
592  CASE P8_41 : RETURN @"P8_41"
593  CASE P8_42 : RETURN @"P8_42"
594  CASE P8_43 : RETURN @"P8_43"
595  CASE P8_44 : RETURN @"P8_44"
596  CASE P8_45 : RETURN @"P8_45"
597  CASE P8_46 : RETURN @"P8_46"
598  CASE P9_11 : RETURN @"P9_11"
599  CASE P9_12 : RETURN @"P9_12"
600  CASE P9_13 : RETURN @"P9_13"
601  CASE P9_14 : RETURN @"P9_14"
602  CASE P9_15 : RETURN @"P9_15"
603  CASE P9_16 : RETURN @"P9_16"
604  CASE P9_17 : RETURN @"P9_17"
605  CASE P9_18 : RETURN @"P9_18"
606  CASE P9_19 : RETURN @"P9_19"
607  CASE P9_20 : RETURN @"P9_20"
608  CASE P9_21 : RETURN @"P9_21"
609  CASE P9_22 : RETURN @"P9_22"
610  CASE P9_23 : RETURN @"P9_23"
611  CASE P9_24 : RETURN @"P9_24"
612  CASE P9_25 : RETURN @"P9_25"
613  CASE P9_26 : RETURN @"P9_26"
614  CASE P9_27 : RETURN @"P9_27"
615  CASE P9_28 : RETURN @"P9_28"
616  CASE P9_29 : RETURN @"P9_29"
617  CASE P9_30 : RETURN @"P9_30"
618  CASE P9_31 : RETURN @"P9_31"
619  CASE P9_41 : RETURN @"P9_41"
620  CASE 106 : RETURN @"P9_41"
621  CASE P9_42 : RETURN @"P9_42"
622  CASE 104 : RETURN @"P9_42"
623  CASE JT_04 : RETURN @"JT_04"
624  CASE JT_05 : RETURN @"JT_05"
625 #ENDMACRO
626 
627 
628 
635 #MACRO BLUE_NAMES()
636  CASE E1_3 : RETURN @"E1_3"
637  CASE E1_4 : RETURN @"E1_4"
638  CASE E2_3 : RETURN @"E2_3"
639  CASE E2_4 : RETURN @"E2_4"
640  CASE E3_3 : RETURN @"E3_3"
641  CASE E3_4 : RETURN @"E3_4"
642  CASE E4_3 : RETURN @"E4_3"
643  CASE E4_4 : RETURN @"E4_4"
644  CASE UT0_3 : RETURN @"UT0_3"
645  CASE UT0_4 : RETURN @"UT0_4"
646  CASE UT1_3 : RETURN @"UT1_3"
647  CASE UT1_4 : RETURN @"UT1_4"
648  CASE UT5_3 : RETURN @"UT5_3"
649  CASE UT5_4 : RETURN @"UT5_4"
650  CASE DSM2_3 : RETURN @"DSM2_3"
651  CASE GP0_3 : RETURN @"GP0_3"
652  CASE GP0_4 : RETURN @"GP0_4"
653  CASE GP0_5 : RETURN @"GP0_5"
654  CASE GP0_6 : RETURN @"GP0_6"
655  CASE GP1_3 : RETURN @"GP1_3"
656  CASE GP1_4 : RETURN @"GP1_4"
657  CASE GP1_5 : RETURN @"GP1_5"
658  CASE GP1_6 : RETURN @"GP1_6"
659  CASE GPS_3 : RETURN @"GPS_3"
660  CASE GPS_4 : RETURN @"GPS_4"
661  CASE SPI1_3 : RETURN @"SPI1_3"
662  CASE SPI1_4 : RETURN @"SPI1_4"
663  CASE SPI1_5 : RETURN @"SPI1_5"
664  CASE SPI1_6 : RETURN @"SPI1_6"
665  CASE SPI2_6 : RETURN @"SPI2_6"
666 #ENDMACRO
667 
668 
669 
676 #MACRO POCKET_NAMES()
677  CASE P1_02 : RETURN @"P1_02"
678  CASE P1_04 : RETURN @"P1_04"
679  CASE P1_06 : RETURN @"P1_06"
680  CASE P1_08 : RETURN @"P1_08"
681  CASE P1_10 : RETURN @"P1_10"
682  CASE P1_12 : RETURN @"P1_12"
683  CASE P1_20 : RETURN @"P1_20"
684  CASE P1_26 : RETURN @"P1_26"
685  CASE P1_28 : RETURN @"P1_28"
686  CASE P1_29 : RETURN @"P1_29"
687  CASE P1_30 : RETURN @"P1_30"
688  CASE P1_31 : RETURN @"P1_31"
689  CASE P1_32 : RETURN @"P1_32"
690  CASE P1_33 : RETURN @"P1_33"
691  CASE P1_34 : RETURN @"P1_34"
692  CASE P1_35 : RETURN @"P1_35"
693  CASE P1_36 : RETURN @"P1_36"
694  CASE P2_01 : RETURN @"P2_01"
695  CASE P2_02 : RETURN @"P2_02"
696  CASE P2_03 : RETURN @"P2_03"
697  CASE P2_04 : RETURN @"P2_04"
698  CASE P2_05 : RETURN @"P2_05"
699  CASE P2_06 : RETURN @"P2_06"
700  CASE P2_07 : RETURN @"P2_07"
701  CASE P2_08 : RETURN @"P2_08"
702  CASE P2_09 : RETURN @"P2_09"
703  CASE P2_10 : RETURN @"P2_10"
704  CASE P2_11 : RETURN @"P2_11"
705  CASE P2_17 : RETURN @"P2_17"
706  CASE P2_18 : RETURN @"P2_18"
707  CASE P2_19 : RETURN @"P2_19"
708  CASE P2_20 : RETURN @"P2_20"
709  CASE P2_22 : RETURN @"P2_22"
710  CASE P2_24 : RETURN @"P2_24"
711  CASE P2_25 : RETURN @"P2_25"
712  CASE P2_27 : RETURN @"P2_27"
713  CASE P2_28 : RETURN @"P2_28"
714  CASE P2_29 : RETURN @"P2_29"
715  CASE P2_30 : RETURN @"P2_30"
716  CASE P2_31 : RETURN @"P2_31"
717  CASE P2_32 : RETURN @"P2_32"
718  CASE P2_33 : RETURN @"P2_33"
719  CASE P2_34 : RETURN @"P2_34"
720  CASE P2_35 : RETURN @"P2_35"
721 #ENDMACRO
722 
723 
724 
739 FUNCTION PruIo.nameBall CDECL(BYVAL Ball AS UInt8) AS ZSTRING PTR
740  SELECT CASE AS CONST BbType ' find model
741  CASE PBB2x36
742  SELECT CASE AS CONST Ball ' 2x36 headers (Pocket)
743  POCKET_NAMES()
744  END SELECT
745  CASE BB_Blue
746  SELECT CASE AS CONST Ball ' connectors (Blue)
747  BLUE_NAMES()
748  END SELECT
749  CASE ELSE
750  SELECT CASE AS CONST Ball ' 2x46 headers (Black, Green, White)
751  BB_NAMES()
752  END SELECT
753  END SELECT
754  SELECT CASE AS CONST Ball
755  CASE SD_01 : RETURN @"SD_01"
756  CASE SD_02 : RETURN @"SD_02"
757  CASE SD_03 : RETURN @"SD_03"
758  CASE SD_05 : RETURN @"SD_05"
759  CASE SD_07 : RETURN @"SD_07"
760  CASE SD_08 : RETURN @"SD_08"
761  CASE SD_10 : RETURN @"SD_10"
762  END SELECT : RETURN 0
763 END FUNCTION
764 
765 
766 
785 FUNCTION PruIo.rb_start CDECL() AS ZSTRING PTR
786  IF DRam[0] <> PRUIO_MSG_MM_WAIT THEN _
787  Errr = @"ring buffer mode not ready" : RETURN Errr
788 
789  DRam[1] = Adc->Samples SHL 1 ' size of ring buffer
790  DRam[2] = EAddr
791  DRam[3] = 0
792  DRam[4] = 1 SHL 4
793 
794  prussdrv_pru_clear_event(PRUIO_EVNT, PruIntNo) ' off we go
795  RETURN 0
796 END FUNCTION
797 
798 
799 
831 FUNCTION PruIo.mm_start CDECL( _
832  BYVAL Trg1 AS UInt32 = 0 _
833  , BYVAL Trg2 AS UInt32 = 0 _
834  , BYVAL Trg3 AS UInt32 = 0 _
835  , BYVAL Trg4 AS UInt32 = 0) AS ZSTRING PTR
836 
837  IF DRam[0] <> PRUIO_MSG_MM_WAIT THEN _
838  Errr = @"measurement mode not ready" : RETURN Errr
839 
840  STATIC AS UInt32 tmin = (1 SHL 22), t_pin
841  DRam[3] = 0
842 
843 '#DEFINE PRUIO_PRE_TRIG(_T_) Trg##_T_ >= tmin ANDALSO (Trg##_T_ AND (1 SHL 4)) THEN : _
844 #DEFINE PRUIO_PRE_TRIG(_T_) Trg##_T_ >= tmin ANDALSO (Trg##_T_ AND (&b10000)) THEN : _
845  IF BIT(Trg##_T_, 5) ORELSE BIT(Adc->Conf->STEPENABLE, (Trg##_T_ AND &b1111) + 1) THEN : _
846  VAR n = (Trg##_T_ SHR 22) * Adc->ChAz : _
847  IF n < Adc->Samples THEN : DRam[3] = n SHL 1 : _
848  ELSE : Errr = @"Trg" #_T_ ": too much pre-trigger samples" : RETURN Errr : _
849  END IF : _
850  ELSE : Errr = @"Trg" #_T_ ": pre-trigger step must be active" : RETURN Errr : _
851  END IF
852 
853 #DEFINE PRUIO_GPIO_TRIG(_T_) BIT(Trg##_T_, 21) THEN : _
854  t_pin = (Trg##_T_ SHR 8) AND &b1111111 : _
855  IF t_pin > PRUIO_AZ_BALL THEN _
856  Errr = @"Trg" #_T_ ": unknown trigger pin number" : RETURN Errr : _
857  END IF : _
858  IF &b111 <> (BallConf[t_pin] AND &b111) THEN _
859  Errr = @"Trg" #_T_ ": trigger pin must be in mode 7 (GPIO)" : RETURN Errr : _
860  ELSE Trg##_T_ AND= &b11111111111100000000000011111111uL : _
861  Trg##_T_ OR= BallGpio(t_pin) SHL 8 : _
862  END IF : _
863  END IF
864 
865  IF Trg1 THEN
866  IF PRUIO_PRE_TRIG(1)
867  ELSE
868  IF PRUIO_GPIO_TRIG(1)
869  IF Trg2 THEN
870  IF PRUIO_PRE_TRIG(2)
871  ELSE
872  IF PRUIO_GPIO_TRIG(2)
873  IF Trg3 THEN
874  IF PRUIO_PRE_TRIG(3)
875  ELSE
876  IF PRUIO_GPIO_TRIG(3)
877  IF Trg4 THEN
878  IF PRUIO_PRE_TRIG(4)
879  ELSE
880  IF PRUIO_GPIO_TRIG(4)
881  END IF
882  END IF
883  END IF
884  END IF
885  END IF
886  END IF
887  END IF
888  END IF
889 
890  DRam[1] = ESize
891  DRam[2] = EAddr
892  DRam[4] = Trg1
893  DRam[5] = Trg2
894  DRam[6] = Trg3
895  DRam[7] = Trg4
896 
897  prussdrv_pru_clear_event(PRUIO_EVNT, PruIntNo) ' off we go
898 
899  prussdrv_pru_wait_event(PRUIO_EVNT) ' wait for end of measurement
900  prussdrv_pru_clear_event(PRUIO_EVNT, PRUIO_IRPT) ' clear interrupt
901  prussdrv_pru_send_event(PruIntNo) ' prepare next start
902  RETURN 0
903 END FUNCTION
904