Exemplo n.º 1
0
        /// <summary>
        /// Configure the module basic settings. Module needs to be initiaized.
        /// </summary>
        /// <param name="address">RF address (3-5 bytes). The width of this address determins the width of all addresses used for sending/receiving.</param>
        /// <param name="channel">RF channel (0-127)</param>
        /// <param name="dataRate">Data Rate to use</param>
        public void Configure(byte[] address, byte channel, NRFDataRate dataRate)
        {
            CheckIsInitialized();
            AddressWidth.Check(address);

            // Set radio channel
            Execute(Commands.W_REGISTER, Registers.RF_CH,
                    new[]
                        {
                            (byte) (channel & 0x7F) // channel is 7 bits
                        });

            // Set Data rate
            var regValue = Execute(Commands.R_REGISTER, Registers.RF_SETUP, new byte[1])[1];

            switch (dataRate)
            {
                case NRFDataRate.DR1Mbps:
                    regValue &= (byte)~(1 << Bits.RF_DR_LOW);  // 0
                    regValue &= (byte)~(1 << Bits.RF_DR_HIGH); // 0
                    break;

                case NRFDataRate.DR2Mbps:
                    regValue &= (byte)~(1 << Bits.RF_DR_LOW);  // 0
                    regValue |= (byte)(1 << Bits.RF_DR_HIGH);  // 1
                    break;

                case NRFDataRate.DR250kbps:
                    regValue |= (byte)(1 << Bits.RF_DR_LOW);   // 1
                    regValue &= (byte)~(1 << Bits.RF_DR_HIGH); // 0
                    break;

                default:
                    throw new ArgumentOutOfRangeException("dataRate");
            }

            Execute(Commands.W_REGISTER, Registers.RF_SETUP, new[] { regValue });

            // Enable dynamic payload length
            Execute(Commands.W_REGISTER, Registers.FEATURE,
                    new[]
                        {
                            (byte) (1 << Bits.EN_DPL)
                        });

            // Set auto-ack
            Execute(Commands.W_REGISTER, Registers.EN_AA,
                    new[]
                        {
                            (byte) (1 << Bits.ENAA_P0 |
                                    1 << Bits.ENAA_P1)
                        });

            // Set dynamic payload length for pipes
            Execute(Commands.W_REGISTER, Registers.DYNPD,
                    new[]
                        {
                            (byte) (1 << Bits.DPL_P0 |
                                    1 << Bits.DPL_P1)
                        });

            // Flush RX FIFO
            Execute(Commands.FLUSH_RX, 0x00, new byte[0]);

            // Flush TX FIFO
            Execute(Commands.FLUSH_TX, 0x00, new byte[0]);

            // Clear IRQ Masks
            Execute(Commands.W_REGISTER, Registers.STATUS,
                    new[]
                        {
                            (byte) (1 << Bits.MASK_RX_DR |
                                    1 << Bits.MASK_TX_DS |
                                    1 << Bits.MAX_RT)
                        });

            // Set default address
            Execute(Commands.W_REGISTER, Registers.SETUP_AW,
                    new[]
                        {
                            AddressWidth.Get(address)
                        });

            // Set module address
            _slot0Address = address;
            Execute(Commands.W_REGISTER, (byte)AddressSlot.Zero, address);

            // Set retransmission values
            Execute(Commands.W_REGISTER, Registers.SETUP_RETR,
                    new[]
                        {
                            (byte) (0x0F << Bits.ARD |
                                    0x0F << Bits.ARC)
                        });

            // Setup, CRC enabled, Power Up, PRX
            SetReceiveMode();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Configure the module basic settings. Module needs to be initiaized.
        /// </summary>
        /// <param name="address">RF address (3-5 bytes). The width of this address determins the width of all addresses used for sending/receiving.</param>
        /// <param name="channel">RF channel (0-127)</param>
        /// <param name="dataRate">Data Rate to use</param>
        public void Configure(byte[] address, byte channel, NRFDataRate dataRate)
        {
            CheckIsInitialized();
            AddressWidth.Check(address);

            // Address must be LSByte to MSByte per nRF24L01+ Datasheet
            address = ByteReverse(address);

            // Set radio channel
            Execute(Commands.W_REGISTER, Registers.RF_CH,
                    new[]
            {
                (byte)(channel & 0x7F)              // channel is 7 bits
            });

            // Set Data rate
            var regValue = Execute(Commands.R_REGISTER, Registers.RF_SETUP, new byte[1])[1];

            switch (dataRate)
            {
            case NRFDataRate.DR1Mbps:
                regValue &= (byte)~(1 << Bits.RF_DR_LOW);      // 0
                regValue &= (byte)~(1 << Bits.RF_DR_HIGH);     // 0
                break;

            case NRFDataRate.DR2Mbps:
                regValue &= (byte)~(1 << Bits.RF_DR_LOW);      // 0
                regValue |= (byte)(1 << Bits.RF_DR_HIGH);      // 1
                break;

            case NRFDataRate.DR250kbps:
                regValue |= (byte)(1 << Bits.RF_DR_LOW);       // 1
                regValue &= (byte)~(1 << Bits.RF_DR_HIGH);     // 0
                break;

            default:
                throw new ArgumentOutOfRangeException("dataRate");
            }

            Execute(Commands.W_REGISTER, Registers.RF_SETUP, new[] { regValue });

            // Enable dynamic payload length
            Execute(Commands.W_REGISTER, Registers.FEATURE,
                    new[]
            {
                (byte)(1 << Bits.EN_DPL)
            });

            // Set auto-ack
            Execute(Commands.W_REGISTER, Registers.EN_AA,
                    new[]
            {
                (byte)(1 << Bits.ENAA_P0 |
                       1 << Bits.ENAA_P1)
            });



            // Set dynamic payload length for pipes
            Execute(Commands.W_REGISTER, Registers.DYNPD,
                    new[]
            {
                (byte)(1 << Bits.DPL_P0 |
                       1 << Bits.DPL_P1)
            });

            // Flush RX FIFO
            Execute(Commands.FLUSH_RX, 0x00, new byte[0]);

            // Flush TX FIFO
            Execute(Commands.FLUSH_TX, 0x00, new byte[0]);

            // Clear IRQ Masks
            Execute(Commands.W_REGISTER, Registers.STATUS,
                    new[]
            {
                (byte)(1 << Bits.MASK_RX_DR |
                       1 << Bits.MASK_TX_DS |
                       1 << Bits.MAX_RT)
            });

            // Set default address
            Execute(Commands.W_REGISTER, Registers.SETUP_AW,
                    new[]
            {
                AddressWidth.Get(address)
            });

            // Set module address
            _slot0Address = address;
            Execute(Commands.W_REGISTER, (byte)AddressSlot.Zero, address);

            // Set retransmission values
            Execute(Commands.W_REGISTER, Registers.SETUP_RETR,
                    new[]
            {
                (byte)(0x0F << Bits.ARD |
                       0x0F << Bits.ARC)
            });

            // Setup, CRC enabled, Power Up, PRX
            SetReceiveMode();
        }