Silicon Labs BGSCRIPT User manual

BGSCRIPT SCRIPTING LANGUAGE
DEVELOPER GUIDE Wednesday, 2 December 2020 Version 34.
1 Version History ________ ________________________________________________________________ 5
2 Introduction ___________________________________________________________________________ 6
3 What is BGScript? ______________________________________________________________________ 7
3.1 BGScript Scripting Language ________________________________________________________ 7
3.2 BGScript vs. BGAPI ________________________________________________________________ 8
4 BGScript Syntax _______________________________________________________________________ 9
4.1 Comments _______________________________________________________________________ 9
4.2 Variables and Values _______________________________________________________________ 9
4.2.1 Values ____________________________________________________________________ 9
4.2.2 Variables __________________________________________________________________ 9
4.2.3 Global Variables ___________________________________________________________ 11
4.2.4 Constant Values ___________________________________________________________ 11
4.2.5 Buffers ___________________________________________________________________ 11
4.2.6 Strings ___________________________________________________________________ 12
4.2.7 Constant Strings ___________________________________________________________ 12
4.3 Expressions _____________________________________________________________________ 14
4.4 Commands ______________________________________________________________________ 15
4.4.1 event <event_name> (<event_parameters>) _____________________________________ 15
4.4.2 if <expression> then [else] end if _______________________________________________ 15
4.4.3 while <expression> end while _________________________________________________ 16
4.4.4 call <command name>(<command parameters>..)[(response parameters)] _____________ 16
4.4.5 let <variable> = <expression> _________________________________________________ 16
4.4.6 return ____________________________________________________________________ 17
4.4.7 sfloat(mantissa, exponent) ___________________________________________________ 17
4.4.8 float(mantissa, exponent) ____________________________________________________ 18
4.4.9 memcpy(destination, source, length) ___________________________________________ 18
4.4.10 memcmp(buffer1, buffer2, length) _____________________________________________ 18
4.4.11 memset(buffer, value, length) ________________________________________________ 19
4.5 Procedures ______________________________________________________________________ 20
4.6 Using multiple script files ___________________________________________________________ 21
4.6.1 import ___________________________________________________________________ 21
4.6.2 export ___________________________________________________________________ 21
5 BGScript Limitations ___________________________________________________________________ 23
5.1 32-bit resolution __________________________________________________________________ 23
5.2 Declaration required before use ______________________________________________________ 23
5.3 Reading internal temperature meter disabled IO interrupts _________________________________ 23
5.4 Writing data to an endpoint, which is not read ___________________________________________ 23
5.5 No interrupts on Port 2 _____________________________________________________________ 23
5.6 Performance _____________________________________________________________________ 23
5.7 RAM ___________________________________________________________________________ 23
5.8 Flash ___________________________________________________________________________ 24
5.9 Stack ___________________________________________________________________________ 24
5.9.1 Interface drivers ____________________________________________________________ 24
5.10 Debugging _____________________________________________________________________ 24
6 Example BGscripts ____________________________________________________________________ 25
6.1 Basics __________________________________________________________________________ 25
6.1.1 Catching system start-up _____________________________________________________ 25
6.1.2 Catching Bluetooth connection event ___________________________________________ 26
6.1.3 Catching Bluetooth disconnection event _________________________________________ 27
6.2 Hardware interfaces _______________________________________________________________ 28
6.2.1 ADC _____________________________________________________________________ 28
6.2.2 I2C ______________________________________________________________________ 30
6.2.3 GPIO ____________________________________________________________________ 31
6.2.4 SPI ______________________________________________________________________ 33
6.2.5 Generating PWM signals ____________________________________________________ 35
6.3 Timers __________________________________________________________________________ 36
6.3.1 Continuous timer generated interrupt ____________________________________________ 36
6.3.2 Single timer generated interrupt ________________________________________________ 37
Silicon Labs Page of 3 55
6.4 USB and UART endpoints __________________________________________________________ 38
6.4.1 UART endpoint ____________________________________________________________ 38
6.4.2 USB endpoint _____________________________________________________________ 39
6.5 Attribute Protocol (ATT) ____________________________________________________________ 40
6.5.1 Catching attribute write event _________________________________________________ 40
6.6 Generic Attribute Profile (GATT) ______________________________________________________ 41
6.6.1 Changing device name ______________________________________________________ 41
6.6.2 Writing to local GATT database _______________________________________________ 42
6.7 PS store ________________________________________________________________________ 43
6.7.1 Writing a PS keys __________________________________________________________ 43
6.7.2 Reading a PS keys _________________________________________________________ 44
6.8 Flash ___________________________________________________________________________ 45
6.8.1 Erasing, Reading and Writing Flash ____________________________________________ 45
6.9 Advanced scripting examples ________________________________________________________ 47
6.9.1 Catching IO events and exposing them in GATT __________________________________ 47
6.10 Bluegiga Development Kit Specific Examples __________________________________________ 48
6.10.1 Display initialization ________________________________________________________ 48
6.10.2 FindMe demo ____________________________________________________________ 49
6.10.3 Temperature and battery readings to display ____________________________________ 50
6.11 BGScript tricks __________________________________________________________________ 52
6.11.1 HEX to ASCII _____________________________________________________________ 52
6.11.2 UINT to ASCII ____________________________________________________________ 52
7 BGScript editors ______________________________________________________________________ 54
7.1 Notepad ++ ______________________________________________________________________ 54
7.1.1 Syntax highlight for BGScript _________________________________________________ 54
Silicon Labs Page of 4 55
Version History
Bluetooth Low Energy Software 1.3.0 compatible document version. The limitation for the maximum size of all DIM variables is removed.
Bluetooth Low Energy Software 1.3.1 compatible document version. Editorial changes and Examples section updated.
Silicon Labs Page of 5 55

