rtl8720dn: add SetupAndConnectToAccessPoint()

This commit is contained in:
sago35
2022-07-23 10:07:22 +09:00
parent ca34b93e5d
commit f94ef9e535
9 changed files with 52 additions and 363 deletions
+8 -10
View File
@@ -2,11 +2,15 @@ package main
import (
"fmt"
"image/color"
"machine"
"strconv"
"time"
"tinygo.org/x/drivers/examples/rtl8720dn"
"tinygo.org/x/drivers/net/http"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freemono"
)
// You can override the setting with the init() in another source code.
@@ -23,11 +27,9 @@ var (
)
var led = machine.LED
var backlight = machine.LCD_BACKLIGHT
func main() {
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
backlight.Configure(machine.PinConfig{Mode: machine.PinOutput})
err := run()
for err != nil {
@@ -37,13 +39,9 @@ func main() {
}
func run() error {
rtl, err := setupRTL8720DN()
if err != nil {
return err
}
http.UseDriver(rtl)
tinyfont.WriteLine(display, &freemono.Regular9pt7b, 5, 15, "setup...", color.RGBA{0x00, 0x00, 0x00, 0xFF})
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
rtl, err := rtl8720dn.SetupAndConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
@@ -55,6 +53,8 @@ func run() error {
fmt.Printf("IP Address : %s\r\n", ip)
fmt.Printf("Mask : %s\r\n", subnet)
fmt.Printf("Gateway : %s\r\n", gateway)
tinyfont.WriteLine(display, &freemono.Regular9pt7b, 5, 15, "setup...done", color.RGBA{0x00, 0x00, 0x00, 0xFF})
tinyfont.WriteLine(display, &freemono.Regular9pt7b, 5, 30, fmt.Sprintf("listen:\nhttp://%s/", ip), color.RGBA{0x00, 0x00, 0x00, 0xFF})
http.HandleFunc("/", root)
http.HandleFunc("/hello", hello)
@@ -154,14 +154,12 @@ func sixlines(w http.ResponseWriter, r *http.Request) {
func LED_ON(w http.ResponseWriter, r *http.Request) {
led.High()
backlight.High()
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
fmt.Fprintf(w, "led.High()")
}
func LED_OFF(w http.ResponseWriter, r *http.Request) {
led.Low()
backlight.Low()
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
fmt.Fprintf(w, "led.Low()")
}
+9 -60
View File
@@ -4,71 +4,20 @@
package main
import (
"device/sam"
"machine"
"runtime/interrupt"
"time"
"tinygo.org/x/drivers/rtl8720dn"
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
"tinygo.org/x/drivers/ili9341"
)
var (
uart UARTx
display *ili9341.Device
)
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)
}
func setupRTL8720DN() (*rtl8720dn.RTL8720DN, error) {
machine.RTL8720D_CHIP_PU.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.RTL8720D_CHIP_PU.Low()
time.Sleep(100 * time.Millisecond)
machine.RTL8720D_CHIP_PU.High()
time.Sleep(1000 * time.Millisecond)
if debug {
waitSerial()
}
uart = UARTx{
UART: &machine.UART{
Buffer: machine.NewRingBuffer(),
Bus: sam.SERCOM0_USART_INT,
SERCOM: 0,
},
}
uart.Interrupt = interrupt.New(sam.IRQ_SERCOM0_2, handleInterrupt)
uart.Configure(machine.UARTConfig{TX: machine.PB24, RX: machine.PC24, BaudRate: 614400})
rtl := rtl8720dn.New(uart)
rtl.Debug(debug)
_, err := rtl.Rpc_tcpip_adapter_init()
if err != nil {
return nil, err
}
return rtl, nil
}
// 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)
func init() {
display = initdisplay.InitDisplay()
display.SetRotation(ili9341.Rotation270)
// override
led = machine.LCD_BACKLIGHT
}