mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-10 18:03:41 +00:00
Add network device driver model, netdev
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
// This example uses TCP to send an HTTP request to retrieve a webpage. The
|
||||
// HTTP request is hand-rolled to avoid the overhead of using http.Get() from
|
||||
// the "net/http" package. See example/net/http-get for the full http.Get()
|
||||
// functionality.
|
||||
//
|
||||
// Example HTTP server:
|
||||
// ---------------------------------------------------------------------------
|
||||
// package main
|
||||
//
|
||||
// import "net/http"
|
||||
//
|
||||
// func main() {
|
||||
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// w.Write([]byte("hello"))
|
||||
// })
|
||||
// http.ListenAndServe(":8080", nil)
|
||||
// }
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"machine"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ssid string
|
||||
pass string
|
||||
// HTTP server address to hit with a GET / request
|
||||
address string = "10.0.0.100:8080"
|
||||
)
|
||||
|
||||
var conn net.Conn
|
||||
|
||||
// Wait for user to open serial console
|
||||
func waitSerial() {
|
||||
for !machine.Serial.DTR() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func dialConnection() {
|
||||
var err error
|
||||
|
||||
println("\r\n---------------\r\nDialing TCP connection")
|
||||
conn, err = net.Dial("tcp", address)
|
||||
for ; err != nil; conn, err = net.Dial("tcp", address) {
|
||||
println("Connection failed:", err.Error())
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
println("Connected!\r")
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
println("Hit an error:", err.Error())
|
||||
panic("BYE")
|
||||
}
|
||||
}
|
||||
|
||||
func makeRequest() {
|
||||
println("Sending HTTP request...")
|
||||
w := bufio.NewWriter(conn)
|
||||
fmt.Fprintln(w, "GET / HTTP/1.1")
|
||||
fmt.Fprintln(w, "Host:", strings.Split(address, ":")[0])
|
||||
fmt.Fprintln(w, "User-Agent: TinyGo")
|
||||
fmt.Fprintln(w, "Connection: close")
|
||||
fmt.Fprintln(w)
|
||||
check(w.Flush())
|
||||
println("Sent!\r\n\r")
|
||||
}
|
||||
|
||||
func readResponse() {
|
||||
r := bufio.NewReader(conn)
|
||||
resp, err := io.ReadAll(r)
|
||||
check(err)
|
||||
println(string(resp))
|
||||
}
|
||||
|
||||
func closeConnection() {
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
waitSerial()
|
||||
|
||||
if err := netdev.NetConnect(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 0; ; i++ {
|
||||
dialConnection()
|
||||
makeRequest()
|
||||
readResponse()
|
||||
closeConnection()
|
||||
println("--------", i, "--------\r\n")
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//go:build wioterminal
|
||||
|
||||
// +build: wioterminal
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/rtl8720dn"
|
||||
)
|
||||
|
||||
var cfg = rtl8720dn.Config{
|
||||
// WiFi AP credentials
|
||||
Ssid: ssid,
|
||||
Passphrase: pass,
|
||||
// Device
|
||||
En: machine.RTL8720D_CHIP_PU,
|
||||
// UART
|
||||
Uart: machine.UART3,
|
||||
Tx: machine.PB24,
|
||||
Rx: machine.PC24,
|
||||
Baudrate: 614400,
|
||||
// Watchdog (set to 0 to disable)
|
||||
WatchdogTimeo: time.Duration(20 * time.Second),
|
||||
}
|
||||
|
||||
var netdev = rtl8720dn.New(&cfg)
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
|
||||
|
||||
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/wifinina"
|
||||
)
|
||||
|
||||
var cfg = wifinina.Config{
|
||||
// WiFi AP credentials
|
||||
Ssid: ssid,
|
||||
Passphrase: pass,
|
||||
// Configure SPI for 8Mhz, Mode 0, MSB First
|
||||
Spi: machine.NINA_SPI,
|
||||
Freq: 8 * 1e6,
|
||||
Sdo: machine.NINA_SDO,
|
||||
Sdi: machine.NINA_SDI,
|
||||
Sck: machine.NINA_SCK,
|
||||
// Device pins
|
||||
Cs: machine.NINA_CS,
|
||||
Ack: machine.NINA_ACK,
|
||||
Gpio0: machine.NINA_GPIO0,
|
||||
Resetn: machine.NINA_RESETN,
|
||||
// Watchdog (set to 0 to disable)
|
||||
WatchdogTimeo: time.Duration(20 * time.Second),
|
||||
}
|
||||
|
||||
var netdev = wifinina.New(&cfg)
|
||||
Reference in New Issue
Block a user