Skip to content

Commit

Permalink
Implement z_condvar_wait_until for rpi_pico port
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Dec 10, 2024
1 parent 6925cd5 commit 4890d42
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/system/rpi_pico/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,38 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) {

return _z_mutex_lock(m);
}

z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) {
if (!cv || !m) {
return _Z_ERR_GENERIC;
}

TickType_t now = xTaskGetTickCount();
TickType_t target_time = pdMS_TO_TICKS(abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000);
TickType_t block_duration = (target_time > now) ? (target_time - now) : 0;

xSemaphoreTake(cv->mutex, portMAX_DELAY);
cv->waiters++;
xSemaphoreGive(cv->mutex);

_z_mutex_unlock(m);

bool timed_out = xSemaphoreTake(cv->sem, block_duration) == pdFALSE;

_z_mutex_lock(m);

if (timed_out) {
xSemaphoreTake(cv->mutex, portMAX_DELAY);
cv->waiters--;
xSemaphoreGive(cv->mutex);
}

if (timeout != NULL) {
*timeout = timed_out;
}

return _Z_RES_OK;
}
#endif // Z_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down Expand Up @@ -263,6 +295,28 @@ unsigned long z_clock_elapsed_s(z_clock_t *instant) {
return elapsed;
}

void z_clock_advance_us(z_clock_t *clock, unsigned long duration) {
clock->tv_sec += duration / 1000000;
clock->tv_nsec += (duration % 1000000) * 1000;

if (clock->tv_nsec >= 1000000000) {
clock->tv_sec += 1;
clock->tv_nsec -= 1000000000;
}
}

void z_clock_advance_ms(z_clock_t *clock, unsigned long duration) {
clock->tv_sec += duration / 1000;
clock->tv_nsec += (duration % 1000) * 1000000;

if (clock->tv_nsec >= 1000000000) {
clock->tv_sec += 1;
clock->tv_nsec -= 1000000000;
}
}

void z_clock_advance_s(z_clock_t *clock, unsigned long duration) { clock->tv_sec += duration; }

/*------------------ Time ------------------*/
z_time_t z_time_now(void) {
z_time_t now;
Expand Down

0 comments on commit 4890d42

Please sign in to comment.