Introduction

This document briefly describes the Bluegiga BGScript programming language for Bluegiga Energy Products. It explains what the BGScript programming language is, what its benefits are, how it may be used, and what its limitations are. The document also contains multiple examples of BGScript code and some API methods, and how it can be used to perform various tasks such as detecting receiving and transmitting data, and managing hardware interfaces like UART, USB, SPI, and I2C.
Bluetooth
Bluetooth
connections,
Low
Silicon Labs Page of 6 55

What is BGScript?

3.1 BGScript Scripting Language
Bluegiga BGScript is a simple BASIC-style programming language that allows end-user applications to be embedded to the Bluegiga fully standalone size, cost and power consumption reductions. Although being a simple and easy-to-learn programming language BGScript does provide features and functions to create fairly complex and powerful applications and it provides the necessary APIs for managing Bluetooth connections, security, data transfer and various hardware interfaces such as UART, USB, SPI, I2C, GPIO, PWM and ADC.
BGScript is fully event based programming language and code execution is started when events such as system start-up,
BGScript applications are developed with Bluegiga's free-of-charge BGScript applications are executed in the BGScript Virtual Machine (VM) that is part of the Bluegiga Bluetooth Low Energy software. The Bluetooth Low Energy SDK comes with all the necessary tools for code editing and compilation and also the needed tools for installing the complied firmware binary to the Bluegiga Bluetooth Low Energy modules. Multiple example applications and code snipplets are also available for Bluegiga implementing applications like thermometers, heart rate transmitters, medical sensors and iBeacons just to mention a few.
The illustration below describes the Bluegiga Bluetooth Low Energy software, API and how BGScript VM and applications interface to it.
Bluetooth
Bluetooth
Bluetooth
Low Energy devices without the need of an external MCU and this enables further
connection, I/O interrupt etc. occur.
Low Energy modules. The benefit of using BGScript is that one can create
Bluetooth
Low Energy SDK and the
Silicon Labs Page of 7 55
Silicon Labs Page of 8 55
Silicon Labs Page of 9 55
Silicon Labs Page of 10 55
Silicon Labs Page of 11 55
Silicon Labs Page of 12 55
Silicon Labs Page of 13 55
Silicon Labs Page of 14 55
Silicon Labs Page of 15 55
Silicon Labs Page of 16 55
Silicon Labs Page of 17 55
Silicon Labs Page of 18 55
Silicon Labs Page of 19 55
Silicon Labs Page of 20 55
Silicon Labs Page of 21 55
Silicon Labs Page of 22 55

