0
votes

I'm using a pair of MRF24J40 radio chips to get one PIC32 microcontroller board to communicate with another via RF transmission. I've got all my code to compile fine but I keep getting an error associated with this code.

typedef struct
    {
        union
        {
            BYTE        Val;  
            struct
            {
                BYTE    packetType      :2;             // type of packet. Possible types are
                                                        // * PACKET_TYPE_DATA - Data type
                                                        // * PACKET_TYPE_COMMAND -  Command type
                                                        // * PACKET_TYPE_ACK -  Acknowledgement type
                                                        // * PACKET_TYPE_RESERVE - Reserved type
                BYTE    broadcast       :1;             // 1: broadcast, 0: unicast
                BYTE    secEn           :1;             // 1: secure the MAC payload, 0: send plain text
                BYTE    repeat          :1;             // 1: allow repeaters to forward the message, 0: send message directly
                BYTE    ackReq          :1;             // 1: acknowledgement required, 0: no acknowldgement
                BYTE    destPrsnt       :1;             // 1: destination address in the packet, 0: destination address not in the packet
                BYTE    sourcePrsnt     :1;             // 1: source address in the packet, 0: source address not in the packet
            } bits;
        } flags;

        BYTE *      SourceAddress;                      // Address of the Sender
        BYTE *      Payload;                            // Pointer to the payload
        BYTE        PayloadLen;                         // Payload size
        BYTE        RSSIValue;                          // RSSI value for the received packet
        BYTE        LQIValue;                           // LQI value for the received packet
        #if defined(IEEE_802_15_4)
            BOOL        altSourceAddress;               // Source address is the alternative network address
            WORD_VAL    SourcePANID;                    // PAN ID of the sender
        #endif
    } MAC_RECEIVED_PACKET;

Basically I've tried everything on earth to be able to change the values of the variables packetType, secEn, ackReq etc. I've tried changing the value directly after their declarations but that seems to be the bit length, not the value. The code (straight from microchip's website) has comments that say 1 = this and 0 = that but I haven't found anywhere where I can change those values. Any help from anyone familiar with these MRF24J40 chips would be greatly appreciated. Thank you.

2

2 Answers

0
votes

I don't think this has anything specific to do with your microcontroller, it's just that you may not be familiar with how struct and union are used to define bitfields in C.

MAC_RECEIVED_PACKET is a struct that has a field called flags. flags is a union between a BYTE and a struct bitfield called bits.

In the declaration, each field in bits is followed by its bit length. Thus, e.g., packetType, which is 2 bits can take on values 0, 1, 2, 3. You set the values like this:

MAC_RECEIVED_PACKET foo;
foo.flags.bits.packetType = 3;
foo.flags.bits.secEn      = 1;
/* etc. */
0
votes

depends on what you mean when you say that you've tried to change the values ​​.. for example if you want to choose the type of packet and set PACKET_TYPE_ACK - Acknowledgement type:

MAC_RECEIVED_PACKET myrpacket; 

myrpacket.flags.bits.packetType=2;

packetType can take this values:

00 =0 --> PACKET_TYPE_DATA
01 =1 --> PACKET_TYPE_COMMAND
10 =2 --> PACKET_TYPE_ACK
11 =3 --> PACKET_TYPE_RESERVE

why?

bits, is a BYTE data ,and packetType ,are the first 2 bits of this byte, as is declared in the structure:

  BYTE    packetType      :2;

so if you want setup ackReq

  myrpacket.flags.bits.ackReq=1;

hope this help you