Exemplo n.º 1
0
uint8_t eeprom_read_byte(const uint16_t address) {

  /* Start data transmission on the bus. */
  eeprom_start();

  /* Send a dummy write to the bus with the requested read address. */
  eeprom_i2c_write(EEPROM_I2C_WRITE_ADDRESS);
  eeprom_i2c_write(address >> 8);
  eeprom_i2c_write(address);

  /* Start data transmission on the bus. */
  eeprom_start();

  /* Send to the bus a read request. */
  eeprom_i2c_write(EEPROM_I2C_READ_ADDRESS);

  /* Read the byte from the EEPROM. */
  uint8_t byte = eeprom_i2c_read();

  /* Return a NACK for acknowledgement. */
  eeprom_i2c_send_ack(EEPROM_I2C_NACK);

  /* Stop data transmission on the bus. */
  eeprom_stop();

  return byte;
}
Exemplo n.º 2
0
Arquivo: main.c Projeto: wuwx/simba
static int test_read_write_bad_address(struct harness_t *harness_p)
{
    uint8_t buf[2];

    memset(&buf[0], 0, sizeof(buf));

    /* Start address outside the EEPROM. */
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,
                             &buf[0],
                             EEPROM_SIZE,
                             sizeof(buf)) == -EINVAL);

    BTASSERT(eeprom_i2c_write(&eeprom_i2c,
                              EEPROM_SIZE,
                              &buf[0],
                              sizeof(buf)) == -EINVAL);

    /* End address outside the EEPROM. */
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,
                             &buf[0],
                             EEPROM_SIZE - 1,
                             sizeof(buf)) == -EINVAL);

    BTASSERT(eeprom_i2c_write(&eeprom_i2c,
                              EEPROM_SIZE - 1,
                              &buf[0],
                              sizeof(buf)) == -EINVAL);

    return (0);
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: wuwx/simba
static int test_read_write_low_high(struct harness_t *harness_p)
{
    uint8_t byte;

    /* Lowest address. */
    byte = 1;
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,
                              0,
                              &byte,
                              sizeof(byte)) == sizeof(byte));

    byte = 0;
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,
                             &byte,
                             0,
                             sizeof(byte)) == sizeof(byte));
    BTASSERT(byte == 1);

    /* Highest address. */
    byte = 2;
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,
                              EEPROM_SIZE - 1,
                              &byte,
                              sizeof(byte)) == sizeof(byte));

    byte = 0;
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,
                             &byte,
                             EEPROM_SIZE - 1,
                             sizeof(byte)) == sizeof(byte));
    BTASSERT(byte == 2);

    return (0);
}
Exemplo n.º 4
0
bool eeprom_write_byte(const uint8_t value, const uint16_t address) {

  /* Start data transmission on the bus. */
  eeprom_start();

  /* Send a write request containing the target address to the bus. */
  eeprom_i2c_write(EEPROM_I2C_WRITE_ADDRESS);
  eeprom_i2c_write(address >> 8);
  eeprom_i2c_write(address);

  /* Send the byte to write to the bus. */
  eeprom_i2c_write(value);

  /* Read the acknowledgement result from the operation. */
  bool result = eeprom_i2c_get_ack();

  /* Stop data transmission on the bus. */
  eeprom_stop();

  return result;
}
Exemplo n.º 5
0
Arquivo: main.c Projeto: wuwx/simba
static int test_read_write_sizes(struct harness_t *harness_p)
{
    int i;
    uint8_t write_buf[153];
    uint8_t read_buf[153];

    /* Write zeros to the EEPROM. */
    memset(&write_buf[0], 0, sizeof(write_buf));
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,
                              0,
                              &write_buf[0],
                              sizeof(write_buf)) == sizeof(write_buf));

    /* Read and verify the written data. */
    memset(&read_buf[0], -1, sizeof(read_buf));
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,
                             &read_buf[0],
                             0,
                             sizeof(read_buf)) ==  sizeof(read_buf));
    BTASSERTM(&read_buf[0], &write_buf[0], sizeof(write_buf));

    /* Write various sizes at various alignments. */
    for (i = 0; i < membersof(write_buf); i++) {
        write_buf[i] = (membersof(write_buf) - i);
    }

    BTASSERT(eeprom_i2c_write(&eeprom_i2c,   0,  &write_buf[0],   1) ==  1);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,   1,  &write_buf[1],   2) ==  2);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,   3,  &write_buf[3],   3) ==  3);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,   6,  &write_buf[6],   4) ==  4);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  10, &write_buf[10],   5) ==  5);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  15, &write_buf[15],   6) ==  6);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  21, &write_buf[21],   7) ==  7);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  28, &write_buf[28],   8) ==  8);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  36, &write_buf[36],   9) ==  9);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  45, &write_buf[45],  10) == 10);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  55, &write_buf[55],  11) == 11);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  66, &write_buf[66],  12) == 12);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  78, &write_buf[78],  13) == 13);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c,  91, &write_buf[91],  14) == 14);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c, 105, &write_buf[105], 15) == 15);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c, 120, &write_buf[120], 16) == 16);
    BTASSERT(eeprom_i2c_write(&eeprom_i2c, 136, &write_buf[136], 17) == 17);

    /* Read and verify the written data. */
    memset(&read_buf[0], -1, sizeof(read_buf));

    BTASSERT(eeprom_i2c_read(&eeprom_i2c,  &read_buf[0],    0,  1) ==  1);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,  &read_buf[1],    1,  2) ==  2);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,  &read_buf[3],    3,  3) ==  3);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c,  &read_buf[6],    6,  4) ==  4);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[10],   10,  5) ==  5);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[15],   15,  6) ==  6);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[21],   21,  7) ==  7);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[28],   28,  8) ==  8);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[36],   36,  9) ==  9);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[45],   45, 10) == 10);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[55],   55, 11) == 11);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[66],   66, 12) == 12);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[78],   78, 13) == 13);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[91],   91, 14) == 14);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[105], 105, 15) == 15);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[120], 120, 16) == 16);
    BTASSERT(eeprom_i2c_read(&eeprom_i2c, &read_buf[136], 136, 17) == 17);

    /* Verify that read and written data match. */
    BTASSERTM(&read_buf[0], &write_buf[0], sizeof(write_buf));

    return (0);
}