-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinterface_arm.go
executable file
·112 lines (86 loc) · 1.79 KB
/
interface_arm.go
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
package ws
import (
"fmt"
"log"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/host/v3"
)
type SPIbus struct {
port spi.Port
c spi.Conn
}
func (s *SPIbus) Write(data []byte) (int, error) {
// write := []byte{0x10, 0x00}
L := len(data)
// log.Println("Length of data %d ", len(data))
// read := make([]byte, len(data))
// _ = read
err := s.c.Tx(data, nil)
// if err := s.c.Tx(data, read); err != nil {
// log.Fatal(err)
// }
if L > 0 {
return L, err
}
// if L == 0 {
return 0, nil
// }
}
func (s *SPIbus) Close() {
s.Close()
}
func (s *SPIbus) Init() error {
// Open SPI port 0.0 at 1 MHz clock speed
// port, err := spi.Open("/dev/spidev0.0")
var err error
s.port, err = spireg.Open("")
if err != nil {
fmt.Printf("Failed to open SPI port: %v\n", err)
return err
}
// defer port.Close()
// Mode: spi.Mode0,
// BitsPerWord: bpw,
// MaxSpeed: 2 * physic.MegaHertz, //channel=2000000
s.c, err = s.port.Connect(2*physic.MegaHertz, spi.Mode0, bpw)
return err
}
func init() {
log.Panicln("ARM mode..")
}
func WriteBytes(data []byte) {
spibus.Write(data)
}
func CloseHW() {
spibus.Close()
}
func InitHW() {
initGPIO()
initSPI()
}
func DigitalWrite(pin int, level gpio.Level) error {
pinname := fmt.Sprintf("GPIO%d", pin)
gpin := gpioreg.ByName(pinname)
// p := gpioreg.ByName(gpiname)
return gpin.Out(level)
}
func initGPIO() {
host.Init() // moving periph.io ( suppoerts recent kernel)
}
var spibus SPIbus
func initSPI() {
spibus.Init()
}
func writeCmd(cmd byte) {
DigitalWrite(DC_PIN, gpio.Low)
spibus.Write([]byte{cmd})
}
func writeData(data ...byte) {
DigitalWrite(DC_PIN, gpio.High)
spibus.Write(data)
}
// ### END OF FILE ###