forked from andrevdm/PiPcd8544Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi.fs
36 lines (27 loc) · 1.16 KB
/
spi.fs
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
module spi
open System
open System.Text
open System.Runtime.InteropServices
module private imp =
[<DllImport( "libwiringPi.so", EntryPoint="wiringPiSPISetup", CallingConvention = CallingConvention.Cdecl, SetLastError=true )>]
extern int wiringPiSPISetup( int channel, int speed );
[<DllImport( "libwiringPi.so", EntryPoint="wiringPiSPIDataRW", CallingConvention = CallingConvention.Cdecl, SetLastError=true )>]
extern void wiringPiSPIDataRW( int channel, byte[] data, int len );
type spiChannel =
| channel0 = 0
| channel1 = 1
let spiSetup (channel:spiChannel) speed =
let res = imp.wiringPiSPISetup( (int channel), speed )
if res = -1 then
failwith (sprintf "spi setup failed %d" res)
let spiWR channel (data : byte[]) =
imp.wiringPiSPIDataRW( (int channel), data, data.Length )
data
let spiW (channel:spiChannel) (data : byte[]) =
spiWR channel data |> ignore
let spiWString (channel:spiChannel) (data : string) =
spiWR channel (System.Text.Encoding.ASCII.GetBytes( data )) |> ignore
let spiWRByte (channel:spiChannel) (data : byte) =
spiWR channel [| data |]
let spiWByte (channel:spiChannel) (data : byte) =
spiWRByte channel data |>ignore