-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb1.hs
194 lines (178 loc) · 6.74 KB
/
usb1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module Main where
-- module System.USB.Hexwax
--import Data.Time.Clock
import Control.Concurrent (threadDelay)
import Data.ByteString as B hiding (putStrLn, getLine)
import Data.Char (intToDigit)
import Data.Word (Word16)
import Numeric (showIntAtBase)
import System.Environment
import System.IO
import Hexwax as HW
import Text.Printf
main :: IO ()
main = do
hw <- HW.attach
case hw of
Just dev -> do
-- Print the FIRMWAREID response
response <- HW.firmwareId dev
-- Get the USB information printed out
HW.printUsbDeviceInfo2 $ hwhDev dev
-- set output unbuffered we we are using ANSI [-sequence
hSetBuffering stdout NoBuffering
--
-- read all bits from PORTB, from datasheet: 9C 02 00 FF
--
-- 18Fx455 datasheetpage 116 EXAMPLE 10-2: INITIALIZING PORTB
HW.setRegister dev regPORTB 0x00 -- clear output data latches
HW.setRegister dev regADCON1 0x0E -- set RB<4:0> as digital I/I pins
HW.setRegister dev regTRISB 0xFF -- set RB<7:0> as inputs
--
-- INTCON2<7? clear to ENABLE PORTB internal pull-ups via LATB
-- DEFAULT STATE: () 1111 -1-1 1111 0101 => F5
-- DESIRED STATE: 0111 -1-1 0111 0101 => E5
--HW.setRegister dev regINTCON2 0xE5
--HW.setRegister dev regLATB 0x00 -- LATB written as 0 ??!?!!?
--HW.getRegister dev regTRISB >>= \x -> print x
-- CMCON to 0x07 (Default value anyway!)
--HW.setRegister dev 0xB4 0x07
printOutput
return ()
where printOutput = do
--HW.wait dev regPORTC 0x00 0xFF
buf1 <- HW.getRegister dev regPORTB
let val1 = (B.index (hwrBuf buf1) 2)
--putStrLn $ "MSB " ++ (showIntAtBase 2 intToDigit val1 " LSB --PORTB")
buf2 <- HW.getPort dev ioPORTB 0xFF
printf "\ESC[sPORTB: %02X %02X\ESC[u"
--printf "PORTB: GETREG: %02X\n" -- GETPORT: %02X\n"
(B.index (hwrBuf buf1) 2)
(B.index (hwrBuf buf2) 2)
--threadDelay 1000000
printOutput
Nothing ->
putStrLn "HexWax device not found"
{-
-- simple LED test
response <- HW.setPort dev 0x01 0xFE 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xFD 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xFB 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xF7 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xFF 0x00 -- all bits off again
putStrLn "*done*"
-}
{-
-- set output unbuffered we we are using ANSI [-sequence
hSetBuffering stdout NoBuffering
-- read all bits from PORTB, from datasheet: 9C 02 00 FF
-- Set portB as all inputs
HW.setRegister dev regTRISB 0xFF
HW.getRegister dev regTRISB >>= \x -> print x
printOutput
return ()
where printOutput = do
HW.wait dev regPORTC 0x00 0xFF
buf1 <- HW.getRegister dev regPORTB
buf2 <- HW.getPort dev ioPORTB 0xFF
printf "\ESC[sPORTB: %02X %02X\ESC[u"
(B.index (hwrBuf buf1) 2)
(B.index (hwrBuf buf2) 2)
printOutput
-}
{-
-- WAIT test... 4 * 250 is one second so we should be able to blink
HW.setPortBit dev ioPORTA 0x00 0x00
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.wait dev regPORTB 0x00 0xFA
HW.setPortBit dev ioPORTA 0x00 0x01
-}
{-
response <- HW.setPort dev 0x01 0xFE 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xFD 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xFB 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xF7 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPort dev 0x01 0xFF 0x00 -- all bits off again
response <- HW.setPortBit dev 0x01 0x00 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x00 0x01
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x01 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x01 0x01
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x02 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x02 0x01
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x03 0x00
putStrLn "Press a key" >> getLine
response <- HW.setPortBit dev 0x01 0x03 0x01
-- stepper motor pattern, one direction
putStrLn "Press key, stepper motor pattern..." >> getLine
-}
{-
hexwaxGo2 :: Device -> IO ()
hexwaxGo2 device = withDeviceHandle device attach
where attach handle = withDetachedKernelDriver handle 0 claim
claim = do
let cmd = BS.replicate 4 '\0'
withClaimedInterface handle ifNum
(HW.writeCmd cmd ifNum handle)
-}
-- withDeviceHandle => withDetachedKernelDriver => withClaimedInterface => HW.writeCmd
{-
hexwaxGo :: Device -> IO ()
hexwaxGo device = do
withDeviceHandle device attach
where attach handle = do
withDetachedKernelDriver handle 0 claim
where claim = do
let cmd = BS.replicate 4 '\0'
withClaimedInterface handle ifNum
(HW.writeCmd cmd ifNum handle)
-}
{-
System.USB.Hexwax
.open -- use default from datasheet
.openAt productId vendorId
.close
.API. as HWA
.setBit .getBit .setPort .getPort etc.
.Util as HWU
--
-- Bipolar stepper-motor driving
--
.bpStepFwd
.bpStepFwd N
.bpStepRev
.bpStepRev N
--
-- Unipolar stepper-motor driving
--
.upStepFwd
.ubpStepFwd N
.upStepRev
.upStepRev N
clever extensions:
the ability to name an arbitrary group of outputs as a "pseudo-register" and then
send a value to it.
-}