libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
stepper2.bas
1 
25 
26 ' include libpruio
27 #INCLUDE ONCE "BBB/pruio.bi"
28 ' include the convenience macros for header pins
29 #INCLUDE ONCE "BBB/pruio_pins.bi"
30 
31 
32 #DEFINE P1 P8_03
33 
34 #DEFINE P2 P8_04
35 
36 #DEFINE P3 P8_05
37 
38 #DEFINE P4 P8_06
39 
40 ' declare pin masks (all pins at one GPIO-SS#SUSYS)
41 DIM SHARED AS UInt32 _
42  SUSYS _
43 , MASKS(8, 1)
44 
45 
46 
56 SUB MaskInit(BYVAL Io AS PruIo PTR)
57  WITH *Io
58  SUSYS = .BallGpio(P4) SHR 5
59  VAR m0 = 1 SHL (.BallGpio(P1) AND &b11111)
60  VAR m1 = 1 SHL (.BallGpio(P2) AND &b11111)
61  VAR m2 = 1 SHL (.BallGpio(P3) AND &b11111)
62  VAR m3 = 1 SHL (.BallGpio(P4) AND &b11111)
63  MASKS(0, 0) = m0 : MASKS(0, 1) = m1 + m2 + m3
64  MASKS(1, 0) = m0 + m1 : MASKS(1, 1) = m2 + m3
65  MASKS(2, 0) = m1 : MASKS(2, 1) = m0 + m2 + m3
66  MASKS(3, 0) = m1 + m2 : MASKS(3, 1) = m0 + m3
67  MASKS(4, 0) = m2 : MASKS(4, 1) = m0 + m1 + m3
68  MASKS(5, 0) = m2 + m3 : MASKS(5, 1) = m0 + m1
69  MASKS(6, 0) = m3 : MASKS(6, 1) = m0 + m1 + m2
70  MASKS(7, 0) = m3 + m0 : MASKS(7, 1) = m1 + m2
71  MASKS(8, 0) = 0 : MASKS(8, 1) = m0 + m1 + m2 + m3
72  END WITH
73 END SUB
74 
75 
90 SUB move(BYVAL Io AS PruIo PTR, BYVAL Rot AS BYTE = 1)
91  STATIC AS long p = 0
92  WITH *Io
93  p += Rot
94  p AND= IIF(Rot AND &b1, &b111, &b110)
95 
96  WITH *.Gpio->Conf(SUSYS)
97  .SETDATAOUT = MASKS(p, 0)
98  .CLEARDATAOUT = MASKS(p, 1)
99  END WITH
100  .Gpio->flush(SUSYS) ' write data to hardware registers
101  END WITH
102 END SUB
103 
104 
105 
106 ' ***** main *****
107 
108 VAR io = NEW PruIo
109 
110 WITH *io
111  DO ' pseudo loop, just to avoid GOTOs
112  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
113 
114 ' initial output pin settings
115  IF .Gpio->config(P1, PRUIO_GPIO_OUT1) THEN ?"failed setting P1 (" & *.Errr & ")"
116  IF .Gpio->config(P2, PRUIO_GPIO_OUT0) THEN ?"failed setting P2 (" & *.Errr & ")"
117  IF .Gpio->config(P3, PRUIO_GPIO_OUT0) THEN ?"failed setting P3 (" & *.Errr & ")"
118  IF .Gpio->config(P4, PRUIO_GPIO_OUT1) THEN ?"failed setting P4 (" & *.Errr & ")"
119  IF .Errr THEN EXIT DO
120  MaskInit(io)
121 
122 ' pin config OK, transfer local settings to PRU and start PRU driver
123  IF .config() THEN ?"config failed (" & *.Errr & ")" : EXIT DO
124 
125  ? ' print user informations
126  ?"Controls: (other keys quit, 1 and 3 only when Direction = 0)"
127  ?" 8 = faster"
128  ?" 4 = rotate CW 5 = stop, hold position 6 = rotate CCW"
129  ?" 1 = single step CW 2 = slower 3 = single step CCW"
130  ?" 0 = stop, power off"
131  ?
132  ?"Pins","Direction","Sleep" : ?
133 
134  VAR w = 128 _
135  , d = 0 _
136  , x = 16 _
137  , y = CSRLIN() - 1
138  LOCATE y, x, 0 : ?RIGHT(" " & d, 2), RIGHT(" " & w, 3); ' user info
139  DO ' endless loop
140  VAR k = ASC(INKEY())
141  IF k THEN
142  SELECT CASE AS CONST k ' react on user keystrokes
143  CASE ASC("2") : IF w < 512 THEN w SHL= 1 ' faster
144  CASE ASC("8") : IF w > 1 THEN w SHR= 1 ' slower
145  CASE ASC("4") : d = -1 ' half step CW
146  CASE ASC("7") : d = -2 ' half step CCW
147  CASE ASC("9") : d = 2 ' full step CW
148  CASE ASC("6") : d = 1 ' full step CCW
149  CASE ASC("5") : d = 0 : move(io, d) ' powered stop
150  CASE ASC("1") : IF 0 = d THEN move(io, -1) ' single step CW
151  CASE ASC("3") : IF 0 = d THEN move(io, 1) ' single step CCW
152  CASE ASC("0") : d = 0 ' unpowered stop
153  WITH *.Gpio->Conf(SUSYS) ' set OFF state
154  .SETDATAOUT = MASKS(8, 0)
155  .CLEARDATAOUT = MASKS(8, 1)
156  END WITH
157  .Gpio->flush(SUSYS) ' write data to hardware registers
158  CASE ELSE : EXIT DO ' finish
159  END SELECT
160 
161  LOCATE y, x, 0
162  ?RIGHT(" " & d, 2), RIGHT(" " & w, 3); ' user info
163  END IF
164  IF d THEN move(io, d) ' move the motor
165  LOCATE y, 1, 0
166  ? .Gpio->Value(P1) & "-" _
167  & .Gpio->Value(P2) & "-" _
168  & .Gpio->Value(P3) & "-" _
169  & .Gpio->Value(P4);
170  SLEEP w ' control the frequency = speed
171  LOOP UNTIL .Errr : ?
172  IF .Errr THEN ?"abborted: " & *.Errr
173 
174  WITH *.Gpio->Conf(SUSYS) ' set OFF state
175  .SETDATAOUT = MASKS(8, 0)
176  .CLEARDATAOUT = MASKS(8, 1)
177  END WITH
178  .Gpio->flush(SUSYS) ' write data to hardware registers
179  IF .Errr THEN ?"zeroing pins failed: : " & *.Errr
180 
181  IF .Errr THEN ?"re-setting pins failed: : " & *.Errr
182  LOOP UNTIL 1
183 END WITH
184 
185 DELETE io ' reset ADC, PinMux and GPIOs, clear memory
186 
187 '' help Doxygen to document the main code
188 '&/** The main function. */
189 '&int main() {PruIo::PruIo(); GpioUdt::config(); PruIo::config(); GpioUdt::setValue(); GpioUdt::Value(); PruIo::~PruIo();}
190