Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bgould committed Jan 15, 2024
1 parent b9f5114 commit 3fccf08
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 29 deletions.
6 changes: 2 additions & 4 deletions adapter_ninafw-machine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build ninafw
//go:build ninafw_machine_init

package bluetooth

Expand All @@ -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
Expand Down
67 changes: 42 additions & 25 deletions adapter_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions examples/scanner/main.go
Original file line number Diff line number Diff line change
@@ -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())

Expand Down
26 changes: 26 additions & 0 deletions examples/scanner/ninafw_custom.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3fccf08

Please sign in to comment.