Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- ...
- 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;
- }
- }
Add Comment
Please, Sign In to add comment