Skip to content

Commit

Permalink
cleaning up custom NINA config
Browse files Browse the repository at this point in the history
  • Loading branch information
bgould committed Jan 16, 2024
1 parent b3e3cf8 commit 80f2612
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 90 deletions.
22 changes: 22 additions & 0 deletions adapter_ninafw-featherwing.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
22 changes: 13 additions & 9 deletions adapter_ninafw-machine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build ninafw_machine_init
//go:build ninafw && ninafw_machine_init

package bluetooth

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

Expand Down
3 changes: 0 additions & 3 deletions examples/scanner/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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: 0 additions & 26 deletions examples/scanner/ninafw_custom.go

This file was deleted.

0 comments on commit 80f2612

Please sign in to comment.