Difference between revisions of "MansOS Radio"

From DiLab
Jump to: navigation, search
(Examples)
Line 81: Line 81:
  
 
== Examples ==
 
== Examples ==
 +
 +
apps/demo/SimpleRadioCountToLeds
  
 
<source lang="c">
 
<source lang="c">

Revision as of 15:25, 16 January 2012

API header file: mos/hil/radio.h

MansOS provides API for Radio communication in physical layer. Platforms without radio modules are also supported, with NULL-radio implementation.

Currently, only one default radio module is supported. When using multiple radio chips on one platform, other radios should be interfaced directly, using chip drivers.

Usage and configuration

Radio support is turned ON by default. It can be disabled by adding the following line to the application config file:

USE_RADIO = n

Dependencies

There are no dependencies for using Radio. However, when using remote reprogramming depends on radio, therefore radio usage is turned on, if remote reprogramming is used.

Function reference

All platform-independent radio functions are described in mos/hil/radio.h . Implementation can be found under mos/hal/platforms/<platform-name>/radio_hal.h

 // Initialize radio, called by the kernel on startup
 void radioInit() 
 
 // Turn the radio listening on. Note that listening is not required if radio is used only to send data
 void radioOn()

 // Turn the radio listening off (and save energy)
 void radioOff()

 // Transmit <len> bytes long data buffer over radio. Radio must be turned on before calling this function!
 // Returns 0 on success, error code as negative value on failure (see mos/hil/errors.h)
 int8_t radioSend(const void *data, uint16_t len)

 // Transmit one byte over radio. Radio must be turned on before calling this function!
 // Returns 0 on success, error code as negative value on failure (see mos/hil/errors.h)
 int8_t radioSendByte(uint8_t data)

 // Write received data into the buffer. It should be called after the radio driver has signaled
 // data availability. Non-blocking function (does not wait for data to arrive)
 // Returns received data length on success, error code as negative value on failure (see mos/hil/errors.h)
 int16_t radioRecv(void *buffer, uint16_t bufferLength)

 // Similar to radioRecv(), but does not keep the result, just clear radio chip data buffers
 void radioDiscard()

// Set a new radio callback function.
// The callback function is called when a packet becomes available.
// In general, the callback function should call radioRecv() to read the packet.
// Returns: old callback function or NULL
RadioRecvFunction radioSetReceiveHandle(RadioRecvFunction functionHandle);

 // Measure current Received Signal Strength Indicator (RSSI)
 int radioGetRSSI()

 // Get Received Signal Strength Indicator (RSSI) of the last received packet
 int8_t radioGetLastRSSI()

 // Get Link Quality Indicator (LQI) of the last received packet
 int8_t radioGetLastLQI()

// Set radio channel to use for data transmission and reception
// The exact behavior of these functions is platform- and chip-dependent.
// For IEEE 802.15.4 compatible radios (such as the CC2420)
// there are 16 channels available in the 2.4 GHz band
// in 5 MHz steps, numbered 11 through 26
void radioSetChannel(int channel)

// Transmit power control.
// The exact behavior of these functions is platform- and chip-dependent.
// For CC2420, a value in range [0 .. 31] is expected,
// where 0 corresponds to the minimal transmit power and 31 - to the maximum
void radioSetTxPower(uint8_t power)

// Returns true if Clear Channel Assessment (CCA) detects that transmission medium is free.
// Always returns true if the chip does not have support for hardware CCA.
bool radioIsChannelClear()

Examples

apps/demo/SimpleRadioCountToLeds
//-----------------------------------------------------------------------------
//  Counter exchange application
//  Mote broadcasts its counter which gets incremented every second.
//  When receiving a counter value, mote displays it on leds.
//  Implementation without threads and sockets
//-----------------------------------------------------------------------------

#include "stdmansos.h"
#include "dprint.h"

// Called when radio packet received - display received counter on LEDs
void radioReceived() {
    static uint16_t counter;
    int16_t len;
    len = radioRecv(&counter, sizeof(counter)); // Read counter - 2 bytes
    if (len > 0) {
        setLeds(counter); // display it on LEDs
    } else if (len < 0 ) {
        // Error - reception failed
    }
}


//-------------------------------------------
//      Entry point for the application
//-------------------------------------------
void appMain(void)
{
    radioInit();
    radioOn();

    // when a radio packet is received, call radioReceived() function
    radioSetReceiveHandle(radioReceived);

    sendCounter();
}


// Send an incrementing counter every second
void sendCounter() {
    uint16_t counter = 0;

    // repeat continuously
    while (1)
    {
        // send counter over radio
        if (radioSend(&counter, sizeof(counter)) != 0) {
            // radio sending failed - handle it!
        }
        msleep(1000); // sleep one second
        ++counter;
    }
}