Initial Release
This commit is contained in:
16
src/host/hardware_timer/CMakeLists.txt
Normal file
16
src/host/hardware_timer/CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
||||
pico_simple_hardware_target(timer)
|
||||
|
||||
target_compile_definitions(hardware_timer INTERFACE
|
||||
PICO_HARDWARE_TIMER_RESOLUTION_US=1000 # to loosen tests a little
|
||||
)
|
||||
|
||||
if (NOT DEFINED PICO_TIME_NO_ALARM_SUPPORT)
|
||||
# we don't have alarm pools in the basic host support, though pico_host_sdl adds it
|
||||
set(PICO_TIME_NO_ALARM_SUPPORT "1" CACHE INTERNAL "")
|
||||
endif()
|
||||
|
||||
if (PICO_TIME_NO_ALARM_SUPPORT)
|
||||
target_compile_definitions(hardware_timer INTERFACE
|
||||
PICO_TIME_DEFAULT_ALARM_POOL_DISABLED=1
|
||||
)
|
||||
endif()
|
42
src/host/hardware_timer/include/hardware/timer.h
Normal file
42
src/host/hardware_timer/include/hardware/timer.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _HARDWARE_TIMER_H
|
||||
#define _HARDWARE_TIMER_H
|
||||
|
||||
#include "pico.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef PARAM_ASSERTIONS_ENABLED_TIMER
|
||||
#define PARAM_ASSERTIONS_ENABLED_TIMER 0
|
||||
#endif
|
||||
|
||||
static inline void check_hardware_alarm_num_param(uint alarm_num) {
|
||||
invalid_params_if(TIMER, alarm_num >= NUM_TIMERS);
|
||||
}
|
||||
|
||||
uint32_t time_us_32();
|
||||
uint64_t time_us_64();
|
||||
void busy_wait_us_32(uint32_t delay_us);
|
||||
void busy_wait_us(uint64_t delay_us);
|
||||
void busy_wait_until(absolute_time_t t);
|
||||
bool time_reached(absolute_time_t t);
|
||||
typedef void (*hardware_alarm_callback_t)(uint alarm_num);
|
||||
void hardware_alarm_claim(uint alarm_num);
|
||||
void hardware_alarm_unclaim(uint alarm_num);
|
||||
void hardware_alarm_set_callback(uint alarm_num, hardware_alarm_callback_t callback);
|
||||
bool hardware_alarm_set_target(uint alarm_num, absolute_time_t t);
|
||||
void hardware_alarm_cancel(uint alarm_num);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
104
src/host/hardware_timer/timer.c
Normal file
104
src/host/hardware_timer/timer.c
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "hardware/timer.h"
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#endif
|
||||
|
||||
// in our case not a busy wait
|
||||
PICO_WEAK_FUNCTION_DEF(busy_wait_us)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_us_32)(uint32_t delay_us) {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
usleep(delay_us);
|
||||
#else
|
||||
assert(false);
|
||||
#endif
|
||||
}
|
||||
PICO_WEAK_FUNCTION_DEF(busy_wait_us)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_us)(uint64_t delay_us) {
|
||||
absolute_time_t t;
|
||||
update_us_since_boot(&t, time_us_64() + delay_us);
|
||||
busy_wait_until(t);
|
||||
}
|
||||
|
||||
// this may or may not wrap
|
||||
PICO_WEAK_FUNCTION_DEF(time_us_64)
|
||||
uint64_t PICO_WEAK_FUNCTION_IMPL_NAME(time_us_64)() {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
// struct timeval tv;
|
||||
// gettimeofday(&tv, NULL);
|
||||
// return tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec;
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.tv_sec * (uint64_t) 1000000 + ts.tv_nsec / 1000;
|
||||
#else
|
||||
panic_unsupported();
|
||||
#endif
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(timer_us_32)
|
||||
uint32_t PICO_WEAK_FUNCTION_IMPL_NAME(timer_us_32)() {
|
||||
return (uint32_t) time_us_64();
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(time_reached)
|
||||
bool PICO_WEAK_FUNCTION_IMPL_NAME(time_reached)(absolute_time_t t) {
|
||||
uint64_t target = to_us_since_boot(t);
|
||||
if (target > 0xffffffffu) return false;
|
||||
return time_us_64() >= target;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(busy_wait_until)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_until)(absolute_time_t target) {
|
||||
#if defined(__unix__)
|
||||
struct timespec tspec;
|
||||
tspec.tv_sec = to_us_since_boot(target) / 1000000;
|
||||
tspec.tv_nsec = (to_us_since_boot(target) % 1000000) * 1000;
|
||||
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tspec, NULL);
|
||||
#else
|
||||
const int chunk = 1u<<30u;
|
||||
uint64_t target_us = to_us_since_boot(target);
|
||||
uint64_t time_us = time_us_64();
|
||||
while (target_us - time_us >= chunk) {
|
||||
busy_wait_us_32(chunk);
|
||||
time_us = time_us_64();
|
||||
}
|
||||
if (target_us != time_us) {
|
||||
busy_wait_us_32(target_us - chunk);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint8_t claimed_alarms;
|
||||
|
||||
void hardware_alarm_claim(uint alarm_num) {
|
||||
assert(!(claimed_alarms & (1u << alarm_num)));
|
||||
claimed_alarms |= 1u <<alarm_num;
|
||||
}
|
||||
|
||||
void hardware_alarm_unclaim(uint alarm_num) {
|
||||
assert(claimed_alarms & (1u << alarm_num));
|
||||
claimed_alarms &= ~(1u <<alarm_num);
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(hardware_alarm_set_callback)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(hardware_alarm_set_callback)(uint alarm_num, hardware_alarm_callback_t callback) {
|
||||
panic_unsupported();
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(hardware_alarm_set_target)
|
||||
bool PICO_WEAK_FUNCTION_IMPL_NAME(hardware_alarm_set_target)(uint alarm_num, absolute_time_t target) {
|
||||
panic_unsupported();
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(hardware_alarm_cancel)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(hardware_alarm_cancel)(uint alarm_num) {
|
||||
panic_unsupported();
|
||||
}
|
Reference in New Issue
Block a user