BGScript Limitations

5.1 32-bit resolution
All operations in BGScript must be done using values that fit into 32 bits. The limitation affects for example long timer intervals. Since the soft timer has a 32.768kHz tick speed, it is possible in theory to have maximum interval of (2^32-1)/32768kHz = 36.4h. If longer timer periods are needed, incremental counters need to be used.
Silicon Labs Page of 23 55
Silicon Labs Page of 24 55
Example BGscripts
This section contains some useful BGScript examples to illustrate syntax and a few of the API commands, responses, and events that you can use with BGScript.
Silicon Labs Page of 25 55
Silicon Labs Page of 26 55
Silicon Labs Page of 27 55

Hardware interfaces

This section contains basic examples that show how to use hardware interfaces such as I2C, SPI, analog input (ADC), etc. from within BGScript.
6.2.1 ADC
The analog-to-digital converter (ADC) built into the BLE modules provides a versatile 8-channel interface capable of up to 12 effective bits (including negative ranges). ADC conversions are triggered with the
Silicon Labs Page of 28 55
The example below shows how to read the internal battery monitor, and how to convert the battery voltage level
into percentage. A full example is included in the Bluetooth Low Energy SDK v1.1 or newer.
The above example requires the Bluetooth Low Energy SDK v1.1 or newer in order to work reliably. The code automatically turns off the external DC/DC (if used) when the AVDD/3 conversion starts, and then re-enables it after the conversion is complete.
Silicon Labs Page of 29 55
I2C
The BLE112 module has a software I2C implementation (bit-banging) which uses two fixed GPIO pins. For communicating over the I2C bus with the BLE112 module, the following hardware setup is needed:
Silicon Labs Page of 30 55

GPIO

GPIO wake-up
When the device has no active tasks (advertising, scanning, or connection maintenance) or user-defined soft timers running, it can enter the lowest power mode (PM3), which consumes only about 400nA (0.4uA). In order to wake up from PM3, an external wake-up signal on a GPIO is required.
The example here shows how an IO interrupt can be used to wake up the module, start advertising, and then stop advertising after 5 seconds so that the module can re-enter PM3 if a remote BLE device does not connect first.
The example above enables a user-defined GPIO interrupt that is handled by the BGScript application. This type of interrupt is triggered and processed only when the edge transition occurs, and once the
event handler has been executed, the module will return to a low-power mode if hardware_io_port_status() possible until the next similar edge transition. The Bluegiga BLE stack provides another way to wake the module in a more permanent, user-controllable manner, by enabling a dedicated . This pin will hold wake-up pin the module in an active/wake state as long as it is asserted, only allowing the module to sleep again when it is de-asserted. This can be very useful if you need to send UART traffic to the module, for instance, since UART data is not processed while the module is asleep.
The wake-up pin is configured in the "hardware.xml" file for your project, and is documented in the Bluetooth
. The following note provides an example configuration:Low Energy Module Configuration Guide
Silicon Labs Page of 31 55
Silicon Labs Page of 32 55
SPI
Silicon Labs Page of 33 55
Silicon Labs Page of 34 55
Silicon Labs Page of 35 55

Timers

