From a6a8d478b9efcf2191af4a565ed89f05466affb2 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 5 Sep 2022 15:35:18 +0200 Subject: [PATCH] examples/rtl8720dn: update all remaining examples for refactored API Signed-off-by: deadprogram --- examples/rtl8720dn/mqttsub/main.go | 23 +++-- examples/rtl8720dn/mqttsub/wioterminal.go | 74 -------------- examples/rtl8720dn/ntpclient/main.go | 14 +-- examples/rtl8720dn/ntpclient/wioterminal.go | 72 -------------- examples/rtl8720dn/tcpclient/main.go | 15 ++- examples/rtl8720dn/tcpclient/wioterminal.go | 72 -------------- examples/rtl8720dn/tlsclient/main.go | 16 +-- examples/rtl8720dn/tlsclient/wioterminal.go | 72 -------------- examples/rtl8720dn/udpstation/main.go | 15 +-- examples/rtl8720dn/udpstation/wioterminal.go | 72 -------------- examples/rtl8720dn/version/main.go | 13 ++- examples/rtl8720dn/webclient-tinyterm/main.go | 40 ++++++-- .../webclient-tinyterm/wioterminal.go | 98 ------------------- examples/rtl8720dn/webclient/main.go | 16 +-- examples/rtl8720dn/webclient/wioterminal.go | 72 -------------- examples/rtl8720dn/webserver/main.go | 14 +-- examples/rtl8720dn/webserver/wioterminal.go | 74 -------------- examples/rtl8720dn/wioterminal.go | 79 --------------- 18 files changed, 94 insertions(+), 757 deletions(-) delete mode 100644 examples/rtl8720dn/mqttsub/wioterminal.go delete mode 100644 examples/rtl8720dn/ntpclient/wioterminal.go delete mode 100644 examples/rtl8720dn/tcpclient/wioterminal.go delete mode 100644 examples/rtl8720dn/tlsclient/wioterminal.go delete mode 100644 examples/rtl8720dn/udpstation/wioterminal.go delete mode 100644 examples/rtl8720dn/webclient-tinyterm/wioterminal.go delete mode 100644 examples/rtl8720dn/webclient/wioterminal.go delete mode 100644 examples/rtl8720dn/webserver/wioterminal.go delete mode 100644 examples/rtl8720dn/wioterminal.go diff --git a/examples/rtl8720dn/mqttsub/main.go b/examples/rtl8720dn/mqttsub/main.go index bff8040..46841f5 100644 --- a/examples/rtl8720dn/mqttsub/main.go +++ b/examples/rtl8720dn/mqttsub/main.go @@ -7,16 +7,17 @@ // // You must also install the Paho MQTT package to build this program: // -// go get -u github.com/eclipse/paho.mqtt.golang +// go get -u github.com/eclipse/paho.mqtt.golang // // You can check that mqttpub/mqttsub is running successfully with the following command. // -// mosquitto_sub -h test.mosquitto.org -t tinygo/tx -// mosquitto_pub -h test.mosquitto.org -t tinygo/rx -m "hello world" -// +// mosquitto_sub -h test.mosquitto.org -t tinygo/tx +// mosquitto_pub -h test.mosquitto.org -t tinygo/rx -m "hello world" package main import ( + "machine" + "fmt" "math/rand" "time" @@ -45,7 +46,7 @@ var buf [0x400]byte var lastRequestTime time.Time var conn net.Conn -var adaptor *rtl8720dn.RTL8720DN +var adaptor *rtl8720dn.Driver func main() { err := run() @@ -68,18 +69,16 @@ func subHandler(client mqtt.Client, msg mqtt.Message) { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) + // change the UART and pins as needed for platforms other than the WioTerminal. + adaptor = rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/mqttsub/wioterminal.go b/examples/rtl8720dn/mqttsub/wioterminal.go deleted file mode 100644 index ad61638..0000000 --- a/examples/rtl8720dn/mqttsub/wioterminal.go +++ /dev/null @@ -1,74 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) -} diff --git a/examples/rtl8720dn/ntpclient/main.go b/examples/rtl8720dn/ntpclient/main.go index 37f62a6..0c9eee2 100644 --- a/examples/rtl8720dn/ntpclient/main.go +++ b/examples/rtl8720dn/ntpclient/main.go @@ -4,12 +4,15 @@ package main import ( + "machine" + "errors" "fmt" "runtime" "time" "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/rtl8720dn" ) // IP address of the server aka "hub". Replace with your own info. @@ -41,18 +44,15 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/ntpclient/wioterminal.go b/examples/rtl8720dn/ntpclient/wioterminal.go deleted file mode 100644 index 076d0d8..0000000 --- a/examples/rtl8720dn/ntpclient/wioterminal.go +++ /dev/null @@ -1,72 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) - 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) -} diff --git a/examples/rtl8720dn/tcpclient/main.go b/examples/rtl8720dn/tcpclient/main.go index 2a65f6c..a493e62 100644 --- a/examples/rtl8720dn/tcpclient/main.go +++ b/examples/rtl8720dn/tcpclient/main.go @@ -4,15 +4,17 @@ // You can open a server to accept connections from this program using: // // nc -w 5 -lk 8080 -// package main import ( + "machine" + "bytes" "fmt" "time" "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/rtl8720dn" ) // You can override the setting with the init() in another source code. @@ -41,18 +43,15 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/tcpclient/wioterminal.go b/examples/rtl8720dn/tcpclient/wioterminal.go deleted file mode 100644 index 076d0d8..0000000 --- a/examples/rtl8720dn/tcpclient/wioterminal.go +++ /dev/null @@ -1,72 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) - 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) -} diff --git a/examples/rtl8720dn/tlsclient/main.go b/examples/rtl8720dn/tlsclient/main.go index 7d83310..21a8987 100644 --- a/examples/rtl8720dn/tlsclient/main.go +++ b/examples/rtl8720dn/tlsclient/main.go @@ -1,6 +1,8 @@ package main import ( + "machine" + "bufio" "fmt" "strings" @@ -68,20 +70,18 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - rtl.SetRootCA(&test_root_ca) - net.UseDriver(rtl) + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() + + adaptor.SetRootCA(&test_root_ca) http.SetBuf(buf[:]) - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/tlsclient/wioterminal.go b/examples/rtl8720dn/tlsclient/wioterminal.go deleted file mode 100644 index 076d0d8..0000000 --- a/examples/rtl8720dn/tlsclient/wioterminal.go +++ /dev/null @@ -1,72 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) - 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) -} diff --git a/examples/rtl8720dn/udpstation/main.go b/examples/rtl8720dn/udpstation/main.go index 02f243d..35742b9 100644 --- a/examples/rtl8720dn/udpstation/main.go +++ b/examples/rtl8720dn/udpstation/main.go @@ -1,12 +1,15 @@ package main import ( + "machine" + "fmt" "strconv" "time" "tinygo.org/x/drivers/net" "tinygo.org/x/drivers/net/http" + "tinygo.org/x/drivers/rtl8720dn" ) // IP address of the server aka "hub". Replace with your own info. @@ -36,19 +39,17 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() + http.SetBuf(buf[:]) - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/udpstation/wioterminal.go b/examples/rtl8720dn/udpstation/wioterminal.go deleted file mode 100644 index 076d0d8..0000000 --- a/examples/rtl8720dn/udpstation/wioterminal.go +++ /dev/null @@ -1,72 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) - 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) -} diff --git a/examples/rtl8720dn/version/main.go b/examples/rtl8720dn/version/main.go index 30cca18..0737822 100644 --- a/examples/rtl8720dn/version/main.go +++ b/examples/rtl8720dn/version/main.go @@ -1,10 +1,12 @@ package main import ( + "machine" + "fmt" "time" - "tinygo.org/x/drivers/examples/rtl8720dn" + "tinygo.org/x/drivers/rtl8720dn" ) var ( @@ -20,13 +22,10 @@ func main() { } func run() error { - //rtl8720dn.Debug(true) - rtl, err := rtl8720dn.Setup() - if err != nil { - return err - } + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() - ver, err := rtl.Version() + ver, err := adaptor.Version() if err != nil { return nil } diff --git a/examples/rtl8720dn/webclient-tinyterm/main.go b/examples/rtl8720dn/webclient-tinyterm/main.go index 446e40c..e46ffaf 100644 --- a/examples/rtl8720dn/webclient-tinyterm/main.go +++ b/examples/rtl8720dn/webclient-tinyterm/main.go @@ -1,14 +1,18 @@ package main import ( + "machine" + "bufio" "fmt" "image/color" "strings" "time" - "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/ili9341" "tinygo.org/x/drivers/net/http" + "tinygo.org/x/drivers/rtl8720dn" + "tinygo.org/x/tinyfont/proggy" "tinygo.org/x/tinyterm" ) @@ -30,6 +34,15 @@ var ( ) var ( + display = ili9341.NewSPI( + machine.SPI3, + machine.LCD_DC, + machine.LCD_SS_PIN, + machine.LCD_RESET, + ) + + backlight = machine.LCD_BACKLIGHT + terminal = tinyterm.NewTerminal(display) black = color.RGBA{0, 0, 0, 255} @@ -66,21 +79,20 @@ func run() error { fmt.Fprintf(terminal, "Running in debug mode.\r\n") fmt.Fprintf(terminal, "A serial connection is required to continue execution.\r\n") } - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) + + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() + http.SetBuf(buf[:]) fmt.Fprintf(terminal, "ConnectToAP()\r\n") - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } fmt.Fprintf(terminal, "connected\r\n\r\n") - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } @@ -134,3 +146,15 @@ func run() error { time.Sleep(10 * time.Second) } } + +func init() { + machine.SPI3.Configure(machine.SPIConfig{ + SCK: machine.LCD_SCK_PIN, + SDO: machine.LCD_SDO_PIN, + SDI: machine.LCD_SDI_PIN, + Frequency: 40000000, + }) + display.Configure(ili9341.Config{}) + + backlight.Configure(machine.PinConfig{machine.PinOutput}) +} diff --git a/examples/rtl8720dn/webclient-tinyterm/wioterminal.go b/examples/rtl8720dn/webclient-tinyterm/wioterminal.go deleted file mode 100644 index 3b45457..0000000 --- a/examples/rtl8720dn/webclient-tinyterm/wioterminal.go +++ /dev/null @@ -1,98 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/ili9341" - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -var ( - display = ili9341.NewSPI( - machine.SPI3, - machine.LCD_DC, - machine.LCD_SS_PIN, - machine.LCD_RESET, - ) - - backlight = machine.LCD_BACKLIGHT -) - -func init() { - machine.SPI3.Configure(machine.SPIConfig{ - SCK: machine.LCD_SCK_PIN, - SDO: machine.LCD_SDO_PIN, - SDI: machine.LCD_SDI_PIN, - Frequency: 40000000, - }) - display.Configure(ili9341.Config{}) - - backlight.Configure(machine.PinConfig{machine.PinOutput}) -} - -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) -} diff --git a/examples/rtl8720dn/webclient/main.go b/examples/rtl8720dn/webclient/main.go index 565481e..cb1b78d 100644 --- a/examples/rtl8720dn/webclient/main.go +++ b/examples/rtl8720dn/webclient/main.go @@ -1,13 +1,15 @@ package main import ( + "machine" + "bufio" "fmt" "strings" "time" - "tinygo.org/x/drivers/net" "tinygo.org/x/drivers/net/http" + "tinygo.org/x/drivers/rtl8720dn" ) // You can override the setting with the init() in another source code. @@ -36,19 +38,17 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() + http.SetBuf(buf[:]) - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/webclient/wioterminal.go b/examples/rtl8720dn/webclient/wioterminal.go deleted file mode 100644 index 076d0d8..0000000 --- a/examples/rtl8720dn/webclient/wioterminal.go +++ /dev/null @@ -1,72 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) - 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) -} diff --git a/examples/rtl8720dn/webserver/main.go b/examples/rtl8720dn/webserver/main.go index 98807da..c6701c0 100644 --- a/examples/rtl8720dn/webserver/main.go +++ b/examples/rtl8720dn/webserver/main.go @@ -7,6 +7,7 @@ import ( "time" "tinygo.org/x/drivers/net/http" + "tinygo.org/x/drivers/rtl8720dn" ) // You can override the setting with the init() in another source code. @@ -37,18 +38,17 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - http.UseDriver(rtl) + adaptor := rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU) + adaptor.Configure() - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + http.UseDriver(adaptor) + + err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - ip, subnet, gateway, err := rtl.GetIP() + ip, subnet, gateway, err := adaptor.GetIP() if err != nil { return err } diff --git a/examples/rtl8720dn/webserver/wioterminal.go b/examples/rtl8720dn/webserver/wioterminal.go deleted file mode 100644 index ad61638..0000000 --- a/examples/rtl8720dn/webserver/wioterminal.go +++ /dev/null @@ -1,74 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package main - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx -) - -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) -} diff --git a/examples/rtl8720dn/wioterminal.go b/examples/rtl8720dn/wioterminal.go deleted file mode 100644 index ad56b87..0000000 --- a/examples/rtl8720dn/wioterminal.go +++ /dev/null @@ -1,79 +0,0 @@ -//go:build wioterminal -// +build wioterminal - -package rtl8720dn - -import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - - "tinygo.org/x/drivers/rtl8720dn" -) - -var ( - uart UARTx - debug bool -) - -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 Setup() (*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 Debug(b bool) { - debug = b -}