Skip Navigation LinksProjects > LPC1343 Reference Design > Tutorial: Using Chibi (802.15.4 Wireless Stack)

Using Chibi (Open-Source 802.15.4 Wireless Stack)

Using Chibi, and open source 802.15.4 wireless stack from Freaklabs, with the LPC1114 or LPC1343

Chibi (a slightly derogatory Japanese term for 'midget') is an easy-to-use, open-source 802.15.4 wireless stack from Freaklabs.  It allows light-weight 'point-to-point' or 'point-to-multipoint' communications with a minimum of effort, and is currently based on the highly-integrated AT86RF212 (700-900MHz) and AT86RF230 (2.4GHz) transceivers from Atmel.

A slightly modified version of Chibi is included in the LPC1343 Code Base and LPC1114 Code Base, allowing you to use it out of the box, though the only transceiver currently supported is the 700-900MHz AT86RF212 (800/900MHz offers significantly better signal penetration in an urban environment than 2.4GHz, though at the expense of slightly reduced transmit speeds).  This tutorial will show you how you can get started with Chibi using the LPC1114 or LPC1343.

You may also be interested in our tutorial on Using Freaklabs RF Antennas with the LPC1114 or LPC1343 if you haven't yet connected a transceiver to your development board.  It has links to some of the antenna boards available directly from Freaklabs if you wish to support some of the work they're doing.

Chibi Node Addresses

Each sensor node with Chibi has a unique 16-bit address (1-65534 or 0x0001-0xFFFE).  To send a point-to-point message you simply need to use the target node's 16-bit address (for example, 0x1234).  To send a point-to-multi-point (or 'global') message, you simply use the global 0xFFFF address, which will cause the outgoing message to be received by every node within range. It's pretty simple, but here's a quick 'Chibi message addressing for dummies' diagram just in case:

Chibi message addressing

Please note that there is no mechanism in place to check address conflicts, and you can give multiple nodes the same address. There are situations where this can actually be useful, but unless it's intentional it's best to try to avoid using the same address twice by attaching a small label on each board with the node's address.  With more than 16500 possible IDs to chose from, you'll run into far bigger problems than addressing before you start running out of potential IDs. 

Basic Mode: Using Chibi via the UART Command-Line Interface

The easiest way to get started with Chibi is to use the command-line interface included with both the LPC1114 Code Base and LPC1343 Code Base (assuming CFG_INTERFACE and CFG_CHIBI were both enabled during compilation). You will need to have one UART to USB adapter for each board to connect the development board to your PC. In this tutorial, we will be using Realterm for our terminal software, though any decent software should work just as well.

chb-addr - Gets or sets the sensor node's unique 16-bit address

Each sensor node in Chibi should be assigned a unique 16-bit address that distinguises it from other sensor nodes. This is particularly important if you wish to have 'point-to-point' communications, rather than just 'point-to-multipoint' (where every message sent out is transmitted to every other node in range). This 16-bit value is stored in the on-board 4K EEPROM, and can be read or updated via the chb-addr command.

The following command will return the current sensor node's address, or change the address and write the updated value to EEPROM. Note that either hexadecimal (preceded by '0x') or decimal (1-65534) values can be used when assigning an address, though they must be within the valid address range of 0x0001-0xFFFE or 1-65534:
chb-addr
Which will return:
"0x0011" (for example)
To set the address send:
chb-addr 0x0421
Which will return:
"Address set to: 0x0421"
NOTE: This command was renamed to 'A' in recent version of the LPC1343 Code Base (v1.0.0 and higher).
chb-send - Transmits the suppled text/value

To send a message, you simply need to provide the target sensor node's address (or 0xFFFF if you wish to send the message to every node within range), along with the message contents (as long at the entire command doesn't exceed to max command size defined via CFG_INTERFACE_MAXMSGSIZE).

The following command will send "Test message" to every node within range:
chb-send 0xFFFF Test message
Which will result in the following being displayed in the command-line interface of every other node within range:
Message received from node 0x0022: Test message  (rssi=27)
NOTE: This command was renamed to 'S' in recent version of the LPC1343 Code Base (v1.0.0 and higher).

chb-send transmit example chb-send receive example

Advanced Mode: Using Chibi Programmatically

You can also access the relatively easy-to-use Chibi API to send or receive messages programmatically.  Chibi is design with a 'Peripheral Control Block' (PCB), which can be used to check Chibi's current state or check if any incoming messages have been received.  An instance of the PCB can always be retrieved with the following command:

Get a reference to the Chibi Peripheral Control Block
// Get a reference to the Chibi PCB
chb_pcb_t *pcb = chb_get_pcb();

Sending and receiving message is relatively straight forward. The following code, for example, will initialise Chibi, get an instance of the PCB, and constantly check if any new messages are available. When a message is received the LED will briefly blink and the message contents, source address and signal strength will be displayed using printf:

Check for Incoming Messages
#include "drivers/chibi/chb.h"
static chb_rx_data_t rx_data;
...
int main (void)
{
  ... // Initialise the CPU, etc.

  // Initialise Chibi
  chb_init();

  // Get a reference to the Chibi PCB
  chb_pcb_t *pcb = chb_get_pcb();

  while(1)
  {
    // Check for incoming messages
    while (pcb->data_rcv)
    {
      // Retrieve message size and details
      rx_data.len = chb_read(&rx_data);
      // Enable LED to indicate message reception
      gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON);
      // Output message to UART
      printf("Message received from node 0x%04X: %s (rssi=%d)\r\n",
          rx_data.src_addr, rx_data.data, pcb->ed);
      // Disable LED
      gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);
    }
  }
}

The following code can be used to send a message (which will be received by every node in range in this case, since we are using the global address 0xFFFF.

Send a message
#include "drivers/chibi/chb.h"

int main (void)
{
  ... // Initialise the CPU, systick timer, etc.

  // Initialise Chibi
  chb_init();

  // Send global message every 500mS
  while(1)
  {
    // Wait 500mS
    systickDelay(500 / CFG_SYSTICK_DELAY_IN_MS);

    // Turn LED on to signal message transmission
    gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON);

    // Send the message
    char *text = "Test";
    chb_write(0xFFFF, text, sizeof(text));

    // Turn the LED off
    gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);
  }
}

  • Facebook
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Furl it!

Comments