Add network device driver model, netdev

This commit is contained in:
Scott Feldman
2023-03-28 12:18:59 -07:00
committed by Ron Evans
parent 00b1b4fbfb
commit 276feecc20
128 changed files with 6389 additions and 15539 deletions
+23
View File
@@ -0,0 +1,23 @@
//go:build challenger_rp2040
// +build: challenger_rp2040
package main
import (
"machine"
"tinygo.org/x/drivers/espat"
)
var cfg = espat.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// UART
Uart: machine.UART1,
Tx: machine.UART1_TX_PIN,
Rx: machine.UART1_RX_PIN,
}
var netdev = espat.New(&cfg)
+104
View File
@@ -0,0 +1,104 @@
// This is an example of an NTP client.
//
// It creates a UDP connection to request the current time and parse the
// response from a NTP server. The system time is set to NTP time.
package main
import (
"fmt"
"io"
"log"
"machine"
"net"
"runtime"
"time"
)
var (
ssid string
pass string
// IP address of the server aka "hub". Replace with your own info.
ntpHost string = "0.pool.ntp.org:123"
)
const NTP_PACKET_SIZE = 48
var response = make([]byte, NTP_PACKET_SIZE)
func main() {
waitSerial()
if err := netdev.NetConnect(); err != nil {
log.Fatal(err)
}
conn, err := net.Dial("udp", ntpHost)
if err != nil {
log.Fatal(err)
}
println("Requesting NTP time...")
t, err := getCurrentTime(conn)
if err != nil {
log.Fatal(fmt.Sprintf("Error getting current time: %v", err))
} else {
message("NTP time: %v", t)
}
conn.Close()
netdev.NetDisconnect()
runtime.AdjustTimeOffset(-1 * int64(time.Since(t)))
for {
message("Current time: %v", time.Now())
time.Sleep(time.Minute)
}
}
// Wait for user to open serial console
func waitSerial() {
for !machine.Serial.DTR() {
time.Sleep(100 * time.Millisecond)
}
}
func getCurrentTime(conn net.Conn) (time.Time, error) {
if err := sendNTPpacket(conn); err != nil {
return time.Time{}, err
}
n, err := conn.Read(response)
if err != nil && err != io.EOF {
return time.Time{}, err
}
if n != NTP_PACKET_SIZE {
return time.Time{}, fmt.Errorf("expected NTP packet size of %d: %d", NTP_PACKET_SIZE, n)
}
return parseNTPpacket(response), nil
}
func sendNTPpacket(conn net.Conn) error {
var request = [48]byte{
0xe3,
}
_, err := conn.Write(request[:])
return err
}
func parseNTPpacket(r []byte) time.Time {
// the timestamp starts at byte 40 of the received packet and is four bytes,
// this is NTP time (seconds since Jan 1 1900):
t := uint32(r[40])<<24 | uint32(r[41])<<16 | uint32(r[42])<<8 | uint32(r[43])
const seventyYears = 2208988800
return time.Unix(int64(t-seventyYears), 0)
}
func message(format string, args ...interface{}) {
println(fmt.Sprintf(format, args...), "\r")
}
+29
View File
@@ -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)
+33
View File
@@ -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)