TsctlSPIReadWriteCExamples

From embeddedTS Manuals
// Do an SPI read transaction to an FPGA expecting a four byte
// sequence of command, address, 16-bit data
unsigned short SPIRegisterRead(SPI *spi,int adrs) {
  // First, allocate two 4 element libtsctl arrays of char
  ArrayAutoOfSize(unsigned char, spiData, 4);
  ArrayAutoOfSize(unsigned char, spiResp, 4);

  // Initialize the array for the transaction
  spiData[0] = 0X70;
  spiData[1] = adrs;
  spiData[2] = data >> 8;
  spiData[3] = data & 0xff;

  // Lock SPI, do the transaction, and unlock
  spi->Lock(spi, 0, 0);
  spi->ReadWrite(spi, 4, spiData, spiResp);
  spi->Unlock(spi, 0, 0);
  return ((unsigned short)spiResp[2] << 8) + (unsigned short)spiResp[3];
}