SPI programming - WriteRead data to SPI


1. Define procedure on the emulator's side
  1. In order to read data from SPL, you should call "WriteRead()" method. This method send data to SPL and read data at the same time.

  2. "WriteRead" method of SPI will request to device (or emulator) to read data so you need to define correspond procedure on the emulator.

  3. First, add some logic on the emulator side.

  4. Select "Emulator" tab.

  5. Add below script on the editor box.

      
    BindSPIComponent  
    	/Id:spi1
    	/Procedure_OnWrite:proc1
    
    procedure proc1
    
    	print "OnReadRequested: " + value.Length + " bytes"
    	
    	data = Util.ToUShort(0xA1A2)
    	
    	spi1.SetReadBuffer(data)
    end
    
      
 

  1. Above procedure will return ushort data (0xA1A2) to application when it receives "Read" request from application.

  2. Click "Execute Script on the Emulator" button or Press "F5" key.



  3. Executed result:




2. WriteRead ushort data from SPI
  1. Use "WriteRead(ushort wdata, ushort[] rbuff)" method to read ushort data from SPI.

  2. "WriteRead" method will request to device (or emulator) to read data so you need to define correspond procedure on the emulator.

  3. Select "Application" tab.

  4. Add below script on the editor box.

      
    wdata = Util.ToUShort(0x00A0)
    rbuff = Util.CreateArrayUShort(1)
    
    spi1.WriteRead(wdata, rbuff)
    
    data = rbuff[0]
    
    print data
    print Util.UShortToHex(data)
    
      


  5. Util.UShortToHex(ushort) method was used to convert ushort data into string of hex format.

  6. Click "Send to MF Application" button or Press "F5" key.

  7. Executed result: