Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions examples/w5500/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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(machine.SPI0, machine.GPIO17)
eth.Configure(w5500.Config{
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 @@ -145,6 +145,7 @@ tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpme
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/si5351/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
104 changes: 104 additions & 0 deletions w5500/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package w5500

import "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 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)

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
_ = 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