This section describes how to use timers with BGscript.
6.3.1 Continuous timer generated interrupt
This example shows how to generate continuous timer generated interrupts
Silicon Labs Page of 36 55
Single timer generated interrupt
The 2nd example shows how to set a timer, which is called only once. This is useful, when some action needs to be implemented only once, like the change of advertisement interval in Proximity profile.
In this example in the beginning the device advertises quickly, but after 30 seconds the advertisement interval is reduced, in order to save battery.
Silicon Labs Page of 37 55
Silicon Labs Page of 38 55
Silicon Labs Page of 39 55

Attribute Protocol (ATT)

This section contains BGscript examples related to Attribute Protocol (ATT) events.
6.5.1 Catching attribute write event
The example shows to to catch an event when remote devices writes an attribute over a Bluetooth connection. A simple FindMe example is used where the remote device writes a single value to the local GATT database indicating the alert level.
Silicon Labs Page of 40 55

Generic Attribute Profile (GATT)

This section shows examples how to manager the local GATT database.
6.6.1 Changing device name
The example below shows how to change the device name using BGScript. In this example we use the following GATT database:
Silicon Labs Page of 41 55
Silicon Labs Page of 42 55
Silicon Labs Page of 43 55
Silicon Labs Page of 44 55

Flash

These examples show how to erase, write and read data from the user data section on the Bluetooth Low Energy devices flash.
6.8.1 Erasing, Reading and Writing Flash
The example shows how to use the raw flash API to access the user data area on the local flash memory.
Silicon Labs Page of 45 55
Silicon Labs Page of 46 55

Advanced scripting examples

This section shows more advanced scripting examples where several functions are made.
6.9.1 Catching IO events and exposing them in GATT
This example shows hot to catch I/O events and exposing them via a custom service in GATT data base. The example service looks like the one below and the I/O characteristic has
and
read notify
properties.
Silicon Labs Page of 47 55
.10 Bluegiga Development Kit Specific Examples
This section contains examples specific to the Bluegiga BLE development kits.
6.10.1 Display initialization
The example below shows how to initialize the display in the BLE development kit and and how to write data to it.
The supported commands can be found from the displays data sheet as well the initialization sequence.
Silicon Labs Page of 48 55
Silicon Labs Page of 49 55
Silicon Labs Page of 50 55
DKBLE112 display, battery and temperature sensors (CONTINUED)
#ADC event listener event hardware_adc_result(input,value)
# Received battery reading if (input = 6) then #Convert HEX to STRING milliv=value/11+8 tmp(0:1) = (milliv/1000) + (milliv / 10000*-10) + 48 tmp(1:1) = (milliv/100) + (milliv / 1000*-10) + 48 tmp(2:1) = (milliv/10) + (milliv / 100*-10) + 48 tmp(3:1) = (milliv) + (milliv / 10*-10) + 48
# Change display data address call hardware_io_port_write(1,$7,$1) call hardware_spi_transfer(0,1,"\x87")
# Write battery value call hardware_io_port_write(1,$7,$3) call hardware_spi_transfer(0,4,tmp(0:4)) call hardware_spi_transfer(0,3," mV") end if
# Received temperature reading if (input = 14) then offset=-1490
# ADC value is 12 MSB celsius = value / 16 # Calculate temperature # ADC*V_ref/ADC_max / T_coeff + offset celsius = (10*celsius*1150/2047) * 10/45 + offset
#Convert HEX to STRING string(0:1) = (celsius / 100) + 48 string(1:1) = (celsius / 10) + (celsius / -100 * 10) + 48 string(2:1) = celsius + (celsius / 10 * -10) + 48
# Change display data address call hardware_io_port_write(1,$7,$1) call hardware_spi_transfer(0,1,"\xc7")
# Write temperature value call hardware_io_port_write(1,$7,$3) call hardware_spi_transfer(0,2,string(0:2)) call hardware_spi_transfer(0,1,".") call hardware_spi_transfer(0,1,string(2:1)) call hardware_spi_transfer(0,1,"\xf2") call hardware_spi_transfer(0,1,"C") end if end
Bluegiga – A Silicon Labs Company Page of 51 55
Silicon Labs Page of 52 55
Silicon Labs Page of 53 55

