Atmel website | ARM Community | AVR freaks | Technical Support
Banner
 FAQ •  Search •  Register •  Login 

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: problem with twi (AT91SAM7X256)
PostPosted: Tue Aug 24, 2010 7:11 pm 
Offline

Joined: Tue Aug 24, 2010 4:12 pm
Posts: 4
hello

i want to implement an image sensor that utilizes an I2C protocal to read/write the registers.

but i wasn't successful !

when i write registers, i see no change in the sensor status. also can't read register values!

i think the sensor doesn't answer the I2C!

i couldn't find any I2C sample code that checks ACK and NACK response.

can anyone help me to write a code to realize ACK from sensor?

this is my code:
Code:
// PIOA-pin setup:
   AT91F_TWI_CfgPIO();
   // clk enable:
   AT91F_TWI_CfgPMC();
   // TWI setup:
   AT91F_TWI_Configure(AT91C_BASE_TWI);   // Reset peripheral(SWRST) // Set Master mode(MSEN)
   // set TWI clk:
   *AT91C_TWI_CWGR = (1<<16)|(45<<8)|(45);

   *AT91C_TWI_MMR = 0x210100;
   *AT91C_TWI_IADR = 0x11;   //internal address
   
   *AT91C_TWI_CR = AT91C_TWI_START | AT91C_TWI_STOP | AT91C_TWI_MSEN;
   *AT91C_TWI_THR = 0xc0;
   
   status = *AT91C_TWI_SR;
   while(status & AT91C_TWI_NACK)
      status = *AT91C_TWI_SR;

   status = *AT91C_TWI_SR;
   while(!(status & AT91C_TWI_TXCOMP))
      status = *AT91C_TWI_SR;


i expect to have an infinite loop if the sensor doesn't ACK.
Code:
   status = *AT91C_TWI_SR;
   while(status & AT91C_TWI_NACK)
      status = *AT91C_TWI_SR;

but doesn't! even if i change the slave address !!

please help me
thanks


Top
 Profile  
 
 Post subject: Re: problem with twi (AT91SAM7X256)
PostPosted: Wed Aug 25, 2010 3:10 pm 
Offline

Joined: Thu Feb 25, 2010 5:02 pm
Posts: 88
Hello -

First thing I would check is, hook a scope to the TWD and TWCK lines (PA10, PA11) and verify they are changing when you send/recieve data...

If they are not changing (especially the clock) then the problem is in the configuration of the TWI.

Regards,

_________________
Duane P. Fridley, IEEE CSDP
Viable Bytes, Inc.


Top
 Profile  
 
 Post subject: Re: problem with twi (AT91SAM7X256)
PostPosted: Wed Aug 25, 2010 8:22 pm 
Offline

Joined: Tue Aug 24, 2010 4:12 pm
Posts: 4
thank you dfridley
but i haven't a scope now!

doubt to my code !
can you tell me is my code correct or not?
i just want a code to clarify if the sensor acknowledges after sending address?


Top
 Profile  
 
 Post subject: Re: problem with twi (AT91SAM7X256)
PostPosted: Wed Aug 25, 2010 9:39 pm 
Offline

Joined: Thu Feb 25, 2010 5:02 pm
Posts: 88
We will need more info...

Can you post the code for these two functions?
Quote:
Code:
// PIOA-pin setup:
   AT91F_TWI_CfgPIO();
   // clk enable:
   AT91F_TWI_CfgPMC();


Also, did this code come from the TWI example at the Atmel site?

Regards,

_________________
Duane P. Fridley, IEEE CSDP
Viable Bytes, Inc.


Top
 Profile  
 
 Post subject: Re: problem with twi (AT91SAM7X256)
PostPosted: Fri Aug 27, 2010 9:04 am 
Offline

Joined: Tue Aug 24, 2010 4:12 pm
Posts: 4
these functions was declared in the lib_at91sam7x56.h from atmel (attached)


Attachments:
lib_AT91SAM7X256.rar [15.08 KiB]
Downloaded 38 times
Top
 Profile  
 
 Post subject: Re: problem with twi (AT91SAM7X256)
PostPosted: Fri Aug 27, 2010 5:28 pm 
Offline

Joined: Thu Feb 25, 2010 5:02 pm
Posts: 88
Quote:
these functions was declared in the lib_at91sam7x56.h from atmel (attached)

Ok, that looks fine.

What about the hardware... Are there external pullups on the TWD and the TWCK lines?

Edit
Finally got a chance to take a closer look at your code.
You probably want to wait until the transmitter is ready (i.e. the data has been sent) before checking for the NACK.

Eg.

Code:
   // --snip--

   *AT91C_TWI_CR = AT91C_TWI_START | AT91C_TWI_STOP | AT91C_TWI_MSEN;
   *AT91C_TWI_THR = 0xc0;
   
   // Wait until the data is sent before checking for NACK or TXCOMP.
   // See flowcharts section 25.9.6 in AT91SAM7X datasheet .
   do
   {
        status = *AT91C_TWI_SR;
   }
   while( !(status & AT91C_TWI_TXRDY) );

   status = *AT91C_TWI_SR;
   while(status & AT91C_TWI_NACK)
      status = *AT91C_TWI_SR;

   // --snip--


Regards,

_________________
Duane P. Fridley, IEEE CSDP
Viable Bytes, Inc.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: