Exemplo n.º 1
0
        public Nrf(PinNumberEnum chipEnable, SpiChannelEnum channel, int spiSpeed = DefaultSpiSpeed)
        {
            _channel    = channel;
            _chipEnable = chipEnable;

            Functionalities.Pins.Setup(_chipEnable, PinModeEnum.Output);
            Functionalities.Pins.Write(_chipEnable, false);

            Functionalities.Spi.Setup(_channel, spiSpeed);

            Functionalities.Sleep.Milli(5);

            var general   = (ConfigurationRegister)ReadRegister(RegisterEnum.Configuration);
            var frequency = (FrequencyChannelRegister)ReadRegister(RegisterEnum.FrequencyChannel);

            State = general.PowerState;
            Mode  = general.TransceiverMode;

            Frequency = frequency.Frequency;

            for (byte pipeNumber = 0; pipeNumber < 6; ++pipeNumber)
            {
                WriteRegister(new PayloadSizeRegister(pipeNumber, PayloadSize));
            }
        }
Exemplo n.º 2
0
        public Module(PinNumberEnum chipEnable, SpiChannelEnum channel, [NotNull] Address address, [NotNull] Address broadcastAddress, byte radioChannel,
                      bool sendContinuously         = false, bool readRetransmissionsCount = false,
                      OutputPowerEnum outputPower   = OutputPowerEnum.P0dbm, DataRateEnum dataRate    = DataRateEnum.S1Mbps,
                      byte autoRetransmitRetryLimit = 10, AutoRetransmitDelayEnum autoRetransmitDelay = AutoRetransmitDelayEnum.T2000us,
                      int spiSpeed = Nrf.DefaultSpiSpeed)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            if (broadcastAddress == null)
            {
                throw new ArgumentNullException(nameof(broadcastAddress));
            }

            Packet.BaseAddress = address;

            Address          = address;
            BroadcastAddress = broadcastAddress;

            _sendContinuously         = sendContinuously;
            _readRetransmissionsCount = readRetransmissionsCount;

            _nrf = new Nrf(chipEnable, channel, spiSpeed);

            if (!_nrf.IsConnected)
            {
                throw new InvalidOperationException("NRF doesn't seem to be connected.");
            }

            _nrf.WriteRegister(new ConfigurationRegister(false, false, false, true, CrcLengthEnum.TwoBytes, PowerStateEnum.Down, TransceiverModeEnum.Receiver));
            _nrf.WriteRegister(new AutoAcknowledgmentRegister(true, true, false, false, false, false));
            _nrf.WriteRegister(new ReceiverAddressRegister(true, true, false, false, false, false));
            _nrf.WriteRegister(new AddressWidthRegister(AddressWidthEnum.FiveBytes));
            _nrf.WriteRegister(new AutoRetransmissionRegister(autoRetransmitDelay, autoRetransmitRetryLimit));
            _nrf.WriteRegister(new FrequencyChannelRegister(radioChannel));
            _nrf.WriteRegister(new RadioRegister(false, false, dataRate, outputPower));
            _nrf.WriteRegister(new FeatureRegister(false, false, true));
            _nrf.WriteRegister(new DynamicPayloadLengthRegister(false, false, false, false, false, false));

            _nrf.Flush();

            _nrf.SetPipeReceiveAddress(1, Address);

            _nrf.ModifyRegister <ConfigurationRegister>(RegisterEnum.Configuration, register => register.TransceiverMode = TransceiverModeEnum.Transmitter);

            _nrf.SetupDelay();

            _nrf.ChangePowerState(PowerStateEnum.Up);

            // Standby-I

            _worker = new BackgroundWorker
            {
                WorkerSupportsCancellation = true,
            };

            _worker.DoWork             += Work;
            _worker.RunWorkerCompleted += WorkCompleted;
        }
Exemplo n.º 3
0
            public static int Setup(SpiChannelEnum channel, int speed)
            {
                if (speed < 500000 || speed > 32000000)
                {
                    throw new ArgumentOutOfRangeException(nameof(speed));
                }

                var spiPin = channel.ToPinNumber();

                Pins.Setup(spiPin, PinModeEnum.Output);
                Pins.Write(spiPin, true);

                var fileDescriptor = Unmanaged.Spi.Setup((int)channel, speed);

                if (fileDescriptor >= 0)
                {
                    return(fileDescriptor);
                }

                switch (fileDescriptor)
                {
                case -1:
                    throw new Exception("Could not setup SPI. Function open() or ioctl() did not execute correctly.");

                default:
                    throw new Exception($"Could not setup SPI. Result: {fileDescriptor}.");
                }
            }
Exemplo n.º 4
0
        public Processor(PinNumberEnum chipEnable, SpiChannelEnum channel, [NotNull] Address address, [NotNull] Address broadcastAddress, byte radioChannel,
                         bool sendContinuously = false, bool readRetransmissionsCount = false)
        {
            _module = new Module(chipEnable, channel, address, broadcastAddress, radioChannel, sendContinuously, readRetransmissionsCount,
                                 spiSpeed: 12000000);

            _module.Received      += OnReceived;
            _module.SendingFailed += OnSendingFailed;
            _module.Sent          += OnSent;
            _module.Error         += OnError;
        }
Exemplo n.º 5
0
 public static PinNumberEnum ToPinNumber(this SpiChannelEnum channel) => channel == SpiChannelEnum.One ? PinNumberEnum.SPI_CE1 : PinNumberEnum.SPI_CE0;
Exemplo n.º 6
0
 public static int Exchange(SpiChannelEnum channel, byte[] data, int?length = null)
 => Unmanaged.Spi.Exchange((int)channel, data, length ?? data.Length);