[En-Nut-Discussion] TWI initialization - new attempt

Piotr Reiter p.reiter at elmech.pl
Tue Feb 14 14:50:32 CET 2006


Hi

I think I can share this piece of code hoping that it clears some things
out.
It is not complete and will not compile but it works in my app.
Please let me know if it helps you.

I recommend VMLAB as testing environment as you do not need to upload
your code
to see if it works. If you decide to use it remember to place the
pullups on i2c bus.

Regards,
Peter

PS
the attached i2c.c is used this way with pcf8583 RTC:

char read_clock( unsigned char *buf, unsigned char offset, char count )
{
char res;
while( !twi_idle() ) IDLE();
twi_enqueue(offset);
twi_nreceive = count;
twi_start(0xA0);
while( !twi_stopped ) IDLE();
res = 0;
while( twi_received() )
{
if( count )
{
*buf++ = twi_get();
count--;
res++;
}
}
return res;
}

Peter Sodermanns napisał(a):
> Hi group,
>
> now my development environment is up to date (Nut/OS 4.0.2,
> WinAVR-20060125) but I'm still stuck with the TWI initialization problem.
>
> By now I've become modest and would be glad to see a single rising
> edge at SCL or SDA pin.
>
>
> My scenario:
> ------------------------------------------------------------
> Ethernut 2.1B2, 1k5 pull ups and oscilloscope probes connected to port
> D0 and D1. Terminal program at COM1.
>
>
> My test program:
> ------------------------------------------------------------
> #include <stdio.h>
> #include <io.h>
> #include <sys/version.h>
> #include <sys/event.h>
> #include <sys/timer.h>
> #include <dev/debug.h>
>
> int main(void)
> {
> u_long baud0 = 19200;
>
> NutRegisterDevice(&devDebug0, 0, 0);
> freopen("uart0", "w", stdout);
> _ioctl(_fileno(stdout), UART_SETSPEED, &baud0);
> NutSleep(200);
> printf_P(PSTR("\n\n\n"));
> printf_P(PSTR("Nut/OS: %s\n"), NutVersionString());
>
> printf_P(PSTR("\n - initial register settings\n"));
> printf_P(PSTR(" TWAR = %02X\n"), TWAR);
> printf_P(PSTR(" TWBR = %02X\n"), TWBR);
> printf_P(PSTR(" TWCR = %02X\n"), TWCR);
> printf_P(PSTR(" TWDR = %02X\n"), TWDR);
> printf_P(PSTR(" TWSR = %02X\n"), TWSR);
>
> TWAR = 0x6F; // slave address
> TWBR = 0xFF; // ~ 28KHz
> TWDR = 0x55; // some data
> TWSR = 0x00; // clear status and prescaler
> // TWCR = 0xE5; // TWINT=1, TWEA=1, TWSTA=1, TWSTO=0, TWWC=0, TWEN=1,
> 0, TWIE=1
> TWCR = 0xA4; // TWINT=1, TWEA=0, TWSTA=1, TWSTO=0, TWWC=0, TWEN=1, 0,
> TWIE=0
> // TWCR = 0x24; // TWINT=0, TWEA=0, TWSTA=1, TWSTO=0, TWWC=0, TWEN=1,
> 0, TWIE=0
>
> printf_P(PSTR("\n - new register settings\n"));
> printf_P(PSTR(" TWAR = %02X\n"), TWAR);
> printf_P(PSTR(" TWBR = %02X\n"), TWBR);
> printf_P(PSTR(" TWCR = %02X\n"), TWCR);
> printf_P(PSTR(" TWDR = %02X\n"), TWDR);
> printf_P(PSTR(" TWSR = %02X\n"), TWSR);
>
> for (;;); // stop here
> }
>
>
> the resulting debug output:
> ------------------------------------------------------------
> Nut/OS: 4.0.2.1
>
> - initial register settings
> TWAR = FE
> TWBR = 00
> TWCR = 00
> TWDR = FF
> TWSR = F8
>
> - new register settings
> TWAR = 6F
> TWBR = FF
> TWCR = 2C
> TWDR = FF
> TWSR = F8
>
>
> ATmega128 documentation says on p 214:
> ------------------------------------------------------------
> TWEN must be set to enable the Two-wire Serial Interface, TWSTA must
> be written to one to transmit a START condition and TWINT must be
> written to one to clear the TWINT flag. The TWI will then test the
> Two-wire Serial Bus and generate a START condition as soon as the bus
> becomes free. After a START condition has been transmitted, the TWINT
> flag is set by hardware, and the status code in TWSR will be $08.
>
> So TWCR = 0xA4 has to be correct.
>
>
> but in real life:
> ------------------------------------------------------------
> no START condition is transmitted,
> TWINT flag is not set,
> status code is $F8 (= no state information available)
>
> I tried that with several boards.
>
>
> Question:
> ------------------------------------------------------------
> Where is the initialization bit that has to be set, too?
>
>
>
> Thankful for any hint
>
> Peter
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
>   

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: i2c.c
URL: <http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20060214/fd593eb9/attachment.c>


More information about the En-Nut-Discussion mailing list