0

What should I change in a class from topic RFID RC522 Raspberry PI 2 Windows IOT if I want to connect second rfid reader via SPI?

I connected the second "ss line" to SPI_CE1_N pin and "reset line" to GPIO12 pin.

I added a constructor in class Mfrc522 and I used SPI_CONTROLLER_NAME, SPI_CHIP_SELECT_LINE, RESET_PIN as a parameters but without success.

Jarek
  • 307
  • 4
  • 12
  • Line 0 maps to physical pin number 24(CS0) on the Rpi2 and line1 maps to physical pin number 26(CS1). Because chip select line bind to SPI device when initializing. You can `Dispose` initialized SPI device and change its chip select line and reinitialize it. – Rita Han Sep 20 '16 at 03:00
  • It doesn't work. During the next initialization program stops. – Jarek Sep 29 '16 at 20:29

2 Answers2

0

The first time initialization

public async Task InitIO()
{
    ...
    ...

    try
    {
        settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
        settings.ClockFrequency = 1000000;
        settings.Mode = SpiMode.Mode0;

        String spiDeviceSelector = SpiDevice.GetDeviceSelector();
        IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);

        _spi = await SpiDevice.FromIdAsync(devices[0].Id, settings);

    }
    catch (Exception ex)
    {
        throw new Exception("SPI Initialization Failed", ex);
    }

    ...
    ...
}

The second time initialization:

public async Task ReInitSpi()
{
    _spi.Dispose(); //Here is important.

    try
    {
        if (settings.ChipSelectLine == 0)
        {
            settings = new SpiConnectionSettings(1); //CS1
        }
        else
        {
            settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE); //CS0
        }

        settings.ClockFrequency = 1000000;
        settings.Mode = SpiMode.Mode0;

        String spiDeviceSelector = SpiDevice.GetDeviceSelector();
        IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);

        _spi = await SpiDevice.FromIdAsync(devices[0].Id, settings);

    }
    /* If initialization fails, display the exception and stop running */
    catch (Exception ex)
    {
        throw new Exception("SPI Initialization Failed", ex);
    }

}

Every time you use it, for example, do it like this:

    await mfrc.ReInitSpi();

    var writeBuffer = new byte[] { 0x55, 0xaa };

    mfrc._spi.Write(writeBuffer);

This works for me. I hope it is helpful for you.

Rita Han
  • 9,574
  • 1
  • 11
  • 24
0

Finally I solved the problem.

Thank You Rita Han for your help. Your code now work great. Now because I had hardware problem.

I connected devices as a picture:

Connection RFID-RC522 to the Raspberry Pi.

After connecting the oscilloscope noticed that the voltage of MISO bus is not correct. Both devices pulling down the voltage to ground. Instead of 3,3V was barely 1,24V. (The minimum voltage for 3,3V CMOS logic level 1 is 2,4V)

One of the devices trying to transmit.

To quickly test I used two diodes (for separating outputs) and resistor (to speed up the falling edge).

Solution - separation output.

Result: 3,3V - Vf = 2,56V

Result - separation output.

Now both devices working great.

I think that ultimately it would be better to use SN74HC125N.

BTW: Why can not change the SS pin by property?

_spi.ConnectionSettings.ChipSelectLine = 1;
Jarek
  • 307
  • 4
  • 12