[En-Nut-Discussion] TwMasterTransact is blocked

CAMBON, Olivier Olivier.CAMBON at astrium.eads.net
Mon Sep 10 13:50:13 CEST 2012


Dear all,

I am a new user of the board Ethernut 2.1 ver C with Nut/OS 4.10.1.
In my system, the ethernut board drives some slave electronic using TWI
/ I2C bus.

I found in the documentation the API TwMasterRegRead and
TwMasterRegWrite but it seems that they are not implemented for AVR.
So, a wrote my own "i2c_get" and "i2c_set" API.

The problem is that sometimes, in the function "i2c_set", the call to
"TwMasterTransact" is blocked.
I tried with or without timeout. The result is the same. The
TwMasterTransact does NOT return.

I copy the source code of "i2c_get" and i2c_set".

Best regards,

Olivier.

/********************** I2C INIT **********************/
uint8_t i2c_init(void)
{
  uint32_t val = 0;

  /* Set the pin as input with pull-up */
  cbi(DDRD,0);
  cbi(DDRD,1);
  sbi(PORTD,0);
  sbi(PORTD,1);

  /* Set as I2C master */
  TwInit(1);
  /* Set the bus speed */
  val = 50000;
  TwIOCtl(TWI_SETSPEED, &val);

  return 0;
}

/********************** I2C GET **********************/
uint8_t i2c_get(uint8_t sla, uint8_t addr, uint8_t nb, uint8_t* data)
{
  int nb_rec = 0;
  int ret = 0;

  /* Check the pointer on data area */
  if(data==NULL) { return 6; }

  /* Do the exchange and check the returned number of data */
  nb_rec = TwMasterTransact(sla, &addr, (uint16_t) 1, data, (uint16_t)
nb, (uint32_t) 500);
  /* Check the error occurred */
  ret = TwMasterError();
  /* If an error is detected */
  if((nb_rec != nb) || (ret != 0))
  {
    /* If no error on transmission, set the returned value to indicate
that the error occurred on the number of data */
    if(ret == 0) { ret = 8; }
  }

  /* Clear the data if not received */
  if(nb_rec < 0) { nb_rec = 0; }
  while(nb_rec < nb ) { data[nb_rec] = 0; nb_rec++; }

  return ret;
}

/********************** I2C SET **********************/
uint8_t i2c_set(uint8_t sla, uint8_t addr, uint8_t nb, uint8_t* data)
{
  uint16_t i = 0;
  uint8_t* buff=NULL;
  int nb_rec = 0;
  int ret = 0;

  /* Check the pointer on the data area */
  if(data == NULL) { return 9; }
  /* Allocate a new area in order to build the frame */
  buff = malloc(nb+1);
  /* Check the allocation */
  if(buff == NULL) { return 10; }
  /* Copy the data to the new area */
  if(nb > 0) { for(i=0; i<nb; i++) { buff[i+1] = data[i]; } }
  /* But the first data is the destination address */
  buff[0] = addr;

  /* Execute the transmission */
  nb_rec = TwMasterTransact(sla, buff, (uint16_t) (((uint16_t)nb)+1),
NULL, (uint16_t) 0, (uint32_t) 100);
  /* Check the error occurred */
  ret = TwMasterError();
  /* If an error is detected */
  if((nb_rec != 0) || (ret != 0))
  {
    /* If no error on transmission, set the returned value to indicate
that the error occurred on the number of data */
    if(ret == 0) { ret = 11; }
  }

  /* Before to exit, free the allocated area */
  free(buff);

  return ret;
}


Ce courriel (incluant ses eventuelles pieces jointes) peut contenir des informations confidentielles et/ou protegees ou dont la diffusion est restreinte. Si vous avez recu ce courriel par erreur, vous ne devez ni le copier, ni l'utiliser, ni en divulguer le contenu a quiconque. Merci d'en avertir immediatement l'expediteur et d'effacer ce courriel de votre systeme. Astrium decline toute responsabilite en cas de corruption par virus, d'alteration ou de falsification de ce courriel lors de sa transmission par voie electronique.
This email (including any attachments) may contain confidential and/or privileged information or information otherwise protected from disclosure. If you are not the intended recipient, please notify the sender immediately, do not copy this message or any attachments and do not use it for any purpose or disclose its content to any person, but delete this message and any attachments from your system. Astrium disclaims any and all liability if this email transmission was virus corrupted, altered or falsified. 
---------------------------------------------------------------------
Astrium SAS (393 341 516 RCS Nanterre) - Capital: 16.587.728 EUR - Siege social: 12 rue Pasteur, 92150 Suresnes, France



More information about the En-Nut-Discussion mailing list