From f94ef9e5358460a03705940aa25bc979cb1996e0 Mon Sep 17 00:00:00 2001 From: sago35 Date: Sat, 23 Jul 2022 10:07:22 +0900 Subject: [PATCH] rtl8720dn: add SetupAndConnectToAccessPoint() --- examples/rtl8720dn/tlsclient/main.go | 27 +----- examples/rtl8720dn/tlsclient/wioterminal.go | 72 --------------- examples/rtl8720dn/webclient-tinyterm/main.go | 27 ++---- .../webclient-tinyterm/wioterminal.go | 88 +------------------ examples/rtl8720dn/webclient/main.go | 21 +---- examples/rtl8720dn/webclient/wioterminal.go | 72 --------------- examples/rtl8720dn/webserver/main.go | 18 ++-- examples/rtl8720dn/webserver/wioterminal.go | 67 ++------------ examples/rtl8720dn/wioterminal.go | 21 +++++ 9 files changed, 51 insertions(+), 362 deletions(-) delete mode 100644 examples/rtl8720dn/tlsclient/wioterminal.go delete mode 100644 examples/rtl8720dn/webclient/wioterminal.go diff --git a/examples/rtl8720dn/tlsclient/main.go b/examples/rtl8720dn/tlsclient/main.go index 7d83310f4..45af98c29 100644 --- a/examples/rtl8720dn/tlsclient/main.go +++ b/examples/rtl8720dn/tlsclient/main.go @@ -6,9 +6,8 @@ import ( "strings" "time" - "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/examples/rtl8720dn" "tinygo.org/x/drivers/net/http" - "tinygo.org/x/drivers/rtl8720dn" ) // You can override the setting with the init() in another source code. @@ -53,12 +52,6 @@ CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4= -----END CERTIFICATE----- ` -var buf [0x1000]byte - -var lastRequestTime time.Time -var conn net.Conn -var adaptor *rtl8720dn.RTL8720DN - func main() { err := run() for err != nil { @@ -68,26 +61,12 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() + rtl8720dn.Debug(debug) + rtl, err := rtl8720dn.SetupAndConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } rtl.SetRootCA(&test_root_ca) - net.UseDriver(rtl) - http.SetBuf(buf[:]) - - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) - if err != nil { - return err - } - - ip, subnet, gateway, err := rtl.GetIP() - if err != nil { - return err - } - fmt.Printf("IP Address : %s\r\n", ip) - fmt.Printf("Mask : %s\r\n", subnet) - fmt.Printf("Gateway : %s\r\n", gateway) // You can send and receive cookies in the following way // import "tinygo.org/x/drivers/net/http/cookiejar" diff --git a/examples/rtl8720dn/tlsclient/wioterminal.go b/examples/rtl8720dn/tlsclient/wioterminal.go deleted file mode 100644 index 076d0d8b7..000000000 --- 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/webclient-tinyterm/main.go b/examples/rtl8720dn/webclient-tinyterm/main.go index 446e40c7f..e521f8ec2 100644 --- a/examples/rtl8720dn/webclient-tinyterm/main.go +++ b/examples/rtl8720dn/webclient-tinyterm/main.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/examples/rtl8720dn" "tinygo.org/x/drivers/net/http" "tinygo.org/x/tinyfont/proggy" "tinygo.org/x/tinyterm" @@ -30,7 +30,7 @@ var ( ) var ( - terminal = tinyterm.NewTerminal(display) + terminal *tinyterm.Terminal black = color.RGBA{0, 0, 0, 255} white = color.RGBA{255, 255, 255, 255} @@ -41,12 +41,10 @@ var ( font = &proggy.TinySZ8pt7b ) -var buf [0x400]byte - func main() { display.FillScreen(black) - backlight.High() + terminal = tinyterm.NewTerminal(display) terminal.Configure(&tinyterm.Config{ Font: font, FontHeight: 10, @@ -65,29 +63,14 @@ func run() error { if debug { fmt.Fprintf(terminal, "Running in debug mode.\r\n") fmt.Fprintf(terminal, "A serial connection is required to continue execution.\r\n") + rtl8720dn.Debug(true) } - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) - http.SetBuf(buf[:]) - - fmt.Fprintf(terminal, "ConnectToAP()\r\n") - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) + _, err := rtl8720dn.SetupAndConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } fmt.Fprintf(terminal, "connected\r\n\r\n") - ip, subnet, gateway, err := rtl.GetIP() - if err != nil { - return err - } - fmt.Fprintf(terminal, "IP Address : %s\r\n", ip) - fmt.Fprintf(terminal, "Mask : %s\r\n", subnet) - fmt.Fprintf(terminal, "Gateway : %s\r\n", gateway) - // You can send and receive cookies in the following way // import "tinygo.org/x/drivers/net/http/cookiejar" // jar, err := cookiejar.New(nil) diff --git a/examples/rtl8720dn/webclient-tinyterm/wioterminal.go b/examples/rtl8720dn/webclient-tinyterm/wioterminal.go index 3b45457af..7555e87fa 100644 --- a/examples/rtl8720dn/webclient-tinyterm/wioterminal.go +++ b/examples/rtl8720dn/webclient-tinyterm/wioterminal.go @@ -4,95 +4,15 @@ package main import ( - "device/sam" - "machine" - "runtime/interrupt" - "time" - + "tinygo.org/x/drivers/examples/ili9341/initdisplay" "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 + display *ili9341.Device ) 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) + display = initdisplay.InitDisplay() + display.SetRotation(ili9341.Rotation0) } diff --git a/examples/rtl8720dn/webclient/main.go b/examples/rtl8720dn/webclient/main.go index 565481e75..de4e56251 100644 --- a/examples/rtl8720dn/webclient/main.go +++ b/examples/rtl8720dn/webclient/main.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/examples/rtl8720dn" "tinygo.org/x/drivers/net/http" ) @@ -25,8 +25,6 @@ var ( debug = false ) -var buf [0x400]byte - func main() { err := run() for err != nil { @@ -36,25 +34,10 @@ func main() { } func run() error { - rtl, err := setupRTL8720DN() - if err != nil { - return err - } - net.UseDriver(rtl) - http.SetBuf(buf[:]) - - err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) - if err != nil { - return err - } - - ip, subnet, gateway, err := rtl.GetIP() + _, err := rtl8720dn.SetupAndConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } - fmt.Printf("IP Address : %s\r\n", ip) - fmt.Printf("Mask : %s\r\n", subnet) - fmt.Printf("Gateway : %s\r\n", gateway) // You can send and receive cookies in the following way // import "tinygo.org/x/drivers/net/http/cookiejar" diff --git a/examples/rtl8720dn/webclient/wioterminal.go b/examples/rtl8720dn/webclient/wioterminal.go deleted file mode 100644 index 076d0d8b7..000000000 --- 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 98807dad7..d83806a23 100644 --- a/examples/rtl8720dn/webserver/main.go +++ b/examples/rtl8720dn/webserver/main.go @@ -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()") } diff --git a/examples/rtl8720dn/webserver/wioterminal.go b/examples/rtl8720dn/webserver/wioterminal.go index ad6163804..1867c2a17 100644 --- a/examples/rtl8720dn/webserver/wioterminal.go +++ b/examples/rtl8720dn/webserver/wioterminal.go @@ -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 init() { + display = initdisplay.InitDisplay() + display.SetRotation(ili9341.Rotation270) -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) + // override + led = machine.LCD_BACKLIGHT } diff --git a/examples/rtl8720dn/wioterminal.go b/examples/rtl8720dn/wioterminal.go index ad56b8770..356ff4ac0 100644 --- a/examples/rtl8720dn/wioterminal.go +++ b/examples/rtl8720dn/wioterminal.go @@ -9,12 +9,15 @@ import ( "runtime/interrupt" "time" + "tinygo.org/x/drivers/net" + "tinygo.org/x/drivers/net/http" "tinygo.org/x/drivers/rtl8720dn" ) var ( uart UARTx debug bool + buf [0x1000]byte ) func handleInterrupt(interrupt.Interrupt) { @@ -55,6 +58,24 @@ func Setup() (*rtl8720dn.RTL8720DN, error) { return rtl, nil } +func SetupAndConnectToAccessPoint(ssid, pass string, timeout time.Duration) (*rtl8720dn.RTL8720DN, error) { + rtl, err := Setup() + if err != nil { + return nil, err + } + + err = rtl.ConnectToAccessPoint(ssid, pass, 10*time.Second) + if err != nil { + return rtl, err + } + + net.UseDriver(rtl) + http.UseDriver(rtl) + http.SetBuf(buf[:]) + + return rtl, nil +} + // Wait for user to open serial console func waitSerial() { for !machine.Serial.DTR() {