Introduction

Blue Basic is a heavily modified port of the TinyBasic interpreter developed to run on the Texas Instruments CC2540 and CC2541 Bluetooth LE chips (BLE) (see http://www.ti.com/product/CC2540 and http://www.ti.com/product/CC2541).

Motivation

If you want to develop code for these chips you have to buy a compiler. The compiler is from IAR Systems and, while it is quite a nice compiler as embedded development goes, it also costs $3,500!! Yes, they do a kickstart version but the code size is far too small to compile even the basic BLE stack from TI, let alone any code you might want to add. There are various low-cost solutions to this problem. One popular option is to buy the device with pre-programmed software, and use it with an Arduino system. There are some very nice devices based on this idea such as the LightBlue Bean (http://punchthrough.com/bean/) from Punch Through Design. But fundamentally the BLE chip is more than capable of running any program you might put on the Arduino; so this seems like a waste. Then there's power consideration, complexity and cost of adding the extra processor.

But finally, and personally, this is just annoying. There's no reason, other than cost, that everyone shouldn't be able to by a cheap BLE chip and program it the way they want for free. That's what Open Software and Open Hardware is about.

Alternative

I have a history of writing interpreter and virtual machines for small device, including the first open source Java VM "Kaffe" (https://github.com/kaffe/kaffe), the original Javacard implementation and the first .NET VM used in smartcards. It seems like a similar, small interpreter approach, could be used to open up the power of these BLE chips. If the chip runs a simple, well understood language, then it become easy for developers, makers, and experiments to write custom code directly on the chip itself, and build useful and interesting things. And, of course, do it for free.

Back to BASIC

I wanted to build an interpreter for these BLE chips which was easy to use and required no extra tools to use. I wanted anyone to be able to take the code, flash it onto any available CC254X chip, and start customizing. This reminded me of my earliest computer, the  Sinclair ZX81 (http://en.wikipedia.org/wiki/Zx81). And this computer ran BASIC with only 1K of RAM. BASIC is an easy language to learn, and particular good for direct, on device development, where we want to reduce the "edit, compile, upload, and run" sequence into "edit and run". It's also very easy to write a compact interpreter. So BASIC is the language of choice.

Bluetooth and other Hardware

Of course, easy access to the Bluetooth stack and access to all the other hardware the CC254X chips have (e.g. digital I/O, ADCs, interrupts, timers, power management, SPI, UART, USB, etc.) is also essential. The BASIC language has been extended to make these features easy and familiar. Where possible, these addition follow the conventions popularized by the Arduino platform. So, for example, to enable pin P0(0) for input, you would use the BASIC command "PINMODE P(0) INPUT" which is similar to the Arduino syntax "pinMode(A0, INPUT)". Bluetooth keywords are model after the Services and Characteristics used in the Bluetooth LE profile and are design to make it simple to create a service, and connect it to other hardware.

A simple service might look like this:

10 ADVERT GENERAL

20 ADVERT "25FB9E91-1616-448D-B5A3-F70A64BDA73A"

30 ADVERT END

40 GATT SERVICE "25FB9E91-1616-448D-B5A3-F70A64BDA73A"

50 GATT CHARACTERISTIC "D8ABBBE7-F10B-4EC3-B781-DBCBD2334400" "On/Off"

60 GATT READ WRITE A ONWRITE GOSUB 200

70 GATT END

80 PINMODE P0(0) OUTPUT

90 GOSUB 200

100 END

200 P0(0) = A

210 RETURN

Obviously there's much to explain here, but essentially this creates and advertises a simple service with one control "On/Off" which, when written to, turns on and off a single output. On my test board this is connected to an LED.

Try it yourself

At some point I will build some hardware specifically loaded with Blue Basic and make it available (including all the Eagle files and so forth). In the meantime you're welcome to try this yourself on your favorite TI BLE hacking board. Some cheap ones can be found at  FastTech (http://www.fasttech.com/product/1292002-ti-cc2540-bluetooth-4-0-ble-2540-transparent) for $7 (including shipping) although they are tricky to solder. These are made by Huamao Technology Co. Ltd (http://www.jnhuamao.cn/bluetooth.asp?ID=1). Alternatively, you can buy a Texas Instrument Development Kit (http://www.ti.com/tool/cc2540dk-mini). You will also need the CC Debugger tool to flash the chips. The software for this is free from TI, but the hardware is about $60 (http://www.amazon.com/gp/offer-listing/B00GRLWU9U) (it comes with the TI dev kit).

Hex

To avoid having to buy a very expansive compiler (rather the point of this), you can find the HEX upload files here:

https://github.com/aanon4/BlueBasic/tree/master/hex

You also need the console app which you can find here:

https://github.com/aanon4/BlueBasic-Console