TsctlTWIWriteCExamples

From embeddedTS Manuals
int main(int argc, char* argv[]) {
  TWI* twi = TWIInit(0);
  int ret;
  TWIResult tret;
 
  if ((ret=twi->Lock(twi,0,1)) < 0) {
    printf("TWI lock error %d\n", ret);
    return 1;
  }
  // Write 4 byte to device DEV_ID at address REG_ADRS
 
  // Allocate a 4 element libtsctl array of char to hold data to send
  ArrayAutoOfSize(char,val,4);
  // Initialize the data to send
  val[0] = 0x12;
  val[1] = 0x34;
  val[2] = 0x56;
  val[3] = 0x78;

  if ((tret = twi->Write(twi,DEV_ID,1,REG_ADRS,val)) != TWISuccess) {
    printf("Error writing TWI: %d\n",tret);
    return 1;
  }
 
  twi->Unlock(twi,0,0);
}