From 3fccf08cb32e134b7cf4f23a437d9e7978b8a2be Mon Sep 17 00:00:00 2001 From: BCG Date: Mon, 15 Jan 2024 12:38:46 -0500 Subject: [PATCH] more testing --- adapter_ninafw-machine.go | 6 +-- adapter_ninafw.go | 67 +++++++++++++++++++------------ examples/scanner/main.go | 4 ++ examples/scanner/ninafw_custom.go | 26 ++++++++++++ 4 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 examples/scanner/ninafw_custom.go diff --git a/adapter_ninafw-machine.go b/adapter_ninafw-machine.go index 3c94a2b3..c619d93e 100644 --- a/adapter_ninafw-machine.go +++ b/adapter_ninafw-machine.go @@ -1,4 +1,4 @@ -//go:build ninafw +//go:build ninafw_machine_init package bluetooth @@ -7,9 +7,7 @@ import ( ) func init() { - NINA_SCK = machine.NINA_SCK - NINA_SDO = machine.NINA_SDO - NINA_SDI = machine.NINA_SDI + NINA_UART = machine.UART1 NINA_CS = machine.NINA_CS NINA_ACK = machine.NINA_ACK NINA_GPIO0 = machine.NINA_GPIO0 diff --git a/adapter_ninafw.go b/adapter_ninafw.go index 05d91f5e..e00ee500 100644 --- a/adapter_ninafw.go +++ b/adapter_ninafw.go @@ -12,10 +12,7 @@ import ( const maxConnections = 1 var ( - // NINA-W102 Pins - NINA_SCK machine.Pin - NINA_SDO machine.Pin - NINA_SDI machine.Pin + NINA_UART *machine.UART NINA_CS machine.Pin NINA_ACK machine.Pin @@ -28,9 +25,9 @@ var ( NINA_RTS machine.Pin // NINA-W102 settings - NINA_BAUDRATE = 115200 - NINA_RESET_INVERTED = true - NINA_SOFT_FLOWCONTROL = false + NINA_BAUDRATE uint32 + NINA_RESET_INVERTED bool + NINA_SOFT_FLOWCONTROL bool ) // Adapter represents the UART connection to the NINA fw. @@ -62,34 +59,51 @@ 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_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput}) - machine.NINA_CS.Low() + NINA_CS.Configure(machine.PinConfig{Mode: machine.PinOutput}) + NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput}) + NINA_CS.Low() - if machine.NINA_RESET_INVERTED { + 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 + } + + if NINA_RESET_INVERTED { resetNINAInverted() } else { resetNINA() } - // serial port for nina chip - uart := machine.UART1 - uart.Configure(machine.UARTConfig{ - TX: machine.NINA_TX, - RX: machine.NINA_RX, - BaudRate: machine.NINA_BAUDRATE, - CTS: machine.NINA_CTS, - RTS: machine.NINA_RTS, - }) - a.hci, a.att = newBLEStack(uart) + if _debug { + println("starting hci") + } a.hci.start() + if _debug { + println("reseting hci") + } if err := a.hci.reset(); err != nil { return err } + if _debug { + println("hci reset successfully") + } + time.Sleep(150 * time.Millisecond) if err := a.hci.setEventMask(0x3FFFFFFFFFFFFFFF); err != nil { @@ -144,16 +158,19 @@ func makeNINAAddress(mac MAC) [6]uint8 { } func resetNINA() { - machine.NINA_RESETN.High() + NINA_RESETN.High() time.Sleep(100 * time.Millisecond) - machine.NINA_RESETN.Low() + NINA_RESETN.Low() time.Sleep(1000 * time.Millisecond) } func resetNINAInverted() { - machine.NINA_RESETN.Low() + if _debug { + println("resetNINAInverted") + } + NINA_RESETN.Low() time.Sleep(100 * time.Millisecond) - machine.NINA_RESETN.High() + NINA_RESETN.High() time.Sleep(1000 * time.Millisecond) } diff --git a/examples/scanner/main.go b/examples/scanner/main.go index 11ef2ad6..f993347d 100644 --- a/examples/scanner/main.go +++ b/examples/scanner/main.go @@ -1,12 +1,16 @@ package main import ( + "time" + "tinygo.org/x/bluetooth" ) 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 new file mode 100644 index 00000000..16acb0db --- /dev/null +++ b/examples/scanner/ninafw_custom.go @@ -0,0 +1,26 @@ +//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 +}