Re: Atmega8 i eksterni eeprom 24c08
03.11.2011. u 20:19 · pre 182 meseca
#6
Uspio sam napokon :) malo truda i trazenja
nasao u libc manualu oko 100stranice, bas za eeprome 24cxx
Evo funkcija za citanje i fala milanmeh :)
ee24xx_read_bytes(uint16_t eeaddr, int len, uint8_t *buf)
{
uint8_t sla, twcr, n = 0;
int rv = 0;
#ifndef WORD_ADDRESS_16BIT
/* patch high bits of EEPROM address into SLA */
sla = TWI_SLA_24CXX | (((eeaddr >> 8) & 0x07) << 1);
#else
/* 16-bit address devices need only TWI Device Address */
sla = TWI_SLA_24CXX;
#endif
/*
* Note [8]
* First cycle: master transmitter mode
*/
restart:
if (n++ >= MAX_ITER)
return -1;
begin:
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_REP_START: /* OK, but should not happen */
case TW_START:
break;
case TW_MT_ARB_LOST: /* Note [9] */
goto begin;
default:
return -1; /* error: not in start condition */
/* NB: do /not/ send stop condition */
}
/* Note [10] */
/* send SLA+W */
TWDR = sla | TW_WRITE;
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_MT_SLA_ACK:
break;
case TW_MT_SLA_NACK: /* nack during select: device busy writing */
/* Note [11] */
goto restart;
case TW_MT_ARB_LOST: /* re-arbitrate */
goto begin;
default:
goto error; /* must send stop condition */
}
#ifdef WORD_ADDRESS_16BIT
TWDR = (eeaddr >> 8); /* 16-bit word address device, send high 8 bits of addr */
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_MT_DATA_ACK:
break;
case TW_MT_DATA_NACK:
goto quit;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error; /* must send stop condition */
}
#endif
TWDR = eeaddr; /* low 8 bits of addr */
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_MT_DATA_ACK:
break;
case TW_MT_DATA_NACK:
goto quit;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error; /* must send stop condition */
}
/*
* Note [12]
* Next cycle(s): master receiver mode
*/
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send (rep.) start condition */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_START: /* OK, but should not happen */
case TW_REP_START:
break;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error;
}
/* send SLA+R */
TWDR = sla | TW_READ;
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_MR_SLA_ACK:
break;
case TW_MR_SLA_NACK:
goto quit;
case TW_MR_ARB_LOST:
goto begin;
default:
goto error;
}
for (twcr = _BV(TWINT) | _BV(TWEN) | _BV(TWEA) /* Note [13] */;
len > 0;
len--)
{
if (len == 1)
twcr = _BV(TWINT) | _BV(TWEN); /* send NAK this time */
TWCR = twcr; /* clear int to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS))
{
case TW_MR_DATA_NACK:
len = 0; /* force end of loop */
/* FALLTHROUGH */
case TW_MR_DATA_ACK:
*buf++ = TWDR;
rv++;
break;
default:
goto error;
}
}
quit:
/* Note [14] */
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
return rv;
error:
rv = -1;
goto quit;
}