Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/w5500/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"machine"
"net"
"net/netip"
"time"

"tinygo.org/x/drivers/netdev"
"tinygo.org/x/drivers/w5500"
)

func main() {
machine.SPI0.Configure(machine.SPIConfig{
Frequency: 33 * machine.MHz,
})
machine.GPIO17.Configure(machine.PinConfig{Mode: machine.PinOutput})

eth := w5500.New()
eth.Configure(w5500.Config{
SPI: machine.SPI0,
CS: machine.GPIO17.Set,
MAC: net.HardwareAddr{0xee, 0xbe, 0xe9, 0xa9, 0xb6, 0x4f},
IP: netip.AddrFrom4([4]byte{192, 168, 1, 2}),
SubnetMask: netip.AddrFrom4([4]byte{255, 255, 255, 0}),
Gateway: netip.AddrFrom4([4]byte{192, 168, 1, 1}),
})
netdev.UseNetdev(eth)

for {
if eth.LinkStatus() != w5500.LinkStatusUp {
println("Waiting for link to be up")

time.Sleep(1 * time.Second)
continue
}
break
}
}
1 change: 1 addition & 0 deletions smoketest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/mai
tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go
tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/max6675/main.go
tinygo build -size short -o ./build/test.hex -target=pico ./examples/ens160/main.go
tinygo build -size short -o ./build/test.hex -target=pico ./examples/w5500/main.go
# network examples (espat)
tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
# network examples (wifinina)
Expand Down
15 changes: 15 additions & 0 deletions w5500/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package w5500

type debug uint8

const (
debugNetdev debug = 1 << iota // show netdev entry points
debugDetail // show chatty w5500 cmds

debugOff = 0
debugAll = debugNetdev | debugDetail
)

func debugging(want debug) bool {
return (_debug & want) != 0
}
116 changes: 116 additions & 0 deletions w5500/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package w5500

import (
"fmt"
"time"
)

func (d *Device) irqPoll(sockn uint8, state uint8, deadline time.Time) uint8 {
waitTime := 500 * time.Microsecond
for {
if !deadline.IsZero() && time.Now().After(deadline) {
if debugging(debugDetail) {
fmt.Println(time.Now(), deadline)
fmt.Printf("[Socket %d] Polling for IRQ %08b timed out.\r\n", sockn, state)
}

// If a deadline is set and it has passed, return 0.
return sockIntUnknown
}

irq := d.readByte(sockInt, sockAddr(sockn)) & 0b00011111
if got := irq & state; got != 0 {
// Acknowledge the interrupt.
d.writeByte(sockInt, sockAddr(sockn), got)

if debugging(debugDetail) {
fmt.Printf("[Socket %d] Got IRQ %08b\r\n", sockn, got)
}

return got
}

d.mu.Unlock()

time.Sleep(waitTime)

// Exponential backoff for polling.
waitTime *= 2
if waitTime > 10*time.Millisecond {
waitTime = 10 * time.Millisecond
}

d.mu.Lock()
}
}

func (d *Device) read(addr uint16, bsb uint8, p []byte) {
d.cs(false)
if len(p) == 0 {
return
}

d.sendReadHeader(addr, bsb)
_ = d.bus.Tx(nil, p)
d.cs(true)
}

func (d *Device) readUint16(addr uint16, bsb uint8) uint16 {
d.cs(false)
d.sendReadHeader(addr, bsb)
buf := d.cmdBuf
_ = d.bus.Tx(nil, buf[:2])
d.cs(true)
return uint16(buf[1]) | uint16(buf[0])<<8
}

func (d *Device) readByte(addr uint16, bsb uint8) byte {
d.cs(false)
d.sendReadHeader(addr, bsb)
r, _ := d.bus.Transfer(byte(0))
d.cs(true)
return r
}

func (d *Device) write(addr uint16, bsb uint8, p []byte) {
d.cs(false)
if len(p) == 0 {
return
}
d.sendWriteHeader(addr, bsb)
_ = d.bus.Tx(p, nil)
d.cs(true)
}

func (d *Device) writeUint16(addr uint16, bsb uint8, v uint16) {
d.cs(false)
d.sendWriteHeader(addr, bsb)
buf := d.cmdBuf
buf[0] = byte(v >> 8)
buf[1] = byte(v & 0xff)
_ = d.bus.Tx(buf[:2], nil)
d.cs(true)
}

func (d *Device) writeByte(addr uint16, bsb uint8, b byte) {
d.cs(false)
d.sendWriteHeader(addr, bsb)
_, _ = d.bus.Transfer(b)
d.cs(true)
}

func (d *Device) sendReadHeader(addr uint16, bsb uint8) {
buf := d.cmdBuf
buf[0] = byte(addr >> 8)
buf[1] = byte(addr & 0xff)
buf[2] = bsb<<3 | 0b000
_ = d.bus.Tx(buf, nil)
}

func (d *Device) sendWriteHeader(addr uint16, bsb uint8) {
buf := d.cmdBuf
buf[0] = byte(addr >> 8)
buf[1] = byte(addr & 0xff)
buf[2] = bsb<<3 | 0b100
_ = d.bus.Tx(buf, nil)
}
Loading