Guest User

Untitled

a guest
Jul 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. private SpiDevice spiDevice;
  2. private SpiDevice spiDevice2;
  3.  
  4. private static final String SPI_PORT = "SPI0.0";
  5. private static final String SPI_PORT2 = "SPI0.1";
  6.  
  7. private Rc522 mRc522;
  8.  
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. PeripheralManager pioService = PeripheralManager.getInstance();
  12. try {
  13. spiDevice = pioService.openSpiDevice(SPI_PORT);
  14. spiDevice2 = pioService.openSpiDevice(SPI_PORT2);
  15.  
  16. mRc522 = new Rc522(spiDevice, spiDevice2, gpioReset);
  17. ...
  18.  
  19. private SpiDevice device;
  20. private SpiDevice device2;
  21.  
  22. private Gpio resetPin;
  23. private int busSpeed = 1000000;
  24. private int busSpeed2 = 13650000; //13.65khz
  25.  
  26. public Rc522(SpiDevice spiDevice, SpiDevice spiDevice2, Gpio resetPin) throws IOException {
  27. this.device = spiDevice;
  28. this.device2 = spiDevice2;
  29.  
  30. initializePeripherals();
  31. }
  32.  
  33. /**
  34. * Performs the initial configuration on hardware ports
  35. * @throws IOException if the hardware board had a problem with its hardware ports
  36. */
  37. private void initializePeripherals() throws IOException {
  38. device.setFrequency(busSpeed);
  39. device2.setFrequency(busSpeed2);
  40. resetPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
  41. initializeDevice();
  42. }
  43.  
  44. /**
  45. * Performs the initial device setup and configure the pins used
  46. */
  47. private void initializeDevice(){
  48. reset();
  49. writeRegister(REGISTER_TIMER_MODE, (byte) 0x8D);
  50. writeRegister(REGISTER_TIMER_PRESCALER_MODE, (byte) 0x3E);
  51. writeRegister(REGISTER_TIMER_RELOAD_LOW, (byte) 30);
  52. writeRegister(REGISTER_TIMER_RELOAD_HIGH, (byte) 0);
  53. writeRegister(REGISTER_TX_MODE, (byte) 0x40);
  54. writeRegister(REGISTER_MODE, (byte) 0x3D);
  55. setAntenna(true);
  56. }
  57.  
  58. /**
  59. * Writes to a RC522 register
  60. * @param address The address to write to
  61. * @param value The value that will be written
  62. */
  63. private void writeRegister(byte address, byte value){
  64. //Log.d(TAG, "writeRegister -->" + address);
  65. //Log.d(TAG, "writeValue -->" + value);
  66.  
  67. byte buffer[] = {(byte) (((address << 1) & 0x7E)), value};
  68. //Log.d(TAG, "writeBuffer -->" + buffer.toString());
  69. byte response[] = new byte[buffer.length];
  70. try {
  71. device.transfer(buffer, response, buffer.length);
  72. //device2.transfer(buffer, response, buffer.length);
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. /**
  79. * Reads the current value on the RC522's register
  80. * @param address The address to read from
  81. * @return the byte value currently stored in the register
  82. */
  83. public byte readRegister(byte address){
  84. byte buffer[] = {(byte) (((address << 1) & 0x7E) | 0x80), 0};
  85. byte response[] = new byte[buffer.length];
  86. try {
  87. device.transfer(buffer, response, buffer.length);
  88. //device2.transfer(buffer, response, buffer.length);
  89. return response[1];
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. return 0;
  93. }
  94. }
Add Comment
Please, Sign In to add comment