Skip to content

Commit 457af75

Browse files
committed
cyw43439: HCI implementation
Signed-off-by: deadprogram <[email protected]>
1 parent 9905abd commit 457af75

File tree

14 files changed

+201
-21
lines changed

14 files changed

+201
-21
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ smoketest-tinygo:
4444
@md5sum test.hex
4545
$(TINYGO) build -o test.uf2 -size=short -target=circuitplay-express -tags="hci hci_uart" ./examples/advertisement
4646
@md5sum test.hex
47+
$(TINYGO) build -o test.uf2 -size=short -target=pico-w ./examples/discover
48+
@md5sum test.hex
49+
$(TINYGO) build -o test.uf2 -size=short -target=badger2040-w ./examples/advertisement
50+
@md5sum test.hex
4751

4852
smoketest-linux:
4953
# Test on Linux.

adapter_cyw43439.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//go:build cyw43439
2+
3+
package bluetooth
4+
5+
import (
6+
"machine"
7+
8+
"log/slog"
9+
10+
"github.com/soypat/cyw43439"
11+
)
12+
13+
const maxConnections = 1
14+
15+
// Adapter represents a SPI connection to the HCI controller on an attached CYW4349 module.
16+
type Adapter struct {
17+
hciAdapter
18+
}
19+
20+
// DefaultAdapter is the default adapter on the current system.
21+
//
22+
// Make sure to call Enable() before using it to initialize the adapter.
23+
var DefaultAdapter = &Adapter{
24+
hciAdapter: hciAdapter{
25+
isDefault: true,
26+
connectHandler: func(device Device, connected bool) {
27+
return
28+
},
29+
connectedDevices: make([]Device, 0, maxConnections),
30+
},
31+
}
32+
33+
// Enable configures the BLE stack. It must be called before any
34+
// Bluetooth-related calls (unless otherwise indicated).
35+
func (a *Adapter) Enable() error {
36+
if debug {
37+
println("Initializing CYW43439 device")
38+
}
39+
40+
dev := cyw43439.NewPicoWDevice()
41+
cfg := cyw43439.DefaultBluetoothConfig()
42+
if debug {
43+
cfg.Logger = slog.New(slog.NewTextHandler(machine.USBCDC, &slog.HandlerOptions{
44+
Level: slog.LevelDebug - 2,
45+
}))
46+
}
47+
48+
err := dev.Init(cfg)
49+
if err != nil {
50+
if debug {
51+
println("Error initializing CYW43439 device", err.Error())
52+
}
53+
return err
54+
}
55+
56+
transport := &hciSPI{dev: dev}
57+
58+
a.hci, a.att = newBLEStack(transport)
59+
if debug {
60+
println("Enabling CYW43439 device")
61+
}
62+
63+
a.enable()
64+
65+
if debug {
66+
println("Enabled CYW43439 device")
67+
}
68+
69+
return nil
70+
}
71+
72+
type hciSPI struct {
73+
dev *cyw43439.Device
74+
}
75+
76+
func (h *hciSPI) startRead() {
77+
}
78+
79+
func (h *hciSPI) endRead() {
80+
}
81+
82+
func (h *hciSPI) Buffered() int {
83+
return h.dev.BufferedHCI()
84+
}
85+
86+
func (h *hciSPI) ReadByte() (byte, error) {
87+
var buf [1]byte
88+
89+
r, err := h.dev.HCIReadWriter()
90+
if err != nil {
91+
return 0, err
92+
}
93+
if _, err := r.Read(buf[:]); err != nil {
94+
return 0, err
95+
}
96+
97+
return buf[0], nil
98+
}
99+
100+
func (h *hciSPI) Read(buf []byte) (int, error) {
101+
r, err := h.dev.HCIReadWriter()
102+
if err != nil {
103+
return 0, err
104+
}
105+
106+
return r.Read(buf)
107+
}
108+
109+
func (h *hciSPI) Write(buf []byte) (int, error) {
110+
w, err := h.dev.HCIReadWriter()
111+
if err != nil {
112+
return 0, err
113+
}
114+
115+
return w.Write(buf)
116+
}

adapter_hci.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build hci || ninafw
1+
//go:build hci || ninafw || cyw43439
22

33
package bluetooth
44

@@ -25,9 +25,18 @@ type hciAdapter struct {
2525
}
2626

2727
func (a *hciAdapter) enable() error {
28-
a.hci.start()
28+
if err := a.hci.start(); err != nil {
29+
if debug {
30+
println("error starting HCI:", err.Error())
31+
}
32+
return err
33+
}
2934

3035
if err := a.hci.reset(); err != nil {
36+
if debug {
37+
println("error resetting HCI:", err.Error())
38+
}
39+
3140
return err
3241
}
3342

adapter_hci_uart.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func (h *hciUART) ReadByte() (byte, error) {
9696
return h.uart.ReadByte()
9797
}
9898

99+
func (h *hciUART) Read(buf []byte) (int, error) {
100+
return h.uart.Read(buf)
101+
}
102+
99103
const writeAttempts = 200
100104

101105
func (h *hciUART) Write(buf []byte) (int, error) {

adapter_ninafw.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func (h *hciUART) ReadByte() (byte, error) {
108108
return h.uart.ReadByte()
109109
}
110110

111+
func (h *hciUART) Read(buf []byte) (int, error) {
112+
return h.uart.Read(buf)
113+
}
114+
111115
const writeAttempts = 200
112116

113117
func (h *hciUART) Write(buf []byte) (int, error) {

att_hci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build hci || ninafw
1+
//go:build hci || ninafw || cyw43439
22

33
package bluetooth
44

gap_hci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build hci || ninafw
1+
//go:build hci || ninafw || cyw43439
22

33
package bluetooth
44

gattc_hci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build hci || ninafw
1+
//go:build hci || ninafw || cyw43439
22

33
package bluetooth
44

gatts_hci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build hci || ninafw
1+
//go:build hci || ninafw || cyw43439
22

33
package bluetooth
44

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module tinygo.org/x/bluetooth
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/go-ole/go-ole v1.2.6
77
github.com/godbus/dbus/v5 v5.1.0
88
github.com/saltosystems/winrt-go v0.0.0-20240509164145-4f7860a3bd2b
9+
github.com/soypat/cyw43439 v0.0.0-20240521202811-13f2f2d46d64
910
github.com/tinygo-org/cbgo v0.0.4
1011
golang.org/x/crypto v0.12.0
1112
tinygo.org/x/drivers v0.26.1-0.20230922160320-ed51435c2ef6
@@ -16,6 +17,9 @@ require (
1617
require (
1718
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
1819
github.com/sirupsen/logrus v1.9.3 // indirect
20+
github.com/soypat/seqs v0.0.0-20240509190925-a6350cee83a7 // indirect
21+
github.com/tinygo-org/pio v0.0.0-20231216154340-cd888eb58899 // indirect
22+
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 // indirect
1923
golang.org/x/sys v0.11.0 // indirect
2024
golang.org/x/term v0.11.0 // indirect
2125
)

0 commit comments

Comments
 (0)