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:


Extensive info on the boot process is provided in Bringing Up the Wi-Fi Interface, so we will refrain from explaining the basics of 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:


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


Since bt.enable is aynchronous, your app will need to detect when the BLE actually turns on. The 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