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

Peter Sodermanns peter.sodermanns at aixcon.de
Tue Feb 14 12:22:54 CET 2006


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



More information about the En-Nut-Discussion mailing list