2023-12-21 01:29:23 +03:00
|
|
|
//go:build ninafw
|
|
|
|
|
|
|
|
package bluetooth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"machine"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxConnections = 1
|
|
|
|
|
2024-01-24 19:35:53 +03:00
|
|
|
// Adapter represents the HCI connection to the NINA fw.
|
2023-12-21 01:29:23 +03:00
|
|
|
type Adapter struct {
|
2024-01-24 19:35:53 +03:00
|
|
|
hciAdapter
|
2023-12-21 01:29:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultAdapter is the default adapter on the current system.
|
|
|
|
//
|
|
|
|
// Make sure to call Enable() before using it to initialize the adapter.
|
|
|
|
var DefaultAdapter = &Adapter{
|
2024-01-24 19:35:53 +03:00
|
|
|
hciAdapter: hciAdapter{
|
|
|
|
isDefault: true,
|
|
|
|
connectHandler: func(device Device, connected bool) {
|
|
|
|
return
|
|
|
|
},
|
|
|
|
connectedDevices: make([]Device, 0, maxConnections),
|
2023-12-21 01:29:23 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable configures the BLE stack. It must be called before any
|
|
|
|
// 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()
|
2024-01-05 19:16:14 +03:00
|
|
|
|
|
|
|
if machine.NINA_RESET_INVERTED {
|
|
|
|
resetNINAInverted()
|
|
|
|
} else {
|
|
|
|
resetNINA()
|
|
|
|
}
|
2023-12-21 01:29:23 +03:00
|
|
|
|
|
|
|
// serial port for nina chip
|
2024-01-06 21:07:18 +03:00
|
|
|
uart := machine.UART_NINA
|
|
|
|
cfg := machine.UARTConfig{
|
2023-12-21 01:29:23 +03:00
|
|
|
TX: machine.NINA_TX,
|
|
|
|
RX: machine.NINA_RX,
|
2024-01-05 19:16:14 +03:00
|
|
|
BaudRate: machine.NINA_BAUDRATE,
|
2024-01-06 21:07:18 +03:00
|
|
|
}
|
|
|
|
if !machine.NINA_SOFT_FLOWCONTROL {
|
|
|
|
cfg.CTS = machine.NINA_CTS
|
|
|
|
cfg.RTS = machine.NINA_RTS
|
|
|
|
}
|
|
|
|
|
|
|
|
uart.Configure(cfg)
|
2023-12-21 01:29:23 +03:00
|
|
|
|
|
|
|
a.hci, a.att = newBLEStack(uart)
|
2024-01-06 21:07:18 +03:00
|
|
|
if machine.NINA_SOFT_FLOWCONTROL {
|
|
|
|
a.hci.softRTS = machine.NINA_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})
|
|
|
|
}
|
2023-12-21 01:29:23 +03:00
|
|
|
|
2024-01-24 19:35:53 +03:00
|
|
|
return a.enable()
|
2023-12-21 01:29:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetNINA() {
|
2024-01-06 21:07:18 +03:00
|
|
|
machine.NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
|
|
|
2023-12-21 01:29:23 +03:00
|
|
|
machine.NINA_RESETN.High()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
machine.NINA_RESETN.Low()
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetNINAInverted() {
|
2024-01-06 21:07:18 +03:00
|
|
|
machine.NINA_RESETN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
|
|
|
2023-12-21 01:29:23 +03:00
|
|
|
machine.NINA_RESETN.Low()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
machine.NINA_RESETN.High()
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
}
|