Ejemplo n.º 1
0
/**
 * @brief   EEPROM write routine.
 * @details Function writes data to EEPROM.
 * @pre     Data must be fit to single EEPROM page.
 *
 * @param[in] eepcfg  pointer to configuration structure of eeprom file
 * @param[in] offset  addres of 1-st byte to be write
 * @param[in] data    pointer to buffer with data to be written
 * @param[in] len     number of bytes to be written
 */
static msg_t eeprom_write(const I2CEepromFileConfig *eepcfg, uint32_t offset,
                          const uint8_t *data, size_t len) {
    msg_t status = RDY_RESET;
    systime_t tmo = calc_timeout(eepcfg->i2cp, (len + 2), 0);

    chDbgCheck(((len <= eepcfg->size) && ((offset + len) <= eepcfg->size)),
               "out of device bounds");
    chDbgCheck((((offset + eepcfg->barrier_low) / eepcfg->pagesize) ==
                (((offset + eepcfg->barrier_low) + len - 1) / eepcfg->pagesize)),
               "data can not be fitted in single page");

    /* write address bytes */
    eeprom_split_addr(eepcfg->write_buf, (offset + eepcfg->barrier_low));
    /* write data bytes */
    memcpy(&(eepcfg->write_buf[2]), data, len);

#if I2C_USE_MUTUAL_EXCLUSION
    i2cAcquireBus(eepcfg->i2cp);
#endif

    status = i2cMasterTransmitTimeout(eepcfg->i2cp, eepcfg->addr,
                                      eepcfg->write_buf, (len + 2), NULL, 0, tmo);

#if I2C_USE_MUTUAL_EXCLUSION
    i2cReleaseBus(eepcfg->i2cp);
#endif

    /* wait until EEPROM process data */
    chThdSleep(eepcfg->write_time);

    return status;
}
Ejemplo n.º 2
0
Archivo: 24aa.c Proyecto: mcu786/24aa
/**
 * @brief   EEPROM read routine.
 *
 * @param[in] addr      addres of 1-st byte to be read
 * @param[in] len       number of bytes to be write
 * @param[in] ext_rxbuf pointer to data buffer
 */
msg_t eeprom_read(uint32_t addr, uint8_t *buf, size_t len){
  msg_t status = RDY_OK;

  chBSemWait(&eeprom_sem);

  chDbgCheck(((len <= EEPROM_SIZE) && ((addr+len) <= EEPROM_SIZE)),
             "requested data out of device bounds");

  eeprom_split_addr(localtxbuf, addr);                /* write address bytes */
  i2c_transmit(eeprom_i2caddr, localtxbuf, 2, buf, len);

  chBSemSignal(&eeprom_sem);
  return status;
}
Ejemplo n.º 3
0
Archivo: 24aa.c Proyecto: mcu786/24aa
/**
 * @brief   EEPROM write routine.
 * @details Function writes data to EEPROM.
 * @pre     Data must be fit to single EEPROM page.
 *
 * @param[in] addr  addres of 1-st byte to be write
 * @param[in] buf   pointer to data
 * @param[in] len   number of bytes to be written
 */
msg_t eeprom_write(uint32_t addr, const uint8_t *buf, size_t len){
  msg_t status = RDY_OK;

  chBSemWait(&eeprom_sem);

  chDbgCheck(((len <= EEPROM_SIZE) && ((addr+len) <= EEPROM_SIZE)),
             "data can not be fitted in device");

  chDbgCheck(((addr / EEPROM_PAGE_SIZE) == ((addr + len - 1) / EEPROM_PAGE_SIZE)),
             "data can not be fitted in single page");

  eeprom_split_addr(localtxbuf, addr);              /* write address bytes */
  memcpy(&(localtxbuf[2]), buf, len);               /* write data bytes */
  i2c_transmit(eeprom_i2caddr, localtxbuf, (len + 2), NULL, 0);

  /* wait until EEPROM process data */
  chThdSleepMilliseconds(EEPROM_WRITE_TIME);
  chBSemSignal(&eeprom_sem);
  return status;
}
Ejemplo n.º 4
0
/**
 * @brief   EEPROM read routine.
 *
 * @param[in] eepcfg    pointer to configuration structure of eeprom file
 * @param[in] offset    addres of 1-st byte to be read
 * @param[in] data      pointer to buffer with data to be written
 * @param[in] len       number of bytes to be red
 */
static msg_t eeprom_read(const I2CEepromFileConfig *eepcfg,
                         uint32_t offset, uint8_t *data, size_t len) {

    msg_t status = RDY_RESET;
    systime_t tmo = calc_timeout(eepcfg->i2cp, 2, len);

    chDbgCheck(((len <= eepcfg->size) && ((offset + len) <= eepcfg->size)),
               "out of device bounds");

    eeprom_split_addr(eepcfg->write_buf, (offset + eepcfg->barrier_low));

#if I2C_USE_MUTUAL_EXCLUSION
    i2cAcquireBus(eepcfg->i2cp);
#endif

    status = i2cMasterTransmitTimeout(eepcfg->i2cp, eepcfg->addr,
                                      eepcfg->write_buf, 2, data, len, tmo);

#if I2C_USE_MUTUAL_EXCLUSION
    i2cReleaseBus(eepcfg->i2cp);
#endif

    return status;
}