0

I use Raspberry pi 3 model B on Android Things.

for RC522 communication, I use https://github.com/Galarzaa90/android-things-rc522

this source success work, only 1 RC522 reader device

but I want 2 RC522 devices. enter image description here

if RC522 reader device only 1 connect.

GPIO set

SDA - GPIO 8 (SPI0 CS0) SCK - GPIO 11 (SPI0 SCLK) MOSI - GPIO 10 (SPI0 MOSI) MISO - GPIO 9 (SPI0 MISO) GND - GND RST - GPIO25 3.3V - 3.3V PWR

is work RC522 device.

and I would like to use 2 RC522 reader devices like the pictures.

this pictures set

SDA (blue) - SPI0 CS1(GPIO 7) , SPO0 CS0 (GPIO 8) 'SCK (green) - SPI0 SCLK MOSI (yellow) - SPI0 MOSI MISO (orange) - SPI0 MISO GND (red) - GND RST (Brown) - GPIO 25 3.3V (black) - 3.3V PWR

but Only the RC522 device to the left of the picture works.

first when I build with https://github.com/Galarzaa90/android-things-rc522 this source.

all RC522 devices not work.

with a little modification, I was able to operate only one RC522 device on the left.

MainActivity.class

private SpiDevice spiDevice;
private SpiDevice spiDevice2;

private static final String SPI_PORT = "SPI0.0";
private static final String SPI_PORT2 = "SPI0.1";

private Rc522 mRc522;

@Override
public void onCreate(Bundle savedInstanceState) {
  PeripheralManager pioService = PeripheralManager.getInstance();
  try {
      spiDevice = pioService.openSpiDevice(SPI_PORT);
      spiDevice2 = pioService.openSpiDevice(SPI_PORT2);

      mRc522 = new Rc522(spiDevice, spiDevice2, gpioReset);
 ...

Rc522.class

private SpiDevice device;
private SpiDevice device2;

private Gpio resetPin;
private int busSpeed = 1000000;
private int busSpeed2 = 13650000;  //13.65khz

public Rc522(SpiDevice spiDevice, SpiDevice spiDevice2, Gpio resetPin) throws IOException {
    this.device = spiDevice;
    this.device2 = spiDevice2;

    initializePeripherals();
}

 /**
 *  Performs the initial configuration on hardware ports
 * @throws IOException if the hardware board had a problem with its hardware ports
 */
 private void initializePeripherals() throws IOException {
    device.setFrequency(busSpeed);
    device2.setFrequency(busSpeed2);
    resetPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
    initializeDevice();
}

/**
 * Performs the initial device setup and configure the pins used
 */
private void initializeDevice(){
    reset();
    writeRegister(REGISTER_TIMER_MODE, (byte) 0x8D);
    writeRegister(REGISTER_TIMER_PRESCALER_MODE, (byte) 0x3E);
    writeRegister(REGISTER_TIMER_RELOAD_LOW, (byte) 30);
    writeRegister(REGISTER_TIMER_RELOAD_HIGH, (byte) 0);
    writeRegister(REGISTER_TX_MODE, (byte) 0x40);
    writeRegister(REGISTER_MODE, (byte) 0x3D);
    setAntenna(true);
}

 /**
 * Writes to a RC522 register
 * @param address The address to write to
 * @param value The value that will be written
 */
private void writeRegister(byte address, byte value){
    //Log.d(TAG, "writeRegister -->" + address);
    //Log.d(TAG, "writeValue -->" + value);

    byte buffer[] = {(byte) (((address << 1) & 0x7E)), value};
    //Log.d(TAG, "writeBuffer -->" + buffer.toString());
    byte response[] = new byte[buffer.length];
    try {
        device.transfer(buffer, response, buffer.length);
        //device2.transfer(buffer, response, buffer.length);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Reads the current value on the RC522's register
 * @param address The address to read from
 * @return the byte value currently stored in the register
 */
public byte readRegister(byte address){
    byte buffer[] = {(byte) (((address << 1) & 0x7E) | 0x80), 0};
    byte response[] = new byte[buffer.length];
    try {
        device.transfer(buffer, response, buffer.length);
        //device2.transfer(buffer, response, buffer.length);
        return response[1];
    } catch (IOException e) {
        e.printStackTrace();
        return 0;
    }
}

I feel like I might be able to modify writeRegister() and readRegister() just a little bit.

perhaps,you have opened multiple devices in android things, if you have ever used an RC522 reader device.

please advice for me

thanks..

17
  • It seems like it is using SPI Pheripheral to communicate. Why are you connecting the i2c Pheripheral?
    – MaNyYaCk
    Jul 17, 2018 at 10:57
  • It looks like you're wiring both peripherals together, is that right? I don't see why you are connecting them together since they connect to separate SPI ports. Jul 17, 2018 at 17:30
  • @MaNyYaCk I use RC522 devices use SPI Pheripheral to communicate. where is the i2c connecting? Jul 18, 2018 at 0:14
  • @NickFelker RC522 github.com/Galarzaa90/android-things-rc522 here site is used the SPI port Jul 18, 2018 at 0:25
  • @hyunwookcho Coming from microcontroller background so SDA and SCL are I2C to me. Sorry. Missed the information written in the back ground. You need to pull the chip select line low of the module that you want to send data to. And pull the line high of the module that you dont want to use.
    – MaNyYaCk
    Jul 18, 2018 at 4:24

1 Answer 1

0

The functions should be something like this.

private void writeRegister(byte address, byte value,byte sensor_Number){
    //Log.d(TAG, "writeRegister -->" + address);
    //Log.d(TAG, "writeValue -->" + value);

    //Logic to pull the line high for the sensor other than the sensor_Number
    //Logic to pull the line low for the sensor_Number.

    byte buffer[] = {(byte) (((address << 1) & 0x7E)), value};
    //Log.d(TAG, "writeBuffer -->" + buffer.toString());
    byte response[] = new byte[buffer.length];
    try {
        device.transfer(buffer, response, buffer.length);
        //device2.transfer(buffer, response, buffer.length);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public byte readRegister(byte address,byte sensor_Number){
    //Logic to pull the line high for the sensor other than the sensor_Number
    //Logic to pull the line low for the sensor_Number.

    byte buffer[] = {(byte) (((address << 1) & 0x7E) | 0x80), 0};
    byte response[] = new byte[buffer.length];
    try {
        device.transfer(buffer, response, buffer.length);
        //device2.transfer(buffer, response, buffer.length);
        return response[1];
    } catch (IOException e) {
        e.printStackTrace();
        return 0;
    }
}

Do write those commented logic yourself because I am useless when it comes to cpp.

edit : I don't know what the intialize device does but if it is configuring the RC522 Module, you need to do that for both the modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.