Skip to content

unor4wifi: add WiFi driver for the Arduino UNO R4 WiFi - #940

Merged
dgryski merged 5 commits into
tinygo-org:devfrom
Frelsaren:unor4wifi
Nov 15, 2026
Merged

dgryski merged 5 commits into
tinygo-org:devfrom
Frelsaren:unor4wifi

Conversation

@Frelsaren

@Frelsaren Frelsaren commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Talks AT commands over UART to the ESP32-S3 bridge firmware and implements netlink and netdev. Supports STA and AP mode, TCP client and server, UDP and DNS. Adds a netlink probe and enables the small net examples for the arduino_uno_r4_wifi build tag.

Tested by serving http server and misc wifi discovery.

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I compared the driver against WiFiS3 and the bridge firmware, and ran the AP path on an UNO R4 WiFi (bridge 0.6.0). AP mode is broken (inline comment), and the AT argument escaping needs work before merge. The rest are smaller.

I also saw the ntpclient smoketest build fail to link for arduino-uno-r4-wifi with __atomic_fetch_sub_8 missing, using a local tinygo build. That may be specific to my tree, so please check it against the tinygo version CI uses.

Comment thread unor4wifi/unor4wifi.go Outdated
// Channel 1 is the WiFiS3 default.
cmd := "AT+BEGINSOFTAP=" + escapeArg(p.Ssid) + "," + escapeArg(p.Passphrase) + ",1"
n, err := d.modem.execInt(cmd)
if err != nil || n != wlAPListening {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AP mode always fails here. The bridge returns the bool from WiFi.softAP(), so success is 1. WiFiS3 checks == 1 too. wlAPListening (7) is a status value, not the return value.

On hardware the log shows +BEGINSOFTAP: 1 then OK, and the driver still returns ErrConnectFailed. Changing the check to n != 1 gives CONNECTED with IP 192.168.4.1.

Comment thread unor4wifi/unor4wifi.go
// escapeArg escapes the characters that the bridge argument parser treats
// as special. See uno-r4-wifi-usb-bridge/UNOR4USBBridge/parser.cpp,
// ATParser::parse, state Argument.
func escapeArg(s string) string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not escape CR or LF. In the bridge parser (parser.cpp) the \n check runs before the escape handling, so a newline in an SSID or passphrase ends the command early. Please reject control characters in user supplied strings, or return an error.

Comment thread unor4wifi/unor4wifi.go Outdated
d.mu.Lock()
defer d.mu.Unlock()

res, err := d.modem.exec("AT+HOSTBYNAME=" + name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name is not escaped here. A comma or quote adds extra arguments. The same applies to host in the AT+CLIENTCONNECT command in Connect. Please run both through escapeArg (plus the control character check).

Comment thread unor4wifi/unor4wifi.go
return netip.AddrPortFrom(ip, uint16(port))
}

func (d *device) Accept(sockfd int) (int, netip.AddrPort, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two problems in Accept.

  1. Any error from SERVERACCEPT (ERROR or a modem timeout) is treated as no connection, so it loops forever with no deadline and no error.
  2. If newSocket fails after the firmware accepted a client, that firmware socket is never closed and leaks.

Please return real errors and send AT+CLIENTCLOSE on the failure path.

Comment thread unor4wifi/unor4wifi.go
return nil
}

func (d *device) NetDisconnect() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NetDisconnect leaves d.sockets as is, and NetConnect calls reset() (SOFTRESETWIFI), which clears the firmware sockets. After a reconnect the Go table can hold entries whose modemSock no longer exists. Please clear the table on disconnect or reset.

Comment thread unor4wifi/unor4wifi.go Outdated
}
err = d.connectAP()
default:
return netlink.ErrConnectModeNoGood

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mode check happens after setupUART and reset. Validating ConnectMode at the top of NetConnect avoids the side effects for a bad mode.

Comment thread unor4wifi/unor4wifi.go Outdated
dns netip.Addr

sockets [maxSockets]*socket
rxbuf [maxChunk]byte

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rxbuf is never used. It costs 1 KB of the 32 KB of RAM.

Comment thread unor4wifi/unor4wifi.go Outdated
}

// The firmware resolves names and dotted addresses through the same
// path, so always use +CLIENTCONNECT like the Arduino WiFiS3 library.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not quite right. WiFiS3 only uses +CLIENTCONNECT when a connection timeout is set and otherwise uses +CLIENTCONNECTNAME. The firmware does accept +CLIENTCONNECT with a timeout argument, so the code works. Please fix the comment.

Comment thread unor4wifi/modem.go
}

