Skip to content

Commit

Permalink
Use external TCP stack (#24)
Browse files Browse the repository at this point in the history
* begin restructuring packages

* use seqs library

* refactor API

* add working DHCP using seqs

* polling inside NIC loop

* begin TCP transactions

* keep working on TCP server

* almost done; work to be done on seqs side

* relatively sleek TCP interaction

* bump seqs version so is buildable

* bump seqs version to latest with new DHCP and reduced stack usage

* better logging API

* move legacy files

* rename cyrw->cyw43439

* move cyrw files into root directory
  • Loading branch information
soypat authored Dec 8, 2023
1 parent c2bd039 commit 5fcfc32
Show file tree
Hide file tree
Showing 76 changed files with 655 additions and 6,452 deletions.
Binary file removed Infineon-CYW43439-DataSheet-v03_00-EN.pdf
Binary file not shown.
38 changes: 12 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
# cyw43439
Driver for the Wifi+bluetooth integrated circuit on the pico.

## Contributions
PRs welcome! Please read most recent developments on [this issue](https://github.com/tinygo-org/tinygo/issues/2947) before contributing.
## Development

Compile and flash the development program to the currently connected Pico W by running:

Run DHCP example:
```shell
tinygo flash -monitor -target=pico ./shelltest/.
tinygo flash -target=pico -opt=1 -stack-size=8kb -size=short -monitor ./examples/dhcp/
```

After the program is done flashing it will enter shell-mode communications with the
Pico W. One has several commands they can run from there on out.

To initialize the board type in the following **shell command** and press enter

```
$i0
Run TCP server example:
```shell
tinygo flash -target=pico -opt=1 -stack-size=8kb -size=short -monitor ./examples/tcpserver/
```

A shell command is composed of 4 parts and serves the purpose of debugging:

1. `$`: While in shell-mode the program waits for the `$` character before parsing input.
2. The next character after the `$` is the "command" character.
3. After the "command" is the number argument, which may not have effect in
some commands
4. The <kbd>Enter</kbd> or newline.

All commands must have these 4 parts

To develop add a command under the [`TestShellmode` function (switch statement)](./shelltest/test_shellmode.go).
This command will probably contain a function or action with the CYW43439 you wish to test.

## Contributions
PRs welcome! Please read most recent developments on [this issue](https://github.com/tinygo-org/tinygo/issues/2947) before contributing.

## FYI
* [RP2040 datasheet](https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf).
Expand Down Expand Up @@ -69,3 +50,8 @@ The PWM API is much more closely matched between the [C](https://github.com/rasp

### Go and TinyGo Ethernet/IP/TCP stack comparisons
![stack comparison](stack_comparison.png)

### Install stringer command
```sh
go install golang.org/x/tools/cmd/stringer@latest
```
File renamed without changes.
3 changes: 2 additions & 1 deletion cy43439.go → _legacy_cyrw/cy43439.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import (
"sync"
"time"

"log/slog"

"github.com/soypat/cyw43439/internal/netlink"
"github.com/soypat/cyw43439/internal/slog"
"github.com/soypat/cyw43439/whd"
"tinygo.org/x/drivers"
)
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion cy43439_poll.go → _legacy_cyrw/cy43439_poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"time"

"github.com/soypat/cyw43439/internal/slog"
"log/slog"

"github.com/soypat/cyw43439/whd"
)

Expand Down
3 changes: 2 additions & 1 deletion cy_io.go → _legacy_cyrw/cy_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"errors"
"machine"

"github.com/soypat/cyw43439/internal/slog"
"log/slog"

"github.com/soypat/cyw43439/whd"
)

Expand Down
3 changes: 2 additions & 1 deletion cy_ioctl.go → _legacy_cyrw/cy_ioctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"time"
"unsafe"

"github.com/soypat/cyw43439/internal/slog"
"log/slog"

"github.com/soypat/cyw43439/whd"
)

Expand Down
5 changes: 3 additions & 2 deletions cyw43439_wifi.go → _legacy_cyrw/cyw43439_wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"time"
"unsafe"

"log/slog"

"github.com/soypat/cyw43439/internal/netlink"
"github.com/soypat/cyw43439/internal/slog"
"github.com/soypat/cyw43439/whd"
)

Expand Down Expand Up @@ -119,7 +120,7 @@ func (d *Device) wifiLinkStatus(itf uint8) (linkStat int32) {
func (d *Device) wifiJoin(ssid, key string, bssid *[6]byte, authType, channel uint32) (err error) {
defer func() {
if err != nil {
d.logError("wifiJoin:failed", slog.Any("err", err))
d.logError("wifiJoin:failed", slog.String("err", err.Error()))
} else {
d.info("wifiJoin:success")
}
Expand Down
71 changes: 71 additions & 0 deletions _legacy_cyrw/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build tinygo

package cyw43439

import (
"context"
"io"
"machine"

"log/slog"
)

const (
verbose_debug = true
initReadback = false
validateDownloads = false
LevelDebugIO = slog.LevelDebug
defaultLevel = LevelDebugIO
)

func (d *Device) debugIO(msg string, attrs ...slog.Attr) {
d.log.LogAttrs(context.Background(), LevelDebugIO, msg, attrs...)
}

func (d *Device) debug(msg string, attrs ...slog.Attr) {
d.log.LogAttrs(context.Background(), slog.LevelDebug, msg, attrs...)
}

func (d *Device) info(msg string, attrs ...slog.Attr) {
d.log.LogAttrs(context.Background(), slog.LevelInfo, msg, attrs...)
}

func (d *Device) logError(msg string, attrs ...slog.Attr) {
d.log.LogAttrs(context.Background(), slog.LevelError, msg, attrs...)
}

func (d *Device) SetLogger(log *slog.Logger) {
d.log = log
}

func _setDefaultLogger(d *Device) {
writer := machine.Serial
handler := slog.NewTextHandler(writer, &slog.HandlerOptions{Level: defaultLevel})
// Small slog handler implemented on our side:
// smallHandler := &handler{w: writer, level: slog.LevelDebug}
d.log = slog.New(handler)
}

var _ slog.Handler = (*handler)(nil)

// handler implements slog.Handler interface minimally.
type handler struct {
level slog.Level
w io.Writer
buf [256]byte
}

func (h *handler) Enabled(_ context.Context, level slog.Level) bool { return level >= h.level }

func (h *handler) Handle(_ context.Context, r slog.Record) error {
n := copy(h.buf[:len(h.buf)-2], r.Message)
h.buf[n] = '\n'
_, err := h.w.Write(h.buf[:n+1])
return err
}
func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
return nil
}
func (h *handler) WithGroup(name string) slog.Handler {
return nil
}
File renamed without changes.
16 changes: 6 additions & 10 deletions cyrw/firmware_embed.go → _legacy_cyrw/firmware_embed.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package cyrw
package cyw43439

import _ "embed"

// This file is here to speedup the IDE
// when developing.

var (
//go:embed firmware/43439A0_clm.bin
clmFW string
//go:embed firmware/43439A0.bin
wifiFW2 string

// Of raw size 225240.
//go:embed firmware/wififw.bin
//go:embed cyrw/firmware/wififw.bin
wifiFW string
// Of raw size 232408 bytes
//go:embed firmware/wifibtfw.bin
//go:embed cyrw/firmware/wifibtfw.bin
wifibtFW string
// Of raw size 6164 bytes.
//go:embed firmware/btfw.bin
//go:embed cyrw/firmware/btfw.bin
btFW string
)

const (
wifiFWLen = 224190
wifibtFWLen = 231077
clmLen = 984
_nvramlen = len(nvram43439)
)

const nvram43439 = "NVRAMRev=$Rev$" + "\x00" +
Expand Down Expand Up @@ -74,7 +70,7 @@ const nvram43439 = "NVRAMRev=$Rev$" + "\x00" +
"spurconfig=0x3" + "\x00" +
"glitch_based_crsmin=1" + "\x00" +
"btc_mode=1" + "\x00" +
"\x00\x00" // C includes null terminator in strings.
"\x00\x00\x00" // C includes null terminator in strings.

/*
var (
Expand Down
File renamed without changes.
21 changes: 11 additions & 10 deletions netlink.go → _legacy_cyrw/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net"
"time"

"log/slog"

"github.com/soypat/cyw43439/internal/netlink"
"github.com/soypat/cyw43439/internal/slog"
"github.com/soypat/cyw43439/whd"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func (d *Device) connectToAP() error {

err := d.WifiConnectTimeout(d.params.SSID, d.params.Passphrase, auth, timeout)
if err != nil {
d.logError("connectToAP:WifiConnectTimeout", slog.Any("err", err))
d.logError("connectToAP:WifiConnectTimeout", slog.String("err", err.Error()))
return err
}
d.info("connected to AP", slog.String("SSID", d.params.SSID))
Expand All @@ -64,14 +65,14 @@ func (d *Device) showIP() {
func (d *Device) netConnect(reset bool) error {
if reset {
/*
country := d.params.Country
if country == "" {
country = "XX"
}
code := whd.CountryCode(country, 0)
if err := d.EnableStaMode(code); err != nil {
return err
}
country := d.params.Country
if country == "" {
country = "XX"
}
code := whd.CountryCode(country, 0)
if err := d.EnableStaMode(code); err != nil {
return err
}
*/
}

Expand Down
4 changes: 2 additions & 2 deletions cyrw/bus.go → bus.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cyrw
package cy43439

// File based on mainly on bus.rs from the reference
// https://github.com/embassy-rs/embassy/blob/26870082427b64d3ca42691c55a2cded5eadc548/cyw43/src/bus.rs
Expand All @@ -7,11 +7,11 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"log/slog"
"reflect"
"time"
"unsafe"

"github.com/soypat/cyw43439/internal/slog"
"github.com/soypat/cyw43439/whd"
"golang.org/x/exp/constraints"
)
Expand Down
6 changes: 3 additions & 3 deletions cyrw/bus_pico_pio.go → bus_pico_pio.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build pico && !cy43nopio

package cyrw
package cy43439

import (
"encoding/binary"
Expand All @@ -13,11 +13,11 @@ import (
var _busOrder = binary.LittleEndian

type spibus struct {
cs OutputPin
cs outputPin
spi piolib.SPI3w
}

func DefaultNew() *Device {
func NewPicoWDevice() *Device {
// Raspberry Pi Pico W pin definitions for the CY43439.
const (
// IRQ = machine.GPIO24 // AKA WL_HOST_WAKE
Expand Down
Loading

0 comments on commit 5fcfc32

Please sign in to comment.