AVR libc: TWI demo code bug??

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the TWI demo example in the AVR-libc docs:

int
ee24xx_write_page(uint16_t eeaddr, int len, uint8_t *buf)
{
 if (eeaddr + len < (eeaddr | (PAGE_SIZE - 1)))
    endaddr = eeaddr + len;
  else
    endaddr = (eeaddr | (PAGE_SIZE - 1)) + 1;
  len = endaddr - eeaddr;
}

Shouldn't that if statement read:

if ( (eeaddr + len - 1) <= (eeaddr | (PAGE_SIZE - 1)))

Seems to be an error in the comparison.

Take for example: eeaddr = 0x00, len = 16 = 0x10 and PAGE_SIZE = 16 = 0x10

we should be able to write this since the data is contained in the first page, but...

(eeaddr + len) = 0x00 + 0x10 = 0x10
(eeaddr | (PAGE_SIZE) - 1) = 0x0f

and the comparison fails. Should it?

Cheers.