@@ -11,24 +11,28 @@ import (
1111
1212const maxConnections = 1
1313
14- var (
15- NINA_UART * machine.UART
16-
17- NINA_CS machine.Pin
18- NINA_ACK machine.Pin
19- NINA_GPIO0 machine.Pin
20- NINA_RESETN machine.Pin
21-
22- NINA_TX machine.Pin
23- NINA_RX machine.Pin
24- NINA_CTS machine.Pin
25- NINA_RTS machine.Pin
26-
27- // NINA-W102 settings
28- NINA_BAUDRATE uint32
29- NINA_RESET_INVERTED bool
30- NINA_SOFT_FLOWCONTROL bool
31- )
14+ // NINAConfig encapsulates the hardware options for the NINA firmware
15+ type NINAConfig struct {
16+ UART * machine.UART
17+
18+ CS machine.Pin
19+ ACK machine.Pin
20+ GPIO0 machine.Pin
21+ RESETN machine.Pin
22+
23+ TX machine.Pin
24+ RX machine.Pin
25+ CTS machine.Pin
26+ RTS machine.Pin
27+
28+ BaudRate uint32
29+ ResetInverted bool
30+ SoftFlowControl bool
31+ }
32+
33+ // AdapterConfig is used to set the hardware options for the NINA adapter prior
34+ // to calling DefaultAdapter.Enable()
35+ var AdapterConfig NINAConfig
3236
3337// Adapter represents the UART connection to the NINA fw.
3438type Adapter struct {
@@ -59,54 +63,47 @@ var DefaultAdapter = &Adapter{
5963// Bluetooth-related calls (unless otherwise indicated).
6064func (a * Adapter ) Enable () error {
6165 // reset the NINA in BLE mode
62- machine . NINA_CS .Configure (machine.PinConfig {Mode : machine .PinOutput })
63- machine . NINA_CS .Low ()
66+ AdapterConfig . CS .Configure (machine.PinConfig {Mode : machine .PinOutput })
67+ AdapterConfig . CS .Low ()
6468
6569 if _debug {
66- println ("tx:" , NINA_TX , "rx:" , NINA_RX , "baudrate:" , NINA_BAUDRATE , "cts:" , NINA_CTS , "rts:" , NINA_RTS )
67- }
68-
69- // serial port for nina chip
70- uart := NINA_UART
71- if err := uart .Configure (machine.UARTConfig {
72- TX : NINA_TX ,
73- RX : NINA_RX ,
74- BaudRate : NINA_BAUDRATE ,
75- CTS : NINA_CTS ,
76- RTS : NINA_RTS ,
77- }); err != nil {
78- println ("error configuring UART:" , err .Error ())
79- return err
70+ println (
71+ "tx:" , AdapterConfig .TX ,
72+ "rx:" , AdapterConfig .RX ,
73+ "baudrate:" , AdapterConfig .BaudRate ,
74+ "cts:" , AdapterConfig .CTS ,
75+ "rts:" , AdapterConfig .RTS ,
76+ )
8077 }
8178
82- if NINA_RESET_INVERTED {
79+ if AdapterConfig . ResetInverted {
8380 resetNINAInverted ()
8481 } else {
8582 resetNINA ()
8683 }
8784
8885 // serial port for nina chip
89- uart := machine . UART_NINA
86+ uart := AdapterConfig . UART
9087 cfg := machine.UARTConfig {
91- TX : machine . NINA_TX ,
92- RX : machine . NINA_RX ,
93- BaudRate : machine . NINA_BAUDRATE ,
88+ TX : AdapterConfig . TX ,
89+ RX : AdapterConfig . RX ,
90+ BaudRate : AdapterConfig . BaudRate ,
9491 }
95- if ! machine . NINA_SOFT_FLOWCONTROL {
96- cfg .CTS = machine . NINA_CTS
97- cfg .RTS = machine . NINA_RTS
92+ if ! AdapterConfig . SoftFlowControl {
93+ cfg .CTS = AdapterConfig . CTS
94+ cfg .RTS = AdapterConfig . RTS
9895 }
9996
10097 uart .Configure (cfg )
10198
10299 a .hci , a .att = newBLEStack (uart )
103- if machine . NINA_SOFT_FLOWCONTROL {
104- a .hci .softRTS = machine . NINA_RTS
100+ if AdapterConfig . SoftFlowControl {
101+ a .hci .softRTS = AdapterConfig . RTS
105102 a .hci .softRTS .Configure (machine.PinConfig {Mode : machine .PinOutput })
106103 a .hci .softRTS .High ()
107104
108- a .hci .softCTS = machine . NINA_CTS
109- machine . NINA_CTS .Configure (machine.PinConfig {Mode : machine .PinInput })
105+ a .hci .softCTS = AdapterConfig . CTS
106+ AdapterConfig . CTS .Configure (machine.PinConfig {Mode : machine .PinInput })
110107 }
111108
112109 if _debug {
@@ -179,20 +176,20 @@ func makeNINAAddress(mac MAC) [6]uint8 {
179176}
180177
181178func resetNINA () {
182- machine . NINA_RESETN .Configure (machine.PinConfig {Mode : machine .PinOutput })
179+ AdapterConfig . RESETN .Configure (machine.PinConfig {Mode : machine .PinOutput })
183180
184- machine . NINA_RESETN .High ()
181+ AdapterConfig . RESETN .High ()
185182 time .Sleep (100 * time .Millisecond )
186- NINA_RESETN .Low ()
183+ AdapterConfig . RESETN .Low ()
187184 time .Sleep (1000 * time .Millisecond )
188185}
189186
190187func resetNINAInverted () {
191- machine . NINA_RESETN .Configure (machine.PinConfig {Mode : machine .PinOutput })
188+ AdapterConfig . RESETN .Configure (machine.PinConfig {Mode : machine .PinOutput })
192189
193- machine . NINA_RESETN .Low ()
190+ AdapterConfig . RESETN .Low ()
194191 time .Sleep (100 * time .Millisecond )
195- NINA_RESETN .High ()
192+ AdapterConfig . RESETN .High ()
196193 time .Sleep (1000 * time .Millisecond )
197194}
198195
0 commit comments