Skip to content

Commit 731e738

Browse files
committed
ninafw: Adding init options to enable customizing hardware
1 parent dc7d1b4 commit 731e738

5 files changed

+95
-20
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ smoketest-tinygo:
4242
@md5sum test.hex
4343
$(TINYGO) build -o test.uf2 -size=short -target=nano-rp2040 ./examples/advertisement
4444
@md5sum test.hex
45+
$(TINYGO) build -o test.uf2 -size=short -target=feather-m4 -tags="ninafw ninafw_featherwing_init" ./examples/advertisement
46+
@md5sum test.hex
47+
$(TINYGO) build -o test.uf2 -size=short -target=pybadge ./examples/advertisement
48+
@md5sum test.hex
4549

4650
smoketest-linux:
4751
# Test on Linux.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ For example, this command can be used to compile and flash an Arduino Nano RP204
284284

285285
tinygo flash -target nano-rp2040 ./examples/heartrate
286286

287+
When using the AirLift WiFi Featherwing with one of Adafruit's feather boards, you can use `ninafw ninafw_featherwing_init` build tags to set up the hardware using the default pins. Make sure to [connect the solder pads on the underside of the board](https://learn.adafruit.com/adafruit-airlift-featherwing-esp32-wifi-co-processor-featherwing/pinouts#spi-and-control-pins-3029450) in order to enable BLE support (see "Optional Control Pins" section).
288+
289+
To use ninafw with other boards, you will need to use the `ninafw` build tag as well as configure the pins and UART for communicating with the ESP32 module by configuring the `AdapterConfig` package variable before calling `DefaultAdapter.Enable()`. See [`adapter_ninafw-featherwing.go`](adapter_ninafw-featherwing.go) for an example of setting the hardware configuration options.
290+
287291
If you want more information about the `nina-fw` firmware, or want to add support for other ESP32-equipped boards, please see https://github.com/arduino/nina-fw
288292

289293
## API stability

adapter_ninafw-featherwing.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build ninafw && ninafw_featherwing_init
2+
3+
package bluetooth
4+
5+
import (
6+
"machine"
7+
)
8+
9+
func init() {
10+
AdapterConfig = NINAConfig{
11+
UART: machine.DefaultUART,
12+
CS: machine.D13,
13+
ACK: machine.D11,
14+
GPIO0: machine.D10,
15+
RESETN: machine.D12,
16+
CTS: machine.D11, // same as ACK
17+
RTS: machine.D10, // same as GPIO0
18+
BaudRate: 115200,
19+
ResetInverted: true,
20+
SoftFlowControl: true,
21+
}
22+
}

adapter_ninafw-machine.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build ninafw && ninafw_machine_init
2+
3+
package bluetooth
4+
5+
import (
6+
"machine"
7+
)
8+
9+
func init() {
10+
AdapterConfig = NINAConfig{
11+
UART: machine.UART_NINA,
12+
CS: machine.NINA_CS,
13+
ACK: machine.NINA_ACK,
14+
GPIO0: machine.NINA_GPIO0,
15+
RESETN: machine.NINA_RESETN,
16+
CTS: machine.NINA_CTS,
17+
RTS: machine.NINA_RTS,
18+
BaudRate: machine.NINA_BAUDRATE,
19+
ResetInverted: machine.NINA_RESET_INVERTED,
20+
SoftFlowControl: machine.NINA_SOFT_FLOWCONTROL,
21+
}
22+
}

adapter_ninafw.go

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ import (
1111

1212
const maxConnections = 1
1313

14+
// NINAConfig encapsulates the hardware options for the NINA firmware
15+
type NINAConfig struct {
16+
UART *machine.UART
17+
18+
CS machine.Pin
19+
ACK machine.Pin
20+
GPIO0 machine.Pin
21+
RESETN machine.Pin
22+
23+
TX machine.Pin
24+
RX machine.Pin
25+
CTS machine.Pin
26+
RTS machine.Pin
27+
28+
BaudRate uint32
29+
ResetInverted bool
30+
SoftFlowControl bool
31+
}
32+
33+
// AdapterConfig is used to set the hardware options for the NINA adapter prior
34+
// to calling DefaultAdapter.Enable()
35+
var AdapterConfig NINAConfig
36+
1437
// Adapter represents the UART connection to the NINA fw.
1538
type Adapter struct {
1639
hci *hci
@@ -40,37 +63,37 @@ var DefaultAdapter = &Adapter{
4063
// Bluetooth-related calls (unless otherwise indicated).
4164
func (a *Adapter) Enable() error {
4265
// reset the NINA in BLE mode
43-
machine.NINA_CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
44-
machine.NINA_CS.Low()
66+
AdapterConfig.CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
67+
AdapterConfig.CS.Low()
4568

46-
if machine.NINA_RESET_INVERTED {
69+
if AdapterConfig.ResetInverted {
4770
resetNINAInverted()
4871
} else {
4972
resetNINA()
5073
}
5174

5275
// serial port for nina chip
53-
uart := machine.UART_NINA
76+
uart := AdapterConfig.UART
5477
cfg := machine.UARTConfig{
55-
TX: machine.NINA_TX,
56-
RX: machine.NINA_RX,
57-
BaudRate: machine.NINA_BAUDRATE,
78+
TX: AdapterConfig.TX,
79+
RX: AdapterConfig.RX,
80+
BaudRate: AdapterConfig.BaudRate,
5881
}
59-
if !machine.NINA_SOFT_FLOWCONTROL {
60-
cfg.CTS = machine.NINA_CTS
61-
cfg.RTS = machine.NINA_RTS
82+
if !AdapterConfig.SoftFlowControl {
83+
cfg.CTS = AdapterConfig.CTS
84+
cfg.RTS = AdapterConfig.RTS
6285
}
6386

6487
uart.Configure(cfg)
6588

6689
a.hci, a.att = newBLEStack(uart)
67-
if machine.NINA_SOFT_FLOWCONTROL {
68-
a.hci.softRTS = machine.NINA_RTS
90+
if AdapterConfig.SoftFlowControl {
91+
a.hci.softRTS = AdapterConfig.RTS
6992
a.hci.softRTS.Configure(machine.PinConfig{Mode: machine.PinOutput})
7093
a.hci.softRTS.High()
7194

72-
a.hci.softCTS = machine.NINA_CTS
73-
machine.NINA_CTS.Configure(machine.PinConfig{Mode: machine.PinInput})
95+
a.hci.softCTS = AdapterConfig.CTS
96+
AdapterConfig.CTS.Configure(machine.PinConfig{Mode: machine.PinInput})
7497
}
7598

7699
a.hci.start()
@@ -133,20 +156,20 @@ func makeNINAAddress(mac MAC) [6]uint8 {
133156
}
134157

135158
func resetNINA() {
136-
machine.NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput})
159+
AdapterConfig.RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput})
137160

138-
machine.NINA_RESETN.High()
161+
AdapterConfig.RESETN.High()
139162
time.Sleep(100 * time.Millisecond)
140-
machine.NINA_RESETN.Low()
163+
AdapterConfig.RESETN.Low()
141164
time.Sleep(1000 * time.Millisecond)
142165
}
143166

144167
func resetNINAInverted() {
145-
machine.NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput})
168+
AdapterConfig.RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput})
146169

147-
machine.NINA_RESETN.Low()
170+
AdapterConfig.RESETN.Low()
148171
time.Sleep(100 * time.Millisecond)
149-
machine.NINA_RESETN.High()
172+
AdapterConfig.RESETN.High()
150173
time.Sleep(1000 * time.Millisecond)
151174
}
152175

0 commit comments

Comments
 (0)