// readLine reads a line and strips the trailing CR LF.
func (m *modem) readLine(deadline time.Time) (string, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m.line can grow until the deadline if no \n arrives. At 115200 baud and a 10 s timeout that is far more than the RAM available. Please cap the line length.

@deadprogram

Copy link
Copy Markdown
Member

How is this different from https://github.com/tinygo-org/drivers/tree/dev/espat ?

@Frelsaren

Copy link
Copy Markdown
Contributor Author

How is this different from https://github.com/tinygo-org/drivers/tree/dev/espat ?

The Uno R4 WiFi runs a custom firmware on it's esp32 so espat doesn't work on this board. Could probably reflash the esp32 but I believe keeping it as is would be better.

@Frelsaren

Copy link
Copy Markdown
Contributor Author

@dgryski Thanks for your review. I have done some changes to cover your comments.

@dgryski

dgryski commented Nov 14, 2026

Copy link
Copy Markdown
Member

Thanks for the fixes. I re-read the new commit against the bridge firmware source and ran it on an UNO R4 WiFi (bridge 0.6.0, ddb6a1b).

Earlier comments

All of them are addressed. AP mode now returns an address (192.168.4.1), control characters are rejected before any modem traffic, DNS names and connect hosts are escaped, Accept returns errors and closes the leaked socket, the mode check runs first, rxbuf is gone and the line length is capped.

Hardware results

20 checks pass: STA connect on a real network, DNS (including a comma in the name and CRLF rejection), TCP GET from example.com, AP start, and UDP socket open, close and reuse.

Please remove fmt

The driver uses fmt.Printf only for the banner and the debug traces. Replacing those with the builtin print took the same test program from 85452 to 48512 bytes of flash (about 43% smaller), with the same console output. Addresses just need .String(). This helps anything that doesn't import fmt itself. The net examples import it already, so they won't change.

Remaining issues

I reproduced each of these on the board. None of them is a crash or data bug, so I'd count them as polish or follow-ups.

  1. Failed connect shows up as a timeout. A wrong password or an unknown SSID takes the full ConnectTimeout and returns ErrConnectTimeout. The bridge never reports a failed connect: a disconnect event just sets status 0. It does write the WiFi.begin() result as the +BEGINSTA response, which the driver ignores. That value may be worth checking.
  2. Link loss isn't detected. If the station link drops, EventNetDown never fires, because nothing polls the status after connecting.
  3. Short AP passphrase. checkParams only checks passphrase length for STA. In AP mode the firmware rejects anything that isn't empty or 8 to 63 characters, and the caller gets a generic ErrConnectFailed. Please return ErrShortPassphrase instead.
  4. Socket limits. The firmware has 8 clients, 4 servers and 4 UDP sockets, and the driver allows 8 sockets of any kind. The fifth UDP socket fails with the raw modem returned ERROR. It would be nicer to return ErrNoMoreSockets.
  5. NetDisconnect doesn't clear d.sockets. The table is cleared on the next NetConnect, but until then Recv on an old fd waits for its deadline and returns i/o timeout instead of ErrInvalidSocketFd.

Other notes

  • Stack size. Without -stack-size 4kb the driver hangs with no output or panic. The README mentions it. A comment in the examples, or a check, would save the next person the debugging.
  • ntpclient link failure. The smoketest failed to link with __atomic_fetch_sub_8 missing. This is a TinyGo runtime gap, not a driver bug. runtime.AdjustTimeOffset with a negative offset compiles to a 64-bit atomic subtract, and the generated baremetal atomics only had add, and and or. The fix is in runtime: add atomic sub for MCUs without native atomics tinygo#6331. Until it lands, the ntpclient smoketest line will fail on a TinyGo that includes commit 743bf45e.
  • Self-test. I have a hardware self-test covering the checks above that I can share. It could go in as examples/unor4wifi-selftest.

@Frelsaren

Copy link
Copy Markdown
Contributor Author

New round of changes has been pushed covering your mentioned issues and notes.

@Frelsaren
Frelsaren requested a review from dgryski November 15, 2026 10:05

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Can you please rebase and fix the conflict on the smoketest script so this can be merged?

Thanks for working on this! Having wifi support for this popular board is a great addition.

Talks AT commands over UART to the ESP32-S3 bridge firmware and
implements netlink and netdev. Supports STA and AP mode, TCP client
and server, UDP and DNS. Adds a netlink probe and enables the small
net examples for the arduino_uno_r4_wifi build tag.
Add an optional watchdog that reports EventNetDown and EventNetUp and
reconnects in station mode. Fail fast with ErrConnectFailed when the
SSID is not found. Allow an open AP with an empty passphrase and clear
the socket table in NetDisconnect.
@Frelsaren

Copy link
Copy Markdown
Contributor Author

Done, thank you for your valuable input.

@dgryski
dgryski merged commit a8e338a into tinygo-org:dev Nov 15, 2026
1 check passed
@Frelsaren
Frelsaren deleted the unor4wifi branch November 15, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants