Skip to content

Commit 0bc3702

Browse files
sago35deadprogram
authored andcommitted
rtl8720dn: add support for rtl8720dn
1 parent b53a8a1 commit 0bc3702

File tree

19 files changed

+10731
-2
lines changed

19 files changed

+10731
-2
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,14 @@ endif
197197
@md5sum ./build/test.hex
198198
tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/sdcard/tinyfs/
199199
@md5sum ./build/test.hex
200+
tinygo build -size short -o ./build/test.hex -target=wioterminal ./examples/rtl8720dn/webclient/
201+
@md5sum ./build/test.hex
200202

201203
DRIVERS = $(wildcard */)
202204
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
203205
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
204206
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x dht keypad4x4 max72xx p1am tone tm1637 \
205-
pcf8563 mcp2515 servo sdcard
207+
pcf8563 mcp2515 servo sdcard rtl8720dn
206208
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
207209

208210
unit-test:

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252

5353
## Currently supported devices
5454

55-
The following 66 devices are supported.
55+
The following 67 devices are supported.
5656

5757
| Device Name | Interface Type |
5858
|----------|-------------|
@@ -100,6 +100,7 @@ The following 66 devices are supported.
100100
| [PCD8544 display](http://eia.udg.edu/~forest/PCD8544_1.pdf) | SPI |
101101
| [PCF8563 real time clock](https://www.nxp.com/docs/en/data-sheet/PCF8563.pdf) | I2C |
102102
| [Resistive Touchscreen (4-wire)](http://ww1.microchip.com/downloads/en/Appnotes/doc8091.pdf) | GPIO |
103+
| [RTL8720DN 2.4G/5G Dual Bands Wireless and BLE5.0](https://www.seeedstudio.com/Realtek8720DN-2-4G-5G-Dual-Bands-Wireless-and-BLE5-0-Combo-Module-p-4442.html) | UART |
103104
| [Semihosting](https://wiki.segger.com/Semihosting) | Debug |
104105
| [Servo](https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all) | PWM |
105106
| [Shift register (PISO)](https://en.wikipedia.org/wiki/Shift_register#Parallel-in_serial-out_\(PISO\)) | GPIO |
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"time"
7+
8+
"tinygo.org/x/drivers/net"
9+
"tinygo.org/x/drivers/net/tls"
10+
"tinygo.org/x/drivers/rtl8720dn"
11+
)
12+
13+
// You can override the setting with the init() in another source code.
14+
// func init() {
15+
// ssid = "your-ssid"
16+
// password = "your-password"
17+
// debug = true
18+
// server = "tinygo.org"
19+
// test_root_ca = "..."
20+
// }
21+
22+
var (
23+
ssid string
24+
password string
25+
server string = "www.example.com"
26+
debug = false
27+
)
28+
29+
// Set the test_root_ca created by the following command
30+
// $ openssl s_client -showcerts -verify 5 -connect www.example.com:443 < /dev/null
31+
var test_root_ca = `-----BEGIN CERTIFICATE-----
32+
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
33+
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
34+
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
35+
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
36+
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
37+
b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG
38+
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB
39+
CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97
40+
nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt
41+
43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P
42+
T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4
43+
gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO
44+
BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR
45+
TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw
46+
DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr
47+
hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg
48+
06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF
49+
PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls
50+
YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk
51+
CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=
52+
-----END CERTIFICATE-----
53+
`
54+
55+
var buf [0x400]byte
56+
57+
var lastRequestTime time.Time
58+
var conn net.Conn
59+
var adaptor *rtl8720dn.RTL8720DN
60+
61+
func main() {
62+
err := run()
63+
for err != nil {
64+
fmt.Printf("error: %s\r\n", err.Error())
65+
time.Sleep(5 * time.Second)
66+
}
67+
}
68+
69+
func run() error {
70+
rtl, err := setupRTL8720DN()
71+
if err != nil {
72+
return err
73+
}
74+
rtl.SetRootCA(&test_root_ca)
75+
net.UseDriver(rtl)
76+
77+
err = rtl.ConnectToAP(ssid, password)
78+
if err != nil {
79+
return err
80+
}
81+
82+
ip, subnet, gateway, err := rtl.GetIP()
83+
if err != nil {
84+
return err
85+
}
86+
fmt.Printf("IP Address : %s\r\n", ip)
87+
fmt.Printf("Mask : %s\r\n", subnet)
88+
fmt.Printf("Gateway : %s\r\n", gateway)
89+
90+
cnt := 0
91+
for {
92+
readConnection()
93+
if time.Now().Sub(lastRequestTime).Milliseconds() >= 10000 {
94+
makeHTTPSRequest()
95+
cnt++
96+
fmt.Printf("-------- %d --------\r\n", cnt)
97+
}
98+
}
99+
}
100+
101+
func readConnection() {
102+
if conn != nil {
103+
for n, err := conn.Read(buf[:]); n > 0; n, err = conn.Read(buf[:]) {
104+
if err != nil {
105+
println("Read error: " + err.Error())
106+
} else {
107+
print(string(buf[0:n]))
108+
}
109+
}
110+
}
111+
}
112+
113+
func makeHTTPSRequest() {
114+
115+
var err error
116+
if conn != nil {
117+
conn.Close()
118+
}
119+
120+
message("\r\n---------------\r\nDialing TCP connection")
121+
conn, err = tls.Dial("tcp", server, nil)
122+
for ; err != nil; conn, err = tls.Dial("tcp", server, nil) {
123+
message("Connection failed: " + err.Error())
124+
time.Sleep(5 * time.Second)
125+
}
126+
println("Connected!\r")
127+
128+
print("Sending HTTPS request...")
129+
fmt.Fprintln(conn, "GET / HTTP/1.1")
130+
fmt.Fprintln(conn, "Host:", strings.Split(server, ":")[0])
131+
fmt.Fprintln(conn, "User-Agent: TinyGo")
132+
fmt.Fprintln(conn, "Connection: close")
133+
fmt.Fprintln(conn)
134+
println("Sent!\r\n\r")
135+
136+
lastRequestTime = time.Now()
137+
}
138+
139+
func message(msg string) {
140+
println(msg, "\r")
141+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// +build wioterminal
2+
3+
package main
4+
5+
import (
6+
"device/sam"
7+
"machine"
8+
"runtime/interrupt"
9+
"time"
10+
11+
"tinygo.org/x/drivers/rtl8720dn"
12+
)
13+
14+
var (
15+
uart UARTx
16+
)
17+
18+
func handleInterrupt(interrupt.Interrupt) {
19+
// should reset IRQ
20+
uart.Receive(byte((uart.Bus.DATA.Get() & 0xFF)))
21+
uart.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
22+
}
23+
24+
func setupRTL8720DN() (*rtl8720dn.RTL8720DN, error) {
25+
machine.RTL8720D_CHIP_PU.Configure(machine.PinConfig{Mode: machine.PinOutput})
26+
machine.RTL8720D_CHIP_PU.Low()
27+
time.Sleep(100 * time.Millisecond)
28+
machine.RTL8720D_CHIP_PU.High()
29+
time.Sleep(1000 * time.Millisecond)
30+
waitSerial()
31+
32+
uart = UARTx{
33+
UART: &machine.UART{
34+
Buffer: machine.NewRingBuffer(),
35+
Bus: sam.SERCOM0_USART_INT,
36+
SERCOM: 0,
37+
},
38+
}
39+
40+
uart.Interrupt = interrupt.New(sam.IRQ_SERCOM0_2, handleInterrupt)
41+
uart.Configure(machine.UARTConfig{TX: machine.PB24, RX: machine.PC24, BaudRate: 614400})
42+
43+
rtl := rtl8720dn.New(uart)
44+
rtl.Debug(debug)
45+
46+
_, err := rtl.Rpc_tcpip_adapter_init()
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
return rtl, nil
52+
}
53+
54+
// Wait for user to open serial console
55+
func waitSerial() {
56+
for !machine.Serial.DTR() {
57+
time.Sleep(100 * time.Millisecond)
58+
}
59+
}
60+
61+
type UARTx struct {
62+
*machine.UART
63+
}
64+
65+
func (u UARTx) Read(p []byte) (n int, err error) {
66+
if u.Buffered() == 0 {
67+
time.Sleep(1 * time.Millisecond)
68+
return 0, nil
69+
}
70+
return u.UART.Read(p)
71+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"image/color"
6+
"time"
7+
8+
"tinygo.org/x/drivers/net"
9+
"tinygo.org/x/drivers/rtl8720dn"
10+
"tinygo.org/x/tinyfont/proggy"
11+
"tinygo.org/x/tinyterm"
12+
)
13+
14+
// You can override the setting with the init() in another source code.
15+
// func init() {
16+
// ssid = "your-ssid"
17+
// password = "your-password"
18+
// debug = true
19+
// server = "tinygo.org"
20+
// }
21+
22+
var (
23+
ssid string
24+
password string
25+
server string = "tinygo.org"
26+
debug = false
27+
)
28+
29+
var (
30+
terminal = tinyterm.NewTerminal(display)
31+
32+
black = color.RGBA{0, 0, 0, 255}
33+
white = color.RGBA{255, 255, 255, 255}
34+
red = color.RGBA{255, 0, 0, 255}
35+
blue = color.RGBA{0, 0, 255, 255}
36+
green = color.RGBA{0, 255, 0, 255}
37+
38+
font = &proggy.TinySZ8pt7b
39+
)
40+
41+
var buf [0x400]byte
42+
43+
var lastRequestTime time.Time
44+
var conn net.Conn
45+
var adaptor *rtl8720dn.RTL8720DN
46+
47+
func main() {
48+
display.FillScreen(black)
49+
backlight.High()
50+
51+
terminal.Configure(&tinyterm.Config{
52+
Font: font,
53+
FontHeight: 10,
54+
FontOffset: 6,
55+
})
56+
57+
err := run()
58+
for err != nil {
59+
fmt.Fprintf(terminal, "error: %s\r\n", err.Error())
60+
time.Sleep(5 * time.Second)
61+
}
62+
}
63+
64+
func run() error {
65+
fmt.Fprintf(terminal, "setupRTL8720DN()\r\n")
66+
rtl, err := setupRTL8720DN()
67+
if err != nil {
68+
return err
69+
}
70+
net.UseDriver(rtl)
71+
72+
fmt.Fprintf(terminal, "ConnectToAP()\r\n")
73+
err = rtl.ConnectToAP(ssid, password)
74+
if err != nil {
75+
return err
76+
}
77+
fmt.Fprintf(terminal, "connected\r\n\r\n")
78+
79+
ip, subnet, gateway, err := rtl.GetIP()
80+
if err != nil {
81+
return err
82+
}
83+
fmt.Fprintf(terminal, "IP Address : %s\r\n", ip)
84+
fmt.Fprintf(terminal, "Mask : %s\r\n", subnet)
85+
fmt.Fprintf(terminal, "Gateway : %s\r\n", gateway)
86+
87+
cnt := 0
88+
for {
89+
readConnection()
90+
if time.Now().Sub(lastRequestTime).Milliseconds() >= 10000 {
91+
makeHTTPRequest()
92+
cnt++
93+
fmt.Fprintf(terminal, "-------- %d --------\r\n", cnt)
94+
}
95+
}
96+
}
97+
98+
func readConnection() {
99+
if conn != nil {
100+
for n, err := conn.Read(buf[:]); n > 0; n, err = conn.Read(buf[:]) {
101+
if err != nil {
102+
fmt.Fprintf(terminal, "Read error: "+err.Error()+"\r\n")
103+
} else {
104+
fmt.Fprintf(terminal, string(buf[0:n]))
105+
}
106+
}
107+
}
108+
}
109+
110+
func makeHTTPRequest() {
111+
112+
var err error
113+
if conn != nil {
114+
conn.Close()
115+
}
116+
117+
// make TCP connection
118+
ip := net.ParseIP(server)
119+
raddr := &net.TCPAddr{IP: ip, Port: 80}
120+
laddr := &net.TCPAddr{Port: 8080}
121+
122+
message("\r\n---------------\r\nDialing TCP connection")
123+
conn, err = net.DialTCP("tcp", laddr, raddr)
124+
for ; err != nil; conn, err = net.DialTCP("tcp", laddr, raddr) {
125+
message("Connection failed: " + err.Error())
126+
time.Sleep(5 * time.Second)
127+
}
128+
fmt.Fprintf(terminal, "Connected!\r\n")
129+
130+
fmt.Fprintf(terminal, "Sending HTTP request...")
131+
fmt.Fprintln(conn, "GET / HTTP/1.1")
132+
fmt.Fprintln(conn, "Host:", server)
133+
fmt.Fprintln(conn, "User-Agent: TinyGo")
134+
fmt.Fprintln(conn, "Connection: close")
135+
fmt.Fprintln(conn)
136+
fmt.Fprintf(terminal, "Sent!\r\n\r\n")
137+
138+
lastRequestTime = time.Now()
139+
}
140+
141+
func message(msg string) {
142+
fmt.Fprintf(terminal, "%s\r\n", msg)
143+
}

0 commit comments

Comments
 (0)