cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Send/Receive routine termination?

jomedfree
Associate II
Posted on November 24, 2009 at 08:50

I2C Send/Receive routine termination?

3 REPLIES 3
jomedfree
Associate II
Posted on May 17, 2011 at 10:00

I am using the code below to access devices like temperature sensor.

If I do not fully reinitialize the I2C peripheral using the I2C0_Config() routine, then the device is not working properly after a first read call.

Is there some flag that must be cleared after the send or receive routine in order to avoid to reconfigure the I2C after each communication?

Thanks in advance for your help.

BTW: the code below works fine (up to now)!

void I2C0_Config(void)

{

/* variables locales */

I2C_InitTypeDef I2C_Struct; // pour initialisation des périphériques I2C

/* I2C0: activations horloges, reset du périphérique et connections externes */

I2C_Cmd (I2C0, ENABLE);

SCU_APBPeriphClockConfig(__I2C0,ENABLE);

I2C_DeInit(I2C0);

Connect_I2C0(); // connexion au reste du monde (dans Gest_Carte.c)

I2C_DeInit(I2C0);

I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;

I2C_Struct.I2C_Ack = I2C_Ack_Enable;

I2C_Struct.I2C_CLKSpeed =400000;

I2C_Struct.I2C_OwnAddress = 0x00; // car mode Master uniquement

I2C_Init(I2C0, &I2C_Struct);

I2C_ITConfig (I2C0, DISABLE);

/* Voilà, I2C0 est pret pour utilisation en mode polling */

}

u8 I2C0_EnvData(u8 Add7bits, u8 NbEnv, u8* BuffEnv)

{

u16 Secu;

/* Réinit du bus par défaut */

I2C0_Config();

/* Envoi START condition */

I2C_GenerateStart (I2C0, ENABLE);

/* Test de EV5 et RAZ */

Secu= 200;

while( (!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT)) && Secu--); // EV5

if (!Secu) return 1; /* indique un PB sur Start */

/* envoi addresse et mode écriture */

I2C_Send7bitAddress (I2C0, (Add7bits<

/* Test de EV6 et RAZ */

Secu= 2000;

while( (!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECTED)) && Secu-- ); // EV6

if (!Secu) return 2; /* indique un PB envoi Adresse */

/* RAZ EV6 en forçant encore PE bit */

I2C_Cmd (I2C0, ENABLE);

/* envoi des données */

while (NbEnv)

{

/* envoi des octets en mode MASTER */

I2C_SendData(I2C0, *BuffEnv);

BuffEnv++;

Secu= 2000;

/* Test de EV8 et RAZ */

while((!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) && Secu--); //EV8

if (!Secu) return 3; /* indique un PB envoi données */

NbEnv--;

}

/* Envoi STOP condition pour fin de communication */

I2C_GenerateSTOP (I2C0, ENABLE);

Secu= 200;

while (Secu--); // pour attendre fin du bit de stop

/* si on arrive ici: tout est OK! */

return 0;

}

u8 I2C0_RecData(u8 Add7bits, u8 NbRec, u8* BuffRec)

{

u16 Secu;

/* Réinit du bus par défaut */

I2C0_Config();

/* Envoi START condition */

I2C_GenerateStart (I2C0, ENABLE);

/* Test de EV5 et RAZ */

Secu= 200;

while( (!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT)) && Secu--); // EV5

if (!Secu) return 1; /* indique un PB sur Start */

/* envoi addresse et mode lecture */

I2C_Send7bitAddress (I2C0, (Add7bits<

/* Test de EV6 et RAZ */

Secu= 2000;

while( (!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECTED)) && Secu-- ); // EV6

if (!Secu) return 2; /* indique un PB envoi Adresse */

/* RAZ EV6 en forçant encore PE bit */

I2C_Cmd (I2C0, ENABLE);

/* réception des données */

while (NbRec)

{

/* attente réception de l'octet en cours */

Secu= 2000;

/* Test de EV3-1 et RAZ */

while((!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) && Secu--); //EV3-1

if (!Secu) return 3; /* indique un PB envoi données */

/* réception en mode MASTER */

if (NbRec == 2) I2C_AcknowledgeConfig (I2C0, DISABLE); // pour NACK dernier octet

*BuffRec= I2C_ReceiveData (I2C0);

BuffRec++;

NbRec--;

}

/* Envoi STOP condition pour fin de communication */

I2C_GenerateSTOP (I2C0, ENABLE);

Secu= 200;

while (Secu--); // pour attendre fin du bit de stop

/* si on arrive ici: tout est OK! */

return 0;

}

alexanderpapsdorf9
Associate II
Posted on May 17, 2011 at 10:00

Hello,

No it is not necessary to reconfigure the I2C interface.

Make sure that you enable the acknowledge bit before each new communication with :

I2C_AcknowledgeConfig(I2C0, ENABLE);

In your read function (I2C0_RecData) the Acknowldge bit has been disabled

on end.

Hope it helps

Alex

jomedfree
Associate II
Posted on May 17, 2011 at 10:00

Yes, the Acknowledge enable were at the begining of the receive/transmitt routines before I replaced it with the full I2C_Config() routine (that also include this acknowledge enable function).

If I do not add the full I2C_Config() routine, it is just like I forget to clear some flags but I can't identify them.