-
Notifications
You must be signed in to change notification settings - Fork 230
feat: add w5500 driver #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
819bcea
feat: add w5500 driver
nrwiersma 0ff733f
fix: cr suggestions
nrwiersma 0f40111
fix: use HAL
nrwiersma 99571a9
fix: remove unneeded type
nrwiersma d08fc78
fix: remove debugging
nrwiersma bdad634
fix: cr suggestions
nrwiersma c91147e
fix: cr suggstion
nrwiersma e65adf2
feat: add go doc
nrwiersma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! netip is the best!