From 80f261290d4fc1d85e47771d459d669d928fffc4 Mon Sep 17 00:00:00 2001 From: BCG Date: Mon, 15 Jan 2024 19:11:25 -0500 Subject: [PATCH] cleaning up custom NINA config --- adapter_ninafw-featherwing.go | 22 +++++++ adapter_ninafw-machine.go | 22 ++++--- adapter_ninafw.go | 101 +++++++++++++++--------------- examples/scanner/main.go | 3 - examples/scanner/ninafw_custom.go | 26 -------- 5 files changed, 84 insertions(+), 90 deletions(-) create mode 100644 adapter_ninafw-featherwing.go delete mode 100644 examples/scanner/ninafw_custom.go diff --git a/adapter_ninafw-featherwing.go b/adapter_ninafw-featherwing.go new file mode 100644 index 00000000..7dee7d12 --- /dev/null +++ b/adapter_ninafw-featherwing.go @@ -0,0 +1,22 @@ +//go:build ninafw && ninafw_featherwing_init + +package bluetooth + +import ( + "machine" +) + +func init() { + AdapterConfig = NINAConfig{ + UART: machine.DefaultUART, + CS: machine.D13, + ACK: machine.D11, + GPIO0: machine.D10, + RESETN: machine.D12, + CTS: machine.D11, // same as ACK + RTS: machine.D10, // same as GPIO0 + BaudRate: 115200, + ResetInverted: true, + SoftFlowControl: true, + } +} diff --git a/adapter_ninafw-machine.go b/adapter_ninafw-machine.go index c619d93e..09d02b7f 100644 --- a/adapter_ninafw-machine.go +++ b/adapter_ninafw-machine.go @@ -1,4 +1,4 @@ -//go:build ninafw_machine_init +//go:build ninafw && ninafw_machine_init package bluetooth @@ -7,12 +7,16 @@ import ( ) func init() { - NINA_UART = machine.UART1 - NINA_CS = machine.NINA_CS - NINA_ACK = machine.NINA_ACK - NINA_GPIO0 = machine.NINA_GPIO0 - NINA_RESETN = machine.NINA_RESETN - NINA_BAUDRATE = machine.NINA_BAUDRATE - NINA_RESET_INVERTED = machine.NINA_RESET_INVERTED - NINA_SOFT_FLOWCONTROL = machine.NINA_SOFT_FLOWCONTROL + AdapterConfig = NINAConfig{ + UART: machine.NINA_UART, + CS: machine.NINA_CS, + ACK: machine.NINA_ACK, + GPIO0: machine.NINA_GPIO0, + RESETN: machine.NINA_RESETN, + CTS: machine.NINA_CTS, + RTS: machine.NINA_RTS, + BaudRate: machine.NINA_BAUDRATE, + ResetInverted: machine.NINA_RESET_INVERTED, + SoftFlowControl: machine.NINA_SOFT_FLOWCONTROL, + } } diff --git a/adapter_ninafw.go b/adapter_ninafw.go index 58554df3..079de9f2 100644 --- a/adapter_ninafw.go +++ b/adapter_ninafw.go @@ -11,24 +11,28 @@ import ( const maxConnections = 1 -var ( - NINA_UART *machine.UART - - NINA_CS machine.Pin - NINA_ACK machine.Pin - NINA_GPIO0 machine.Pin - NINA_RESETN machine.Pin - - NINA_TX machine.Pin - NINA_RX machine.Pin - NINA_CTS machine.Pin - NINA_RTS machine.Pin - - // NINA-W102 settings - NINA_BAUDRATE uint32 - NINA_RESET_INVERTED bool - NINA_SOFT_FLOWCONTROL bool -) +// NINAConfig encapsulates the hardware options for the NINA firmware +type NINAConfig struct { + UART *machine.UART + + CS machine.Pin + ACK machine.Pin + GPIO0 machine.Pin + RESETN machine.Pin + + TX machine.Pin + RX machine.Pin + CTS machine.Pin + RTS machine.Pin + + BaudRate uint32 + ResetInverted bool + SoftFlowControl bool +} + +// AdapterConfig is used to set the hardware options for the NINA adapter prior +// to calling DefaultAdapter.Enable() +var AdapterConfig NINAConfig // Adapter represents the UART connection to the NINA fw. type Adapter struct { @@ -59,54 +63,47 @@ var DefaultAdapter = &Adapter{ // Bluetooth-related calls (unless otherwise indicated). func (a *Adapter) Enable() error { // reset the NINA in BLE mode - machine.NINA_CS.Configure(machine.PinConfig{Mode: machine.PinOutput}) - machine.NINA_CS.Low() + AdapterConfig.CS.Configure(machine.PinConfig{Mode: machine.PinOutput}) + AdapterConfig.CS.Low() if _debug { - println("tx:", NINA_TX, "rx:", NINA_RX, "baudrate:", NINA_BAUDRATE, "cts:", NINA_CTS, "rts:", NINA_RTS) - } - - // serial port for nina chip - uart := NINA_UART - if err := uart.Configure(machine.UARTConfig{ - TX: NINA_TX, - RX: NINA_RX, - BaudRate: NINA_BAUDRATE, - CTS: NINA_CTS, - RTS: NINA_RTS, - }); err != nil { - println("error configuring UART:", err.Error()) - return err + println( + "tx:", AdapterConfig.TX, + "rx:", AdapterConfig.RX, + "baudrate:", AdapterConfig.BaudRate, + "cts:", AdapterConfig.CTS, + "rts:", AdapterConfig.RTS, + ) } - if NINA_RESET_INVERTED { + if AdapterConfig.ResetInverted { resetNINAInverted() } else { resetNINA() } // serial port for nina chip - uart := machine.UART_NINA + uart := AdapterConfig.UART cfg := machine.UARTConfig{ - TX: machine.NINA_TX, - RX: machine.NINA_RX, - BaudRate: machine.NINA_BAUDRATE, + TX: AdapterConfig.TX, + RX: AdapterConfig.RX, + BaudRate: AdapterConfig.BaudRate, } - if !machine.NINA_SOFT_FLOWCONTROL { - cfg.CTS = machine.NINA_CTS - cfg.RTS = machine.NINA_RTS + if !AdapterConfig.SoftFlowControl { + cfg.CTS = AdapterConfig.CTS + cfg.RTS = AdapterConfig.RTS } uart.Configure(cfg) a.hci, a.att = newBLEStack(uart) - if machine.NINA_SOFT_FLOWCONTROL { - a.hci.softRTS = machine.NINA_RTS + if AdapterConfig.SoftFlowControl { + a.hci.softRTS = AdapterConfig.RTS a.hci.softRTS.Configure(machine.PinConfig{Mode: machine.PinOutput}) a.hci.softRTS.High() - a.hci.softCTS = machine.NINA_CTS - machine.NINA_CTS.Configure(machine.PinConfig{Mode: machine.PinInput}) + a.hci.softCTS = AdapterConfig.CTS + AdapterConfig.CTS.Configure(machine.PinConfig{Mode: machine.PinInput}) } if _debug { @@ -179,20 +176,20 @@ func makeNINAAddress(mac MAC) [6]uint8 { } func resetNINA() { - machine.NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput}) + AdapterConfig.RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput}) - machine.NINA_RESETN.High() + AdapterConfig.RESETN.High() time.Sleep(100 * time.Millisecond) - NINA_RESETN.Low() + AdapterConfig.RESETN.Low() time.Sleep(1000 * time.Millisecond) } func resetNINAInverted() { - machine.NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput}) + AdapterConfig.RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput}) - machine.NINA_RESETN.Low() + AdapterConfig.RESETN.Low() time.Sleep(100 * time.Millisecond) - NINA_RESETN.High() + AdapterConfig.RESETN.High() time.Sleep(1000 * time.Millisecond) } diff --git a/examples/scanner/main.go b/examples/scanner/main.go index f993347d..ffa2ffd1 100644 --- a/examples/scanner/main.go +++ b/examples/scanner/main.go @@ -1,8 +1,6 @@ package main import ( - "time" - "tinygo.org/x/bluetooth" ) @@ -10,7 +8,6 @@ var adapter = bluetooth.DefaultAdapter func main() { - time.Sleep(2 * time.Second) // Enable BLE interface. must("enable BLE stack", adapter.Enable()) diff --git a/examples/scanner/ninafw_custom.go b/examples/scanner/ninafw_custom.go deleted file mode 100644 index 16acb0db..00000000 --- a/examples/scanner/ninafw_custom.go +++ /dev/null @@ -1,26 +0,0 @@ -//go:build ninafw && !ninafw_machine_init - -package main - -import ( - "machine" - - "tinygo.org/x/bluetooth" -) - -func init() { - bluetooth.NINA_UART = machine.DefaultUART - bluetooth.NINA_CS = machine.D13 - bluetooth.NINA_ACK = machine.D11 - bluetooth.NINA_GPIO0 = machine.D10 - bluetooth.NINA_RESETN = machine.D12 - bluetooth.NINA_BAUDRATE = 115200 // machine.NINA_BAUDRATE - bluetooth.NINA_RESET_INVERTED = true // machine.NINA_RESET_INVERTED - bluetooth.NINA_SOFT_FLOWCONTROL = true // machine.NINA_SOFT_FLOWCONTROL - - bluetooth.NINA_TX = machine.UART_TX_PIN - bluetooth.NINA_RX = machine.UART_RX_PIN - - bluetooth.NINA_CTS = bluetooth.NINA_ACK - bluetooth.NINA_RTS = bluetooth.NINA_GPIO0 -}