Example #1
0
int
spiflash_wait_ready(struct spiflash_dev *dev, uint32_t timeout_ms)
{
    uint32_t ticks;
    os_time_t exp_time;
    os_time_ms_to_ticks(timeout_ms, &ticks);
    exp_time = os_time_get() + ticks;

    while (!spiflash_device_ready(dev)) {
        if (os_time_get() > exp_time) {
            return -1;
        }
    }
    return 0;
}
u32_t
sys_arch_sem_wait(sys_sem_t *sem, u32_t timo)
{
    u32_t now;

    if (timo == 0) {
        timo = OS_WAIT_FOREVER;
    } else {
        if (os_time_ms_to_ticks(timo, &timo)) {
            timo = OS_WAIT_FOREVER - 1;
        }
    }
    now = os_time_get();
    if (os_sem_pend(sem, timo) == OS_TIMEOUT) {
        return SYS_ARCH_TIMEOUT;
    }
    return (now - os_time_get()) * 1000 / OS_TICKS_PER_SEC;
}
u32_t
sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, uint32_t timo)
{
    u32_t now;
    void *val;

    if (timo == 0) {
        timo = OS_WAIT_FOREVER;
    } else {
        if (os_time_ms_to_ticks(timo, &timo)) {
            timo = OS_WAIT_FOREVER - 1;
        }
    }
    now = os_time_get();
    if (os_queue_get(mbox, &val, timo)) {
        return SYS_ARCH_TIMEOUT;
    }
    if (msg != NULL) {
        *msg = val;
    }
    return (now - os_time_get()) * 1000 / OS_TICKS_PER_SEC;
}
/**
 * Lock access to the charge_control_itf specified by cci. Blocks until lock acquired.
 *
 * @param The charge_ctrl_itf to lock
 * @param The timeout
 *
 * @return 0 on success, non-zero on failure.
 */
static int
ad5061_itf_lock(struct charge_control_itf *cci, uint32_t timeout)
{
    int rc;
    os_time_t ticks;

    if (!cci->cci_lock) {
        return 0;
    }

    rc = os_time_ms_to_ticks(timeout, &ticks);
    if (rc) {
        return rc;
    }

    rc = os_mutex_pend(cci->cci_lock, ticks);
    if (rc == 0 || rc == OS_NOT_STARTED) {
        return (0);
    }

    return (rc);
}
Example #5
0
int
lis2dw12_stream_read(struct sensor *sensor,
                   sensor_type_t sensor_type,
                   sensor_data_func_t read_func,
                   void *read_arg,
                   uint32_t time_ms)
{
    struct lis2dw12 *lis2dw12;
    struct sensor_itf *itf;
    int rc;
    os_time_t time_ticks;
    os_time_t stop_ticks = 0;
    struct lis2dw12_private_driver_data *pdd;
    uint8_t fifo_samples;

    /* If the read isn't looking for accel data, don't do anything. */
    if (!(sensor_type & SENSOR_TYPE_ACCELEROMETER)) {
        return SYS_EINVAL;
    }

    lis2dw12 = (struct lis2dw12 *)SENSOR_GET_DEVICE(sensor);
    itf = SENSOR_GET_ITF(sensor);
    pdd = &lis2dw12->pdd;

    undo_interrupt(&lis2dw12->intr);

    if (pdd->interrupt) {
        return SYS_EBUSY;
    }

    /* enable interrupt */
    pdd->interrupt = &lis2dw12->intr;

    rc = enable_interrupt(sensor, lis2dw12->cfg.stream_read_interrupt);
    if (rc) {
        goto done;
    }

    if (time_ms != 0) {
        rc = os_time_ms_to_ticks(time_ms, &time_ticks);
        if (rc) {
            goto done;
        }
        stop_ticks = os_time_get() + time_ticks;
    }

    for (;;) {
        wait_interrupt(&lis2dw12->intr, pdd->int_num);
        fifo_samples = 1;
        
        while(fifo_samples > 0) {
            rc = lis2dw12_do_read(sensor, read_func, read_arg);
            if (rc) {
                goto done;
            }

            rc = lis2dw12_get_fifo_samples(itf, &fifo_samples);
            if (rc) {
                goto done;
            }
        }
        
        if (time_ms != 0 && OS_TIME_TICK_GT(os_time_get(), stop_ticks)) {
                break;
        }
    }

done:
    /* disable interrupt */
    pdd->interrupt = NULL;
    rc = disable_interrupt(sensor, lis2dw12->cfg.stream_read_interrupt);

    return rc;
}