libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruss_toggle.bas
Go to the documentation of this file.
1 
16 
17 #include ONCE "BBB/pruio.bi" ' the library (for pinmuxing)
18 #include ONCE "BBB/pruio_pins.bi" ' CPU balls human readable
19 #include ONCE "BBB/pruio_prussdrv.bi" ' user space part of uio_pruss
20 
21 
22 
53 FUNCTION load_firmware(BYVAL IRam AS UInt32) AS Int32
54  DIM AS CONST UInt32 PRUcode(...) = { _
55  &h240000E0 _
56  , &hF100A081 _
57  , &h240001E0 _
58  , &h0801E0E1 _
59  , &h30820006 _
60  , &h14E1FEFE _
61  , &h240000E0 _
62  , &h240000E0 _
63  , &h240000E0 _
64  , &h240000E0 _
65  , &h1003031F _
66  , &h2A000000 _
67  , &h21000400 }
68  VAR l = (UBOUND(PRUcode) + 1) * SIZEOF(PRUcode)
69  RETURN 0 >= prussdrv_pru_write_memory(IRam, 0, @PRUcode(0), l)
70 END FUNCTION
71 
72 
73 ' our configuration is for PUR-0, so PRU-1 for libpruio
75 DO
76  DIM AS UInt32 _
77  pru_num _
78  , pru_iram _
79  , pru_dram _
80  , pru_intr
81 '
82 ' Check init success
83 '
84  IF io->Errr THEN _
85  ?"initialisation failed (" & *io->Errr & ")" : EXIT DO
86 
87  IF io->PruNo THEN ' we use the other PRU
88  pru_num = 0
89  pru_iram = PRUSS0_PRU0_IRAM
90  pru_dram = PRUSS0_PRU0_DRAM
91  pru_intr = PRU0_ARM_INTERRUPT
92  ELSE
93  pru_num = 1
94  pru_iram = PRUSS0_PRU1_IRAM
95  pru_dram = PRUSS0_PRU1_DRAM
96  pru_intr = PRU0_ARM_INTERRUPT ' libpruio uses PRU1_ARM_INTERRUPT!
97  END IF
98 '
99 ' Now prepare the other PRU
100 '
101  IF prussdrv_open(PRU_EVTOUT_0) THEN _ ' note: PRU_EVTOUT_5 for libpruio
102  ?"prussdrv_open failed" : EXIT DO
103  ' Note: no prussdrv_pruintc_init(), libpruio did it already
104 
105  IF load_firmware(pru_iram) < 0 THEN _
106  ?"failed loading PRUSS instructions" : EXIT DO
107 '
108 ' Pinmuxing (some examples first)
109 '
110  '' set PRU-0-r31 bit 15 input with pull up resistor
111  'IF io->setPin(io, P8_15, 6 OR PRUIO_RX_ACTIV OR PRUIO_PULL_UP) THEN _
112  '?"P8_15 configuration failed (" & *io->Errr & ")" : EXIT DO
113 
114  '' set PRU-0-r31 bit 10 input (mode 6), pull down resistor
115  'IF io->setPin(io, SD_08, 6 OR PRUIO_RX_ACTIV OR PRUIO_PULL_DOWN) THEN _
116  '?"SD_08 configuration failed (" & *io->Errr & ")" : EXIT DO
117 
118  '' set PRU-0-r30 bit 10 output (mode 5)
119  'IF io->setPin(io, SD_08, 5 OR PRUIO_NO_PULL) THEN _
120  '?"SD_08 configuration failed (" & *io->Errr & ")" : EXIT DO
121 
122  ' set PRU-0-r30 bit 15 output (mode 6), pull down resistor
123  IF io->setPin(io, P8_11, 6) THEN _
124  ?"P8_11 configuration failed (" & *io->Errr & ")" : EXIT DO
125 
126  /' ... '/
127 '
128 ' Prepare libpruio measurement
129 '
130  IF io->Cap->config(P9_42, 2.) THEN _ ' configure CAP input pin
131  ?"failed setting input P9_42 (" & *io->Errr & ")" : EXIT DO
132  IF io->config(1, 0, 0, 0) THEN _
133  ?"config failed (" & *io->Errr & ")" : EXIT DO
134  DIM AS float_t _
135  f _
136  , d
137 '
138 ' Pass parameters to PRU
139 '
140  DIM AS UInt32 PTR dram
141  prussdrv_map_prumem(pru_dram, CAST(ANY PTR, @dram)) ' get dram pointer
142  dram[0] = 15 ' bit number, must match configured pin (P8_11)
143  dram[1] = 16 ' loop count (max 16 bit = 65535)
144  dram[2] = pru_intr + 16 ' the interrupt we're waiting for
145 '
146 ' Execute 20 times
147 '
148  ?"instructions loaded, starting PRU-" & pru_num
149  FOR i AS UInt32 = 0 TO 20
150  prussdrv_pru_enable(pru_num) ' start
151 
152  prussdrv_pru_wait_event(PRU_EVTOUT_0) ' wait until PRU finished
153 
154  IF io->Cap->Value(P9_42, @f, @d) THEN _ ' get last measurement
155  ?"failed reading input P9_42 (" & *io->Errr & ")" : EXIT DO
156 
157  ?"--> Frequency: " & (f * .000001) & " MHz, Duty:" & (d * 100) & " %" ' results
158 
159  prussdrv_pru_clear_event(PRU_EVTOUT_0, pru_intr) ' clr interrupt
160  NEXT
161 
162  prussdrv_pru_disable(pru_num) ' Disable PRU
163  ' note: no prussdrv_exit(), libpruio does it in the destructor
164 LOOP UNTIL 1
165 
166 DELETE io ' destroy driver structure
167 
168 '' help Doxygen to document the main code
169 '&/** The main function. */
170 '&int main() {PruIo::PruIo(); PruIo::setPin(); PruIo::config(); CapMod::config(); CapMod::Value(); PruIo::~PruIo();}
171