Bringing Up and Enabling the BLE Interface

In order to enable the BLE portion of the WA2000, you need to bring up the wln. object first. This involves:

• Configuring the GPIO lines used to interface with the WA2000.

• Resetting the WA2000.

• Booting up the WA2000 using the wln.boot method.

Extensive info on the boot process is provided in Bringing up Wi-Fi interface so we will refrain from explaining the basics of the module initialization here.

Here is the code, with comments, that will bring up the WA2000. This code only touches on things that are necessary for making the BLE interface operational.

** Tibbo Basic **

Const WLN_RST=PL_IO_NUM_51
Const WLN_CS=PL_IO_NUM_49
Const WLN_DI=PL_IO_NUM_52
Const WLN_DO=PL_IO_NUM_50
Const WLN_CLK=PL_IO_NUM_53

declare sub delay_ms(delay as integer)

'====================================================================

sub on_sys_init()
 'configure the GPIO lines
 wln.csmap=WLN_CS
 io.num=WLN_CS
 io.enabled=YES
 wln.dimap=WLN_DI
 wln.domap=WLN_DO
 io.num=WLN_DO
 io.enabled=YES
 wln.clkmap=WLN_CLK
 io.num=WLN_CLK
 io.enabled=YES
 io.num=WLN_RST
 io.enabled=YES
 wln.disable

 'reset the WA2000
 io.num=WLN_RST
 io.state=LOW
 delay_ms(1)
 io.state=HIGH

 '----- set MAC address (optional) -----
 wln.mac="0.2.100.50.23.34"        

 wln.boot(0)

 bt.name="EM2000 node 1" 'all spaces will be deleted, so the name will be "EM2000node1"
 bt.emulation=PL_WLN_BT_EMULATION_MODE_MICROCHIP 'added for clarity, this is the defaul value

 'allocate TX and RX buffers
 bt.txbuffrq(1)
 bt.rxbuffrq(1)
 sys.buffalloc

 'enable the BLE interface and wait for it to become enabled
 '(blocking version)
 bt.enable
 while bt.enabled=NO
 wend

 bt.advertise=YES 'allow the device to be discoverable

 sys.debugprint("BLE is now ready. MAC= "+bt.mac+".\r\n") 'notice how the MAC address is reported here
end sub

sub delay_ms(delay as integer)
 sys.timercountms=0
 while sys.timercountms<delay
 wend
end sub

As you can see, after the module is booted using wln.boot , you need to take several additional steps:

• Set the advertising name in the bt.name property. This is the name that will be seen on your central device (an iOS or Android smartphone, etc.).

• Select the emulation mode (Nordic or Microchip) using the bt.emulation property (can be omitted if you want to stick to default Microchip emulation).

• Allocate TX and RX buffers (assign at least one buffer page for each). More on this in Allocating TX and RX buffers .

• Enable the BLE with ble.enable .

• Enable advertising through bt.advertise . This step is only necessary if you want your device to be discoverable, and this is not always true.

The BLE interface has its own MAC address. A unique BT MAC is already hardcoded into the WA2000 during the manufacturing. If needed, you can also set your own MAC .

Since ble.enable is aynchronous, your app will need to detect when the BLE actually turns on. Example above polls the state of wln.enabled — this is a blocking way. The non-blocking way relies on on_bt_event :

** Tibbo Basic **

...
 'enable the BLE interface and wait for it to become enabled
 '(blocking version)
 bt.enable
end sub

sub on_bt_event(bt_event as enum pl_bt_events)
 select case bt_event
 case PL_BT_EVENT_CONNECTED:

 case PL_BT_EVENT_DISCONNECTED:

 case PL_BT_EVENT_ENABLED:
         bt.advertise=YES 'allow the device to be discoverable                
         sys.debugprint("BLE is now ready\r\n")

 case PL_BT_EVENT_DISABLED:

 end select
end sub