Bus Mode Selection and Remote Control
Bus modes discussed in this document are defined in Serial Interface .
This tutorial demonstrates operation in the three operational states of the BGX: local COMMAND mode, STREAM mode, and remote COMMAND mode. The tutorial also demonstrates the following methods by which mode switches can be executed:
- manual mode transition using a port pin
- manual mode transition using commands through a breakout sequence across the serial interface
- manual mode transition through remote command execution
- automatic mode transition as a function of BLE link connection status
This demonstration uses two BGX13P EXP boards. It is similar to the procedure described in Getting Started , with variations to cover the various methods for bus mode selection.
Setup
Plug each of the BGX13P EXP boards into a separate USB port on a computer and open a terminal program to communicate with each board. See Getting Started, Evaluating BGX with a terminal program .
It is assumed both BGX EXP boards are in their factory default configuration. To revert a BGX13 to its factory default configuration, see the fac command definition .
Manual Bus Mode Selection Using the str_select GPIO
The BGX EXP devices boot up as BLE peripherals with advertising turned on, in
local
COMMAND_MODE
, indicated by the output from the BGX:
[COMMAND_MODE]
>
The BGX EXP board offers two mechanical buttons: BTN0 connected to GPIO 2, and BTN1 connected to GPIO 3. Configure BTN0 to use str_select in 'edge' mode, which switches modes when BGX detects a rising edge on the configured input pin.
To configure GPIO 2 to str_select in edge mode, run the following in the terminal program:
> set bu s c edge
Success
> gfu 2 str_select
Success
Now when you touch and release BTN0, you will see the BGX switching between local command mode and stream mode:
> STREAM_MODE
COMMAND_MODE
STREAM_MODE
COMMAND_MODE
In the next section, the str_select setting will be used. Note that this configuration is not saved to non-volatile memory, because save was not called. To revert back to defaults, click the 'reset' button on the EXP board, or call the reboot command.
Connecting BGX to BGX
BGX devices can operate in both central and peripheral roles. In a BGX-to-BGX connection, one BGX will serve as central and the other will operate as peripheral.
To connect to a peripheral BGX, that BGX must be advertising. Default configuration for the BGX device configures advertising to turn off after a few minutes. Press the Reset button on the device that hasn't been configured to use str_select.
Once the device resets, it will begin advertising again. You can also use the adv command to turn on advertising:
On peripheral BGX device's terminal:
> adv
Success
On the other BGX device, enter the command scan high . Within a few seconds, the BGX will displays a list of BGX devices discovered. For example:
> scan high
! # RSSI BD_ADDR Device Name
>
\# 1 -58 20:73:7a:12:9a:42 BGX-8F18
Issue the con (connect) command on the device used as central, with the index number of the BGX board listed by the scan results.
> con 1
Success
Automatic Mode Transitions
When the devices connect, the following will appear on the peripheral's terminal:
STREAM_MODE
The peripheral's switch to stream mode upon connection is an example of automatic mode transition, which is default behavior meant to simplify the communication process. Automatic mode transitions function as follows:
- If a connection is successful, the device transitions to stream mode
- If a connection is ended, the device transitions to local command mode
Note that the central has remained in command mode and did not switch to stream mode automatically. If a BGX device has been configured to use str_select as an input pin, automatic mode transitions are disabled and the BGX allows mode transitions to be controlled manually.
Manual Bus Mode Selection Using the str Command
The central in command mode can be switched to stream mode by issuing the str command.
On the central's terminal:
> str
STREAM_MODE
Now both devices enter STREAM_MODE upon connection, as this is the default configuration behavior. Data can be transmitted between the devices now, simply by inputting characters through the BGX device's RX pin. In one terminal, type a message. The other terminal will then display the received message, which has been transferred out the BGX device's TX pin. In the example below, one BGX transmitted "Hello!" to the other in the BLE link.
> STREAM_MODE
Hello!
Transitioning from Stream Mode to Command Mode with a Breakout Sequence
A BGX device in stream mode can transition to command mode by sending an breakout sequence of characters through the BGX device's serial interface.
This breakout sequence can be configured or disabled using the bu s s variable.
Note that it can be difficult to send a breakout sequence in some terminal programs due to intra-byte latency. In order to distinguish between the breakout sequence and bytes meant to be transmitted during stream mode operation, the BGX requires minimal delay between bytes to validate a breakout sequence.
Transmitting the following to the central's serial interface will switch operation to command mode, using the default breakout sequence:
> $$$
Note that the breakout sequence is defined in hexadecimal, and so the data sent to the central BGX here is 0x24 0x24 0x24.
The breakout sequence must be transmitted within certain timing restraints to be recognized as a breakout sequence instead of regular data. For more information on the timing requirements, see bu s s .
After sending this breakout sequence, the BGX acknowledges entrance to command mode as follows:
COMMAND_MODE
Serial Data Rate Limitations of the Breakout Sequence
Due to timing limitations, the breakout sequence will not work reliably if your serial data rate is less that 5000 bits-per-second. If your application requires a serial data rate below 5000 bps then you must use GPIO Mode Selection in order to switch the bus mode from stream mode to command mode.
Remotely Controlling a BGX Device
To remote control a BGX module, it must be placed in remote COMMAND mode.
This demonstration assumes the two BGX devices are in the state configured in the previous steps:
- Two BGX devices are connected to each other
- The central BGX is in command mode after execution of the breakout sequence
- The peripheral BGX is still in stream mode
The
rbmode
command works by setting the BLE
mode
characteristic for the peripheral BGX. Setting the BLE
mode
characteristic to
remote COMMAND mode is the
only
way to place a BGX module in remote COMMAND
mode.
Note that this configuration of the BLE mode characteristic is also how the BGXpress Framework for mobile apps remotely transitions a connected BGX to command mode.
Placing the Peripheral BGX in Remote COMMAND Mode
On the central BGX device's terminal, issue the rbmode (remote bus mode) command, with no argument. The device responds with the remote device bus mode:
> rbmode
stream
On the central BGX device's terminal, issue the
rbmode
(remote bus mode) command with the
remote
argument:
> rbmode remote
Success
The peripheral BGX switches to remote COMMAND mode:
REMOTE_MODE
Note that the central BGX is still in local COMMAND mode, and is not yet able to send commands to the peripheral BGX. The central BGX can continue executing commands in it's local command mode. For the central BGX to execute commands remotely on the peripheral BGX, the central BGX must switch to stream mode:
> str
STREAM_MODE
> gfu 0 stdio
Success
> gdi 0 olo
Success
At this point, the peripheral BGX device's GPIO 0 has been configured to a Standard I/O pin in push-pull output, initialized as logic low.
Note that LED0 has turned on. The LED0 can now be controlled remotely by executing the gse command.
> gse 0 1
Success
> gse 0 0
Success