-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrtl8720dn.go
120 lines (96 loc) · 2.02 KB
/
rtl8720dn.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
113
114
115
116
117
118
119
120
//go:build wioterminal
// +build wioterminal
package initialize
import (
"device/sam"
"machine"
"runtime"
"runtime/interrupt"
"time"
"tinygo.org/x/drivers/net/http"
"tinygo.org/x/drivers/rtl8720dn"
)
var (
rtl *rtl8720dn.Driver
connected bool
uart UARTx
debug bool
buf [0x1000]byte
)
func handleInterrupt(interrupt.Interrupt) {
// should reset IRQ
uart.Receive(byte((uart.Bus.DATA.Get() & 0xFF)))
uart.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
}
// SetupRTL8720DN sets up the RTL8270DN for use.
func SetupRTL8720DN() (*rtl8720dn.Driver, error) {
rtl = rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU)
if debug {
waitSerial()
}
rtl.Debug(debug)
rtl.Configure()
connected = true
return rtl, nil
}
// Wifi sets up the RTL8720DN and connects it to Wi-Fi.
func Wifi(ssid, pass string, timeout time.Duration) (*rtl8720dn.Driver, error) {
_, err := SetupRTL8720DN()
if err != nil {
return nil, err
}
err = rtl.ConnectToAccessPoint(ssid, pass, 10*time.Second)
if err != nil {
return rtl, err
}
http.UseDriver(rtl)
http.SetBuf(buf[:])
// NTP
t, err := GetCurrentTime()
if err != nil {
return nil, err
}
runtime.AdjustTimeOffset(-1 * int64(time.Since(t)))
return rtl, nil
}
func Device() *rtl8720dn.Driver {
return rtl
}
func Connected() bool {
return connected
}
func IP() rtl8720dn.IPAddress {
ip, _, _, _ := rtl.GetIP()
return ip
}
func Subnet() rtl8720dn.IPAddress {
_, subnet, _, _ := rtl.GetIP()
return subnet
}
func Gateway() rtl8720dn.IPAddress {
_, _, gateway, _ := rtl.GetIP()
return gateway
}
func SetRootCA(s *string) {
rtl.SetRootCA(s)
}
// Wait for user to open serial console
func waitSerial() {
for !machine.Serial.DTR() {
time.Sleep(100 * time.Millisecond)
}
}
type UARTx struct {
*machine.UART
}
func (u UARTx) Read(p []byte) (n int, err error) {
if u.Buffered() == 0 {
time.Sleep(1 * time.Millisecond)
return 0, nil
}
return u.UART.Read(p)
}
// Debug sets the debug mode.
func Debug(b bool) {
debug = b
}