A Simple Personal Area Network(IEEE802.15.4) with Microchip MRF24J40

This document describes how to implement IEEE 802.15.4 for a simple wireless network using Microchip MRF24J40 module.

MRF24J40:

MRF24J40 is a complete IEEE 802.15.4 radio and operates in the 2.4GHz freq band.  The MRF24J40 supports ZigBee™, MiWi™ and proprietary protocols to provide an ideal solution for wireless sensor networks, home automation, building automation and consumer applications.

Features:

  • Complete IEEE 802.15.4 radio
  • Supports ZigBee™ and MiWi™ protocols
  • Simple four-wire SPI interface
  • Integrated 20 MHz and 32.768 kHz oscillator drive
  • Low current consumption
  • Hardware CSMA-CA Mechanism
  • Automatic ACK response
  • Hardware security engine (AES-128)
  • Automatic packet retransmit capable
  • Small 40-pin leadless QFN 6×6 mm2 package

Reference: Microchip official website

Modes:

As far as I know this module have designed for small wireless solutions.

This module can work in 3 modes:

  • Promiscuous Mode
  • Error Mode
  • IEEE 802.15.4 (Normal Mode)

The difference between “Promiscuous Mode” and “Error Mode” is in CRC,In “Error Mode”, packets are accepted with a good or bad CRC but in “Promiscuous Mode”, it only packets with a good CRC are accepted and actually it doesn’t check any PANID,address and extended address.

In “Normal Mode”, only packets with a good CRC are accepted and this module satisfies the requirements of the IEEE 802.15.4 Specification as below:

1.The frame type subfield of the frame control field shall not contain an illegal frame type.
2.If the frame type indicates that the frame is a beacon frame, the source PAN identifier shall match macPANId unless macPANID is equal to 0xFFFF, in which case, the beacon frame will be accepted regardless of the source PAN identifier.
3.If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the broadcast PAN identifier (0xFFFF).
4.If a short destination address is included in the frame, it shall match either macShortAddressor the broadcast address (0xFFFF). Otherwise, if an extended destination address is included in the frame, it shall match aExtendedAddress.
5.If only source addressing fields are included in a data or MAC command frame, the frame shall be accepted only if the device is a PAN coordinator and the source PAN identifier matches macPANId.

IEEE 802.15.4 Data Packet:

The below image shows the Data Packets:

IEEE802.15.4_PacketIn this document I want to describe the “Data Frame Format” as it can be so useful

The “Header” is important part of packet:

IEEE802.15.4_MAC_Frame

In “Header”, the “Frame Control” takes an important role:

IEEE802.15.4_Frame_controlThe details of each field are out of this document’s scope, for more information, I recommend you to read IEEE 802.15.4 Specification.

Code:

typedef enum
{
  Beacon,
  Data,
  Acknowledgment,
  MacCommand
} IEEEFrameType;

typedef enum
{
  PanAndAddressNotPresent,
  Reserved,
  ShortAddress,
  ExtendedAddress
} IEEEAddressingMode;

typedef union
{
  struct // Big Endian
  {
    unsigned char HighByte;
    unsigned char LowByte;
  };

  unsigned short All;
} IEEEAddress;

typedef union
{
  struct // Big Endian
  {
    unsigned char Reserved2 : 2;
    IEEEAddressingMode DestAddressingMode : 2;
    unsigned char FrameVersion : 2;
    IEEEAddressingMode SourceAddressingMode : 2;

    IEEEFrameType FrameType : 3;
    unsigned char SecurityEnable : 1;
    unsigned char FramePending : 1;
    unsigned char AckRequired : 1;
    unsigned char PanIdCompression : 1;
    unsigned char Reserved1 : 1;
  } bits;

  IEEEAddress Value;
} IEEEFrameControl;

typedef struct
{
  unsigned char HeaderLenght;
  unsigned char FrameLenght;
  IEEEFrameControl FrameControl; 
  unsigned char SentSequenceNumber;
  unsigned char ReceivedSequenceNumber;
  IEEEAddress DestinationPanId;
  IEEEAddress DestinationAddress;
  IEEEAddress SourcePanId;
  IEEEAddress SourceAddress;
  unsigned char Payload[128];
} IEEEMacFrame;

And then you can use this code easily:

    IEEEMacFrame mrfIeeeMacFrame;
    mrfIeeeMacFrame.HeaderLenght                            = 11;
    mrfIeeeMacFrame.FrameControl.bits.FrameType             = Data;
    mrfIeeeMacFrame.FrameControl.bits.SecurityEnable        = 0;
    mrfIeeeMacFrame.FrameControl.bits.FramePending          = 0;
    mrfIeeeMacFrame.FrameControl.bits.AckRequired           = 0;
    mrfIeeeMacFrame.FrameControl.bits.PanIdCompression      = 1;
    mrfIeeeMacFrame.FrameControl.bits.Reserved1             = 0;
    mrfIeeeMacFrame.FrameControl.bits.Reserved2             = 0;
    mrfIeeeMacFrame.FrameControl.bits.DestAddressingMode    = ShortAddress;
    mrfIeeeMacFrame.FrameControl.bits.FrameVersion          = 1;
    mrfIeeeMacFrame.FrameControl.bits.SourceAddressingMode  = ShortAddress;
    
    mrfIeeeMacFrame.SourcePanId.All = sourcePanId;
    mrfIeeeMacFrame.SourceAddress.All = sourceAddress;

You can download the complete code with IEEE implementation and MRF24J40 functions:

The Code has been written for STM8 micro-controller,but if you want to change the type of micro-controller you might have to change the “Mrf24j40PinInit” and the Low Level functions such as SPI read/write and GPIO functions.

The STM8 micro-controller memory is Big-Endian, If your micro-controller Memory is Little-Endian you have to change order of Union members.

You can download it here:MRF24J40_IEEE802.15.4.zip