Friday, August 29, 2014

Bluegiga’s BLED112, BGLib and Code::Blocks

There is a lot of good documentation on BGLib and the BGAPI on Bluegiga’s website.  I found the first couple of slides in the BGAPI BGLib Overview presentation very helpful for a high-level understanding.

Along with the sdk source two examples are installed.  Reviewing these examples provides additional information on how to use the sdk.

After you successfully install the Bluegiga SDK you will end up with a ble-1.2.2-100 folder (assuming you install version 1.2.2 of the sdk) under the install directory.  I let the install proceed with defaults so on my system it ended up on C:\Bluegiga.  The sdk is in the src directory.

image

The sdk consists of two c files and two header files.

image

  • apitypes.h consists of a number of type definitions.
  • cmd_def.h defines a number of constants and data structures.
  • cmd_def.c sets up the data structure that is used to dispatch events based on the packets received.
  • commands.c defines the default functions that get called when an event is dispatched.  These functions have no executable statements and therefore when called just return.  In your program you will need to define those functions you wish to use and comment them out or remove them from commands.c

In the examples commands.c has been renamed to stubs.c.  The examples also contain a Makefile for building them.  The Makefile contains logic for MINGW under Windows but as was planning to  use Code::Blocks I didn’t test them.

In using BGLib with MINGW in Code::Blocks 12.11 I found that the apitypes.h and cmd_def.h files had to be modified.  The modifications were necessary to get the PACKSTRUCT macro defined properly.  The problem is that PACKED is defined in windef.h for MINGW without gcc_struct which causes incorrect packing of data.

In apitypes.h I commented out the lines beginning with #ifdef __GNUC__ and ending with the corresponding #endif.  In cmd_def.h I commented out the lines starting with #ifdef PACKED down to the corresponding #else and then the corresponding #endif several lines below.

apitypes.h modifications:

image

cmd_def.h modifications:

image

To setup the scan_demo example in Code::Blocks I suggest the following steps:

Execute Code::Blocks and under File select New and create a Console project for C.

image

Enter scan_demo as the project title.  Code::Blocks will create new project folder in your projects folder.

image

The new project will have a main.c file and a scan_demo.cbp Code::Blocks project file.

image

Close Code:: Blocks and go to the folder with the Bluegiga scan_demo in it and copy all the apitypes.h, cmd_def.c, cmd)def.h, main.c and stubs.c and paste these into the Code::Blocks project folder.  You will get a warning about main.c as it already exists in the Code::Blocks project folder.  Answer that you want to replace the existing file with the copied file.

image

Execute Code::Blocks and open your scan_demo project.  Select the scan_demo project in the left side window and the select Project from the menu bar and Add Files…  Select the new files you added to your project from the example directory.

image

Using the gear icon build the project.

image

As the scan_demo program requires the COM port of the BLED112 as a parameter you will need to open a console window and navigate to the executable in your scan_demo project directory.  In this case I built a Debug copy so it was in the bin\Debug directory.

Run the scan_demo program with the appropriate COM port.  In my case this was com8:

scan_demo com8

As long as you have a BLE device that is advertising you will see its address along with the RSSI power value in the console window.

image

You should now be ready to move on to compiling an example that receives data from your BLE device.  In my case that is an RFduino which is the subject of my next post.

Hope you find this helpful.  My thanks to Bluegiga for their sdk and the documentation on their website and to the folks responsible for Code::Blocks.

If you find something that doesn’t work for you or you have questions please post them as comments and I will get back to you as soon as I can.

2 comments:

Anonymous said...

thanks a lot for this tutorial.
easy to follow.

forengineer said...

In the latest version of bglib directory the cmd_def.h file reads as follows. Following your post from 2014, I am having an issue of what changes, if any, are required with this newer version. Thanks!

/* Compability */
#ifndef PACKSTRUCT
/*Default packed configuration*/
#ifdef __GNUC__
#ifdef _WIN32
#define PACKSTRUCT( decl ) decl __attribute__((__packed__,gcc_struct))
#else
#define PACKSTRUCT( decl ) decl __attribute__((__packed__))
#endif
#define ALIGNED __attribute__((aligned(0x4)))
#elif __IAR_SYSTEMS_ICC__

#define PACKSTRUCT( decl ) __packed decl

#define ALIGNED
#elif _MSC_VER //msvc

#define PACKSTRUCT( decl ) __pragma( pack(push, 1) ) decl __pragma( pack(pop) )
#define ALIGNED
#else
#define PACKSTRUCT(a) a PACKED
#endif
#endif