BGScript editors

This section contains different tips and tricks for editors and IDEs.
7.1 Notepad ++
Notepad++ is very flexible text editor for programming purposes. Application and documentation can be downloaded from .http://notepad-plus-plus.org/
7.1.1 Syntax highlight for BGScript
Notepad++ doesn't currently contain syntax highlighting for BGScript by default. You can however download syntax highlighting rules defined by Bluegiga.
Installing the BGScript syntax highlight rules into Notepad++ is easy:
Download the syntax highlighting rules from
https://www.silabs.com/support/resources.p-wireless_bluetooth-low-energy_bluegiga-legacy-modules
Silicon Labs Page of 54 55
IoT Portfolio
www.silabs.com/IoT
SW/HW
www.silabs.com/simplicity
Quality
www.silabs.com/quality
Support and Community
community.silabs.com
Disclaimer
Silicon Labs intends to provide customers with the latest, accurate, and in-depth documentation of all peripherals and modules available for system and software implementers using or intending to use the Silicon Labs products. Characterization data, available modules and peripherals, memory sizes and memory addresses refer to each specific device, and "Typical" parameters provided can and do vary in different applications. Application examples described herein are for illustrative purposes only. Silicon Labs reserves the right to make changes without further notice to the product information, specifications, and descriptions herein, and does not give warranties as to the accuracy or completeness of the included information. Without prior notification, Silicon Labs may update product firmware during the manufacturing process for security or reliability reasons. Such changes will not alter the specifications or the performance of the product. Silicon Labs shall have no liability for the consequences of use of the information supplied in this document. This document does not imply or expressly grant any license to design or fabricate any integrated circuits. The products are not designed or authorized to be used within any FDA Class III devices, applications for which FDA premarket approval is required or Life Support Systems without the specific written consent of Silicon Labs. A "Life Support System" is any product or system intended to support or sustain life and/or health, which, if it fails, can be reasonably expected to result in significant personal injury or death. Silicon Labs products are not designed or authorized for military applications. Silicon Labs products shall under no circumstances be used in weapons of mass destruction including (but not limited to) nuclear, biological or chemical weapons, or missiles capable of delivering such weapons. Silicon Labs disclaims all express and implied warranties and shall not be responsible or liable for any injuries or damages related to use of a Silicon Labs product in such unauthorized applications.
Trademark Information
Silicon Laboratories Inc.® , Silicon Laboratories®, Silicon Labs®, SiLabs® and the Silicon Labs logo®, Bluegiga®, Bluegiga Logo®, ClockBuilder®, CMEMS®, DSPLL®, EFM®, EFM32®, EFR, Ember®, Energy Micro, Energy Micro logo and combinations thereof, "the world’s most energy friendly microcontrollers", Ember®, EZLink®, EZRadio®, EZRadioPRO®, Gecko®, Gecko OS, Gecko OS Studio, ISOmodem®, Precision32®, ProSLIC®, Simplicity Studio®, SiPHY®, Telegesis, the Telegesis Logo®, USBXpress® , Zentri, the Zentri logo and Zentri DMS, Z­Wave®, and others are trademarks or registered trademarks of Silicon Labs. ARM, CORTEX, Cortex-M3 and THUMB are trademarks or registered trademarks of ARM Holdings. Keil is a registered trademark of ARM Limited. Wi-Fi is a registered trademark of the Wi-Fi Alliance. All other products or brand names mentioned herein are trademarks of their respective holders.
Silicon Laboratories Inc. 400 West Cesar Chavez Austin, TX 78701 USA
http://www.silabs.com
Simplicity Studio
One-click access to MCU and wireless tools, documentation, software, source code libraries & more. Available for Windows, Mac and Linux!
Loading...